xref: /haiku/src/kits/shared/PromptWindow.cpp (revision b8ded2f89783a220c7b3019d48266a682cc79158)
1 /*
2  * Copyright 2012-2013, Rene Gollent, rene@gollent.com.
3  * Distributed under the terms of the MIT License.
4  */
5 #include "PromptWindow.h"
6 
7 #include <Button.h>
8 #include <Catalog.h>
9 #include <LayoutBuilder.h>
10 #include <StringView.h>
11 #include <TextControl.h>
12 
13 
14 static const uint32 kAcceptInput = 'acin';
15 
16 
PromptWindow(const char * title,const char * label,const char * info,BMessenger target,BMessage * message)17 PromptWindow::PromptWindow(const char* title, const char* label,
18 	const char* info, BMessenger target, BMessage* message)
19 	:
20 	BWindow(BRect(), title, B_FLOATING_WINDOW, B_NOT_RESIZABLE
21 			| B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
22 	fTarget(target),
23 	fMessage(message)
24 {
25 	fInfoView = new BStringView("info", info);
26 	fTextControl = new BTextControl("promptcontrol", label, NULL,
27 		new BMessage(kAcceptInput));
28 	BButton* cancelButton = new BButton("Cancel", new
29 		BMessage(B_QUIT_REQUESTED));
30 	BButton* acceptButton = new BButton("Accept", new
31 		BMessage(kAcceptInput));
32 	BLayoutBuilder::Group<>(this, B_VERTICAL)
33 		.SetInsets(B_USE_DEFAULT_SPACING)
34 		.Add(fInfoView)
35 		.Add(fTextControl)
36 		.AddGroup(B_HORIZONTAL)
37 			.AddGlue()
38 			.Add(cancelButton)
39 			.Add(acceptButton)
40 		.End()
41 	.End();
42 
43 	if (info == NULL)
44 		fInfoView->Hide();
45 
46 	fTextControl->TextView()->SetExplicitMinSize(BSize(200.0, B_SIZE_UNSET));
47 	fTextControl->SetTarget(this);
48 	acceptButton->SetTarget(this);
49 	cancelButton->SetTarget(this);
50 	fTextControl->MakeFocus(true);
51 
52 	SetDefaultButton(acceptButton);
53 }
54 
55 
~PromptWindow()56 PromptWindow::~PromptWindow()
57 {
58 	delete fMessage;
59 }
60 
61 
62 void
MessageReceived(BMessage * message)63 PromptWindow::MessageReceived(BMessage* message)
64 {
65 	switch (message->what)
66 	{
67 		case kAcceptInput:
68 		{
69 			fMessage->AddString("text", fTextControl->TextView()->Text());
70 			fTarget.SendMessage(fMessage);
71 			PostMessage(B_QUIT_REQUESTED);
72 		}
73 		default:
74 		{
75 			BWindow::MessageReceived(message);
76 			break;
77 		}
78 	}
79 }
80 
81 
82 status_t
SetTarget(BMessenger messenger)83 PromptWindow::SetTarget(BMessenger messenger)
84 {
85 	fTarget = messenger;
86 	return B_OK;
87 }
88 
89 
90 status_t
SetMessage(BMessage * message)91 PromptWindow::SetMessage(BMessage* message)
92 {
93 	delete fMessage;
94 	fMessage = message;
95 	return B_OK;
96 }
97