xref: /haiku/src/apps/drivesetup/ChangeParametersPanel.cpp (revision 4a850ca730d8282b5b924e49e09b4ba4d6db7f54)
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, partition->Parent());
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* partition,
113 	BPartition* parent)
114 {
115 	const char* name = "";
116 	if (partition != NULL)
117 		name = partition->Name();
118 
119 	fNameTextControl = new BTextControl("Name Control",
120 		B_TRANSLATE("Partition name:"), name, NULL);
121 	if (partition != NULL)
122 		fSupportsName = partition->CanSetName();
123 	else if (parent != NULL)
124 		fSupportsName = parent->SupportsChildName();
125 
126 	fTypePopUpMenu = new BPopUpMenu("Partition Type");
127 
128 	int32 cookie = 0;
129 	BString supportedType;
130 	if (parent != NULL) {
131 		while (parent->GetNextSupportedChildType(&cookie, &supportedType)
132 			== B_OK) {
133 			BMessage* message = new BMessage(MSG_PARTITION_TYPE);
134 			message->AddString("type", supportedType);
135 			BMenuItem* item = new BMenuItem(supportedType, message);
136 			fTypePopUpMenu->AddItem(item);
137 
138 			if (strcmp(supportedType, kPartitionTypeBFS) == 0)
139 				item->SetMarked(true);
140 		}
141 	}
142 
143 	fTypeMenuField = new BMenuField(B_TRANSLATE("Partition type:"),
144 		fTypePopUpMenu);
145 	fSupportsType = fTypePopUpMenu->CountItems() != 0;
146 
147 	fOkButton->SetLabel(B_TRANSLATE("Change"));
148 }
149 
150 
151 bool
152 ChangeParametersPanel::NeedsEditor() const
153 {
154 	return !fSupportsName && !fSupportsType;
155 }
156 
157 
158 bool
159 ChangeParametersPanel::ValidWithoutEditor() const
160 {
161 	return !NeedsEditor();
162 }
163 
164 
165 status_t
166 ChangeParametersPanel::ParametersReceived(const BString& parameters,
167 	BMessage& storage)
168 {
169 	// get name
170 	status_t status = storage.SetString("name", fNameTextControl->Text());
171 
172 	// get type
173 	if (BMenuItem* item = fTypeMenuField->Menu()->FindMarked()) {
174 		const char* type;
175 		BMessage* message = item->Message();
176 		if (!message || message->FindString("type", &type) != B_OK)
177 			type = kPartitionTypeBFS;
178 
179 		if (status == B_OK)
180 			status = storage.SetString("type", type);
181 	}
182 
183 	return status;
184 }
185 
186 
187 void
188 ChangeParametersPanel::AddControls(BLayoutBuilder::Group<>& builder,
189 	BView* editorView)
190 {
191 	if (fSupportsName || fSupportsType) {
192 		BLayoutBuilder::Group<>::GridBuilder gridBuilder
193 			= builder.AddGrid(0.0, B_USE_DEFAULT_SPACING);
194 
195 		if (fSupportsName) {
196 			gridBuilder.Add(fNameTextControl->CreateLabelLayoutItem(), 0, 0)
197 				.Add(fNameTextControl->CreateTextViewLayoutItem(), 1, 0);
198 		}
199 		if (fSupportsType) {
200 			gridBuilder.Add(fTypeMenuField->CreateLabelLayoutItem(), 0, 1)
201 				.Add(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
202 		}
203 	}
204 
205 	if (editorView != NULL)
206 		builder.Add(editorView);
207 }
208