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