From 6bfaef2ba656b4fc87065fb2f56c7fbcb6358609 Mon Sep 17 00:00:00 2001 From: Rene Gollent Date: Sun, 4 Nov 2012 09:31:26 +0100 Subject: [PATCH] Add basic helper class for user input prompting. --- headers/private/shared/PromptWindow.h | 34 +++++++++++ src/kits/shared/Jamfile | 1 + src/kits/shared/PromptWindow.cpp | 81 +++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 headers/private/shared/PromptWindow.h create mode 100644 src/kits/shared/PromptWindow.cpp diff --git a/headers/private/shared/PromptWindow.h b/headers/private/shared/PromptWindow.h new file mode 100644 index 0000000000..80d376fd26 --- /dev/null +++ b/headers/private/shared/PromptWindow.h @@ -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 +#include + + +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_ diff --git a/src/kits/shared/Jamfile b/src/kits/shared/Jamfile index 8992caa24f..2459568ac7 100644 --- a/src/kits/shared/Jamfile +++ b/src/kits/shared/Jamfile @@ -26,6 +26,7 @@ StaticLibrary libshared.a : Keymap.cpp LongAndDragTrackingFilter.cpp NaturalCompare.cpp + PromptWindow.cpp QueryFile.cpp RWLockManager.cpp SHA256.cpp diff --git a/src/kits/shared/PromptWindow.cpp b/src/kits/shared/PromptWindow.cpp new file mode 100644 index 0000000000..71bb0e9efc --- /dev/null +++ b/src/kits/shared/PromptWindow.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2012, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ +#include "PromptWindow.h" + +#include +#include +#include +#include + + +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); +}