xref: /haiku/src/kits/tracker/TrackerSettingsWindow.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
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 #include <Catalog.h>
36 #include <ControlLook.h>
37 #include <LayoutBuilder.h>
38 #include <Locale.h>
39 
40 #include "SettingsViews.h"
41 #include "TrackerSettings.h"
42 #include "TrackerSettingsWindow.h"
43 
44 #include <ScrollView.h>
45 
46 
47 namespace BPrivate {
48 
49 class SettingsItem : public BStringItem {
50 	public:
51 		SettingsItem(const char *label, SettingsView *view);
52 
53 		void DrawItem(BView *owner, BRect rect, bool drawEverything);
54 
55 		SettingsView *View();
56 
57 	private:
58 		SettingsView *fSettingsView;
59 };
60 
61 }	// namespace BPrivate
62 
63 
64 const uint32 kSettingsViewChanged = 'Svch';
65 const uint32 kDefaultsButtonPressed = 'Apbp';
66 const uint32 kRevertButtonPressed = 'Rebp';
67 
68 
69 #undef B_TRANSLATE_CONTEXT
70 #define B_TRANSLATE_CONTEXT "TrackerSettingsWindow"
71 
72 TrackerSettingsWindow::TrackerSettingsWindow()
73 	:
74 	BWindow(BRect(80, 80, 450, 350), B_TRANSLATE("Tracker preferences"),
75 		B_TITLED_WINDOW, B_NOT_MINIMIZABLE | B_NOT_RESIZABLE
76 		| B_NO_WORKSPACE_ACTIVATION	| B_NOT_ANCHORED_ON_ACTIVATE
77 		| B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
78 {
79 	fSettingsTypeListView = new BListView("List View", B_SINGLE_SELECTION_LIST);
80 
81 	BScrollView* scrollView = new BScrollView("scrollview",
82 		fSettingsTypeListView, B_FRAME_EVENTS | B_WILL_DRAW, false, true);
83 
84 	fDefaultsButton = new BButton("Defaults", B_TRANSLATE("Defaults"),
85 		new BMessage(kDefaultsButtonPressed));
86 	fDefaultsButton->SetEnabled(false);
87 
88 	fRevertButton = new BButton("Revert", B_TRANSLATE("Revert"),
89 		new BMessage(kRevertButtonPressed));
90 	fRevertButton->SetEnabled(false);
91 
92 	fSettingsContainerBox = new BBox("SettingsContainerBox");
93 
94 	const float spacing = be_control_look->DefaultItemSpacing();
95 
96 	BLayoutBuilder::Group<>(this)
97 		.AddGroup(B_HORIZONTAL, spacing)
98 			.Add(scrollView)
99 			.AddGroup(B_VERTICAL, spacing)
100 				.Add(fSettingsContainerBox)
101 				.AddGroup(B_HORIZONTAL, spacing)
102 					.Add(fDefaultsButton)
103 					.Add(fRevertButton)
104 					.AddGlue()
105 				.End()
106 			.End()
107 		.SetInsets(spacing, spacing, spacing, spacing)
108 		.End();
109 
110 	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Desktop"),
111 		new DesktopSettingsView()));
112 	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Windows"),
113 		new WindowsSettingsView()));
114 	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Trash"),
115 		new TrashSettingsView()));
116 	fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Volume icons"),
117 		new SpaceBarSettingsView()));
118 
119 	// constraint the listview width so that the longest item fits
120 	float width = 0;
121 	fSettingsTypeListView->GetPreferredSize(&width, NULL);
122 	width += B_V_SCROLL_BAR_WIDTH;
123 	fSettingsTypeListView->SetExplicitMinSize(BSize(width, 0));
124 	fSettingsTypeListView->SetExplicitMaxSize(BSize(width, B_SIZE_UNLIMITED));
125 
126 	fSettingsTypeListView->SetSelectionMessage(new BMessage(kSettingsViewChanged));
127 	fSettingsTypeListView->Select(0);
128 }
129 
130 
131 bool
132 TrackerSettingsWindow::QuitRequested()
133 {
134 	bool isHidden = false;
135 
136 	if (Lock()) {
137 		isHidden = IsHidden();
138 		Unlock();
139 	} else
140 		return true;
141 
142 	if (isHidden)
143 		return true;
144 
145 	Hide();
146 
147 	return false;
148 }
149 
150 
151 void
152 TrackerSettingsWindow::MessageReceived(BMessage *message)
153 {
154 	switch (message->what) {
155 		case kSettingsContentsModified:
156 			_HandleChangedContents();
157 			break;
158 
159 		case kDefaultsButtonPressed:
160 			_HandlePressedDefaultsButton();
161 			break;
162 
163 		case kRevertButtonPressed:
164 			_HandlePressedRevertButton();
165 			break;
166 
167 		case kSettingsViewChanged:
168 			_HandleChangedSettingsView();
169 			break;
170 
171 		default:
172 			_inherited::MessageReceived(message);
173 	}
174 }
175 
176 
177 void
178 TrackerSettingsWindow::Show()
179 {
180 	if (Lock()) {
181 		int32 itemCount = fSettingsTypeListView->CountItems();
182 
183 		for (int32 i = 0; i < itemCount; i++) {
184 			_ViewAt(i)->RecordRevertSettings();
185 			_ViewAt(i)->ShowCurrentSettings();
186 		}
187 
188 		fSettingsTypeListView->Invalidate();
189 
190 		_UpdateButtons();
191 
192 		Unlock();
193 	}
194 	_inherited::Show();
195 }
196 
197 
198 SettingsView *
199 TrackerSettingsWindow::_ViewAt(int32 i)
200 {
201 	if (!Lock())
202 		return NULL;
203 
204 	SettingsItem *item = dynamic_cast<SettingsItem*>(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)
278 		oldView->RemoveSelf();
279 
280 	SettingsItem *selectedItem =
281 		dynamic_cast<SettingsItem*>(fSettingsTypeListView->ItemAt(currentSelection));
282 
283 	if (selectedItem) {
284 		fSettingsContainerBox->SetLabel(selectedItem->Text());
285 
286 		BView *view = selectedItem->View();
287 		view->SetViewColor(fSettingsContainerBox->ViewColor());
288 		view->Hide();
289 		fSettingsContainerBox->AddChild(view);
290 
291 		view->Show();
292 	}
293 }
294 
295 
296 //	#pragma mark -
297 
298 
299 SettingsItem::SettingsItem(const char *label, SettingsView *view)
300 	: BStringItem(label),
301 	fSettingsView(view)
302 {
303 }
304 
305 
306 void
307 SettingsItem::DrawItem(BView *owner, BRect rect, bool drawEverything)
308 {
309 	const rgb_color kModifiedColor = {0, 0, 255, 0};
310 	const rgb_color kBlack = {0, 0, 0, 0};
311 	const rgb_color kSelectedColor = {140, 140, 140, 0};
312 
313 	if (fSettingsView) {
314 		bool isRevertable = fSettingsView->IsRevertable();
315 		bool isSelected = IsSelected();
316 
317 		if (isSelected || drawEverything) {
318 			rgb_color color;
319 			if (isSelected)
320 				color = kSelectedColor;
321 			else
322 				color = owner->ViewColor();
323 
324 			owner->SetHighColor(color);
325 			owner->SetLowColor(color);
326 			owner->FillRect(rect);
327 		}
328 
329 		if (isRevertable)
330 			owner->SetHighColor(kModifiedColor);
331 		else
332 			owner->SetHighColor(kBlack);
333 
334 		font_height fheight;
335 		owner->GetFontHeight(&fheight);
336 
337 		owner->DrawString(Text(), BPoint(rect.left + 4, rect.top
338 			+ fheight.ascent + 2 + floorf(fheight.leading / 2)));
339 
340 		owner->SetHighColor(kBlack);
341 		owner->SetLowColor(owner->ViewColor());
342 	}
343 }
344 
345 
346 SettingsView *
347 SettingsItem::View()
348 {
349 	return fSettingsView;
350 }
351