xref: /haiku/src/apps/deskbar/PreferencesWindow.cpp (revision cbed190f71b8aff814bf95539c39a1bcfb953ed8)
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 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(
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 	fClockSeconds = new BCheckBox(B_TRANSLATE("Show seconds"),
62 		new BMessage(kShowSeconds));
63 	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
64 		new BMessage(kAlwaysTop));
65 	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
66 		new BMessage(kAutoRaise));
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 
128 	_EnableDisableDependentItems();
129 
130 	// Targets
131 	fAppsSort->SetTarget(be_app);
132 	fAppsSortTrackerFirst->SetTarget(be_app);
133 	fAppsExpandNew->SetTarget(be_app);
134 
135 	fClockSeconds->SetTarget(replicantTray);
136 
137 	fWindowAlwaysOnTop->SetTarget(be_app);
138 	fWindowAutoRaise->SetTarget(be_app);
139 
140 	// Layout
141 	fMenuBox = new BBox("fMenuBox");
142 	fAppsBox = new BBox("fAppsBox");
143 	fClockBox = new BBox("fClockBox");
144 	fWindowBox = new BBox("fWindowBox");
145 
146 	fMenuBox->SetLabel(B_TRANSLATE("Menu"));
147 	fAppsBox->SetLabel(B_TRANSLATE("Applications"));
148 	fClockBox->SetLabel(B_TRANSLATE("Clock"));
149 	fWindowBox->SetLabel(B_TRANSLATE("Window"));
150 
151 	BView* view;
152 	view = BLayoutBuilder::Group<>()
153 		.AddGroup(B_VERTICAL, 10)
154 			.AddGroup(B_HORIZONTAL, 0)
155 				.AddGroup(B_VERTICAL, 0)
156 					.Add(fMenuRecentDocuments)
157 					.Add(fMenuRecentFolders)
158 					.Add(fMenuRecentApplications)
159 					.End()
160 				.AddGroup(B_VERTICAL, 0)
161 					.Add(fMenuRecentDocumentCount)
162 					.Add(fMenuRecentFolderCount)
163 					.Add(fMenuRecentApplicationCount)
164 					.End()
165 				.End()
166 			.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
167 				new BMessage(kEditMenuInTracker)))
168 			.SetInsets(10, 10, 10, 10)
169 			.End()
170 		.View();
171 	fMenuBox->AddChild(view);
172 
173 	view = BLayoutBuilder::Group<>()
174 		.AddGroup(B_VERTICAL, 1)
175 			.Add(fAppsSort)
176 			.Add(fAppsSortTrackerFirst)
177 			.Add(fAppsShowExpanders)
178 			.AddGroup(B_HORIZONTAL, 0)
179 				.SetInsets(20, 0, 0, 0)
180 				.Add(fAppsExpandNew)
181 				.End()
182 			.AddGlue()
183 			.SetInsets(10, 10, 10, 10)
184 			.End()
185 		.View();
186 	fAppsBox->AddChild(view);
187 
188 	view = BLayoutBuilder::Group<>()
189 		.AddGroup(B_VERTICAL, 1)
190 			.Add(fClockSeconds)
191 			.AddGlue()
192 			.SetInsets(10, 10, 10, 10)
193 			.End()
194 		.View();
195 	fClockBox->AddChild(view);
196 
197 	view = BLayoutBuilder::Group<>()
198 		.AddGroup(B_VERTICAL, 1)
199 			.Add(fWindowAlwaysOnTop)
200 			.Add(fWindowAutoRaise)
201 			.AddGlue()
202 			.SetInsets(10, 10, 10, 10)
203 			.End()
204 		.View();
205 	fWindowBox->AddChild(view);
206 
207 	BLayoutBuilder::Group<>(this)
208 		.AddGrid(5, 5)
209 			.Add(fMenuBox, 0, 0)
210 			.Add(fWindowBox, 1, 0)
211 			.Add(fAppsBox, 0, 1)
212 			.Add(fClockBox, 1, 1)
213 			.SetInsets(10, 10, 10, 10)
214 			.End()
215 		.End();
216 
217 	CenterOnScreen();
218 }
219 
220 
221 PreferencesWindow::~PreferencesWindow()
222 {
223 	_UpdateRecentCounts();
224 	be_app->PostMessage(kConfigClose);
225 }
226 
227 
228 void
229 PreferencesWindow::MessageReceived(BMessage* message)
230 {
231 	switch (message->what) {
232 		case kEditMenuInTracker:
233 			OpenWithTracker(B_USER_DESKBAR_DIRECTORY);
234 			break;
235 
236 		case kUpdateRecentCounts:
237 			_UpdateRecentCounts();
238 			break;
239 
240 		case kSuperExpando:
241 			_EnableDisableDependentItems();
242 			be_app->PostMessage(message);
243 			break;
244 
245 		case kStateChanged:
246 			_EnableDisableDependentItems();
247 			break;
248 
249 		default:
250 			BWindow::MessageReceived(message);
251 			break;
252 	}
253 }
254 
255 
256 void
257 PreferencesWindow::_UpdateRecentCounts()
258 {
259 	BMessage message(kUpdateRecentCounts);
260 
261 	int32 docCount = atoi(fMenuRecentDocumentCount->Text());
262 	int32 appCount = atoi(fMenuRecentApplicationCount->Text());
263 	int32 folderCount = atoi(fMenuRecentFolderCount->Text());
264 
265 	message.AddInt32("documents", max_c(0, docCount));
266 	message.AddInt32("applications", max_c(0, appCount));
267 	message.AddInt32("folders", max_c(0, folderCount));
268 
269 	message.AddBool("documentsEnabled", fMenuRecentDocuments->Value());
270 	message.AddBool("applicationsEnabled", fMenuRecentApplications->Value());
271 	message.AddBool("foldersEnabled", fMenuRecentFolders->Value());
272 
273 	be_app->PostMessage(&message);
274 
275 	_EnableDisableDependentItems();
276 }
277 
278 
279 void
280 PreferencesWindow::_EnableDisableDependentItems()
281 {
282 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
283 	if (barApp->BarView()->Vertical()
284 		&& barApp->BarView()->Expando()) {
285 		fAppsShowExpanders->SetEnabled(true);
286 		fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value());
287 	} else {
288 		fAppsShowExpanders->SetEnabled(false);
289 		fAppsExpandNew->SetEnabled(false);
290 	}
291 
292 	if (fMenuRecentDocuments->Value())
293 		fMenuRecentDocumentCount->SetEnabled(true);
294 	else
295 		fMenuRecentDocumentCount->SetEnabled(false);
296 
297 	if (fMenuRecentApplications->Value())
298 		fMenuRecentApplicationCount->SetEnabled(true);
299 	else
300 		fMenuRecentApplicationCount->SetEnabled(false);
301 
302 	if (fMenuRecentFolders->Value())
303 		fMenuRecentFolderCount->SetEnabled(true);
304 	else
305 		fMenuRecentFolderCount->SetEnabled(false);
306 }
307 
308 
309 void
310 PreferencesWindow::WindowActivated(bool active)
311 {
312 	if (!active && IsMinimized())
313 		PostMessage(B_QUIT_REQUESTED);
314 }
315 
316