aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/MenuItem.cc
diff options
context:
space:
mode:
authorMathias Gumz <akira@fluxbox.org>2015-01-28 15:02:59 (GMT)
committerMathias Gumz <akira@fluxbox.org>2015-01-28 15:02:59 (GMT)
commit03ce82a4737b834767c03341db9362ada24c775a (patch)
tree2444031bbd47d97b1e0af64aa2f1d2a037a08a2e /src/FbTk/MenuItem.cc
parentfc245408d6975d0813cd4440e7089d987b54d42e (diff)
downloadfluxbox-03ce82a4737b834767c03341db9362ada24c775a.zip
fluxbox-03ce82a4737b834767c03341db9362ada24c775a.tar.bz2
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.
Diffstat (limited to 'src/FbTk/MenuItem.cc')
-rw-r--r--src/FbTk/MenuItem.cc52
1 files changed, 32 insertions, 20 deletions
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 @@
28#include "App.hh" 28#include "App.hh"
29#include "StringUtil.hh" 29#include "StringUtil.hh"
30#include "Menu.hh" 30#include "Menu.hh"
31
31#include <X11/keysym.h> 32#include <X11/keysym.h>
32 33
33namespace FbTk { 34namespace FbTk {
34 35
36MenuItem::~MenuItem() { }
37
35void MenuItem::click(int button, int time, unsigned int mods) { 38void MenuItem::click(int button, int time, unsigned int mods) {
36 if (m_command.get() != 0) { 39 if (m_command.get() != 0) {
37 if (m_menu && m_close_on_click && (mods & ControlMask) == 0) 40 if (m_menu && m_close_on_click && (mods & ControlMask) == 0)
@@ -43,41 +46,50 @@ void MenuItem::click(int button, int time, unsigned int mods) {
43} 46}
44 47
45void MenuItem::drawLine(FbDrawable &draw, 48void MenuItem::drawLine(FbDrawable &draw,
46 const FbTk::ThemeProxy<MenuTheme> &theme, size_t size, 49 const FbTk::ThemeProxy<MenuTheme> &theme, size_t n_chars,
47 int text_x, int text_y, unsigned int width) const { 50 int text_x, int text_y, unsigned int width,
48 51 size_t skip_chars) const {
49 unsigned int height = theme->itemHeight();
50 int bevelW = theme->bevelWidth();
51 52
52 int font_top = (height - theme->hiliteFont().height())/2; 53 // avoid drawing an ugly dot
53 int underline_height = font_top + theme->hiliteFont().ascent() + 2; 54 if (n_chars == 0) {
54 int bottom = height - bevelW - 1; 55 return;
55 56 }
56 text_y += bottom > underline_height ? underline_height : bottom;
57 57
58 int text_w = theme->hiliteFont().textWidth(label()); 58 const FbString& text = m_label.visual();
59 const size_t n = std::min(n_chars, text.size());
60 const FbTk::Font& font = theme->hiliteFont();
61 int font_height = static_cast<int>(font.height());
62 int height = static_cast<int>(theme->itemHeight());
63 int font_top = (height - font_height)/2;
64 int bevel_width = static_cast<int>(theme->bevelWidth());
65 int underline_height = font_top + font.ascent() + 2;
66 int bottom = height - bevel_width - 1;
67 int text_w = font.textWidth(label());
68 int text_pixels = font.textWidth(text.c_str()+skip_chars, n);
69 int skip_pixels = 0;
70 if (skip_chars > 0) {
71 skip_pixels = font.textWidth(text.c_str(), skip_chars);
72 }
59 73
60 const FbString& visual = m_label.visual(); 74 text_y += std::min(bottom, underline_height);
61 BiDiString search_string(FbString(visual, 0, size > visual.size() ? visual.size() : size));
62 int search_string_w = theme->hiliteFont().textWidth(search_string);
63 75
64 // pay attention to the text justification 76 // pay attention to the text justification
65 switch(theme->hiliteFontJustify()) { 77 switch(theme->hiliteFontJustify()) {
66 case FbTk::LEFT: 78 case FbTk::LEFT:
67 text_x += bevelW + height + 1; 79 text_x += bevel_width + height + 1;
68 break; 80 break;
69 case FbTk::RIGHT: 81 case FbTk::RIGHT:
70 text_x += width - (height + bevelW + text_w); 82 text_x += width - (height + bevel_width + text_w);
71 break; 83 break;
72 default: //center 84 default: //center
73 text_x += ((width + 1 - text_w) / 2); 85 text_x += ((width + 1 - text_w) / 2);
74 break; 86 break;
75 } 87 }
76 88
77 // avoid drawing an ugly dot 89 text_x += skip_pixels;
78 if (size != 0) 90
79 draw.drawLine(theme->hiliteUnderlineGC().gc(), 91 draw.drawLine(theme->hiliteUnderlineGC().gc(),
80 text_x, text_y, text_x + search_string_w, text_y); 92 text_x, text_y, text_x + text_pixels, text_y);
81 93
82} 94}
83 95