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