xref: /haiku/src/add-ons/disk_systems/bfs/InitializeParameterEditor.cpp (revision 03187b607b2b5eec7ee059f1ead09bdba14991fb)
1 /*
2  * Copyright 2009, Bryce Groff, brycegroff@gmail.com.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 
9 #include "InitializeParameterEditor.h"
10 
11 #include <Button.h>
12 #include <GroupLayoutBuilder.h>
13 #include <GridLayoutBuilder.h>
14 #include <MenuField.h>
15 #include <MenuItem.h>
16 #include <PartitionParameterEditor.h>
17 #include <PopUpMenu.h>
18 #include <SpaceLayoutItem.h>
19 #include <View.h>
20 #include <Window.h>
21 
22 
23 static uint32 MSG_BLOCK_SIZE = 'blsz';
24 
25 
26 InitializeBFSEditor::InitializeBFSEditor()
27 	:
28 	BPartitionParameterEditor(),
29 	fView(NULL),
30 	fNameTC(NULL),
31 	fBlockSizeMF(NULL),
32 	fParameters(NULL)
33 {
34 	_CreateViewControls();
35 }
36 
37 
38 InitializeBFSEditor::~InitializeBFSEditor()
39 {
40 }
41 
42 
43 BView*
44 InitializeBFSEditor::View()
45 {
46 	return fView;
47 }
48 
49 
50 bool
51 InitializeBFSEditor::FinishedEditing()
52 {
53 	fParameters = "";
54 	if (BMenuItem* item = fBlockSizeMF->Menu()->FindMarked()) {
55 		const char* size;
56 		BMessage* message = item->Message();
57 		if (!message || message->FindString("size", &size) < B_OK)
58 			size = "2048";
59 		// TODO: use libroot driver settings API
60 		fParameters << "block_size " << size << ";\n";
61 	}
62 	fParameters << "name \"" << fNameTC->Text() << "\";\n";
63 
64 	return true;
65 }
66 
67 
68 status_t
69 InitializeBFSEditor::GetParameters(BString* parameters)
70 {
71 	if (parameters == NULL)
72 		return B_BAD_VALUE;
73 
74 	*parameters = fParameters;
75 	return B_OK;
76 }
77 
78 
79 status_t
80 InitializeBFSEditor::PartitionNameChanged(const char* name)
81 {
82 	fNameTC->SetText(name);
83 	return B_OK;
84 }
85 
86 
87 void
88 InitializeBFSEditor::_CreateViewControls()
89 {
90 	fNameTC = new BTextControl("Name", "Haiku", NULL);
91 	// TODO find out what is the max length for this specific FS partition name
92 	fNameTC->TextView()->SetMaxBytes(31);
93 
94 	BPopUpMenu* blocksizeMenu = new BPopUpMenu("Blocksize");
95 	BMessage* message = new BMessage(MSG_BLOCK_SIZE);
96 	message->AddString("size", "1024");
97 	blocksizeMenu->AddItem(new BMenuItem("1024 (Mostly small files)",
98 		message));
99 	message = new BMessage(MSG_BLOCK_SIZE);
100 	message->AddString("size", "2048");
101 	BMenuItem* defaultItem = new BMenuItem("2048 (Recommended)", message);
102 	blocksizeMenu->AddItem(defaultItem);
103 	message = new BMessage(MSG_BLOCK_SIZE);
104 	message->AddString("size", "4096");
105 	blocksizeMenu->AddItem(new BMenuItem("4096", message));
106 	message = new BMessage(MSG_BLOCK_SIZE);
107 	message->AddString("size", "8192");
108 	blocksizeMenu->AddItem(new BMenuItem("8192 (Mostly large files)",
109 		message));
110 
111 	fBlockSizeMF = new BMenuField("Blocksize", blocksizeMenu, NULL);
112 	defaultItem->SetMarked(true);
113 
114 	fView = BGroupLayoutBuilder(B_VERTICAL, 5)
115 		.Add(BSpaceLayoutItem::CreateVerticalStrut(10))
116 
117 		// test views
118 		.Add(BGridLayoutBuilder(10, 10)
119 			// row 1
120 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(5), 0, 0)
121 
122 			.Add(fNameTC->CreateLabelLayoutItem(), 1, 0)
123 			.Add(fNameTC->CreateTextViewLayoutItem(), 2, 0)
124 
125 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 3, 0)
126 
127 			// row 2
128 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 0, 1)
129 
130 			.Add(fBlockSizeMF->CreateLabelLayoutItem(), 1, 1)
131 			.Add(fBlockSizeMF->CreateMenuBarLayoutItem(), 2, 1)
132 
133 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(5), 3, 1)
134 		)
135 	;
136 }
137