1/*
2 * Copyright 2004, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "TextRequestDialog.h"
7#include "InterfaceUtils.h"
8
9#include <Button.h>
10#include <TextControl.h>
11#include <TextView.h>
12
13
14// GUI constants
15static const uint32 kWindowWidth = 250;
16static const uint32 kWindowHeight = 10 + 20 + 10 + 25 + 5;
17static const BRect kWindowRect(0, 0, kWindowWidth, kWindowHeight);
18static const uint32 kDefaultButtonWidth = 80;
19
20// message constants
21static const uint32 kMsgButton = 'MBTN';
22static const uint32 kMsgUpdateControls = 'UCTL';
23
24// labels
25static const char *kLabelOK = "OK";
26static const char *kLabelCancel = "Cancel";
27
28
29TextRequestDialog::TextRequestDialog(const char *title, const char *information,
30		const char *request, const char *text = NULL)
31	: BWindow(kWindowRect, title, B_MODAL_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE, 0),
32	fInvoker(NULL)
33{
34	BRect rect = Bounds();
35	BView *backgroundView = new BView(rect, "background", B_FOLLOW_ALL_SIDES, 0);
36	backgroundView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
37	rect.InsetBy(5, 5);
38	rect.bottom = rect.top;
39		// init
40
41	if(information) {
42		BRect textRect(rect);
43		textRect.OffsetTo(0, 0);
44		fTextView = new BTextView(rect, "TextView", textRect, B_FOLLOW_NONE,
45			B_WILL_DRAW);
46		fTextView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
47		fTextView->MakeSelectable(false);
48		fTextView->MakeEditable(false);
49		fTextView->SetText(information);
50		float textHeight = fTextView->TextHeight(0, fTextView->CountLines());
51		backgroundView->ResizeBy(0, textHeight + 5);
52		ResizeBy(0, textHeight + 5);
53		fTextView->ResizeBy(0, textHeight - textRect.Height());
54		rect.bottom += textHeight + 5;
55		backgroundView->AddChild(fTextView);
56	} else
57		fTextView = NULL;
58
59	rect.top = rect.bottom + 5;
60	rect.bottom = rect.top + 20;
61	fTextControl = new BTextControl(rect, "request", request, text, NULL);
62	fTextControl->SetModificationMessage(new BMessage(kMsgUpdateControls));
63	fTextControl->SetDivider(fTextControl->StringWidth(fTextControl->Label()) + 5);
64	if(text && strlen(text) > 0)
65		fTextControl->TextView()->SelectAll();
66
67	rect.top = rect.bottom + 10;
68	rect.bottom = rect.top + 25;
69	rect.left = rect.right - kDefaultButtonWidth;
70	BMessage message(kMsgButton);
71	message.AddInt32("which", 1);
72	fOKButton = new BButton(rect, "okButton", kLabelOK, new BMessage(message));
73	rect.right = rect.left - 10;
74	rect.left = rect.right - kDefaultButtonWidth;
75	message.ReplaceInt32("which", 0);
76	BButton *cancelButton = new BButton(rect, "cancelButton", kLabelCancel,
77		new BMessage(message));
78	backgroundView->AddChild(cancelButton);
79	backgroundView->AddChild(fOKButton);
80	backgroundView->AddChild(fTextControl);
81	AddChild(backgroundView);
82
83	fTextControl->MakeFocus(true);
84	SetDefaultButton(fOKButton);
85
86	UpdateControls();
87}
88
89
90TextRequestDialog::~TextRequestDialog()
91{
92	delete fInvoker;
93}
94
95
96void
97TextRequestDialog::MessageReceived(BMessage *message)
98{
99	switch(message->what) {
100		case kMsgButton: {
101			if(!fInvoker || !fInvoker->Message())
102				return;
103
104			int32 which;
105			message->FindInt32("which", &which);
106			BMessage toSend(*fInvoker->Message());
107			toSend.AddInt32("which", which);
108			if(which == 1)
109				toSend.AddString("text", fTextControl->Text());
110
111			fInvoker->Invoke(&toSend);
112			PostMessage(B_QUIT_REQUESTED);
113		} break;
114
115		case kMsgUpdateControls:
116			UpdateControls();
117		break;
118
119		default:
120			BWindow::MessageReceived(message);
121	}
122}
123
124
125bool
126TextRequestDialog::QuitRequested()
127{
128	return true;
129}
130
131
132status_t
133TextRequestDialog::Go(BInvoker *invoker)
134{
135	fInvoker = invoker;
136	MoveTo(center_on_screen(Bounds()));
137	Show();
138
139	return B_OK;
140}
141
142
143void
144TextRequestDialog::UpdateControls()
145{
146	fOKButton->SetEnabled(fTextControl->TextView()->TextLength() > 0);
147}
148