xref: /haiku/src/apps/drivesetup/ChangeParametersPanel.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2008-2013 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Axel Dörfler, axeld@pinc-software.de.
8  *		Bryce Groff	<bgroff@hawaii.edu>
9  *		Karsten Heimrich <host.haiku@gmx.de>
10  */
11 
12 
13 #include "ChangeParametersPanel.h"
14 
15 #include <Button.h>
16 #include <Catalog.h>
17 #include <ControlLook.h>
18 #include <DiskDeviceTypes.h>
19 #include <MenuField.h>
20 #include <MenuItem.h>
21 #include <MessageFilter.h>
22 #include <PopUpMenu.h>
23 #include <String.h>
24 #include <TextControl.h>
25 #include <Variant.h>
26 
27 #include "Support.h"
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "ChangeParametersPanel"
32 
33 
34 enum {
35 	MSG_PARTITION_TYPE			= 'type',
36 };
37 
38 
39 ChangeParametersPanel::ChangeParametersPanel(BWindow* window,
40 	BPartition* partition)
41 	:
42 	AbstractParametersPanel(window)
43 {
44 	CreateChangeControls(partition);
45 
46 	Init(B_PROPERTIES_PARAMETER_EDITOR, "", partition);
47 }
48 
49 
50 ChangeParametersPanel::~ChangeParametersPanel()
51 {
52 }
53 
54 
55 status_t
56 ChangeParametersPanel::Go(BString& name, BString& type, BString& parameters)
57 {
58 	BMessage storage;
59 	return Go(name, type, parameters, storage);
60 }
61 
62 
63 void
64 ChangeParametersPanel::MessageReceived(BMessage* message)
65 {
66 	switch (message->what) {
67 		case MSG_PARTITION_TYPE:
68 			if (fEditor != NULL) {
69 				const char* type;
70 				if (message->FindString("type", &type) == B_OK)
71 					fEditor->ParameterChanged("type", BVariant(type));
72 			}
73 			break;
74 
75 		default:
76 			AbstractParametersPanel::MessageReceived(message);
77 	}
78 }
79 
80 
81 // #pragma mark - protected
82 
83 
84 ChangeParametersPanel::ChangeParametersPanel(BWindow* window)
85 	:
86 	AbstractParametersPanel(window)
87 {
88 }
89 
90 
91 status_t
92 ChangeParametersPanel::Go(BString& name, BString& type, BString& parameters,
93 	BMessage& storage)
94 {
95 	// The object will be deleted in Go(), so we need to get the values via
96 	// a BMessage
97 	status_t status = AbstractParametersPanel::Go(parameters, storage);
98 	if (status != B_OK)
99 		return status;
100 
101 	// get name
102 	name.SetTo(storage.GetString("name", NULL));
103 
104 	// get type
105 	type.SetTo(storage.GetString("type", NULL));
106 
107 	return B_OK;
108 }
109 
110 
111 void
112 ChangeParametersPanel::CreateChangeControls(BPartition* parent)
113 {
114 	fNameTextControl = new BTextControl("Name Control",
115 		B_TRANSLATE("Partition name:"),	"", NULL);
116 	fSupportsName = parent->SupportsChildName();
117 
118 	fTypePopUpMenu = new BPopUpMenu("Partition Type");
119 
120 	int32 cookie = 0;
121 	BString supportedType;
122 	while (parent->GetNextSupportedChildType(&cookie, &supportedType) == B_OK) {
123 		BMessage* message = new BMessage(MSG_PARTITION_TYPE);
124 		message->AddString("type", supportedType);
125 		BMenuItem* item = new BMenuItem(supportedType, message);
126 		fTypePopUpMenu->AddItem(item);
127 
128 		if (strcmp(supportedType, kPartitionTypeBFS) == 0)
129 			item->SetMarked(true);
130 	}
131 
132 	fTypeMenuField = new BMenuField(B_TRANSLATE("Partition type:"),
133 		fTypePopUpMenu);
134 	fSupportsType = fTypePopUpMenu->CountItems() != 0;
135 
136 	fOkButton->SetLabel(B_TRANSLATE("Change"));
137 }
138 
139 
140 bool
141 ChangeParametersPanel::NeedsEditor() const
142 {
143 	return !fSupportsName && !fSupportsType;
144 }
145 
146 
147 bool
148 ChangeParametersPanel::ValidWithoutEditor() const
149 {
150 	return !NeedsEditor();
151 }
152 
153 
154 status_t
155 ChangeParametersPanel::ParametersReceived(const BString& parameters,
156 	BMessage& storage)
157 {
158 	// get name
159 	status_t status = storage.SetString("name", fNameTextControl->Text());
160 
161 	// get type
162 	if (BMenuItem* item = fTypeMenuField->Menu()->FindMarked()) {
163 		const char* type;
164 		BMessage* message = item->Message();
165 		if (!message || message->FindString("type", &type) != B_OK)
166 			type = kPartitionTypeBFS;
167 
168 		if (status == B_OK)
169 			status = storage.SetString("type", type);
170 	}
171 
172 	return status;
173 }
174 
175 
176 void
177 ChangeParametersPanel::AddControls(BLayoutBuilder::Group<>& builder,
178 	BView* editorView)
179 {
180 	if (fSupportsName || fSupportsType) {
181 		BLayoutBuilder::Group<>::GridBuilder gridBuilder
182 			= builder.AddGrid(0.0, B_USE_DEFAULT_SPACING);
183 
184 		if (fSupportsName) {
185 			gridBuilder.Add(fNameTextControl->CreateLabelLayoutItem(), 0, 0)
186 				.Add(fNameTextControl->CreateTextViewLayoutItem(), 1, 0);
187 		}
188 		if (fSupportsType) {
189 			gridBuilder.Add(fTypeMenuField->CreateLabelLayoutItem(), 0, 1)
190 				.Add(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
191 		}
192 	}
193 
194 	if (editorView != NULL)
195 		builder.Add(editorView);
196 }
197