xref: /haiku/src/kits/tracker/TrackerSettingsWindow.cpp (revision 32832cbe47f991cc6d2b29824903181d8baaaa63)
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()));
118 	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Windows"),
119 		new WindowsSettingsView()));
120 	fSettingsTypeListView->AddItem(new SettingsItem(
121 		B_TRANSLATE("Volume icons"), new SpaceBarSettingsView()));
122 
123 	// constraint the listview width so that the longest item fits
124 	float width = 0;
125 	fSettingsTypeListView->GetPreferredSize(&width, NULL);
126 	width += B_V_SCROLL_BAR_WIDTH;
127 	fSettingsTypeListView->SetExplicitMinSize(BSize(width, 0));
128 	fSettingsTypeListView->SetExplicitMaxSize(BSize(width, B_SIZE_UNLIMITED));
129 
130 	fSettingsTypeListView->SetSelectionMessage(
131 		new BMessage(kSettingsViewChanged));
132 	fSettingsTypeListView->Select(0);
133 }
134 
135 
136 bool
137 TrackerSettingsWindow::QuitRequested()
138 {
139 	if (IsHidden())
140 		return true;
141 
142 	Hide();
143 	return false;
144 }
145 
146 
147 void
148 TrackerSettingsWindow::MessageReceived(BMessage* message)
149 {
150 	switch (message->what) {
151 		case kSettingsContentsModified:
152 			_HandleChangedContents();
153 			break;
154 
155 		case kDefaultsButtonPressed:
156 			_HandlePressedDefaultsButton();
157 			break;
158 
159 		case kRevertButtonPressed:
160 			_HandlePressedRevertButton();
161 			break;
162 
163 		case kSettingsViewChanged:
164 			_HandleChangedSettingsView();
165 			break;
166 
167 		default:
168 			_inherited::MessageReceived(message);
169 			break;
170 	}
171 }
172 
173 
174 void
175 TrackerSettingsWindow::Show()
176 {
177 	if (Lock()) {
178 		int32 itemCount = fSettingsTypeListView->CountItems();
179 
180 		for (int32 i = 0; i < itemCount; i++) {
181 			_ViewAt(i)->RecordRevertSettings();
182 			_ViewAt(i)->ShowCurrentSettings();
183 		}
184 
185 		fSettingsTypeListView->Invalidate();
186 
187 		_UpdateButtons();
188 
189 		Unlock();
190 	}
191 
192 	if (IsHidden()) {
193 		// move to current workspace
194 		SetWorkspaces(B_CURRENT_WORKSPACE);
195 	}
196 
197 	_inherited::Show();
198 }
199 
200 
201 SettingsView*
202 TrackerSettingsWindow::_ViewAt(int32 i)
203 {
204 	if (!Lock())
205 		return NULL;
206 
207 	SettingsItem* item = dynamic_cast<SettingsItem*>
208 		(fSettingsTypeListView->ItemAt(i));
209 
210 	Unlock();
211 
212 	return item->View();
213 }
214 
215 
216 void
217 TrackerSettingsWindow::_HandleChangedContents()
218 {
219 	fSettingsTypeListView->Invalidate();
220 	_UpdateButtons();
221 
222 	TrackerSettings().SaveSettings(false);
223 }
224 
225 
226 void
227 TrackerSettingsWindow::_UpdateButtons()
228 {
229 	int32 itemCount = fSettingsTypeListView->CountItems();
230 
231 	bool defaultable = false;
232 	bool revertable = false;
233 
234 	for (int32 i = 0; i < itemCount; i++) {
235 		defaultable |= _ViewAt(i)->IsDefaultable();
236 		revertable |= _ViewAt(i)->IsRevertable();
237 	}
238 
239 	fDefaultsButton->SetEnabled(defaultable);
240 	fRevertButton->SetEnabled(revertable);
241 }
242 
243 
244 void
245 TrackerSettingsWindow::_HandlePressedDefaultsButton()
246 {
247 	int32 itemCount = fSettingsTypeListView->CountItems();
248 
249 	for (int32 i = 0; i < itemCount; i++) {
250 		if (_ViewAt(i)->IsDefaultable())
251 			_ViewAt(i)->SetDefaults();
252 	}
253 
254 	_HandleChangedContents();
255 }
256 
257 
258 void
259 TrackerSettingsWindow::_HandlePressedRevertButton()
260 {
261 	int32 itemCount = fSettingsTypeListView->CountItems();
262 
263 	for (int32 i = 0; i < itemCount; i++) {
264 		if (_ViewAt(i)->IsRevertable())
265 			_ViewAt(i)->Revert();
266 	}
267 
268 	_HandleChangedContents();
269 }
270 
271 
272 void
273 TrackerSettingsWindow::_HandleChangedSettingsView()
274 {
275 	int32 currentSelection = fSettingsTypeListView->CurrentSelection();
276 	if (currentSelection < 0)
277 		return;
278 
279 	BView* oldView = fSettingsContainerBox->ChildAt(0);
280 
281 	if (oldView != NULL)
282 		oldView->RemoveSelf();
283 
284 	SettingsItem* selectedItem =
285 		dynamic_cast<SettingsItem*>
286 			(fSettingsTypeListView->ItemAt(currentSelection));
287 
288 	if (selectedItem != NULL) {
289 		fSettingsContainerBox->SetLabel(selectedItem->Text());
290 
291 		BView* view = selectedItem->View();
292 		view->SetViewColor(fSettingsContainerBox->ViewColor());
293 		view->Hide();
294 		fSettingsContainerBox->AddChild(view);
295 
296 		view->Show();
297 	}
298 }
299 
300 
301 //	#pragma mark - SettingsItem
302 
303 
304 SettingsItem::SettingsItem(const char* label, SettingsView* view)
305 	:
306 	BStringItem(label),
307 	fSettingsView(view)
308 {
309 }
310 
311 
312 void
313 SettingsItem::DrawItem(BView* owner, BRect rect, bool drawEverything)
314 {
315 	if (fSettingsView) {
316 		bool isRevertable = fSettingsView->IsRevertable();
317 		bool isSelected = IsSelected();
318 
319 		if (isSelected || drawEverything) {
320 			rgb_color color;
321 			if (isSelected)
322 				color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
323 			else
324 				color = owner->ViewColor();
325 
326 			owner->SetHighColor(color);
327 			owner->SetLowColor(color);
328 			owner->FillRect(rect);
329 		}
330 
331 		if (isRevertable)
332 			owner->SetFont(be_bold_font);
333 		else
334 			owner->SetFont(be_plain_font);
335 
336 		if (isSelected)
337 			owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
338 		else
339 			owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
340 
341 		font_height fheight;
342 		owner->GetFontHeight(&fheight);
343 
344 		owner->DrawString(Text(),
345 			BPoint(rect.left + be_control_look->DefaultLabelSpacing(),
346 				rect.top + fheight.ascent + 2 + floorf(fheight.leading / 2)));
347 
348 		owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
349 		owner->SetLowColor(owner->ViewColor());
350 	}
351 }
352 
353 
354 SettingsView*
355 SettingsItem::View()
356 {
357 	return fSettingsView;
358 }
359