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