xref: /haiku/src/apps/deskbar/PreferencesWindow.cpp (revision 2f0eb7b11e9cdb428b2da5bfaac25c9998b02e03)
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 	fClockFullDate = new BCheckBox(B_TRANSLATE("Full date"),
64 		new BMessage(kFullDate));
65 
66 	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
67 		new BMessage(kAlwaysTop));
68 	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
69 		new BMessage(kAutoRaise));
70 
71 	BTextView* docTextView = fMenuRecentDocumentCount->TextView();
72 	BTextView* appTextView = fMenuRecentApplicationCount->TextView();
73 	BTextView* folderTextView = fMenuRecentFolderCount->TextView();
74 
75 	for (int32 i = 0; i < 256; i++) {
76 		if (!isdigit(i)) {
77 			docTextView->DisallowChar(i);
78 			appTextView->DisallowChar(i);
79 			folderTextView->DisallowChar(i);
80 		}
81 	}
82 
83 	docTextView->SetMaxBytes(4);
84 	appTextView->SetMaxBytes(4);
85 	folderTextView->SetMaxBytes(4);
86 
87 	// Values
88 	TBarApp* barApp = static_cast<TBarApp*>(be_app);
89 	desk_settings* appSettings = barApp->Settings();;
90 
91 	fAppsSort->SetValue(appSettings->sortRunningApps);
92 	fAppsSortTrackerFirst->SetValue(appSettings->trackerAlwaysFirst);
93 	fAppsShowExpanders->SetValue(appSettings->superExpando);
94 	fAppsExpandNew->SetValue(appSettings->expandNewTeams);
95 
96 	int32 docCount = appSettings->recentDocsCount;
97 	int32 appCount = appSettings->recentAppsCount;
98 	int32 folderCount = appSettings->recentFoldersCount;
99 
100 	fMenuRecentDocuments->SetValue(appSettings->recentDocsEnabled);
101 	fMenuRecentDocumentCount->SetEnabled(appSettings->recentDocsEnabled);
102 
103 	fMenuRecentApplications->SetValue(appSettings->recentAppsEnabled);
104 	fMenuRecentApplicationCount->SetEnabled(appSettings->recentAppsEnabled);
105 
106 	fMenuRecentFolders->SetValue(appSettings->recentFoldersEnabled);
107 	fMenuRecentFolderCount->SetEnabled(appSettings->recentFoldersEnabled);
108 
109 	BString docString;
110 	BString appString;
111 	BString folderString;
112 
113 	docString << docCount;
114 	appString << appCount;
115 	folderString << folderCount;
116 
117 	fMenuRecentDocumentCount->SetText(docString.String());
118 	fMenuRecentApplicationCount->SetText(appString.String());
119 	fMenuRecentFolderCount->SetText(folderString.String());
120 
121 	TReplicantTray* replicantTray = barApp->BarView()->fReplicantTray;
122 
123 	fClockSeconds->SetValue(replicantTray->ShowingSeconds());
124 	fClockFullDate->SetValue(replicantTray->ShowingFullDate());
125 
126 	bool showingClock = barApp->BarView()->ShowingClock();
127 	fClockSeconds->SetEnabled(showingClock);
128 	fClockFullDate->SetEnabled(replicantTray->CanShowFullDate());
129 
130 	fWindowAlwaysOnTop->SetValue(appSettings->alwaysOnTop);
131 	fWindowAutoRaise->SetValue(appSettings->autoRaise);
132 
133 	_EnableDisableDependentItems();
134 
135 	// Targets
136 	fAppsSort->SetTarget(be_app);
137 	fAppsSortTrackerFirst->SetTarget(be_app);
138 	fAppsExpandNew->SetTarget(be_app);
139 
140 	fClockSeconds->SetTarget(replicantTray);
141 	fClockFullDate->SetTarget(replicantTray);
142 
143 	fWindowAlwaysOnTop->SetTarget(be_app);
144 	fWindowAutoRaise->SetTarget(be_app);
145 
146 	// Layout
147 	fMenuBox = new BBox("fMenuBox");
148 	fAppsBox = new BBox("fAppsBox");
149 	fClockBox = new BBox("fClockBox");
150 	fWindowBox = new BBox("fWindowBox");
151 
152 	fMenuBox->SetLabel(B_TRANSLATE("Menu"));
153 	fAppsBox->SetLabel(B_TRANSLATE("Applications"));
154 	fClockBox->SetLabel(B_TRANSLATE("Clock"));
155 	fWindowBox->SetLabel(B_TRANSLATE("Window"));
156 
157 	BView* view;
158 	view = BLayoutBuilder::Group<>()
159 		.AddGroup(B_VERTICAL, 10)
160 			.AddGroup(B_HORIZONTAL, 0)
161 				.AddGroup(B_VERTICAL, 0)
162 					.Add(fMenuRecentDocuments)
163 					.Add(fMenuRecentFolders)
164 					.Add(fMenuRecentApplications)
165 					.End()
166 				.AddGroup(B_VERTICAL, 0)
167 					.Add(fMenuRecentDocumentCount)
168 					.Add(fMenuRecentFolderCount)
169 					.Add(fMenuRecentApplicationCount)
170 					.End()
171 				.End()
172 			.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
173 				new BMessage(kEditMenuInTracker)))
174 			.SetInsets(10, 10, 10, 10)
175 			.End()
176 		.View();
177 	fMenuBox->AddChild(view);
178 
179 	view = BLayoutBuilder::Group<>()
180 		.AddGroup(B_VERTICAL, 1)
181 			.Add(fAppsSort)
182 			.Add(fAppsSortTrackerFirst)
183 			.Add(fAppsShowExpanders)
184 			.AddGroup(B_HORIZONTAL, 0)
185 				.SetInsets(20, 0, 0, 0)
186 				.Add(fAppsExpandNew)
187 				.End()
188 			.AddGlue()
189 			.SetInsets(10, 10, 10, 10)
190 			.End()
191 		.View();
192 	fAppsBox->AddChild(view);
193 
194 	view = BLayoutBuilder::Group<>()
195 		.AddGroup(B_VERTICAL, 1)
196 			.Add(fClockSeconds)
197 			.Add(fClockFullDate)
198 			.AddGlue()
199 			.SetInsets(10, 10, 10, 10)
200 			.End()
201 		.View();
202 	fClockBox->AddChild(view);
203 
204 	view = BLayoutBuilder::Group<>()
205 		.AddGroup(B_VERTICAL, 1)
206 			.Add(fWindowAlwaysOnTop)
207 			.Add(fWindowAutoRaise)
208 			.AddGlue()
209 			.SetInsets(10, 10, 10, 10)
210 			.End()
211 		.View();
212 	fWindowBox->AddChild(view);
213 
214 	BLayoutBuilder::Group<>(this)
215 		.AddGrid(5, 5)
216 			.Add(fMenuBox, 0, 0)
217 			.Add(fWindowBox, 1, 0)
218 			.Add(fAppsBox, 0, 1)
219 			.Add(fClockBox, 1, 1)
220 			.SetInsets(10, 10, 10, 10)
221 			.End()
222 		.End();
223 
224 	CenterOnScreen();
225 }
226 
227 
228 PreferencesWindow::~PreferencesWindow()
229 {
230 	_UpdateRecentCounts();
231 	be_app->PostMessage(kConfigClose);
232 }
233 
234 
235 void
236 PreferencesWindow::MessageReceived(BMessage* message)
237 {
238 	switch (message->what) {
239 		case kEditMenuInTracker:
240 			OpenWithTracker(B_USER_DESKBAR_DIRECTORY);
241 			break;
242 
243 		case kUpdateRecentCounts:
244 			_UpdateRecentCounts();
245 			break;
246 
247 		case kSuperExpando:
248 			_EnableDisableDependentItems();
249 			be_app->PostMessage(message);
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 	if (fAppsShowExpanders->Value())
286 		fAppsExpandNew->SetEnabled(true);
287 	else
288 		fAppsExpandNew->SetEnabled(false);
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