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