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