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