xref: /haiku/src/kits/interface/MenuWindow.cpp (revision 2c11ec31c876f2b77e3d03c9776dbf99119bbce7)
1c7023acdSAxel Dörfler /*
2c7023acdSAxel Dörfler  * Copyright 2001-2006, Haiku, Inc.
3c7023acdSAxel Dörfler  * Distributed under the terms of the MIT License.
4c7023acdSAxel Dörfler  *
5c7023acdSAxel Dörfler  * Authors:
6c7023acdSAxel Dörfler  *		Marc Flerackers (mflerackers@androme.be)
7c7023acdSAxel Dörfler  *		Stefano Ceccherini (burton666@libero.it)
8c7023acdSAxel Dörfler  */
9c7023acdSAxel Dörfler 
10c7023acdSAxel Dörfler //!	BMenuWindow is a custom BWindow for BMenus.
11c7023acdSAxel Dörfler 
125b752875SStefano Ceccherini // TODO: Add scrollers
135b752875SStefano Ceccherini 
14446b8c19SStefano Ceccherini #include <Menu.h>
15446b8c19SStefano Ceccherini 
16*2c11ec31SStefano Ceccherini #include <MenuPrivate.h>
17*2c11ec31SStefano Ceccherini #include <MenuWindow.h>
18c7023acdSAxel Dörfler #include <WindowPrivate.h>
19c7023acdSAxel Dörfler 
20e411c658SStefano Ceccherini 
214ff5ade2SStefano Ceccherini // This draws the frame around the BMenu
225b752875SStefano Ceccherini class BMenuFrame : public BView {
235b752875SStefano Ceccherini 	public:
2461ba5a32SStefano Ceccherini 		BMenuFrame(BMenu *menu);
255b752875SStefano Ceccherini 
265aa032f1SStefano Ceccherini 		virtual void AttachedToWindow();
275aa032f1SStefano Ceccherini 		virtual void Draw(BRect updateRect);
2861ba5a32SStefano Ceccherini 
2961ba5a32SStefano Ceccherini 	private:
3061ba5a32SStefano Ceccherini 		BMenu *fMenu;
315b752875SStefano Ceccherini };
325b752875SStefano Ceccherini 
335b752875SStefano Ceccherini 
3461ba5a32SStefano Ceccherini BMenuWindow::BMenuWindow(const char *name, BMenu *menu)
351664b981SStefano Ceccherini 	// The window will be resized by BMenu, so just pass a dummy rect
36c7023acdSAxel Dörfler 	: BWindow(BRect(0, 0, 0, 0), name, B_BORDERED_WINDOW_LOOK, kMenuWindowFeel,
37c7023acdSAxel Dörfler 			B_NOT_ZOOMABLE | B_AVOID_FOCUS),
385b752875SStefano Ceccherini 	fUpperScroller(NULL),
395b752875SStefano Ceccherini 	fLowerScroller(NULL)
40446b8c19SStefano Ceccherini {
4161ba5a32SStefano Ceccherini 	SetMenu(menu);
42446b8c19SStefano Ceccherini }
43446b8c19SStefano Ceccherini 
44446b8c19SStefano Ceccherini 
45446b8c19SStefano Ceccherini BMenuWindow::~BMenuWindow()
46446b8c19SStefano Ceccherini {
47446b8c19SStefano Ceccherini }
485aa032f1SStefano Ceccherini 
495aa032f1SStefano Ceccherini 
5061ba5a32SStefano Ceccherini void
5161ba5a32SStefano Ceccherini BMenuWindow::SetMenu(BMenu *menu)
5261ba5a32SStefano Ceccherini {
5361ba5a32SStefano Ceccherini 	if (CountChildren() > 0)
5461ba5a32SStefano Ceccherini 		RemoveChild(ChildAt(0));
5561ba5a32SStefano Ceccherini 	BMenuFrame *menuFrame = new BMenuFrame(menu);
5661ba5a32SStefano Ceccherini 	AddChild(menuFrame);
5761ba5a32SStefano Ceccherini }
5861ba5a32SStefano Ceccherini 
5961ba5a32SStefano Ceccherini 
60c7023acdSAxel Dörfler //	#pragma mark -
61c7023acdSAxel Dörfler 
62c7023acdSAxel Dörfler 
6361ba5a32SStefano Ceccherini BMenuFrame::BMenuFrame(BMenu *menu)
64c7023acdSAxel Dörfler 	: BView(BRect(0, 0, 1, 1), "menu frame", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
6561ba5a32SStefano Ceccherini 	fMenu(menu)
665aa032f1SStefano Ceccherini {
675aa032f1SStefano Ceccherini }
685aa032f1SStefano Ceccherini 
695aa032f1SStefano Ceccherini 
705aa032f1SStefano Ceccherini void
715aa032f1SStefano Ceccherini BMenuFrame::AttachedToWindow()
725aa032f1SStefano Ceccherini {
735aa032f1SStefano Ceccherini 	BView::AttachedToWindow();
745aa032f1SStefano Ceccherini 	ResizeTo(Window()->Bounds().Width(), Window()->Bounds().Height());
7555b35fb4SStefano Ceccherini 	if (fMenu != NULL) {
7655b35fb4SStefano Ceccherini 		BFont font;
7755b35fb4SStefano Ceccherini 		fMenu->GetFont(&font);
7855b35fb4SStefano Ceccherini 		SetFont(&font);
7955b35fb4SStefano Ceccherini 	}
805aa032f1SStefano Ceccherini }
815aa032f1SStefano Ceccherini 
825aa032f1SStefano Ceccherini 
835aa032f1SStefano Ceccherini void
845aa032f1SStefano Ceccherini BMenuFrame::Draw(BRect updateRect)
855aa032f1SStefano Ceccherini {
8661ba5a32SStefano Ceccherini 	if (fMenu->CountItems() == 0) {
8755b35fb4SStefano Ceccherini 		// TODO: Review this as it's a bit hacky.
8855b35fb4SStefano Ceccherini 		// Menu has a size of 0, 0, since there are no items in it.
8955b35fb4SStefano Ceccherini 		// So the BMenuFrame class has to fake it and draw an empty item.
9055b35fb4SStefano Ceccherini 		// Note that we can't add a real "empty" item because then we couldn't
9155b35fb4SStefano Ceccherini 		// tell if the item was added by us or not.
9255b35fb4SStefano Ceccherini 		// See also BMenu::UpdateWindowViewSize()
9361ba5a32SStefano Ceccherini 		SetHighColor(ui_color(B_MENU_BACKGROUND_COLOR));
9455b35fb4SStefano Ceccherini 		SetLowColor(HighColor());
9561ba5a32SStefano Ceccherini 		FillRect(updateRect);
9655b35fb4SStefano Ceccherini 
9761ba5a32SStefano Ceccherini 		font_height height;
9855b35fb4SStefano Ceccherini 		GetFontHeight(&height);
9955b35fb4SStefano Ceccherini 		SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DISABLED_LABEL_TINT));
100*2c11ec31SStefano Ceccherini 		DrawString(kEmptyMenuLabel, BPoint(3, ceilf(height.ascent + 2)));
10161ba5a32SStefano Ceccherini 	}
1025aa032f1SStefano Ceccherini 
1035aa032f1SStefano Ceccherini 	SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_2_TINT));
10461ba5a32SStefano Ceccherini 	BRect bounds(Bounds());
105c7023acdSAxel Dörfler 
1065aa032f1SStefano Ceccherini 	StrokeLine(BPoint(bounds.right, bounds.top),
1075aa032f1SStefano Ceccherini 		BPoint(bounds.right, bounds.bottom - 1));
1085aa032f1SStefano Ceccherini 	StrokeLine(BPoint(bounds.left + 1, bounds.bottom),
1095aa032f1SStefano Ceccherini 		BPoint(bounds.right, bounds.bottom));
1105aa032f1SStefano Ceccherini }
111