1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/dynarray.h"
18 WX_DEFINE_EXPORTED_ARRAY_PTR(wxAcceleratorEntry
*, wxAcceleratorArray
);
21 class WXDLLIMPEXP_FWD_CORE wxFrame
;
23 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
24 class WXDLLIMPEXP_FWD_CORE wxToolBar
;
27 class wxMenuRadioItemsData
;
29 // Not using a combined wxToolBar/wxMenuBar? then use
30 // a commandbar in WinCE .NET to implement the
31 // menubar, since there is no ::SetMenu function.
32 #if defined(__WXWINCE__)
33 # if ((_WIN32_WCE >= 400) && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) || \
34 defined(__HANDHELDPC__)
35 # define WINCE_WITH_COMMANDBAR
37 # define WINCE_WITHOUT_COMMANDBAR
42 #include "wx/arrstr.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class WXDLLIMPEXP_CORE wxMenu
: public wxMenuBase
52 wxMenu(const wxString
& title
, long style
= 0)
53 : wxMenuBase(title
, style
) { Init(); }
55 wxMenu(long style
= 0) : wxMenuBase(style
) { Init(); }
61 virtual void SetTitle(const wxString
& title
);
66 // Create a new menu from the given native HMENU. Takes ownership of the
67 // menu handle and will delete it when this object is destroyed.
68 static wxMenu
*MSWNewFromHMENU(WXHMENU hMenu
) { return new wxMenu(hMenu
); }
71 // implementation only from now on
72 // -------------------------------
74 bool MSWCommand(WXUINT param
, WXWORD id
);
76 // get the native menu handle
77 WXHMENU
GetHMenu() const { return m_hMenu
; }
79 // Return the start and end position of the radio group to which the item
80 // at the given position belongs. Returns false if there is no radio group
81 // containing this position.
82 bool MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const;
85 // called by wxMenuBar to build its accel table from the accels of all menus
86 bool HasAccels() const { return !m_accels
.empty(); }
87 size_t GetAccelCount() const { return m_accels
.size(); }
88 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
90 // called by wxMenuItem when its accels changes
91 void UpdateAccel(wxMenuItem
*item
);
93 // helper used by wxMenu itself (returns the index in m_accels)
94 int FindAccel(int id
) const;
96 // used only by wxMDIParentFrame currently but could be useful elsewhere:
97 // returns a new accelerator table with accelerators for just this menu
98 // (shouldn't be called if we don't have any accelerators)
99 wxAcceleratorTable
*CreateAccelTable() const;
100 #endif // wxUSE_ACCEL
102 #if wxUSE_OWNER_DRAWN
104 int GetMaxAccelWidth()
106 if (m_maxAccelWidth
== -1)
107 CalculateMaxAccelWidth();
108 return m_maxAccelWidth
;
111 void ResetMaxAccelWidth()
113 m_maxAccelWidth
= -1;
116 // get the menu with given handle (recursively)
117 wxMenu
* MSWGetMenu(WXHMENU hMenu
);
120 void CalculateMaxAccelWidth();
122 #endif // wxUSE_OWNER_DRAWN
125 virtual wxMenuItem
* DoAppend(wxMenuItem
*item
);
126 virtual wxMenuItem
* DoInsert(size_t pos
, wxMenuItem
*item
);
127 virtual wxMenuItem
* DoRemove(wxMenuItem
*item
);
130 // This constructor is private, use MSWNewFromHMENU() to use it.
131 wxMenu(WXHMENU hMenu
);
133 // Common part of all ctors, it doesn't create a new HMENU.
136 // Common part of all ctors except of the one above taking a native menu
137 // handler: calls InitNoCreate() and also creates a new menu.
140 // common part of Append/Insert (behaves as Append is pos == (size_t)-1)
141 bool DoInsertOrAppend(wxMenuItem
*item
, size_t pos
= (size_t)-1);
144 // This variable contains the description of the radio item groups and
145 // allows to find whether an item at the given position is part of the
146 // group and also where its group starts and ends.
148 // It is initially NULL and only allocated if we have any radio items.
149 wxMenuRadioItemsData
*m_radioData
;
151 // if true, insert a breal before appending the next item
154 // the menu handle of this menu
158 // the accelerators for our menu items
159 wxAcceleratorArray m_accels
;
160 #endif // wxUSE_ACCEL
162 #if wxUSE_OWNER_DRAWN
163 // true if the menu has any ownerdrawn items
166 // the max width of menu items bitmaps
167 int m_maxBitmapWidth
;
169 // the max width of menu items accels
171 #endif // wxUSE_OWNER_DRAWN
173 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenu
)
176 // ----------------------------------------------------------------------------
177 // Menu Bar (a la Windows)
178 // ----------------------------------------------------------------------------
180 class WXDLLIMPEXP_CORE wxMenuBar
: public wxMenuBarBase
184 // default constructor
187 wxMenuBar(long style
);
188 // menubar takes ownership of the menus arrays but copies the titles
189 wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
= 0);
190 virtual ~wxMenuBar();
192 // menubar construction
193 virtual bool Append( wxMenu
*menu
, const wxString
&title
);
194 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
195 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
196 virtual wxMenu
*Remove(size_t pos
);
198 virtual void EnableTop( size_t pos
, bool flag
);
199 virtual bool IsEnabledTop(size_t pos
) const;
200 virtual void SetMenuLabel( size_t pos
, const wxString
& label
);
201 virtual wxString
GetMenuLabel( size_t pos
) const;
203 // implementation from now on
205 virtual void Detach();
206 virtual void Attach(wxFrame
*frame
);
208 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
209 // Under WinCE, a menubar is owned by the frame's toolbar
210 void SetToolBar(wxToolBar
* toolBar
) { m_toolBar
= toolBar
; }
211 wxToolBar
* GetToolBar() const { return m_toolBar
; }
214 #ifdef WINCE_WITH_COMMANDBAR
215 WXHWND
GetCommandBar() const { return m_commandBar
; }
216 bool AddAdornments(long style
);
220 // update the accel table (must be called after adding/deleting a menu)
221 void RebuildAccelTable();
222 #endif // wxUSE_ACCEL
224 // get the menu handle
225 WXHMENU
GetHMenu() const { return m_hMenu
; }
227 // if the menubar is modified, the display is not updated automatically,
228 // call this function to update it (m_menuBarFrame should be !NULL)
231 // To avoid compile warning
232 void Refresh( bool eraseBackground
,
233 const wxRect
*rect
= (const wxRect
*) NULL
) { wxWindow::Refresh(eraseBackground
, rect
); }
235 // get the menu with given handle (recursively)
236 wxMenu
* MSWGetMenu(WXHMENU hMenu
);
239 // common part of all ctors
244 // Return the MSW position for a wxMenu which is sometimes different from
245 // the wxWidgets position.
246 int MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
);
248 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
249 wxToolBar
* m_toolBar
;
252 #ifdef WINCE_WITH_COMMANDBAR
254 bool m_adornmentsAdded
;
258 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenuBar
)
261 #endif // _WX_MENU_H_