2 * Copyright 2008-2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Michael Pfeiffer <laplace@users.sourceforge.net>
10 #include "DefaultPartitionPage.h"
14 #include <ControlLook.h>
18 #include <MenuField.h>
19 #include <PopUpMenu.h>
20 #include <RadioButton.h>
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "DefaultPartitionPage"
32 kMsgPartition
= 'part',
37 // The timeout code to wait indefinitely
38 // Note: The timeout is encoded in seconds, -1 indicates to wait indefinitely
39 const int32 kTimeoutIndefinitely
= -1;
40 const int32 kDefaultTimeout
= kTimeoutIndefinitely
;
42 struct TimeoutOption
{
47 static const TimeoutOption gTimeoutOptions
[] = {
48 { 0, B_TRANSLATE_MARK("Immediately")},
49 { 1, B_TRANSLATE_MARK("After one second")},
50 { 2, B_TRANSLATE_MARK("After two seconds")},
51 { 3, B_TRANSLATE_MARK("After three seconds")},
52 { 4, B_TRANSLATE_MARK("After four seconds")},
53 { 5, B_TRANSLATE_MARK("After five seconds")},
54 { 60, B_TRANSLATE_MARK("After one minute")},
55 { kTimeoutIndefinitely
, B_TRANSLATE_MARK("Never")}
59 #define kNumberOfTimeoutOptions \
60 (int32)(sizeof(gTimeoutOptions) / sizeof(TimeoutOption))
64 get_index_for_timeout(int32 timeout
)
66 int32 defaultIndex
= 0;
67 for (int32 i
= 0; i
< kNumberOfTimeoutOptions
; i
++) {
68 if (gTimeoutOptions
[i
].timeout
== timeout
)
71 if (gTimeoutOptions
[i
].timeout
== kDefaultTimeout
)
79 get_timeout_for_index(int32 index
)
82 return gTimeoutOptions
[0].timeout
;
83 if (index
>= kNumberOfTimeoutOptions
)
84 return gTimeoutOptions
[kNumberOfTimeoutOptions
-1].timeout
;
85 return gTimeoutOptions
[index
].timeout
;
90 get_label_for_timeout(int32 timeout
)
92 int32 index
= get_index_for_timeout(timeout
);
93 return gTimeoutOptions
[index
].label
;
100 DefaultPartitionPage::DefaultPartitionPage(BMessage
* settings
, BRect frame
,
103 WizardPageView(settings
, frame
, name
, B_FOLLOW_ALL
,
104 B_WILL_DRAW
| B_FRAME_EVENTS
| B_FULL_UPDATE_ON_RESIZE
)
110 DefaultPartitionPage::~DefaultPartitionPage()
116 DefaultPartitionPage::FrameResized(float width
, float height
)
118 WizardPageView::FrameResized(width
, height
);
124 DefaultPartitionPage::AttachedToWindow()
126 fDefaultPartition
->Menu()->SetTargetForItems(this);
127 fTimeoutSlider
->SetTarget(this);
132 DefaultPartitionPage::MessageReceived(BMessage
* msg
)
138 msg
->FindInt32("index", &index
);
139 fSettings
->ReplaceInt32("defaultPartition", index
);
144 int32 sliderValue
= fTimeoutSlider
->Value();
145 int32 timeout
= get_timeout_for_index(sliderValue
);
146 fSettings
->ReplaceInt32("timeout", timeout
);
149 _GetTimeoutLabel(timeout
, label
);
150 fTimeoutSlider
->SetLabel(label
.String());
155 WizardPageView::MessageReceived(msg
);
161 DefaultPartitionPage::_BuildUI()
163 const float kTextDistance
= be_control_look
->DefaultItemSpacing();
164 BRect
rect(Bounds());
167 text
<< B_TRANSLATE_COMMENT("Default Partition", "Title") << "\n"
168 << B_TRANSLATE("Please specify a default partition and a timeout.\n"
169 "The boot menu will load the default partition after "
170 "the timeout unless you select another partition. You "
171 "can also have the boot menu wait indefinitely for you "
172 "to select a partition.\n"
173 "Keep the 'ALT' key pressed to disable the timeout at boot time.");
175 fDescription
= CreateDescription(rect
, "description", text
);
176 MakeHeading(fDescription
);
177 AddChild(fDescription
);
178 LayoutDescriptionVertically(fDescription
);
179 rect
.top
= fDescription
->Frame().bottom
+ kTextDistance
;
181 BPopUpMenu
* popUpMenu
= _CreatePopUpMenu();
182 fDefaultPartition
= new BMenuField(rect
, "partitions",
183 B_TRANSLATE_COMMENT("Default Partition:", "Menu field label"),
185 float divider
= be_plain_font
->StringWidth(fDefaultPartition
->Label()) + 3;
186 fDefaultPartition
->SetDivider(divider
);
187 AddChild(fDefaultPartition
);
188 fDefaultPartition
->ResizeToPreferred();
191 rect
.top
= fDefaultPartition
->Frame().bottom
+ kTextDistance
;
193 fSettings
->FindInt32("timeout", &timeout
);
194 BString timeoutLabel
;
195 _GetTimeoutLabel(timeout
, timeoutLabel
);
197 int32 sliderValue
= get_index_for_timeout(timeout
);
199 fTimeoutSlider
= new BSlider(rect
, "timeout", timeoutLabel
.String(),
200 new BMessage(kMsgTimeout
), 0, kNumberOfTimeoutOptions
-1,
202 B_FOLLOW_LEFT_RIGHT
| B_FOLLOW_TOP
);
203 fTimeoutSlider
->SetModificationMessage(new BMessage(kMsgTimeout
));
204 fTimeoutSlider
->SetValue(sliderValue
);
205 fTimeoutSlider
->SetLimitLabels(B_TRANSLATE("Immediately"),
206 B_TRANSLATE("Never"));
207 fTimeoutSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
208 fTimeoutSlider
->SetHashMarkCount(kNumberOfTimeoutOptions
);
209 fTimeoutSlider
->ResizeToPreferred();
210 AddChild(fTimeoutSlider
);
217 DefaultPartitionPage::_CreatePopUpMenu()
219 int32 defaultPartitionIndex
;
220 fSettings
->FindInt32("defaultPartition", &defaultPartitionIndex
);
222 BMenuItem
* selectedItem
= NULL
;
223 int32 selectedItemIndex
= 0;
225 BPopUpMenu
* menu
= new BPopUpMenu(B_TRANSLATE_COMMENT("Partitions",
226 "Pop up menu title"));
228 for (int32 i
= 0; fSettings
->FindMessage("partition", i
, &message
) == B_OK
;
231 if (message
.FindBool("show", &show
) != B_OK
|| !show
)
235 message
.FindString("name", &name
);
237 BMessage
* msg
= new BMessage(kMsgPartition
);
238 msg
->AddInt32("index", i
);
239 BMenuItem
* item
= new BMenuItem(name
.String(), msg
);
241 if (defaultPartitionIndex
== i
|| selectedItem
== NULL
) {
243 selectedItemIndex
= i
;
246 fSettings
->ReplaceInt32("defaultPartition", selectedItemIndex
);
247 selectedItem
->SetMarked(true);
253 DefaultPartitionPage::_GetTimeoutLabel(int32 timeout
, BString
& label
)
255 const char* text
= B_TRANSLATE_NOCOLLECT(get_label_for_timeout(timeout
));
256 label
= B_TRANSLATE("Timeout: %s");
257 label
.ReplaceFirst("%s", text
);
262 DefaultPartitionPage::_Layout()
264 LayoutDescriptionVertically(fDescription
);
266 const float kTextDistance
= be_control_look
->DefaultItemSpacing();
267 float left
= fDefaultPartition
->Frame().left
;
268 float top
= fDescription
->Frame().bottom
+ kTextDistance
;
270 fDefaultPartition
->MoveTo(left
, top
);
271 top
= fDefaultPartition
->Frame().bottom
+ kTextDistance
;
273 fTimeoutSlider
->MoveTo(left
, top
);