vfs: check userland buffers before reading them.
[haiku.git] / src / apps / launchbox / NamePanel.cpp
blob053aae349e39d0f47ffb600d2174c2a354b8c4f1
1 /*
2 * Copyright 2006-2011, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <stdio.h>
8 #include <Button.h>
9 #include <Catalog.h>
10 #include <LayoutBuilder.h>
11 #include <Screen.h>
12 #include <TextControl.h>
14 #include "NamePanel.h"
16 #undef B_TRANSLATION_CONTEXT
17 #define B_TRANSLATION_CONTEXT "LaunchBox"
20 enum {
21 MSG_PANEL_OK,
22 MSG_PANEL_CANCEL,
26 NamePanel::NamePanel(const char* label, const char* text, BWindow* window,
27 BHandler* target, BMessage* message, const BSize& size)
29 Panel(BRect(B_ORIGIN, size), B_TRANSLATE("Name Panel"),
30 B_MODAL_WINDOW_LOOK, B_MODAL_SUBSET_WINDOW_FEEL,
31 B_ASYNCHRONOUS_CONTROLS | B_NOT_V_RESIZABLE
32 | B_AUTO_UPDATE_SIZE_LIMITS),
33 fWindow(window),
34 fTarget(target),
35 fMessage(message),
36 fSavedTargetWindowFeel(B_NORMAL_WINDOW_FEEL)
38 BButton* defaultButton = new BButton(B_TRANSLATE("OK"),
39 new BMessage(MSG_PANEL_OK));
40 BButton* cancelButton = new BButton(B_TRANSLATE("Cancel"),
41 new BMessage(MSG_PANEL_CANCEL));
42 fNameTC = new BTextControl(label, text, NULL);
43 BLayoutItem* inputItem = fNameTC->CreateTextViewLayoutItem();
44 inputItem->SetExplicitMinSize(
45 BSize(fNameTC->StringWidth("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
46 B_SIZE_UNSET));
48 BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
49 .AddGlue()
51 // controls
52 .AddGroup(B_HORIZONTAL, 5)
53 .AddStrut(5)
55 // text control
56 .Add(fNameTC->CreateLabelLayoutItem())
57 .Add(inputItem)
58 .AddStrut(5)
59 .End()
61 .AddGlue()
63 // buttons
64 .AddGroup(B_HORIZONTAL, 5)
65 .AddGlue()
66 .Add(cancelButton)
67 .Add(defaultButton)
68 .AddStrut(5)
69 .End()
71 .AddGlue();
73 SetDefaultButton(defaultButton);
74 fNameTC->MakeFocus(true);
76 if (fWindow != NULL && fWindow->Lock()) {
77 fSavedTargetWindowFeel = fWindow->Feel();
78 if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL)
79 fWindow->SetFeel(B_NORMAL_WINDOW_FEEL);
80 fWindow->Unlock();
83 AddToSubset(fWindow);
87 NamePanel::~NamePanel()
89 if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL
90 && fWindow != NULL && fWindow->Lock()) {
91 fWindow->SetFeel(fSavedTargetWindowFeel);
92 fWindow->Unlock();
94 delete fMessage;
98 void NamePanel::MessageReceived(BMessage* message)
100 switch (message->what) {
101 case MSG_PANEL_CANCEL:
102 Quit();
103 break;
104 case MSG_PANEL_OK: {
105 if (!fTarget)
106 fTarget = fWindow;
107 BLooper* looper = fTarget ? fTarget->Looper() : NULL;
108 if (fMessage && looper) {
109 BMessage cloneMessage(*fMessage);
110 cloneMessage.AddString("name", fNameTC->Text());
111 cloneMessage.AddRect("frame", Frame());
112 looper->PostMessage(&cloneMessage, fTarget);
114 Quit();
115 break;
117 default:
118 Panel::MessageReceived(message);