From 6ceb786df329045b49dc4f2a8e7ba76b772746a9 Mon Sep 17 00:00:00 2001 From: Jim Ramsay Date: Thu, 14 Jan 2010 11:49:48 -0500 Subject: Add Menu::setActive and MenuItem::setActive API This provides a virtual function which may be overridden by subclasses so they are notified when a different menu item is highlight. Menu::setActive is called first, with the index of the item that is about to be highlighted, though this may be '-1'. MenuItem::setActive will recieve a boolean which is true if this MenuItem is highlighted and false otherwise. --- src/FbTk/Menu.cc | 25 +++++++++++++++---------- src/FbTk/Menu.hh | 2 ++ src/FbTk/MenuItem.hh | 8 +++++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/FbTk/Menu.cc b/src/FbTk/Menu.cc index ded5dab..d44612e 100644 --- a/src/FbTk/Menu.cc +++ b/src/FbTk/Menu.cc @@ -220,7 +220,7 @@ int Menu::insert(MenuItem *item, int pos) { menuitems.insert(menuitems.begin() + pos, item); fixMenuItemIndices(); if (m_active_index >= pos) - m_active_index++; + setActive(m_active_index + 1); } m_need_update = true; // we need to redraw the menu return menuitems.size(); @@ -274,7 +274,7 @@ int Menu::remove(unsigned int index) { m_which_sub--; if (static_cast(m_active_index) > index) - m_active_index--; + setActive(m_active_index-1); m_need_update = true; // we need to redraw the menu @@ -335,7 +335,7 @@ void Menu::cycleItems(bool reverse) { void Menu::setActiveIndex(int new_index) { // clear the items and close any open submenus int old_active_index = m_active_index; - m_active_index = new_index; + setActive(new_index); if (validIndex(old_active_index) && menuitems[old_active_index] != 0) { if (menuitems[old_active_index]->submenu()) { @@ -361,7 +361,7 @@ void Menu::enterSubmenu() { drawSubmenu(m_active_index); submenu->grabInputFocus(); - submenu->m_active_index = -1; // so we land on 0 after nextItem() + submenu->setActive(-1); // so we land on 0 after nextItem() submenu->cycleItems(false); } @@ -386,11 +386,11 @@ void Menu::updateMenu() { for (size_t i = 1; i < menuitems.size(); i++) { if (validIndex(m_active_index + i) && menuitems[m_active_index + i]->isEnabled()) { - m_active_index += i; + setActive(m_active_index + i); break; } else if (validIndex(m_active_index - i) && menuitems[m_active_index - i]->isEnabled()) { - m_active_index -= i; + setActive(m_active_index - i); break; } } @@ -622,7 +622,7 @@ void Menu::internal_hide(bool first) { // if we have an active index we need to redraw it // as non active int old = m_active_index; - m_active_index = -1; + setActive(-1); clearItem(old); // clear old area from highlight if (shown == this) { @@ -932,7 +932,7 @@ void Menu::buttonReleaseEvent(XButtonEvent &re) { menuitems[w]->click(re.button, re.time, re.state); } else { int old = m_active_index; - m_active_index = w; + setActive(w); clearItem(old); } clearItem(w); @@ -983,7 +983,7 @@ void Menu::motionNotifyEvent(XMotionEvent &me) { if (itmp->isEnabled()) { int old = m_active_index; - m_active_index = w; + setActive(w); clearItem(w); clearItem(old); @@ -1148,7 +1148,7 @@ void Menu::leaveNotifyEvent(XCrossingEvent &ce) { if (validIndex(m_which_sub) && m_active_index != m_which_sub && menuitems[m_which_sub]->submenu()->isVisible()) { int old = m_active_index; - m_active_index = m_which_sub; + setActive(m_which_sub); clearItem(m_active_index); clearItem(old); menuitems[m_which_sub]->submenu()->stopHide(); @@ -1280,6 +1280,8 @@ void Menu::clearItem(int index, bool clear, int search_index) { true, false, item_x, item_y, item_w, item_h); + item->setActive(highlight); + if (search_index < (int)m_matches.size()) drawLine(index, m_type_ahead.stringSize()); } @@ -1313,6 +1315,9 @@ void Menu::highlightItem(int index) { item_x, item_y, item_w, item_h); + MenuItem *item = menuitems[index]; + if (item) + item->setActive(true); } void Menu::resetTypeAhead() { diff --git a/src/FbTk/Menu.hh b/src/FbTk/Menu.hh index 09bdd8c..dfefbc0 100644 --- a/src/FbTk/Menu.hh +++ b/src/FbTk/Menu.hh @@ -188,6 +188,8 @@ protected: virtual void update(FbTk::Subject *); + virtual void setActive(int index) { m_active_index = index; } + private: void openSubmenu(); diff --git a/src/FbTk/MenuItem.hh b/src/FbTk/MenuItem.hh index 9150849..9a7a08f 100644 --- a/src/FbTk/MenuItem.hh +++ b/src/FbTk/MenuItem.hh @@ -46,6 +46,7 @@ public: m_submenu(0), m_enabled(true), m_selected(false), + m_active(false), m_close_on_click(true), m_toggle_item(false) { } @@ -55,6 +56,7 @@ public: m_submenu(0), m_enabled(true), m_selected(false), + m_active(false), m_close_on_click(true), m_toggle_item(false) { } @@ -65,6 +67,7 @@ public: m_submenu(0), m_enabled(true), m_selected(false), + m_active(false), m_close_on_click(true), m_toggle_item(false) { } @@ -76,6 +79,7 @@ public: m_command(cmd), m_enabled(true), m_selected(false), + m_active(false), m_close_on_click(true), m_toggle_item(false) { } @@ -86,6 +90,7 @@ public: m_submenu(submenu), m_enabled(true), m_selected(false), + m_active(false), m_close_on_click(true), m_toggle_item(false) { } @@ -96,6 +101,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; } + virtual void setActive(bool val) { m_active = 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; } @@ -152,7 +158,7 @@ private: Menu *m_menu; ///< the menu we live in 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_enabled, m_selected, m_active; bool m_close_on_click, m_toggle_item; int m_index; -- cgit v0.11.2