1 /* 2 * Copyright 2001-2013, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus, superstippi@gmx.de 7 * Marc Flerackers, mflerackers@androme.be 8 * John Scipione, jscipione@gmail.com 9 * Ingo Weinhold, bonefish@cs.tu-berlin.de 10 */ 11 12 13 #include <MenuField.h> 14 15 #include <algorithm> 16 #include <stdio.h> 17 // for printf in TRACE 18 #include <stdlib.h> 19 #include <string.h> 20 21 #include <AbstractLayoutItem.h> 22 #include <BMCPrivate.h> 23 #include <ControlLook.h> 24 #include <LayoutUtils.h> 25 #include <MenuBar.h> 26 #include <MenuItem.h> 27 #include <MenuPrivate.h> 28 #include <Message.h> 29 #include <MessageFilter.h> 30 #include <Thread.h> 31 #include <Window.h> 32 33 #include <binary_compatibility/Interface.h> 34 #include <binary_compatibility/Support.h> 35 36 37 #ifdef CALLED 38 # undef CALLED 39 #endif 40 #ifdef TRACE 41 # undef TRACE 42 #endif 43 44 //#define TRACE_MENU_FIELD 45 #ifdef TRACE_MENU_FIELD 46 # include <FunctionTracer.h> 47 static int32 sFunctionDepth = -1; 48 # define CALLED(x...) FunctionTracer _ft("BMenuField", __FUNCTION__, \ 49 sFunctionDepth) 50 # define TRACE(x...) { BString _to; \ 51 _to.Append(' ', (sFunctionDepth + 1) * 2); \ 52 printf("%s", _to.String()); printf(x); } 53 #else 54 # define CALLED(x...) 55 # define TRACE(x...) 56 #endif 57 58 59 static const float kMinMenuBarWidth = 20.0f; 60 // found by experimenting on BeOS R5 61 62 63 namespace { 64 const char* const kFrameField = "BMenuField:layoutItem:frame"; 65 const char* const kMenuBarItemField = "BMenuField:barItem"; 66 const char* const kLabelItemField = "BMenuField:labelItem"; 67 } 68 69 70 // #pragma mark - LabelLayoutItem 71 72 73 class BMenuField::LabelLayoutItem : public BAbstractLayoutItem { 74 public: 75 LabelLayoutItem(BMenuField* parent); 76 LabelLayoutItem(BMessage* archive); 77 78 virtual bool IsVisible(); 79 virtual void SetVisible(bool visible); 80 81 virtual BRect Frame(); 82 virtual void SetFrame(BRect frame); 83 84 void SetParent(BMenuField* parent); 85 virtual BView* View(); 86 87 virtual BSize BaseMinSize(); 88 virtual BSize BaseMaxSize(); 89 virtual BSize BasePreferredSize(); 90 virtual BAlignment BaseAlignment(); 91 92 virtual status_t Archive(BMessage* into, bool deep = true) const; 93 static BArchivable* Instantiate(BMessage* from); 94 95 private: 96 BMenuField* fParent; 97 BRect fFrame; 98 }; 99 100 101 // #pragma mark - MenuBarLayoutItem 102 103 104 class BMenuField::MenuBarLayoutItem : public BAbstractLayoutItem { 105 public: 106 MenuBarLayoutItem(BMenuField* parent); 107 MenuBarLayoutItem(BMessage* from); 108 109 virtual bool IsVisible(); 110 virtual void SetVisible(bool visible); 111 112 virtual BRect Frame(); 113 virtual void SetFrame(BRect frame); 114 115 void SetParent(BMenuField* parent); 116 virtual BView* View(); 117 118 virtual BSize BaseMinSize(); 119 virtual BSize BaseMaxSize(); 120 virtual BSize BasePreferredSize(); 121 virtual BAlignment BaseAlignment(); 122 123 virtual status_t Archive(BMessage* into, bool deep = true) const; 124 static BArchivable* Instantiate(BMessage* from); 125 126 private: 127 BMenuField* fParent; 128 BRect fFrame; 129 }; 130 131 132 // #pragma mark - LayoutData 133 134 135 struct BMenuField::LayoutData { 136 LayoutData() 137 : 138 label_layout_item(NULL), 139 menu_bar_layout_item(NULL), 140 previous_height(-1), 141 valid(false) 142 { 143 } 144 145 LabelLayoutItem* label_layout_item; 146 MenuBarLayoutItem* menu_bar_layout_item; 147 float previous_height; // used in FrameResized() for 148 // invalidation 149 font_height font_info; 150 float label_width; 151 float label_height; 152 BSize min; 153 BSize menu_bar_min; 154 bool valid; 155 }; 156 157 158 // #pragma mark - MouseDownFilter 159 160 161 class MouseDownFilter : public BMessageFilter 162 { 163 public: 164 MouseDownFilter(); 165 virtual ~MouseDownFilter(); 166 167 virtual filter_result Filter(BMessage* message, BHandler** target); 168 }; 169 170 171 MouseDownFilter::MouseDownFilter() 172 : 173 BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE) 174 { 175 } 176 177 178 MouseDownFilter::~MouseDownFilter() 179 { 180 } 181 182 183 filter_result 184 MouseDownFilter::Filter(BMessage* message, BHandler** target) 185 { 186 return message->what == B_MOUSE_DOWN ? B_SKIP_MESSAGE : B_DISPATCH_MESSAGE; 187 } 188 189 190 // #pragma mark - BMenuField 191 192 193 using BPrivate::MenuPrivate; 194 195 BMenuField::BMenuField(BRect frame, const char* name, const char* label, 196 BMenu* menu, uint32 resizingMode, uint32 flags) 197 : 198 BView(frame, name, resizingMode, flags) 199 { 200 CALLED(); 201 202 TRACE("frame.width: %.2f, height: %.2f\n", frame.Width(), frame.Height()); 203 204 InitObject(label); 205 206 frame.OffsetTo(B_ORIGIN); 207 _InitMenuBar(menu, frame, false); 208 209 InitObject2(); 210 } 211 212 213 BMenuField::BMenuField(BRect frame, const char* name, const char* label, 214 BMenu* menu, bool fixedSize, uint32 resizingMode, uint32 flags) 215 : 216 BView(frame, name, resizingMode, flags) 217 { 218 InitObject(label); 219 220 fFixedSizeMB = fixedSize; 221 222 frame.OffsetTo(B_ORIGIN); 223 _InitMenuBar(menu, frame, fixedSize); 224 225 InitObject2(); 226 } 227 228 229 BMenuField::BMenuField(const char* name, const char* label, BMenu* menu, 230 uint32 flags) 231 : 232 BView(name, flags | B_FRAME_EVENTS) 233 { 234 InitObject(label); 235 236 _InitMenuBar(menu, BRect(0, 0, 100, 15), true); 237 238 InitObject2(); 239 } 240 241 242 BMenuField::BMenuField(const char* label, BMenu* menu, uint32 flags) 243 : 244 BView(NULL, flags | B_FRAME_EVENTS) 245 { 246 InitObject(label); 247 248 _InitMenuBar(menu, BRect(0, 0, 100, 15), true); 249 250 InitObject2(); 251 } 252 253 254 //! Copy&Paste error, should be removed at some point (already private) 255 BMenuField::BMenuField(const char* name, const char* label, BMenu* menu, 256 BMessage* message, uint32 flags) 257 : 258 BView(name, flags | B_FRAME_EVENTS) 259 { 260 InitObject(label); 261 262 _InitMenuBar(menu, BRect(0, 0, 100, 15), true); 263 264 InitObject2(); 265 } 266 267 268 //! Copy&Paste error, should be removed at some point (already private) 269 BMenuField::BMenuField(const char* label, BMenu* menu, BMessage* message) 270 : 271 BView(NULL, B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS) 272 { 273 InitObject(label); 274 275 _InitMenuBar(menu, BRect(0, 0, 100, 15), true); 276 277 InitObject2(); 278 } 279 280 281 BMenuField::BMenuField(BMessage* data) 282 : 283 BView(BUnarchiver::PrepareArchive(data)) 284 { 285 BUnarchiver unarchiver(data); 286 const char* label = NULL; 287 data->FindString("_label", &label); 288 289 InitObject(label); 290 291 data->FindFloat("_divide", &fDivider); 292 293 int32 align; 294 if (data->FindInt32("_align", &align) == B_OK) 295 SetAlignment((alignment)align); 296 297 if (!BUnarchiver::IsArchiveManaged(data)) 298 _InitMenuBar(data); 299 unarchiver.Finish(); 300 } 301 302 303 BMenuField::~BMenuField() 304 { 305 free(fLabel); 306 307 status_t dummy; 308 if (fMenuTaskID >= 0) 309 wait_for_thread(fMenuTaskID, &dummy); 310 311 delete fLayoutData; 312 delete fMouseDownFilter; 313 } 314 315 316 BArchivable* 317 BMenuField::Instantiate(BMessage* data) 318 { 319 if (validate_instantiation(data, "BMenuField")) 320 return new BMenuField(data); 321 322 return NULL; 323 } 324 325 326 status_t 327 BMenuField::Archive(BMessage* data, bool deep) const 328 { 329 BArchiver archiver(data); 330 status_t ret = BView::Archive(data, deep); 331 332 if (ret == B_OK && Label()) 333 ret = data->AddString("_label", Label()); 334 335 if (ret == B_OK && !IsEnabled()) 336 ret = data->AddBool("_disable", true); 337 338 if (ret == B_OK) 339 ret = data->AddInt32("_align", Alignment()); 340 if (ret == B_OK) 341 ret = data->AddFloat("_divide", Divider()); 342 343 if (ret == B_OK && fFixedSizeMB) 344 ret = data->AddBool("be:fixeds", true); 345 346 bool dmark = false; 347 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) 348 dmark = menuBar->IsPopUpMarkerShown(); 349 350 data->AddBool("be:dmark", dmark); 351 352 return archiver.Finish(ret); 353 } 354 355 356 status_t 357 BMenuField::AllArchived(BMessage* into) const 358 { 359 status_t err; 360 if ((err = BView::AllArchived(into)) != B_OK) 361 return err; 362 363 BArchiver archiver(into); 364 365 BArchivable* menuBarItem = fLayoutData->menu_bar_layout_item; 366 if (archiver.IsArchived(menuBarItem)) 367 err = archiver.AddArchivable(kMenuBarItemField, menuBarItem); 368 369 if (err != B_OK) 370 return err; 371 372 BArchivable* labelBarItem = fLayoutData->label_layout_item; 373 if (archiver.IsArchived(labelBarItem)) 374 err = archiver.AddArchivable(kLabelItemField, labelBarItem); 375 376 return err; 377 } 378 379 380 status_t 381 BMenuField::AllUnarchived(const BMessage* from) 382 { 383 BUnarchiver unarchiver(from); 384 385 status_t err = B_OK; 386 if ((err = BView::AllUnarchived(from)) != B_OK) 387 return err; 388 389 _InitMenuBar(from); 390 391 if (unarchiver.IsInstantiated(kMenuBarItemField)) { 392 MenuBarLayoutItem*& menuItem = fLayoutData->menu_bar_layout_item; 393 err = unarchiver.FindObject(kMenuBarItemField, 394 BUnarchiver::B_DONT_ASSUME_OWNERSHIP, menuItem); 395 396 if (err == B_OK) 397 menuItem->SetParent(this); 398 else 399 return err; 400 } 401 402 if (unarchiver.IsInstantiated(kLabelItemField)) { 403 LabelLayoutItem*& labelItem = fLayoutData->label_layout_item; 404 err = unarchiver.FindObject(kLabelItemField, 405 BUnarchiver::B_DONT_ASSUME_OWNERSHIP, labelItem); 406 407 if (err == B_OK) 408 labelItem->SetParent(this); 409 } 410 411 return err; 412 } 413 414 415 void 416 BMenuField::Draw(BRect updateRect) 417 { 418 _DrawLabel(updateRect); 419 _DrawMenuBar(updateRect); 420 } 421 422 423 void 424 BMenuField::AttachedToWindow() 425 { 426 CALLED(); 427 rgb_color color; 428 429 BView* parent = Parent(); 430 if (parent != NULL) { 431 // inherit the color from parent 432 color = parent->ViewColor(); 433 if (color == B_TRANSPARENT_COLOR) 434 color = ui_color(B_PANEL_BACKGROUND_COLOR); 435 } else 436 color = ui_color(B_PANEL_BACKGROUND_COLOR); 437 438 SetViewColor(color); 439 SetLowColor(color); 440 } 441 442 443 void 444 BMenuField::AllAttached() 445 { 446 CALLED(); 447 448 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height()); 449 450 float width = Bounds().Width(); 451 if (!fFixedSizeMB && _MenuBarWidth() < kMinMenuBarWidth) { 452 // The menu bar is too narrow, resize it to fit the menu items 453 BMenuItem* item = fMenuBar->ItemAt(0); 454 if (item != NULL) { 455 float right; 456 fMenuBar->GetItemMargins(NULL, NULL, &right, NULL); 457 width = item->Frame().Width() + kVMargin + _MenuBarOffset() + right; 458 } 459 } 460 461 ResizeTo(width, fMenuBar->Bounds().Height() + kVMargin * 2); 462 463 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height()); 464 } 465 466 467 void 468 BMenuField::MouseDown(BPoint where) 469 { 470 BRect bounds = fMenuBar->ConvertFromParent(Bounds()); 471 472 fMenuBar->StartMenuBar(-1, false, true, &bounds); 473 474 fMenuTaskID = spawn_thread((thread_func)_thread_entry, 475 "_m_task_", B_NORMAL_PRIORITY, this); 476 if (fMenuTaskID >= 0 && resume_thread(fMenuTaskID) == B_OK) { 477 if (fMouseDownFilter->Looper() == NULL) 478 Window()->AddCommonFilter(fMouseDownFilter); 479 480 MouseDownThread<BMenuField>::TrackMouse(this, &BMenuField::_DoneTracking, 481 &BMenuField::_Track); 482 } 483 } 484 485 486 void 487 BMenuField::KeyDown(const char* bytes, int32 numBytes) 488 { 489 switch (bytes[0]) { 490 case B_SPACE: 491 case B_RIGHT_ARROW: 492 case B_DOWN_ARROW: 493 { 494 if (!IsEnabled()) 495 break; 496 497 BRect bounds = fMenuBar->ConvertFromParent(Bounds()); 498 499 fMenuBar->StartMenuBar(0, true, true, &bounds); 500 501 bounds = Bounds(); 502 bounds.right = fDivider; 503 504 Invalidate(bounds); 505 } 506 507 default: 508 BView::KeyDown(bytes, numBytes); 509 } 510 } 511 512 513 void 514 BMenuField::MakeFocus(bool focused) 515 { 516 if (IsFocus() == focused) 517 return; 518 519 BView::MakeFocus(focused); 520 521 if (Window() != NULL) 522 Invalidate(); // TODO: use fLayoutData->label_width 523 } 524 525 526 void 527 BMenuField::MessageReceived(BMessage* message) 528 { 529 BView::MessageReceived(message); 530 } 531 532 533 void 534 BMenuField::WindowActivated(bool active) 535 { 536 BView::WindowActivated(active); 537 538 if (IsFocus()) 539 Invalidate(); 540 } 541 542 543 void 544 BMenuField::MouseMoved(BPoint point, uint32 code, const BMessage* message) 545 { 546 BView::MouseMoved(point, code, message); 547 } 548 549 550 void 551 BMenuField::MouseUp(BPoint where) 552 { 553 BView::MouseUp(where); 554 } 555 556 557 void 558 BMenuField::DetachedFromWindow() 559 { 560 BView::DetachedFromWindow(); 561 } 562 563 564 void 565 BMenuField::AllDetached() 566 { 567 BView::AllDetached(); 568 } 569 570 571 void 572 BMenuField::FrameMoved(BPoint newPosition) 573 { 574 BView::FrameMoved(newPosition); 575 } 576 577 578 void 579 BMenuField::FrameResized(float newWidth, float newHeight) 580 { 581 BView::FrameResized(newWidth, newHeight); 582 583 if (fFixedSizeMB) { 584 // we have let the menubar resize itself, but 585 // in fixed size mode, the menubar is supposed to 586 // be at the right end of the view always. Since 587 // the menu bar is in follow left/right mode then, 588 // resizing ourselfs might have caused the menubar 589 // to be outside now 590 fMenuBar->ResizeTo(_MenuBarWidth(), fMenuBar->Frame().Height()); 591 } 592 593 if (newHeight != fLayoutData->previous_height && Label()) { 594 // The height changed, which means the label has to move and we 595 // probably also invalidate a part of the borders around the menu bar. 596 // So don't be shy and invalidate the whole thing. 597 Invalidate(); 598 } 599 600 fLayoutData->previous_height = newHeight; 601 } 602 603 604 BMenu* 605 BMenuField::Menu() const 606 { 607 return fMenu; 608 } 609 610 611 BMenuBar* 612 BMenuField::MenuBar() const 613 { 614 return fMenuBar; 615 } 616 617 618 BMenuItem* 619 BMenuField::MenuItem() const 620 { 621 return fMenuBar->ItemAt(0); 622 } 623 624 625 void 626 BMenuField::SetLabel(const char* label) 627 { 628 if (fLabel) { 629 if (label && strcmp(fLabel, label) == 0) 630 return; 631 632 free(fLabel); 633 } 634 635 fLabel = strdup(label); 636 637 if (Window()) 638 Invalidate(); 639 640 InvalidateLayout(); 641 } 642 643 644 const char* 645 BMenuField::Label() const 646 { 647 return fLabel; 648 } 649 650 651 void 652 BMenuField::SetEnabled(bool on) 653 { 654 if (fEnabled == on) 655 return; 656 657 fEnabled = on; 658 fMenuBar->SetEnabled(on); 659 660 if (Window()) { 661 fMenuBar->Invalidate(fMenuBar->Bounds()); 662 Invalidate(Bounds()); 663 } 664 } 665 666 667 bool 668 BMenuField::IsEnabled() const 669 { 670 return fEnabled; 671 } 672 673 674 void 675 BMenuField::SetAlignment(alignment label) 676 { 677 fAlign = label; 678 } 679 680 681 alignment 682 BMenuField::Alignment() const 683 { 684 return fAlign; 685 } 686 687 688 void 689 BMenuField::SetDivider(float position) 690 { 691 position = roundf(position); 692 693 float delta = fDivider - position; 694 if (delta == 0.0f) 695 return; 696 697 fDivider = position; 698 699 if ((Flags() & B_SUPPORTS_LAYOUT) != 0) { 700 // We should never get here, since layout support means, we also 701 // layout the divider, and don't use this method at all. 702 Relayout(); 703 } else { 704 BRect dirty(fMenuBar->Frame()); 705 706 fMenuBar->MoveTo(_MenuBarOffset(), kVMargin); 707 708 if (fFixedSizeMB) 709 fMenuBar->ResizeTo(_MenuBarWidth(), dirty.Height()); 710 711 dirty = dirty | fMenuBar->Frame(); 712 dirty.InsetBy(-kVMargin, -kVMargin); 713 714 Invalidate(dirty); 715 } 716 } 717 718 719 float 720 BMenuField::Divider() const 721 { 722 return fDivider; 723 } 724 725 726 void 727 BMenuField::ShowPopUpMarker() 728 { 729 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) { 730 menuBar->TogglePopUpMarker(true); 731 menuBar->Invalidate(); 732 } 733 } 734 735 736 void 737 BMenuField::HidePopUpMarker() 738 { 739 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) { 740 menuBar->TogglePopUpMarker(false); 741 menuBar->Invalidate(); 742 } 743 } 744 745 746 BHandler* 747 BMenuField::ResolveSpecifier(BMessage* message, int32 index, 748 BMessage* specifier, int32 form, const char* property) 749 { 750 return BView::ResolveSpecifier(message, index, specifier, form, property); 751 } 752 753 754 status_t 755 BMenuField::GetSupportedSuites(BMessage* data) 756 { 757 return BView::GetSupportedSuites(data); 758 } 759 760 761 void 762 BMenuField::ResizeToPreferred() 763 { 764 CALLED(); 765 766 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n", 767 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 768 769 fMenuBar->ResizeToPreferred(); 770 771 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n", 772 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 773 774 BView::ResizeToPreferred(); 775 776 Invalidate(); 777 } 778 779 780 void 781 BMenuField::GetPreferredSize(float* _width, float* _height) 782 { 783 CALLED(); 784 785 _ValidateLayoutData(); 786 787 if (_width) 788 *_width = fLayoutData->min.width; 789 790 if (_height) 791 *_height = fLayoutData->min.height; 792 } 793 794 795 BSize 796 BMenuField::MinSize() 797 { 798 CALLED(); 799 800 _ValidateLayoutData(); 801 return BLayoutUtils::ComposeSize(ExplicitMinSize(), fLayoutData->min); 802 } 803 804 805 BSize 806 BMenuField::MaxSize() 807 { 808 CALLED(); 809 810 _ValidateLayoutData(); 811 812 BSize max = fLayoutData->min; 813 max.width = B_SIZE_UNLIMITED; 814 815 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), max); 816 } 817 818 819 BSize 820 BMenuField::PreferredSize() 821 { 822 CALLED(); 823 824 _ValidateLayoutData(); 825 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), fLayoutData->min); 826 } 827 828 829 BLayoutItem* 830 BMenuField::CreateLabelLayoutItem() 831 { 832 if (fLayoutData->label_layout_item == NULL) 833 fLayoutData->label_layout_item = new LabelLayoutItem(this); 834 835 return fLayoutData->label_layout_item; 836 } 837 838 839 BLayoutItem* 840 BMenuField::CreateMenuBarLayoutItem() 841 { 842 if (fLayoutData->menu_bar_layout_item == NULL) { 843 // align the menu bar in the full available space 844 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, 845 B_ALIGN_VERTICAL_UNSET)); 846 fLayoutData->menu_bar_layout_item = new MenuBarLayoutItem(this); 847 } 848 849 return fLayoutData->menu_bar_layout_item; 850 } 851 852 853 status_t 854 BMenuField::Perform(perform_code code, void* _data) 855 { 856 switch (code) { 857 case PERFORM_CODE_MIN_SIZE: 858 ((perform_data_min_size*)_data)->return_value 859 = BMenuField::MinSize(); 860 return B_OK; 861 862 case PERFORM_CODE_MAX_SIZE: 863 ((perform_data_max_size*)_data)->return_value 864 = BMenuField::MaxSize(); 865 return B_OK; 866 867 case PERFORM_CODE_PREFERRED_SIZE: 868 ((perform_data_preferred_size*)_data)->return_value 869 = BMenuField::PreferredSize(); 870 return B_OK; 871 872 case PERFORM_CODE_LAYOUT_ALIGNMENT: 873 ((perform_data_layout_alignment*)_data)->return_value 874 = BMenuField::LayoutAlignment(); 875 return B_OK; 876 877 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH: 878 ((perform_data_has_height_for_width*)_data)->return_value 879 = BMenuField::HasHeightForWidth(); 880 return B_OK; 881 882 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH: 883 { 884 perform_data_get_height_for_width* data 885 = (perform_data_get_height_for_width*)_data; 886 BMenuField::GetHeightForWidth(data->width, &data->min, &data->max, 887 &data->preferred); 888 return B_OK; 889 } 890 891 case PERFORM_CODE_SET_LAYOUT: 892 { 893 perform_data_set_layout* data = (perform_data_set_layout*)_data; 894 BMenuField::SetLayout(data->layout); 895 return B_OK; 896 } 897 898 case PERFORM_CODE_LAYOUT_INVALIDATED: 899 { 900 perform_data_layout_invalidated* data 901 = (perform_data_layout_invalidated*)_data; 902 BMenuField::LayoutInvalidated(data->descendants); 903 return B_OK; 904 } 905 906 case PERFORM_CODE_DO_LAYOUT: 907 { 908 BMenuField::DoLayout(); 909 return B_OK; 910 } 911 912 case PERFORM_CODE_ALL_UNARCHIVED: 913 { 914 perform_data_all_unarchived* data 915 = (perform_data_all_unarchived*)_data; 916 data->return_value = BMenuField::AllUnarchived(data->archive); 917 return B_OK; 918 } 919 920 case PERFORM_CODE_ALL_ARCHIVED: 921 { 922 perform_data_all_archived* data 923 = (perform_data_all_archived*)_data; 924 data->return_value = BMenuField::AllArchived(data->archive); 925 return B_OK; 926 } 927 } 928 929 return BView::Perform(code, _data); 930 } 931 932 933 void 934 BMenuField::LayoutInvalidated(bool descendants) 935 { 936 CALLED(); 937 938 fLayoutData->valid = false; 939 } 940 941 942 void 943 BMenuField::DoLayout() 944 { 945 // Bail out, if we shan't do layout. 946 if ((Flags() & B_SUPPORTS_LAYOUT) == 0) 947 return; 948 949 CALLED(); 950 951 // If the user set a layout, we let the base class version call its 952 // hook. 953 if (GetLayout()) { 954 BView::DoLayout(); 955 return; 956 } 957 958 _ValidateLayoutData(); 959 960 // validate current size 961 BSize size(Bounds().Size()); 962 if (size.width < fLayoutData->min.width) 963 size.width = fLayoutData->min.width; 964 965 if (size.height < fLayoutData->min.height) 966 size.height = fLayoutData->min.height; 967 968 // divider 969 float divider = 0; 970 if (fLayoutData->label_layout_item != NULL 971 && fLayoutData->menu_bar_layout_item != NULL 972 && fLayoutData->label_layout_item->Frame().IsValid() 973 && fLayoutData->menu_bar_layout_item->Frame().IsValid()) { 974 // We have valid layout items, they define the divider location. 975 divider = fLayoutData->menu_bar_layout_item->Frame().left 976 - fLayoutData->label_layout_item->Frame().left; 977 } else if (fLayoutData->label_width > 0) { 978 divider = fLayoutData->label_width 979 + be_control_look->DefaultLabelSpacing(); 980 } 981 982 // menu bar 983 BRect dirty(fMenuBar->Frame()); 984 BRect menuBarFrame(divider + kVMargin, kVMargin, size.width - kVMargin, 985 size.height - kVMargin); 986 987 // place the menu bar and set the divider 988 BLayoutUtils::AlignInFrame(fMenuBar, menuBarFrame); 989 990 fDivider = divider; 991 992 // invalidate dirty region 993 dirty = dirty | fMenuBar->Frame(); 994 dirty.InsetBy(-kVMargin, -kVMargin); 995 996 Invalidate(dirty); 997 } 998 999 1000 void BMenuField::_ReservedMenuField1() {} 1001 void BMenuField::_ReservedMenuField2() {} 1002 void BMenuField::_ReservedMenuField3() {} 1003 1004 1005 void 1006 BMenuField::InitObject(const char* label) 1007 { 1008 CALLED(); 1009 1010 fLabel = NULL; 1011 fMenu = NULL; 1012 fMenuBar = NULL; 1013 fAlign = B_ALIGN_LEFT; 1014 fEnabled = true; 1015 fFixedSizeMB = false; 1016 fMenuTaskID = -1; 1017 fLayoutData = new LayoutData; 1018 fMouseDownFilter = new MouseDownFilter(); 1019 1020 SetLabel(label); 1021 1022 if (label) 1023 fDivider = floorf(Frame().Width() / 2.0f); 1024 else 1025 fDivider = 0; 1026 } 1027 1028 1029 void 1030 BMenuField::InitObject2() 1031 { 1032 CALLED(); 1033 1034 if (!fFixedSizeMB) { 1035 float height; 1036 fMenuBar->GetPreferredSize(NULL, &height); 1037 fMenuBar->ResizeTo(_MenuBarWidth(), height); 1038 } 1039 1040 TRACE("frame(%.1f, %.1f, %.1f, %.1f) (%.2f, %.2f)\n", 1041 fMenuBar->Frame().left, fMenuBar->Frame().top, 1042 fMenuBar->Frame().right, fMenuBar->Frame().bottom, 1043 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 1044 1045 fMenuBar->AddFilter(new _BMCFilter_(this, B_MOUSE_DOWN)); 1046 } 1047 1048 1049 void 1050 BMenuField::_DrawLabel(BRect updateRect) 1051 { 1052 CALLED(); 1053 1054 BRect rect(Bounds()); 1055 rect.right = fDivider; 1056 if (!rect.IsValid() || !rect.Intersects(updateRect)) 1057 return; 1058 1059 _ValidateLayoutData(); 1060 font_height& fh = fLayoutData->font_info; 1061 1062 const char* label = Label(); 1063 if (label == NULL) 1064 return; 1065 1066 // horizontal alignment 1067 float x; 1068 switch (fAlign) { 1069 case B_ALIGN_RIGHT: 1070 x = fDivider - fLayoutData->label_width - 3.0f; 1071 break; 1072 1073 case B_ALIGN_CENTER: 1074 x = fDivider - roundf(fLayoutData->label_width / 2.0f); 1075 break; 1076 1077 default: 1078 x = 0.0; 1079 break; 1080 } 1081 1082 // vertical alignment 1083 float y = rect.top 1084 + roundf((rect.Height() + 1 - fh.ascent - fh.descent) / 2.0f) 1085 + fh.ascent; 1086 1087 const rgb_color lowColor = LowColor(); 1088 1089 MenuPrivate menuPrivate(fMenuBar); 1090 if (menuPrivate.State() != MENU_STATE_CLOSED) 1091 SetLowColor(ui_color(B_MENU_SELECTED_BACKGROUND_COLOR)); 1092 1093 BRect fillRect(rect.InsetByCopy(0, kVMargin)); 1094 fillRect.right -= kVMargin * 2; 1095 FillRect(fillRect, B_SOLID_LOW); 1096 1097 uint32 flags = 0; 1098 if (!IsEnabled()) 1099 flags |= BControlLook::B_DISABLED; 1100 1101 be_control_look->DrawLabel(this, label, LowColor(), flags, BPoint(x, y)); 1102 1103 SetLowColor(lowColor); 1104 } 1105 1106 1107 void 1108 BMenuField::_DrawMenuBar(BRect updateRect) 1109 { 1110 CALLED(); 1111 1112 BRect rect(fMenuBar->Frame().InsetByCopy(-kVMargin, -kVMargin)); 1113 if (!rect.IsValid() || !rect.Intersects(updateRect)) 1114 return; 1115 1116 uint32 flags = 0; 1117 if (!IsEnabled()) 1118 flags |= BControlLook::B_DISABLED; 1119 1120 if (IsFocus() && Window()->IsActive()) 1121 flags |= BControlLook::B_FOCUSED; 1122 1123 be_control_look->DrawMenuFieldFrame(this, rect, updateRect, 1124 fMenuBar->LowColor(), LowColor(), flags); 1125 } 1126 1127 1128 void 1129 BMenuField::InitMenu(BMenu* menu) 1130 { 1131 menu->SetFont(be_plain_font); 1132 1133 int32 index = 0; 1134 BMenu* subMenu; 1135 1136 while ((subMenu = menu->SubmenuAt(index++)) != NULL) 1137 InitMenu(subMenu); 1138 } 1139 1140 1141 /*static*/ int32 1142 BMenuField::_thread_entry(void* arg) 1143 { 1144 return static_cast<BMenuField*>(arg)->_MenuTask(); 1145 } 1146 1147 1148 int32 1149 BMenuField::_MenuTask() 1150 { 1151 if (!LockLooper()) 1152 return 0; 1153 1154 Invalidate(); 1155 UnlockLooper(); 1156 1157 bool tracking; 1158 do { 1159 snooze(20000); 1160 if (!LockLooper()) 1161 return 0; 1162 1163 tracking = fMenuBar->fTracking; 1164 1165 UnlockLooper(); 1166 } while (tracking); 1167 1168 if (LockLooper()) { 1169 Invalidate(); 1170 UnlockLooper(); 1171 } 1172 1173 return 0; 1174 } 1175 1176 1177 void 1178 BMenuField::_UpdateFrame() 1179 { 1180 CALLED(); 1181 1182 if (fLayoutData->label_layout_item == NULL 1183 || fLayoutData->menu_bar_layout_item == NULL) { 1184 return; 1185 } 1186 1187 BRect labelFrame = fLayoutData->label_layout_item->Frame(); 1188 BRect menuFrame = fLayoutData->menu_bar_layout_item->Frame(); 1189 1190 if (!labelFrame.IsValid() || !menuFrame.IsValid()) 1191 return; 1192 1193 // update divider 1194 fDivider = menuFrame.left - labelFrame.left; 1195 1196 // update our frame 1197 MoveTo(labelFrame.left, labelFrame.top); 1198 BSize oldSize = Bounds().Size(); 1199 ResizeTo(menuFrame.left + menuFrame.Width() - labelFrame.left, 1200 menuFrame.top + menuFrame.Height() - labelFrame.top); 1201 BSize newSize = Bounds().Size(); 1202 1203 // If the size changes, ResizeTo() will trigger a relayout, otherwise 1204 // we need to do that explicitly. 1205 if (newSize != oldSize) 1206 Relayout(); 1207 } 1208 1209 1210 void 1211 BMenuField::_InitMenuBar(BMenu* menu, BRect frame, bool fixedSize) 1212 { 1213 CALLED(); 1214 1215 fMenu = menu; 1216 InitMenu(menu); 1217 1218 if ((Flags() & B_SUPPORTS_LAYOUT) != 0) { 1219 fMenuBar = new _BMCMenuBar_(this); 1220 } else { 1221 frame.left = _MenuBarOffset(); 1222 frame.top = kVMargin; 1223 frame.right -= kVMargin; 1224 frame.bottom -= kVMargin; 1225 1226 TRACE("frame(%.1f, %.1f, %.1f, %.1f) (%.2f, %.2f)\n", 1227 frame.left, frame.top, frame.right, frame.bottom, 1228 frame.Width(), frame.Height()); 1229 1230 fMenuBar = new _BMCMenuBar_(frame, fixedSize, this); 1231 } 1232 1233 if (fixedSize) { 1234 // align the menu bar in the full available space 1235 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, 1236 B_ALIGN_VERTICAL_UNSET)); 1237 } else { 1238 // align the menu bar left in the available space 1239 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, 1240 B_ALIGN_VERTICAL_UNSET)); 1241 } 1242 1243 AddChild(fMenuBar); 1244 fMenuBar->AddItem(menu); 1245 fMenuBar->SetFont(be_plain_font); 1246 } 1247 1248 1249 void 1250 BMenuField::_InitMenuBar(const BMessage* archive) 1251 { 1252 bool fixed; 1253 if (archive->FindBool("be:fixeds", &fixed) == B_OK) 1254 fFixedSizeMB = fixed; 1255 1256 fMenuBar = (BMenuBar*)FindView("_mc_mb_"); 1257 if (fMenuBar == NULL) { 1258 _InitMenuBar(new BMenu(""), BRect(0, 0, 100, 15), fFixedSizeMB); 1259 InitObject2(); 1260 } else { 1261 fMenuBar->AddFilter(new _BMCFilter_(this, B_MOUSE_DOWN)); 1262 // this is normally done in InitObject2() 1263 } 1264 1265 fMenu = fMenuBar->SubmenuAt(0); 1266 1267 bool disable; 1268 if (archive->FindBool("_disable", &disable) == B_OK) 1269 SetEnabled(!disable); 1270 1271 bool dmark = false; 1272 archive->FindBool("be:dmark", &dmark); 1273 _BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar); 1274 if (menuBar != NULL) 1275 menuBar->TogglePopUpMarker(dmark); 1276 } 1277 1278 1279 void 1280 BMenuField::_ValidateLayoutData() 1281 { 1282 CALLED(); 1283 1284 if (fLayoutData->valid) 1285 return; 1286 1287 // cache font height 1288 font_height& fh = fLayoutData->font_info; 1289 GetFontHeight(&fh); 1290 1291 const char* label = Label(); 1292 if (label != NULL) { 1293 fLayoutData->label_width = ceilf(StringWidth(label)); 1294 fLayoutData->label_height = ceilf(fh.ascent) + ceilf(fh.descent); 1295 } else { 1296 fLayoutData->label_width = 0; 1297 fLayoutData->label_height = 0; 1298 } 1299 1300 // compute the minimal divider 1301 float divider = 0; 1302 if (fLayoutData->label_width > 0) { 1303 divider = fLayoutData->label_width 1304 + be_control_look->DefaultLabelSpacing(); 1305 } 1306 1307 // If we shan't do real layout, we let the current divider take influence. 1308 if ((Flags() & B_SUPPORTS_LAYOUT) == 0) 1309 divider = std::max(divider, fDivider); 1310 1311 // get the minimal (== preferred) menu bar size 1312 // TODO: BMenu::MinSize() is using the ResizeMode() to decide the 1313 // minimum width. If the mode is B_FOLLOW_LEFT_RIGHT, it will use the 1314 // parent's frame width or window's frame width. So at least the returned 1315 // size is wrong, but apparantly it doesn't have much bad effect. 1316 fLayoutData->menu_bar_min = fMenuBar->MinSize(); 1317 1318 TRACE("menu bar min width: %.2f\n", fLayoutData->menu_bar_min.width); 1319 1320 // compute our minimal (== preferred) size 1321 BSize min(fLayoutData->menu_bar_min); 1322 min.width += 2 * kVMargin; 1323 min.height += 2 * kVMargin; 1324 1325 if (divider > 0) 1326 min.width += divider; 1327 1328 if (fLayoutData->label_height > min.height) 1329 min.height = fLayoutData->label_height; 1330 1331 fLayoutData->min = min; 1332 1333 fLayoutData->valid = true; 1334 ResetLayoutInvalidation(); 1335 1336 TRACE("width: %.2f, height: %.2f\n", min.width, min.height); 1337 } 1338 1339 1340 float 1341 BMenuField::_MenuBarOffset() const 1342 { 1343 return std::max(fDivider + kVMargin, kVMargin); 1344 } 1345 1346 1347 float 1348 BMenuField::_MenuBarWidth() const 1349 { 1350 return Bounds().Width() - (_MenuBarOffset() + kVMargin); 1351 } 1352 1353 1354 void 1355 BMenuField::_DoneTracking(BPoint point) 1356 { 1357 Window()->RemoveCommonFilter(fMouseDownFilter); 1358 } 1359 1360 1361 void 1362 BMenuField::_Track(BPoint point, uint32) 1363 { 1364 } 1365 1366 1367 // #pragma mark - BMenuField::LabelLayoutItem 1368 1369 1370 BMenuField::LabelLayoutItem::LabelLayoutItem(BMenuField* parent) 1371 : 1372 fParent(parent), 1373 fFrame() 1374 { 1375 } 1376 1377 1378 BMenuField::LabelLayoutItem::LabelLayoutItem(BMessage* from) 1379 : 1380 BAbstractLayoutItem(from), 1381 fParent(NULL), 1382 fFrame() 1383 { 1384 from->FindRect(kFrameField, &fFrame); 1385 } 1386 1387 1388 bool 1389 BMenuField::LabelLayoutItem::IsVisible() 1390 { 1391 return !fParent->IsHidden(fParent); 1392 } 1393 1394 1395 void 1396 BMenuField::LabelLayoutItem::SetVisible(bool visible) 1397 { 1398 // not allowed 1399 } 1400 1401 1402 BRect 1403 BMenuField::LabelLayoutItem::Frame() 1404 { 1405 return fFrame; 1406 } 1407 1408 1409 void 1410 BMenuField::LabelLayoutItem::SetFrame(BRect frame) 1411 { 1412 fFrame = frame; 1413 fParent->_UpdateFrame(); 1414 } 1415 1416 1417 void 1418 BMenuField::LabelLayoutItem::SetParent(BMenuField* parent) 1419 { 1420 fParent = parent; 1421 } 1422 1423 1424 BView* 1425 BMenuField::LabelLayoutItem::View() 1426 { 1427 return fParent; 1428 } 1429 1430 1431 BSize 1432 BMenuField::LabelLayoutItem::BaseMinSize() 1433 { 1434 fParent->_ValidateLayoutData(); 1435 1436 if (fParent->Label() == NULL) 1437 return BSize(-1, -1); 1438 1439 return BSize(fParent->fLayoutData->label_width 1440 + be_control_look->DefaultLabelSpacing(), 1441 fParent->fLayoutData->label_height); 1442 } 1443 1444 1445 BSize 1446 BMenuField::LabelLayoutItem::BaseMaxSize() 1447 { 1448 return BaseMinSize(); 1449 } 1450 1451 1452 BSize 1453 BMenuField::LabelLayoutItem::BasePreferredSize() 1454 { 1455 return BaseMinSize(); 1456 } 1457 1458 1459 BAlignment 1460 BMenuField::LabelLayoutItem::BaseAlignment() 1461 { 1462 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT); 1463 } 1464 1465 1466 status_t 1467 BMenuField::LabelLayoutItem::Archive(BMessage* into, bool deep) const 1468 { 1469 BArchiver archiver(into); 1470 status_t err = BAbstractLayoutItem::Archive(into, deep); 1471 1472 if (err == B_OK) 1473 err = into->AddRect(kFrameField, fFrame); 1474 1475 return archiver.Finish(err); 1476 } 1477 1478 1479 BArchivable* 1480 BMenuField::LabelLayoutItem::Instantiate(BMessage* from) 1481 { 1482 if (validate_instantiation(from, "BMenuField::LabelLayoutItem")) 1483 return new LabelLayoutItem(from); 1484 1485 return NULL; 1486 } 1487 1488 1489 // #pragma mark - BMenuField::MenuBarLayoutItem 1490 1491 1492 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMenuField* parent) 1493 : 1494 fParent(parent), 1495 fFrame() 1496 { 1497 // by default the part right of the divider shall have an unlimited maximum 1498 // width 1499 SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 1500 } 1501 1502 1503 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMessage* from) 1504 : 1505 BAbstractLayoutItem(from), 1506 fParent(NULL), 1507 fFrame() 1508 { 1509 from->FindRect(kFrameField, &fFrame); 1510 } 1511 1512 1513 bool 1514 BMenuField::MenuBarLayoutItem::IsVisible() 1515 { 1516 return !fParent->IsHidden(fParent); 1517 } 1518 1519 1520 void 1521 BMenuField::MenuBarLayoutItem::SetVisible(bool visible) 1522 { 1523 // not allowed 1524 } 1525 1526 1527 BRect 1528 BMenuField::MenuBarLayoutItem::Frame() 1529 { 1530 return fFrame; 1531 } 1532 1533 1534 void 1535 BMenuField::MenuBarLayoutItem::SetFrame(BRect frame) 1536 { 1537 fFrame = frame; 1538 fParent->_UpdateFrame(); 1539 } 1540 1541 1542 void 1543 BMenuField::MenuBarLayoutItem::SetParent(BMenuField* parent) 1544 { 1545 fParent = parent; 1546 } 1547 1548 1549 BView* 1550 BMenuField::MenuBarLayoutItem::View() 1551 { 1552 return fParent; 1553 } 1554 1555 1556 BSize 1557 BMenuField::MenuBarLayoutItem::BaseMinSize() 1558 { 1559 fParent->_ValidateLayoutData(); 1560 1561 BSize size = fParent->fLayoutData->menu_bar_min; 1562 size.width += 2 * kVMargin; 1563 size.height += 2 * kVMargin; 1564 1565 return size; 1566 } 1567 1568 1569 BSize 1570 BMenuField::MenuBarLayoutItem::BaseMaxSize() 1571 { 1572 BSize size(BaseMinSize()); 1573 size.width = B_SIZE_UNLIMITED; 1574 1575 return size; 1576 } 1577 1578 1579 BSize 1580 BMenuField::MenuBarLayoutItem::BasePreferredSize() 1581 { 1582 return BaseMinSize(); 1583 } 1584 1585 1586 BAlignment 1587 BMenuField::MenuBarLayoutItem::BaseAlignment() 1588 { 1589 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT); 1590 } 1591 1592 1593 status_t 1594 BMenuField::MenuBarLayoutItem::Archive(BMessage* into, bool deep) const 1595 { 1596 BArchiver archiver(into); 1597 status_t err = BAbstractLayoutItem::Archive(into, deep); 1598 1599 if (err == B_OK) 1600 err = into->AddRect(kFrameField, fFrame); 1601 1602 return archiver.Finish(err); 1603 } 1604 1605 1606 BArchivable* 1607 BMenuField::MenuBarLayoutItem::Instantiate(BMessage* from) 1608 { 1609 if (validate_instantiation(from, "BMenuField::MenuBarLayoutItem")) 1610 return new MenuBarLayoutItem(from); 1611 return NULL; 1612 } 1613 1614 1615 extern "C" void 1616 B_IF_GCC_2(InvalidateLayout__10BMenuFieldb, _ZN10BMenuField16InvalidateLayoutEb)( 1617 BMenuField* field, bool descendants) 1618 { 1619 perform_data_layout_invalidated data; 1620 data.descendants = descendants; 1621 1622 field->Perform(PERFORM_CODE_LAYOUT_INVALIDATED, &data); 1623 } 1624 1625