1 /* 2 * Copyright 2001-2011, 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 if (be_control_look != NULL) { 366 frame.InsetBy(-kVMargin, -kVMargin); 367 rgb_color base = fMenuBar->LowColor(); 368 rgb_color background = LowColor(); 369 uint32 flags = 0; 370 if (!fMenuBar->IsEnabled()) 371 flags |= BControlLook::B_DISABLED; 372 if (active) 373 flags |= BControlLook::B_FOCUSED; 374 be_control_look->DrawMenuFieldFrame(this, frame, update, base, 375 background, flags); 376 return; 377 } 378 379 if (frame.InsetByCopy(-kVMargin, -kVMargin).Intersects(update)) { 380 SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_2_TINT)); 381 StrokeLine(BPoint(frame.left - 1.0f, frame.top - 1.0f), 382 BPoint(frame.left - 1.0f, frame.bottom - 1.0f)); 383 StrokeLine(BPoint(frame.left - 1.0f, frame.top - 1.0f), 384 BPoint(frame.right - 1.0f, frame.top - 1.0f)); 385 386 StrokeLine(BPoint(frame.left + 1.0f, frame.bottom + 1.0f), 387 BPoint(frame.right + 1.0f, frame.bottom + 1.0f)); 388 StrokeLine(BPoint(frame.right + 1.0f, frame.top + 1.0f)); 389 390 SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_4_TINT)); 391 StrokeLine(BPoint(frame.left - 1.0f, frame.bottom), 392 BPoint(frame.left - 1.0f, frame.bottom)); 393 StrokeLine(BPoint(frame.right, frame.top - 1.0f), 394 BPoint(frame.right, frame.top - 1.0f)); 395 } 396 397 if (active || fTransition) { 398 SetHighColor(active ? ui_color(B_KEYBOARD_NAVIGATION_COLOR) : 399 ViewColor()); 400 StrokeRect(frame.InsetByCopy(-kVMargin, -kVMargin)); 401 402 fTransition = false; 403 } 404 } 405 406 407 void 408 BMenuField::AttachedToWindow() 409 { 410 CALLED(); 411 412 BView* parent = Parent(); 413 if (parent != NULL) { 414 // inherit the color from parent 415 rgb_color color = parent->ViewColor(); 416 if (color == B_TRANSPARENT_COLOR) 417 color = ui_color(B_PANEL_BACKGROUND_COLOR); 418 419 SetViewColor(color); 420 SetLowColor(color); 421 } 422 } 423 424 425 void 426 BMenuField::AllAttached() 427 { 428 CALLED(); 429 430 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height()); 431 432 ResizeTo(Bounds().Width(), 433 fMenuBar->Bounds().Height() + kVMargin + kVMargin); 434 435 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height()); 436 } 437 438 439 void 440 BMenuField::MouseDown(BPoint where) 441 { 442 if (!fMenuBar->Frame().Contains(where)) 443 return; 444 445 if (!fMenuBar->IsEnabled()) 446 return; 447 448 BRect bounds = fMenuBar->ConvertFromParent(Bounds()); 449 450 fMenuBar->StartMenuBar(-1, false, true, &bounds); 451 452 fMenuTaskID = spawn_thread((thread_func)_thread_entry, 453 "_m_task_", B_NORMAL_PRIORITY, this); 454 if (fMenuTaskID >= 0) 455 resume_thread(fMenuTaskID); 456 } 457 458 459 void 460 BMenuField::KeyDown(const char* bytes, int32 numBytes) 461 { 462 switch (bytes[0]) { 463 case B_SPACE: 464 case B_RIGHT_ARROW: 465 case B_DOWN_ARROW: 466 { 467 if (!IsEnabled()) 468 break; 469 470 BRect bounds = fMenuBar->ConvertFromParent(Bounds()); 471 472 fMenuBar->StartMenuBar(0, true, true, &bounds); 473 474 fSelected = true; 475 fTransition = true; 476 477 bounds = Bounds(); 478 bounds.right = fDivider; 479 480 Invalidate(bounds); 481 } 482 483 default: 484 BView::KeyDown(bytes, numBytes); 485 } 486 } 487 488 489 void 490 BMenuField::MakeFocus(bool state) 491 { 492 if (IsFocus() == state) 493 return; 494 495 BView::MakeFocus(state); 496 497 if (Window()) 498 Invalidate(); // TODO: use fLayoutData->label_width 499 } 500 501 502 void 503 BMenuField::MessageReceived(BMessage* msg) 504 { 505 BView::MessageReceived(msg); 506 } 507 508 509 void 510 BMenuField::WindowActivated(bool state) 511 { 512 BView::WindowActivated(state); 513 514 if (IsFocus()) 515 Invalidate(); 516 } 517 518 519 void 520 BMenuField::MouseUp(BPoint point) 521 { 522 BView::MouseUp(point); 523 } 524 525 526 void 527 BMenuField::MouseMoved(BPoint point, uint32 code, const BMessage* message) 528 { 529 BView::MouseMoved(point, code, message); 530 } 531 532 533 void 534 BMenuField::DetachedFromWindow() 535 { 536 BView::DetachedFromWindow(); 537 } 538 539 540 void 541 BMenuField::AllDetached() 542 { 543 BView::AllDetached(); 544 } 545 546 547 void 548 BMenuField::FrameMoved(BPoint newPosition) 549 { 550 BView::FrameMoved(newPosition); 551 } 552 553 554 void 555 BMenuField::FrameResized(float newWidth, float newHeight) 556 { 557 BView::FrameResized(newWidth, newHeight); 558 559 if (newHeight != fLayoutData->previous_height && Label()) { 560 // The height changed, which means the label has to move and we 561 // probably also invalidate a part of the borders around the menu bar. 562 // So don't be shy and invalidate the whole thing. 563 Invalidate(); 564 } 565 566 fLayoutData->previous_height = newHeight; 567 } 568 569 570 BMenu* 571 BMenuField::Menu() const 572 { 573 return fMenu; 574 } 575 576 577 BMenuBar* 578 BMenuField::MenuBar() const 579 { 580 return fMenuBar; 581 } 582 583 584 BMenuItem* 585 BMenuField::MenuItem() const 586 { 587 return fMenuBar->ItemAt(0); 588 } 589 590 591 void 592 BMenuField::SetLabel(const char* label) 593 { 594 if (fLabel) { 595 if (label && strcmp(fLabel, label) == 0) 596 return; 597 598 free(fLabel); 599 } 600 601 fLabel = strdup(label); 602 603 if (Window()) 604 Invalidate(); 605 606 InvalidateLayout(); 607 } 608 609 610 const char* 611 BMenuField::Label() const 612 { 613 return fLabel; 614 } 615 616 617 void 618 BMenuField::SetEnabled(bool on) 619 { 620 if (fEnabled == on) 621 return; 622 623 fEnabled = on; 624 fMenuBar->SetEnabled(on); 625 626 if (Window()) { 627 fMenuBar->Invalidate(fMenuBar->Bounds()); 628 Invalidate(Bounds()); 629 } 630 } 631 632 633 bool 634 BMenuField::IsEnabled() const 635 { 636 return fEnabled; 637 } 638 639 640 void 641 BMenuField::SetAlignment(alignment label) 642 { 643 fAlign = label; 644 } 645 646 647 alignment 648 BMenuField::Alignment() const 649 { 650 return fAlign; 651 } 652 653 654 void 655 BMenuField::SetDivider(float divider) 656 { 657 divider = floorf(divider + 0.5); 658 659 float dx = fDivider - divider; 660 661 if (dx == 0.0f) 662 return; 663 664 fDivider = divider; 665 666 if (Flags() & B_SUPPORTS_LAYOUT) { 667 // We should never get here, since layout support means, we also 668 // layout the divider, and don't use this method at all. 669 Relayout(); 670 } else { 671 BRect dirty(fMenuBar->Frame()); 672 673 fMenuBar->MoveTo(_MenuBarOffset(), kVMargin); 674 675 if (fFixedSizeMB) 676 fMenuBar->ResizeTo(_MenuBarWidth(), dirty.Height()); 677 678 dirty = dirty | fMenuBar->Frame(); 679 dirty.InsetBy(-kVMargin, -kVMargin); 680 681 Invalidate(dirty); 682 } 683 } 684 685 686 float 687 BMenuField::Divider() const 688 { 689 return fDivider; 690 } 691 692 693 void 694 BMenuField::ShowPopUpMarker() 695 { 696 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) { 697 menuBar->TogglePopUpMarker(true); 698 menuBar->Invalidate(); 699 } 700 } 701 702 703 void 704 BMenuField::HidePopUpMarker() 705 { 706 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) { 707 menuBar->TogglePopUpMarker(false); 708 menuBar->Invalidate(); 709 } 710 } 711 712 713 BHandler* 714 BMenuField::ResolveSpecifier(BMessage* message, int32 index, 715 BMessage* specifier, int32 form, const char* property) 716 { 717 return BView::ResolveSpecifier(message, index, specifier, form, property); 718 } 719 720 721 status_t 722 BMenuField::GetSupportedSuites(BMessage* data) 723 { 724 return BView::GetSupportedSuites(data); 725 } 726 727 728 void 729 BMenuField::ResizeToPreferred() 730 { 731 CALLED(); 732 733 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n", 734 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 735 736 fMenuBar->ResizeToPreferred(); 737 738 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n", 739 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 740 741 BView::ResizeToPreferred(); 742 743 if (fFixedSizeMB) { 744 // we have let the menubar resize itself, but 745 // in fixed size mode, the menubar is supposed to 746 // be at the right end of the view always. Since 747 // the menu bar is in follow left/right mode then, 748 // resizing ourselfs might have caused the menubar 749 // to be outside now 750 fMenuBar->ResizeTo(_MenuBarWidth(), fMenuBar->Frame().Height()); 751 } 752 } 753 754 755 void 756 BMenuField::GetPreferredSize(float* _width, float* _height) 757 { 758 CALLED(); 759 760 _ValidateLayoutData(); 761 762 if (_width) 763 *_width = fLayoutData->min.width; 764 765 if (_height) 766 *_height = fLayoutData->min.height; 767 } 768 769 770 BSize 771 BMenuField::MinSize() 772 { 773 CALLED(); 774 775 _ValidateLayoutData(); 776 return BLayoutUtils::ComposeSize(ExplicitMinSize(), fLayoutData->min); 777 } 778 779 780 BSize 781 BMenuField::MaxSize() 782 { 783 CALLED(); 784 785 _ValidateLayoutData(); 786 787 BSize max = fLayoutData->min; 788 max.width = B_SIZE_UNLIMITED; 789 790 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), max); 791 } 792 793 794 BSize 795 BMenuField::PreferredSize() 796 { 797 CALLED(); 798 799 _ValidateLayoutData(); 800 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), fLayoutData->min); 801 } 802 803 804 void 805 BMenuField::InvalidateLayout(bool descendants) 806 { 807 CALLED(); 808 809 fLayoutData->valid = false; 810 811 BView::InvalidateLayout(descendants); 812 } 813 814 815 BLayoutItem* 816 BMenuField::CreateLabelLayoutItem() 817 { 818 if (!fLayoutData->label_layout_item) 819 fLayoutData->label_layout_item = new LabelLayoutItem(this); 820 return fLayoutData->label_layout_item; 821 } 822 823 824 BLayoutItem* 825 BMenuField::CreateMenuBarLayoutItem() 826 { 827 if (!fLayoutData->menu_bar_layout_item) { 828 // align the menu bar in the full available space 829 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, 830 B_ALIGN_VERTICAL_UNSET)); 831 fLayoutData->menu_bar_layout_item = new MenuBarLayoutItem(this); 832 } 833 return fLayoutData->menu_bar_layout_item; 834 } 835 836 837 status_t 838 BMenuField::Perform(perform_code code, void* _data) 839 { 840 switch (code) { 841 case PERFORM_CODE_MIN_SIZE: 842 ((perform_data_min_size*)_data)->return_value 843 = BMenuField::MinSize(); 844 return B_OK; 845 case PERFORM_CODE_MAX_SIZE: 846 ((perform_data_max_size*)_data)->return_value 847 = BMenuField::MaxSize(); 848 return B_OK; 849 case PERFORM_CODE_PREFERRED_SIZE: 850 ((perform_data_preferred_size*)_data)->return_value 851 = BMenuField::PreferredSize(); 852 return B_OK; 853 case PERFORM_CODE_LAYOUT_ALIGNMENT: 854 ((perform_data_layout_alignment*)_data)->return_value 855 = BMenuField::LayoutAlignment(); 856 return B_OK; 857 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH: 858 ((perform_data_has_height_for_width*)_data)->return_value 859 = BMenuField::HasHeightForWidth(); 860 return B_OK; 861 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH: 862 { 863 perform_data_get_height_for_width* data 864 = (perform_data_get_height_for_width*)_data; 865 BMenuField::GetHeightForWidth(data->width, &data->min, &data->max, 866 &data->preferred); 867 return B_OK; 868 } 869 case PERFORM_CODE_SET_LAYOUT: 870 { 871 perform_data_set_layout* data = (perform_data_set_layout*)_data; 872 BMenuField::SetLayout(data->layout); 873 return B_OK; 874 } 875 case PERFORM_CODE_INVALIDATE_LAYOUT: 876 { 877 perform_data_invalidate_layout* data 878 = (perform_data_invalidate_layout*)_data; 879 BMenuField::InvalidateLayout(data->descendants); 880 return B_OK; 881 } 882 case PERFORM_CODE_DO_LAYOUT: 883 { 884 BMenuField::DoLayout(); 885 return B_OK; 886 } 887 case PERFORM_CODE_ALL_UNARCHIVED: 888 { 889 perform_data_all_unarchived* data 890 = (perform_data_all_unarchived*)_data; 891 892 data->return_value = BMenuField::AllUnarchived(data->archive); 893 return B_OK; 894 } 895 case PERFORM_CODE_ALL_ARCHIVED: 896 { 897 perform_data_all_archived* data 898 = (perform_data_all_archived*)_data; 899 900 data->return_value = BMenuField::AllArchived(data->archive); 901 return B_OK; 902 } 903 } 904 905 return BView::Perform(code, _data); 906 } 907 908 909 void 910 BMenuField::DoLayout() 911 { 912 // Bail out, if we shan't do layout. 913 if (!(Flags() & B_SUPPORTS_LAYOUT)) 914 return; 915 916 CALLED(); 917 918 // If the user set a layout, we let the base class version call its 919 // hook. 920 if (GetLayout()) { 921 BView::DoLayout(); 922 return; 923 } 924 925 _ValidateLayoutData(); 926 927 // validate current size 928 BSize size(Bounds().Size()); 929 if (size.width < fLayoutData->min.width) 930 size.width = fLayoutData->min.width; 931 if (size.height < fLayoutData->min.height) 932 size.height = fLayoutData->min.height; 933 934 // divider 935 float divider = 0; 936 if (fLayoutData->label_layout_item && fLayoutData->menu_bar_layout_item) { 937 // We have layout items. They define the divider location. 938 divider = fLayoutData->menu_bar_layout_item->Frame().left 939 - fLayoutData->label_layout_item->Frame().left; 940 } else { 941 if (fLayoutData->label_width > 0) 942 divider = fLayoutData->label_width + 5; 943 } 944 945 // menu bar 946 BRect dirty(fMenuBar->Frame()); 947 BRect menuBarFrame(divider + kVMargin, kVMargin, size.width - kVMargin, 948 size.height - kVMargin); 949 950 // place the menu bar and set the divider 951 BLayoutUtils::AlignInFrame(fMenuBar, menuBarFrame); 952 953 fDivider = divider; 954 955 // invalidate dirty region 956 dirty = dirty | fMenuBar->Frame(); 957 dirty.InsetBy(-kVMargin, -kVMargin); 958 959 Invalidate(dirty); 960 } 961 962 963 void BMenuField::_ReservedMenuField1() {} 964 void BMenuField::_ReservedMenuField2() {} 965 void BMenuField::_ReservedMenuField3() {} 966 967 968 void 969 BMenuField::InitObject(const char* label) 970 { 971 CALLED(); 972 973 fLabel = NULL; 974 fMenu = NULL; 975 fMenuBar = NULL; 976 fAlign = B_ALIGN_LEFT; 977 fEnabled = true; 978 fSelected = false; 979 fTransition = false; 980 fFixedSizeMB = false; 981 fMenuTaskID = -1; 982 fLayoutData = new LayoutData; 983 984 SetLabel(label); 985 986 if (label) 987 fDivider = (float)floor(Frame().Width() / 2.0f); 988 else 989 fDivider = 0; 990 } 991 992 993 void 994 BMenuField::InitObject2() 995 { 996 CALLED(); 997 998 float height; 999 fMenuBar->GetPreferredSize(NULL, &height); 1000 fMenuBar->ResizeTo(_MenuBarWidth(), height); 1001 1002 TRACE("frame(%.1f, %.1f, %.1f, %.1f) (%.2f, %.2f)\n", 1003 fMenuBar->Frame().left, fMenuBar->Frame().top, 1004 fMenuBar->Frame().right, fMenuBar->Frame().bottom, 1005 fMenuBar->Frame().Width(), fMenuBar->Frame().Height()); 1006 1007 fMenuBar->AddFilter(new _BMCFilter_(this, B_MOUSE_DOWN)); 1008 } 1009 1010 1011 void 1012 BMenuField::DrawLabel(BRect bounds, BRect update) 1013 { 1014 CALLED(); 1015 1016 _ValidateLayoutData(); 1017 font_height& fh = fLayoutData->font_info; 1018 1019 if (Label()) { 1020 SetLowColor(ViewColor()); 1021 1022 // horizontal alignment 1023 float x; 1024 switch (fAlign) { 1025 case B_ALIGN_RIGHT: 1026 x = fDivider - fLayoutData->label_width - 3.0; 1027 break; 1028 1029 case B_ALIGN_CENTER: 1030 x = fDivider - fLayoutData->label_width / 2.0; 1031 break; 1032 1033 default: 1034 x = 0.0; 1035 break; 1036 } 1037 1038 // vertical alignment 1039 float y = Bounds().top 1040 + (Bounds().Height() + 1 - fh.ascent - fh.descent) / 2 1041 + fh.ascent; 1042 y = floor(y + 0.5); 1043 1044 SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), 1045 IsEnabled() ? B_DARKEN_MAX_TINT : B_DISABLED_LABEL_TINT)); 1046 DrawString(Label(), BPoint(x, y)); 1047 } 1048 } 1049 1050 1051 void 1052 BMenuField::InitMenu(BMenu* menu) 1053 { 1054 menu->SetFont(be_plain_font); 1055 1056 int32 index = 0; 1057 BMenu* subMenu; 1058 1059 while ((subMenu = menu->SubmenuAt(index++)) != NULL) 1060 InitMenu(subMenu); 1061 } 1062 1063 1064 /*static*/ int32 1065 BMenuField::_thread_entry(void* arg) 1066 { 1067 return static_cast<BMenuField*>(arg)->_MenuTask(); 1068 } 1069 1070 1071 int32 1072 BMenuField::_MenuTask() 1073 { 1074 if (!LockLooper()) 1075 return 0; 1076 1077 fSelected = true; 1078 fTransition = true; 1079 Invalidate(); 1080 UnlockLooper(); 1081 1082 bool tracking; 1083 do { 1084 snooze(20000); 1085 if (!LockLooper()) 1086 return 0; 1087 1088 tracking = fMenuBar->fTracking; 1089 1090 UnlockLooper(); 1091 } while (tracking); 1092 1093 if (LockLooper()) { 1094 fSelected = false; 1095 fTransition = true; 1096 Invalidate(); 1097 UnlockLooper(); 1098 } 1099 1100 return 0; 1101 } 1102 1103 1104 void 1105 BMenuField::_UpdateFrame() 1106 { 1107 CALLED(); 1108 1109 if (fLayoutData->label_layout_item && fLayoutData->menu_bar_layout_item) { 1110 BRect labelFrame = fLayoutData->label_layout_item->Frame(); 1111 BRect menuFrame = fLayoutData->menu_bar_layout_item->Frame(); 1112 1113 // update divider 1114 fDivider = menuFrame.left - labelFrame.left; 1115 1116 // update our frame 1117 MoveTo(labelFrame.left, labelFrame.top); 1118 BSize oldSize = Bounds().Size(); 1119 ResizeTo(menuFrame.left + menuFrame.Width() - labelFrame.left, 1120 menuFrame.top + menuFrame.Height() - labelFrame.top); 1121 BSize newSize = Bounds().Size(); 1122 1123 // If the size changes, ResizeTo() will trigger a relayout, otherwise 1124 // we need to do that explicitly. 1125 if (newSize != oldSize) 1126 Relayout(); 1127 } 1128 } 1129 1130 1131 void 1132 BMenuField::_InitMenuBar(BMenu* menu, BRect frame, bool fixedSize) 1133 { 1134 CALLED(); 1135 1136 fMenu = menu; 1137 InitMenu(menu); 1138 1139 if ((Flags() & B_SUPPORTS_LAYOUT)) { 1140 fMenuBar = new _BMCMenuBar_(fixedSize, this); 1141 } else { 1142 frame.left = _MenuBarOffset(); 1143 frame.top = kVMargin; 1144 frame.right -= kVMargin; 1145 frame.bottom -= kVMargin; 1146 1147 TRACE("frame(%.1f, %.1f, %.1f, %.1f) (%.2f, %.2f)\n", 1148 frame.left, frame.top, frame.right, frame.bottom, 1149 frame.Width(), frame.Height()); 1150 1151 fMenuBar = new _BMCMenuBar_(frame, fixedSize, this); 1152 } 1153 1154 if (fixedSize) { 1155 // align the menu bar in the full available space 1156 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, 1157 B_ALIGN_VERTICAL_UNSET)); 1158 } else { 1159 // align the menu bar left in the available space 1160 fMenuBar->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, 1161 B_ALIGN_VERTICAL_UNSET)); 1162 } 1163 1164 AddChild(fMenuBar); 1165 fMenuBar->AddItem(menu); 1166 1167 fMenuBar->SetFont(be_plain_font); 1168 } 1169 1170 1171 void 1172 BMenuField::_InitMenuBar(const BMessage* archive) 1173 { 1174 bool fixed; 1175 if (archive->FindBool("be:fixeds", &fixed) == B_OK) 1176 fFixedSizeMB = fixed; 1177 1178 fMenuBar = (BMenuBar*)FindView("_mc_mb_"); 1179 if (!fMenuBar) { 1180 _InitMenuBar(new BMenu(""), BRect(0, 0, 100, 15), fFixedSizeMB); 1181 InitObject2(); 1182 } else { 1183 fMenuBar->AddFilter(new _BMCFilter_(this, B_MOUSE_DOWN)); 1184 // this is normally done in InitObject2() 1185 } 1186 1187 fMenu = fMenuBar->SubmenuAt(0); 1188 1189 bool disable; 1190 if (archive->FindBool("_disable", &disable) == B_OK) 1191 SetEnabled(!disable); 1192 1193 bool dmark = false; 1194 archive->FindBool("be:dmark", &dmark); 1195 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) 1196 menuBar->TogglePopUpMarker(dmark); 1197 } 1198 1199 1200 void 1201 BMenuField::_ValidateLayoutData() 1202 { 1203 CALLED(); 1204 1205 if (fLayoutData->valid) 1206 return; 1207 1208 // cache font height 1209 font_height& fh = fLayoutData->font_info; 1210 GetFontHeight(&fh); 1211 1212 if (Label() != NULL) { 1213 fLayoutData->label_width = ceilf(StringWidth(Label())); 1214 fLayoutData->label_height = ceilf(fh.ascent) + ceilf(fh.descent); 1215 } else { 1216 fLayoutData->label_width = 0; 1217 fLayoutData->label_height = 0; 1218 } 1219 1220 // compute the minimal divider 1221 float divider = 0; 1222 if (fLayoutData->label_width > 0) 1223 divider = fLayoutData->label_width + 5; 1224 1225 // If we shan't do real layout, we let the current divider take influence. 1226 if (!(Flags() & B_SUPPORTS_LAYOUT)) 1227 divider = max_c(divider, fDivider); 1228 1229 // get the minimal (== preferred) menu bar size 1230 // TODO: BMenu::MinSize() is using the ResizeMode() to decide the 1231 // minimum width. If the mode is B_FOLLOW_LEFT_RIGHT, it will use the 1232 // parent's frame width or window's frame width. So at least the returned 1233 // size is wrong, but apparantly it doesn't have much bad effect. 1234 fLayoutData->menu_bar_min = fMenuBar->MinSize(); 1235 1236 TRACE("menu bar min width: %.2f\n", fLayoutData->menu_bar_min.width); 1237 1238 // compute our minimal (== preferred) size 1239 BSize min(fLayoutData->menu_bar_min); 1240 min.width += 2 * kVMargin; 1241 min.height += 2 * kVMargin; 1242 1243 if (divider > 0) 1244 min.width += divider; 1245 if (fLayoutData->label_height > min.height) 1246 min.height = fLayoutData->label_height; 1247 1248 fLayoutData->min = min; 1249 1250 fLayoutData->valid = true; 1251 ResetLayoutInvalidation(); 1252 1253 TRACE("width: %.2f, height: %.2f\n", min.width, min.height); 1254 } 1255 1256 1257 float 1258 BMenuField::_MenuBarOffset() const 1259 { 1260 return max_c(kVMargin, fDivider + kVMargin); 1261 } 1262 1263 1264 float 1265 BMenuField::_MenuBarWidth() const 1266 { 1267 return Bounds().Width() - (_MenuBarOffset() + kVMargin); 1268 } 1269 1270 1271 // #pragma mark - 1272 1273 1274 BMenuField::LabelLayoutItem::LabelLayoutItem(BMenuField* parent) 1275 : 1276 fParent(parent), 1277 fFrame() 1278 { 1279 } 1280 1281 1282 BMenuField::LabelLayoutItem::LabelLayoutItem(BMessage* from) 1283 : 1284 BAbstractLayoutItem(from), 1285 fParent(NULL), 1286 fFrame() 1287 { 1288 from->FindRect(kFrameField, &fFrame); 1289 } 1290 1291 1292 bool 1293 BMenuField::LabelLayoutItem::IsVisible() 1294 { 1295 return !fParent->IsHidden(fParent); 1296 } 1297 1298 1299 void 1300 BMenuField::LabelLayoutItem::SetVisible(bool visible) 1301 { 1302 // not allowed 1303 } 1304 1305 1306 BRect 1307 BMenuField::LabelLayoutItem::Frame() 1308 { 1309 return fFrame; 1310 } 1311 1312 1313 void 1314 BMenuField::LabelLayoutItem::SetFrame(BRect frame) 1315 { 1316 fFrame = frame; 1317 fParent->_UpdateFrame(); 1318 } 1319 1320 1321 void 1322 BMenuField::LabelLayoutItem::SetParent(BMenuField* parent) 1323 { 1324 fParent = parent; 1325 } 1326 1327 1328 BView* 1329 BMenuField::LabelLayoutItem::View() 1330 { 1331 return fParent; 1332 } 1333 1334 1335 BSize 1336 BMenuField::LabelLayoutItem::BaseMinSize() 1337 { 1338 fParent->_ValidateLayoutData(); 1339 1340 if (!fParent->Label()) 1341 return BSize(-1, -1); 1342 1343 return BSize(fParent->fLayoutData->label_width + 5, 1344 fParent->fLayoutData->label_height); 1345 } 1346 1347 1348 BSize 1349 BMenuField::LabelLayoutItem::BaseMaxSize() 1350 { 1351 return BaseMinSize(); 1352 } 1353 1354 1355 BSize 1356 BMenuField::LabelLayoutItem::BasePreferredSize() 1357 { 1358 return BaseMinSize(); 1359 } 1360 1361 1362 BAlignment 1363 BMenuField::LabelLayoutItem::BaseAlignment() 1364 { 1365 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT); 1366 } 1367 1368 1369 status_t 1370 BMenuField::LabelLayoutItem::Archive(BMessage* into, bool deep) const 1371 { 1372 BArchiver archiver(into); 1373 status_t err = BAbstractLayoutItem::Archive(into, deep); 1374 1375 if (err == B_OK) 1376 err = into->AddRect(kFrameField, fFrame); 1377 1378 return archiver.Finish(err); 1379 } 1380 1381 1382 BArchivable* 1383 BMenuField::LabelLayoutItem::Instantiate(BMessage* from) 1384 { 1385 if (validate_instantiation(from, "BMenuField::LabelLayoutItem")) 1386 return new LabelLayoutItem(from); 1387 return NULL; 1388 } 1389 1390 1391 // #pragma mark - 1392 1393 1394 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMenuField* parent) 1395 : 1396 fParent(parent), 1397 fFrame() 1398 { 1399 // by default the part right of the divider shall have an unlimited maximum 1400 // width 1401 SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 1402 } 1403 1404 1405 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMessage* from) 1406 : 1407 BAbstractLayoutItem(from), 1408 fParent(NULL), 1409 fFrame() 1410 { 1411 from->FindRect(kFrameField, &fFrame); 1412 } 1413 1414 1415 bool 1416 BMenuField::MenuBarLayoutItem::IsVisible() 1417 { 1418 return !fParent->IsHidden(fParent); 1419 } 1420 1421 1422 void 1423 BMenuField::MenuBarLayoutItem::SetVisible(bool visible) 1424 { 1425 // not allowed 1426 } 1427 1428 1429 BRect 1430 BMenuField::MenuBarLayoutItem::Frame() 1431 { 1432 return fFrame; 1433 } 1434 1435 1436 void 1437 BMenuField::MenuBarLayoutItem::SetFrame(BRect frame) 1438 { 1439 fFrame = frame; 1440 fParent->_UpdateFrame(); 1441 } 1442 1443 1444 void 1445 BMenuField::MenuBarLayoutItem::SetParent(BMenuField* parent) 1446 { 1447 fParent = parent; 1448 } 1449 1450 1451 BView* 1452 BMenuField::MenuBarLayoutItem::View() 1453 { 1454 return fParent; 1455 } 1456 1457 1458 BSize 1459 BMenuField::MenuBarLayoutItem::BaseMinSize() 1460 { 1461 fParent->_ValidateLayoutData(); 1462 1463 BSize size = fParent->fLayoutData->menu_bar_min; 1464 size.width += 2 * kVMargin; 1465 size.height += 2 * kVMargin; 1466 1467 return size; 1468 } 1469 1470 1471 BSize 1472 BMenuField::MenuBarLayoutItem::BaseMaxSize() 1473 { 1474 BSize size(BaseMinSize()); 1475 size.width = B_SIZE_UNLIMITED; 1476 return size; 1477 } 1478 1479 1480 BSize 1481 BMenuField::MenuBarLayoutItem::BasePreferredSize() 1482 { 1483 return BaseMinSize(); 1484 } 1485 1486 1487 BAlignment 1488 BMenuField::MenuBarLayoutItem::BaseAlignment() 1489 { 1490 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT); 1491 } 1492 1493 1494 status_t 1495 BMenuField::MenuBarLayoutItem::Archive(BMessage* into, bool deep) const 1496 { 1497 BArchiver archiver(into); 1498 status_t err = BAbstractLayoutItem::Archive(into, deep); 1499 1500 if (err == B_OK) 1501 err = into->AddRect(kFrameField, fFrame); 1502 1503 return archiver.Finish(err); 1504 } 1505 1506 1507 BArchivable* 1508 BMenuField::MenuBarLayoutItem::Instantiate(BMessage* from) 1509 { 1510 if (validate_instantiation(from, "BMenuField::MenuBarLayoutItem")) 1511 return new MenuBarLayoutItem(from); 1512 return NULL; 1513 } 1514 1515