xref: /haiku/src/apps/launchbox/NamePanel.cpp (revision 76e9533e9e9945f62053bf7c737df2f3e1735bbd)
1 /*
2  * Copyright 2006-2009, 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 enum {
19 	MSG_PANEL_OK,
20 	MSG_PANEL_CANCEL,
21 };
22 
23 // constructor
24 NamePanel::NamePanel(const char* label, const char* text, BWindow* window,
25 		BHandler* target, BMessage* message, BRect frame)
26 	:
27 	Panel(frame, B_TRANSLATE("Name Panel"),
28 		B_MODAL_WINDOW_LOOK, B_MODAL_SUBSET_WINDOW_FEEL,
29 		B_ASYNCHRONOUS_CONTROLS | B_NOT_V_RESIZABLE
30 			| B_AUTO_UPDATE_SIZE_LIMITS),
31 	fWindow(window),
32 	fTarget(target),
33 	fMessage(message)
34 {
35 	BButton* defaultButton = new BButton(B_TRANSLATE("OK"),
36 		new BMessage(MSG_PANEL_OK));
37 	BButton* cancelButton = new BButton(B_TRANSLATE("Cancel"),
38 		new BMessage(MSG_PANEL_CANCEL));
39 	fNameTC = new BTextControl(label, text, NULL);
40 
41 	BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
42 		.AddGlue()
43 
44 		// controls
45 		.AddGroup(B_HORIZONTAL, 5)
46 			.AddStrut(5)
47 
48 			// text control
49 			.Add(fNameTC->CreateLabelLayoutItem())
50 			.Add(fNameTC->CreateTextViewLayoutItem())
51 			.AddStrut(5)
52 			.End()
53 
54 		.AddGlue()
55 
56 		// buttons
57 		.AddGroup(B_HORIZONTAL, 5)
58 			.AddGlue()
59 			.Add(cancelButton)
60 			.Add(defaultButton)
61 			.AddStrut(5)
62 			.End()
63 
64 		.AddGlue();
65 
66 	SetDefaultButton(defaultButton);
67 	fNameTC->MakeFocus(true);
68 
69 	if (fWindow && fWindow->Lock()) {
70 		fSavedTargetWindowFeel = fWindow->Feel();
71 		if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL)
72 			fWindow->SetFeel(B_NORMAL_WINDOW_FEEL);
73 		fWindow->Unlock();
74 	}
75 
76 	AddToSubset(fWindow);
77 
78 	if (!frame.IsValid())
79 		CenterOnScreen();
80 
81 	Show();
82 }
83 
84 
85 NamePanel::~NamePanel()
86 {
87 	if (fWindow && fWindow->Lock()) {
88 		fWindow->SetFeel(fSavedTargetWindowFeel);
89 		fWindow->Unlock();
90 	}
91 	delete fMessage;
92 }
93 
94 
95 void NamePanel::MessageReceived(BMessage* message)
96 {
97 	switch (message->what) {
98 		case MSG_PANEL_CANCEL:
99 			Quit();
100 			break;
101 		case MSG_PANEL_OK: {
102 			if (!fTarget)
103 				fTarget = fWindow;
104 			BLooper* looper = fTarget ? fTarget->Looper() : NULL;
105 			if (fMessage && looper) {
106 				BMessage cloneMessage(*fMessage);
107 				cloneMessage.AddString("name", fNameTC->Text());
108 				cloneMessage.AddRect("frame", Frame());
109 				looper->PostMessage(&cloneMessage, fTarget);
110 			}
111 			Quit();
112 			break;
113 		}
114 		default:
115 			Panel::MessageReceived(message);
116 	}
117 }
118