xref: /haiku/src/kits/shared/PromptWindow.cpp (revision 2510baa4685f8f570c607ceedfd73473d69342c4)
1 /*
2  * Copyright 2012, 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 <TextControl.h>
11 
12 
13 static const uint32 kAcceptInput = 'acin';
14 
15 
16 PromptWindow::PromptWindow(const char* title, const char* label,
17 	BMessenger target, BMessage* message)
18 	:
19 	BWindow(BRect(), title, B_FLOATING_WINDOW, B_NOT_RESIZABLE
20 			| B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
21 	fTarget(target),
22 	fMessage(message)
23 {
24 	fTextControl = new BTextControl("promptcontrol", label, NULL,
25 		new BMessage(kAcceptInput));
26 	BButton* cancelButton = new BButton("Cancel", new
27 		BMessage(B_QUIT_REQUESTED));
28 	BButton* acceptButton = new BButton("Accept", new
29 		BMessage(kAcceptInput));
30 	BLayoutBuilder::Group<>(this, B_VERTICAL)
31 		.Add(fTextControl)
32 		.AddGroup(B_HORIZONTAL)
33 			.Add(acceptButton)
34 			.Add(cancelButton);
35 
36 	fTextControl->TextView()->SetExplicitMinSize(BSize(
37 			fTextControl->TextView()->StringWidth("1234567890"), B_SIZE_UNSET));
38 	fTextControl->SetTarget(this);
39 	acceptButton->SetTarget(this);
40 	cancelButton->SetTarget(this);
41 	fTextControl->MakeFocus(true);
42 }
43 
44 
45 PromptWindow::~PromptWindow()
46 {
47 	delete fMessage;
48 }
49 
50 
51 void
52 PromptWindow::MessageReceived(BMessage* message)
53 {
54 	switch (message->what)
55 	{
56 		case kAcceptInput:
57 		{
58 			fMessage->AddString("text", fTextControl->TextView()->Text());
59 			fTarget.SendMessage(fMessage);
60 			PostMessage(B_QUIT_REQUESTED);
61 		}
62 		default:
63 		{
64 			BWindow::MessageReceived(message);
65 			break;
66 		}
67 	}
68 }
69 
70 
71 status_t
72 PromptWindow::SetTarget(BMessenger messenger)
73 {
74 	fTarget = messenger;
75 	return B_OK;
76 }
77 
78 
79 status_t
80 PromptWindow::SetMessage(BMessage* message)
81 {
82 	delete fMessage;
83 	fMessage = message;
84 	return B_OK;
85 }
86