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"
21 #include "wx/dynarray.h"
23 class WXDLLEXPORT wxMenuItem
;
24 class WXDLLEXPORT wxMenuBar
;
25 class WXDLLEXPORT wxMenu
;
26 class WXDLLEXPORT wxFrame
;
28 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class WXDLLEXPORT wxMenu
: public wxEvtHandler
36 DECLARE_DYNAMIC_CLASS(wxMenu
)
40 wxMenu(const wxString
& title
,
41 const wxFunction func
)
46 wxMenu( long WXUNUSED(style
) )
48 Init( wxEmptyString
);
51 wxMenu(const wxString
& title
= wxEmptyString
, long WXUNUSED(style
) = 0)
59 // append a separator to the menu
60 void AppendSeparator();
61 // append a normal item to the menu
62 void Append(int id
, const wxString
& label
,
63 const wxString
& helpString
= wxEmptyString
,
64 bool checkable
= FALSE
);
66 void Append(int id
, const wxString
& label
,
68 const wxString
& helpString
= wxEmptyString
);
69 // append anything (create wxMenuItem first)
70 void Append(wxMenuItem
*pItem
);
72 // insert a break in the menu
76 // If it's a submenu, menu is not destroyed.
77 // VZ: why? shouldn't it return "wxMenu *" then?
81 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
82 void* GetClientData() const { return m_clientData
; }
85 // enable/disable item
86 void Enable(int id
, bool enable
);
88 bool IsEnabled(int id
) const;
90 // check/uncheck item - only for checkable items, of course
91 void Check(int id
, bool check
);
93 bool IsChecked(int id
) const;
97 void SetTitle(const wxString
& label
);
98 const wxString
GetTitle() const;
100 void SetLabel(int id
, const wxString
& label
);
101 wxString
GetLabel(int id
) const;
103 virtual void SetHelpString(int id
, const wxString
& helpString
);
104 virtual wxString
GetHelpString(int id
) const;
106 // get the list of items
107 wxList
& GetItems() const { return (wxList
&)m_menuItems
; }
110 // returns id of the item matching the given string or wxNOT_FOUND
111 virtual int FindItem(const wxString
& itemString
) const;
112 // returns NULL if not found
113 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
114 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
115 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**itemMenu
= NULL
) const;
117 // Updates the UI for a menu and all submenus recursively. source is the
118 // object that has the update event handlers defined for it. If NULL, the
119 // menu or associated window will be used.
120 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
122 bool ProcessCommand(wxCommandEvent
& event
);
124 virtual void SetParent(wxEvtHandler
*parent
) { m_parent
= parent
; }
125 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
126 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
129 bool MSWCommand(WXUINT param
, WXWORD id
);
131 void SetInvokingWindow(wxWindow
*pWin
) { m_pInvokingWindow
= pWin
; }
132 wxWindow
*GetInvokingWindow() const { return m_pInvokingWindow
; }
134 // semi-private accessors
135 // get the window which contains this menu
136 wxWindow
*GetWindow() const;
137 // get the menu handle
138 WXHMENU
GetHMenu() const;
140 // only for wxMenuBar
141 void Attach(wxMenuBar
*menubar
);
145 size_t GetAccelCount() const { return m_accelKeyCodes
.GetCount(); }
146 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
147 #endif // wxUSE_ACCEL
149 wxFunction
GetCallback() const { return m_callback
; }
150 void Callback(const wxFunction func
) { m_callback
= func
; }
151 wxFunction m_callback
;
153 #ifdef WXWIN_COMPATIBILITY
154 // compatibility: these functions are deprecated
155 bool Enabled(int id
) const { return IsEnabled(id
); }
156 bool Checked(int id
) const { return IsChecked(id
); }
158 #endif // WXWIN_COMPATIBILITY
161 // common part of all ctors
162 void Init(const wxString
& title
, const wxFunction func
= NULL
);
166 // This is used when m_hMenu is NULL because we don't want to
167 // delete it in ~wxMenu (it's been added to a parent menu).
168 // But we'll still need the handle for other purposes.
169 // Might be better to have a flag saying whether it's deleteable or not.
170 WXHMENU m_savehMenu
; // Used for Enable() on popup
175 wxMenu
* m_topLevelMenu
;
176 wxMenuBar
* m_menuBar
;
178 wxEvtHandler
* m_parent
;
179 wxEvtHandler
* m_eventHandler
;
180 wxWindow
*m_pInvokingWindow
;
184 // the accelerators data
185 wxArrayInt m_accelKeyCodes
, m_accelFlags
, m_accelIds
;
186 #endif // wxUSE_ACCEL
189 // ----------------------------------------------------------------------------
190 // Menu Bar (a la Windows)
191 // ----------------------------------------------------------------------------
193 class WXDLLEXPORT wxMenuBar
: public wxEvtHandler
195 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
199 // default constructor
202 wxMenuBar(long style
);
203 // menubar takes ownership of the menus arrays but copies the titles
204 wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[]);
205 virtual ~wxMenuBar();
207 // menubar construction
209 void Append(wxMenu
*menu
, const wxString
& title
);
210 virtual void Delete(wxMenu
*menu
, int index
= 0); /* Menu not destroyed */
213 // NB: must only be used AFTER menu has been attached to frame,
214 // otherwise use individual menus to enable/disable items
216 void Enable(int id
, bool enable
);
217 // TRUE if item enabled
218 bool IsEnabled(int id
) const;
220 void EnableTop(int pos
, bool enable
);
222 // works only with checkable items
223 void Check(int id
, bool check
);
225 bool IsChecked(int id
) const;
227 void SetLabel(int id
, const wxString
& label
) ;
228 wxString
GetLabel(int id
) const ;
230 virtual void SetHelpString(int id
, const wxString
& helpString
);
231 virtual wxString
GetHelpString(int id
) const ;
233 void SetLabelTop(int pos
, const wxString
& label
) ;
234 wxString
GetLabelTop(int pos
) const ;
236 // notifications: return FALSE to prevent the menu from being
238 virtual bool OnAppend(wxMenu
*menu
, const wxChar
*title
);
239 virtual bool OnDelete(wxMenu
*menu
, int index
);
242 // by menu and item names, returns wxNOT_FOUND if not found
243 virtual int FindMenuItem(const wxString
& menuString
,
244 const wxString
& itemString
) const;
245 // returns NULL if not found
246 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
247 // returns NULL if not found, fills menuForItem if !NULL
248 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**menuForItem
= NULL
) const;
251 int GetMenuCount() const { return m_menuCount
; }
252 wxMenu
*GetMenu(int i
) const { return m_menus
[i
]; }
254 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
255 wxEvtHandler
*GetEventHandler() { return m_eventHandler
; }
257 #ifdef WXWIN_COMPATIBILITY
258 // compatibility: these functions are deprecated
259 bool Enabled(int id
) const { return IsEnabled(id
); }
260 bool Checked(int id
) const { return IsChecked(id
); }
261 #endif // WXWIN_COMPATIBILITY
264 // returns TRUE if we're attached to a frame
265 bool IsAttached() const { return m_menuBarFrame
!= NULL
; }
266 // get the frame we live in
267 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
269 void Attach(wxFrame
*frame
);
272 // get the accel table for the menus
273 const wxAcceleratorTable
& GetAccelTable() const { return m_accelTable
; }
274 #endif // wxUSE_ACCEL
276 // get the menu handle
277 WXHMENU
GetHMenu() const { return m_hMenu
; }
280 // common part of all ctors
283 // if the menubar is modified, the display is not updated automatically,
284 // call this function to update it (m_menuBarFrame should be !NULL)
287 wxEvtHandler
*m_eventHandler
;
291 wxFrame
*m_menuBarFrame
;
295 // the accelerator table for all accelerators in all our menus
296 wxAcceleratorTable m_accelTable
;
297 #endif // wxUSE_ACCEL
300 #endif // _WX_MENU_H_