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