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