1 // MenuItem.hh for FbTk - Fluxbox Toolkit
2 // Copyright (c) 2003-2004 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 // $Id: MenuItem.hh 4063 2005-06-23 03:09:39Z fluxgen $
23 
24 #ifndef FBTK_MENUITEM_HH
25 #define FBTK_MENUITEM_HH
26 
27 #include "RefCount.hh"
28 #include "Command.hh"
29 #include "PixmapWithMask.hh"
30 
31 #include <string>
32 #include <memory>
33 
34 namespace FbTk {
35 
36 class Menu;
37 class MenuTheme;
38 class FbDrawable;
39 
40 ///   An interface for a menu item in Menu
41 class MenuItem {
42 public:
MenuItem()43     MenuItem()
44         : m_label(""),
45           m_menu(0),
46           m_submenu(0),
47           m_enabled(true),
48           m_selected(false),
49           m_toggle_item(false)
50     { }
MenuItem(const char * label)51     explicit MenuItem(
52              const char *label)
53         : m_label(label ? label : ""),
54           m_menu(0),
55           m_submenu(0),
56           m_enabled(true),
57           m_selected(false),
58           m_toggle_item(false)
59     { }
60 
MenuItem(const char * label,Menu & host_menu)61     MenuItem(const char *label, Menu &host_menu)
62         : m_label(label ? label : ""),
63           m_menu(&host_menu),
64           m_submenu(0),
65           m_enabled(true),
66           m_selected(false),
67           m_toggle_item(false)
68     { }
69     /// create a menu item with a specific command to be executed on click
MenuItem(const char * label,RefCount<Command> & cmd,Menu * menu=0)70     MenuItem(const char *label, RefCount<Command> &cmd, Menu *menu = 0):
71         m_label(label ? label : ""),
72         m_menu(menu),
73         m_submenu(0),
74         m_command(cmd),
75         m_enabled(true),
76         m_selected(false),
77         m_toggle_item(false) {
78 
79     }
80 
MenuItem(const char * label,Menu * submenu,Menu * host_menu=0)81     MenuItem(const char *label, Menu *submenu, Menu *host_menu = 0)
82         : m_label(label ? label : "")
83         , m_menu(host_menu)
84         , m_submenu(submenu)
85         , m_enabled(true)
86         , m_selected(false),
87           m_toggle_item(false)
88     { }
~MenuItem()89     virtual ~MenuItem() { }
90 
setCommand(RefCount<Command> & cmd)91     inline void setCommand(RefCount<Command> &cmd) { m_command = cmd; }
setSelected(bool selected)92     virtual inline void setSelected(bool selected) { m_selected = selected; }
setEnabled(bool enabled)93     virtual inline void setEnabled(bool enabled) { m_enabled = enabled; }
setLabel(const char * label)94     virtual inline void setLabel(const char *label) { m_label = (label ? label : ""); }
setToggleItem(bool val)95     virtual inline void setToggleItem(bool val) { m_toggle_item = val; }
96     void setIcon(const std::string &filename, int screen_num);
submenu()97     virtual Menu *submenu() { return m_submenu; }
98     /**
99         @name accessors
100     */
101     //@{
label() const102     virtual inline const std::string &label() const { return m_label; }
submenu() const103     virtual const Menu *submenu() const { return m_submenu; }
isEnabled() const104     virtual inline bool isEnabled() const { return m_enabled; }
isSelected() const105     virtual inline bool isSelected() const { return m_selected; }
isToggleItem() const106     virtual inline bool isToggleItem() const { return m_toggle_item; }
107     virtual unsigned int width(const MenuTheme &theme) const;
108     virtual unsigned int height(const MenuTheme &theme) const;
109     virtual void draw(FbDrawable &drawable,
110                       const MenuTheme &theme,
111                       bool highlight,
112                       // "foreground" is the transient bits - more likely to change
113                       bool draw_foreground, bool draw_background,
114                       int x, int y,
115                       unsigned int width, unsigned int height) const;
116     virtual void updateTheme(const MenuTheme &theme);
117     /**
118        Called when the item was clicked with a specific button
119        @param button the button number
120        @param time the time stamp
121     */
122     virtual void click(int button, int time);
123     /// must use this to show submenu to ensure consistency for object like window menu in ClientMenu (see Workspace.cc)
124     virtual void showSubmenu();
command()125     RefCount<Command> &command() { return m_command; }
command() const126     const RefCount<Command> &command() const { return m_command; }
127     //@}
128 
setMenu(Menu & menu)129     void setMenu(Menu &menu) { m_menu = &menu; }
menu()130     Menu *menu() { return m_menu; }
131 
132 private:
133     std::string m_label; ///< label of this item
134     Menu *m_menu; ///< the menu we live in
135     Menu *m_submenu; ///< a submenu, 0 if we don't have one
136     RefCount<Command> m_command; ///< command to be executed
137     bool m_enabled, m_selected;
138     bool m_toggle_item;
139 
140     struct Icon {
141         std::auto_ptr<PixmapWithMask> pixmap;
142         std::string filename;
143     };
144     std::auto_ptr<Icon> m_icon;
145 
146 };
147 
148 } // end namespace FbTk
149 
150 #endif // FBTK_MENUITEM_HH
151