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"
22 #include "wx/string.h"
28 class WXDLLEXPORT wxMenuItem
;
29 class WXDLLEXPORT wxMenuBar
;
30 class WXDLLEXPORT wxMenu
;
31 class WXDLLEXPORT wxFrame
;
33 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
35 WX_DEFINE_ARRAY(wxAcceleratorEntry
*, wxAcceleratorArray
);
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 class WXDLLEXPORT wxMenu
: public wxEvtHandler
43 DECLARE_DYNAMIC_CLASS(wxMenu
)
47 wxMenu(const wxString
& title
,
48 const wxFunction func
)
53 wxMenu( long WXUNUSED(style
) )
55 Init( wxEmptyString
);
58 wxMenu(const wxString
& title
= wxEmptyString
, long WXUNUSED(style
) = 0)
66 // append a separator to the menu
67 void AppendSeparator();
68 // append a normal item to the menu
69 void Append(int id
, const wxString
& label
,
70 const wxString
& helpString
= wxEmptyString
,
71 bool checkable
= FALSE
);
73 void Append(int id
, const wxString
& label
,
75 const wxString
& helpString
= wxEmptyString
);
76 // append anything (create wxMenuItem first)
77 void Append(wxMenuItem
*pItem
);
79 // insert a break in the menu
83 // If it's a submenu, menu is not destroyed.
84 // VZ: why? shouldn't it return "wxMenu *" then?
88 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
89 void* GetClientData() const { return m_clientData
; }
92 // enable/disable item
93 void Enable(int id
, bool enable
);
95 bool IsEnabled(int id
) const;
97 // check/uncheck item - only for checkable items, of course
98 void Check(int id
, bool check
);
100 bool IsChecked(int id
) const;
104 void SetTitle(const wxString
& label
);
105 const wxString
GetTitle() const;
107 void SetLabel(int id
, const wxString
& label
);
108 wxString
GetLabel(int id
) const;
110 virtual void SetHelpString(int id
, const wxString
& helpString
);
111 virtual wxString
GetHelpString(int id
) const;
113 // get the list of items
114 wxList
& GetItems() const { return (wxList
&)m_menuItems
; }
117 // returns id of the item matching the given string or wxNOT_FOUND
118 virtual int FindItem(const wxString
& itemString
) const;
119 // returns NULL if not found
120 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
121 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
122 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**itemMenu
= NULL
) const;
124 // Updates the UI for a menu and all submenus recursively. source is the
125 // object that has the update event handlers defined for it. If NULL, the
126 // menu or associated window will be used.
127 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
129 bool ProcessCommand(wxCommandEvent
& event
);
131 virtual void SetParent(wxEvtHandler
*parent
) { m_parent
= parent
; }
132 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
133 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
136 bool MSWCommand(WXUINT param
, WXWORD id
);
138 void SetInvokingWindow(wxWindow
*pWin
) { m_pInvokingWindow
= pWin
; }
139 wxWindow
*GetInvokingWindow() const { return m_pInvokingWindow
; }
141 // semi-private accessors
142 // get the window which contains this menu
143 wxWindow
*GetWindow() const;
144 // get the menu handle
145 WXHMENU
GetHMenu() const;
147 // only for wxMenuBar
148 void Attach(wxMenuBar
*menubar
);
152 size_t GetAccelCount() const { return m_accels
.GetCount(); }
153 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
154 #endif // wxUSE_ACCEL
156 wxFunction
GetCallback() const { return m_callback
; }
157 void Callback(const wxFunction func
) { m_callback
= func
; }
158 wxFunction m_callback
;
160 #ifdef WXWIN_COMPATIBILITY
161 // compatibility: these functions are deprecated
162 bool Enabled(int id
) const { return IsEnabled(id
); }
163 bool Checked(int id
) const { return IsChecked(id
); }
165 #endif // WXWIN_COMPATIBILITY
168 // common part of all ctors
169 void Init(const wxString
& title
, const wxFunction func
= NULL
);
173 // This is used when m_hMenu is NULL because we don't want to
174 // delete it in ~wxMenu (it's been added to a parent menu).
175 // But we'll still need the handle for other purposes.
176 // Might be better to have a flag saying whether it's deleteable or not.
177 WXHMENU m_savehMenu
; // Used for Enable() on popup
182 wxMenu
* m_topLevelMenu
;
183 wxMenuBar
* m_menuBar
;
185 wxEvtHandler
* m_parent
;
186 wxEvtHandler
* m_eventHandler
;
187 wxWindow
*m_pInvokingWindow
;
191 // the accelerators for our menu items
192 wxAcceleratorArray m_accels
;
193 #endif // wxUSE_ACCEL
196 // ----------------------------------------------------------------------------
197 // Menu Bar (a la Windows)
198 // ----------------------------------------------------------------------------
200 class WXDLLEXPORT wxMenuBar
: public wxMenuBarBase
204 // default constructor
207 wxMenuBar(long style
);
208 // menubar takes ownership of the menus arrays but copies the titles
209 wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[]);
210 virtual ~wxMenuBar();
212 // menubar construction
213 virtual bool Append( wxMenu
*menu
, const wxString
&title
);
214 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
215 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
216 virtual wxMenu
*Remove(size_t pos
);
218 virtual int FindMenuItem(const wxString
& menuString
,
219 const wxString
& itemString
) const;
220 virtual wxMenuItem
* FindItem( int id
, wxMenu
**menu
= NULL
) const;
222 virtual void EnableTop( size_t pos
, bool flag
);
223 virtual void SetLabelTop( size_t pos
, const wxString
& label
);
224 virtual wxString
GetLabelTop( size_t pos
) const;
226 // notifications: return FALSE to prevent the menu from being
228 virtual bool OnAppend(wxMenu
*menu
, const wxChar
*title
);
229 virtual bool OnDelete(wxMenu
*menu
, int index
);
231 // compatibility: these functions are deprecated
232 #ifdef WXWIN_COMPATIBILITY
233 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
234 wxEvtHandler
*GetEventHandler() { return m_eventHandler
; }
236 bool Enabled(int id
) const { return IsEnabled(id
); }
237 bool Checked(int id
) const { return IsChecked(id
); }
238 #endif // WXWIN_COMPATIBILITY
240 // implementation from now on
242 int FindMenu(const wxString
& title
);
245 // returns TRUE if we're attached to a frame
246 bool IsAttached() const { return m_menuBarFrame
!= NULL
; }
247 // get the frame we live in
248 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
250 void Attach(wxFrame
*frame
);
253 // get the accel table for the menus
254 const wxAcceleratorTable
& GetAccelTable() const { return m_accelTable
; }
255 #endif // wxUSE_ACCEL
257 // get the menu handle
258 WXHMENU
GetHMenu() const { return m_hMenu
; }
260 // if the menubar is modified, the display is not updated automatically,
261 // call this function to update it (m_menuBarFrame should be !NULL)
265 // common part of all ctors
268 #ifdef WXWIN_COMPATIBILITY
269 wxEvtHandler
*m_eventHandler
;
270 #endif // WXWIN_COMPATIBILITY
272 wxArrayString m_titles
;
274 wxFrame
*m_menuBarFrame
;
278 // the accelerator table for all accelerators in all our menus
279 wxAcceleratorTable m_accelTable
;
280 #endif // wxUSE_ACCEL
283 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
286 #endif // _WX_MENU_H_