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