From 8eb2ea889d00a6facba0259883d9d29b0deac2ff Mon Sep 17 00:00:00 2001 From: fluxgen Date: Mon, 7 Jun 2004 20:34:23 +0000 Subject: drawing of items is now done in MenuItem --- src/FbTk/MenuItem.cc | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/FbTk/MenuItem.hh | 41 +++++++++---- 2 files changed, 185 insertions(+), 14 deletions(-) diff --git a/src/FbTk/MenuItem.cc b/src/FbTk/MenuItem.cc index 0dbdc69..55aef9a 100644 --- a/src/FbTk/MenuItem.cc +++ b/src/FbTk/MenuItem.cc @@ -1,5 +1,5 @@ // MenuItem.cc for FbTk - Fluxbox Toolkit -// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) +// Copyright (c) 2003-2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), @@ -19,10 +19,13 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: MenuItem.cc,v 1.1 2003/01/12 17:06:07 fluxgen Exp $ +// $Id: MenuItem.cc,v 1.2 2004/06/07 20:34:23 fluxgen Exp $ #include "MenuItem.hh" #include "Command.hh" +#include "GContext.hh" +#include "MenuTheme.hh" +#include "App.hh" namespace FbTk { @@ -31,4 +34,155 @@ void MenuItem::click(int button, int time) { m_command->execute(); } +void MenuItem::draw(FbDrawable &draw, + const MenuTheme &theme, + bool highlight, + int x, int y, + unsigned int width, unsigned int height) const { + if (label().empty()) + return; + + const GContext &tgc = + (highlight ? theme.hiliteTextGC() : + (isEnabled() ? theme.frameTextGC() : theme.disableTextGC() ) ); + int text_y = y, text_x = x; + + int text_w = theme.frameFont().textWidth(label().c_str(), label().size()); + + text_y = y + theme.bevelWidth()/2 + theme.frameFont().ascent()/2 + height/2; + + switch(theme.frameFontJustify()) { + case FbTk::LEFT: + text_x = x + theme.bevelWidth() + height + 1; + break; + + case FbTk::RIGHT: + text_x = x + width - (height + theme.bevelWidth() + text_w); + break; + default: //center + text_x = x + ((width + 1 - text_w) / 2); + break; + } + + theme.frameFont().drawText(draw.drawable(), // drawable + theme.screenNum(), + tgc.gc(), + m_label.c_str(), m_label.size(), // text string and lenght + text_x, text_y); // position + + GC gc = + ((highlight || isSelected()) ? theme.hiliteTextGC().gc() : + theme.frameTextGC().gc()); + + int sel_x = x + height/4; + + + if (theme.bulletPos() == FbTk::RIGHT) + sel_x += width - height - 2*theme.bevelWidth(); + + if (isToggleItem() && theme.unselectedPixmap().pixmap().drawable() != 0) { + XSetClipMask(FbTk::App::instance()->display(), + gc, + theme.unselectedPixmap().mask().drawable()); + XSetClipOrigin(FbTk::App::instance()->display(), + gc, sel_x, y); + // copy bullet pixmap to drawable + draw.copyArea(theme.unselectedPixmap().pixmap().drawable(), + gc, + 0, 0, + sel_x, y, + theme.unselectedPixmap().width(), + theme.unselectedPixmap().height()); + // disable clip mask + XSetClipMask(FbTk::App::instance()->display(), + gc, + None); + } + + + if (submenu()) { + if (theme.bulletPixmap().pixmap().drawable() != 0) { + // enable clip mask + XSetClipMask(FbTk::App::instance()->display(), + gc, + theme.bulletPixmap().mask().drawable()); + XSetClipOrigin(FbTk::App::instance()->display(), + gc, sel_x, y); + // copy bullet pixmap to frame + draw.copyArea(theme.bulletPixmap().pixmap().drawable(), + gc, + 0, 0, + sel_x, y, + theme.bulletPixmap().width(), + theme.bulletPixmap().height()); + // disable clip mask + XSetClipMask(FbTk::App::instance()->display(), + gc, + None); + } else { + unsigned int half_w = height / 2, quarter_w = height / 4; + int sel_y = y + height/4; + switch (theme.bullet()) { + case MenuTheme::SQUARE: + draw.drawRectangle(gc, sel_x, sel_y, half_w, half_w); + break; + + case MenuTheme::TRIANGLE: + XPoint tri[3]; + + if (theme.bulletPos() == FbTk::RIGHT) { + tri[0].x = sel_x + quarter_w - 2; + tri[0].y = sel_y + quarter_w - 2; + tri[1].x = 4; + tri[1].y = 2; + tri[2].x = -4; + tri[2].y = 2; + } else { + tri[0].x = sel_x + quarter_w - 2; + tri[0].y = y + half_w; + tri[1].x = 4; + tri[1].y = 2; + tri[2].x = 0; + tri[2].y = -4; + } + + draw.fillPolygon(gc, tri, 3, Convex, + CoordModePrevious); + break; + + case MenuTheme::DIAMOND: + XPoint dia[4]; + + dia[0].x = sel_x + quarter_w - 3; + dia[0].y = y + half_w; + dia[1].x = 3; + dia[1].y = -3; + dia[2].x = 3; + dia[2].y = 3; + dia[3].x = -3; + dia[3].y = 3; + + draw.fillPolygon(gc, dia, 4, Convex, + CoordModePrevious); + break; + default: + break; + } + } + } + +} + +unsigned int MenuItem::height(const MenuTheme &theme) const { + return std::max(theme.frameFont().height() + theme.bevelWidth(), theme.itemHeight()); +} + +unsigned int MenuItem::width(const MenuTheme &theme) const { + // textwidth + bevel width on each side of the text + return theme.frameFont().textWidth(label().c_str(), label().size()) + 2*(theme.bevelWidth() + height(theme)); + +} + + + }; // end namespace FbTk diff --git a/src/FbTk/MenuItem.hh b/src/FbTk/MenuItem.hh index 6aeb40c..1d9ed20 100644 --- a/src/FbTk/MenuItem.hh +++ b/src/FbTk/MenuItem.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: MenuItem.hh,v 1.4 2003/12/16 17:06:52 fluxgen Exp $ +// $Id: MenuItem.hh,v 1.5 2004/06/07 20:33:20 fluxgen Exp $ #ifndef FBTK_MENUITEM_HH #define FBTK_MENUITEM_HH @@ -31,11 +31,20 @@ namespace FbTk { class Menu; +class MenuTheme; +class FbDrawable; /// An interface for a menu item in Menu class MenuItem { public: - MenuItem( + MenuItem() + : m_label(""), + m_submenu(0), + m_enabled(true), + m_selected(false), + m_toggle_item(false) + { } + explicit MenuItem( const char *label) : m_label(label ? label : ""), m_submenu(0), @@ -63,21 +72,29 @@ public: { } virtual ~MenuItem() { } - void setCommand(RefCount &cmd) { m_command = cmd; } - virtual void setSelected(bool selected) { m_selected = selected; } - virtual void setEnabled(bool enabled) { m_enabled = enabled; } - virtual void setLabel(const char *label) { m_label = (label ? label : ""); } - virtual void setToggleItem(bool val) { m_toggle_item = val; } + inline void setCommand(RefCount &cmd) { m_command = cmd; } + virtual inline void setSelected(bool selected) { m_selected = selected; } + virtual inline void setEnabled(bool enabled) { m_enabled = enabled; } + virtual inline void setLabel(const char *label) { m_label = (label ? label : ""); } + virtual inline void setToggleItem(bool val) { m_toggle_item = val; } Menu *submenu() { return m_submenu; } /** @name accessors */ //@{ - virtual const std::string &label() const { return m_label; } - const Menu *submenu() const { return m_submenu; } - virtual bool isEnabled() const { return m_enabled; } - virtual bool isSelected() const { return m_selected; } - virtual bool isToggleItem() const { return m_toggle_item; } + virtual inline const std::string &label() const { return m_label; } + inline const Menu *submenu() const { return m_submenu; } + virtual inline bool isEnabled() const { return m_enabled; } + virtual inline bool isSelected() const { return m_selected; } + virtual inline bool isToggleItem() const { return m_toggle_item; } + virtual unsigned int width(const MenuTheme &theme) const; + virtual unsigned int height(const MenuTheme &theme) const; + virtual void draw(FbDrawable &drawable, + const MenuTheme &theme, + bool highlight, + int x, int y, + unsigned int width, unsigned int height) const; + virtual void updateTheme(const MenuTheme &theme) { } /** Called when the item was clicked with a specific button @param button the button number -- cgit v0.11.2