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