16bfaef2bSRene Gollent /*
2aa366c07SRene Gollent * Copyright 2012-2013, Rene Gollent, rene@gollent.com.
36bfaef2bSRene Gollent * Distributed under the terms of the MIT License.
46bfaef2bSRene Gollent */
56bfaef2bSRene Gollent #include "PromptWindow.h"
66bfaef2bSRene Gollent
76bfaef2bSRene Gollent #include <Button.h>
86bfaef2bSRene Gollent #include <Catalog.h>
96bfaef2bSRene Gollent #include <LayoutBuilder.h>
10aa366c07SRene Gollent #include <StringView.h>
116bfaef2bSRene Gollent #include <TextControl.h>
126bfaef2bSRene Gollent
136bfaef2bSRene Gollent
146bfaef2bSRene Gollent static const uint32 kAcceptInput = 'acin';
156bfaef2bSRene Gollent
166bfaef2bSRene Gollent
PromptWindow(const char * title,const char * label,const char * info,BMessenger target,BMessage * message)176bfaef2bSRene Gollent PromptWindow::PromptWindow(const char* title, const char* label,
18aa366c07SRene Gollent const char* info, BMessenger target, BMessage* message)
196bfaef2bSRene Gollent :
206bfaef2bSRene Gollent BWindow(BRect(), title, B_FLOATING_WINDOW, B_NOT_RESIZABLE
216bfaef2bSRene Gollent | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
226bfaef2bSRene Gollent fTarget(target),
236bfaef2bSRene Gollent fMessage(message)
246bfaef2bSRene Gollent {
25aa366c07SRene Gollent fInfoView = new BStringView("info", info);
266bfaef2bSRene Gollent fTextControl = new BTextControl("promptcontrol", label, NULL,
276bfaef2bSRene Gollent new BMessage(kAcceptInput));
286bfaef2bSRene Gollent BButton* cancelButton = new BButton("Cancel", new
296bfaef2bSRene Gollent BMessage(B_QUIT_REQUESTED));
306bfaef2bSRene Gollent BButton* acceptButton = new BButton("Accept", new
316bfaef2bSRene Gollent BMessage(kAcceptInput));
326bfaef2bSRene Gollent BLayoutBuilder::Group<>(this, B_VERTICAL)
33*447f975cSRene Gollent .SetInsets(B_USE_DEFAULT_SPACING)
34aa366c07SRene Gollent .Add(fInfoView)
356bfaef2bSRene Gollent .Add(fTextControl)
366bfaef2bSRene Gollent .AddGroup(B_HORIZONTAL)
37309f473cSRene Gollent .AddGlue()
38309f473cSRene Gollent .Add(cancelButton)
396bfaef2bSRene Gollent .Add(acceptButton)
40309f473cSRene Gollent .End()
41309f473cSRene Gollent .End();
426bfaef2bSRene Gollent
43aa366c07SRene Gollent if (info == NULL)
44aa366c07SRene Gollent fInfoView->Hide();
45aa366c07SRene Gollent
46309f473cSRene Gollent fTextControl->TextView()->SetExplicitMinSize(BSize(200.0, B_SIZE_UNSET));
476bfaef2bSRene Gollent fTextControl->SetTarget(this);
486bfaef2bSRene Gollent acceptButton->SetTarget(this);
496bfaef2bSRene Gollent cancelButton->SetTarget(this);
50862696d9SRene Gollent fTextControl->MakeFocus(true);
51309f473cSRene Gollent
52309f473cSRene Gollent SetDefaultButton(acceptButton);
536bfaef2bSRene Gollent }
546bfaef2bSRene Gollent
556bfaef2bSRene Gollent
~PromptWindow()566bfaef2bSRene Gollent PromptWindow::~PromptWindow()
576bfaef2bSRene Gollent {
586bfaef2bSRene Gollent delete fMessage;
596bfaef2bSRene Gollent }
606bfaef2bSRene Gollent
616bfaef2bSRene Gollent
626bfaef2bSRene Gollent void
MessageReceived(BMessage * message)636bfaef2bSRene Gollent PromptWindow::MessageReceived(BMessage* message)
646bfaef2bSRene Gollent {
656bfaef2bSRene Gollent switch (message->what)
666bfaef2bSRene Gollent {
676bfaef2bSRene Gollent case kAcceptInput:
686bfaef2bSRene Gollent {
696bfaef2bSRene Gollent fMessage->AddString("text", fTextControl->TextView()->Text());
706bfaef2bSRene Gollent fTarget.SendMessage(fMessage);
716bfaef2bSRene Gollent PostMessage(B_QUIT_REQUESTED);
726bfaef2bSRene Gollent }
736bfaef2bSRene Gollent default:
746bfaef2bSRene Gollent {
756bfaef2bSRene Gollent BWindow::MessageReceived(message);
766bfaef2bSRene Gollent break;
776bfaef2bSRene Gollent }
786bfaef2bSRene Gollent }
796bfaef2bSRene Gollent }
806bfaef2bSRene Gollent
816bfaef2bSRene Gollent
826bfaef2bSRene Gollent status_t
SetTarget(BMessenger messenger)836bfaef2bSRene Gollent PromptWindow::SetTarget(BMessenger messenger)
846bfaef2bSRene Gollent {
85862696d9SRene Gollent fTarget = messenger;
86862696d9SRene Gollent return B_OK;
876bfaef2bSRene Gollent }
886bfaef2bSRene Gollent
896bfaef2bSRene Gollent
906bfaef2bSRene Gollent status_t
SetMessage(BMessage * message)916bfaef2bSRene Gollent PromptWindow::SetMessage(BMessage* message)
926bfaef2bSRene Gollent {
93862696d9SRene Gollent delete fMessage;
94862696d9SRene Gollent fMessage = message;
95862696d9SRene Gollent return B_OK;
966bfaef2bSRene Gollent }
97