1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Menu.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
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:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
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.
29 #include "EventHandler.hh"
47 enum Type
{ Normal
, Separator
};
48 inline MenuItem(Type t
, const ustring
&l
= ustring())
49 : sub(0), lbl(l
), ident(~0u), indx(~0u), height(0),
50 separator(t
== Separator
),
51 active(0), title(0), enabled(1), checked(0)
53 inline MenuItem(Menu
*s
, const ustring
&l
)
54 : sub(s
), lbl(l
), ident(~0u), indx(~0u), height(0), separator(0),
55 active(0), title(0), enabled(1), checked(0)
58 inline bool isSeparator(void) const
59 { return bool(separator
); }
60 inline bool isActive(void) const
61 { return bool(active
); }
62 inline bool isTitle(void) const
63 { return bool(title
); }
64 inline bool isEnabled(void) const
65 { return bool(enabled
); }
66 inline bool isChecked(void) const
67 { return bool(checked
); }
69 inline unsigned int id(void) const
71 inline unsigned int index(void) const
74 inline Menu
*submenu(void) const
77 inline const ustring
&label(void) const
86 unsigned int separator
: 1;
87 unsigned int active
: 1;
88 unsigned int title
: 1;
89 unsigned int enabled
: 1;
90 unsigned int checked
: 1;
98 class MenuStyle
: public NoCopy
101 static MenuStyle
*get(Application
&app
, unsigned int screen
);
103 void load(const Resource
&resource
);
106 unsigned int separatorHeight(void) const;
107 unsigned int titleMargin(void) const;
108 unsigned int frameMargin(void) const;
109 unsigned int itemMargin(void) const;
112 inline const Texture
&titleTexture(void) const
113 { return title
.texture
; }
114 inline const Texture
&frameTexture(void) const
115 { return frame
.texture
; }
116 inline const Texture
&activeTexture(void) const
117 { return active
.texture
; }
120 inline const bt::Color
&titleForegroundColor(void) const
121 { return title
.foreground
; }
122 inline const bt::Color
&titleTextColor(void) const
123 { return title
.text
; }
124 inline const bt::Color
&frameForegroundColor(void) const
125 { return frame
.foreground
; }
126 inline const bt::Color
&frameTextColor(void) const
127 { return frame
.text
; }
130 inline const Font
&titleFont(void) const
131 { return title
.font
; }
132 inline const Font
&frameFont(void) const
133 { return frame
.font
; }
136 Rect
titleRect(const ustring
&text
) const;
137 Rect
itemRect(const MenuItem
&item
) const;
140 void drawTitle(Window window
, const Rect
&rect
,
141 const ustring
&text
) const;
142 void drawItem(Window window
, const Rect
&rect
,
143 const MenuItem
&item
, Pixmap activePixmap
) const;
146 MenuStyle(Application
&app
, unsigned int screen
);
149 unsigned int _screen
;
169 // font and alignment used from frame above
171 unsigned int title_margin
, frame_margin
, item_indent
;
173 static MenuStyle
**styles
;
177 class Menu
: public EventHandler
, public NoCopy
180 Menu(Application
&app
, unsigned int screen
);
183 inline Window
windowID(void) const
186 unsigned int insertItem(const MenuItem
&item
,
187 unsigned int id
= ~0u,
188 unsigned int index
= ~0u);
190 inline unsigned int insertItem(const ustring
&label
,
191 unsigned int id
= ~0u,
192 unsigned int index
= ~0u)
193 { return insertItem(MenuItem(MenuItem::Normal
, label
), id
, index
); }
195 inline unsigned int insertItem(const ustring
&label
,
197 unsigned int id
= ~0u,
198 unsigned int index
= ~0u)
199 { return insertItem(MenuItem(submenu
, label
), id
, index
); }
201 inline void insertSeparator(unsigned int index
= ~0u)
202 { (void) insertItem(MenuItem(MenuItem::Separator
), ~0u, index
); }
205 void changeItem(unsigned int id
,
206 const ustring
&newlabel
,
207 unsigned int newid
= ~0u);
209 void setItemEnabled(unsigned int id
, bool enabled
);
210 bool isItemEnabled(unsigned int id
) const;
212 void setItemChecked(unsigned int id
, bool checked
);
213 bool isItemChecked(unsigned int id
) const;
215 void removeItem(unsigned int id
);
216 void removeIndex(unsigned int index
);
219 inline unsigned int count(void) const
220 { return _items
.size(); }
222 inline const ustring
&title(void) const
224 inline void setTitle(const ustring
&newtitle
)
225 { _title
= newtitle
; }
226 void showTitle(void);
227 void hideTitle(void);
229 inline bool isVisible(void) const
232 void popup(int x
, int y
,
233 bool centered
= true);
234 virtual void popup(int x
, int y
,
235 const Rect
&constraint
,
236 bool centered
= true);
237 void move(int x
, int y
);
238 virtual void show(void);
239 virtual void hide(void);
241 virtual void refresh(void);
242 virtual void reconfigure(void);
244 inline bool autoDelete(void) const
245 { return _auto_delete
; }
246 inline void setAutoDelete(bool ad
)
247 { _auto_delete
= ad
; }
250 virtual void buttonPressEvent(const XButtonEvent
* const event
);
251 virtual void buttonReleaseEvent(const XButtonEvent
* const event
);
252 virtual void motionNotifyEvent(const XMotionEvent
* const event
);
253 virtual void leaveNotifyEvent(const XCrossingEvent
* const /*unused*/);
254 virtual void exposeEvent(const XExposeEvent
* const event
);
255 virtual void keyPressEvent(const XKeyEvent
* const event
);
257 virtual void titleClicked(unsigned int button
);
258 virtual void itemClicked(unsigned int id
, unsigned int button
);
260 virtual void hideAll(void);
262 virtual void tearOff(void);
264 Rect
geometry(void) const;
267 void updateSize(void);
268 void updatePixmaps(void);
270 typedef std::list
<MenuItem
> ItemList
;
272 unsigned int verifyId(unsigned int id
= ~0u);
273 void activateItem(const Rect
&rect
, MenuItem
&item
);
274 void deactivateItem(const Rect
&rect
, MenuItem
&item
,
275 bool hide_submenu
= true);
276 void activateIndex(unsigned int index
);
277 void showActiveSubmenu(void);
279 ItemList::iterator
findItem(unsigned int id
, Rect
& r
);
281 void activateSubmenu(void);
282 void positionRect(Rect
& r
, int &row
, int &col
);
283 void invalidateSize(void);
285 void removeItemByIterator(ItemList::iterator
& it
);
288 unsigned int _screen
;
291 Pixmap _tpixmap
, _fpixmap
, _apixmap
;
293 Rect _rect
; // entire menu
294 Rect _trect
; // title
295 Rect _frect
; // frame
296 Rect _irect
; // items inside the frame
302 std::vector
<bool> _id_bits
;
305 Menu
*_current_submenu
;
306 Menu
*_active_submenu
;
307 unsigned int _motion
;
309 unsigned int _active_index
;