xref: /haiku/src/apps/deskbar/PreferencesWindow.cpp (revision 837b16251d4b2b6249ebcaa19bb319cbe82c6126)
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 <Catalog.h>
13 #include <CheckBox.h>
14 #include <GroupLayout.h>
15 #include <Locale.h>
16 #include <LayoutBuilder.h>
17 #include <OpenWithTracker.h>
18 #include <RadioButton.h>
19 #include <SeparatorView.h>
20 
21 #include <ctype.h>
22 
23 #include "BarApp.h"
24 #include "StatusView.h"
25 
26 
27 #undef B_TRANSLATE_CONTEXT
28 #define B_TRANSLATE_CONTEXT "PreferencesWindow"
29 
30 PreferencesWindow::PreferencesWindow(BRect frame)
31 	:
32 	BWindow(frame, B_TRANSLATE("Deskbar preferences"), B_TITLED_WINDOW,
33 		B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE)
34 {
35 	// Controls
36 	fMenuRecentDocuments = new BCheckBox(B_TRANSLATE("Recent documents:"),
37 		new BMessage(kUpdateRecentCounts));
38 	fMenuRecentApplications = new BCheckBox(B_TRANSLATE("Recent applications:"),
39 		new BMessage(kUpdateRecentCounts));
40 	fMenuRecentFolders = new BCheckBox(B_TRANSLATE("Recent folders:"),
41 		new BMessage(kUpdateRecentCounts));
42 
43 	fMenuRecentDocumentCount = new BTextControl(NULL, NULL,
44 		new BMessage(kUpdateRecentCounts));
45 	fMenuRecentApplicationCount = new BTextControl(NULL, NULL,
46 		new BMessage(kUpdateRecentCounts));
47 	fMenuRecentFolderCount = new BTextControl(NULL, NULL,
48 		new BMessage(kUpdateRecentCounts));
49 
50 	fAppsSort = new BCheckBox(B_TRANSLATE("Sort running applications"),
51 		new BMessage(kSortRunningApps));
52 	fAppsSortTrackerFirst = new BCheckBox(B_TRANSLATE("Tracker always first"),
53 		new BMessage(kTrackerFirst));
54 	fAppsShowExpanders = new BCheckBox(B_TRANSLATE("Show application expander"),
55 		new BMessage(kSuperExpando));
56 	fAppsExpandNew = new BCheckBox(B_TRANSLATE("Expand new applications"),
57 		new BMessage(kExpandNewTeams));
58 
59 	fClockSeconds = new BCheckBox(B_TRANSLATE("Show seconds"),
60 		new BMessage(kShowSeconds));
61 	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
62 		new BMessage(kAlwaysTop));
63 	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
64 		new BMessage(kAutoRaise));
65 	fWindowAutoHide = new BCheckBox(B_TRANSLATE("Auto-hide"),
66 		new BMessage(kAutoHide));
67 
68 	BTextView* docTextView = fMenuRecentDocumentCount->TextView();
69 	BTextView* appTextView = fMenuRecentApplicationCount->TextView();
70 	BTextView* folderTextView = fMenuRecentFolderCount->TextView();
71 
72 	for (int32 i = 0; i < 256; i++) {
73 		if (!isdigit(i)) {
74 			docTextView->DisallowChar(i);
75 			appTextView->DisallowChar(i);
76 			folderTextView->DisallowChar(i);
77 		}
78 	}
79 
80 	docTextView->SetMaxBytes(4);
81 	appTextView->SetMaxBytes(4);
82 	folderTextView->SetMaxBytes(4);
83 
84 	// Values
85 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
86 	desk_settings* appSettings = barApp->Settings();
87 
88 	fAppsSort->SetValue(appSettings->sortRunningApps);
89 	fAppsSortTrackerFirst->SetValue(appSettings->trackerAlwaysFirst);
90 	fAppsShowExpanders->SetValue(appSettings->superExpando);
91 	fAppsExpandNew->SetValue(appSettings->expandNewTeams);
92 
93 	int32 docCount = appSettings->recentDocsCount;
94 	int32 appCount = appSettings->recentAppsCount;
95 	int32 folderCount = appSettings->recentFoldersCount;
96 
97 	fMenuRecentDocuments->SetValue(appSettings->recentDocsEnabled);
98 	fMenuRecentDocumentCount->SetEnabled(appSettings->recentDocsEnabled);
99 
100 	fMenuRecentApplications->SetValue(appSettings->recentAppsEnabled);
101 	fMenuRecentApplicationCount->SetEnabled(appSettings->recentAppsEnabled);
102 
103 	fMenuRecentFolders->SetValue(appSettings->recentFoldersEnabled);
104 	fMenuRecentFolderCount->SetEnabled(appSettings->recentFoldersEnabled);
105 
106 	BString docString;
107 	BString appString;
108 	BString folderString;
109 
110 	docString << docCount;
111 	appString << appCount;
112 	folderString << folderCount;
113 
114 	fMenuRecentDocumentCount->SetText(docString.String());
115 	fMenuRecentApplicationCount->SetText(appString.String());
116 	fMenuRecentFolderCount->SetText(folderString.String());
117 
118 	TReplicantTray* replicantTray = barApp->BarView()->fReplicantTray;
119 
120 	fClockSeconds->SetValue(replicantTray->ShowingSeconds());
121 
122 	bool showingClock = barApp->BarView()->ShowingClock();
123 	fClockSeconds->SetEnabled(showingClock);
124 
125 	fWindowAlwaysOnTop->SetValue(appSettings->alwaysOnTop);
126 	fWindowAutoRaise->SetValue(appSettings->autoRaise);
127 	fWindowAutoHide->SetValue(appSettings->autoHide);
128 
129 	_EnableDisableDependentItems();
130 
131 	// Targets
132 	fAppsSort->SetTarget(be_app);
133 	fAppsSortTrackerFirst->SetTarget(be_app);
134 	fAppsExpandNew->SetTarget(be_app);
135 
136 	fClockSeconds->SetTarget(replicantTray);
137 
138 	fWindowAlwaysOnTop->SetTarget(be_app);
139 	fWindowAutoRaise->SetTarget(be_app);
140 	fWindowAutoHide->SetTarget(be_app);
141 
142 	// Layout
143 	fMenuBox = new BBox("fMenuBox");
144 	fAppsBox = new BBox("fAppsBox");
145 	fClockBox = new BBox("fClockBox");
146 	fWindowBox = new BBox("fWindowBox");
147 
148 	fMenuBox->SetLabel(B_TRANSLATE("Menu"));
149 	fAppsBox->SetLabel(B_TRANSLATE("Applications"));
150 	fClockBox->SetLabel(B_TRANSLATE("Clock"));
151 	fWindowBox->SetLabel(B_TRANSLATE("Window"));
152 
153 	BView* view;
154 	view = BLayoutBuilder::Group<>()
155 		.AddGroup(B_VERTICAL, 10)
156 			.AddGroup(B_HORIZONTAL, 0)
157 				.AddGroup(B_VERTICAL, 0)
158 					.Add(fMenuRecentDocuments)
159 					.Add(fMenuRecentFolders)
160 					.Add(fMenuRecentApplications)
161 					.End()
162 				.AddGroup(B_VERTICAL, 0)
163 					.Add(fMenuRecentDocumentCount)
164 					.Add(fMenuRecentFolderCount)
165 					.Add(fMenuRecentApplicationCount)
166 					.End()
167 				.End()
168 			.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
169 				new BMessage(kEditMenuInTracker)))
170 			.SetInsets(10, 10, 10, 10)
171 			.End()
172 		.View();
173 	fMenuBox->AddChild(view);
174 
175 	view = BLayoutBuilder::Group<>()
176 		.AddGroup(B_VERTICAL, 1)
177 			.Add(fAppsSort)
178 			.Add(fAppsSortTrackerFirst)
179 			.Add(fAppsShowExpanders)
180 			.AddGroup(B_HORIZONTAL, 0)
181 				.SetInsets(20, 0, 0, 0)
182 				.Add(fAppsExpandNew)
183 				.End()
184 			.AddGlue()
185 			.SetInsets(10, 10, 10, 10)
186 			.End()
187 		.View();
188 	fAppsBox->AddChild(view);
189 
190 	view = BLayoutBuilder::Group<>()
191 		.AddGroup(B_VERTICAL, 1)
192 			.Add(fClockSeconds)
193 			.AddGlue()
194 			.SetInsets(10, 10, 10, 10)
195 			.End()
196 		.View();
197 	fClockBox->AddChild(view);
198 
199 	view = BLayoutBuilder::Group<>()
200 		.AddGroup(B_VERTICAL, 1)
201 			.Add(fWindowAlwaysOnTop)
202 			.Add(fWindowAutoRaise)
203 			.Add(fWindowAutoHide)
204 			.AddGlue()
205 			.SetInsets(10, 10, 10, 10)
206 			.End()
207 		.View();
208 	fWindowBox->AddChild(view);
209 
210 	BLayoutBuilder::Group<>(this)
211 		.AddGrid(5, 5)
212 			.Add(fMenuBox, 0, 0)
213 			.Add(fWindowBox, 1, 0)
214 			.Add(fAppsBox, 0, 1)
215 			.Add(fClockBox, 1, 1)
216 			.SetInsets(10, 10, 10, 10)
217 			.End()
218 		.End();
219 
220 	CenterOnScreen();
221 }
222 
223 
224 PreferencesWindow::~PreferencesWindow()
225 {
226 	_UpdateRecentCounts();
227 	be_app->PostMessage(kConfigClose);
228 }
229 
230 
231 void
232 PreferencesWindow::MessageReceived(BMessage* message)
233 {
234 	switch (message->what) {
235 		case kEditMenuInTracker:
236 			OpenWithTracker(B_USER_DESKBAR_DIRECTORY);
237 			break;
238 
239 		case kUpdateRecentCounts:
240 			_UpdateRecentCounts();
241 			break;
242 
243 		case kSuperExpando:
244 			_EnableDisableDependentItems();
245 			be_app->PostMessage(message);
246 			break;
247 
248 		case kStateChanged:
249 			_EnableDisableDependentItems();
250 			break;
251 
252 		default:
253 			BWindow::MessageReceived(message);
254 			break;
255 	}
256 }
257 
258 
259 void
260 PreferencesWindow::_UpdateRecentCounts()
261 {
262 	BMessage message(kUpdateRecentCounts);
263 
264 	int32 docCount = atoi(fMenuRecentDocumentCount->Text());
265 	int32 appCount = atoi(fMenuRecentApplicationCount->Text());
266 	int32 folderCount = atoi(fMenuRecentFolderCount->Text());
267 
268 	message.AddInt32("documents", max_c(0, docCount));
269 	message.AddInt32("applications", max_c(0, appCount));
270 	message.AddInt32("folders", max_c(0, folderCount));
271 
272 	message.AddBool("documentsEnabled", fMenuRecentDocuments->Value());
273 	message.AddBool("applicationsEnabled", fMenuRecentApplications->Value());
274 	message.AddBool("foldersEnabled", fMenuRecentFolders->Value());
275 
276 	be_app->PostMessage(&message);
277 
278 	_EnableDisableDependentItems();
279 }
280 
281 
282 void
283 PreferencesWindow::_EnableDisableDependentItems()
284 {
285 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
286 	if (barApp->BarView()->Vertical()
287 		&& barApp->BarView()->Expando()) {
288 		fAppsShowExpanders->SetEnabled(true);
289 		fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value());
290 	} else {
291 		fAppsShowExpanders->SetEnabled(false);
292 		fAppsExpandNew->SetEnabled(false);
293 	}
294 
295 	fMenuRecentDocumentCount->SetEnabled(
296 		fMenuRecentDocuments->Value() != B_CONTROL_OFF);
297 	fMenuRecentApplicationCount->SetEnabled(
298 		fMenuRecentApplications->Value() != B_CONTROL_OFF);
299 	fMenuRecentFolderCount->SetEnabled(
300 		fMenuRecentFolders->Value() != B_CONTROL_OFF);
301 
302 	fWindowAutoRaise->SetEnabled(
303 		fWindowAlwaysOnTop->Value() == B_CONTROL_OFF);
304 }
305 
306 
307 void
308 PreferencesWindow::WindowActivated(bool active)
309 {
310 	if (!active && IsMinimized())
311 		PostMessage(B_QUIT_REQUESTED);
312 }
313