1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34
35
36#include <Catalog.h>
37#include <ControlLook.h>
38#include <InterfaceDefs.h>
39#include <LayoutBuilder.h>
40#include <Locale.h>
41#include <ScrollView.h>
42
43#include "TrackerSettings.h"
44#include "TrackerSettingsWindow.h"
45
46
47namespace BPrivate {
48
49class SettingsItem : public BStringItem {
50public:
51	SettingsItem(const char* label, SettingsView* view);
52
53	void DrawItem(BView* owner, BRect rect, bool drawEverything);
54
55	SettingsView* View();
56
57private:
58	SettingsView* fSettingsView;
59};
60
61}	// namespace BPrivate
62
63
64const uint32 kSettingsViewChanged = 'Svch';
65const uint32 kDefaultsButtonPressed = 'Apbp';
66const uint32 kRevertButtonPressed = 'Rebp';
67
68
69#undef B_TRANSLATION_CONTEXT
70#define B_TRANSLATION_CONTEXT "TrackerSettingsWindow"
71
72
73//	#pragma mark - TrackerSettingsWindow
74
75
76TrackerSettingsWindow::TrackerSettingsWindow()
77	:
78	BWindow(BRect(80, 80, 450, 350), B_TRANSLATE("Tracker preferences"),
79		B_TITLED_WINDOW, B_NOT_MINIMIZABLE | B_NOT_RESIZABLE
80			| B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
81			| B_AUTO_UPDATE_SIZE_LIMITS)
82{
83	fSettingsTypeListView = new BListView("List View",
84		B_SINGLE_SELECTION_LIST);
85
86	BScrollView* scrollView = new BScrollView("scrollview",
87		fSettingsTypeListView, B_FRAME_EVENTS | B_WILL_DRAW, false, true);
88
89	fDefaultsButton = new BButton("Defaults", B_TRANSLATE("Defaults"),
90		new BMessage(kDefaultsButtonPressed));
91	fDefaultsButton->SetEnabled(false);
92
93	fRevertButton = new BButton("Revert", B_TRANSLATE("Revert"),
94		new BMessage(kRevertButtonPressed));
95	fRevertButton->SetEnabled(false);
96
97	fSettingsContainerBox = new BBox("SettingsContainerBox");
98
99//	const float spacing = be_control_look->DefaultItemSpacing();
100
101	BLayoutBuilder::Group<>(this)
102		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
103			.Add(scrollView)
104			.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
105				.Add(fSettingsContainerBox)
106				.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
107					.Add(fDefaultsButton)
108					.Add(fRevertButton)
109					.AddGlue()
110				.End()
111			.End()
112		.SetInsets(B_USE_WINDOW_SPACING)
113		.End();
114
115	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Desktop"),
116		new DesktopSettingsView()), kDesktopSettings);
117	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Windows"),
118		new WindowsSettingsView()), kWindowsSettings);
119	fSettingsTypeListView->AddItem(new SettingsItem(
120		B_TRANSLATE("Volume icons"), new SpaceBarSettingsView()),
121		kSpaceBarSettings);
122	fSettingsTypeListView->AddItem(new SettingsItem(
123		B_TRANSLATE("Disk mount"), new AutomountSettingsPanel()),
124		kAutomountSettings);
125
126	// constraint the listview width so that the longest item fits
127	float width = 0;
128	fSettingsTypeListView->GetPreferredSize(&width, NULL);
129	width += B_V_SCROLL_BAR_WIDTH;
130	fSettingsTypeListView->SetExplicitMinSize(BSize(width, 0));
131	fSettingsTypeListView->SetExplicitMaxSize(BSize(width, B_SIZE_UNLIMITED));
132
133	fSettingsTypeListView->SetSelectionMessage(
134		new BMessage(kSettingsViewChanged));
135	fSettingsTypeListView->Select(0);
136}
137
138
139bool
140TrackerSettingsWindow::QuitRequested()
141{
142	if (IsHidden())
143		return true;
144
145	Hide();
146	return false;
147}
148
149
150void
151TrackerSettingsWindow::MessageReceived(BMessage* message)
152{
153	switch (message->what) {
154		case kSettingsContentsModified:
155			_HandleChangedContents();
156			break;
157
158		case kDefaultsButtonPressed:
159			_HandlePressedDefaultsButton();
160			break;
161
162		case kRevertButtonPressed:
163			_HandlePressedRevertButton();
164			break;
165
166		case kSettingsViewChanged:
167			_HandleChangedSettingsView();
168			break;
169
170		default:
171			_inherited::MessageReceived(message);
172			break;
173	}
174}
175
176
177void
178TrackerSettingsWindow::Show()
179{
180	if (Lock()) {
181		int32 itemCount = fSettingsTypeListView->CountItems();
182
183		for (int32 i = 0; i < itemCount; i++) {
184			_ViewAt(i)->RecordRevertSettings();
185			_ViewAt(i)->ShowCurrentSettings();
186		}
187
188		fSettingsTypeListView->Invalidate();
189
190		_UpdateButtons();
191
192		Unlock();
193	}
194
195	if (IsHidden()) {
196		// move to current workspace
197		SetWorkspaces(B_CURRENT_WORKSPACE);
198	}
199
200	_inherited::Show();
201}
202
203
204void
205TrackerSettingsWindow::ShowPage(SettingsPage page)
206{
207	fSettingsTypeListView->Select(page);
208}
209
210
211SettingsView*
212TrackerSettingsWindow::_ViewAt(int32 i)
213{
214	if (!Lock())
215		return NULL;
216
217	SettingsItem* item = dynamic_cast<SettingsItem*>(
218		fSettingsTypeListView->ItemAt(i));
219
220	Unlock();
221
222	return item != NULL ? item->View() : NULL;
223}
224
225
226void
227TrackerSettingsWindow::_HandleChangedContents()
228{
229	fSettingsTypeListView->Invalidate();
230	_UpdateButtons();
231
232	TrackerSettings().SaveSettings(false);
233}
234
235
236void
237TrackerSettingsWindow::_UpdateButtons()
238{
239	int32 itemCount = fSettingsTypeListView->CountItems();
240
241	bool defaultable = false;
242	bool revertable = false;
243
244	for (int32 i = 0; i < itemCount; i++) {
245		defaultable |= _ViewAt(i)->IsDefaultable();
246		revertable |= _ViewAt(i)->IsRevertable();
247	}
248
249	fDefaultsButton->SetEnabled(defaultable);
250	fRevertButton->SetEnabled(revertable);
251}
252
253
254void
255TrackerSettingsWindow::_HandlePressedDefaultsButton()
256{
257	int32 itemCount = fSettingsTypeListView->CountItems();
258
259	for (int32 i = 0; i < itemCount; i++) {
260		if (_ViewAt(i)->IsDefaultable())
261			_ViewAt(i)->SetDefaults();
262	}
263
264	_HandleChangedContents();
265}
266
267
268void
269TrackerSettingsWindow::_HandlePressedRevertButton()
270{
271	int32 itemCount = fSettingsTypeListView->CountItems();
272
273	for (int32 i = 0; i < itemCount; i++) {
274		if (_ViewAt(i)->IsRevertable())
275			_ViewAt(i)->Revert();
276	}
277
278	_HandleChangedContents();
279}
280
281
282void
283TrackerSettingsWindow::_HandleChangedSettingsView()
284{
285	int32 currentSelection = fSettingsTypeListView->CurrentSelection();
286	if (currentSelection < 0)
287		return;
288
289	BView* oldView = fSettingsContainerBox->ChildAt(0);
290
291	if (oldView != NULL)
292		oldView->RemoveSelf();
293
294	SettingsItem* selectedItem = dynamic_cast<SettingsItem*>(
295		fSettingsTypeListView->ItemAt(currentSelection));
296	if (selectedItem != NULL) {
297		fSettingsContainerBox->SetLabel(selectedItem->Text());
298
299		BView* view = selectedItem->View();
300		float tint = B_NO_TINT;
301		view->SetViewUIColor(fSettingsContainerBox->ViewUIColor(&tint), tint);
302		view->Hide();
303		fSettingsContainerBox->AddChild(view);
304
305		view->Show();
306	}
307}
308
309
310//	#pragma mark - SettingsItem
311
312
313SettingsItem::SettingsItem(const char* label, SettingsView* view)
314	:
315	BStringItem(label),
316	fSettingsView(view)
317{
318}
319
320
321void
322SettingsItem::DrawItem(BView* owner, BRect rect, bool drawEverything)
323{
324	if (fSettingsView) {
325		bool isRevertable = fSettingsView->IsRevertable();
326		bool isSelected = IsSelected();
327
328		if (isSelected || drawEverything) {
329			rgb_color color;
330			if (isSelected)
331				color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
332			else
333				color = owner->ViewColor();
334
335			owner->SetHighColor(color);
336			owner->SetLowColor(color);
337			owner->FillRect(rect);
338		}
339
340		if (isRevertable)
341			owner->SetFont(be_bold_font);
342		else
343			owner->SetFont(be_plain_font);
344
345		if (isSelected)
346			owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
347		else
348			owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
349
350		font_height fheight;
351		owner->GetFontHeight(&fheight);
352
353		owner->DrawString(Text(),
354			BPoint(rect.left + be_control_look->DefaultLabelSpacing(),
355				rect.top + fheight.ascent + 2 + floorf(fheight.leading / 2)));
356
357		owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
358		owner->SetLowColor(owner->ViewColor());
359	}
360}
361
362
363SettingsView*
364SettingsItem::View()
365{
366	return fSettingsView;
367}
368