From 31df2d8bd618cae590d9b0e76aee11021d4c77d3 Mon Sep 17 00:00:00 2001 From: Mark Tiefenbruck Date: Mon, 17 Dec 2007 21:09:20 -0800 Subject: make MenuItem responsible for decision to close menu when clicked --- src/BoolMenuItem.hh | 4 +++- src/FbTk/Menu.cc | 21 +++++++++++---------- src/FbTk/Menu.hh | 4 ++-- src/FbTk/MenuItem.cc | 2 ++ src/FbTk/MenuItem.hh | 40 ++++++++++++++++++++++------------------ src/IntResMenuItem.hh | 1 + src/RootCmdMenuItem.cc | 1 + src/StyleMenuItem.cc | 1 + 8 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/BoolMenuItem.hh b/src/BoolMenuItem.hh index 29eef15..d10a7ac 100644 --- a/src/BoolMenuItem.hh +++ b/src/BoolMenuItem.hh @@ -34,11 +34,13 @@ public: FbTk::MenuItem(label, cmd), m_item(item) { FbTk::MenuItem::setSelected(m_item); setToggleItem(true); + setCloseOnClick(false); } BoolMenuItem(const FbTk::FbString &label, bool &item): FbTk::MenuItem(label), m_item(item) { FbTk::MenuItem::setSelected(m_item); - setToggleItem(true); + setToggleItem(true); + setCloseOnClick(false); } bool isSelected() const { return m_item; } // toggle state diff --git a/src/FbTk/Menu.cc b/src/FbTk/Menu.cc index 88240f1..3be0d14 100644 --- a/src/FbTk/Menu.cc +++ b/src/FbTk/Menu.cc @@ -199,15 +199,15 @@ Menu::~Menu() { } int Menu::insert(const FbString &label, RefCount &cmd, int pos) { - return insert(new MenuItem(label, cmd), pos); + return insert(new MenuItem(label, cmd, this), pos); } int Menu::insert(const FbString &label, int pos) { - return insert(new MenuItem(label), pos); + return insert(new MenuItem(label, *this), pos); } int Menu::insert(const FbString &label, Menu *submenu, int pos) { - return insert(new MenuItem(label, submenu), pos); + return insert(new MenuItem(label, submenu, this), pos); } int Menu::insert(MenuItem *item, int pos) { @@ -516,15 +516,15 @@ void Menu::show() { raise(); if (shown && shown != this && shown != m_parent) - shown->hide(true); + shown->hide(); shown = this; } -void Menu::hide(bool force) { +void Menu::hide() { - if (!isVisible() || m_torn && !force) + if (!isVisible()) return; // if parent is visible, go to first parent and hide it @@ -570,6 +570,9 @@ void Menu::redrawFrame(FbDrawable &drawable) { void Menu::internal_hide(bool first) { + if (!first && m_torn) + return; + if (validIndex(m_which_sub)) { MenuItem *tmp = menuitems[m_which_sub]; tmp->submenu()->internal_hide(false); @@ -815,8 +818,6 @@ void Menu::handleEvent(XEvent &event) { if (event.type == FocusOut) { if (s_focused == this) s_focused = 0; - if (shown == this && !m_torn) - hide(); // I don't know why, but I get a FocusIn event when closing the menu with // the mouse over it -- probably an xorg bug, but it's easy to address here } else if (event.type == FocusIn && m_visible) { @@ -1275,9 +1276,9 @@ void Menu::drawLine(int index, int size){ item->drawLine(menu.frame, theme(), size, item_x, item_y, menu.item_w); } -void Menu::hideShownMenu(bool force) { +void Menu::hideShownMenu() { if (shown) - shown->hide(force); + shown->hide(); } }; // end namespace FbTk diff --git a/src/FbTk/Menu.hh b/src/FbTk/Menu.hh index 2a84c77..53dc494 100644 --- a/src/FbTk/Menu.hh +++ b/src/FbTk/Menu.hh @@ -124,7 +124,7 @@ public: /// show menu virtual void show(); /// hide menu - virtual void hide(bool force = false); + virtual void hide(); virtual void clearWindow(); #ifdef NOT_USED void setActiveIndex(int index) { m_active_index = index; } @@ -160,7 +160,7 @@ public: inline unsigned char alpha() const { return theme().alpha(); } inline static Menu *shownMenu() { return shown; } inline static Menu *focused() { return s_focused; } - static void hideShownMenu(bool force = true); + static void hideShownMenu(); /// @return menuitem at index inline const MenuItem *find(unsigned int index) const { return menuitems[index]; } inline MenuItem *find(unsigned int index) { return menuitems[index]; } diff --git a/src/FbTk/MenuItem.cc b/src/FbTk/MenuItem.cc index 2576d2b..7d309f4 100644 --- a/src/FbTk/MenuItem.cc +++ b/src/FbTk/MenuItem.cc @@ -35,6 +35,8 @@ namespace FbTk { void MenuItem::click(int button, int time) { if (m_command.get() != 0) { + if (m_menu && m_close_on_click) + m_menu->hide(); // we need a local variable, since the command may destroy this object RefCount tmp(m_command); tmp->execute(); diff --git a/src/FbTk/MenuItem.hh b/src/FbTk/MenuItem.hh index 7fac8c7..3b02f2e 100644 --- a/src/FbTk/MenuItem.hh +++ b/src/FbTk/MenuItem.hh @@ -47,15 +47,16 @@ public: m_submenu(0), m_enabled(true), m_selected(false), + m_close_on_click(true), m_toggle_item(false) { } - explicit MenuItem( - const FbString &label) + explicit MenuItem(const FbString &label) : m_label(label), m_menu(0), m_submenu(0), m_enabled(true), m_selected(false), + m_close_on_click(true), m_toggle_item(false) { } @@ -65,26 +66,28 @@ public: m_submenu(0), m_enabled(true), m_selected(false), + m_close_on_click(true), m_toggle_item(false) { } /// create a menu item with a specific command to be executed on click - MenuItem(const FbString &label, RefCount &cmd, Menu *menu = 0): - m_label(label), - m_menu(menu), - m_submenu(0), - m_command(cmd), - m_enabled(true), - m_selected(false), - m_toggle_item(false) { - - } + MenuItem(const FbString &label, RefCount &cmd, Menu *menu = 0) + : m_label(label), + m_menu(menu), + m_submenu(0), + m_command(cmd), + m_enabled(true), + m_selected(false), + m_close_on_click(true), + m_toggle_item(false) + { } MenuItem(const FbString &label, Menu *submenu, Menu *host_menu = 0) - : m_label(label) - , m_menu(host_menu) - , m_submenu(submenu) - , m_enabled(true) - , m_selected(false), + : m_label(label), + m_menu(host_menu), + m_submenu(submenu), + m_enabled(true), + m_selected(false), + m_close_on_click(true), m_toggle_item(false) { } virtual ~MenuItem() { } @@ -94,6 +97,7 @@ public: virtual void setEnabled(bool enabled) { m_enabled = enabled; } virtual void setLabel(const FbString &label) { m_label = label; } virtual void setToggleItem(bool val) { m_toggle_item = val; } + void setCloseOnClick(bool val) { m_close_on_click = val; } void setIcon(const std::string &filename, int screen_num); virtual Menu *submenu() { return m_submenu; } /** @@ -150,7 +154,7 @@ private: Menu *m_submenu; ///< a submenu, 0 if we don't have one RefCount m_command; ///< command to be executed bool m_enabled, m_selected; - bool m_toggle_item; + bool m_close_on_click, m_toggle_item; int m_index; struct Icon { diff --git a/src/IntResMenuItem.hh b/src/IntResMenuItem.hh index 768870e..e49681f 100644 --- a/src/IntResMenuItem.hh +++ b/src/IntResMenuItem.hh @@ -37,6 +37,7 @@ public: FbTk::MenuItem(label, host_menu), m_org_label(FbTk::MenuItem::label()), m_max(max_val), m_min(min_val), m_res(res) { updateLabel(); + setCloseOnClick(false); } /* Utility, but doesn't get found in anonymous namespace? */ diff --git a/src/RootCmdMenuItem.cc b/src/RootCmdMenuItem.cc index a52a4d8..0e51b03 100644 --- a/src/RootCmdMenuItem.cc +++ b/src/RootCmdMenuItem.cc @@ -40,6 +40,7 @@ RootCmdMenuItem::RootCmdMenuItem(const FbTk::FbString &label, setwp_cmd(new FbCommands::ExecuteCmd(prog + " \"" + m_filename + "\"")); setCommand(setwp_cmd); setToggleItem(true); + setCloseOnClick(false); } diff --git a/src/StyleMenuItem.cc b/src/StyleMenuItem.cc index b1325e5..7d9954d 100644 --- a/src/StyleMenuItem.cc +++ b/src/StyleMenuItem.cc @@ -39,6 +39,7 @@ StyleMenuItem::StyleMenuItem(const FbTk::FbString &label, const std::string &fil SetStyleCmd(m_filename)); setCommand(setstyle_cmd); setToggleItem(true); + setCloseOnClick(false); } -- cgit v0.11.2