xref: /haiku/src/kits/tracker/TrackerSettingsWindow.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
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->MoveBy(0, -fDefaultsButton->Bounds().Height());
92 	topView->AddChild(fDefaultsButton);
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 	topView->AddChild(fRevertButton);
101 
102 	rect = scrollView->Frame();
103 	rect.left = rect.right + 10;
104 	rect.right = Bounds().right - 10;
105 	rect.bottom = fDefaultsButton->Frame().top - 10;
106 	fSettingsContainerBox = new BBox(rect, NULL, B_FOLLOW_ALL);
107 	topView->AddChild(fSettingsContainerBox);
108 
109 	rect = _SettingsFrame();
110 
111 	fSettingsTypeListView->AddItem(new SettingsItem("Desktop",
112 		new DesktopSettingsView(rect)));
113 	fSettingsTypeListView->AddItem(new SettingsItem("Windows",
114 		new WindowsSettingsView(rect)));
115 	fSettingsTypeListView->AddItem(new SettingsItem("Date and Time",
116 		new TimeFormatSettingsView(rect)));
117 	fSettingsTypeListView->AddItem(new SettingsItem("Trash",
118 		new TrashSettingsView(rect)));
119 	fSettingsTypeListView->AddItem(new SettingsItem("Volume Icons",
120 		new SpaceBarSettingsView(rect)));
121 
122 	// compute preferred view size
123 
124 	float minWidth = 0, minHeight = 0;
125 
126 	for (int32 i = 0; i < fSettingsTypeListView->CountItems(); i++) {
127 		SettingsView* view = ((SettingsItem *)fSettingsTypeListView->ItemAt(i))->View();
128 
129 		float width, height;
130 		view->GetPreferredSize(&width, &height);
131 
132 		if (minWidth < width)
133 			minWidth = width;
134 		if (minHeight < height)
135 			minHeight = height;
136 	}
137 
138 	ResizeBy(max_c(minWidth - rect.Width(), 0), max_c(minHeight - rect.Height(), 0));
139 		// make sure window is large enough to contain all views
140 
141 	fSettingsTypeListView->SetSelectionMessage(new BMessage(kSettingsViewChanged));
142 	fSettingsTypeListView->Select(0);
143 }
144 
145 
146 bool
147 TrackerSettingsWindow::QuitRequested()
148 {
149 	bool isHidden = false;
150 
151 	if (Lock()) {
152 		isHidden = IsHidden();
153 		Unlock();
154 	} else
155 		return true;
156 
157 	if (isHidden)
158 		return true;
159 
160 	Hide();
161 
162 	return false;
163 }
164 
165 
166 void
167 TrackerSettingsWindow::MessageReceived(BMessage *message)
168 {
169 	switch (message->what) {
170 		case kSettingsContentsModified:
171 			_HandleChangedContents();
172 			break;
173 
174 		case kDefaultsButtonPressed:
175 			_HandlePressedDefaultsButton();
176 			break;
177 
178 		case kRevertButtonPressed:
179 			_HandlePressedRevertButton();
180 			break;
181 
182 		case kSettingsViewChanged:
183 			_HandleChangedSettingsView();
184 			break;
185 
186 		default:
187 			_inherited::MessageReceived(message);
188 	}
189 }
190 
191 
192 void
193 TrackerSettingsWindow::Show()
194 {
195 	if (Lock()) {
196 		int32 itemCount = fSettingsTypeListView->CountItems();
197 
198 		for (int32 i = 0; i < itemCount; i++) {
199 			_ViewAt(i)->RecordRevertSettings();
200 			_ViewAt(i)->ShowCurrentSettings();
201 		}
202 
203 		fSettingsTypeListView->Invalidate();
204 
205 		Unlock();
206 	}
207 	_inherited::Show();
208 }
209 
210 
211 SettingsView *
212 TrackerSettingsWindow::_ViewAt(int32 i)
213 {
214 	if (!Lock())
215 		return NULL;
216 
217 	SettingsItem *item = dynamic_cast<SettingsItem*>(fSettingsTypeListView->ItemAt(i));
218 
219 	Unlock();
220 
221 	return item->View();
222 }
223 
224 
225 BRect
226 TrackerSettingsWindow::_SettingsFrame()
227 {
228 	font_height fontHeight;
229 	be_bold_font->GetHeight(&fontHeight);
230 
231 	BRect rect = fSettingsContainerBox->Bounds().InsetByCopy(8, 8);
232 	rect.top += ceilf(fontHeight.ascent + fontHeight.descent);
233 
234 	return rect;
235 }
236 
237 
238 void
239 TrackerSettingsWindow::_HandleChangedContents()
240 {
241 	int32 itemCount = fSettingsTypeListView->CountItems();
242 
243 	bool revertable = false;
244 
245 	for (int32 i = 0; i < itemCount; i++) {
246 		revertable |= _ViewAt(i)->IsRevertable();
247 	}
248 
249 	fSettingsTypeListView->Invalidate();
250 	fRevertButton->SetEnabled(revertable);
251 
252 	TrackerSettings().SaveSettings(false);
253 }
254 
255 
256 void
257 TrackerSettingsWindow::_HandlePressedDefaultsButton()
258 {
259 	int32 itemCount = fSettingsTypeListView->CountItems();
260 
261 	for (int32 i = 0; i < itemCount; i++)
262 		_ViewAt(i)->SetDefaults();
263 
264 	_HandleChangedContents();
265 }
266 
267 
268 void
269 TrackerSettingsWindow::_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 
282 void
283 TrackerSettingsWindow::_HandleChangedSettingsView()
284 {
285 	int32 currentSelection = fSettingsTypeListView->CurrentSelection();
286 	if (currentSelection < 0)
287 		return;
288 
289 	BView *oldView = fSettingsContainerBox->ChildAt(0);
290 
291 	if (oldView)
292 		oldView->RemoveSelf();
293 
294 	SettingsItem *selectedItem =
295 		dynamic_cast<SettingsItem*>(fSettingsTypeListView->ItemAt(currentSelection));
296 
297 	if (selectedItem) {
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 		// Resize view after it has been attached to the window, so that
306 		// it's resizing modes are respected
307 		BRect rect = _SettingsFrame();
308 		view->ResizeTo(rect.Width(), rect.Height());
309 		view->Show();
310 	}
311 }
312 
313 
314 //	#pragma mark -
315 
316 
317 SettingsItem::SettingsItem(const char *label, SettingsView *view)
318 	: BStringItem(label),
319 	fSettingsView(view)
320 {
321 }
322 
323 
324 void
325 SettingsItem::DrawItem(BView *owner, BRect rect, bool drawEverything)
326 {
327 	const rgb_color kModifiedColor = {0, 0, 255, 0};
328 	const rgb_color kBlack = {0, 0, 0, 0};
329 	const rgb_color kSelectedColor = {140, 140, 140, 0};
330 
331 	if (fSettingsView) {
332 		bool isRevertable = fSettingsView->IsRevertable();
333 		bool isSelected = IsSelected();
334 
335 		if (isSelected || drawEverything) {
336 			rgb_color color;
337 			if (isSelected)
338 				color = kSelectedColor;
339 			else
340 				color = owner->ViewColor();
341 
342 			owner->SetHighColor(color);
343 			owner->SetLowColor(color);
344 			owner->FillRect(rect);
345 		}
346 
347 		if (isRevertable)
348 			owner->SetHighColor(kModifiedColor);
349 		else
350 			owner->SetHighColor(kBlack);
351 
352 		owner->MovePenTo(rect.left + 4, rect.bottom - 2);
353 
354 		owner->DrawString(Text());
355 
356 		owner->SetHighColor(kBlack);
357 		owner->SetLowColor(owner->ViewColor());
358 	}
359 }
360 
361 
362 SettingsView *
363 SettingsItem::View()
364 {
365 	return fSettingsView;
366 }
367