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