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