From 03ce82a4737b834767c03341db9362ada24c775a Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Wed, 28 Jan 2015 16:02:59 +0100 Subject: Feature: typeahead in menu matches text anywhere This commit implements a tweak to the typeahead feature already existent in fluxbox: If the user opens up a menu and starts typing, fluxbox tries to detect matching menu items and makes them available for quick selection. The typed pattern is now search also in the middle of the text. I opted to strip down the code quite a bit and remove complexity by throwing out FbTk::TypeAhead and FbTk::SearchResult because I do not see the need for a general solution when the only use case for such a feature is in fluxbox' menus. FbTk::ITypeAheadable shrunk down to 2 functions; the whole file might be combined with the code that implements FbTk::Menu::TypeSearch. FbTk::Menu::setIndex() and related code is also gone: the position of each menu item is defined by it's position in the items container. This reduces the mount of book keeping fluxbox has to do. Fewer moving parts is a good thing. It's possible that users start to complaint because they expect their typed pattern to match only at the beginning of the text OR that some demand other tweaks. We will see. This commit also fixes a regression introduced by 8387742c. The bug made the menu vanish. --- src/ClientMenu.cc | 5 +- src/FbTk/ITypeAheadable.hh | 15 -- src/FbTk/Makemodule.am | 3 - src/FbTk/Menu.cc | 504 ++++++++++++++++++++++++++++----------------- src/FbTk/Menu.hh | 49 ++--- src/FbTk/MenuItem.cc | 52 +++-- src/FbTk/MenuItem.hh | 25 +-- src/FbTk/SearchResult.cc | 51 ----- src/FbTk/SearchResult.hh | 55 ----- src/FbTk/TypeAhead.hh | 177 ---------------- 10 files changed, 384 insertions(+), 552 deletions(-) delete mode 100644 src/FbTk/SearchResult.cc delete mode 100644 src/FbTk/SearchResult.hh delete mode 100644 src/FbTk/TypeAhead.hh diff --git a/src/ClientMenu.cc b/src/ClientMenu.cc index 84f2025..1a7417a 100644 --- a/src/ClientMenu.cc +++ b/src/ClientMenu.cc @@ -26,11 +26,12 @@ #include "Window.hh" #include "WindowCmd.hh" #include "FocusControl.hh" -#include #include "FbTk/MenuItem.hh" #include "FbTk/MemFun.hh" +#include + namespace { // anonymous class ClientMenuItem: public FbTk::MenuItem { @@ -159,5 +160,5 @@ void ClientMenu::clientDied(Focusable &win) { // update accordingly if (cl_item) - remove(cl_item->getIndex()); + FbTk::Menu::removeItem(cl_item); } diff --git a/src/FbTk/ITypeAheadable.hh b/src/FbTk/ITypeAheadable.hh index 9c8e819..f3d432f 100644 --- a/src/FbTk/ITypeAheadable.hh +++ b/src/FbTk/ITypeAheadable.hh @@ -24,12 +24,6 @@ #include "FbString.hh" -#ifdef HAVE_CCTYPE - #include -#else - #include -#endif // HAVE_CCTYPE - namespace FbTk { // abstract base class providing access and validation @@ -39,15 +33,6 @@ public: virtual const std::string &iTypeString() const = 0; virtual bool isEnabled() const { return true; } - char iTypeChar(size_t i) const { return iTypeString()[i]; } - bool iTypeCheckStringSize(size_t sz) const { - return (iTypeString().size() > sz); - } - bool iTypeCompareChar(char ch, size_t sz) const { - return (bool)iTypeCheckStringSize(sz) && - tolower(iTypeChar(sz)) == tolower(ch); - } - }; } // end namespace FbTk diff --git a/src/FbTk/Makemodule.am b/src/FbTk/Makemodule.am index 98e8805..57f70f2 100644 --- a/src/FbTk/Makemodule.am +++ b/src/FbTk/Makemodule.am @@ -124,8 +124,6 @@ libFbTk_a_SOURCES = \ src/FbTk/Resource.cc \ src/FbTk/Resource.hh \ src/FbTk/STLUtil.hh \ - src/FbTk/SearchResult.cc \ - src/FbTk/SearchResult.hh \ src/FbTk/Select2nd.hh \ src/FbTk/SelectArg.hh \ src/FbTk/Shape.cc \ @@ -154,7 +152,6 @@ libFbTk_a_SOURCES = \ src/FbTk/Timer.hh \ src/FbTk/Transparent.cc \ src/FbTk/Transparent.hh \ - src/FbTk/TypeAhead.hh \ src/FbTk/Util.hh \ src/FbTk/XFontImp.cc \ src/FbTk/XFontImp.hh \ diff --git a/src/FbTk/Menu.cc b/src/FbTk/Menu.cc index 8076946..246d65d 100644 --- a/src/FbTk/Menu.cc +++ b/src/FbTk/Menu.cc @@ -44,6 +44,8 @@ #include #include +#include + #ifdef DEBUG #include using std::cerr; @@ -67,11 +69,144 @@ void renderMenuPixmap(Pixmap& pm, FbTk::FbWindow* win, int width, int height, co } } + +// finds 'pattern' in 'text', case insensitive. +// returns position or std::string::npos if not found. +// +// implements Boyer–Moore–Horspool +size_t search_string(const std::string& text, const std::string& pattern) { + + if (pattern.empty()) { + return 0; + } + if (text.empty() || pattern.size() > text.size()) { + return std::string::npos; + } + + size_t t; + size_t tlen = text.size(); + + // simple case, no need to be too clever + if (pattern.size() == 1) { + int b = std::tolower(pattern[0]); + for (t = 0; t < tlen; t++) { + if (b == std::tolower(text[t])) { + return t; + } + } + return std::string::npos; + } + + + size_t plast = pattern.size() - 1; + size_t p; + + // prepare skip-table + // + size_t skip[256]; + for (p = 0; p < sizeof(skip)/sizeof(skip[0]); p++) { + skip[p] = plast + 1; + } + for (p = 0; p < plast; p++) { + skip[std::tolower(pattern[p])] = plast - p; + } + + // match + for (t = 0; t + plast < tlen; ) { + for (p = plast; std::tolower(text[t+p]) == std::tolower(pattern[p]); p--) { + if (p == 0) { + return t+p; + } + } + t += skip[text[t+p]]; + } + + return std::string::npos; +} + } // end of anonymous namespace namespace FbTk { +// a small helper which applies search operations on a list of MenuItems*. +// the former incarnation of this class was FbTk::TypeAhead in combination with +// the now non-existent FbTk::SearchResults, but the complexity of these +// are not needed for our use case. as a bonus we have less lose parts +// flying around. +class FbTk::Menu::TypeSearch { +public: + TypeSearch(std::vector& items) : m_items(items) { } + + size_t size() const { return pattern.size(); } + void clear() { pattern.clear(); } + void add(char c) { pattern.push_back(c); } + void backspace() { + size_t s = pattern.size(); + if (s > 0) { + pattern.erase(s - 1, 1); + } + } + + // is 'pattern' matching something? + bool has_match() { + size_t l = m_items.size(); + size_t i; + for (i = 0; i < l; i++) { + if (!m_items[i]->isEnabled()) + continue; + if (search_string(m_items[i]->iTypeString(), pattern) != std::string::npos) { + return true; + } + } + return false; + } + + // would 'the_pattern' match something? + bool would_match(const std::string& the_pattern) { + size_t l = m_items.size(); + size_t i; + for (i = 0; i < l; i++) { + if (!m_items[i]->isEnabled()) + continue; + if (search_string(m_items[i]->iTypeString(), the_pattern) != std::string::npos) { + return true; + } + } + return false; + } + + size_t num_matches() { + size_t l = m_items.size(); + size_t i, n; + for (i = 0, n = 0; i < l; i++) { + if (!m_items[i]->isEnabled()) + continue; + if (search_string(m_items[i]->iTypeString(), pattern) != std::string::npos) { + n++; + } + } + return n; + } + + + // returns true if m_text matches against m_items[i] and stores + // the position where it matches in the string + bool get_match(size_t i, size_t& idx) { + if (i > m_items.size()) { + return false; + } + idx = search_string(m_items[i]->iTypeString(), pattern); + return idx != std::string::npos; + } + + std::string pattern; +private: + const std::vector& m_items; +}; + + + Menu* s_shown = 0; // if there's a menu open at all Menu* s_focused = 0; // holds currently focused menu @@ -115,7 +250,7 @@ Menu::Menu(FbTk::ThemeProxy &tm, ImageControl &imgctrl): m_internal_menu = false; m_state.moving = m_state.closing = m_state.torn = m_state.visible = false; - m_type_ahead.init(m_items); + m_search.reset(new TypeSearch(m_items)); m_x_move = m_y_move = 0; m_which_sub = -1; @@ -148,7 +283,7 @@ Menu::Menu(FbTk::ThemeProxy &tm, ImageControl &imgctrl): FbTk::EventManager &evm = *FbTk::EventManager::instance(); evm.add(*this, m_window); - // strip focus change mask from attrib, since we should only use it with + // strip focus change mask from attrib, since we should only use it with // main window event_mask ^= FocusChangeMask; event_mask |= EnterWindowMask | LeaveWindowMask; @@ -214,11 +349,9 @@ int Menu::insertItem(MenuItem *item, int pos) { if (item == 0) return m_items.size(); if (pos == -1) { - item->setIndex(m_items.size()); m_items.push_back(item); } else { m_items.insert(m_items.begin() + pos, item); - fixMenuItemIndices(); if (m_active_index >= pos) m_active_index++; } @@ -238,13 +371,8 @@ int Menu::findSubmenuIndex(const FbTk::Menu* submenu) const { } -void Menu::fixMenuItemIndices() { - for (size_t i = 0; i < m_items.size(); i++) - m_items[i]->setIndex(i); -} - int Menu::remove(unsigned int index) { - if (index >= m_items.size()) { + if (!validIndex(index)) { #ifdef DEBUG cerr << __FILE__ << "(" << __LINE__ << ") Bad index (" << index << ") given to Menu::remove()" @@ -254,42 +382,66 @@ int Menu::remove(unsigned int index) { return -1; } - Menuitems::iterator it = m_items.begin() + index; - MenuItem *item = (*it); + return removeItem(m_items[index]); +} - if (item) { - if (!m_matches.empty()) - resetTypeAhead(); +int Menu::removeItem(FbTk::MenuItem* item) { + + size_t l = m_items.size(); + size_t i; + size_t found = 0; - m_items.erase(it); + // find index of first occurance of item + for (i = 0; i < l; i++) { + if (m_items[i] == item) { + found++; + } + } + + if (found == 0) { + return l; + } - // avoid O(n^2) algorithm with removeAll() - if (index != m_items.size()) - fixMenuItemIndices(); + // sigh. http://en.wikipedia.org/wiki/Erase-remove_idiom + std::vector::iterator start_erase = std::remove(m_items.begin(), m_items.end(), item); + m_items.erase(start_erase, m_items.end()); + if (item) { Menu* sm = item->submenu(); if (sm) { if (! sm->m_internal_menu) { delete sm; } } - delete item; } - if (static_cast(m_which_sub) == index) + m_need_update = true; + + if (m_items.empty()) { m_which_sub = -1; - else if (static_cast(m_which_sub) > index) - m_which_sub--; + m_active_index = 0; + return 0; + } - if (static_cast(m_active_index) > index) - m_active_index--; + if (static_cast(m_which_sub) == i) + m_which_sub = -1; + else if (static_cast(m_which_sub) > i) + m_which_sub -= found; - m_need_update = true; // we need to redraw the menu + if (static_cast(m_active_index) > i) { + int ai = static_cast(m_active_index) - found; + if (ai >= 0) { + m_active_index = ai; + } else { + m_active_index = 0; + } + } return m_items.size(); } + void Menu::removeAll() { while (!m_items.empty()) remove(m_items.size()-1); @@ -304,39 +456,32 @@ void Menu::lower() { } void Menu::cycleItems(bool reverse) { - Menuitems& items = m_items; - if (m_type_ahead.stringSize()) - items = m_matches; - if (items.empty()) + if (m_items.empty()) return; - // find the next item to select - // this algorithm assumes menuitems are sorted properly - int new_index = -1; - bool passed = !validIndex(m_active_index); - for (size_t i = 0; i < items.size(); i++) { - if (!isItemSelectable(items[i]->getIndex()) || - items[i]->getIndex() == m_active_index) - continue; + int offset = reverse ? -1 : 1; + int l = m_items.size(); + int i = m_active_index; + size_t ignore; - // determine whether or not we've passed the active index - if (!passed && items[i]->getIndex() > m_active_index) { - if (reverse && new_index != -1) - break; - passed = true; + for (i += offset; i != m_active_index; i += offset ) { + if (i < 0) { + i = l - 1; + } else if (i >= l) { + i = 0; + } + + if (!isItemSelectable(i)) { + continue; } - // decide if we want to keep this item - if (passed && !reverse) { - new_index = items[i]->getIndex(); + // empty-string ("nothing was typed") matches always + if (m_search->get_match(i, ignore)) { + setActiveIndex(i); break; - } else if (reverse || new_index == -1) - new_index = items[i]->getIndex(); + } } - - if (new_index != -1) - setActiveIndex(new_index); } void Menu::setActiveIndex(int new_index) { @@ -403,10 +548,10 @@ void Menu::updateMenu() { } unsigned int ii = 0; - Menuitems::iterator it = m_items.begin(); - Menuitems::iterator it_end = m_items.end(); - for (; it != it_end; ++it) { - ii = (*it)->width(theme()); + size_t l = m_items.size(); + size_t i; + for (i = 0; i < l; i++) { + ii = m_items[i]->width(theme()); m_item_w = (ii > m_item_w ? ii : m_item_w); } @@ -470,9 +615,9 @@ void Menu::updateMenu() { // render image, disable cache and let the theme remove the pixmap theme()->setSelectedPixmap(m_image_ctrl. renderImage(hw, hw, - theme()->hiliteTexture(), ROT0, + theme()->hiliteTexture(), ROT0, false // no cache - ), + ), false); // the theme takes care of this pixmap if (!theme()->highlightSelectedPixmap().pixmap().drawable()) { @@ -480,9 +625,9 @@ void Menu::updateMenu() { // render image, disable cache and let the theme remove the pixmap theme()->setHighlightSelectedPixmap(m_image_ctrl. renderImage(hw, hw, - theme()->frameTexture(), ROT0, + theme()->frameTexture(), ROT0, false // no cache - ), + ), false); // theme takes care of this pixmap } } @@ -524,8 +669,7 @@ void Menu::show() { if (m_need_update) updateMenu(); - m_type_ahead.reset(); - m_matches.clear(); + m_search->clear(); m_window.showSubwindows(); m_window.show(); @@ -576,7 +720,9 @@ void Menu::clearWindow() { m_frame.win.clear(); // clear foreground bits of frame items - for (size_t i = 0; i < m_items.size(); i++) { + size_t i; + size_t l = m_items.size(); + for (i = 0; i < l; i++) { clearItem(i, false); // no clear } m_shape->update(); @@ -758,12 +904,9 @@ void Menu::drawSubmenu(unsigned int index) { int Menu::drawItem(FbDrawable &drawable, unsigned int index, bool highlight, bool exclusive_drawable) { - if (index >= m_items.size() || m_items.empty() || - m_rows_per_column == 0) + if (!validIndex(index) || m_items.empty()) { return 0; - - MenuItem *item = m_items[index]; - if (! item) return 0; + } // ensure we do not divide by 0 and thus cause a SIGFPE if (m_rows_per_column == 0) { @@ -774,6 +917,10 @@ int Menu::drawItem(FbDrawable &drawable, unsigned int index, return 0; } + MenuItem *item = m_items[index]; + if (!item) + return 0; + int column = index / m_rows_per_column; int row = index - (column * m_rows_per_column); int item_x = (column * m_item_w); @@ -797,8 +944,9 @@ void Menu::setLabel(const FbTk::BiDiString &labelstr) { void Menu::setItemSelected(unsigned int index, bool sel) { - if (index >= m_items.size()) return; - + if (!validIndex(index)) { + return; + } MenuItem *item = find(index); if (! item) return; @@ -807,8 +955,9 @@ void Menu::setItemSelected(unsigned int index, bool sel) { bool Menu::isItemSelected(unsigned int index) const{ - if (index >= m_items.size()) return false; - + if (!validIndex(index)) { + return false; + } const MenuItem *item = find(index); if (!item) return false; @@ -818,29 +967,30 @@ bool Menu::isItemSelected(unsigned int index) const{ void Menu::setItemEnabled(unsigned int index, bool enable) { - if (index >= m_items.size()) return; - + if (!validIndex(index)) { + return; + } MenuItem *item = find(index); - if (! item) return; - - item->setEnabled(enable); + if (item) { + item->setEnabled(enable); + } } bool Menu::isItemEnabled(unsigned int index) const { - if (index >= m_items.size()) return false; - + if (!validIndex(index)) { + return false; + } const MenuItem *item = find(index); if (!item) return false; - return item->isEnabled(); } bool Menu::isItemSelectable(unsigned int index) const { - - if (index >= m_items.size()) return false; - + if (!validIndex(index)) { + return false; + } const MenuItem *item = find(index); return (!item || !item->isEnabled()) ? false : true; } @@ -876,7 +1026,7 @@ void Menu::buttonPressEvent(XButtonEvent &be) { int i = (be.y / theme()->itemHeight()); int w = (column * m_rows_per_column) + i; - if (validIndex(w) && isItemSelectable(static_cast(w))) { + if (isItemSelectable(static_cast(w))) { MenuItem *item = m_items[w]; if (item->submenu()) { @@ -1006,7 +1156,7 @@ void Menu::motionNotifyEvent(XMotionEvent &me) { void Menu::exposeEvent(XExposeEvent &ee) { // some xservers (eg: nxserver) send XExposeEvent for the unmapped menu. - // this caused a SIGFPE in ::clearItem(), since m_rows_per_column is + // this caused a SIGFPE in ::clearItem(), since m_rows_per_column is // still 0 -> division by 0. // // it is still unclear, why nxserver behaves this way @@ -1040,9 +1190,9 @@ void Menu::exposeEvent(XExposeEvent &ee) { int index = id + i * m_rows_per_column; if (index < static_cast(m_items.size()) && index >= 0) { - Menuitems::iterator it = m_items.begin() + index; - Menuitems::iterator it_end = m_items.end(); - for (ii = id; ii <= id_d && it != it_end; ++it, ii++) { + size_t l = m_items.size(); + size_t j; + for (j = 0, ii = id; ii <= id_d && j < l; i++, ii++) { int index = ii + (i * m_rows_per_column); // redraw the item clearItem(index); @@ -1063,11 +1213,11 @@ void Menu::keyPressEvent(XKeyEvent &event) { switch (ks) { case XK_Up: resetTypeAhead(); - cycleItems(true); + cycleItems(UP); break; case XK_Down: resetTypeAhead(); - cycleItems(false); + cycleItems(DOWN); break; case XK_Left: // enter parent if we have one resetTypeAhead(); @@ -1093,17 +1243,16 @@ void Menu::keyPressEvent(XKeyEvent &event) { enterSubmenu(); break; case XK_Escape: // close menu - m_type_ahead.reset(); + m_search->clear(); m_state.torn = false; hide(true); break; case XK_BackSpace: - if (m_type_ahead.stringSize() == 0) { + if (m_search->size() == 0) { internal_hide(); break; } - - m_type_ahead.putBackSpace(); + m_search->backspace(); drawTypeAheadItems(); break; case XK_KP_Enter: @@ -1123,25 +1272,29 @@ void Menu::keyPressEvent(XKeyEvent &event) { } break; case XK_Tab: - case XK_ISO_Left_Tab: + case XK_ISO_Left_Tab: // XXX: number matches == 1 -> enter submenu if (validIndex(m_active_index) && isItemEnabled(m_active_index) && - m_items[m_active_index]->submenu() && m_matches.size() == 1) { + m_items[m_active_index]->submenu() && m_search->num_matches() == 1) { enterSubmenu(); - m_type_ahead.reset(); + m_search->clear(); } else { - m_type_ahead.seek(); cycleItems((bool)(event.state & ShiftMask)); } drawTypeAheadItems(); break; default: - m_type_ahead.putCharacter(keychar[0]); - // if current item doesn't match new search string, find the next one - drawTypeAheadItems(); - if (!m_matches.empty() && (!validIndex(m_active_index) || - std::find(m_matches.begin(), m_matches.end(), - find(m_active_index)) == m_matches.end())) - cycleItems(false); + + if (m_search->would_match(m_search->pattern + keychar[0])) { + m_search->add(keychar[0]); + drawTypeAheadItems(); + // if current item doesn't match new search string, find + // the next one + size_t ignore; + if (!m_search->get_match(m_active_index, ignore)) { + cycleItems(DOWN); + } + } + break; } } @@ -1160,26 +1313,30 @@ void Menu::leaveNotifyEvent(XCrossingEvent &ce) { } void Menu::reconfigure() { + m_shape->setPlaces(theme()->shapePlaces()); + FbTk::Color bc = theme()->borderColor(); + int bw = theme()->borderWidth(); + int alpha = this->alpha(); + int opaque = 255; + if (FbTk::Transparent::haveComposite()) { - m_window.setOpaque(alpha()); - m_title.win.setAlpha(255); - m_frame.win.setAlpha(255); - } else { - m_window.setOpaque(255); - m_title.win.setAlpha(alpha()); - m_frame.win.setAlpha(alpha()); + std::swap(opaque, alpha); } + m_window.setOpaque(opaque); + m_title.win.setAlpha(alpha); + m_frame.win.setAlpha(alpha); + m_need_update = true; // redraw items - m_window.setBorderColor(theme()->borderColor()); - m_title.win.setBorderColor(theme()->borderColor()); - m_frame.win.setBorderColor(theme()->borderColor()); + m_window.setBorderColor(bc); + m_title.win.setBorderColor(bc); + m_frame.win.setBorderColor(bc); - m_window.setBorderWidth(theme()->borderWidth()); - m_title.win.setBorderWidth(theme()->borderWidth()); + m_window.setBorderWidth(bw); + m_title.win.setBorderWidth(bw); updateMenu(); } @@ -1187,19 +1344,19 @@ void Menu::reconfigure() { void Menu::openSubmenu() { - int item = m_active_index; - if (!isVisible() || !validIndex(item) || !m_items[item]->isEnabled() || + size_t i = m_active_index; + if (!isVisible() || !validIndex(i) || !m_items[i]->isEnabled() || (s_focused != this && s_focused && s_focused->isVisible())) return; - clearItem(item); + clearItem(i); - if (m_items[item]->submenu() != 0) { + if (m_items[i]->submenu() != 0) { // stop hide timer, so it doesnt hides the menu if we // have the same submenu as the last shown submenu // (window menu for clients inside workspacemenu for example) - m_items[item]->submenu()->m_hide_timer.stop(); - drawSubmenu(item); + m_items[i]->submenu()->m_hide_timer.stop(); + drawSubmenu(i); } } @@ -1222,10 +1379,10 @@ void Menu::themeReconfigured() { m_need_update = true; - Menuitems::iterator it = m_items.begin(); - Menuitems::iterator it_end = m_items.end(); - for (; it != it_end; ++it) { - (*it)->updateTheme(theme()); + size_t l = m_items.size(); + size_t i; + for (i = 0; i < l; i++) { + m_items[i]->updateTheme(theme()); } reconfigure(); } @@ -1265,35 +1422,47 @@ void Menu::clearItem(int index, bool clear, int search_index) { int column = index / m_rows_per_column; int row = index - (column * m_rows_per_column); - unsigned int item_w = m_item_w; - unsigned int item_h = theme()->itemHeight(); + int item_w = m_item_w; + int item_h = theme()->itemHeight(); int item_x = (column * item_w); int item_y = (row * item_h); bool highlight = (index == m_active_index && isItemSelectable(index)); - if (search_index < 0) // need to underline the item - search_index = std::find(m_matches.begin(), m_matches.end(), - find(index)) - m_matches.begin(); + size_t start_idx = std::string::npos; + size_t end_idx = std::string::npos; + + if (m_search->get_match(index, start_idx)) { + end_idx = start_idx + m_search->size(); + +#if 0 + std::cerr << "m_search " << index << "|" + << m_items[index]->iTypeString() << "|" << m_search->text + << "|" << start_idx << " " << end_idx << "\n"; +#endif + } // don't highlight if moving, doesn't work with alpha on if (highlight && !m_state.moving) { highlightItem(index); - if (search_index < static_cast(m_matches.size())) - drawLine(index, m_type_ahead.stringSize()); + if (start_idx != end_idx) { // need a underline (aka "matched item") + m_items[index]->drawLine(m_frame.win, theme(), + end_idx - start_idx, item_x, item_y, m_item_w, start_idx); + } return; - } else if (clear) - m_frame.win.clearArea(item_x, item_y, item_w, item_h); + } - MenuItem* item = m_items[index]; - if (!item) - return; + if (clear) { + m_frame.win.clearArea(item_x, item_y, item_w, item_h); + } - item->draw(m_frame.win, theme(), highlight, - true, false, item_x, item_y, - item_w, item_h); + m_items[index]->draw(m_frame.win, theme(), highlight, + true, false, item_x, item_y, + item_w, item_h); - if (search_index < static_cast(m_matches.size())) - drawLine(index, m_type_ahead.stringSize()); + if (start_idx != end_idx) { // need a underline (aka "matched item") + m_items[index]->drawLine(m_frame.win, theme(), + end_idx - start_idx, item_x, item_y, m_item_w, start_idx); + } } // Area must have been cleared before calling highlight @@ -1310,8 +1479,8 @@ void Menu::highlightItem(int index) { int column = index / m_rows_per_column; int row = index - (column * m_rows_per_column); - unsigned int item_w = m_item_w; - unsigned int item_h = theme()->itemHeight(); + int item_w = m_item_w; + int item_h = theme()->itemHeight(); int item_x = (column * m_item_w); int item_y = (row * item_h); FbPixmap buffer = FbPixmap(m_frame.win, item_w, item_h, m_frame.win.depth()); @@ -1340,46 +1509,15 @@ void Menu::highlightItem(int index) { } void Menu::resetTypeAhead() { - Menuitems vec = m_matches; - Menuitems::iterator it = vec.begin(); - m_type_ahead.reset(); - m_matches.clear(); - - for (; it != vec.end(); ++it) - clearItem((*it)->getIndex(), true, 1); + m_search->clear(); + drawTypeAheadItems(); } void Menu::drawTypeAheadItems() { - // remove underlines from old matches - for (size_t i = 0; i < m_matches.size(); i++) - clearItem(m_matches[i]->getIndex(), true, m_matches.size()); - - m_matches = m_type_ahead.matched(); - for (size_t j = 0; j < m_matches.size(); j++) - clearItem(m_matches[j]->getIndex(), false, j); -} - -// underline menuitem[index] with respect to matchstringsize size -void Menu::drawLine(int index, int size){ - if (!validIndex(index)) - return; - - // ensure we do not divide by 0 and thus cause a SIGFPE - if (m_rows_per_column == 0) { -#if DEBUG - cerr << __FILE__ << "(" << __LINE__ - << ") Error: m_rows_per_column == 0 in FbTk::Menu::drawLine()\n"; -#endif - return; + size_t i; + for (i = 0; i < m_items.size(); i++) { + clearItem(i, true); } - - int column = index / m_rows_per_column; - int row = index - (column * m_rows_per_column); - int item_x = (column * m_item_w); - int item_y = (row * theme()->itemHeight()); - - FbTk::MenuItem *item = find(index); - item->drawLine(m_frame.win, theme(), size, item_x, item_y, m_item_w); } void Menu::setTitleVisibility(bool b) { @@ -1391,8 +1529,4 @@ void Menu::setTitleVisibility(bool b) { titleWindow().raise(); } - - - - } // end namespace FbTk diff --git a/src/FbTk/Menu.hh b/src/FbTk/Menu.hh index 9716a89..39440a8 100644 --- a/src/FbTk/Menu.hh +++ b/src/FbTk/Menu.hh @@ -33,7 +33,6 @@ #include "EventHandler.hh" #include "MenuTheme.hh" #include "Timer.hh" -#include "TypeAhead.hh" namespace FbTk { @@ -53,6 +52,7 @@ public: enum Alignment{ ALIGNDONTCARE = 1, ALIGNTOP, ALIGNBOTTOM }; enum { RIGHT = 1, LEFT }; + enum { UP = 1, DOWN = 0 }; /** Bullet type @@ -71,6 +71,7 @@ public: int insertSubmenu(const FbString &label, Menu *submenu, int pos= -1); int insertItem(MenuItem *item, int pos=-1); int remove(unsigned int item); + int removeItem(MenuItem* item); void removeAll(); void setInternalMenu(bool val = true) { m_internal_menu = val; } void setAlignment(Alignment a) { m_alignment = a; } @@ -180,24 +181,16 @@ private: void startHide(); void stopHide(); - FbTk::ThemeProxy &m_theme; - Menu *m_parent; - ImageControl &m_image_ctrl; - - typedef std::vector Menuitems; - Menuitems m_items; - TypeAhead m_type_ahead; - Menuitems m_matches; - void resetTypeAhead(); void drawTypeAheadItems(); - void drawLine(int index, int size); - void fixMenuItemIndices(); - struct Rect { - int x, y; - unsigned int width, height; - } m_screen; + + Menu *m_parent; + + class TypeSearch; + + std::vector m_items; + std::auto_ptr m_search; struct State { bool moving; @@ -206,14 +199,20 @@ private: bool torn; // torn from parent } m_state; + bool m_need_update; bool m_internal_menu; ///< whether we should destroy this menu or if it's managed somewhere else + int m_active_index; ///< current highlighted index int m_which_sub; + int m_x_move; + int m_y_move; - Alignment m_alignment; + struct Rect { + int x, y; + unsigned int width, height; + } m_screen; // the menu window FbTk::FbWindow m_window; - Pixmap m_hilite_pixmap; // the title struct Title { @@ -231,24 +230,20 @@ private: } m_frame; - int m_x_move; - int m_y_move; - // the menuitems are rendered in a grid with // 'm_columns' (a minimum of 'm_min_columns') and // a max of 'm_rows_per_column' int m_columns; int m_rows_per_column; int m_min_columns; - unsigned int m_item_w; - int m_active_index; ///< current highlighted index - - // the corners - std::auto_ptr m_shape; + FbTk::ThemeProxy& m_theme; + ImageControl& m_image_ctrl; + std::auto_ptr m_shape; // the corners + Pixmap m_hilite_pixmap; + Alignment m_alignment; - bool m_need_update; Timer m_submenu_timer; Timer m_hide_timer; diff --git a/src/FbTk/MenuItem.cc b/src/FbTk/MenuItem.cc index c128c57..3918a96 100644 --- a/src/FbTk/MenuItem.cc +++ b/src/FbTk/MenuItem.cc @@ -28,10 +28,13 @@ #include "App.hh" #include "StringUtil.hh" #include "Menu.hh" + #include namespace FbTk { +MenuItem::~MenuItem() { } + void MenuItem::click(int button, int time, unsigned int mods) { if (m_command.get() != 0) { if (m_menu && m_close_on_click && (mods & ControlMask) == 0) @@ -43,41 +46,50 @@ void MenuItem::click(int button, int time, unsigned int mods) { } void MenuItem::drawLine(FbDrawable &draw, - const FbTk::ThemeProxy &theme, size_t size, - int text_x, int text_y, unsigned int width) const { - - unsigned int height = theme->itemHeight(); - int bevelW = theme->bevelWidth(); + const FbTk::ThemeProxy &theme, size_t n_chars, + int text_x, int text_y, unsigned int width, + size_t skip_chars) const { - int font_top = (height - theme->hiliteFont().height())/2; - int underline_height = font_top + theme->hiliteFont().ascent() + 2; - int bottom = height - bevelW - 1; - - text_y += bottom > underline_height ? underline_height : bottom; + // avoid drawing an ugly dot + if (n_chars == 0) { + return; + } - int text_w = theme->hiliteFont().textWidth(label()); + const FbString& text = m_label.visual(); + const size_t n = std::min(n_chars, text.size()); + const FbTk::Font& font = theme->hiliteFont(); + int font_height = static_cast(font.height()); + int height = static_cast(theme->itemHeight()); + int font_top = (height - font_height)/2; + int bevel_width = static_cast(theme->bevelWidth()); + int underline_height = font_top + font.ascent() + 2; + int bottom = height - bevel_width - 1; + int text_w = font.textWidth(label()); + int text_pixels = font.textWidth(text.c_str()+skip_chars, n); + int skip_pixels = 0; + if (skip_chars > 0) { + skip_pixels = font.textWidth(text.c_str(), skip_chars); + } - const FbString& visual = m_label.visual(); - BiDiString search_string(FbString(visual, 0, size > visual.size() ? visual.size() : size)); - int search_string_w = theme->hiliteFont().textWidth(search_string); + text_y += std::min(bottom, underline_height); // pay attention to the text justification switch(theme->hiliteFontJustify()) { case FbTk::LEFT: - text_x += bevelW + height + 1; + text_x += bevel_width + height + 1; break; case FbTk::RIGHT: - text_x += width - (height + bevelW + text_w); + text_x += width - (height + bevel_width + text_w); break; default: //center text_x += ((width + 1 - text_w) / 2); break; } - // avoid drawing an ugly dot - if (size != 0) - draw.drawLine(theme->hiliteUnderlineGC().gc(), - text_x, text_y, text_x + search_string_w, text_y); + text_x += skip_pixels; + + draw.drawLine(theme->hiliteUnderlineGC().gc(), + text_x, text_y, text_x + text_pixels, text_y); } diff --git a/src/FbTk/MenuItem.hh b/src/FbTk/MenuItem.hh index 320d065..4bbfcf3 100644 --- a/src/FbTk/MenuItem.hh +++ b/src/FbTk/MenuItem.hh @@ -47,8 +47,7 @@ public: m_enabled(true), m_selected(false), m_close_on_click(true), - m_toggle_item(false), - m_index(0) + m_toggle_item(false) { } explicit MenuItem(const BiDiString &label) @@ -58,8 +57,7 @@ public: m_enabled(true), m_selected(false), m_close_on_click(true), - m_toggle_item(false), - m_index(0) + m_toggle_item(false) { } MenuItem(const BiDiString &label, Menu &host_menu) @@ -69,8 +67,7 @@ public: m_enabled(true), m_selected(false), m_close_on_click(true), - m_toggle_item(false), - m_index(0) + m_toggle_item(false) { } /// create a menu item with a specific command to be executed on click MenuItem(const BiDiString &label, RefCount > &cmd, Menu *menu = 0) @@ -81,8 +78,7 @@ public: m_enabled(true), m_selected(false), m_close_on_click(true), - m_toggle_item(false), - m_index(0) + m_toggle_item(false) { } MenuItem(const BiDiString &label, Menu *submenu, Menu *host_menu = 0) @@ -92,10 +88,9 @@ public: m_enabled(true), m_selected(false), m_close_on_click(true), - m_toggle_item(false), - m_index(0) + m_toggle_item(false) { } - virtual ~MenuItem() { } + virtual ~MenuItem(); void setCommand(RefCount > &cmd) { m_command = cmd; } virtual void setSelected(bool selected) { m_selected = selected; } @@ -119,14 +114,12 @@ public: virtual bool isToggleItem() const { return m_toggle_item; } // iType functions - virtual void setIndex(int index) { m_index = index; } - virtual int getIndex() { return m_index; } const FbString &iTypeString() const { return m_label.visual(); } virtual void drawLine(FbDrawable &draw, const FbTk::ThemeProxy &theme, - size_t size, + size_t n_chars, int text_x, int text_y, - unsigned int width) const; + unsigned int width, size_t skip_chars = 0) const; virtual unsigned int width(const FbTk::ThemeProxy &theme) const; virtual unsigned int height(const FbTk::ThemeProxy &theme) const; @@ -160,14 +153,12 @@ private: RefCount > m_command; ///< command to be executed bool m_enabled, m_selected; bool m_close_on_click, m_toggle_item; - int m_index; struct Icon { std::auto_ptr pixmap; std::string filename; }; std::auto_ptr m_icon; - }; } // end namespace FbTk diff --git a/src/FbTk/SearchResult.cc b/src/FbTk/SearchResult.cc deleted file mode 100644 index 68d9ecf..0000000 --- a/src/FbTk/SearchResult.cc +++ /dev/null @@ -1,51 +0,0 @@ -// SearchResult.cc for FbTk - Fluxbox Toolkit -// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -#include "SearchResult.hh" -#include "ITypeAheadable.hh" - -namespace FbTk { - -void SearchResult::seek() { - switch (m_results.size()) { - case 0: - break; - case 1: - m_seeked_string = m_results[0]->iTypeString(); - break; - default: - bool seekforward = true; - for (size_t i=1; i < m_results.size() && seekforward && - m_results[0]->iTypeCheckStringSize(m_seeked_string.size()); i++) { - if (!m_results[i]->iTypeCompareChar( - m_results[0]->iTypeChar(m_seeked_string.size()), - m_seeked_string.size())) { - seekforward = false; - } else if (i == m_results.size() - 1) { - m_seeked_string += m_results[0]->iTypeChar(m_seeked_string.size()); - i = 0; - } - } - break; - } -} - -} // end namespace FbTk diff --git a/src/FbTk/SearchResult.hh b/src/FbTk/SearchResult.hh deleted file mode 100644 index a28249c..0000000 --- a/src/FbTk/SearchResult.hh +++ /dev/null @@ -1,55 +0,0 @@ -// SearchResult.hh for FbTk - Fluxbox Toolkit -// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -#ifndef FBTK_SEARCHRESULT_HH -#define FBTK_SEARCHRESULT_HH - -#include -#include - -namespace FbTk { - -class ITypeAheadable; - -class SearchResult { -public: - typedef std::vector < ITypeAheadable* > BaseItems; - typedef BaseItems::iterator BaseItemsIt; - - SearchResult(const std::string &to_search_for): - m_seeked_string(to_search_for) { } - - void add(ITypeAheadable* item) { m_results.push_back(item); } - size_t size() const { return m_results.size(); } - const BaseItems& result() const { return m_results; } - const std::string& seekedString() const { return m_seeked_string; } - - void seek(); - -private: - BaseItems m_results; - std::string m_seeked_string; - -}; - -} // end namespace FbTk - -#endif // FBTK_SEARCHRESULT_HH diff --git a/src/FbTk/TypeAhead.hh b/src/FbTk/TypeAhead.hh deleted file mode 100644 index 119e3af..0000000 --- a/src/FbTk/TypeAhead.hh +++ /dev/null @@ -1,177 +0,0 @@ -// TypeAhead.hh for FbTk - Fluxbox Toolkit -// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -#ifndef FBTK_TYPEAHEAD_HH -#define FBTK_TYPEAHEAD_HH - -#include "ITypeAheadable.hh" -#include "SearchResult.hh" - -namespace FbTk { - -template -class TypeAhead { -#if 0 - -// a class template can't be split into separate interface + implementation files, an interface summary is given here: - -public: - void init(Items const &items); - -// accessors: - int stringSize() const { return m_searchstr.size(); } - Items matched() const; - -// modifiers: - Items putCharacter(char ch); - void putBackSpace(); - void reset() - -private: - SearchResults m_search_results; - std::string m_searchstr; - Items const *m_ref; - -// helper - void fillValues(BaseItems const &search, ValueVec &fillin) const; - -// reverts to searchstate before current - void revert(); - -// search performs iteration and sets state - void search(char char_to_test); - void doSearch(char to_test, - Items const &items, - SearchResult &mySearchResult) const; - void doSearch(char to_test, - BaseItems const &search, - SearchResult &mySearchResult) const; -#endif - -public: - typedef std::vector < ITypeAheadable* > BaseItems; - typedef BaseItems::const_iterator BaseItemscIt; - typedef std::vector < SearchResult > SearchResults; - typedef typename Items::const_iterator ItemscIt; - - TypeAhead() : m_ref(0) { } - - void init(Items const &items) { m_ref = &items; } - - size_t stringSize() const { return m_searchstr.size(); } - - void seek() { - if (!m_search_results.empty()) - m_searchstr = m_search_results.back().seekedString(); - } - - Items putCharacter(char ch) { - if (isprint(ch)) - search(ch); - return matched(); - } - - void putBackSpace() { - if (!m_search_results.empty()) - revert(); - } - - void reset() { - m_searchstr.clear(); - m_search_results.clear(); - } - - Items matched() const { - Items last_matched; - - if (!m_search_results.empty()) - fillValues(m_search_results.back().result(), last_matched); - else - return *m_ref; - return last_matched; - } - -private: - SearchResults m_search_results; - std::string m_searchstr; - Items const *m_ref; // reference to vector we are operating on - - void fillValues(BaseItems const &search, Items &fillin) const { - for (BaseItemscIt it = search.begin(); it != search.end(); ++it) { - Item_Type tmp = dynamic_cast(*it); - if (tmp) - fillin.push_back(tmp); - } - } - - void revert() { - m_search_results.pop_back(); - if (m_search_results.empty()) - m_searchstr.clear(); - else - m_searchstr = m_search_results.back().seekedString(); - } - - void search(char char_to_test) { - SearchResult mySearchResult(m_searchstr + char_to_test); - size_t num_items = m_ref->size(); - - // check if we have already a searched set - if (m_search_results.empty()) - doSearch(char_to_test, *m_ref, mySearchResult); - else { - num_items = m_search_results.back().size(); - doSearch(char_to_test, m_search_results.back().result(), - mySearchResult); - } - - if (mySearchResult.size() > 0 ) { - if (mySearchResult.size() < num_items) { - mySearchResult.seek(); - m_search_results.push_back(mySearchResult); - } - m_searchstr += char_to_test; - } - } - - // iteration based on original list of items - void doSearch(char to_test, Items const &items, - SearchResult &mySearchResult) const { - for (ItemscIt it = items.begin(); it != items.end(); ++it) { - if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled()) - mySearchResult.add(*it); - } - } - - // iteration based on last SearchResult - void doSearch(char to_test, BaseItems const &search, - SearchResult &mySearchResult) const { - for (BaseItemscIt it = search.begin(); it != search.end(); ++it) { - if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled()) - mySearchResult.add(*it); - } - } - -}; // end Class TypeAhead - -} // end namespace FbTk - -#endif // FBTK_TYPEAHEAD_HH -- cgit v0.11.2