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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/dynarray.h"
19 WX_DEFINE_EXPORTED_ARRAY_PTR(wxAcceleratorEntry
*, wxAcceleratorArray
);
22 class WXDLLIMPEXP_FWD_CORE wxFrame
;
24 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
25 class WXDLLIMPEXP_FWD_CORE wxToolBar
;
28 class wxMenuRadioItemsData
;
30 // Not using a combined wxToolBar/wxMenuBar? then use
31 // a commandbar in WinCE .NET to implement the
32 // menubar, since there is no ::SetMenu function.
33 #if defined(__WXWINCE__)
34 # if ((_WIN32_WCE >= 400) && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) || \
35 defined(__HANDHELDPC__)
36 # define WINCE_WITH_COMMANDBAR
38 # define WINCE_WITHOUT_COMMANDBAR
43 #include "wx/arrstr.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class WXDLLIMPEXP_CORE wxMenu
: public wxMenuBase
53 wxMenu(const wxString
& title
, long style
= 0)
54 : wxMenuBase(title
, style
) { Init(); }
56 wxMenu(long style
= 0) : wxMenuBase(style
) { Init(); }
62 virtual void SetTitle(const wxString
& title
);
67 // Create a new menu from the given native HMENU. Takes ownership of the
68 // menu handle and will delete it when this object is destroyed.
69 static wxMenu
*MSWNewFromHMENU(WXHMENU hMenu
) { return new wxMenu(hMenu
); }
72 // implementation only from now on
73 // -------------------------------
75 bool MSWCommand(WXUINT param
, WXWORD id
);
77 // get the native menu handle
78 WXHMENU
GetHMenu() const { return m_hMenu
; }
80 // Return the start and end position of the radio group to which the item
81 // at the given position belongs. Returns false if there is no radio group
82 // containing this position.
83 bool MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const;
86 // called by wxMenuBar to build its accel table from the accels of all menus
87 bool HasAccels() const { return !m_accels
.empty(); }
88 size_t GetAccelCount() const { return m_accels
.size(); }
89 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
91 // called by wxMenuItem when its accels changes
92 void UpdateAccel(wxMenuItem
*item
);
94 // helper used by wxMenu itself (returns the index in m_accels)
95 int FindAccel(int id
) const;
97 // used only by wxMDIParentFrame currently but could be useful elsewhere:
98 // returns a new accelerator table with accelerators for just this menu
99 // (shouldn't be called if we don't have any accelerators)
100 wxAcceleratorTable
*CreateAccelTable() const;
101 #endif // wxUSE_ACCEL
103 #if wxUSE_OWNER_DRAWN
105 int GetMaxAccelWidth()
107 if (m_maxAccelWidth
== -1)
108 CalculateMaxAccelWidth();
109 return m_maxAccelWidth
;
112 void ResetMaxAccelWidth()
114 m_maxAccelWidth
= -1;
117 // get the menu with given handle (recursively)
118 wxMenu
* MSWGetMenu(WXHMENU hMenu
);
121 void CalculateMaxAccelWidth();
123 #endif // wxUSE_OWNER_DRAWN
126 virtual wxMenuItem
* DoAppend(wxMenuItem
*item
);
127 virtual wxMenuItem
* DoInsert(size_t pos
, wxMenuItem
*item
);
128 virtual wxMenuItem
* DoRemove(wxMenuItem
*item
);
131 // This constructor is private, use MSWNewFromHMENU() to use it.
132 wxMenu(WXHMENU hMenu
);
134 // Common part of all ctors, it doesn't create a new HMENU.
137 // Common part of all ctors except of the one above taking a native menu
138 // handler: calls InitNoCreate() and also creates a new menu.
141 // common part of Append/Insert (behaves as Append is pos == (size_t)-1)
142 bool DoInsertOrAppend(wxMenuItem
*item
, size_t pos
= (size_t)-1);
145 // This variable contains the description of the radio item groups and
146 // allows to find whether an item at the given position is part of the
147 // group and also where its group starts and ends.
149 // It is initially NULL and only allocated if we have any radio items.
150 wxMenuRadioItemsData
*m_radioData
;
152 // if true, insert a breal before appending the next item
155 // the menu handle of this menu
159 // the accelerators for our menu items
160 wxAcceleratorArray m_accels
;
161 #endif // wxUSE_ACCEL
163 #if wxUSE_OWNER_DRAWN
164 // true if the menu has any ownerdrawn items
167 // the max width of menu items bitmaps
168 int m_maxBitmapWidth
;
170 // the max width of menu items accels
172 #endif // wxUSE_OWNER_DRAWN
174 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenu
)
177 // ----------------------------------------------------------------------------
178 // Menu Bar (a la Windows)
179 // ----------------------------------------------------------------------------
181 class WXDLLIMPEXP_CORE wxMenuBar
: public wxMenuBarBase
185 // default constructor
188 wxMenuBar(long style
);
189 // menubar takes ownership of the menus arrays but copies the titles
190 wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
= 0);
191 virtual ~wxMenuBar();
193 // menubar construction
194 virtual bool Append( wxMenu
*menu
, const wxString
&title
);
195 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
196 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
197 virtual wxMenu
*Remove(size_t pos
);
199 virtual void EnableTop( size_t pos
, bool flag
);
200 virtual bool IsEnabledTop(size_t pos
) const;
201 virtual void SetMenuLabel( size_t pos
, const wxString
& label
);
202 virtual wxString
GetMenuLabel( size_t pos
) const;
204 // implementation from now on
206 virtual void Detach();
207 virtual void Attach(wxFrame
*frame
);
209 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
210 // Under WinCE, a menubar is owned by the frame's toolbar
211 void SetToolBar(wxToolBar
* toolBar
) { m_toolBar
= toolBar
; }
212 wxToolBar
* GetToolBar() const { return m_toolBar
; }
215 #ifdef WINCE_WITH_COMMANDBAR
216 WXHWND
GetCommandBar() const { return m_commandBar
; }
217 bool AddAdornments(long style
);
221 // update the accel table (must be called after adding/deleting a menu)
222 void RebuildAccelTable();
223 #endif // wxUSE_ACCEL
225 // get the menu handle
226 WXHMENU
GetHMenu() const { return m_hMenu
; }
228 // if the menubar is modified, the display is not updated automatically,
229 // call this function to update it (m_menuBarFrame should be !NULL)
232 // To avoid compile warning
233 void Refresh( bool eraseBackground
,
234 const wxRect
*rect
= (const wxRect
*) NULL
) { wxWindow::Refresh(eraseBackground
, rect
); }
236 // get the menu with given handle (recursively)
237 wxMenu
* MSWGetMenu(WXHMENU hMenu
);
240 // common part of all ctors
245 // Return the MSW position for a wxMenu which is sometimes different from
246 // the wxWidgets position.
247 int MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
);
249 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
250 wxToolBar
* m_toolBar
;
253 #ifdef WINCE_WITH_COMMANDBAR
255 bool m_adornmentsAdded
;
259 DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenuBar
)
262 #endif // _WX_MENU_H_