xref: /haiku/src/apps/deskbar/PreferencesWindow.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
1 /*
2  * Copyright 2009-2011 Haiku, Inc.
3  * All Rights Reserved. Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		John Scipione, jscipione@gmail.com
7  *		Jonas Sundström, jonas@kirilla.com
8  */
9 
10 
11 #include "PreferencesWindow.h"
12 
13 #include <ctype.h>
14 
15 #include <Box.h>
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <CheckBox.h>
19 #include <ControlLook.h>
20 #include <FormattingConventions.h>
21 #include <GroupLayout.h>
22 #include <Locale.h>
23 #include <LayoutBuilder.h>
24 #include <OpenWithTracker.h>
25 #include <RadioButton.h>
26 #include <Roster.h>
27 #include <SeparatorView.h>
28 #include <Slider.h>
29 #include <StringView.h>
30 #include <TextControl.h>
31 #include <View.h>
32 
33 #include "BarApp.h"
34 #include "StatusView.h"
35 
36 
37 static const float kIndentSpacing
38 	= be_control_look->DefaultItemSpacing() * 2.3;
39 
40 
41 #undef B_TRANSLATION_CONTEXT
42 #define B_TRANSLATION_CONTEXT "PreferencesWindow"
43 
44 
45 PreferencesWindow::PreferencesWindow(BRect frame)
46 	:
47 	BWindow(frame, B_TRANSLATE("Deskbar preferences"), B_TITLED_WINDOW,
48 		B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE)
49 {
50 	// Menu controls
51 	fMenuRecentDocuments = new BCheckBox(B_TRANSLATE("Recent documents:"),
52 		new BMessage(kUpdateRecentCounts));
53 	fMenuRecentApplications = new BCheckBox(B_TRANSLATE("Recent applications:"),
54 		new BMessage(kUpdateRecentCounts));
55 	fMenuRecentFolders = new BCheckBox(B_TRANSLATE("Recent folders:"),
56 		new BMessage(kUpdateRecentCounts));
57 
58 	fMenuRecentDocumentCount = new BTextControl(NULL, NULL,
59 		new BMessage(kUpdateRecentCounts));
60 	fMenuRecentApplicationCount = new BTextControl(NULL, NULL,
61 		new BMessage(kUpdateRecentCounts));
62 	fMenuRecentFolderCount = new BTextControl(NULL, NULL,
63 		new BMessage(kUpdateRecentCounts));
64 
65 	// Applications controls
66 	fAppsSort = new BCheckBox(B_TRANSLATE("Sort running applications"),
67 		new BMessage(kSortRunningApps));
68 	fAppsSortTrackerFirst = new BCheckBox(B_TRANSLATE("Tracker always first"),
69 		new BMessage(kTrackerFirst));
70 	fAppsShowExpanders = new BCheckBox(B_TRANSLATE("Show application expander"),
71 		new BMessage(kSuperExpando));
72 	fAppsExpandNew = new BCheckBox(B_TRANSLATE("Expand new applications"),
73 		new BMessage(kExpandNewTeams));
74 	fAppsHideLabels = new BCheckBox(B_TRANSLATE("Hide application names"),
75 		new BMessage(kHideLabels));
76 	fAppsIconSizeSlider = new BSlider("icon_size", B_TRANSLATE("Icon size"),
77 		NULL, kMinimumIconSize / kIconSizeInterval,
78 		kMaximumIconSize / kIconSizeInterval, B_HORIZONTAL);
79 	fAppsIconSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
80 	fAppsIconSizeSlider->SetHashMarkCount((kMaximumIconSize - kMinimumIconSize)
81 		/ kIconSizeInterval + 1);
82 	fAppsIconSizeSlider->SetLimitLabels(B_TRANSLATE("Small"),
83 		B_TRANSLATE("Large"));
84 	fAppsIconSizeSlider->SetModificationMessage(new BMessage(kResizeTeamIcons));
85 
86 	// Window controls
87 	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
88 		new BMessage(kAlwaysTop));
89 	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
90 		new BMessage(kAutoRaise));
91 	fWindowAutoHide = new BCheckBox(B_TRANSLATE("Auto-hide"),
92 		new BMessage(kAutoHide));
93 
94 	// Clock controls
95 	fShowSeconds = new BCheckBox(B_TRANSLATE("Show seconds"),
96 		new BMessage(kShowSeconds));
97 	fShowDayOfWeek = new BCheckBox(B_TRANSLATE("Show day of week"),
98 		new BMessage(kShowDayOfWeek));
99 
100 	// Get settings from BarApp
101 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
102 	desk_settings* settings = barApp->Settings();
103 
104 	// Menu settings
105 	BTextView* docTextView = fMenuRecentDocumentCount->TextView();
106 	BTextView* appTextView = fMenuRecentApplicationCount->TextView();
107 	BTextView* folderTextView = fMenuRecentFolderCount->TextView();
108 
109 	for (int32 i = 0; i < 256; i++) {
110 		if (!isdigit(i)) {
111 			docTextView->DisallowChar(i);
112 			appTextView->DisallowChar(i);
113 			folderTextView->DisallowChar(i);
114 		}
115 	}
116 
117 	docTextView->SetMaxBytes(4);
118 	appTextView->SetMaxBytes(4);
119 	folderTextView->SetMaxBytes(4);
120 
121 	int32 docCount = settings->recentDocsCount;
122 	int32 appCount = settings->recentAppsCount;
123 	int32 folderCount = settings->recentFoldersCount;
124 
125 	fMenuRecentDocuments->SetValue(settings->recentDocsEnabled);
126 	fMenuRecentDocumentCount->SetEnabled(settings->recentDocsEnabled);
127 
128 	fMenuRecentApplications->SetValue(settings->recentAppsEnabled);
129 	fMenuRecentApplicationCount->SetEnabled(settings->recentAppsEnabled);
130 
131 	fMenuRecentFolders->SetValue(settings->recentFoldersEnabled);
132 	fMenuRecentFolderCount->SetEnabled(settings->recentFoldersEnabled);
133 
134 	BString docString;
135 	BString appString;
136 	BString folderString;
137 
138 	docString << docCount;
139 	appString << appCount;
140 	folderString << folderCount;
141 
142 	fMenuRecentDocumentCount->SetText(docString.String());
143 	fMenuRecentApplicationCount->SetText(appString.String());
144 	fMenuRecentFolderCount->SetText(folderString.String());
145 
146 	// Applications settings
147 	fAppsSort->SetValue(settings->sortRunningApps);
148 	fAppsSortTrackerFirst->SetValue(settings->trackerAlwaysFirst);
149 	fAppsShowExpanders->SetValue(settings->superExpando);
150 	fAppsExpandNew->SetValue(settings->expandNewTeams);
151 	fAppsHideLabels->SetValue(settings->hideLabels);
152 	fAppsIconSizeSlider->SetValue(settings->iconSize / kIconSizeInterval);
153 
154 	// Window settings
155 	fWindowAlwaysOnTop->SetValue(settings->alwaysOnTop);
156 	fWindowAutoRaise->SetValue(settings->autoRaise);
157 	fWindowAutoHide->SetValue(settings->autoHide);
158 
159 	// Clock settings
160 	TReplicantTray* replicantTray = barApp->BarView()->ReplicantTray();
161 	if (replicantTray->Time() != NULL) {
162 		fShowSeconds->SetValue(replicantTray->Time()->ShowSeconds());
163 		fShowDayOfWeek->SetValue(replicantTray->Time()->ShowDayOfWeek());
164 	} else {
165 		fShowSeconds->SetValue(settings->showSeconds);
166 		fShowDayOfWeek->SetValue(settings->showDayOfWeek);
167 	}
168 
169 	EnableDisableDependentItems();
170 
171 	// Targets
172 	fAppsSort->SetTarget(be_app);
173 	fAppsSortTrackerFirst->SetTarget(be_app);
174 	fAppsExpandNew->SetTarget(be_app);
175 	fAppsHideLabels->SetTarget(be_app);
176 	fAppsIconSizeSlider->SetTarget(be_app);
177 
178 	fWindowAlwaysOnTop->SetTarget(be_app);
179 	fWindowAutoRaise->SetTarget(be_app);
180 	fWindowAutoHide->SetTarget(be_app);
181 
182 	fShowSeconds->SetTarget(replicantTray);
183 	fShowDayOfWeek->SetTarget(replicantTray);
184 
185 	// Layout
186 	fMenuBox = new BBox("fMenuBox");
187 	fAppsBox = new BBox("fAppsBox");
188 	fWindowBox = new BBox("fWindowBox");
189 	fClockBox = new BBox("fClockBox");
190 
191 	fMenuBox->SetLabel(B_TRANSLATE("Menu"));
192 	fAppsBox->SetLabel(B_TRANSLATE("Applications"));
193 	fWindowBox->SetLabel(B_TRANSLATE("Window"));
194 	fClockBox->SetLabel(B_TRANSLATE("Clock"));
195 
196 	BView* view;
197 	view = BLayoutBuilder::Group<>()
198 		.AddGroup(B_VERTICAL, 0)
199 			.AddGroup(B_HORIZONTAL, 0)
200 				.AddGroup(B_VERTICAL, 0)
201 					.Add(fMenuRecentDocuments)
202 					.Add(fMenuRecentFolders)
203 					.Add(fMenuRecentApplications)
204 					.End()
205 				.AddGroup(B_VERTICAL, 0)
206 					.Add(fMenuRecentDocumentCount)
207 					.Add(fMenuRecentFolderCount)
208 					.Add(fMenuRecentApplicationCount)
209 					.End()
210 				.End()
211 			.AddGroup(B_VERTICAL, 0)
212 				.SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
213 				.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
214 					new BMessage(kEditMenuInTracker)))
215 				.End()
216 			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
217 				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
218 			.End()
219 		.View();
220 	fMenuBox->AddChild(view);
221 
222 	view = BLayoutBuilder::Group<>()
223 		.AddGroup(B_VERTICAL, 0)
224 			.Add(fAppsSort)
225 			.Add(fAppsSortTrackerFirst)
226 			.Add(fAppsShowExpanders)
227 			.AddGroup(B_HORIZONTAL, 0)
228 				.SetInsets(kIndentSpacing, 0, 0, 0)
229 				.Add(fAppsExpandNew)
230 				.End()
231 			.Add(fAppsHideLabels)
232 			.AddGroup(B_HORIZONTAL, 0)
233 				.SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
234 				.Add(fAppsIconSizeSlider)
235 				.End()
236 			.AddGlue()
237 			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
238 				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
239 			.End()
240 		.View();
241 	fAppsBox->AddChild(view);
242 
243 	view = BLayoutBuilder::Group<>()
244 		.AddGroup(B_VERTICAL, 0)
245 			.Add(fWindowAlwaysOnTop)
246 			.Add(fWindowAutoRaise)
247 			.Add(fWindowAutoHide)
248 			.AddGlue()
249 			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
250 				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
251 			.End()
252 		.View();
253 	fWindowBox->AddChild(view);
254 
255 	view = BLayoutBuilder::Group<>()
256 		.AddGroup(B_VERTICAL, 0)
257 			.Add(fShowSeconds)
258 			.Add(fShowDayOfWeek)
259 			.AddGlue()
260 			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
261 				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
262 			.End()
263 		.View();
264 	fClockBox->AddChild(view);
265 
266 	BLayoutBuilder::Group<>(this)
267 		.AddGrid(5, 5)
268 			.Add(fMenuBox, 0, 0)
269 			.Add(fWindowBox, 1, 0)
270 			.Add(fAppsBox, 0, 1)
271 			.Add(fClockBox, 1, 1)
272 			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
273 				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
274 			.End()
275 		.End();
276 
277 	CenterOnScreen();
278 }
279 
280 
281 PreferencesWindow::~PreferencesWindow()
282 {
283 	UpdateRecentCounts();
284 	be_app->PostMessage(kConfigClose);
285 }
286 
287 
288 void
289 PreferencesWindow::MessageReceived(BMessage* message)
290 {
291 	switch (message->what) {
292 		case kEditMenuInTracker:
293 			OpenWithTracker(B_USER_DESKBAR_DIRECTORY);
294 			break;
295 
296 		case kUpdateRecentCounts:
297 			UpdateRecentCounts();
298 			break;
299 
300 		case kSuperExpando:
301 			EnableDisableDependentItems();
302 			be_app->PostMessage(message);
303 			break;
304 
305 		case kStateChanged:
306 			EnableDisableDependentItems();
307 			break;
308 
309 		default:
310 			BWindow::MessageReceived(message);
311 			break;
312 	}
313 }
314 
315 
316 void
317 PreferencesWindow::WindowActivated(bool active)
318 {
319 	if (!active && IsMinimized())
320 		PostMessage(B_QUIT_REQUESTED);
321 }
322 
323 
324 void
325 PreferencesWindow::UpdateRecentCounts()
326 {
327 	BMessage message(kUpdateRecentCounts);
328 
329 	int32 docCount = atoi(fMenuRecentDocumentCount->Text());
330 	int32 appCount = atoi(fMenuRecentApplicationCount->Text());
331 	int32 folderCount = atoi(fMenuRecentFolderCount->Text());
332 
333 	message.AddInt32("documents", max_c(0, docCount));
334 	message.AddInt32("applications", max_c(0, appCount));
335 	message.AddInt32("folders", max_c(0, folderCount));
336 
337 	message.AddBool("documentsEnabled", fMenuRecentDocuments->Value());
338 	message.AddBool("applicationsEnabled", fMenuRecentApplications->Value());
339 	message.AddBool("foldersEnabled", fMenuRecentFolders->Value());
340 
341 	be_app->PostMessage(&message);
342 
343 	EnableDisableDependentItems();
344 }
345 
346 
347 void
348 PreferencesWindow::EnableDisableDependentItems()
349 {
350 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
351 	if (barApp->BarView()->Vertical()
352 		&& barApp->BarView()->ExpandoState()) {
353 		fAppsShowExpanders->SetEnabled(true);
354 		fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value());
355 	} else {
356 		fAppsShowExpanders->SetEnabled(false);
357 		fAppsExpandNew->SetEnabled(false);
358 	}
359 
360 	fMenuRecentDocumentCount->SetEnabled(
361 		fMenuRecentDocuments->Value() != B_CONTROL_OFF);
362 	fMenuRecentApplicationCount->SetEnabled(
363 		fMenuRecentApplications->Value() != B_CONTROL_OFF);
364 	fMenuRecentFolderCount->SetEnabled(
365 		fMenuRecentFolders->Value() != B_CONTROL_OFF);
366 
367 	fWindowAutoRaise->SetEnabled(
368 		fWindowAlwaysOnTop->Value() == B_CONTROL_OFF);
369 }
370