xref: /haiku/src/apps/launchbox/NamePanel.cpp (revision fccd8899fcb583bfb73c5c26c9fcd714b963959b)
1 /*
2  * Copyright 2006-2011, Stephan Aßmus <superstippi@gmx.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdio.h>
7 
8 #include <Button.h>
9 #include <Catalog.h>
10 #include <LayoutBuilder.h>
11 #include <Screen.h>
12 #include <TextControl.h>
13 
14 #include "NamePanel.h"
15 
16 #undef B_TRANSLATE_CONTEXT
17 #define B_TRANSLATE_CONTEXT "LaunchBox"
18 
19 
20 enum {
21 	MSG_PANEL_OK,
22 	MSG_PANEL_CANCEL,
23 };
24 
25 
26 NamePanel::NamePanel(const char* label, const char* text, BWindow* window,
27 		BHandler* target, BMessage* message, const BSize& size)
28 	:
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 {
37 	BButton* defaultButton = new BButton(B_TRANSLATE("OK"),
38 		new BMessage(MSG_PANEL_OK));
39 	BButton* cancelButton = new BButton(B_TRANSLATE("Cancel"),
40 		new BMessage(MSG_PANEL_CANCEL));
41 	fNameTC = new BTextControl(label, text, NULL);
42 	BLayoutItem* inputItem = fNameTC->CreateTextViewLayoutItem();
43 	inputItem->SetExplicitMinSize(
44 		BSize(fNameTC->StringWidth("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
45 			B_SIZE_UNSET));
46 
47 	BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
48 		.AddGlue()
49 
50 		// controls
51 		.AddGroup(B_HORIZONTAL, 5)
52 			.AddStrut(5)
53 
54 			// text control
55 			.Add(fNameTC->CreateLabelLayoutItem())
56 			.Add(inputItem)
57 			.AddStrut(5)
58 			.End()
59 
60 		.AddGlue()
61 
62 		// buttons
63 		.AddGroup(B_HORIZONTAL, 5)
64 			.AddGlue()
65 			.Add(cancelButton)
66 			.Add(defaultButton)
67 			.AddStrut(5)
68 			.End()
69 
70 		.AddGlue();
71 
72 	SetDefaultButton(defaultButton);
73 	fNameTC->MakeFocus(true);
74 
75 	if (fWindow && fWindow->Lock()) {
76 		fSavedTargetWindowFeel = fWindow->Feel();
77 		if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL)
78 			fWindow->SetFeel(B_NORMAL_WINDOW_FEEL);
79 		fWindow->Unlock();
80 	}
81 
82 	AddToSubset(fWindow);
83 }
84 
85 
86 NamePanel::~NamePanel()
87 {
88 	if (fWindow && fWindow->Lock()) {
89 		fWindow->SetFeel(fSavedTargetWindowFeel);
90 		fWindow->Unlock();
91 	}
92 	delete fMessage;
93 }
94 
95 
96 void NamePanel::MessageReceived(BMessage* message)
97 {
98 	switch (message->what) {
99 		case MSG_PANEL_CANCEL:
100 			Quit();
101 			break;
102 		case MSG_PANEL_OK: {
103 			if (!fTarget)
104 				fTarget = fWindow;
105 			BLooper* looper = fTarget ? fTarget->Looper() : NULL;
106 			if (fMessage && looper) {
107 				BMessage cloneMessage(*fMessage);
108 				cloneMessage.AddString("name", fNameTC->Text());
109 				cloneMessage.AddRect("frame", Frame());
110 				looper->PostMessage(&cloneMessage, fTarget);
111 			}
112 			Quit();
113 			break;
114 		}
115 		default:
116 			Panel::MessageReceived(message);
117 	}
118 }
119