xref: /haiku/src/kits/interface/MenuWindow.cpp (revision a4f6a81235ca2522c01f532de13cad9b729d4029)
1 /*
2  * Copyright 2001-2006, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Marc Flerackers (mflerackers@androme.be)
7  *		Stefano Ceccherini (burton666@libero.it)
8  */
9 
10 //!	BMenuWindow is a custom BWindow for BMenus.
11 
12 // TODO: Add scrollers
13 
14 #include <Menu.h>
15 #include <MenuWindow.h>
16 
17 #include <WindowPrivate.h>
18 
19 
20 // This draws the frame around the BMenu
21 class BMenuFrame : public BView {
22 	public:
23 		BMenuFrame(BMenu *menu);
24 
25 		virtual void AttachedToWindow();
26 		virtual void Draw(BRect updateRect);
27 
28 	private:
29 		BMenu *fMenu;
30 };
31 
32 
33 BMenuWindow::BMenuWindow(const char *name, BMenu *menu)
34 	// The window will be resized by BMenu, so just pass a dummy rect
35 	: BWindow(BRect(0, 0, 0, 0), name, B_BORDERED_WINDOW_LOOK, kMenuWindowFeel,
36 			B_NOT_ZOOMABLE | B_AVOID_FOCUS),
37 	fUpperScroller(NULL),
38 	fLowerScroller(NULL)
39 {
40 	SetMenu(menu);
41 }
42 
43 
44 BMenuWindow::~BMenuWindow()
45 {
46 }
47 
48 
49 void
50 BMenuWindow::SetMenu(BMenu *menu)
51 {
52 	if (CountChildren() > 0)
53 		RemoveChild(ChildAt(0));
54 	BMenuFrame *menuFrame = new BMenuFrame(menu);
55 	AddChild(menuFrame);
56 }
57 
58 
59 //	#pragma mark -
60 
61 
62 BMenuFrame::BMenuFrame(BMenu *menu)
63 	: BView(BRect(0, 0, 1, 1), "menu frame", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
64 	fMenu(menu)
65 {
66 }
67 
68 
69 void
70 BMenuFrame::AttachedToWindow()
71 {
72 	BView::AttachedToWindow();
73 	ResizeTo(Window()->Bounds().Width(), Window()->Bounds().Height());
74 	if (fMenu != NULL) {
75 		BFont font;
76 		fMenu->GetFont(&font);
77 		SetFont(&font);
78 	}
79 }
80 
81 
82 void
83 BMenuFrame::Draw(BRect updateRect)
84 {
85 	if (fMenu->CountItems() == 0) {
86 		// TODO: Review this as it's a bit hacky.
87 		// Menu has a size of 0, 0, since there are no items in it.
88 		// So the BMenuFrame class has to fake it and draw an empty item.
89 		// Note that we can't add a real "empty" item because then we couldn't
90 		// tell if the item was added by us or not.
91 		// See also BMenu::UpdateWindowViewSize()
92 		SetHighColor(ui_color(B_MENU_BACKGROUND_COLOR));
93 		SetLowColor(HighColor());
94 		FillRect(updateRect);
95 
96 		font_height height;
97 		GetFontHeight(&height);
98 		SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DISABLED_LABEL_TINT));
99 		DrawString("<empty>", BPoint(2, ceilf(height.ascent + 2)));
100 	}
101 
102 	SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_2_TINT));
103 	BRect bounds(Bounds());
104 
105 	StrokeLine(BPoint(bounds.right, bounds.top),
106 		BPoint(bounds.right, bounds.bottom - 1));
107 	StrokeLine(BPoint(bounds.left + 1, bounds.bottom),
108 		BPoint(bounds.right, bounds.bottom));
109 }
110