1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "menu.h"
22 class WXDLLEXPORT wxMenuItem
;
23 class WXDLLEXPORT wxMenuBar
;
24 class WXDLLEXPORT wxMenu
;
25 class WXDLLEXPORT wxFrame
;
27 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class WXDLLEXPORT wxMenu
: public wxEvtHandler
35 DECLARE_DYNAMIC_CLASS(wxMenu
)
39 wxMenu(const wxString
& title
= wxEmptyString
, const wxFunction func
= NULL
);
43 // append a separator to the menu
44 void AppendSeparator();
45 // append a normal item to the menu
46 void Append(int id
, const wxString
& label
,
47 const wxString
& helpString
= wxEmptyString
,
48 bool checkable
= FALSE
);
50 void Append(int id
, const wxString
& label
,
52 const wxString
& helpString
= wxEmptyString
);
53 // append anything (create wxMenuItem first)
54 void Append(wxMenuItem
*pItem
);
56 // insert a break in the menu
60 // If it's a submenu, menu is not destroyed.
61 // VZ: why? shouldn't it return "wxMenu *" then?
65 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
66 void* GetClientData() const { return m_clientData
; }
69 // enable/disable item
70 void Enable(int id
, bool enable
);
72 bool IsEnabled(int id
) const;
74 // check/uncheck item - only for checkable items, of course
75 void Check(int id
, bool check
);
77 bool IsChecked(int id
) const;
81 void SetTitle(const wxString
& label
);
82 const wxString
GetTitle() const;
84 void SetLabel(int id
, const wxString
& label
);
85 wxString
GetLabel(int id
) const;
87 virtual void SetHelpString(int id
, const wxString
& helpString
);
88 virtual wxString
GetHelpString(int id
) const;
90 // get the list of items
91 wxList
& GetItems() const { return (wxList
&)m_menuItems
; }
94 // returns id of the item matching the given string or wxNOT_FOUND
95 virtual int FindItem(const wxString
& itemString
) const;
96 // returns NULL if not found
97 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
98 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
99 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**itemMenu
= NULL
) const;
101 // Updates the UI for a menu and all submenus recursively. source is the
102 // object that has the update event handlers defined for it. If NULL, the
103 // menu or associated window will be used.
104 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
106 bool ProcessCommand(wxCommandEvent
& event
);
108 virtual void SetParent(wxEvtHandler
*parent
) { m_parent
= parent
; }
109 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
110 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
113 bool MSWCommand(WXUINT param
, WXWORD id
);
115 void SetInvokingWindow(wxWindow
*pWin
) { m_pInvokingWindow
= pWin
; }
116 wxWindow
*GetInvokingWindow() const { return m_pInvokingWindow
; }
118 // semi-private accessors
119 // get the window which contains this menu
120 wxWindow
*GetWindow() const;
121 // get the menu handle
122 WXHMENU
GetHMenu() const;
124 // only for wxMenuBar
125 void Attach(wxMenuBar
*menubar
);
128 size_t GetAccelCount() const { return m_accelKeyCodes
.GetCount(); }
129 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
131 #ifdef WXWIN_COMPATIBILITY
132 void Callback(const wxFunction func
) { m_callback
= func
; }
134 // compatibility: these functions are deprecated
135 bool Enabled(int id
) const { return IsEnabled(id
); }
136 bool Checked(int id
) const { return IsChecked(id
); }
139 wxFunction m_callback
;
140 #endif // WXWIN_COMPATIBILITY
145 // This is used when m_hMenu is NULL because we don't want to
146 // delete it in ~wxMenu (it's been added to a parent menu).
147 // But we'll still need the handle for other purposes.
148 // Might be better to have a flag saying whether it's deleteable or not.
149 WXHMENU m_savehMenu
; // Used for Enable() on popup
154 wxMenu
* m_topLevelMenu
;
155 wxMenuBar
* m_menuBar
;
157 wxEvtHandler
* m_parent
;
158 wxEvtHandler
* m_eventHandler
;
159 wxWindow
*m_pInvokingWindow
;
162 // the accelerators data
163 wxArrayInt m_accelKeyCodes
, m_accelFlags
, m_accelIds
;
166 // ----------------------------------------------------------------------------
167 // Menu Bar (a la Windows)
168 // ----------------------------------------------------------------------------
170 class WXDLLEXPORT wxMenuBar
: public wxEvtHandler
172 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
176 // default constructor
179 wxMenuBar(long style
);
180 // menubar takes ownership of the menus arrays but copies the titles
181 wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[]);
182 virtual ~wxMenuBar();
184 // menubar construction
186 void Append(wxMenu
*menu
, const wxString
& title
);
187 virtual void Delete(wxMenu
*menu
, int index
= 0); /* Menu not destroyed */
190 // NB: must only be used AFTER menu has been attached to frame,
191 // otherwise use individual menus to enable/disable items
193 void Enable(int id
, bool enable
);
194 // TRUE if item enabled
195 bool IsEnabled(int id
) const;
197 void EnableTop(int pos
, bool enable
);
199 // works only with checkable items
200 void Check(int id
, bool check
);
202 bool IsChecked(int id
) const;
204 void SetLabel(int id
, const wxString
& label
) ;
205 wxString
GetLabel(int id
) const ;
207 virtual void SetHelpString(int id
, const wxString
& helpString
);
208 virtual wxString
GetHelpString(int id
) const ;
210 void SetLabelTop(int pos
, const wxString
& label
) ;
211 wxString
GetLabelTop(int pos
) const ;
213 // notifications: return FALSE to prevent the menu from being
215 virtual bool OnAppend(wxMenu
*menu
, const char *title
);
216 virtual bool OnDelete(wxMenu
*menu
, int index
);
219 // by menu and item names, returns wxNOT_FOUND if not found
220 virtual int FindMenuItem(const wxString
& menuString
,
221 const wxString
& itemString
) const;
222 // returns NULL if not found
223 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
224 // returns NULL if not found, fills menuForItem if !NULL
225 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**menuForItem
= NULL
) const;
228 int GetMenuCount() const { return m_menuCount
; }
229 wxMenu
*GetMenu(int i
) const { return m_menus
[i
]; }
231 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
232 wxEvtHandler
*GetEventHandler() { return m_eventHandler
; }
234 #ifdef WXWIN_COMPATIBILITY
235 // compatibility: these functions are deprecated
236 bool Enabled(int id
) const { return IsEnabled(id
); }
237 bool Checked(int id
) const { return IsChecked(id
); }
238 #endif // WXWIN_COMPATIBILITY
241 // returns TRUE if we're attached to a frame
242 bool IsAttached() const { return m_menuBarFrame
!= NULL
; }
243 // get the frame we live in
244 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
246 void Attach(wxFrame
*frame
);
248 // get the accel table for the menus
249 const wxAcceleratorTable
& GetAccelTable() const { return m_accelTable
; }
250 // get the menu handle
251 WXHMENU
GetHMenu() const { return m_hMenu
; }
254 // common part of all ctors
257 // if the menubar is modified, the display is not updated automatically,
258 // call this function to update it (m_menuBarFrame should be !NULL)
261 wxEvtHandler
*m_eventHandler
;
265 wxFrame
*m_menuBarFrame
;
268 // the accelerator table for all accelerators in all our menus
269 wxAcceleratorTable m_accelTable
;
272 #endif // _WX_MENU_H_