1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar classes
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/dynarray.h"
18 #include "wx/string.h"
24 class WXDLLEXPORT wxMenuItem
;
25 class WXDLLEXPORT wxMenuBar
;
26 class WXDLLEXPORT wxMenu
;
27 class WXDLLEXPORT wxFrame
;
29 WXDLLEXPORT_DATA(extern const char*) wxEmptyString
;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
34 class WXDLLEXPORT wxMenu
: public wxEvtHandler
36 DECLARE_DYNAMIC_CLASS(wxMenu
)
39 wxMenu(const wxString
& title
,
40 const wxFunction func
)
45 wxMenu( long WXUNUSED(style
) )
47 Init( wxEmptyString
);
50 wxMenu(const wxString
& title
= wxEmptyString
, long WXUNUSED(style
) = 0)
58 // append a separator to the menu
59 void AppendSeparator();
60 // append a normal item to the menu
61 void Append(int id
, const wxString
& label
,
62 const wxString
& helpString
= wxEmptyString
,
63 bool checkable
= FALSE
);
65 void Append(int id
, const wxString
& label
,
67 const wxString
& helpString
= wxEmptyString
);
68 // append anything (create wxMenuItem first)
69 void Append(wxMenuItem
*pItem
);
71 // insert a break in the menu
75 // If it's a submenu, menu is not destroyed.
76 // VZ: why? shouldn't it return "wxMenu *" then?
80 inline void SetClientData(void* clientData
) { m_clientData
= clientData
; }
81 inline void* GetClientData() const { return m_clientData
; }
84 // enable/disable item
85 void Enable(int id
, bool enable
);
87 bool IsEnabled(int id
) const;
89 // check/uncheck item - only for checkable items, of course
90 void Check(int id
, bool check
);
92 bool IsChecked(int id
) const;
96 void SetTitle(const wxString
& label
);
97 const wxString
GetTitle() const;
99 void SetLabel(int id
, const wxString
& label
);
100 wxString
GetLabel(int id
) const;
102 virtual void SetHelpString(int id
, const wxString
& helpString
);
103 virtual wxString
GetHelpString(int id
) const;
105 // get the list of items
106 inline wxList
& GetItems() const { return (wxList
&)m_menuItems
; }
109 // returns id of the item matching the given string or wxNOT_FOUND
110 virtual int FindItem(const wxString
& itemString
) const;
111 // returns NULL if not found
112 inline wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
113 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
114 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**itemMenu
= NULL
) const;
116 // Updates the UI for a menu and all submenus recursively. source is the
117 // object that has the update event handlers defined for it. If NULL, the
118 // menu or associated window will be used.
119 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
121 bool ProcessCommand(wxCommandEvent
& event
);
123 inline virtual void SetParent(wxEvtHandler
*parent
) { m_parent
= parent
; }
124 inline void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
125 inline wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
128 bool OS2Command(WXUINT param
, WXWORD id
);
130 inline void SetInvokingWindow(wxWindow
*pWin
) { m_pInvokingWindow
= pWin
; }
131 inline wxWindow
*GetInvokingWindow() const { return m_pInvokingWindow
; }
133 // semi-private accessors
134 // get the window which contains this menu
135 wxWindow
*GetWindow() const;
136 // get the menu handle
137 WXHMENU
GetHMenu() const;
139 // only for wxMenuBar
140 void Attach(wxMenuBar
*menubar
);
144 inline size_t GetAccelCount() const { return m_accelKeyCodes
.GetCount(); }
145 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
146 #endif // wxUSE_ACCEL
148 inline wxFunction
GetCallback() const { return m_callback
; }
149 inline void Callback(const wxFunction func
) { m_callback
= func
; }
150 wxFunction m_callback
;
152 #ifdef WXWIN_COMPATIBILITY
153 // compatibility: these functions are deprecated
154 inline bool Enabled(int id
) const { return IsEnabled(id
); }
155 inline bool Checked(int id
) const { return IsChecked(id
); }
157 #endif // WXWIN_COMPATIBILITY
160 // common part of all ctors
161 void Init(const wxString
& title
, const wxFunction func
= NULL
);
165 // This is used when m_hMenu is NULL because we don't want to
166 // delete it in ~wxMenu (it's been added to a parent menu).
167 // But we'll still need the handle for other purposes.
168 // Might be better to have a flag saying whether it's deleteable or not.
169 WXHMENU m_savehMenu
; // Used for Enable() on popup
174 wxMenu
* m_topLevelMenu
;
175 wxMenuBar
* m_menuBar
;
177 wxEvtHandler
* m_parent
;
178 wxEvtHandler
* m_eventHandler
;
179 wxWindow
*m_pInvokingWindow
;
183 // the accelerators data
184 wxArrayInt m_accelKeyCodes
, m_accelFlags
, m_accelIds
;
185 #endif // wxUSE_ACCEL
188 // ----------------------------------------------------------------------------
189 // Menu Bar (a la Windows)
190 // ----------------------------------------------------------------------------
192 class WXDLLEXPORT wxMenuBar
: public wxEvtHandler
194 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
198 // default constructor
201 wxMenuBar(long style
);
202 // menubar takes ownership of the menus arrays but copies the titles
203 wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[]);
204 virtual ~wxMenuBar();
206 // menubar construction
208 void Append(wxMenu
*menu
, const wxString
& title
);
209 void Insert(int pos
, wxMenu
* menu
, const wxString
& title
);
210 void ReplaceMenu(int pos
, wxMenu
* new_menu
, const wxString
& title
);
211 int FindMenu(const wxString
& title
);
213 virtual void Delete(wxMenu
*menu
, int index
= 0); /* Menu not destroyed */
216 // NB: must only be used AFTER menu has been attached to frame,
217 // otherwise use individual menus to enable/disable items
219 void Enable(int id
, bool enable
);
220 // TRUE if item enabled
221 bool IsEnabled(int id
) const;
223 void EnableTop(int pos
, bool enable
);
225 // works only with checkable items
226 void Check(int id
, bool check
);
228 bool IsChecked(int id
) const;
230 void SetLabel(int id
, const wxString
& label
) ;
231 wxString
GetLabel(int id
) const ;
233 virtual void SetHelpString(int id
, const wxString
& helpString
);
234 virtual wxString
GetHelpString(int id
) const ;
236 void SetLabelTop(int pos
, const wxString
& label
) ;
237 wxString
GetLabelTop(int pos
) const ;
239 // notifications: return FALSE to prevent the menu from being
241 virtual bool OnAppend(wxMenu
*menu
, const wxChar
*title
);
242 virtual bool OnDelete(wxMenu
*menu
, int index
);
245 // by menu and item names, returns wxNOT_FOUND if not found
246 virtual int FindMenuItem(const wxString
& menuString
,
247 const wxString
& itemString
) const;
248 // returns NULL if not found
249 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
250 // returns NULL if not found, fills menuForItem if !NULL
251 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**menuForItem
= NULL
) const;
254 inline int GetMenuCount() const { return m_menuCount
; }
255 inline wxMenu
*GetMenu(int i
) const { return m_menus
[i
]; }
257 inline void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
258 inline wxEvtHandler
*GetEventHandler() { return m_eventHandler
; }
260 #ifdef WXWIN_COMPATIBILITY
261 // compatibility: these functions are deprecated
262 inline bool Enabled(int id
) const { return IsEnabled(id
); }
263 inline bool Checked(int id
) const { return IsChecked(id
); }
264 #endif // WXWIN_COMPATIBILITY
267 // returns TRUE if we're attached to a frame
268 inline bool IsAttached() const { return m_menuBarFrame
!= NULL
; }
269 // get the frame we live in
270 inline wxFrame
*GetFrame() const { return m_menuBarFrame
; }
272 void Attach(wxFrame
*frame
);
275 // get the accel table for the menus
276 inline const wxAcceleratorTable
& GetAccelTable() const { return m_accelTable
; }
277 #endif // wxUSE_ACCEL
279 // get the menu handle
280 inline WXHMENU
GetHMenu() const { return m_hMenu
; }
282 // if the menubar is modified, the display is not updated automatically,
283 // call this function to update it (m_menuBarFrame should be !NULL)
287 // common part of all ctors
290 wxEvtHandler
*m_eventHandler
;
294 wxFrame
*m_menuBarFrame
;
298 // the accelerator table for all accelerators in all our menus
299 wxAcceleratorTable m_accelTable
;
300 #endif // wxUSE_ACCEL
303 #endif // _WX_MENU_H_