Ticket #8188: workspacesdiff1.patch
File workspacesdiff1.patch, 13.9 KB (added by , 13 years ago) |
---|
-
src/apps/workspaces/Workspaces.cpp
diff --git src/apps/workspaces/Workspaces.cpp src/apps/workspaces/Workspaces.cpp index e7b99f9..f92b580 100644
4 4 * This file is distributed under the terms of the MIT License. 5 5 * 6 6 * Authors: 7 * François Revol, revol@free.fr8 7 * Axel Dörfler, axeld@pinc-software.de 9 * Oliver "Madison" Kohl, 8 * Daniel Devine, devine@ddevnet.net 9 * Oliver "Madison" Kohl 10 10 * Matt Madia 11 * François Revol, revol@free.fr 11 12 */ 12 13 13 14 … … 21 22 #include <FindDirectory.h> 22 23 #include <Locale.h> 23 24 #include <MenuItem.h> 25 #include <new.h> 24 26 #include <Path.h> 25 27 #include <PopUpMenu.h> 26 28 #include <Roster.h> 27 29 #include <Screen.h> 28 30 #include <TextView.h> 29 31 #include <Window.h> 30 31 32 #include <ctype.h> 32 33 #include <stdio.h> 33 34 #include <stdlib.h> … … static const uint32 kMsgToggleAutoRaise = 'tgAR'; 55 56 static const uint32 kMsgToggleAlwaysOnTop = 'tgAT'; 56 57 static const uint32 kMsgToggleLiveInDeskbar = 'tgDb'; 57 58 58 static const float kScreenBorderOffset = 10.0;59 59 60 60 extern "C" _EXPORT BView* instantiate_deskbar_item(); 61 61 … … class WorkspacesSettings { 71 71 bool AlwaysOnTop() const { return fAlwaysOnTop; } 72 72 bool HasTitle() const { return fHasTitle; } 73 73 bool HasBorder() const { return fHasBorder; } 74 bool SettingsLoaded() const { return fLoaded; } 74 75 75 76 void UpdateFramesForScreen(BRect screenFrame); 76 77 void UpdateScreenFrame(); … … class WorkspacesSettings { 90 91 bool fAlwaysOnTop; 91 92 bool fHasTitle; 92 93 bool fHasBorder; 94 bool fLoaded; 93 95 }; 94 96 95 97 class WorkspacesView : public BView { 96 98 public: 97 WorkspacesView(BRect frame, bool showDragger );99 WorkspacesView(BRect frame, bool showDragger=true); 98 100 WorkspacesView(BMessage* archive); 99 101 ~WorkspacesView(); 100 102 … … class WorkspacesWindow : public BWindow { 134 136 virtual void MessageReceived(BMessage *msg); 135 137 virtual bool QuitRequested(); 136 138 137 void SetAutoRaise(bool enable); 138 bool IsAutoRaising() const { return fAutoRaising; } 139 void SetAutoRaising(bool enable); 140 bool IsAutoRaising() const { return fSettings->AutoRaising(); } 141 bool HasTitle() const { return fSettings->HasTitle(); } 142 float GetTabHeight() const { return fTabHeight; } 143 float GetBorderWidth() const { return fBorderWidth; } 144 float GetScreenBorderOffset() const { return 2.0 * fBorderWidth; } 139 145 140 146 private: 141 147 WorkspacesSettings *fSettings; 142 bool fAutoRaising; 148 float fTabHeight; 149 float fBorderWidth; 143 150 }; 144 151 145 152 class WorkspacesApp : public BApplication { … … WorkspacesSettings::WorkspacesSettings() 167 174 { 168 175 UpdateScreenFrame(); 169 176 170 bool loaded = false;171 177 BScreen screen; 172 178 BPath path; 173 179 BFile file; 180 174 181 if (_Open(file, B_READ_ONLY) == B_OK) { 175 182 BMessage settings; 176 183 if (settings.Unflatten(&file) == B_OK) { 177 184 if (settings.FindRect("window", &fWindowFrame) == B_OK 178 && settings.FindRect("screen", &fScreenFrame) == B_OK) 179 loaded = true; 185 && settings.FindRect("screen", &fScreenFrame) == B_OK){ 186 fLoaded = true; 187 } 180 188 181 189 settings.FindBool("auto-raise", &fAutoRaising); 182 190 settings.FindBool("always on top", &fAlwaysOnTop); … … WorkspacesSettings::WorkspacesSettings() 187 195 fHasBorder = true; 188 196 } 189 197 } else { 190 // try reading BeOS compatible settings 191 BPath path; 192 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { 198 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK ) { 199 // try reading BeOS compatible settings 193 200 path.Append(kOldSettingFile); 194 201 BFile file(path.Path(), B_READ_ONLY); 195 if (file.InitCheck() == B_OK 202 if (file.InitCheck() == B_OK 196 203 && file.Read(&fWindowFrame, sizeof(BRect)) == sizeof(BRect)) { 197 204 // we now also store the frame of the screen to know 198 205 // in which context the window frame has been chosen … … WorkspacesSettings::WorkspacesSettings() 201 208 fScreenFrame = frame; 202 209 else 203 210 fScreenFrame = screen.Frame(); 204 205 loaded = true; 211 fLoaded = true; 206 212 } 207 213 } 208 214 } 209 215 210 if ( loaded) {211 // if the current screen frame is different from the one216 if (fLoaded) { 217 // If the current screen frame is different from the one 212 218 // just loaded, we need to alter the window frame accordingly 213 219 if (fScreenFrame != screen.Frame()) 214 220 UpdateFramesForScreen(screen.Frame()); 215 221 } 216 217 if (!loaded218 || !(screen.Frame().right + 5 >= fWindowFrame.right219 && screen.Frame().bottom + 5 >= fWindowFrame.bottom220 && screen.Frame().left - 5 <= fWindowFrame.left221 && screen.Frame().top - 5 <= fWindowFrame.top)) {222 // set to some usable defaults223 float screenWidth = screen.Frame().Width();224 float screenHeight = screen.Frame().Height();225 float aspectRatio = screenWidth / screenHeight;226 227 uint32 columns, rows;228 BPrivate::get_workspaces_layout(&columns, &rows);229 230 // default size of ~1/10 of screen width231 float workspaceWidth = screenWidth / 10;232 float workspaceHeight = workspaceWidth / aspectRatio;233 234 float width = floor(workspaceWidth * columns);235 float height = floor(workspaceHeight * rows);236 237 float tabHeight = 20;238 // TODO: find tabHeight without being a window239 240 // shrink to fit more241 while (width + 2 * kScreenBorderOffset > screenWidth242 || height + 2 * kScreenBorderOffset + tabHeight > screenHeight) {243 width = floor(0.95 * width);244 height = floor(0.95 * height);245 }246 247 fWindowFrame = fScreenFrame;248 fWindowFrame.OffsetBy(-kScreenBorderOffset, -kScreenBorderOffset);249 fWindowFrame.left = fWindowFrame.right - width;250 fWindowFrame.top = fWindowFrame.bottom - height;251 }252 222 } 253 223 254 224 … … WorkspacesSettings::~WorkspacesSettings() 260 230 return; 261 231 262 232 BMessage settings('wksp'); 263 233 264 234 if (settings.AddRect("window", fWindowFrame) == B_OK 265 235 && settings.AddRect("screen", fScreenFrame) == B_OK 266 236 && settings.AddBool("auto-raise", fAutoRaising) == B_OK … … WorkspacesSettings::~WorkspacesSettings() 268 238 && settings.AddBool("has title", fHasTitle) == B_OK 269 239 && settings.AddBool("has border", fHasBorder) == B_OK) 270 240 settings.Flatten(&file); 241 else 242 fprintf(stderr,"An error occured while saving settings.\n"); 271 243 } 272 244 273 245 … … WorkspacesSettings::_Open(BFile& file, int mode) 297 269 298 270 void 299 271 WorkspacesSettings::UpdateFramesForScreen(BRect newScreenFrame) 300 { 272 { 301 273 // don't change the position if the screen frame hasn't changed 302 274 if (newScreenFrame == fScreenFrame) 303 275 return; … … WorkspacesSettings::UpdateFramesForScreen(BRect newScreenFrame) 313 285 fWindowFrame.OffsetTo(fWindowFrame.left, 314 286 newScreenFrame.bottom - (fScreenFrame.bottom - fWindowFrame.top)); 315 287 } 316 317 288 fScreenFrame = newScreenFrame; 318 289 } 319 290 … … WorkspacesSettings::SetWindowFrame(BRect frame) 336 307 // #pragma mark - 337 308 338 309 339 WorkspacesView::WorkspacesView(BRect frame, bool showDragger =true)310 WorkspacesView::WorkspacesView(BRect frame, bool showDragger) 340 311 : 341 312 BView(frame, kDeskbarItemName, B_FOLLOW_ALL, 342 kWorkspacesViewFlag | B_FRAME_EVENTS), 343 fParentWhichDrawsOnChildren(NULL), 313 kWorkspacesViewFlag | B_FRAME_EVENTS), // kWorkspacesViewFlag is picked up by the app_server. 314 fParentWhichDrawsOnChildren(NULL), // ...That's how the desktop frames are done! 344 315 fCurrentFrame(frame) 345 316 { 346 317 if(showDragger) { … … WorkspacesView::Instantiate(BMessage* archive) 380 351 if (!validate_instantiation(archive, "WorkspacesView")) 381 352 return NULL; 382 353 383 return new WorkspacesView(archive);354 return new(std::nothrow) WorkspacesView(archive); 384 355 } 385 356 386 357 … … status_t 388 359 WorkspacesView::Archive(BMessage* archive, bool deep) const 389 360 { 390 361 status_t status = BView::Archive(archive, deep); 391 if (status == B_OK)392 status = archive->AddString("add_on", kSignature);393 if (status == B_OK)394 status = archive->AddString("class", "WorkspacesView");395 396 362 return status; 397 363 } 398 364 … … WorkspacesView::_AboutRequested() 420 386 font.SetFace(B_BOLD_FACE); 421 387 view->SetFontAndColor(0, 10, &font); 422 388 423 alert->Go( );389 alert->Go(NULL); 424 390 } 425 391 426 392 … … WorkspacesView::MessageReceived(BMessage* message) 511 477 // since HasItem() locks up we just remove directly. 512 478 BDeskbar deskbar; 513 479 // we shouldn't do this here actually, but it works for now... 514 deskbar.RemoveItem 480 deskbar.RemoveItem(kDeskbarItemName); 515 481 break; 516 482 } 517 483 … … WorkspacesView::MouseMoved(BPoint where, uint32 transit, 534 500 535 501 where = ConvertToScreen(where); 536 502 BScreen screen(window); 537 BRect frame = screen.Frame(); 538 if (where.x == frame.left || where.x == frame.right 539 || where.y == frame.top || where.y == frame.bottom) { 540 // cursor is on screen edge 541 if (window->Frame().Contains(where)) 503 BRect screenFrame = screen.Frame(); 504 BRect windowFrame = window->Frame(); 505 float tabHeight = window->GetTabHeight(); 506 float borderWidth = window->GetBorderWidth(); 507 508 // cursor is on screen edge 509 if (where.x == screenFrame.left || where.x == screenFrame.right 510 || where.y == screenFrame.top || where.y == screenFrame.bottom) { 511 512 if (!window->HasTitle()) 513 tabHeight = 0; 514 515 // Stretch frame so that it may meet the edge of the screen. 516 windowFrame.InsetBy(-borderWidth,-(tabHeight + borderWidth)); 517 518 if (windowFrame.Contains(where)) 542 519 window->Activate(); 543 520 } 544 521 } … … WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings) 642 619 BWindow(settings->WindowFrame(), B_TRANSLATE_SYSTEM_NAME("Workspaces"), 643 620 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 644 621 B_AVOID_FRONT | B_WILL_ACCEPT_FIRST_CLICK, B_ALL_WORKSPACES), 645 fSettings(settings), 646 fAutoRaising(false) 622 fSettings(settings) 647 623 { 648 624 AddChild(new WorkspacesView(Bounds())); 625 626 // Turn window decor on to grab decor widths. 627 BMessage windowSettings; 628 SetLook(B_TITLED_WINDOW_LOOK); 629 if (GetDecoratorSettings(&windowSettings) == B_OK){ 630 BRect tabFrame = windowSettings.FindRect("tab frame"); 631 float borderWidth = windowSettings.FindFloat("border width"); 632 fTabHeight = tabFrame.Height(); 633 fBorderWidth = borderWidth; 634 635 // NOTE: Below is based on old code from WorkspacesSettings::WorkspacesSettings(). 636 // Decorator dimensions are dynamically called and set instead of hardcoded here. 637 638 // TODO: refresh tab height and border width variables if decor is changed. 639 640 BScreen screen; 641 642 float screenWidth = screen.Frame().Width(); 643 float screenHeight = screen.Frame().Height(); 644 float aspectRatio = screenWidth / screenHeight; 645 646 uint32 columns, rows; 647 BPrivate::get_workspaces_layout(&columns, &rows); 648 649 // default size of ~1/10 of screen width 650 float workspaceWidth = screenWidth / 10; 651 float workspaceHeight = workspaceWidth / aspectRatio; 652 653 float width = floor(workspaceWidth * columns); 654 float height = floor(workspaceHeight * rows); 655 656 // If you have too many workspaces to fit on the screen, shrink until they fit. 657 while (width + 2 * borderWidth > screenWidth 658 || height + 2 * borderWidth + GetTabHeight() > screenHeight) { 659 width = floor(0.95 * width); 660 height = floor(0.95 * height); 661 } 662 663 BRect windowFrame = fSettings->WindowFrame(); 664 BRect frame; 665 666 // If we don't have settings loaded or decorator/screen dimensions changed, load defaults. 667 if (!fSettings->SettingsLoaded() || fSettings->ScreenFrame() != screen.Frame()) { 668 frame = fSettings->ScreenFrame(); 669 frame.left = frame.right - width; 670 frame.top = frame.bottom - height; 671 ResizeTo(frame.Width(), frame.Height()); 672 // Put it in bottom corner by default. 673 MoveTo(screenWidth - frame.Width() - borderWidth, 674 screenHeight - frame.Height() - borderWidth); 675 } else { 676 frame = windowFrame; 677 } 678 fSettings->SetWindowFrame(frame); 679 } 649 680 650 681 if (!fSettings->HasBorder()) 651 682 SetLook(B_NO_BORDER_WINDOW_LOOK); … … WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings) 655 686 if (fSettings->AlwaysOnTop()) 656 687 SetFeel(B_FLOATING_ALL_WINDOW_FEEL); 657 688 else 658 SetAutoRais e(fSettings->AutoRaising());689 SetAutoRaising(fSettings->AutoRaising()); 659 690 } 660 691 661 692 … … WorkspacesWindow::FrameResized(float width, float height) 685 716 { 686 717 if (!modifiers() & B_SHIFT_KEY) { 687 718 BWindow::FrameResized(width, height); 719 fSettings->SetWindowFrame(Frame()); 688 720 return; 689 721 } 690 722 // This is only run if you are trying to keep the aspect ratio. 691 723 uint32 columns, rows; 692 724 BPrivate::get_workspaces_layout(&columns, &rows); 693 725 … … WorkspacesWindow::Zoom(BPoint origin, float width, float height) 724 756 width = floor(workspaceWidth * columns); 725 757 height = floor(workspaceHeight * rows); 726 758 727 float tabHeight = Frame().top - DecoratorFrame().top; 728 729 while (width + 2 * kScreenBorderOffset > screenWidth 730 || height + 2 * kScreenBorderOffset + tabHeight > screenHeight) { 759 while (width + 2 * GetScreenBorderOffset() > screenWidth 760 || height + 2 * GetScreenBorderOffset() + GetTabHeight() > screenHeight) { 731 761 width = floor(0.95 * width); 732 762 height = floor(0.95 * height); 733 763 } … … WorkspacesWindow::Zoom(BPoint origin, float width, float height) 735 765 ResizeTo(width, height); 736 766 737 767 origin = screen.Frame().RightBottom(); 738 origin.x -= kScreenBorderOffset+ width;739 origin.y -= kScreenBorderOffset+ height;768 origin.x -= GetScreenBorderOffset() + width; 769 origin.y -= GetScreenBorderOffset() + height; 740 770 741 771 MoveTo(origin); 742 772 } … … WorkspacesWindow::MessageReceived(BMessage *message) 797 827 } 798 828 799 829 case kMsgToggleAutoRaise: 800 SetAutoRais e(!IsAutoRaising());830 SetAutoRaising(!IsAutoRaising()); 801 831 SetFeel(B_NORMAL_WINDOW_FEEL); 802 832 break; 803 833 … … WorkspacesWindow::QuitRequested() 845 875 846 876 847 877 void 848 WorkspacesWindow::SetAutoRais e(bool enable)878 WorkspacesWindow::SetAutoRaising(bool enable) 849 879 { 850 if (enable == fAutoRaising)880 if (enable == IsAutoRaising()) 851 881 return; 852 882 853 fAutoRaising = enable;854 883 fSettings->SetAutoRaising(enable); 855 884 856 885 if (enable) … … WorkspacesApp::ArgvReceived(int32 argc, char **argv) 923 952 else if (!strcmp(argv[i], "--alwaysontop")) 924 953 fWindow->SetFeel(B_FLOATING_ALL_WINDOW_FEEL); 925 954 else if (!strcmp(argv[i], "--autoraise")) 926 fWindow->SetAutoRais e(true);955 fWindow->SetAutoRaising(true); 927 956 else { 928 957 const char *programName = strrchr(argv[0], '/'); 929 958 programName = programName ? programName + 1 : argv[0];