xref: /haiku/src/apps/drivesetup/CreateParametersPanel.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
1 /*
2  * Copyright 2008-2013 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  *		Axel Dörfler, axeld@pinc-software.de.
8  *		Bryce Groff	<bgroff@hawaii.edu>
9  *		Karsten Heimrich <host.haiku@gmx.de>
10  */
11 
12 
13 #include "CreateParametersPanel.h"
14 
15 #include <Button.h>
16 #include <Catalog.h>
17 #include <ControlLook.h>
18 #include <DiskDeviceTypes.h>
19 #include <MenuField.h>
20 #include <MenuItem.h>
21 #include <MessageFilter.h>
22 #include <PopUpMenu.h>
23 #include <String.h>
24 #include <TextControl.h>
25 #include <Variant.h>
26 
27 #include "Support.h"
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "CreateParametersPanel"
32 
33 
34 enum {
35 	MSG_PARTITION_TYPE			= 'type',
36 	MSG_SIZE_SLIDER				= 'ssld',
37 	MSG_SIZE_TEXTCONTROL		= 'stct'
38 };
39 
40 static const uint32 kMegaByte = 0x100000;
41 
42 
43 CreateParametersPanel::CreateParametersPanel(BWindow* window,
44 	BPartition* partition, off_t offset, off_t size)
45 	:
46 	ChangeParametersPanel(window)
47 {
48 	_CreateCreateControls(partition, offset, size);
49 
50 	Init(B_CREATE_PARAMETER_EDITOR, "", partition);
51 }
52 
53 
54 CreateParametersPanel::~CreateParametersPanel()
55 {
56 }
57 
58 
59 status_t
60 CreateParametersPanel::Go(off_t& offset, off_t& size, BString& name,
61 	BString& type, BString& parameters)
62 {
63 	// The object will be deleted in Go(), so we need to get the values via
64 	// a BMessage
65 
66 	BMessage storage;
67 	status_t status = ChangeParametersPanel::Go(name, type, parameters,
68 		storage);
69 	if (status != B_OK)
70 		return status;
71 
72 	// Return the value back as bytes.
73 	size = storage.GetInt64("size", 0);
74 	offset = storage.GetInt64("offset", 0);
75 
76 	return B_OK;
77 }
78 
79 
80 void
81 CreateParametersPanel::MessageReceived(BMessage* message)
82 {
83 	switch (message->what) {
84 		case MSG_SIZE_SLIDER:
85 			_UpdateSizeTextControl();
86 			break;
87 
88 		case MSG_SIZE_TEXTCONTROL:
89 		{
90 			off_t size = strtoll(fSizeTextControl->Text(), NULL, 10) * kMegaByte;
91 			if (size >= 0 && size <= fSizeSlider->MaxPartitionSize())
92 				fSizeSlider->SetSize(size);
93 			else
94 				_UpdateSizeTextControl();
95 			break;
96 		}
97 
98 		default:
99 			ChangeParametersPanel::MessageReceived(message);
100 	}
101 }
102 
103 
104 bool
105 CreateParametersPanel::NeedsEditor() const
106 {
107 	return false;
108 }
109 
110 
111 status_t
112 CreateParametersPanel::ParametersReceived(const BString& parameters,
113 	BMessage& storage)
114 {
115 	// Return the value back as bytes.
116 	status_t status = storage.SetInt64("size", fSizeSlider->Size());
117 	if (status == B_OK)
118 		status = storage.SetInt64("offset", fSizeSlider->Offset());
119 
120 	if (status != B_OK)
121 		return status;
122 
123 	return ChangeParametersPanel::ParametersReceived(parameters, storage);
124 }
125 
126 
127 void
128 CreateParametersPanel::AddControls(BLayoutBuilder::Group<>& builder,
129 	BView* editorView)
130 {
131 	builder
132 		.Add(fSizeSlider)
133 		.Add(fSizeTextControl);
134 
135 	ChangeParametersPanel::AddControls(builder, editorView);
136 }
137 
138 
139 void
140 CreateParametersPanel::_CreateCreateControls(BPartition* parent, off_t offset,
141 	off_t size)
142 {
143 	// Setup the controls
144 	// TODO: use a lower granularity for smaller disks -- but this would
145 	// require being able to parse arbitrary size strings with unit
146 	fSizeSlider = new SizeSlider("Slider", B_TRANSLATE("Partition size"), NULL,
147 		offset, size, kMegaByte);
148 	fSizeSlider->SetPosition(1.0);
149 	fSizeSlider->SetModificationMessage(new BMessage(MSG_SIZE_SLIDER));
150 
151 	fSizeTextControl = new BTextControl("Size Control", "", "", NULL);
152 	for(int32 i = 0; i < 256; i++)
153 		fSizeTextControl->TextView()->DisallowChar(i);
154 	for(int32 i = '0'; i <= '9'; i++)
155 		fSizeTextControl->TextView()->AllowChar(i);
156 	_UpdateSizeTextControl();
157 	fSizeTextControl->SetModificationMessage(
158 		new BMessage(MSG_SIZE_TEXTCONTROL));
159 
160 	CreateChangeControls(NULL, parent);
161 
162 	fOkButton->SetLabel(B_TRANSLATE("Create"));
163 }
164 
165 
166 void
167 CreateParametersPanel::_UpdateSizeTextControl()
168 {
169 	BString sizeString;
170 	sizeString << fSizeSlider->Size() / kMegaByte;
171 	fSizeTextControl->SetText(sizeString.String());
172 }
173