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