xref: /haiku/src/apps/bootmanager/PartitionsPage.cpp (revision 4fd62caa9acc437534c41bbb7d3fc9d53e915005)
1 /*
2  * Copyright 2008-2011, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 		Axel Dörfler, <axeld@pinc-software.de>
7  *		Michael Pfeiffer, laplace@users.sourceforge.net
8  *		Ingo Weinhold <bonefish@cs.tu-berlin.de>
9  */
10 
11 
12 #include "PartitionsPage.h"
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <Catalog.h>
18 #include <CheckBox.h>
19 #include <ControlLook.h>
20 #include <LayoutBuilder.h>
21 #include <RadioButton.h>
22 #include <ScrollView.h>
23 #include <SeparatorView.h>
24 #include <StringView.h>
25 #include <TextControl.h>
26 #include <TextView.h>
27 
28 #include <StringForSize.h>
29 
30 
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "PartitionsPage"
33 
34 
35 const uint32 kMessageShow = 'show';
36 const uint32 kMessageName = 'name';
37 
38 
39 PartitionsPage::PartitionsPage(BMessage* settings, const char* name)
40 	:
41 	WizardPageView(settings, name)
42 {
43 	_BuildUI();
44 }
45 
46 
47 PartitionsPage::~PartitionsPage()
48 {
49 }
50 
51 
52 void
53 PartitionsPage::PageCompleted()
54 {
55 	BGridLayout* layout = (BGridLayout*)fPartitions->GetLayout();
56 	int32 index = 0;
57 
58 	for (int32 row = 0; row < layout->CountRows(); row += 3, index++) {
59 		BCheckBox* showBox
60 			= dynamic_cast<BCheckBox*>(layout->ItemAt(0, row)->View());
61 		BTextControl* nameControl
62 			= dynamic_cast<BTextControl*>(layout->ItemAt(1, row)->View());
63 		if (nameControl == NULL || showBox == NULL)
64 			debugger("partitions page is broken");
65 
66 		BMessage partition;
67 		if (fSettings->FindMessage("partition", index, &partition) != B_OK)
68 			continue;
69 
70 		partition.ReplaceBool("show", showBox->Value() != 0);
71 		partition.ReplaceString("name", nameControl->Text());
72 
73 		fSettings->ReplaceMessage("partition", index, &partition);
74 	}
75 }
76 
77 
78 void
79 PartitionsPage::_BuildUI()
80 {
81 	BString text;
82 	text << B_TRANSLATE_COMMENT("Partitions", "Title") << "\n"
83 		<< B_TRANSLATE("The following partitions were detected. Please "
84 			"check the box next to the partitions to be included "
85 			"in the boot menu. You can also set the names of the "
86 			"partitions as you would like them to appear in the "
87 			"boot menu.");
88 	fDescription = CreateDescription("description", text);
89 	MakeHeading(fDescription);
90 
91 	fPartitions = new BGridView("partitions", 0,
92 		be_control_look->DefaultItemSpacing() / 3);
93 
94 	BLayoutBuilder::Grid<>(fPartitions)
95 		.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
96 			B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
97 		.SetColumnWeight(0, 0)
98 		.SetColumnWeight(1, 1)
99 		.SetColumnWeight(2, 0.5)
100 		.SetColumnWeight(3, 0.5);
101 	_FillPartitionsView(fPartitions);
102 
103 	BScrollView* scrollView = new BScrollView("scrollView", fPartitions, 0,
104 		false, true);
105 
106 	SetLayout(new BGroupLayout(B_VERTICAL));
107 
108 	BLayoutBuilder::Group<>((BGroupLayout*)GetLayout())
109 		.Add(fDescription)
110 		.Add(scrollView);
111 }
112 
113 
114 void
115 PartitionsPage::_FillPartitionsView(BView* view)
116 {
117 	// show | name | type | size | path
118 
119 	int32 rowNumber = 0;
120 
121 	BMessage message;
122 	for (int32 i = 0; fSettings->FindMessage("partition", i, &message) == B_OK;
123 			i++, rowNumber++) {
124 		// get partition data
125 		bool show;
126 		BString name;
127 		BString type;
128 		BString path;
129 		int64 size;
130 		message.FindBool("show", &show);
131 		message.FindString("name", &name);
132 		message.FindString("type", &type);
133 		message.FindString("path", &path);
134 		message.FindInt64("size", &size);
135 
136 		// check box
137 		BCheckBox* checkBox = new BCheckBox("show", "",
138 			_CreateControlMessage(kMessageShow, i));
139 		if (show)
140 			checkBox->SetValue(1);
141 
142 		// name
143 		BTextControl* nameControl = new BTextControl("name", "",
144 			name.String(), _CreateControlMessage(kMessageName, i));
145 		nameControl->SetExplicitMinSize(BSize(StringWidth("WWWWWWWWWWWWWW"),
146 			B_SIZE_UNSET));
147 
148 		// size
149 		BString sizeText;
150 		_CreateSizeText(size, &sizeText);
151 		sizeText << ", " << type.String();
152 		BStringView* typeView = new BStringView("type", sizeText.String());
153 		typeView->SetExplicitAlignment(
154 			BAlignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_UNSET));
155 
156 		// path
157 		BStringView* pathView = new BStringView("path", path.String());
158 		pathView->SetExplicitAlignment(
159 			BAlignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_UNSET));
160 
161 		if (rowNumber > 0) {
162 			BLayoutBuilder::Grid<>((BGridLayout*)view->GetLayout())
163 				.Add(new BSeparatorView(B_HORIZONTAL), 0, rowNumber, 4, 1)
164 				.SetRowWeight(rowNumber, 0);
165 			rowNumber++;
166 		}
167 
168 		BLayoutBuilder::Grid<>((BGridLayout*)view->GetLayout())
169 			.Add(checkBox, 0, rowNumber, 1, 2)
170 			.Add(nameControl, 1, rowNumber, 1, 2)
171 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 2, rowNumber)
172 			.Add(typeView, 3, rowNumber)
173 			.Add(pathView, 3, rowNumber + 1)
174 			.SetRowWeight(rowNumber + 1, 1);
175 		rowNumber++;
176 	}
177 }
178 
179 
180 void
181 PartitionsPage::_CreateSizeText(int64 size, BString* text)
182 {
183 	char buffer[256];
184 	*text = string_for_size(size, buffer, sizeof(buffer));
185 }
186 
187 
188 BMessage*
189 PartitionsPage::_CreateControlMessage(uint32 what, int32 index)
190 {
191 	BMessage* message = new BMessage(what);
192 	message->AddInt32("index", index);
193 	return message;
194 }
195