1 /*
2 * Copyright 2015, François Revol <revol@free.fr>
3 * Copyright 2009-2010, Stephan Aßmus <superstippi@gmx.de>
4 * Copyright 2009, Bryce Groff, brycegroff@gmail.com.
5 * Distributed under the terms of the MIT License.
6 */
7
8
9 #include <stdio.h>
10
11 #include "InitializeParameterEditor.h"
12
13 #include <Button.h>
14 #include <Catalog.h>
15 #include <CheckBox.h>
16 #include <ControlLook.h>
17 #include <GridLayoutBuilder.h>
18 #include <MenuField.h>
19 #include <MenuItem.h>
20 #include <Partition.h>
21 #include <PopUpMenu.h>
22 #include <SpaceLayoutItem.h>
23 #include <TextControl.h>
24 #include <Variant.h>
25 #include <View.h>
26 #include <Window.h>
27
28
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "FAT_Initialize_Parameter"
31
32
33 static uint32 MSG_NAME_CHANGED = 'nmch';
34 static uint32 MSG_FAT_BITS = 'fatb';
35
InitializeFATEditor()36 InitializeFATEditor::InitializeFATEditor()
37 :
38 BPartitionParameterEditor(),
39 fView(NULL),
40 fNameControl(NULL),
41 fFatBitsMenuField(NULL),
42 fParameters(NULL)
43 {
44 _CreateViewControls();
45 }
46
47
~InitializeFATEditor()48 InitializeFATEditor::~InitializeFATEditor()
49 {
50 }
51
52
53 void
SetTo(BPartition * partition)54 InitializeFATEditor::SetTo(BPartition* partition)
55 {
56 BString name = partition->Name();
57 if (name.IsEmpty())
58 name = partition->RawContentName();
59 if (!name.IsEmpty())
60 fNameControl->SetText(name.String());
61 }
62
63
64 BView*
View()65 InitializeFATEditor::View()
66 {
67 return fView;
68 }
69
70
71 bool
ValidateParameters() const72 InitializeFATEditor::ValidateParameters() const
73 {
74 // The name must be set
75 return fNameControl->TextView()->TextLength() > 0;
76 }
77
78
79 status_t
ParameterChanged(const char * name,const BVariant & variant)80 InitializeFATEditor::ParameterChanged(const char* name,
81 const BVariant& variant)
82 {
83 if (!strcmp(name, "name"))
84 fNameControl->SetText(variant.ToString());
85 return B_OK;
86 }
87
88
89 status_t
GetParameters(BString & parameters)90 InitializeFATEditor::GetParameters(BString& parameters)
91 {
92 parameters = "";
93
94 if (BMenuItem* item = fFatBitsMenuField->Menu()->FindMarked()) {
95 const char* size;
96 BMessage* message = item->Message();
97 if (!message || message->FindString("fat", &size) < B_OK)
98 size = "0"; // autodetect
99 // TODO: use libroot driver settings API
100 parameters << "fat " << size << ";\n";
101 }
102
103 parameters << "name \"";
104 parameters << fNameControl->Text() << "\";\n";
105 printf("p:%s", parameters.String());
106 return B_OK;
107 }
108
109
110 void
_CreateViewControls()111 InitializeFATEditor::_CreateViewControls()
112 {
113 fNameControl = new BTextControl(B_TRANSLATE("Name:"), "New FAT Vol",
114 NULL);
115 fNameControl->SetModificationMessage(new BMessage(MSG_NAME_CHANGED));
116 // TODO find out what is the max length for this specific FS partition name
117 fNameControl->TextView()->SetMaxBytes(11);
118
119 BPopUpMenu* fatBitsMenu = new BPopUpMenu("fat size");
120 BMessage* message = new BMessage(MSG_FAT_BITS);
121 message->AddString("fat", "0");
122 BMenuItem* defaultItem = new BMenuItem(
123 B_TRANSLATE("Auto (default)"), message);
124 fatBitsMenu->AddItem(defaultItem);
125 message = new BMessage(MSG_FAT_BITS);
126 message->AddString("fat", "12");
127 fatBitsMenu->AddItem(new BMenuItem("12", message));
128 message = new BMessage(MSG_FAT_BITS);
129 message->AddString("fat", "16");
130 fatBitsMenu->AddItem(new BMenuItem("16", message));
131 message = new BMessage(MSG_FAT_BITS);
132 message->AddString("fat", "32");
133 fatBitsMenu->AddItem(new BMenuItem("32", message));
134
135 fFatBitsMenuField = new BMenuField(B_TRANSLATE("FAT bits:"),
136 fatBitsMenu);
137 defaultItem->SetMarked(true);
138
139 float spacing = be_control_look->DefaultItemSpacing();
140
141 fView = BGridLayoutBuilder(spacing, spacing)
142 // row 1
143 .Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
144 .Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
145
146 // row 2
147 .Add(fFatBitsMenuField->CreateLabelLayoutItem(), 0, 1)
148 .Add(fFatBitsMenuField->CreateMenuBarLayoutItem(), 1, 1).View()
149 ;
150 }
151