xref: /haiku/src/kits/tracker/TrackerSettingsWindow.cpp (revision b3de82492af3b6412ffaf7eb87fd6e1995755685)
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 "SettingsViews.h"
37 #include "TrackerSettings.h"
38 #include "TrackerSettingsWindow.h"
39 
40 //#include <CheckBox.h>
41 #include <ScrollView.h>
42 
43 
44 namespace BPrivate {
45 
46 class SettingsItem : public BStringItem {
47 	public:
48 		SettingsItem(const char *label, SettingsView *view);
49 
50 		void DrawItem(BView *owner, BRect rect, bool drawEverything);
51 
52 		SettingsView *View();
53 
54 	private:
55 		SettingsView *fSettingsView;
56 };
57 
58 }	// namespace BPrivate
59 
60 
61 const uint32 kSettingsViewChanged = 'Svch';
62 const uint32 kDefaultsButtonPressed = 'Apbp';
63 const uint32 kRevertButtonPressed = 'Rebp';
64 
65 
66 TrackerSettingsWindow::TrackerSettingsWindow()
67 	: BWindow(BRect(80, 80, 450, 350), "Tracker preferences", B_TITLED_WINDOW,
68 		B_NOT_MINIMIZABLE | B_NOT_RESIZABLE | B_NO_WORKSPACE_ACTIVATION
69 		| B_NOT_ANCHORED_ON_ACTIVATE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
70 {
71 	BRect rect = Bounds();
72 	BView *topView = new BView(rect, "Background", B_FOLLOW_ALL, 0);
73 	topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
74 	AddChild(topView);
75 
76 	rect.InsetBy(10, 10);
77 	rect.right = rect.left + be_plain_font->StringWidth("Volume Icons")
78 		+ (float)B_V_SCROLL_BAR_WIDTH + 40.0f;
79 	fSettingsTypeListView = new BListView(rect, "List View", B_SINGLE_SELECTION_LIST,
80 		B_FOLLOW_LEFT | B_FOLLOW_TOP_BOTTOM);
81 	BScrollView* scrollView = new BScrollView("scrollview", fSettingsTypeListView,
82 		B_FOLLOW_LEFT | B_FOLLOW_TOP_BOTTOM, B_FRAME_EVENTS | B_WILL_DRAW, false, true);
83 	topView->AddChild(scrollView);
84 
85 	rect = scrollView->Frame();
86 	rect.left = rect.right + 10;
87 	rect.top = rect.bottom;
88 	fDefaultsButton = new BButton(rect, "Defaults", "Defaults",
89 		new BMessage(kDefaultsButtonPressed), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
90 	fDefaultsButton->ResizeToPreferred();
91 	fDefaultsButton->SetEnabled(false);
92 	fDefaultsButton->MoveBy(0, -fDefaultsButton->Bounds().Height());
93 
94 	rect = fDefaultsButton->Frame();
95 	rect.left = rect.right + 10;
96 	fRevertButton = new BButton(rect, "Revert", "Revert",
97 		new BMessage(kRevertButtonPressed), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
98 	fRevertButton->SetEnabled(false);
99 	fRevertButton->ResizeToPreferred();
100 
101 	rect = scrollView->Frame();
102 	rect.left = rect.right + 10;
103 	rect.right = Bounds().right - 10;
104 	rect.bottom = fDefaultsButton->Frame().top - 10;
105 	fSettingsContainerBox = new BBox(rect, NULL, B_FOLLOW_ALL);
106 	topView->AddChild(fSettingsContainerBox);
107 	topView->AddChild(fDefaultsButton);
108 	topView->AddChild(fRevertButton);
109 
110 	rect = _SettingsFrame();
111 
112 	fSettingsTypeListView->AddItem(new SettingsItem("Desktop",
113 		new DesktopSettingsView(rect)));
114 	fSettingsTypeListView->AddItem(new SettingsItem("Windows",
115 		new WindowsSettingsView(rect)));
116 	fSettingsTypeListView->AddItem(new SettingsItem("Date & Time",
117 		new TimeFormatSettingsView(rect)));
118 	fSettingsTypeListView->AddItem(new SettingsItem("Trash",
119 		new TrashSettingsView(rect)));
120 	fSettingsTypeListView->AddItem(new SettingsItem("Volume icons",
121 		new SpaceBarSettingsView(rect)));
122 
123 	// compute preferred view size
124 
125 	float minWidth = 0, minHeight = 0;
126 
127 	for (int32 i = 0; i < fSettingsTypeListView->CountItems(); i++) {
128 		SettingsView* view = ((SettingsItem *)fSettingsTypeListView->ItemAt(i))->View();
129 
130 		float width, height;
131 		view->GetPreferredSize(&width, &height);
132 
133 		if (minWidth < width)
134 			minWidth = width;
135 		if (minHeight < height)
136 			minHeight = height;
137 	}
138 
139 	ResizeBy(max_c(minWidth - rect.Width(), 0), max_c(minHeight - rect.Height(), 0));
140 		// make sure window is large enough to contain all views
141 
142 	fSettingsTypeListView->SetSelectionMessage(new BMessage(kSettingsViewChanged));
143 	fSettingsTypeListView->Select(0);
144 }
145 
146 
147 bool
148 TrackerSettingsWindow::QuitRequested()
149 {
150 	bool isHidden = false;
151 
152 	if (Lock()) {
153 		isHidden = IsHidden();
154 		Unlock();
155 	} else
156 		return true;
157 
158 	if (isHidden)
159 		return true;
160 
161 	Hide();
162 
163 	return false;
164 }
165 
166 
167 void
168 TrackerSettingsWindow::MessageReceived(BMessage *message)
169 {
170 	switch (message->what) {
171 		case kSettingsContentsModified:
172 			_HandleChangedContents();
173 			break;
174 
175 		case kDefaultsButtonPressed:
176 			_HandlePressedDefaultsButton();
177 			break;
178 
179 		case kRevertButtonPressed:
180 			_HandlePressedRevertButton();
181 			break;
182 
183 		case kSettingsViewChanged:
184 			_HandleChangedSettingsView();
185 			break;
186 
187 		default:
188 			_inherited::MessageReceived(message);
189 	}
190 }
191 
192 
193 void
194 TrackerSettingsWindow::Show()
195 {
196 	if (Lock()) {
197 		int32 itemCount = fSettingsTypeListView->CountItems();
198 
199 		for (int32 i = 0; i < itemCount; i++) {
200 			_ViewAt(i)->RecordRevertSettings();
201 			_ViewAt(i)->ShowCurrentSettings();
202 		}
203 
204 		fSettingsTypeListView->Invalidate();
205 
206 		_UpdateButtons();
207 
208 		Unlock();
209 	}
210 	_inherited::Show();
211 }
212 
213 
214 SettingsView *
215 TrackerSettingsWindow::_ViewAt(int32 i)
216 {
217 	if (!Lock())
218 		return NULL;
219 
220 	SettingsItem *item = dynamic_cast<SettingsItem*>(fSettingsTypeListView->ItemAt(i));
221 
222 	Unlock();
223 
224 	return item->View();
225 }
226 
227 
228 BRect
229 TrackerSettingsWindow::_SettingsFrame()
230 {
231 	font_height fontHeight;
232 	be_bold_font->GetHeight(&fontHeight);
233 
234 	BRect rect = fSettingsContainerBox->Bounds().InsetByCopy(8, 8);
235 	rect.top += ceilf(fontHeight.ascent + fontHeight.descent);
236 
237 	return rect;
238 }
239 
240 
241 void
242 TrackerSettingsWindow::_HandleChangedContents()
243 {
244 	fSettingsTypeListView->Invalidate();
245 	_UpdateButtons();
246 
247 	TrackerSettings().SaveSettings(false);
248 }
249 
250 
251 void
252 TrackerSettingsWindow::_UpdateButtons()
253 {
254 	int32 itemCount = fSettingsTypeListView->CountItems();
255 
256 	bool defaultable = false;
257 	bool revertable = false;
258 
259 	for (int32 i = 0; i < itemCount; i++) {
260 		defaultable |= _ViewAt(i)->IsDefaultable();
261 		revertable |= _ViewAt(i)->IsRevertable();
262 	}
263 
264 	fDefaultsButton->SetEnabled(defaultable);
265 	fRevertButton->SetEnabled(revertable);
266 }
267 
268 
269 void
270 TrackerSettingsWindow::_HandlePressedDefaultsButton()
271 {
272 	int32 itemCount = fSettingsTypeListView->CountItems();
273 
274 	for (int32 i = 0; i < itemCount; i++) {
275 		if (_ViewAt(i)->IsDefaultable())
276 			_ViewAt(i)->SetDefaults();
277 	}
278 
279 	_HandleChangedContents();
280 }
281 
282 
283 void
284 TrackerSettingsWindow::_HandlePressedRevertButton()
285 {
286 	int32 itemCount = fSettingsTypeListView->CountItems();
287 
288 	for (int32 i = 0; i < itemCount; i++) {
289 		if (_ViewAt(i)->IsRevertable())
290 			_ViewAt(i)->Revert();
291 	}
292 
293 	_HandleChangedContents();
294 }
295 
296 
297 void
298 TrackerSettingsWindow::_HandleChangedSettingsView()
299 {
300 	int32 currentSelection = fSettingsTypeListView->CurrentSelection();
301 	if (currentSelection < 0)
302 		return;
303 
304 	BView *oldView = fSettingsContainerBox->ChildAt(0);
305 
306 	if (oldView)
307 		oldView->RemoveSelf();
308 
309 	SettingsItem *selectedItem =
310 		dynamic_cast<SettingsItem*>(fSettingsTypeListView->ItemAt(currentSelection));
311 
312 	if (selectedItem) {
313 		fSettingsContainerBox->SetLabel(selectedItem->Text());
314 
315 		BView *view = selectedItem->View();
316 		view->SetViewColor(fSettingsContainerBox->ViewColor());
317 		view->Hide();
318 		fSettingsContainerBox->AddChild(view);
319 
320 		// Resize view after it has been attached to the window, so that
321 		// it's resizing modes are respected
322 		BRect rect = _SettingsFrame();
323 		view->ResizeTo(rect.Width(), rect.Height());
324 		view->Show();
325 	}
326 }
327 
328 
329 //	#pragma mark -
330 
331 
332 SettingsItem::SettingsItem(const char *label, SettingsView *view)
333 	: BStringItem(label),
334 	fSettingsView(view)
335 {
336 }
337 
338 
339 void
340 SettingsItem::DrawItem(BView *owner, BRect rect, bool drawEverything)
341 {
342 	const rgb_color kModifiedColor = {0, 0, 255, 0};
343 	const rgb_color kBlack = {0, 0, 0, 0};
344 	const rgb_color kSelectedColor = {140, 140, 140, 0};
345 
346 	if (fSettingsView) {
347 		bool isRevertable = fSettingsView->IsRevertable();
348 		bool isSelected = IsSelected();
349 
350 		if (isSelected || drawEverything) {
351 			rgb_color color;
352 			if (isSelected)
353 				color = kSelectedColor;
354 			else
355 				color = owner->ViewColor();
356 
357 			owner->SetHighColor(color);
358 			owner->SetLowColor(color);
359 			owner->FillRect(rect);
360 		}
361 
362 		if (isRevertable)
363 			owner->SetHighColor(kModifiedColor);
364 		else
365 			owner->SetHighColor(kBlack);
366 
367 		font_height fheight;
368 		owner->GetFontHeight(&fheight);
369 
370 		owner->DrawString(Text(), BPoint(rect.left + 4, rect.top
371 			+ fheight.ascent + 2 + floorf(fheight.leading / 2)));
372 
373 		owner->SetHighColor(kBlack);
374 		owner->SetLowColor(owner->ViewColor());
375 	}
376 }
377 
378 
379 SettingsView *
380 SettingsItem::View()
381 {
382 	return fSettingsView;
383 }
384