xref: /haiku/src/apps/deskbar/PreferencesWindow.cpp (revision 95e02d49729b6639eee2368bf96508fc17cf9be0)
1 /*
2  * Copyright 2009-2011 Haiku, Inc.
3  * All Rights Reserved. Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jonas Sundström, jonas@kirilla.com
7  */
8 
9 
10 #include "PreferencesWindow.h"
11 
12 #include <ctype.h>
13 
14 #include <Catalog.h>
15 #include <CheckBox.h>
16 #include <FormattingConventions.h>
17 #include <GroupLayout.h>
18 #include <Locale.h>
19 #include <LayoutBuilder.h>
20 #include <OpenWithTracker.h>
21 #include <RadioButton.h>
22 #include <SeparatorView.h>
23 #include <Slider.h>
24 #include <StringView.h>
25 #include <View.h>
26 
27 #include "BarApp.h"
28 #include "StatusView.h"
29 
30 
31 #undef B_TRANSLATE_CONTEXT
32 #define B_TRANSLATE_CONTEXT "PreferencesWindow"
33 
34 PreferencesWindow::PreferencesWindow(BRect frame)
35 	:
36 	BWindow(frame, B_TRANSLATE("Deskbar preferences"), B_TITLED_WINDOW,
37 		B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE)
38 {
39 	// Menu controls
40 	fMenuRecentDocuments = new BCheckBox(B_TRANSLATE("Recent documents:"),
41 		new BMessage(kUpdateRecentCounts));
42 	fMenuRecentApplications = new BCheckBox(B_TRANSLATE("Recent applications:"),
43 		new BMessage(kUpdateRecentCounts));
44 	fMenuRecentFolders = new BCheckBox(B_TRANSLATE("Recent folders:"),
45 		new BMessage(kUpdateRecentCounts));
46 
47 	fMenuRecentDocumentCount = new BTextControl(NULL, NULL,
48 		new BMessage(kUpdateRecentCounts));
49 	fMenuRecentApplicationCount = new BTextControl(NULL, NULL,
50 		new BMessage(kUpdateRecentCounts));
51 	fMenuRecentFolderCount = new BTextControl(NULL, NULL,
52 		new BMessage(kUpdateRecentCounts));
53 
54 	// Applications controls
55 	fAppsSort = new BCheckBox(B_TRANSLATE("Sort running applications"),
56 		new BMessage(kSortRunningApps));
57 	fAppsSortTrackerFirst = new BCheckBox(B_TRANSLATE("Tracker always first"),
58 		new BMessage(kTrackerFirst));
59 	fAppsShowExpanders = new BCheckBox(B_TRANSLATE("Show application expander"),
60 		new BMessage(kSuperExpando));
61 	fAppsExpandNew = new BCheckBox(B_TRANSLATE("Expand new applications"),
62 		new BMessage(kExpandNewTeams));
63 	fAppsHideLabels = new BCheckBox(B_TRANSLATE("Hide application names"),
64 		new BMessage(kHideLabels));
65 	fAppsIconSizeSlider = new BSlider("icon_size", B_TRANSLATE("Icon size"),
66 		NULL, kMinimumIconSize / kIconSizeInterval,
67 		kMaximumIconSize / kIconSizeInterval, B_HORIZONTAL);
68 	fAppsIconSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
69 	fAppsIconSizeSlider->SetHashMarkCount((kMaximumIconSize - kMinimumIconSize)
70 		/ kIconSizeInterval + 1);
71 	fAppsIconSizeSlider->SetLimitLabels(B_TRANSLATE("Small"),
72 		B_TRANSLATE("Large"));
73 	fAppsIconSizeSlider->SetModificationMessage(new BMessage(kResizeTeamIcons));
74 
75 	// Window controls
76 	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
77 		new BMessage(kAlwaysTop));
78 	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
79 		new BMessage(kAutoRaise));
80 	fWindowAutoHide = new BCheckBox(B_TRANSLATE("Auto-hide"),
81 		new BMessage(kAutoHide));
82 
83 	// Clock controls
84 	BMessage* timeInterval12HoursMessage = new BMessage(kTimeIntervalChanged);
85 	timeInterval12HoursMessage->AddBool("use24HourClock", false);
86 	fTimeInterval12HourRadioButton = new BRadioButton("time inteval",
87 		B_TRANSLATE("12 hour"), timeInterval12HoursMessage);
88 
89 	BMessage* timeInterval24HoursMessage = new BMessage(kTimeIntervalChanged);
90 	timeInterval24HoursMessage->AddBool("use24HourClock", true);
91 	fTimeInterval24HourRadioButton = new BRadioButton("time inteval",
92 		B_TRANSLATE("24 hour"), timeInterval24HoursMessage);
93 
94 	BMessage* timeFormatShortMessage = new BMessage(kTimeFormatChanged);
95 	timeFormatShortMessage->AddUInt32("time format", B_SHORT_TIME_FORMAT);
96 	fTimeFormatShortRadioButton = new BRadioButton("time format",
97 		"Short", timeFormatShortMessage);
98 
99 	BMessage* timeFormatMediumMessage = new BMessage(kTimeFormatChanged);
100 	timeFormatMediumMessage->AddUInt32("time format", B_MEDIUM_TIME_FORMAT);
101 	fTimeFormatMediumRadioButton = new BRadioButton("time format",
102 		"Medium", timeFormatMediumMessage);
103 
104 	BMessage* timeFormatLongMessage = new BMessage(kTimeFormatChanged);
105 	timeFormatLongMessage->AddUInt32("time format", B_LONG_TIME_FORMAT);
106 	fTimeFormatLongRadioButton = new BRadioButton("time format",
107 		"Long", timeFormatLongMessage);
108 
109 	_UpdateTimeFormatRadioButtonLabels();
110 
111 	// Get settings from BarApp
112 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
113 	desk_settings* settings = barApp->Settings();
114 
115 	// Menu settings
116 	BTextView* docTextView = fMenuRecentDocumentCount->TextView();
117 	BTextView* appTextView = fMenuRecentApplicationCount->TextView();
118 	BTextView* folderTextView = fMenuRecentFolderCount->TextView();
119 
120 	for (int32 i = 0; i < 256; i++) {
121 		if (!isdigit(i)) {
122 			docTextView->DisallowChar(i);
123 			appTextView->DisallowChar(i);
124 			folderTextView->DisallowChar(i);
125 		}
126 	}
127 
128 	docTextView->SetMaxBytes(4);
129 	appTextView->SetMaxBytes(4);
130 	folderTextView->SetMaxBytes(4);
131 
132 	int32 docCount = settings->recentDocsCount;
133 	int32 appCount = settings->recentAppsCount;
134 	int32 folderCount = settings->recentFoldersCount;
135 
136 	fMenuRecentDocuments->SetValue(settings->recentDocsEnabled);
137 	fMenuRecentDocumentCount->SetEnabled(settings->recentDocsEnabled);
138 
139 	fMenuRecentApplications->SetValue(settings->recentAppsEnabled);
140 	fMenuRecentApplicationCount->SetEnabled(settings->recentAppsEnabled);
141 
142 	fMenuRecentFolders->SetValue(settings->recentFoldersEnabled);
143 	fMenuRecentFolderCount->SetEnabled(settings->recentFoldersEnabled);
144 
145 	BString docString;
146 	BString appString;
147 	BString folderString;
148 
149 	docString << docCount;
150 	appString << appCount;
151 	folderString << folderCount;
152 
153 	fMenuRecentDocumentCount->SetText(docString.String());
154 	fMenuRecentApplicationCount->SetText(appString.String());
155 	fMenuRecentFolderCount->SetText(folderString.String());
156 
157 	// Applications settings
158 	fAppsSort->SetValue(settings->sortRunningApps);
159 	fAppsSortTrackerFirst->SetValue(settings->trackerAlwaysFirst);
160 	fAppsShowExpanders->SetValue(settings->superExpando);
161 	fAppsExpandNew->SetValue(settings->expandNewTeams);
162 	fAppsHideLabels->SetValue(settings->hideLabels);
163 	fAppsIconSizeSlider->SetValue(settings->iconSize / kIconSizeInterval);
164 
165 	// Window settings
166 	fWindowAlwaysOnTop->SetValue(settings->alwaysOnTop);
167 	fWindowAutoRaise->SetValue(settings->autoRaise);
168 	fWindowAutoHide->SetValue(settings->autoHide);
169 
170 	// Clock settings
171 	BFormattingConventions conventions;
172 	BLocale::Default()->GetFormattingConventions(&conventions);
173 	if (conventions.Use24HourClock())
174 		fTimeInterval24HourRadioButton->SetValue(B_CONTROL_ON);
175 	else
176 		fTimeInterval12HourRadioButton->SetValue(B_CONTROL_ON);
177 
178 	switch (settings->timeFormat) {
179 		case B_LONG_TIME_FORMAT:
180 			fTimeFormatLongRadioButton->SetValue(B_CONTROL_ON);
181 			break;
182 		case B_MEDIUM_TIME_FORMAT:
183 			fTimeFormatMediumRadioButton->SetValue(B_CONTROL_ON);
184 			break;
185 		default:
186 			fTimeFormatShortRadioButton->SetValue(B_CONTROL_ON);
187 	}
188 
189 	_EnableDisableDependentItems();
190 
191 	// Targets
192 	fAppsSort->SetTarget(be_app);
193 	fAppsSortTrackerFirst->SetTarget(be_app);
194 	fAppsExpandNew->SetTarget(be_app);
195 	fAppsHideLabels->SetTarget(be_app);
196 	fAppsIconSizeSlider->SetTarget(be_app);
197 
198 	fWindowAlwaysOnTop->SetTarget(be_app);
199 	fWindowAutoRaise->SetTarget(be_app);
200 	fWindowAutoHide->SetTarget(be_app);
201 
202 	TReplicantTray* replicantTray = barApp->BarView()->fReplicantTray;
203 	fTimeInterval12HourRadioButton->SetTarget(replicantTray);
204 	fTimeInterval24HourRadioButton->SetTarget(replicantTray);
205 	fTimeFormatShortRadioButton->SetTarget(replicantTray);
206 	fTimeFormatMediumRadioButton->SetTarget(replicantTray);
207 	fTimeFormatLongRadioButton->SetTarget(replicantTray);
208 
209 	// Layout
210 	fMenuBox = new BBox("fMenuBox");
211 	fAppsBox = new BBox("fAppsBox");
212 	fWindowBox = new BBox("fWindowBox");
213 	fClockBox = new BBox("fClockBox");
214 
215 	fMenuBox->SetLabel(B_TRANSLATE("Menu"));
216 	fAppsBox->SetLabel(B_TRANSLATE("Applications"));
217 	fWindowBox->SetLabel(B_TRANSLATE("Window"));
218 	fClockBox->SetLabel(B_TRANSLATE("Clock"));
219 
220 	BView* view;
221 	view = BLayoutBuilder::Group<>()
222 		.AddGroup(B_VERTICAL, 10)
223 			.AddGroup(B_HORIZONTAL, 0)
224 				.AddGroup(B_VERTICAL, 0)
225 					.Add(fMenuRecentDocuments)
226 					.Add(fMenuRecentFolders)
227 					.Add(fMenuRecentApplications)
228 					.End()
229 				.AddGroup(B_VERTICAL, 0)
230 					.Add(fMenuRecentDocumentCount)
231 					.Add(fMenuRecentFolderCount)
232 					.Add(fMenuRecentApplicationCount)
233 					.End()
234 				.End()
235 			.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
236 				new BMessage(kEditMenuInTracker)))
237 			.SetInsets(10, 10, 10, 10)
238 			.End()
239 		.View();
240 	fMenuBox->AddChild(view);
241 
242 	view = BLayoutBuilder::Group<>()
243 		.AddGroup(B_VERTICAL, 1)
244 			.Add(fAppsSort)
245 			.Add(fAppsSortTrackerFirst)
246 			.Add(fAppsShowExpanders)
247 			.AddGroup(B_HORIZONTAL, 0)
248 				.SetInsets(20, 0, 0, 0)
249 				.Add(fAppsExpandNew)
250 				.End()
251 			.Add(fAppsIconSizeSlider)
252 			.Add(fAppsHideLabels)
253 			.AddGlue()
254 			.SetInsets(10, 10, 10, 10)
255 			.End()
256 		.View();
257 	fAppsBox->AddChild(view);
258 
259 	view = BLayoutBuilder::Group<>()
260 		.AddGroup(B_VERTICAL, 1)
261 			.Add(fWindowAlwaysOnTop)
262 			.Add(fWindowAutoRaise)
263 			.Add(fWindowAutoHide)
264 			.AddGlue()
265 			.SetInsets(10, 10, 10, 10)
266 			.End()
267 		.View();
268 	fWindowBox->AddChild(view);
269 
270 	BStringView* timeIntervalLabel = new BStringView("interval",
271 		B_TRANSLATE("Interval"));
272 	timeIntervalLabel->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED,
273 		B_SIZE_UNSET));
274 	timeIntervalLabel->SetLowColor((rgb_color){255, 255, 255, 255});
275 
276 	BGroupLayout* timeIntervalLayout = new BGroupLayout(B_VERTICAL, 0);
277 	timeIntervalLayout->SetInsets(10, 0, 0, 0);
278 	BView* timeIntervalView = new BView("interval", 0, timeIntervalLayout);
279 	timeIntervalView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
280 	timeIntervalView->SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
281 	timeIntervalView->AddChild(fTimeInterval12HourRadioButton);
282 	timeIntervalView->AddChild(fTimeInterval24HourRadioButton);
283 
284 	BStringView* timeFormatLabel = new BStringView("format",
285 		B_TRANSLATE("Format"));
286 	timeFormatLabel->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED,
287 		B_SIZE_UNSET));
288 	timeFormatLabel->SetLowColor((rgb_color){255, 255, 255, 255});
289 
290 	BGroupLayout* timeFormatLayout = new BGroupLayout(B_VERTICAL, 0);
291 	timeFormatLayout->SetInsets(10, 0, 0, 0);
292 	BView* timeFormatView = new BView("format", 0, timeFormatLayout);
293 	timeFormatView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
294 	timeFormatView->SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
295 	timeFormatView->AddChild(fTimeFormatShortRadioButton);
296 	timeFormatView->AddChild(fTimeFormatMediumRadioButton);
297 	timeFormatView->AddChild(fTimeFormatLongRadioButton);
298 
299 	view = BLayoutBuilder::Group<>()
300 		.AddGroup(B_VERTICAL, 10)
301 			.AddGroup(B_VERTICAL, 0)
302 				.Add(timeIntervalLabel)
303 				.Add(timeIntervalView)
304 				.End()
305 			.AddGroup(B_VERTICAL, 0)
306 				.Add(timeFormatLabel)
307 				.Add(timeFormatView)
308 				.End()
309 			.AddGlue()
310 			.SetInsets(10, 10, 10, 10)
311 			.End()
312 		.View();
313 	fClockBox->AddChild(view);
314 
315 	BLayoutBuilder::Group<>(this)
316 		.AddGrid(5, 5)
317 			.Add(fMenuBox, 0, 0)
318 			.Add(fWindowBox, 1, 0)
319 			.Add(fAppsBox, 0, 1)
320 			.Add(fClockBox, 1, 1)
321 			.SetInsets(10, 10, 10, 10)
322 			.End()
323 		.End();
324 
325 	CenterOnScreen();
326 }
327 
328 
329 PreferencesWindow::~PreferencesWindow()
330 {
331 	_UpdateRecentCounts();
332 	be_app->PostMessage(kConfigClose);
333 }
334 
335 
336 void
337 PreferencesWindow::MessageReceived(BMessage* message)
338 {
339 	switch (message->what) {
340 		case kEditMenuInTracker:
341 			OpenWithTracker(B_USER_DESKBAR_DIRECTORY);
342 			break;
343 
344 		case kUpdateRecentCounts:
345 			_UpdateRecentCounts();
346 			break;
347 
348 		case kSuperExpando:
349 			_EnableDisableDependentItems();
350 			be_app->PostMessage(message);
351 			break;
352 
353 		case kStateChanged:
354 			_EnableDisableDependentItems();
355 			break;
356 
357 		default:
358 			BWindow::MessageReceived(message);
359 			break;
360 	}
361 }
362 
363 
364 void
365 PreferencesWindow::WindowActivated(bool active)
366 {
367 	if (!active && IsMinimized())
368 		PostMessage(B_QUIT_REQUESTED);
369 }
370 
371 
372 void
373 PreferencesWindow::_UpdateRecentCounts()
374 {
375 	BMessage message(kUpdateRecentCounts);
376 
377 	int32 docCount = atoi(fMenuRecentDocumentCount->Text());
378 	int32 appCount = atoi(fMenuRecentApplicationCount->Text());
379 	int32 folderCount = atoi(fMenuRecentFolderCount->Text());
380 
381 	message.AddInt32("documents", max_c(0, docCount));
382 	message.AddInt32("applications", max_c(0, appCount));
383 	message.AddInt32("folders", max_c(0, folderCount));
384 
385 	message.AddBool("documentsEnabled", fMenuRecentDocuments->Value());
386 	message.AddBool("applicationsEnabled", fMenuRecentApplications->Value());
387 	message.AddBool("foldersEnabled", fMenuRecentFolders->Value());
388 
389 	be_app->PostMessage(&message);
390 
391 	_EnableDisableDependentItems();
392 }
393 
394 
395 void
396 PreferencesWindow::_EnableDisableDependentItems()
397 {
398 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
399 	if (barApp->BarView()->Vertical()
400 		&& barApp->BarView()->ExpandoState()) {
401 		fAppsShowExpanders->SetEnabled(true);
402 		fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value());
403 	} else {
404 		fAppsShowExpanders->SetEnabled(false);
405 		fAppsExpandNew->SetEnabled(false);
406 	}
407 
408 	fMenuRecentDocumentCount->SetEnabled(
409 		fMenuRecentDocuments->Value() != B_CONTROL_OFF);
410 	fMenuRecentApplicationCount->SetEnabled(
411 		fMenuRecentApplications->Value() != B_CONTROL_OFF);
412 	fMenuRecentFolderCount->SetEnabled(
413 		fMenuRecentFolders->Value() != B_CONTROL_OFF);
414 
415 	fWindowAutoRaise->SetEnabled(
416 		fWindowAlwaysOnTop->Value() == B_CONTROL_OFF);
417 }
418 
419 
420 void
421 PreferencesWindow::_UpdateTimeFormatRadioButtonLabels()
422 {
423 	time_t timeValue = (time_t)time(NULL);
424 	BString result;
425 
426 	BLocale::Default()->FormatTime(&result, timeValue, B_SHORT_TIME_FORMAT);
427 	fTimeFormatShortRadioButton->SetLabel(result);
428 
429 	BLocale::Default()->FormatTime(&result, timeValue, B_MEDIUM_TIME_FORMAT);
430 	fTimeFormatMediumRadioButton->SetLabel(result);
431 
432 	BLocale::Default()->FormatTime(&result, timeValue, B_LONG_TIME_FORMAT);
433 	fTimeFormatLongRadioButton->SetLabel(result);
434 }
435