tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / interface / MenuField.cpp
blob8f3e4ed0e0296b0727e401697bba87288863a9e5
1 /*
2 * Copyright 2001-2013, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
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
13 #include <MenuField.h>
15 #include <algorithm>
16 #include <stdio.h>
17 // for printf in TRACE
18 #include <stdlib.h>
19 #include <string.h>
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>
33 #include <binary_compatibility/Interface.h>
34 #include <binary_compatibility/Support.h>
37 #ifdef CALLED
38 # undef CALLED
39 #endif
40 #ifdef TRACE
41 # undef TRACE
42 #endif
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
59 static const float kMinMenuBarWidth = 20.0f;
60 // found by experimenting on BeOS R5
63 namespace {
64 const char* const kFrameField = "BMenuField:layoutItem:frame";
65 const char* const kMenuBarItemField = "BMenuField:barItem";
66 const char* const kLabelItemField = "BMenuField:labelItem";
70 // #pragma mark - LabelLayoutItem
73 class BMenuField::LabelLayoutItem : public BAbstractLayoutItem {
74 public:
75 LabelLayoutItem(BMenuField* parent);
76 LabelLayoutItem(BMessage* archive);
78 virtual bool IsVisible();
79 virtual void SetVisible(bool visible);
81 virtual BRect Frame();
82 virtual void SetFrame(BRect frame);
84 void SetParent(BMenuField* parent);
85 virtual BView* View();
87 virtual BSize BaseMinSize();
88 virtual BSize BaseMaxSize();
89 virtual BSize BasePreferredSize();
90 virtual BAlignment BaseAlignment();
92 virtual status_t Archive(BMessage* into, bool deep = true) const;
93 static BArchivable* Instantiate(BMessage* from);
95 private:
96 BMenuField* fParent;
97 BRect fFrame;
101 // #pragma mark - MenuBarLayoutItem
104 class BMenuField::MenuBarLayoutItem : public BAbstractLayoutItem {
105 public:
106 MenuBarLayoutItem(BMenuField* parent);
107 MenuBarLayoutItem(BMessage* from);
109 virtual bool IsVisible();
110 virtual void SetVisible(bool visible);
112 virtual BRect Frame();
113 virtual void SetFrame(BRect frame);
115 void SetParent(BMenuField* parent);
116 virtual BView* View();
118 virtual BSize BaseMinSize();
119 virtual BSize BaseMaxSize();
120 virtual BSize BasePreferredSize();
121 virtual BAlignment BaseAlignment();
123 virtual status_t Archive(BMessage* into, bool deep = true) const;
124 static BArchivable* Instantiate(BMessage* from);
126 private:
127 BMenuField* fParent;
128 BRect fFrame;
132 // #pragma mark - LayoutData
135 struct BMenuField::LayoutData {
136 LayoutData()
138 label_layout_item(NULL),
139 menu_bar_layout_item(NULL),
140 previous_height(-1),
141 valid(false)
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;
158 // #pragma mark - MouseDownFilter
161 class MouseDownFilter : public BMessageFilter
163 public:
164 MouseDownFilter();
165 virtual ~MouseDownFilter();
167 virtual filter_result Filter(BMessage* message, BHandler** target);
171 MouseDownFilter::MouseDownFilter()
173 BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE)
178 MouseDownFilter::~MouseDownFilter()
183 filter_result
184 MouseDownFilter::Filter(BMessage* message, BHandler** target)
186 return message->what == B_MOUSE_DOWN ? B_SKIP_MESSAGE : B_DISPATCH_MESSAGE;
190 // #pragma mark - BMenuField
193 using BPrivate::MenuPrivate;
195 BMenuField::BMenuField(BRect frame, const char* name, const char* label,
196 BMenu* menu, uint32 resizingMode, uint32 flags)
198 BView(frame, name, resizingMode, flags)
200 CALLED();
202 TRACE("frame.width: %.2f, height: %.2f\n", frame.Width(), frame.Height());
204 InitObject(label);
206 frame.OffsetTo(B_ORIGIN);
207 _InitMenuBar(menu, frame, false);
209 InitObject2();
213 BMenuField::BMenuField(BRect frame, const char* name, const char* label,
214 BMenu* menu, bool fixedSize, uint32 resizingMode, uint32 flags)
216 BView(frame, name, resizingMode, flags)
218 InitObject(label);
220 fFixedSizeMB = fixedSize;
222 frame.OffsetTo(B_ORIGIN);
223 _InitMenuBar(menu, frame, fixedSize);
225 InitObject2();
229 BMenuField::BMenuField(const char* name, const char* label, BMenu* menu,
230 uint32 flags)
232 BView(name, flags | B_FRAME_EVENTS)
234 InitObject(label);
236 _InitMenuBar(menu, BRect(0, 0, 100, 15), true);
238 InitObject2();
242 BMenuField::BMenuField(const char* label, BMenu* menu, uint32 flags)
244 BView(NULL, flags | B_FRAME_EVENTS)
246 InitObject(label);
248 _InitMenuBar(menu, BRect(0, 0, 100, 15), true);
250 InitObject2();
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)
258 BView(name, flags | B_FRAME_EVENTS)
260 InitObject(label);
262 _InitMenuBar(menu, BRect(0, 0, 100, 15), true);
264 InitObject2();
268 //! Copy&Paste error, should be removed at some point (already private)
269 BMenuField::BMenuField(const char* label, BMenu* menu, BMessage* message)
271 BView(NULL, B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS)
273 InitObject(label);
275 _InitMenuBar(menu, BRect(0, 0, 100, 15), true);
277 InitObject2();
281 BMenuField::BMenuField(BMessage* data)
283 BView(BUnarchiver::PrepareArchive(data))
285 BUnarchiver unarchiver(data);
286 const char* label = NULL;
287 data->FindString("_label", &label);
289 InitObject(label);
291 data->FindFloat("_divide", &fDivider);
293 int32 align;
294 if (data->FindInt32("_align", &align) == B_OK)
295 SetAlignment((alignment)align);
297 if (!BUnarchiver::IsArchiveManaged(data))
298 _InitMenuBar(data);
299 unarchiver.Finish();
303 BMenuField::~BMenuField()
305 free(fLabel);
307 status_t dummy;
308 if (fMenuTaskID >= 0)
309 wait_for_thread(fMenuTaskID, &dummy);
311 delete fLayoutData;
312 delete fMouseDownFilter;
316 BArchivable*
317 BMenuField::Instantiate(BMessage* data)
319 if (validate_instantiation(data, "BMenuField"))
320 return new BMenuField(data);
322 return NULL;
326 status_t
327 BMenuField::Archive(BMessage* data, bool deep) const
329 BArchiver archiver(data);
330 status_t ret = BView::Archive(data, deep);
332 if (ret == B_OK && Label())
333 ret = data->AddString("_label", Label());
335 if (ret == B_OK && !IsEnabled())
336 ret = data->AddBool("_disable", true);
338 if (ret == B_OK)
339 ret = data->AddInt32("_align", Alignment());
340 if (ret == B_OK)
341 ret = data->AddFloat("_divide", Divider());
343 if (ret == B_OK && fFixedSizeMB)
344 ret = data->AddBool("be:fixeds", true);
346 bool dmark = false;
347 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar))
348 dmark = menuBar->IsPopUpMarkerShown();
350 data->AddBool("be:dmark", dmark);
352 return archiver.Finish(ret);
356 status_t
357 BMenuField::AllArchived(BMessage* into) const
359 status_t err;
360 if ((err = BView::AllArchived(into)) != B_OK)
361 return err;
363 BArchiver archiver(into);
365 BArchivable* menuBarItem = fLayoutData->menu_bar_layout_item;
366 if (archiver.IsArchived(menuBarItem))
367 err = archiver.AddArchivable(kMenuBarItemField, menuBarItem);
369 if (err != B_OK)
370 return err;
372 BArchivable* labelBarItem = fLayoutData->label_layout_item;
373 if (archiver.IsArchived(labelBarItem))
374 err = archiver.AddArchivable(kLabelItemField, labelBarItem);
376 return err;
380 status_t
381 BMenuField::AllUnarchived(const BMessage* from)
383 BUnarchiver unarchiver(from);
385 status_t err = B_OK;
386 if ((err = BView::AllUnarchived(from)) != B_OK)
387 return err;
389 _InitMenuBar(from);
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);
396 if (err == B_OK)
397 menuItem->SetParent(this);
398 else
399 return err;
402 if (unarchiver.IsInstantiated(kLabelItemField)) {
403 LabelLayoutItem*& labelItem = fLayoutData->label_layout_item;
404 err = unarchiver.FindObject(kLabelItemField,
405 BUnarchiver::B_DONT_ASSUME_OWNERSHIP, labelItem);
407 if (err == B_OK)
408 labelItem->SetParent(this);
411 return err;
415 void
416 BMenuField::Draw(BRect updateRect)
418 _DrawLabel(updateRect);
419 _DrawMenuBar(updateRect);
423 void
424 BMenuField::AttachedToWindow()
426 CALLED();
427 rgb_color color;
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);
438 SetViewColor(color);
439 SetLowColor(color);
443 void
444 BMenuField::AllAttached()
446 CALLED();
448 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height());
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;
461 ResizeTo(width, fMenuBar->Bounds().Height() + kVMargin * 2);
463 TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height());
467 void
468 BMenuField::MouseDown(BPoint where)
470 BRect bounds = fMenuBar->ConvertFromParent(Bounds());
472 fMenuBar->StartMenuBar(-1, false, true, &bounds);
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);
480 MouseDownThread<BMenuField>::TrackMouse(this, &BMenuField::_DoneTracking,
481 &BMenuField::_Track);
486 void
487 BMenuField::KeyDown(const char* bytes, int32 numBytes)
489 switch (bytes[0]) {
490 case B_SPACE:
491 case B_RIGHT_ARROW:
492 case B_DOWN_ARROW:
494 if (!IsEnabled())
495 break;
497 BRect bounds = fMenuBar->ConvertFromParent(Bounds());
499 fMenuBar->StartMenuBar(0, true, true, &bounds);
501 bounds = Bounds();
502 bounds.right = fDivider;
504 Invalidate(bounds);
507 default:
508 BView::KeyDown(bytes, numBytes);
513 void
514 BMenuField::MakeFocus(bool focused)
516 if (IsFocus() == focused)
517 return;
519 BView::MakeFocus(focused);
521 if (Window() != NULL)
522 Invalidate(); // TODO: use fLayoutData->label_width
526 void
527 BMenuField::MessageReceived(BMessage* message)
529 BView::MessageReceived(message);
533 void
534 BMenuField::WindowActivated(bool active)
536 BView::WindowActivated(active);
538 if (IsFocus())
539 Invalidate();
543 void
544 BMenuField::MouseMoved(BPoint point, uint32 code, const BMessage* message)
546 BView::MouseMoved(point, code, message);
550 void
551 BMenuField::MouseUp(BPoint where)
553 BView::MouseUp(where);
557 void
558 BMenuField::DetachedFromWindow()
560 BView::DetachedFromWindow();
564 void
565 BMenuField::AllDetached()
567 BView::AllDetached();
571 void
572 BMenuField::FrameMoved(BPoint newPosition)
574 BView::FrameMoved(newPosition);
578 void
579 BMenuField::FrameResized(float newWidth, float newHeight)
581 BView::FrameResized(newWidth, newHeight);
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());
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();
600 fLayoutData->previous_height = newHeight;
604 BMenu*
605 BMenuField::Menu() const
607 return fMenu;
611 BMenuBar*
612 BMenuField::MenuBar() const
614 return fMenuBar;
618 BMenuItem*
619 BMenuField::MenuItem() const
621 return fMenuBar->ItemAt(0);
625 void
626 BMenuField::SetLabel(const char* label)
628 if (fLabel) {
629 if (label && strcmp(fLabel, label) == 0)
630 return;
632 free(fLabel);
635 fLabel = strdup(label);
637 if (Window())
638 Invalidate();
640 InvalidateLayout();
644 const char*
645 BMenuField::Label() const
647 return fLabel;
651 void
652 BMenuField::SetEnabled(bool on)
654 if (fEnabled == on)
655 return;
657 fEnabled = on;
658 fMenuBar->SetEnabled(on);
660 if (Window()) {
661 fMenuBar->Invalidate(fMenuBar->Bounds());
662 Invalidate(Bounds());
667 bool
668 BMenuField::IsEnabled() const
670 return fEnabled;
674 void
675 BMenuField::SetAlignment(alignment label)
677 fAlign = label;
681 alignment
682 BMenuField::Alignment() const
684 return fAlign;
688 void
689 BMenuField::SetDivider(float position)
691 position = roundf(position);
693 float delta = fDivider - position;
694 if (delta == 0.0f)
695 return;
697 fDivider = position;
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());
706 fMenuBar->MoveTo(_MenuBarOffset(), kVMargin);
708 if (fFixedSizeMB)
709 fMenuBar->ResizeTo(_MenuBarWidth(), dirty.Height());
711 dirty = dirty | fMenuBar->Frame();
712 dirty.InsetBy(-kVMargin, -kVMargin);
714 Invalidate(dirty);
719 float
720 BMenuField::Divider() const
722 return fDivider;
726 void
727 BMenuField::ShowPopUpMarker()
729 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) {
730 menuBar->TogglePopUpMarker(true);
731 menuBar->Invalidate();
736 void
737 BMenuField::HidePopUpMarker()
739 if (_BMCMenuBar_* menuBar = dynamic_cast<_BMCMenuBar_*>(fMenuBar)) {
740 menuBar->TogglePopUpMarker(false);
741 menuBar->Invalidate();
746 BHandler*
747 BMenuField::ResolveSpecifier(BMessage* message, int32 index,
748 BMessage* specifier, int32 form, const char* property)
750 return BView::ResolveSpecifier(message, index, specifier, form, property);
754 status_t
755 BMenuField::GetSupportedSuites(BMessage* data)
757 return BView::GetSupportedSuites(data);
761 void
762 BMenuField::ResizeToPreferred()
764 CALLED();
766 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n",
767 fMenuBar->Frame().Width(), fMenuBar->Frame().Height());
769 fMenuBar->ResizeToPreferred();
771 TRACE("fMenuBar->Frame().width: %.2f, height: %.2f\n",
772 fMenuBar->Frame().Width(), fMenuBar->Frame().Height());
774 BView::ResizeToPreferred();
776 Invalidate();
780 void
781 BMenuField::GetPreferredSize(float* _width, float* _height)
783 CALLED();
785 _ValidateLayoutData();
787 if (_width)
788 *_width = fLayoutData->min.width;
790 if (_height)
791 *_height = fLayoutData->min.height;
795 BSize
796 BMenuField::MinSize()
798 CALLED();
800 _ValidateLayoutData();
801 return BLayoutUtils::ComposeSize(ExplicitMinSize(), fLayoutData->min);
805 BSize
806 BMenuField::MaxSize()
808 CALLED();
810 _ValidateLayoutData();
812 BSize max = fLayoutData->min;
813 max.width = B_SIZE_UNLIMITED;
815 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), max);
819 BSize
820 BMenuField::PreferredSize()
822 CALLED();
824 _ValidateLayoutData();
825 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), fLayoutData->min);
829 BLayoutItem*
830 BMenuField::CreateLabelLayoutItem()
832 if (fLayoutData->label_layout_item == NULL)
833 fLayoutData->label_layout_item = new LabelLayoutItem(this);
835 return fLayoutData->label_layout_item;
839 BLayoutItem*
840 BMenuField::CreateMenuBarLayoutItem()
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);
849 return fLayoutData->menu_bar_layout_item;
853 status_t
854 BMenuField::Perform(perform_code code, void* _data)
856 switch (code) {
857 case PERFORM_CODE_MIN_SIZE:
858 ((perform_data_min_size*)_data)->return_value
859 = BMenuField::MinSize();
860 return B_OK;
862 case PERFORM_CODE_MAX_SIZE:
863 ((perform_data_max_size*)_data)->return_value
864 = BMenuField::MaxSize();
865 return B_OK;
867 case PERFORM_CODE_PREFERRED_SIZE:
868 ((perform_data_preferred_size*)_data)->return_value
869 = BMenuField::PreferredSize();
870 return B_OK;
872 case PERFORM_CODE_LAYOUT_ALIGNMENT:
873 ((perform_data_layout_alignment*)_data)->return_value
874 = BMenuField::LayoutAlignment();
875 return B_OK;
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;
882 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH:
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;
891 case PERFORM_CODE_SET_LAYOUT:
893 perform_data_set_layout* data = (perform_data_set_layout*)_data;
894 BMenuField::SetLayout(data->layout);
895 return B_OK;
898 case PERFORM_CODE_LAYOUT_INVALIDATED:
900 perform_data_layout_invalidated* data
901 = (perform_data_layout_invalidated*)_data;
902 BMenuField::LayoutInvalidated(data->descendants);
903 return B_OK;
906 case PERFORM_CODE_DO_LAYOUT:
908 BMenuField::DoLayout();
909 return B_OK;
912 case PERFORM_CODE_ALL_UNARCHIVED:
914 perform_data_all_unarchived* data
915 = (perform_data_all_unarchived*)_data;
916 data->return_value = BMenuField::AllUnarchived(data->archive);
917 return B_OK;
920 case PERFORM_CODE_ALL_ARCHIVED:
922 perform_data_all_archived* data
923 = (perform_data_all_archived*)_data;
924 data->return_value = BMenuField::AllArchived(data->archive);
925 return B_OK;
929 return BView::Perform(code, _data);
933 void
934 BMenuField::LayoutInvalidated(bool descendants)
936 CALLED();
938 fLayoutData->valid = false;
942 void
943 BMenuField::DoLayout()
945 // Bail out, if we shan't do layout.
946 if ((Flags() & B_SUPPORTS_LAYOUT) == 0)
947 return;
949 CALLED();
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;
958 _ValidateLayoutData();
960 // validate current size
961 BSize size(Bounds().Size());
962 if (size.width < fLayoutData->min.width)
963 size.width = fLayoutData->min.width;
965 if (size.height < fLayoutData->min.height)
966 size.height = fLayoutData->min.height;
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();
982 // menu bar
983 BRect dirty(fMenuBar->Frame());
984 BRect menuBarFrame(divider + kVMargin, kVMargin, size.width - kVMargin,
985 size.height - kVMargin);
987 // place the menu bar and set the divider
988 BLayoutUtils::AlignInFrame(fMenuBar, menuBarFrame);
990 fDivider = divider;
992 // invalidate dirty region
993 dirty = dirty | fMenuBar->Frame();
994 dirty.InsetBy(-kVMargin, -kVMargin);
996 Invalidate(dirty);
1000 void BMenuField::_ReservedMenuField1() {}
1001 void BMenuField::_ReservedMenuField2() {}
1002 void BMenuField::_ReservedMenuField3() {}
1005 void
1006 BMenuField::InitObject(const char* label)
1008 CALLED();
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();
1020 SetLabel(label);
1022 if (label)
1023 fDivider = floorf(Frame().Width() / 2.0f);
1024 else
1025 fDivider = 0;
1029 void
1030 BMenuField::InitObject2()
1032 CALLED();
1034 if (!fFixedSizeMB) {
1035 float height;
1036 fMenuBar->GetPreferredSize(NULL, &height);
1037 fMenuBar->ResizeTo(_MenuBarWidth(), height);
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());
1045 fMenuBar->AddFilter(new _BMCFilter_(this, B_MOUSE_DOWN));
1049 void
1050 BMenuField::_DrawLabel(BRect updateRect)
1052 CALLED();
1054 BRect rect(Bounds());
1055 rect.right = fDivider;
1056 if (!rect.IsValid() || !rect.Intersects(updateRect))
1057 return;
1059 _ValidateLayoutData();
1060 font_height& fh = fLayoutData->font_info;
1062 const char* label = Label();
1063 if (label == NULL)
1064 return;
1066 // horizontal alignment
1067 float x;
1068 switch (fAlign) {
1069 case B_ALIGN_RIGHT:
1070 x = fDivider - fLayoutData->label_width - 3.0f;
1071 break;
1073 case B_ALIGN_CENTER:
1074 x = fDivider - roundf(fLayoutData->label_width / 2.0f);
1075 break;
1077 default:
1078 x = 0.0;
1079 break;
1082 // vertical alignment
1083 float y = rect.top
1084 + roundf((rect.Height() + 1 - fh.ascent - fh.descent) / 2.0f)
1085 + fh.ascent;
1087 const rgb_color lowColor = LowColor();
1089 MenuPrivate menuPrivate(fMenuBar);
1090 if (menuPrivate.State() != MENU_STATE_CLOSED)
1091 SetLowColor(ui_color(B_MENU_SELECTED_BACKGROUND_COLOR));
1093 BRect fillRect(rect.InsetByCopy(0, kVMargin));
1094 fillRect.right -= kVMargin * 2;
1095 FillRect(fillRect, B_SOLID_LOW);
1097 uint32 flags = 0;
1098 if (!IsEnabled())
1099 flags |= BControlLook::B_DISABLED;
1101 be_control_look->DrawLabel(this, label, LowColor(), flags, BPoint(x, y));
1103 SetLowColor(lowColor);
1107 void
1108 BMenuField::_DrawMenuBar(BRect updateRect)
1110 CALLED();
1112 BRect rect(fMenuBar->Frame().InsetByCopy(-kVMargin, -kVMargin));
1113 if (!rect.IsValid() || !rect.Intersects(updateRect))
1114 return;
1116 uint32 flags = 0;
1117 if (!IsEnabled())
1118 flags |= BControlLook::B_DISABLED;
1120 if (IsFocus() && Window()->IsActive())
1121 flags |= BControlLook::B_FOCUSED;
1123 be_control_look->DrawMenuFieldFrame(this, rect, updateRect,
1124 fMenuBar->LowColor(), LowColor(), flags);
1128 void
1129 BMenuField::InitMenu(BMenu* menu)
1131 menu->SetFont(be_plain_font);
1133 int32 index = 0;
1134 BMenu* subMenu;
1136 while ((subMenu = menu->SubmenuAt(index++)) != NULL)
1137 InitMenu(subMenu);
1141 /*static*/ int32
1142 BMenuField::_thread_entry(void* arg)
1144 return static_cast<BMenuField*>(arg)->_MenuTask();
1148 int32
1149 BMenuField::_MenuTask()
1151 if (!LockLooper())
1152 return 0;
1154 Invalidate();
1155 UnlockLooper();
1157 bool tracking;
1158 do {
1159 snooze(20000);
1160 if (!LockLooper())
1161 return 0;
1163 tracking = fMenuBar->fTracking;
1165 UnlockLooper();
1166 } while (tracking);
1168 if (LockLooper()) {
1169 Invalidate();
1170 UnlockLooper();
1173 return 0;
1177 void
1178 BMenuField::_UpdateFrame()
1180 CALLED();
1182 if (fLayoutData->label_layout_item == NULL
1183 || fLayoutData->menu_bar_layout_item == NULL) {
1184 return;
1187 BRect labelFrame = fLayoutData->label_layout_item->Frame();
1188 BRect menuFrame = fLayoutData->menu_bar_layout_item->Frame();
1190 if (!labelFrame.IsValid() || !menuFrame.IsValid())
1191 return;
1193 // update divider
1194 fDivider = menuFrame.left - labelFrame.left;
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();
1203 // If the size changes, ResizeTo() will trigger a relayout, otherwise
1204 // we need to do that explicitly.
1205 if (newSize != oldSize)
1206 Relayout();
1210 void
1211 BMenuField::_InitMenuBar(BMenu* menu, BRect frame, bool fixedSize)
1213 CALLED();
1215 fMenu = menu;
1216 InitMenu(menu);
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;
1226 TRACE("frame(%.1f, %.1f, %.1f, %.1f) (%.2f, %.2f)\n",
1227 frame.left, frame.top, frame.right, frame.bottom,
1228 frame.Width(), frame.Height());
1230 fMenuBar = new _BMCMenuBar_(frame, fixedSize, this);
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));
1243 AddChild(fMenuBar);
1244 fMenuBar->AddItem(menu);
1245 fMenuBar->SetFont(be_plain_font);
1249 void
1250 BMenuField::_InitMenuBar(const BMessage* archive)
1252 bool fixed;
1253 if (archive->FindBool("be:fixeds", &fixed) == B_OK)
1254 fFixedSizeMB = fixed;
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()
1265 fMenu = fMenuBar->SubmenuAt(0);
1267 bool disable;
1268 if (archive->FindBool("_disable", &disable) == B_OK)
1269 SetEnabled(!disable);
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);
1279 void
1280 BMenuField::_ValidateLayoutData()
1282 CALLED();
1284 if (fLayoutData->valid)
1285 return;
1287 // cache font height
1288 font_height& fh = fLayoutData->font_info;
1289 GetFontHeight(&fh);
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;
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();
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);
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();
1318 TRACE("menu bar min width: %.2f\n", fLayoutData->menu_bar_min.width);
1320 // compute our minimal (== preferred) size
1321 BSize min(fLayoutData->menu_bar_min);
1322 min.width += 2 * kVMargin;
1323 min.height += 2 * kVMargin;
1325 if (divider > 0)
1326 min.width += divider;
1328 if (fLayoutData->label_height > min.height)
1329 min.height = fLayoutData->label_height;
1331 fLayoutData->min = min;
1333 fLayoutData->valid = true;
1334 ResetLayoutInvalidation();
1336 TRACE("width: %.2f, height: %.2f\n", min.width, min.height);
1340 float
1341 BMenuField::_MenuBarOffset() const
1343 return std::max(fDivider + kVMargin, kVMargin);
1347 float
1348 BMenuField::_MenuBarWidth() const
1350 return Bounds().Width() - (_MenuBarOffset() + kVMargin);
1354 void
1355 BMenuField::_DoneTracking(BPoint point)
1357 Window()->RemoveCommonFilter(fMouseDownFilter);
1361 void
1362 BMenuField::_Track(BPoint point, uint32)
1367 // #pragma mark - BMenuField::LabelLayoutItem
1370 BMenuField::LabelLayoutItem::LabelLayoutItem(BMenuField* parent)
1372 fParent(parent),
1373 fFrame()
1378 BMenuField::LabelLayoutItem::LabelLayoutItem(BMessage* from)
1380 BAbstractLayoutItem(from),
1381 fParent(NULL),
1382 fFrame()
1384 from->FindRect(kFrameField, &fFrame);
1388 bool
1389 BMenuField::LabelLayoutItem::IsVisible()
1391 return !fParent->IsHidden(fParent);
1395 void
1396 BMenuField::LabelLayoutItem::SetVisible(bool visible)
1398 // not allowed
1402 BRect
1403 BMenuField::LabelLayoutItem::Frame()
1405 return fFrame;
1409 void
1410 BMenuField::LabelLayoutItem::SetFrame(BRect frame)
1412 fFrame = frame;
1413 fParent->_UpdateFrame();
1417 void
1418 BMenuField::LabelLayoutItem::SetParent(BMenuField* parent)
1420 fParent = parent;
1424 BView*
1425 BMenuField::LabelLayoutItem::View()
1427 return fParent;
1431 BSize
1432 BMenuField::LabelLayoutItem::BaseMinSize()
1434 fParent->_ValidateLayoutData();
1436 if (fParent->Label() == NULL)
1437 return BSize(-1, -1);
1439 return BSize(fParent->fLayoutData->label_width
1440 + be_control_look->DefaultLabelSpacing(),
1441 fParent->fLayoutData->label_height);
1445 BSize
1446 BMenuField::LabelLayoutItem::BaseMaxSize()
1448 return BaseMinSize();
1452 BSize
1453 BMenuField::LabelLayoutItem::BasePreferredSize()
1455 return BaseMinSize();
1459 BAlignment
1460 BMenuField::LabelLayoutItem::BaseAlignment()
1462 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
1466 status_t
1467 BMenuField::LabelLayoutItem::Archive(BMessage* into, bool deep) const
1469 BArchiver archiver(into);
1470 status_t err = BAbstractLayoutItem::Archive(into, deep);
1472 if (err == B_OK)
1473 err = into->AddRect(kFrameField, fFrame);
1475 return archiver.Finish(err);
1479 BArchivable*
1480 BMenuField::LabelLayoutItem::Instantiate(BMessage* from)
1482 if (validate_instantiation(from, "BMenuField::LabelLayoutItem"))
1483 return new LabelLayoutItem(from);
1485 return NULL;
1489 // #pragma mark - BMenuField::MenuBarLayoutItem
1492 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMenuField* parent)
1494 fParent(parent),
1495 fFrame()
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));
1503 BMenuField::MenuBarLayoutItem::MenuBarLayoutItem(BMessage* from)
1505 BAbstractLayoutItem(from),
1506 fParent(NULL),
1507 fFrame()
1509 from->FindRect(kFrameField, &fFrame);
1513 bool
1514 BMenuField::MenuBarLayoutItem::IsVisible()
1516 return !fParent->IsHidden(fParent);
1520 void
1521 BMenuField::MenuBarLayoutItem::SetVisible(bool visible)
1523 // not allowed
1527 BRect
1528 BMenuField::MenuBarLayoutItem::Frame()
1530 return fFrame;
1534 void
1535 BMenuField::MenuBarLayoutItem::SetFrame(BRect frame)
1537 fFrame = frame;
1538 fParent->_UpdateFrame();
1542 void
1543 BMenuField::MenuBarLayoutItem::SetParent(BMenuField* parent)
1545 fParent = parent;
1549 BView*
1550 BMenuField::MenuBarLayoutItem::View()
1552 return fParent;
1556 BSize
1557 BMenuField::MenuBarLayoutItem::BaseMinSize()
1559 fParent->_ValidateLayoutData();
1561 BSize size = fParent->fLayoutData->menu_bar_min;
1562 size.width += 2 * kVMargin;
1563 size.height += 2 * kVMargin;
1565 return size;
1569 BSize
1570 BMenuField::MenuBarLayoutItem::BaseMaxSize()
1572 BSize size(BaseMinSize());
1573 size.width = B_SIZE_UNLIMITED;
1575 return size;
1579 BSize
1580 BMenuField::MenuBarLayoutItem::BasePreferredSize()
1582 return BaseMinSize();
1586 BAlignment
1587 BMenuField::MenuBarLayoutItem::BaseAlignment()
1589 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
1593 status_t
1594 BMenuField::MenuBarLayoutItem::Archive(BMessage* into, bool deep) const
1596 BArchiver archiver(into);
1597 status_t err = BAbstractLayoutItem::Archive(into, deep);
1599 if (err == B_OK)
1600 err = into->AddRect(kFrameField, fFrame);
1602 return archiver.Finish(err);
1606 BArchivable*
1607 BMenuField::MenuBarLayoutItem::Instantiate(BMessage* from)
1609 if (validate_instantiation(from, "BMenuField::MenuBarLayoutItem"))
1610 return new MenuBarLayoutItem(from);
1611 return NULL;
1615 extern "C" void
1616 B_IF_GCC_2(InvalidateLayout__10BMenuFieldb, _ZN10BMenuField16InvalidateLayoutEb)(
1617 BMenuField* field, bool descendants)
1619 perform_data_layout_invalidated data;
1620 data.descendants = descendants;
1622 field->Perform(PERFORM_CODE_LAYOUT_INVALIDATED, &data);