diff options
Diffstat (limited to 'src/FbTk/Menu.hh')
-rw-r--r-- | src/FbTk/Menu.hh | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/src/FbTk/Menu.hh b/src/FbTk/Menu.hh new file mode 100644 index 0000000..7c8b6ef --- /dev/null +++ b/src/FbTk/Menu.hh | |||
@@ -0,0 +1,222 @@ | |||
1 | // Menu.hh for FbTk - Fluxbox Toolkit | ||
2 | // Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // | ||
4 | // Basemenu.hh for Blackbox - an X11 Window manager | ||
5 | // Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net) | ||
6 | // | ||
7 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
8 | // copy of this software and associated documentation files (the "Software"), | ||
9 | // to deal in the Software without restriction, including without limitation | ||
10 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
11 | // and/or sell copies of the Software, and to permit persons to whom the | ||
12 | // Software is furnished to do so, subject to the following conditions: | ||
13 | // | ||
14 | // The above copyright notice and this permission notice shall be included in | ||
15 | // all copies or substantial portions of the Software. | ||
16 | // | ||
17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
20 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
22 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
23 | // DEALINGS IN THE SOFTWARE. | ||
24 | |||
25 | // $Id: Menu.hh,v 1.29 2003/12/18 18:03:23 fluxgen Exp $ | ||
26 | |||
27 | #ifndef FBTK_MENU_HH | ||
28 | #define FBTK_MENU_HH | ||
29 | |||
30 | #include <X11/Xlib.h> | ||
31 | #include <vector> | ||
32 | #include <string> | ||
33 | #include <memory> | ||
34 | |||
35 | #include "FbWindow.hh" | ||
36 | #include "EventHandler.hh" | ||
37 | #include "RefCount.hh" | ||
38 | #include "Command.hh" | ||
39 | #include "Observer.hh" | ||
40 | #include "FbPixmap.hh" | ||
41 | #include "MenuTheme.hh" | ||
42 | #include "Timer.hh" | ||
43 | |||
44 | namespace FbTk { | ||
45 | |||
46 | class MenuItem; | ||
47 | class ImageControl; | ||
48 | class Transparent; | ||
49 | |||
50 | /// Base class for menus | ||
51 | class Menu: public FbTk::EventHandler, protected FbTk::Observer { | ||
52 | public: | ||
53 | enum Alignment{ ALIGNDONTCARE = 1, ALIGNTOP, ALIGNBOTTOM }; | ||
54 | enum { RIGHT = 1, LEFT }; | ||
55 | |||
56 | /** | ||
57 | Bullet type | ||
58 | */ | ||
59 | enum { EMPTY = 0, SQUARE, TRIANGLE, DIAMOND }; | ||
60 | |||
61 | Menu(MenuTheme &tm, ImageControl &imgctrl); | ||
62 | virtual ~Menu(); | ||
63 | |||
64 | /** | ||
65 | @name manipulators | ||
66 | */ | ||
67 | //@{ | ||
68 | /// add a menu item with a label and a command | ||
69 | int insert(const char *label, RefCount<Command> &cmd, int pos=-1); | ||
70 | /// add empty menu item | ||
71 | int insert(const char *label, int pos=-1); | ||
72 | /// add submenu | ||
73 | int insert(const char *label, Menu *submenu, int pos= -1); | ||
74 | /// add menu item | ||
75 | int insert(MenuItem *item, int pos=-1); | ||
76 | /// remove an item | ||
77 | int remove(unsigned int item); | ||
78 | /// remove all items | ||
79 | void removeAll(); | ||
80 | inline void setInternalMenu(bool val = true) { internal_menu = val; } | ||
81 | inline void setAlignment(Alignment a) { m_alignment = a; } | ||
82 | inline void setTorn() { torn = true; } | ||
83 | inline void removeParent() { if (internal_menu) m_parent = 0; } | ||
84 | /// raise this window | ||
85 | virtual void raise(); | ||
86 | /// lower this window | ||
87 | virtual void lower(); | ||
88 | /// select next item | ||
89 | void nextItem(); | ||
90 | /// select previous item | ||
91 | void prevItem(); | ||
92 | void enterSubmenu(); | ||
93 | void enterParent(); | ||
94 | |||
95 | void disableTitle(); | ||
96 | void enableTitle(); | ||
97 | |||
98 | /** | ||
99 | @name event handlers | ||
100 | */ | ||
101 | //@{ | ||
102 | void handleEvent(XEvent &event); | ||
103 | void buttonPressEvent(XButtonEvent &bp); | ||
104 | void buttonReleaseEvent(XButtonEvent &br); | ||
105 | void motionNotifyEvent(XMotionEvent &mn); | ||
106 | void enterNotifyEvent(XCrossingEvent &en); | ||
107 | void leaveNotifyEvent(XCrossingEvent &ce); | ||
108 | void exposeEvent(XExposeEvent &ee); | ||
109 | void keyPressEvent(XKeyEvent &ke); | ||
110 | //@} | ||
111 | /// get input focus | ||
112 | void grabInputFocus(); | ||
113 | virtual void reconfigure(); | ||
114 | /// set label string | ||
115 | void setLabel(const char *labelstr); | ||
116 | /// move menu to x,y | ||
117 | void move(int x, int y); | ||
118 | void update(int active_index = -1); | ||
119 | void setItemSelected(unsigned int index, bool val); | ||
120 | void setItemEnabled(unsigned int index, bool val); | ||
121 | inline void setMinimumSublevels(int m) { menu.minsub = m; } | ||
122 | virtual void drawSubmenu(unsigned int index); | ||
123 | /// show menu | ||
124 | virtual void show(); | ||
125 | /// hide menu | ||
126 | virtual void hide(); | ||
127 | virtual void clearWindow(); | ||
128 | /*@}*/ | ||
129 | |||
130 | /** | ||
131 | @name accessors | ||
132 | */ | ||
133 | //@{ | ||
134 | inline bool isTorn() const { return torn; } | ||
135 | inline bool isVisible() const { return visible; } | ||
136 | inline int screenNumber() const { return menu.window.screenNumber(); } | ||
137 | inline Window window() const { return menu.window.window(); } | ||
138 | inline FbWindow &fbwindow() { return menu.window; } | ||
139 | inline const FbWindow &fbwindow() const { return menu.window; } | ||
140 | inline FbWindow &titleWindow() { return menu.title; } | ||
141 | inline FbWindow &frameWindow() { return menu.frame; } | ||
142 | inline const std::string &label() const { return menu.label; } | ||
143 | inline int x() const { return menu.window.x(); } | ||
144 | inline int y() const { return menu.window.y(); } | ||
145 | inline unsigned int width() const { return menu.window.width(); } | ||
146 | inline unsigned int height() const { return menu.window.height(); } | ||
147 | inline unsigned int numberOfItems() const { return menuitems.size(); } | ||
148 | inline int currentSubmenu() const { return which_sub; } | ||
149 | inline unsigned int titleHeight() const { return menu.title_h; } | ||
150 | bool hasSubmenu(unsigned int index) const; | ||
151 | bool isItemSelected(unsigned int index) const; | ||
152 | bool isItemEnabled(unsigned int index) const; | ||
153 | inline const MenuTheme &theme() const { return m_theme; } | ||
154 | inline unsigned char alpha() const { return m_theme.alpha(); } | ||
155 | inline static Menu *focused() { return s_focused; } | ||
156 | /// @return menuitem at index | ||
157 | inline const MenuItem *find(unsigned int index) const { return menuitems[index]; } | ||
158 | inline MenuItem *find(unsigned int index) { return menuitems[index]; } | ||
159 | //@} | ||
160 | |||
161 | protected: | ||
162 | |||
163 | inline void setTitleVisibility(bool b) { title_vis = b; m_need_update = true; } | ||
164 | inline void setMovable(bool b) { movable = b; } | ||
165 | inline void setHideTree(bool h) { hide_tree = h; } | ||
166 | |||
167 | virtual void itemSelected(int button, unsigned int index) { } | ||
168 | virtual void drawItem(unsigned int index, bool highlight = false, | ||
169 | bool clear= false, bool render_trans = true, | ||
170 | int x= -1, int y= -1, | ||
171 | unsigned int width= 0, unsigned int height= 0); | ||
172 | virtual void redrawTitle(); | ||
173 | virtual void internal_hide(); | ||
174 | inline Menu *parent() { return m_parent; } | ||
175 | inline const Menu *parent() const { return m_parent; } | ||
176 | |||
177 | void update(FbTk::Subject *) { reconfigure(); } | ||
178 | |||
179 | private: | ||
180 | |||
181 | void openSubmenu(); | ||
182 | void closeMenu(); | ||
183 | void startHide(); | ||
184 | void stopHide(); | ||
185 | |||
186 | void renderTransFrame(); | ||
187 | |||
188 | typedef std::vector<MenuItem *> Menuitems; | ||
189 | const MenuTheme &m_theme; | ||
190 | Menu *m_parent; | ||
191 | ImageControl &m_image_ctrl; | ||
192 | Menuitems menuitems; | ||
193 | |||
194 | const unsigned int m_screen_width, m_screen_height; | ||
195 | bool moving, visible, movable, torn, internal_menu, title_vis, shifted, | ||
196 | hide_tree; | ||
197 | |||
198 | int which_sub, which_press, which_sbl; | ||
199 | Alignment m_alignment; | ||
200 | int m_border_width; | ||
201 | struct _menu { | ||
202 | Pixmap frame_pixmap, title_pixmap, hilite_pixmap, sel_pixmap; | ||
203 | FbTk::FbWindow window, frame, title; | ||
204 | |||
205 | std::string label; | ||
206 | int x_move, y_move, x_shift, y_shift, sublevels, persub, minsub, | ||
207 | grab_x, grab_y; | ||
208 | unsigned int title_h, frame_h, item_w, item_h, bevel_w, | ||
209 | bevel_h; | ||
210 | } menu; | ||
211 | |||
212 | Drawable m_root_pm; | ||
213 | static Menu *s_focused; ///< holds current input focused menu, so one can determine if a menu is focused | ||
214 | FbPixmap m_frame_pm; | ||
215 | bool m_need_update; | ||
216 | Timer m_submenu_timer; | ||
217 | Timer m_hide_timer; | ||
218 | }; | ||
219 | |||
220 | } // end namespace FbTk | ||
221 | |||
222 | #endif // FBTK_MENU_HH | ||