Add basic helper class for user input prompting.

This commit is contained in:
Rene Gollent 2012-11-04 09:31:26 +01:00
parent 674bc40589
commit 6bfaef2ba6
3 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#ifndef PROMPT_WINDOW_H_
#define PROMPT_WINDOW_H_
#include <Messenger.h>
#include <Window.h>
class BTextControl;
class PromptWindow : public BWindow
{
public:
// PromptWindow takes ownership of message
PromptWindow(const char* title, const char* label, BMessenger target,
BMessage* message = NULL);
~PromptWindow();
virtual void MessageReceived(BMessage* message);
status_t SetTarget(BMessenger messenger);
status_t SetMessage(BMessage* message);
private:
BTextControl* fTextControl;
BMessenger fTarget;
BMessage* fMessage;
};
#endif // PROMPT_WINDOW_H_

View File

@ -26,6 +26,7 @@ StaticLibrary libshared.a :
Keymap.cpp
LongAndDragTrackingFilter.cpp
NaturalCompare.cpp
PromptWindow.cpp
QueryFile.cpp
RWLockManager.cpp
SHA256.cpp

View File

@ -0,0 +1,81 @@
/*
* Copyright 2012, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "PromptWindow.h"
#include <Button.h>
#include <Catalog.h>
#include <LayoutBuilder.h>
#include <TextControl.h>
static const uint32 kAcceptInput = 'acin';
PromptWindow::PromptWindow(const char* title, const char* label,
BMessenger target, BMessage* message)
:
BWindow(BRect(), title, B_FLOATING_WINDOW, B_NOT_RESIZABLE
| B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
fTarget(target),
fMessage(message)
{
fTextControl = new BTextControl("promptcontrol", label, NULL,
new BMessage(kAcceptInput));
BButton* cancelButton = new BButton("Cancel", new
BMessage(B_QUIT_REQUESTED));
BButton* acceptButton = new BButton("Accept", new
BMessage(kAcceptInput));
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(fTextControl)
.AddGroup(B_HORIZONTAL)
.Add(acceptButton)
.Add(cancelButton);
fTextControl->TextView()->SetExplicitMinSize(BSize(
fTextControl->TextView()->StringWidth("1234567890"), B_SIZE_UNSET));
fTextControl->SetTarget(this);
acceptButton->SetTarget(this);
cancelButton->SetTarget(this);
}
PromptWindow::~PromptWindow()
{
delete fMessage;
}
void
PromptWindow::MessageReceived(BMessage* message)
{
switch (message->what)
{
case kAcceptInput:
{
fMessage->AddString("text", fTextControl->TextView()->Text());
fTarget.SendMessage(fMessage);
PostMessage(B_QUIT_REQUESTED);
}
default:
{
BWindow::MessageReceived(message);
break;
}
}
}
status_t
PromptWindow::SetTarget(BMessenger messenger)
{
return fTextControl->SetTarget(messenger);
}
status_t
PromptWindow::SetMessage(BMessage* message)
{
return fTextControl->SetMessage(message);
}