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