1/*
2 * Copyright 2009-2011 Haiku, Inc.
3 * All Rights Reserved. Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		John Scipione, jscipione@gmail.com
7 *		Jonas Sundström, jonas@kirilla.com
8 */
9
10
11#include "PreferencesWindow.h"
12
13#include <ctype.h>
14
15#include <Button.h>
16#include <Catalog.h>
17#include <CheckBox.h>
18#include <ControlLook.h>
19#include <FormattingConventions.h>
20#include <GroupLayout.h>
21#include <ListView.h>
22#include <Locale.h>
23#include <LayoutBuilder.h>
24#include <OpenWithTracker.h>
25#include <RadioButton.h>
26#include <Roster.h>
27#include <SeparatorView.h>
28#include <Slider.h>
29#include <TabView.h>
30#include <TextControl.h>
31#include <View.h>
32
33#include "BarApp.h"
34#include "StatusView.h"
35
36
37static const float kIndentSpacing
38	= be_control_look->DefaultItemSpacing() * 2.3;
39static const uint32 kSettingsViewChanged = 'Svch';
40
41
42#undef B_TRANSLATION_CONTEXT
43#define B_TRANSLATION_CONTEXT "PreferencesWindow"
44
45
46PreferencesWindow::PreferencesWindow(BRect frame)
47	:
48	BWindow(frame, B_TRANSLATE("Deskbar preferences"), B_TITLED_WINDOW,
49		B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE)
50{
51	// Menu controls
52	fMenuRecentDocuments = new BCheckBox(B_TRANSLATE("Recent documents:"),
53		new BMessage(kUpdateRecentCounts));
54	fMenuRecentApplications = new BCheckBox(B_TRANSLATE("Recent applications:"),
55		new BMessage(kUpdateRecentCounts));
56	fMenuRecentFolders = new BCheckBox(B_TRANSLATE("Recent folders:"),
57		new BMessage(kUpdateRecentCounts));
58
59	fMenuRecentDocumentCount = new BTextControl(NULL, NULL,
60		new BMessage(kUpdateRecentCounts));
61	fMenuRecentApplicationCount = new BTextControl(NULL, NULL,
62		new BMessage(kUpdateRecentCounts));
63	fMenuRecentFolderCount = new BTextControl(NULL, NULL,
64		new BMessage(kUpdateRecentCounts));
65
66	// Applications controls
67	fAppsSort = new BCheckBox(B_TRANSLATE("Sort running applications"),
68		new BMessage(kSortRunningApps));
69	fAppsSortTrackerFirst = new BCheckBox(B_TRANSLATE("Tracker always first"),
70		new BMessage(kTrackerFirst));
71	fAppsShowExpanders = new BCheckBox(B_TRANSLATE("Show application expander"),
72		new BMessage(kSuperExpando));
73	fAppsExpandNew = new BCheckBox(B_TRANSLATE("Expand new applications"),
74		new BMessage(kExpandNewTeams));
75	fAppsHideLabels = new BCheckBox(B_TRANSLATE("Hide application names"),
76		new BMessage(kHideLabels));
77	fAppsIconSizeSlider = new BSlider("icon_size", B_TRANSLATE("Icon size"),
78		NULL, kMinimumIconSize / kIconSizeInterval,
79		kMaximumIconSize / kIconSizeInterval, B_HORIZONTAL);
80	fAppsIconSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
81	fAppsIconSizeSlider->SetHashMarkCount((kMaximumIconSize - kMinimumIconSize)
82		/ kIconSizeInterval + 1);
83	fAppsIconSizeSlider->SetLimitLabels(B_TRANSLATE("Small"),
84		B_TRANSLATE("Large"));
85	fAppsIconSizeSlider->SetModificationMessage(new BMessage(kResizeTeamIcons));
86
87	// Window controls
88	fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"),
89		new BMessage(kAlwaysTop));
90	fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"),
91		new BMessage(kAutoRaise));
92	fWindowAutoHide = new BCheckBox(B_TRANSLATE("Auto-hide"),
93		new BMessage(kAutoHide));
94
95	// Get settings from BarApp
96	TBarApp* barApp = static_cast<TBarApp*>(be_app);
97	desk_settings* settings = barApp->Settings();
98
99	// Menu settings
100	BTextView* docTextView = fMenuRecentDocumentCount->TextView();
101	BTextView* appTextView = fMenuRecentApplicationCount->TextView();
102	BTextView* folderTextView = fMenuRecentFolderCount->TextView();
103
104	for (int32 i = 0; i < 256; i++) {
105		if (!isdigit(i)) {
106			docTextView->DisallowChar(i);
107			appTextView->DisallowChar(i);
108			folderTextView->DisallowChar(i);
109		}
110	}
111
112	docTextView->SetMaxBytes(4);
113	appTextView->SetMaxBytes(4);
114	folderTextView->SetMaxBytes(4);
115
116	int32 docCount = settings->recentDocsCount;
117	int32 appCount = settings->recentAppsCount;
118	int32 folderCount = settings->recentFoldersCount;
119
120	fMenuRecentDocuments->SetValue(settings->recentDocsEnabled);
121	fMenuRecentDocumentCount->SetEnabled(settings->recentDocsEnabled);
122
123	fMenuRecentApplications->SetValue(settings->recentAppsEnabled);
124	fMenuRecentApplicationCount->SetEnabled(settings->recentAppsEnabled);
125
126	fMenuRecentFolders->SetValue(settings->recentFoldersEnabled);
127	fMenuRecentFolderCount->SetEnabled(settings->recentFoldersEnabled);
128
129	BString docString;
130	BString appString;
131	BString folderString;
132
133	docString << docCount;
134	appString << appCount;
135	folderString << folderCount;
136
137	fMenuRecentDocumentCount->SetText(docString.String());
138	fMenuRecentApplicationCount->SetText(appString.String());
139	fMenuRecentFolderCount->SetText(folderString.String());
140
141	// Applications settings
142	fAppsSort->SetValue(settings->sortRunningApps);
143	fAppsSortTrackerFirst->SetValue(settings->trackerAlwaysFirst);
144	fAppsShowExpanders->SetValue(settings->superExpando);
145	fAppsExpandNew->SetValue(settings->expandNewTeams);
146	fAppsHideLabels->SetValue(settings->hideLabels);
147	fAppsIconSizeSlider->SetValue(settings->iconSize / kIconSizeInterval);
148
149	// Window settings
150	fWindowAlwaysOnTop->SetValue(settings->alwaysOnTop);
151	fWindowAutoRaise->SetValue(settings->autoRaise);
152	fWindowAutoHide->SetValue(settings->autoHide);
153
154	EnableDisableDependentItems();
155
156	// Targets
157	fAppsSort->SetTarget(be_app);
158	fAppsSortTrackerFirst->SetTarget(be_app);
159	fAppsExpandNew->SetTarget(be_app);
160	fAppsHideLabels->SetTarget(be_app);
161	fAppsIconSizeSlider->SetTarget(be_app);
162
163	fWindowAlwaysOnTop->SetTarget(be_app);
164	fWindowAutoRaise->SetTarget(be_app);
165	fWindowAutoHide->SetTarget(be_app);
166
167	// Layout
168	BView* menuSettingsView = BLayoutBuilder::Group<>()
169		.AddGroup(B_VERTICAL, 0)
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			.AddGroup(B_VERTICAL, 0)
183				.SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
184				.Add(new BButton(B_TRANSLATE("Edit menu" B_UTF8_ELLIPSIS),
185					new BMessage(kEditMenuInTracker)))
186				.End()
187			.AddGlue()
188			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
189				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
190			.End()
191		.View();
192	menuSettingsView->SetName(B_TRANSLATE("Menu"));
193
194	BView* applicationsSettingsView = BLayoutBuilder::Group<>()
195		.AddGroup(B_VERTICAL, 0)
196			.Add(fAppsSort)
197			.Add(fAppsSortTrackerFirst)
198			.Add(fAppsShowExpanders)
199			.AddGroup(B_HORIZONTAL, 0)
200				.SetInsets(kIndentSpacing, 0, 0, 0)
201				.Add(fAppsExpandNew)
202				.End()
203			.Add(fAppsHideLabels)
204			.AddGroup(B_HORIZONTAL, 0)
205				.SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
206				.Add(fAppsIconSizeSlider)
207				.End()
208			.AddGlue()
209			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
210				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
211			.End()
212		.View();
213	applicationsSettingsView->SetName(B_TRANSLATE("Applications"));
214
215	BView* windowSettingsView = BLayoutBuilder::Group<>()
216		.AddGroup(B_VERTICAL, 0)
217			.Add(fWindowAlwaysOnTop)
218			.Add(fWindowAutoRaise)
219			.Add(fWindowAutoHide)
220			.AddGlue()
221			.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
222				B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
223			.End()
224		.View();
225	windowSettingsView->SetName(B_TRANSLATE("Window"));
226
227	BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
228	tabView->AddTab(menuSettingsView);
229	tabView->AddTab(applicationsSettingsView);
230	tabView->AddTab(windowSettingsView);
231
232	BLayoutBuilder::Group<>(this)
233		.Add(tabView)
234		.SetInsets(B_USE_DEFAULT_SPACING)
235		.End();
236
237	CenterOnScreen();
238}
239
240
241PreferencesWindow::~PreferencesWindow()
242{
243	UpdateRecentCounts();
244}
245
246
247void
248PreferencesWindow::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		case kStateChanged:
265			EnableDisableDependentItems();
266			break;
267
268		default:
269			BWindow::MessageReceived(message);
270			break;
271	}
272}
273
274
275bool
276PreferencesWindow::QuitRequested()
277{
278	if (IsHidden())
279		return true;
280
281	Hide();
282	return false;
283}
284
285
286void
287PreferencesWindow::WindowActivated(bool active)
288{
289	if (!active && IsMinimized())
290		PostMessage(B_QUIT_REQUESTED);
291}
292
293
294void
295PreferencesWindow::UpdateRecentCounts()
296{
297	BMessage message(kUpdateRecentCounts);
298
299	int32 docCount = atoi(fMenuRecentDocumentCount->Text());
300	int32 appCount = atoi(fMenuRecentApplicationCount->Text());
301	int32 folderCount = atoi(fMenuRecentFolderCount->Text());
302
303	message.AddInt32("documents", max_c(0, docCount));
304	message.AddInt32("applications", max_c(0, appCount));
305	message.AddInt32("folders", max_c(0, folderCount));
306
307	message.AddBool("documentsEnabled", fMenuRecentDocuments->Value());
308	message.AddBool("applicationsEnabled", fMenuRecentApplications->Value());
309	message.AddBool("foldersEnabled", fMenuRecentFolders->Value());
310
311	be_app->PostMessage(&message);
312
313	EnableDisableDependentItems();
314}
315
316
317void
318PreferencesWindow::EnableDisableDependentItems()
319{
320	TBarApp* barApp = static_cast<TBarApp*>(be_app);
321	if (barApp->BarView()->Vertical()
322		&& barApp->BarView()->ExpandoState()) {
323		fAppsShowExpanders->SetEnabled(true);
324		fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value());
325	} else {
326		fAppsShowExpanders->SetEnabled(false);
327		fAppsExpandNew->SetEnabled(false);
328	}
329
330	fMenuRecentDocumentCount->SetEnabled(
331		fMenuRecentDocuments->Value() != B_CONTROL_OFF);
332	fMenuRecentApplicationCount->SetEnabled(
333		fMenuRecentApplications->Value() != B_CONTROL_OFF);
334	fMenuRecentFolderCount->SetEnabled(
335		fMenuRecentFolders->Value() != B_CONTROL_OFF);
336
337	fWindowAutoRaise->SetEnabled(
338		fWindowAlwaysOnTop->Value() == B_CONTROL_OFF);
339}
340