xref: /haiku/src/add-ons/disk_systems/fat/InitializeParameterEditor.cpp (revision 385ee03ba83b7a40d315e17b03031b3ca37820c0)
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 
36 InitializeFATEditor::InitializeFATEditor()
37 	:
38 	BPartitionParameterEditor(),
39 	fView(NULL),
40 	fNameControl(NULL),
41 	fFatBitsMenuField(NULL),
42 	fParameters(NULL)
43 {
44 	_CreateViewControls();
45 }
46 
47 
48 InitializeFATEditor::~InitializeFATEditor()
49 {
50 }
51 
52 
53 void
54 InitializeFATEditor::SetTo(BPartition* partition)
55 {
56 	BString name = partition->ContentName();
57 	if (!name.IsEmpty())
58 		fNameControl->SetText(name.String());
59 }
60 
61 
62 BView*
63 InitializeFATEditor::View()
64 {
65 	return fView;
66 }
67 
68 
69 bool
70 InitializeFATEditor::ValidateParameters() const
71 {
72 	// The name must be set
73 	return fNameControl->TextView()->TextLength() > 0;
74 }
75 
76 
77 status_t
78 InitializeFATEditor::ParameterChanged(const char* name,
79 	const BVariant& variant)
80 {
81 	if (!strcmp(name, "name"))
82 		fNameControl->SetText(variant.ToString());
83 	return B_OK;
84 }
85 
86 
87 status_t
88 InitializeFATEditor::GetParameters(BString& parameters)
89 {
90 	parameters = "";
91 
92 	if (BMenuItem* item = fFatBitsMenuField->Menu()->FindMarked()) {
93 		const char* size;
94 		BMessage* message = item->Message();
95 		if (!message || message->FindString("fat", &size) < B_OK)
96 			size = "0"; // autodetect
97 		// TODO: use libroot driver settings API
98 		parameters << "fat " << size << ";\n";
99 	}
100 
101 	parameters << "name \"";
102 	parameters << fNameControl->Text() << "\";\n";
103 printf("p:%s", parameters.String());
104 	return B_OK;
105 }
106 
107 
108 void
109 InitializeFATEditor::_CreateViewControls()
110 {
111 	fNameControl = new BTextControl(B_TRANSLATE("Name:"), "New FAT Vol",
112 		NULL);
113 	fNameControl->SetModificationMessage(new BMessage(MSG_NAME_CHANGED));
114 	// TODO find out what is the max length for this specific FS partition name
115 	fNameControl->TextView()->SetMaxBytes(11);
116 
117 	BPopUpMenu* fatBitsMenu = new BPopUpMenu("fat size");
118 	BMessage* message = new BMessage(MSG_FAT_BITS);
119 	message->AddString("fat", "0");
120 	BMenuItem* defaultItem = new BMenuItem(
121 		B_TRANSLATE("Auto (default)"), message);
122 	fatBitsMenu->AddItem(defaultItem);
123 	message = new BMessage(MSG_FAT_BITS);
124 	message->AddString("fat", "12");
125 	fatBitsMenu->AddItem(new BMenuItem("12", message));
126 	message = new BMessage(MSG_FAT_BITS);
127 	message->AddString("fat", "16");
128 	fatBitsMenu->AddItem(new BMenuItem("16", message));
129 	message = new BMessage(MSG_FAT_BITS);
130 	message->AddString("fat", "32");
131 	fatBitsMenu->AddItem(new BMenuItem("32", message));
132 
133 	fFatBitsMenuField = new BMenuField(B_TRANSLATE("FAT bits:"),
134 		fatBitsMenu);
135 	defaultItem->SetMarked(true);
136 
137 	float spacing = be_control_look->DefaultItemSpacing();
138 
139 	fView = BGridLayoutBuilder(spacing, spacing)
140 		// row 1
141 		.Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
142 		.Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
143 
144 		// row 2
145 		.Add(fFatBitsMenuField->CreateLabelLayoutItem(), 0, 1)
146 		.Add(fFatBitsMenuField->CreateMenuBarLayoutItem(), 1, 1).View()
147 	;
148 }
149