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