10255f411SMichael Pfeiffer/*
2197f59b0SAxel Dörfler * Copyright 2008-2011, Haiku, Inc. All rights reserved.
30255f411SMichael Pfeiffer * Distributed under the terms of the MIT License.
404bcc0b4SMatt Madia *
51059b4e7SMichael Pfeiffer * Authors:
61059b4e7SMichael Pfeiffer *		Michael Pfeiffer <laplace@users.sourceforge.net>
70255f411SMichael Pfeiffer */
80255f411SMichael Pfeiffer
90255f411SMichael Pfeiffer
100255f411SMichael Pfeiffer#include "DefaultPartitionPage.h"
110255f411SMichael Pfeiffer
120255f411SMichael Pfeiffer
1309ccc66eSMichael Pfeiffer#include <Catalog.h>
144fc2c378SMichael Pfeiffer#include <ControlLook.h>
155421c7d6SAdrien Destugues#include <Locale.h>
160255f411SMichael Pfeiffer#include <Message.h>
170255f411SMichael Pfeiffer#include <MenuItem.h>
180255f411SMichael Pfeiffer#include <MenuField.h>
190255f411SMichael Pfeiffer#include <PopUpMenu.h>
200255f411SMichael Pfeiffer#include <RadioButton.h>
214fc2c378SMichael Pfeiffer#include <Slider.h>
220255f411SMichael Pfeiffer#include <string.h>
230255f411SMichael Pfeiffer#include <String.h>
244fc2c378SMichael Pfeiffer#include <TextView.h>
250255f411SMichael Pfeiffer
260255f411SMichael Pfeiffer
27546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
28546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "DefaultPartitionPage"
2909ccc66eSMichael Pfeiffer
3009ccc66eSMichael Pfeiffer
314fc2c378SMichael Pfeifferenum {
324fc2c378SMichael Pfeiffer	kMsgPartition = 'part',
334fc2c378SMichael Pfeiffer	kMsgTimeout = 'time'
344fc2c378SMichael Pfeiffer};
354fc2c378SMichael Pfeiffer
364fc2c378SMichael Pfeiffer
374fc2c378SMichael Pfeiffer// The timeout code to wait indefinitely
384fc2c378SMichael Pfeiffer// Note: The timeout is encoded in seconds, -1 indicates to wait indefinitely
394fc2c378SMichael Pfeifferconst int32 kTimeoutIndefinitely = -1;
404fc2c378SMichael Pfeifferconst int32 kDefaultTimeout = kTimeoutIndefinitely;
414fc2c378SMichael Pfeiffer
425b788d58SAxel Dörflerstruct TimeoutOption {
434fc2c378SMichael Pfeiffer	int32 timeout;
444fc2c378SMichael Pfeiffer	const char* label;
455b788d58SAxel Dörfler};
464fc2c378SMichael Pfeiffer
475b788d58SAxel Dörflerstatic const TimeoutOption gTimeoutOptions[] = {
4804bcc0b4SMatt Madia	{ 0, B_TRANSLATE_MARK("Immediately")},
4904bcc0b4SMatt Madia	{ 1, B_TRANSLATE_MARK("After one second")},
5004bcc0b4SMatt Madia	{ 2, B_TRANSLATE_MARK("After two seconds")},
5104bcc0b4SMatt Madia	{ 3, B_TRANSLATE_MARK("After three seconds")},
5204bcc0b4SMatt Madia	{ 4, B_TRANSLATE_MARK("After four seconds")},
5304bcc0b4SMatt Madia	{ 5, B_TRANSLATE_MARK("After five seconds")},
5404bcc0b4SMatt Madia	{ 60, B_TRANSLATE_MARK("After one minute")},
5504bcc0b4SMatt Madia	{ kTimeoutIndefinitely, B_TRANSLATE_MARK("Never")}
564fc2c378SMichael Pfeiffer};
574fc2c378SMichael Pfeiffer
584fc2c378SMichael Pfeiffer
594fc2c378SMichael Pfeiffer#define kNumberOfTimeoutOptions \
604fc2c378SMichael Pfeiffer	(int32)(sizeof(gTimeoutOptions) / sizeof(TimeoutOption))
614fc2c378SMichael Pfeiffer
624fc2c378SMichael Pfeiffer
634fc2c378SMichael Pfeifferstatic int32
644fc2c378SMichael Pfeifferget_index_for_timeout(int32 timeout)
654fc2c378SMichael Pfeiffer{
664fc2c378SMichael Pfeiffer	int32 defaultIndex = 0;
674fc2c378SMichael Pfeiffer	for (int32 i = 0; i < kNumberOfTimeoutOptions; i ++) {
684fc2c378SMichael Pfeiffer		if (gTimeoutOptions[i].timeout == timeout)
694fc2c378SMichael Pfeiffer			return i;
704fc2c378SMichael Pfeiffer
714fc2c378SMichael Pfeiffer		if (gTimeoutOptions[i].timeout == kDefaultTimeout)
724fc2c378SMichael Pfeiffer			defaultIndex = i;
734fc2c378SMichael Pfeiffer	}
744fc2c378SMichael Pfeiffer	return defaultIndex;
754fc2c378SMichael Pfeiffer}
764fc2c378SMichael Pfeiffer
774fc2c378SMichael Pfeiffer
784fc2c378SMichael Pfeifferstatic int32
794fc2c378SMichael Pfeifferget_timeout_for_index(int32 index)
804fc2c378SMichael Pfeiffer{
814fc2c378SMichael Pfeiffer	if (index < 0)
824fc2c378SMichael Pfeiffer		return gTimeoutOptions[0].timeout;
834fc2c378SMichael Pfeiffer	if (index >= kNumberOfTimeoutOptions)
844fc2c378SMichael Pfeiffer		return gTimeoutOptions[kNumberOfTimeoutOptions-1].timeout;
854fc2c378SMichael Pfeiffer	return gTimeoutOptions[index].timeout;
864fc2c378SMichael Pfeiffer}
874fc2c378SMichael Pfeiffer
884fc2c378SMichael Pfeiffer
894fc2c378SMichael Pfeifferconst char*
904fc2c378SMichael Pfeifferget_label_for_timeout(int32 timeout)
914fc2c378SMichael Pfeiffer{
924fc2c378SMichael Pfeiffer	int32 index = get_index_for_timeout(timeout);
934fc2c378SMichael Pfeiffer	return gTimeoutOptions[index].label;
944fc2c378SMichael Pfeiffer}
950255f411SMichael Pfeiffer
960255f411SMichael Pfeiffer
975b788d58SAxel Dörfler// #pragma mark -
985b788d58SAxel Dörfler
995b788d58SAxel Dörfler
1005b788d58SAxel DörflerDefaultPartitionPage::DefaultPartitionPage(BMessage* settings, BRect frame,
1015b788d58SAxel Dörfler	const char* name)
1025b788d58SAxel Dörfler	:
1035b788d58SAxel Dörfler	WizardPageView(settings, frame, name, B_FOLLOW_ALL,
1040255f411SMichael Pfeiffer		B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE)
1050255f411SMichael Pfeiffer{
1060255f411SMichael Pfeiffer	_BuildUI();
1070255f411SMichael Pfeiffer}
1080255f411SMichael Pfeiffer
1090255f411SMichael Pfeiffer
1100255f411SMichael PfeifferDefaultPartitionPage::~DefaultPartitionPage()
1110255f411SMichael Pfeiffer{
1120255f411SMichael Pfeiffer}
1130255f411SMichael Pfeiffer
1140255f411SMichael Pfeiffer
1150255f411SMichael Pfeiffervoid
1160255f411SMichael PfeifferDefaultPartitionPage::FrameResized(float width, float height)
1170255f411SMichael Pfeiffer{
1180255f411SMichael Pfeiffer	WizardPageView::FrameResized(width, height);
1190255f411SMichael Pfeiffer	_Layout();
1200255f411SMichael Pfeiffer}
1210255f411SMichael Pfeiffer
1220255f411SMichael Pfeiffer
1230255f411SMichael Pfeiffervoid
1240255f411SMichael PfeifferDefaultPartitionPage::AttachedToWindow()
1250255f411SMichael Pfeiffer{
1260255f411SMichael Pfeiffer	fDefaultPartition->Menu()->SetTargetForItems(this);
1274fc2c378SMichael Pfeiffer	fTimeoutSlider->SetTarget(this);
1280255f411SMichael Pfeiffer}
1290255f411SMichael Pfeiffer
1300255f411SMichael Pfeiffer
1310255f411SMichael Pfeiffervoid
1320255f411SMichael PfeifferDefaultPartitionPage::MessageReceived(BMessage* msg)
1330255f411SMichael Pfeiffer{
1340255f411SMichael Pfeiffer	switch (msg->what) {
1350255f411SMichael Pfeiffer		case kMsgPartition:
1365b788d58SAxel Dörfler		{
1375b788d58SAxel Dörfler			int32 index;
1385b788d58SAxel Dörfler			msg->FindInt32("index", &index);
1395b788d58SAxel Dörfler			fSettings->ReplaceInt32("defaultPartition", index);
1400255f411SMichael Pfeiffer			break;
1415b788d58SAxel Dörfler		}
1420255f411SMichael Pfeiffer		case kMsgTimeout:
1435b788d58SAxel Dörfler		{
1445b788d58SAxel Dörfler			int32 sliderValue = fTimeoutSlider->Value();
1455b788d58SAxel Dörfler			int32 timeout = get_timeout_for_index(sliderValue);
1465b788d58SAxel Dörfler			fSettings->ReplaceInt32("timeout", timeout);
1475b788d58SAxel Dörfler
1485b788d58SAxel Dörfler			BString label;
1495b788d58SAxel Dörfler			_GetTimeoutLabel(timeout, label);
1505b788d58SAxel Dörfler			fTimeoutSlider->SetLabel(label.String());
1510255f411SMichael Pfeiffer			break;
1525b788d58SAxel Dörfler		}
1535b788d58SAxel Dörfler
1540255f411SMichael Pfeiffer		default:
1550255f411SMichael Pfeiffer			WizardPageView::MessageReceived(msg);
1560255f411SMichael Pfeiffer	}
1570255f411SMichael Pfeiffer}
1580255f411SMichael Pfeiffer
1590255f411SMichael Pfeiffer
1600255f411SMichael Pfeiffervoid
1610255f411SMichael PfeifferDefaultPartitionPage::_BuildUI()
1620255f411SMichael Pfeiffer{
1635b788d58SAxel Dörfler	const float kTextDistance = be_control_look->DefaultItemSpacing();
1640255f411SMichael Pfeiffer	BRect rect(Bounds());
16504bcc0b4SMatt Madia
16609ccc66eSMichael Pfeiffer	BString text;
167197f59b0SAxel Dörfler	text << B_TRANSLATE_COMMENT("Default Partition", "Title") << "\n"
168197f59b0SAxel Dörfler		<< B_TRANSLATE("Please specify a default partition and a timeout.\n"
169197f59b0SAxel Dörfler			"The boot menu will load the default partition after "
170197f59b0SAxel Dörfler			"the timeout unless you select another partition. You "
171197f59b0SAxel Dörfler			"can also have the boot menu wait indefinitely for you "
172197f59b0SAxel Dörfler			"to select a partition.\n"
173197f59b0SAxel Dörfler			"Keep the 'ALT' key pressed to disable the timeout at boot time.");
17409ccc66eSMichael Pfeiffer
17509ccc66eSMichael Pfeiffer	fDescription = CreateDescription(rect, "description", text);
1760255f411SMichael Pfeiffer	MakeHeading(fDescription);
1770255f411SMichael Pfeiffer	AddChild(fDescription);
1780255f411SMichael Pfeiffer	LayoutDescriptionVertically(fDescription);
1790255f411SMichael Pfeiffer	rect.top = fDescription->Frame().bottom + kTextDistance;
18004bcc0b4SMatt Madia
18104bcc0b4SMatt Madia	BPopUpMenu* popUpMenu = _CreatePopUpMenu();
18209ccc66eSMichael Pfeiffer	fDefaultPartition = new BMenuField(rect, "partitions",
18304bcc0b4SMatt Madia		B_TRANSLATE_COMMENT("Default Partition:", "Menu field label"),
18404bcc0b4SMatt Madia		popUpMenu);
1850255f411SMichael Pfeiffer	float divider = be_plain_font->StringWidth(fDefaultPartition->Label()) + 3;
1860255f411SMichael Pfeiffer	fDefaultPartition->SetDivider(divider);
1870255f411SMichael Pfeiffer	AddChild(fDefaultPartition);
1880255f411SMichael Pfeiffer	fDefaultPartition->ResizeToPreferred();
18904bcc0b4SMatt Madia
1904fc2c378SMichael Pfeiffer	// timeout slider
1910255f411SMichael Pfeiffer	rect.top = fDefaultPartition->Frame().bottom + kTextDistance;
1920255f411SMichael Pfeiffer	int32 timeout;
1930255f411SMichael Pfeiffer	fSettings->FindInt32("timeout", &timeout);
1944fc2c378SMichael Pfeiffer	BString timeoutLabel;
1954fc2c378SMichael Pfeiffer	_GetTimeoutLabel(timeout, timeoutLabel);
1964fc2c378SMichael Pfeiffer
1974fc2c378SMichael Pfeiffer	int32 sliderValue = get_index_for_timeout(timeout);
1984fc2c378SMichael Pfeiffer
1994fc2c378SMichael Pfeiffer	fTimeoutSlider = new BSlider(rect, "timeout", timeoutLabel.String(),
2004fc2c378SMichael Pfeiffer		new BMessage(kMsgTimeout), 0, kNumberOfTimeoutOptions-1,
2014fc2c378SMichael Pfeiffer		B_BLOCK_THUMB,
2024fc2c378SMichael Pfeiffer		B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
2034fc2c378SMichael Pfeiffer	fTimeoutSlider->SetModificationMessage(new BMessage(kMsgTimeout));
2044fc2c378SMichael Pfeiffer	fTimeoutSlider->SetValue(sliderValue);
20504bcc0b4SMatt Madia	fTimeoutSlider->SetLimitLabels(B_TRANSLATE("Immediately"),
20604bcc0b4SMatt Madia		B_TRANSLATE("Never"));
2074fc2c378SMichael Pfeiffer	fTimeoutSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
2084fc2c378SMichael Pfeiffer	fTimeoutSlider->SetHashMarkCount(kNumberOfTimeoutOptions);
2094fc2c378SMichael Pfeiffer	fTimeoutSlider->ResizeToPreferred();
2104fc2c378SMichael Pfeiffer	AddChild(fTimeoutSlider);
21104bcc0b4SMatt Madia
2120255f411SMichael Pfeiffer	_Layout();
2130255f411SMichael Pfeiffer}
2140255f411SMichael Pfeiffer
2150255f411SMichael Pfeiffer
2160255f411SMichael PfeifferBPopUpMenu*
2170255f411SMichael PfeifferDefaultPartitionPage::_CreatePopUpMenu()
2180255f411SMichael Pfeiffer{
2190255f411SMichael Pfeiffer	int32 defaultPartitionIndex;
2200255f411SMichael Pfeiffer	fSettings->FindInt32("defaultPartition", &defaultPartitionIndex);
22104bcc0b4SMatt Madia
2220255f411SMichael Pfeiffer	BMenuItem* selectedItem = NULL;
2230255f411SMichael Pfeiffer	int32 selectedItemIndex = 0;
22404bcc0b4SMatt Madia
22504bcc0b4SMatt Madia	BPopUpMenu* menu = new BPopUpMenu(B_TRANSLATE_COMMENT("Partitions",
226c7e6a670SMichael Pfeiffer		"Pop up menu title"));
2270255f411SMichael Pfeiffer	BMessage message;
228c7e6a670SMichael Pfeiffer	for (int32 i = 0; fSettings->FindMessage("partition", i, &message) == B_OK;
2295b788d58SAxel Dörfler			i++) {
2300255f411SMichael Pfeiffer		bool show;
2310255f411SMichael Pfeiffer		if (message.FindBool("show", &show) != B_OK || !show)
2320255f411SMichael Pfeiffer			continue;
23304bcc0b4SMatt Madia
2340255f411SMichael Pfeiffer		BString name;
2350255f411SMichael Pfeiffer		message.FindString("name", &name);
2360255f411SMichael Pfeiffer
2370255f411SMichael Pfeiffer		BMessage* msg = new BMessage(kMsgPartition);
2380255f411SMichael Pfeiffer		msg->AddInt32("index", i);
2390255f411SMichael Pfeiffer		BMenuItem* item = new BMenuItem(name.String(), msg);
2400255f411SMichael Pfeiffer		menu->AddItem(item);
2410255f411SMichael Pfeiffer		if (defaultPartitionIndex == i || selectedItem == NULL) {
2420255f411SMichael Pfeiffer			selectedItem = item;
2430255f411SMichael Pfeiffer			selectedItemIndex = i;
2440255f411SMichael Pfeiffer		}
2450255f411SMichael Pfeiffer	}
2460255f411SMichael Pfeiffer	fSettings->ReplaceInt32("defaultPartition", selectedItemIndex);
2470255f411SMichael Pfeiffer	selectedItem->SetMarked(true);
2480255f411SMichael Pfeiffer	return menu;
2490255f411SMichael Pfeiffer}
2500255f411SMichael Pfeiffer
2510255f411SMichael Pfeiffer
2524fc2c378SMichael Pfeiffervoid
2534fc2c378SMichael PfeifferDefaultPartitionPage::_GetTimeoutLabel(int32 timeout, BString& label)
2540255f411SMichael Pfeiffer{
2558eff03f5SOliver Tappe	const char* text = B_TRANSLATE_NOCOLLECT(get_label_for_timeout(timeout));
25604bcc0b4SMatt Madia	label = B_TRANSLATE("Timeout: %s");
2574fc2c378SMichael Pfeiffer	label.ReplaceFirst("%s", text);
2580255f411SMichael Pfeiffer}
2590255f411SMichael Pfeiffer
2607437e205SMichael Pfeiffer
2610255f411SMichael Pfeiffervoid
2620255f411SMichael PfeifferDefaultPartitionPage::_Layout()
2630255f411SMichael Pfeiffer{
2640255f411SMichael Pfeiffer	LayoutDescriptionVertically(fDescription);
26504bcc0b4SMatt Madia
2665b788d58SAxel Dörfler	const float kTextDistance = be_control_look->DefaultItemSpacing();
2670255f411SMichael Pfeiffer	float left = fDefaultPartition->Frame().left;
2680255f411SMichael Pfeiffer	float top = fDescription->Frame().bottom + kTextDistance;
26904bcc0b4SMatt Madia
2704fc2c378SMichael Pfeiffer	fDefaultPartition->MoveTo(left, top);
2714fc2c378SMichael Pfeiffer	top = fDefaultPartition->Frame().bottom + kTextDistance;
2724fc2c378SMichael Pfeiffer
2734fc2c378SMichael Pfeiffer	fTimeoutSlider->MoveTo(left, top);
2740255f411SMichael Pfeiffer}
275