Files
haiku-beta6/src/kits/interface/MenuWindow.cpp
T

112 lines
2.4 KiB
C++
Raw Normal View History

/*
* Copyright 2001-2006, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Marc Flerackers ([email protected])
* Stefano Ceccherini ([email protected])
*/
//! BMenuWindow is a custom BWindow for BMenus.
// TODO: Add scrollers
2004-12-27 09:05:16 +00:00
#include <Menu.h>
2004-12-27 09:05:16 +00:00
#include <MenuPrivate.h>
#include <MenuWindow.h>
#include <WindowPrivate.h>
2004-12-27 09:56:01 +00:00
// This draws the frame around the BMenu
class BMenuFrame : public BView {
public:
BMenuFrame(BMenu *menu);
virtual void AttachedToWindow();
virtual void Draw(BRect updateRect);
private:
BMenu *fMenu;
};
BMenuWindow::BMenuWindow(const char *name, BMenu *menu)
// The window will be resized by BMenu, so just pass a dummy rect
: BWindow(BRect(0, 0, 0, 0), name, B_BORDERED_WINDOW_LOOK, kMenuWindowFeel,
B_NOT_ZOOMABLE | B_AVOID_FOCUS),
fUpperScroller(NULL),
fLowerScroller(NULL)
2004-12-27 09:05:16 +00:00
{
if (menu != NULL)
SetMenu(menu);
2004-12-27 09:05:16 +00:00
}
BMenuWindow::~BMenuWindow()
{
}
2005-12-20 16:28:52 +00:00
void
BMenuWindow::SetMenu(BMenu *menu)
{
if (CountChildren() > 0)
RemoveChild(ChildAt(0));
BMenuFrame *menuFrame = new BMenuFrame(menu);
AddChild(menuFrame);
}
// #pragma mark -
BMenuFrame::BMenuFrame(BMenu *menu)
: BView(BRect(0, 0, 1, 1), "menu frame", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
fMenu(menu)
2005-12-20 16:28:52 +00:00
{
}
void
BMenuFrame::AttachedToWindow()
{
BView::AttachedToWindow();
ResizeTo(Window()->Bounds().Width(), Window()->Bounds().Height());
2006-01-31 23:28:19 +00:00
if (fMenu != NULL) {
BFont font;
fMenu->GetFont(&font);
SetFont(&font);
}
2005-12-20 16:28:52 +00:00
}
void
BMenuFrame::Draw(BRect updateRect)
{
if (fMenu->CountItems() == 0) {
2006-01-31 23:28:19 +00:00
// TODO: Review this as it's a bit hacky.
// Menu has a size of 0, 0, since there are no items in it.
// So the BMenuFrame class has to fake it and draw an empty item.
// Note that we can't add a real "empty" item because then we couldn't
// tell if the item was added by us or not.
// See also BMenu::UpdateWindowViewSize()
SetHighColor(ui_color(B_MENU_BACKGROUND_COLOR));
2006-01-31 23:28:19 +00:00
SetLowColor(HighColor());
FillRect(updateRect);
2006-01-31 23:28:19 +00:00
font_height height;
2006-01-31 23:28:19 +00:00
GetFontHeight(&height);
SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DISABLED_LABEL_TINT));
DrawString(kEmptyMenuLabel, BPoint(3, ceilf(height.ascent + 2)));
}
SetHighColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR), B_DARKEN_2_TINT));
2005-12-20 16:28:52 +00:00
BRect bounds(Bounds());
2005-12-20 16:28:52 +00:00
StrokeLine(BPoint(bounds.right, bounds.top),
BPoint(bounds.right, bounds.bottom - 1));
2005-12-20 16:28:52 +00:00
StrokeLine(BPoint(bounds.left + 1, bounds.bottom),
BPoint(bounds.right, bounds.bottom));
2005-12-20 16:28:52 +00:00
}