xref: /haiku/src/apps/launchbox/NamePanel.cpp (revision 4e3137c085bae361922078f123dceb92da700640)
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_TRANSLATION_CONTEXT
17 #define B_TRANSLATION_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 	fSavedTargetWindowFeel(B_NORMAL_WINDOW_FEEL)
37 {
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));
47 
48 	BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
49 		.AddGlue()
50 
51 		// controls
52 		.AddGroup(B_HORIZONTAL, 5)
53 			.AddStrut(5)
54 
55 			// text control
56 			.Add(fNameTC->CreateLabelLayoutItem())
57 			.Add(inputItem)
58 			.AddStrut(5)
59 			.End()
60 
61 		.AddGlue()
62 
63 		// buttons
64 		.AddGroup(B_HORIZONTAL, 5)
65 			.AddGlue()
66 			.Add(cancelButton)
67 			.Add(defaultButton)
68 			.AddStrut(5)
69 			.End()
70 
71 		.AddGlue();
72 
73 	SetDefaultButton(defaultButton);
74 	fNameTC->MakeFocus(true);
75 
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();
81 	}
82 
83 	AddToSubset(fWindow);
84 }
85 
86 
87 NamePanel::~NamePanel()
88 {
89 	if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL
90 		&& fWindow != NULL && fWindow->Lock()) {
91 		fWindow->SetFeel(fSavedTargetWindowFeel);
92 		fWindow->Unlock();
93 	}
94 	delete fMessage;
95 }
96 
97 
98 void NamePanel::MessageReceived(BMessage* message)
99 {
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);
113 			}
114 			Quit();
115 			break;
116 		}
117 		default:
118 			Panel::MessageReceived(message);
119 	}
120 }
121