summaryrefslogtreecommitdiff
path: root/src/FbTk/MenuItem.hh
blob: 9150849a2b9bffa3332e67d18802868e509ff411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// MenuItem.hh for FbTk - Fluxbox Toolkit
// Copyright (c) 2003-2004 Henrik Kinnunen (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_MENUITEM_HH
#define FBTK_MENUITEM_HH

#include "RefCount.hh"
#include "Command.hh"
#include "PixmapWithMask.hh"
#include "ITypeAheadable.hh"
#include "FbString.hh"

#include <memory>

namespace FbTk {

class Menu;
class MenuTheme;
class FbDrawable;
template <class T> class ThemeProxy;

///   An interface for a menu item in Menu
class MenuItem : public FbTk::ITypeAheadable {
public:
    MenuItem()
        : m_label(""),
          m_menu(0),
          m_submenu(0),
          m_enabled(true),
          m_selected(false),
          m_close_on_click(true),
          m_toggle_item(false)
    { }
    explicit MenuItem(const FbString &label)
        : m_label(label),
          m_menu(0),
          m_submenu(0),
          m_enabled(true),
          m_selected(false),
          m_close_on_click(true),
          m_toggle_item(false)
    { }

    MenuItem(const FbString &label, Menu &host_menu)
        : m_label(label),
          m_menu(&host_menu),
          m_submenu(0),
          m_enabled(true),
          m_selected(false),
          m_close_on_click(true),
          m_toggle_item(false)
    { }
    /// create a menu item with a specific command to be executed on click
    MenuItem(const FbString &label, RefCount<Command<void> > &cmd, Menu *menu = 0)
        : m_label(label),
          m_menu(menu),
          m_submenu(0),
          m_command(cmd),
          m_enabled(true),
          m_selected(false),
          m_close_on_click(true),
          m_toggle_item(false)
    { }

    MenuItem(const FbString &label, Menu *submenu, Menu *host_menu = 0)
        : m_label(label),
          m_menu(host_menu),
          m_submenu(submenu),
          m_enabled(true),
          m_selected(false),
          m_close_on_click(true),
          m_toggle_item(false)
    { }
    virtual ~MenuItem() { }

    void setCommand(RefCount<Command<void> > &cmd) { m_command = cmd; }
    virtual void setSelected(bool selected) { m_selected = selected; }
    virtual void setEnabled(bool enabled) { m_enabled = enabled; }
    virtual void setLabel(const FbString &label) { m_label = label; }
    virtual void setToggleItem(bool val) { m_toggle_item = val; }
    void setCloseOnClick(bool val) { m_close_on_click = val; }
    void setIcon(const std::string &filename, int screen_num);
    virtual Menu *submenu() { return m_submenu; }
    /**
        @name accessors
    */
    //@{
    virtual const std::string &label() const { return m_label; }
    virtual const PixmapWithMask *icon() const {
        return m_icon.get() ? m_icon->pixmap.get() : 0;
    }
    virtual const Menu *submenu() const { return m_submenu; }
    virtual bool isEnabled() const { return m_enabled; }
    virtual bool isSelected() const { return m_selected; }
    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 std::string &iTypeString() const { return m_label; }
    virtual void drawLine(FbDrawable &draw,
                      const FbTk::ThemeProxy<MenuTheme> &theme,
                      size_t size,
                      int text_x, int text_y,
                      unsigned int width) const;

    virtual unsigned int width(const FbTk::ThemeProxy<MenuTheme> &theme) const;
    virtual unsigned int height(const FbTk::ThemeProxy<MenuTheme> &theme) const;
    virtual void draw(FbDrawable &drawable,
                      const FbTk::ThemeProxy<MenuTheme> &theme,
                      bool highlight,
                      // "foreground" is the transient bits - more likely to change
                      bool draw_foreground, bool draw_background,
                      int x, int y,
                      unsigned int width, unsigned int height) const;
    virtual void updateTheme(const FbTk::ThemeProxy<MenuTheme> &theme);
    /**
       Called when the item was clicked with a specific button
       @param button the button number
       @param time the time stamp
    */
    virtual void click(int button, int time, unsigned int mods);
    /// must use this to show submenu to ensure consistency for object like window menu in ClientMenu (see Workspace.cc)
    virtual void showSubmenu();
    RefCount<Command<void> > &command() { return m_command; }
    const RefCount<Command<void> > &command() const { return m_command; }
    //@}

    void setMenu(Menu &menu) { m_menu = &menu; }
    Menu *menu() { return m_menu; }

private:
    FbString m_label; ///< label of this item
    Menu *m_menu; ///< the menu we live in
    Menu *m_submenu; ///< a submenu, 0 if we don't have one
    RefCount<Command<void> > 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<PixmapWithMask> pixmap;
        std::string filename;
    };
    std::auto_ptr<Icon> m_icon;

};

} // end namespace FbTk

#endif // FBTK_MENUITEM_HH