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 <Box.h> 16 #include <Button.h> 17 #include <Catalog.h> 18 #include <CheckBox.h> 19 #include <ControlLook.h> 20 #include <File.h> 21 #include <FormattingConventions.h> 22 #include <GroupLayout.h> 23 #include <ListView.h> 24 #include <Locale.h> 25 #include <LayoutBuilder.h> 26 #include <OpenWithTracker.h> 27 #include <Path.h> 28 #include <RadioButton.h> 29 #include <Roster.h> 30 #include <SeparatorView.h> 31 #include <Screen.h> 32 #include <Slider.h> 33 #include <SpaceLayoutItem.h> 34 #include <TextControl.h> 35 #include <View.h> 36 37 #include "BarApp.h" 38 #include "DeskbarUtils.h" 39 #include "StatusView.h" 40 41 42 static const float kIndentSpacing 43 = be_control_look->DefaultItemSpacing() * 2.3; 44 static const uint32 kSettingsViewChanged = 'Svch'; 45 static const char* kSettingsFileName = "prefs_window_settings"; 46 47 48 #undef B_TRANSLATION_CONTEXT 49 #define B_TRANSLATION_CONTEXT "PreferencesWindow" 50 51 52 PreferencesWindow::PreferencesWindow(BRect frame) 53 : 54 BWindow(frame, B_TRANSLATE("Deskbar preferences"), B_TITLED_WINDOW, 55 B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE) 56 { 57 // Initial settings (used by revert button) 58 memcpy(&fSettings, static_cast<TBarApp*>(be_app)->Settings(), 59 sizeof(desk_settings)); 60 61 // Menu controls 62 fMenuRecentDocuments = new BCheckBox(B_TRANSLATE("Recent documents:"), 63 new BMessage(kUpdateRecentCounts)); 64 fMenuRecentApplications = new BCheckBox(B_TRANSLATE("Recent applications:"), 65 new BMessage(kUpdateRecentCounts)); 66 fMenuRecentFolders = new BCheckBox(B_TRANSLATE("Recent folders:"), 67 new BMessage(kUpdateRecentCounts)); 68 69 fMenuRecentDocumentCount = new BTextControl(NULL, NULL, 70 new BMessage(kUpdateRecentCounts)); 71 fMenuRecentApplicationCount = new BTextControl(NULL, NULL, 72 new BMessage(kUpdateRecentCounts)); 73 fMenuRecentFolderCount = new BTextControl(NULL, NULL, 74 new BMessage(kUpdateRecentCounts)); 75 76 // Applications controls 77 fAppsSort = new BCheckBox(B_TRANSLATE("Sort applications by name"), 78 new BMessage(kSortRunningApps)); 79 fAppsSortTrackerFirst = new BCheckBox(B_TRANSLATE("Tracker always first"), 80 new BMessage(kTrackerFirst)); 81 fAppsShowExpanders = new BCheckBox(B_TRANSLATE("Show application expander"), 82 new BMessage(kSuperExpando)); 83 fAppsExpandNew = new BCheckBox(B_TRANSLATE("Expand new applications"), 84 new BMessage(kExpandNewTeams)); 85 fAppsHideLabels = new BCheckBox(B_TRANSLATE("Hide application names"), 86 new BMessage(kHideLabels)); 87 fAppsIconSizeSlider = new BSlider("icon_size", B_TRANSLATE("Icon size"), 88 new BMessage(kResizeTeamIcons), kMinimumIconSize / kIconSizeInterval, 89 kMaximumIconSize / kIconSizeInterval, B_HORIZONTAL); 90 fAppsIconSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM); 91 fAppsIconSizeSlider->SetHashMarkCount((kMaximumIconSize - kMinimumIconSize) 92 / kIconSizeInterval + 1); 93 fAppsIconSizeSlider->SetLimitLabels(B_TRANSLATE("Small"), 94 B_TRANSLATE("Large")); 95 fAppsIconSizeSlider->SetModificationMessage(new BMessage(kResizeTeamIcons)); 96 97 // Window controls 98 fWindowAlwaysOnTop = new BCheckBox(B_TRANSLATE("Always on top"), 99 new BMessage(kAlwaysTop)); 100 fWindowAutoRaise = new BCheckBox(B_TRANSLATE("Auto-raise"), 101 new BMessage(kAutoRaise)); 102 fWindowAutoHide = new BCheckBox(B_TRANSLATE("Auto-hide"), 103 new BMessage(kAutoHide)); 104 105 // Menu settings 106 BTextView* docTextView = fMenuRecentDocumentCount->TextView(); 107 BTextView* appTextView = fMenuRecentApplicationCount->TextView(); 108 BTextView* folderTextView = fMenuRecentFolderCount->TextView(); 109 110 for (int32 i = 0; i < 256; i++) { 111 if (!isdigit(i)) { 112 docTextView->DisallowChar(i); 113 appTextView->DisallowChar(i); 114 folderTextView->DisallowChar(i); 115 } 116 } 117 118 docTextView->SetMaxBytes(4); 119 appTextView->SetMaxBytes(4); 120 folderTextView->SetMaxBytes(4); 121 122 int32 docCount = fSettings.recentDocsCount; 123 int32 appCount = fSettings.recentAppsCount; 124 int32 folderCount = fSettings.recentFoldersCount; 125 126 fMenuRecentDocuments->SetValue(fSettings.recentDocsEnabled); 127 fMenuRecentDocumentCount->SetEnabled(fSettings.recentDocsEnabled); 128 129 fMenuRecentApplications->SetValue(fSettings.recentAppsEnabled); 130 fMenuRecentApplicationCount->SetEnabled(fSettings.recentAppsEnabled); 131 132 fMenuRecentFolders->SetValue(fSettings.recentFoldersEnabled); 133 fMenuRecentFolderCount->SetEnabled(fSettings.recentFoldersEnabled); 134 135 BString docString; 136 BString appString; 137 BString folderString; 138 139 docString << docCount; 140 appString << appCount; 141 folderString << folderCount; 142 143 fMenuRecentDocumentCount->SetText(docString.String()); 144 fMenuRecentApplicationCount->SetText(appString.String()); 145 fMenuRecentFolderCount->SetText(folderString.String()); 146 147 // Applications settings 148 fAppsSort->SetValue(fSettings.sortRunningApps); 149 fAppsSortTrackerFirst->SetValue(fSettings.trackerAlwaysFirst); 150 fAppsShowExpanders->SetValue(fSettings.superExpando); 151 fAppsExpandNew->SetValue(fSettings.expandNewTeams); 152 fAppsHideLabels->SetValue(fSettings.hideLabels); 153 fAppsIconSizeSlider->SetValue(fSettings.iconSize 154 / kIconSizeInterval); 155 156 // Window settings 157 fWindowAlwaysOnTop->SetValue(fSettings.alwaysOnTop); 158 fWindowAutoRaise->SetValue(fSettings.autoRaise); 159 fWindowAutoHide->SetValue(fSettings.autoHide); 160 161 _EnableDisableDependentItems(); 162 163 // Targets 164 fAppsSort->SetTarget(be_app); 165 fAppsSortTrackerFirst->SetTarget(be_app); 166 fAppsShowExpanders->SetTarget(be_app); 167 fAppsExpandNew->SetTarget(be_app); 168 fAppsHideLabels->SetTarget(be_app); 169 fAppsIconSizeSlider->SetTarget(be_app); 170 171 fWindowAlwaysOnTop->SetTarget(be_app); 172 fWindowAutoRaise->SetTarget(be_app); 173 fWindowAutoHide->SetTarget(be_app); 174 175 // Applications 176 BBox* appsSettingsBox = new BBox("applications"); 177 appsSettingsBox->SetLabel(B_TRANSLATE("Applications")); 178 appsSettingsBox->AddChild(BLayoutBuilder::Group<>() 179 .AddGroup(B_VERTICAL, 0) 180 .Add(fAppsSort) 181 .Add(fAppsSortTrackerFirst) 182 .Add(fAppsShowExpanders) 183 .AddGroup(B_HORIZONTAL, 0) 184 .Add(BSpaceLayoutItem::CreateHorizontalStrut(kIndentSpacing)) 185 .Add(fAppsExpandNew) 186 .End() 187 .Add(fAppsHideLabels) 188 .AddGlue() 189 .Add(BSpaceLayoutItem::CreateVerticalStrut(B_USE_SMALL_SPACING)) 190 .Add(fAppsIconSizeSlider) 191 .SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING, 192 B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING) 193 .End() 194 .View()); 195 196 // Menu 197 BBox* menuBox = new BBox("menu"); 198 menuBox->SetLabel(B_TRANSLATE("Menu")); 199 menuBox->AddChild(BLayoutBuilder::Group<>() 200 .AddGroup(B_VERTICAL, 0) 201 .AddGroup(B_HORIZONTAL, 0) 202 .AddGroup(B_VERTICAL, 0) 203 .Add(fMenuRecentDocuments) 204 .Add(fMenuRecentFolders) 205 .Add(fMenuRecentApplications) 206 .End() 207 .AddGroup(B_VERTICAL, 0) 208 .Add(fMenuRecentDocumentCount) 209 .Add(fMenuRecentFolderCount) 210 .Add(fMenuRecentApplicationCount) 211 .End() 212 .End() 213 .Add(BSpaceLayoutItem::CreateVerticalStrut( 214 B_USE_SMALL_SPACING)) 215 .Add(new BButton(B_TRANSLATE("Edit in Tracker" 216 B_UTF8_ELLIPSIS), new BMessage(kEditInTracker))) 217 .AddGlue() 218 .SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING, 219 B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING) 220 .End() 221 .View()); 222 223 // Window 224 BBox* windowSettingsBox = new BBox("window"); 225 windowSettingsBox->SetLabel(B_TRANSLATE("Window")); 226 windowSettingsBox->AddChild(BLayoutBuilder::Group<>() 227 .AddGroup(B_VERTICAL, 0) 228 .Add(fWindowAlwaysOnTop) 229 .Add(fWindowAutoRaise) 230 .Add(fWindowAutoHide) 231 .AddGlue() 232 .SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING, 233 B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING) 234 .End() 235 .View()); 236 237 // Action Buttons 238 fDefaultsButton = new BButton(B_TRANSLATE("Defaults"), 239 new BMessage(kDefaults)); 240 fRevertButton = new BButton(B_TRANSLATE("Revert"), 241 new BMessage(kRevert)); 242 243 // Layout 244 BLayoutBuilder::Group<>(this) 245 .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING) 246 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) 247 .Add(appsSettingsBox) 248 .AddGroup(B_VERTICAL, B_USE_SMALL_SPACING) 249 .Add(menuBox) 250 .Add(windowSettingsBox) 251 .End() 252 .End() 253 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) 254 .Add(fDefaultsButton) 255 .Add(fRevertButton) 256 .AddGlue() 257 .End() 258 .SetInsets(B_USE_DEFAULT_SPACING) 259 .End(); 260 261 BMessage windowSettings; 262 BPoint where; 263 if (_LoadSettings(&windowSettings) == B_OK 264 && windowSettings.FindPoint("window_position", &where) == B_OK 265 && BScreen(this).Frame().Contains(where)) { 266 MoveTo(where); 267 } else 268 CenterOnScreen(); 269 } 270 271 272 PreferencesWindow::~PreferencesWindow() 273 { 274 _UpdateRecentCounts(); 275 } 276 277 278 void 279 PreferencesWindow::MessageReceived(BMessage* message) 280 { 281 switch (message->what) { 282 case kEditInTracker: 283 OpenWithTracker(B_USER_DESKBAR_DIRECTORY); 284 break; 285 286 case kUpdatePreferences: 287 _EnableDisableDependentItems(); 288 _UpdateButtons(); 289 break; 290 291 case kUpdateRecentCounts: 292 _UpdateRecentCounts(); 293 _UpdateButtons(); 294 break; 295 296 case kStateChanged: 297 _EnableDisableDependentItems(); 298 break; 299 300 case kRevert: 301 _UpdatePreferences(&fSettings); 302 break; 303 304 case kDefaults: 305 _UpdatePreferences( 306 static_cast<TBarApp*>(be_app)->DefaultSettings()); 307 break; 308 309 default: 310 BWindow::MessageReceived(message); 311 break; 312 } 313 } 314 315 316 bool 317 PreferencesWindow::QuitRequested() 318 { 319 BMessage windowSettings; 320 windowSettings.AddPoint("window_position", Frame().LeftTop()); 321 _SaveSettings(&windowSettings); 322 323 be_app->PostMessage(kConfigQuit); 324 325 return false; 326 } 327 328 329 void 330 PreferencesWindow::Show() 331 { 332 if (IsHidden()) 333 SetWorkspaces(B_CURRENT_WORKSPACE); 334 335 _UpdateButtons(); 336 337 BWindow::Show(); 338 } 339 340 341 // #pragma mark - private methods 342 343 344 void 345 PreferencesWindow::_EnableDisableDependentItems() 346 { 347 TBarApp* barApp = static_cast<TBarApp*>(be_app); 348 if (barApp->BarView()->Vertical() 349 && barApp->BarView()->ExpandoState()) { 350 fAppsShowExpanders->SetEnabled(true); 351 fAppsExpandNew->SetEnabled(fAppsShowExpanders->Value()); 352 } else { 353 fAppsShowExpanders->SetEnabled(false); 354 fAppsExpandNew->SetEnabled(false); 355 } 356 357 fMenuRecentDocumentCount->SetEnabled( 358 fMenuRecentDocuments->Value() != B_CONTROL_OFF); 359 fMenuRecentFolderCount->SetEnabled( 360 fMenuRecentFolders->Value() != B_CONTROL_OFF); 361 fMenuRecentApplicationCount->SetEnabled( 362 fMenuRecentApplications->Value() != B_CONTROL_OFF); 363 364 fWindowAutoRaise->SetEnabled( 365 fWindowAlwaysOnTop->Value() == B_CONTROL_OFF); 366 } 367 368 369 bool 370 PreferencesWindow::_IsDefaultable() 371 { 372 desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings(); 373 desk_settings* defaults = static_cast<TBarApp*>(be_app)->DefaultSettings(); 374 375 return defaults->sortRunningApps != settings->sortRunningApps 376 || defaults->trackerAlwaysFirst != settings->trackerAlwaysFirst 377 || defaults->superExpando != settings->superExpando 378 || defaults->expandNewTeams != settings->expandNewTeams 379 || defaults->hideLabels != settings->hideLabels 380 || defaults->iconSize != settings->iconSize 381 || defaults->recentAppsEnabled != settings->recentAppsEnabled 382 || defaults->recentDocsEnabled != settings->recentDocsEnabled 383 || defaults->recentFoldersEnabled 384 != settings->recentFoldersEnabled 385 || defaults->recentAppsCount != settings->recentAppsCount 386 || defaults->recentDocsCount != settings->recentDocsCount 387 || defaults->recentFoldersCount != settings->recentFoldersCount 388 || defaults->alwaysOnTop != settings->alwaysOnTop 389 || defaults->autoRaise != settings->autoRaise 390 || defaults->autoHide != settings->autoHide; 391 } 392 393 394 bool 395 PreferencesWindow::_IsRevertable() 396 { 397 desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings(); 398 399 return fSettings.sortRunningApps != settings->sortRunningApps 400 || fSettings.trackerAlwaysFirst != settings->trackerAlwaysFirst 401 || fSettings.superExpando != settings->superExpando 402 || fSettings.expandNewTeams != settings->expandNewTeams 403 || fSettings.hideLabels != settings->hideLabels 404 || fSettings.iconSize != settings->iconSize 405 || fSettings.recentAppsEnabled != settings->recentAppsEnabled 406 || fSettings.recentDocsEnabled != settings->recentDocsEnabled 407 || fSettings.recentFoldersEnabled 408 != settings->recentFoldersEnabled 409 || fSettings.recentAppsCount != settings->recentAppsCount 410 || fSettings.recentDocsCount != settings->recentDocsCount 411 || fSettings.recentFoldersCount != settings->recentFoldersCount 412 || fSettings.alwaysOnTop != settings->alwaysOnTop 413 || fSettings.autoRaise != settings->autoRaise 414 || fSettings.autoHide != settings->autoHide; 415 } 416 417 418 status_t 419 PreferencesWindow::_InitSettingsFile(BFile* file, bool write) 420 { 421 BPath prefsPath; 422 status_t status = GetDeskbarSettingsDirectory(prefsPath); 423 if (status != B_OK) 424 return status; 425 426 status = prefsPath.Append(kSettingsFileName); 427 if (status != B_OK) 428 return status; 429 430 if (write) { 431 status = file->SetTo(prefsPath.Path(), 432 B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY); 433 } else 434 status = file->SetTo(prefsPath.Path(), B_READ_ONLY); 435 436 return status; 437 } 438 439 440 status_t 441 PreferencesWindow::_LoadSettings(BMessage* settings) 442 { 443 BFile prefsFile; 444 status_t status = _InitSettingsFile(&prefsFile, false); 445 if (status != B_OK) 446 return status; 447 448 return settings->Unflatten(&prefsFile); 449 } 450 451 452 status_t 453 PreferencesWindow::_SaveSettings(BMessage* settings) 454 { 455 BFile prefsFile; 456 status_t status = _InitSettingsFile(&prefsFile, true); 457 if (status != B_OK) 458 return status; 459 460 return settings->Flatten(&prefsFile); 461 } 462 463 464 void 465 PreferencesWindow::_UpdateButtons() 466 { 467 fDefaultsButton->SetEnabled(_IsDefaultable()); 468 fRevertButton->SetEnabled(_IsRevertable()); 469 } 470 471 472 void 473 PreferencesWindow::_UpdatePreferences(desk_settings* settings) 474 { 475 desk_settings* current = static_cast<TBarApp*>(be_app)->Settings(); 476 bool updateRecentCounts = false; 477 478 if (current->sortRunningApps != settings->sortRunningApps) { 479 fAppsSort->SetValue(settings->sortRunningApps); 480 fAppsSort->Invoke(); 481 } 482 if (current->trackerAlwaysFirst != settings->trackerAlwaysFirst) { 483 fAppsSortTrackerFirst->SetValue(settings->trackerAlwaysFirst); 484 fAppsSortTrackerFirst->Invoke(); 485 } 486 if (current->superExpando != settings->superExpando) { 487 fAppsShowExpanders->SetValue(settings->superExpando); 488 fAppsShowExpanders->Invoke(); 489 } 490 if (current->expandNewTeams != settings->expandNewTeams) { 491 fAppsExpandNew->SetValue(settings->expandNewTeams); 492 fAppsExpandNew->Invoke(); 493 } 494 if (current->hideLabels != settings->hideLabels) { 495 fAppsHideLabels->SetValue(settings->hideLabels); 496 fAppsHideLabels->Invoke(); 497 } 498 if (current->iconSize != settings->iconSize) { 499 fAppsIconSizeSlider->SetValue(settings->iconSize / kIconSizeInterval); 500 fAppsIconSizeSlider->Invoke(); 501 } 502 if (current->recentDocsEnabled != settings->recentDocsEnabled) { 503 fMenuRecentDocuments->SetValue(settings->recentDocsEnabled 504 ? B_CONTROL_ON : B_CONTROL_OFF); 505 updateRecentCounts = true; 506 } 507 if (current->recentFoldersEnabled != settings->recentFoldersEnabled) { 508 fMenuRecentFolders->SetValue(settings->recentFoldersEnabled 509 ? B_CONTROL_ON : B_CONTROL_OFF); 510 updateRecentCounts = true; 511 } 512 if (current->recentAppsEnabled != settings->recentAppsEnabled) { 513 fMenuRecentApplications->SetValue(settings->recentAppsEnabled 514 ? B_CONTROL_ON : B_CONTROL_OFF); 515 updateRecentCounts = true; 516 } 517 if (current->recentDocsCount != settings->recentDocsCount) { 518 BString docString; 519 docString << settings->recentDocsCount; 520 fMenuRecentDocumentCount->SetText(docString.String()); 521 updateRecentCounts = true; 522 } 523 if (current->recentFoldersCount != settings->recentFoldersCount) { 524 BString folderString; 525 folderString << settings->recentFoldersCount; 526 fMenuRecentFolderCount->SetText(folderString.String()); 527 updateRecentCounts = true; 528 } 529 if (current->recentAppsCount != settings->recentAppsCount) { 530 BString appString; 531 appString << settings->recentAppsCount; 532 fMenuRecentApplicationCount->SetText(appString.String()); 533 updateRecentCounts = true; 534 } 535 if (current->alwaysOnTop != settings->alwaysOnTop) { 536 fWindowAlwaysOnTop->SetValue(settings->alwaysOnTop); 537 fWindowAlwaysOnTop->Invoke(); 538 } 539 if (current->autoRaise != settings->autoRaise) { 540 fWindowAutoRaise->SetValue(settings->autoRaise); 541 fWindowAutoRaise->Invoke(); 542 } 543 if (current->autoHide != settings->autoHide) { 544 fWindowAutoHide->SetValue(settings->autoHide); 545 fWindowAutoHide->Invoke(); 546 } 547 548 if (updateRecentCounts) 549 _UpdateRecentCounts(); 550 } 551 552 553 void 554 PreferencesWindow::_UpdateRecentCounts() 555 { 556 BMessage message(kUpdateRecentCounts); 557 558 int32 docCount = atoi(fMenuRecentDocumentCount->Text()); 559 int32 appCount = atoi(fMenuRecentApplicationCount->Text()); 560 int32 folderCount = atoi(fMenuRecentFolderCount->Text()); 561 562 message.AddInt32("documents", max_c(0, docCount)); 563 message.AddInt32("applications", max_c(0, appCount)); 564 message.AddInt32("folders", max_c(0, folderCount)); 565 566 message.AddBool("documentsEnabled", fMenuRecentDocuments->Value()); 567 message.AddBool("applicationsEnabled", fMenuRecentApplications->Value()); 568 message.AddBool("foldersEnabled", fMenuRecentFolders->Value()); 569 570 be_app->PostMessage(&message); 571 572 _EnableDisableDependentItems(); 573 } 574