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