1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/msw/menu.h
3 // Purpose:     wxMenu, wxMenuBar classes
4 // Author:      Julian Smart
5 // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
6 // Created:     01/02/97
7 // Copyright:   (c) Julian Smart
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef _WX_MENU_H_
12 #define _WX_MENU_H_
13 
14 #if wxUSE_ACCEL
15     #include "wx/accel.h"
16     #include "wx/dynarray.h"
17 
18     WX_DEFINE_EXPORTED_ARRAY_PTR(wxAcceleratorEntry *, wxAcceleratorArray);
19 #endif // wxUSE_ACCEL
20 
21 class WXDLLIMPEXP_FWD_CORE wxFrame;
22 
23 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
24 class WXDLLIMPEXP_FWD_CORE wxToolBar;
25 #endif
26 
27 class wxMenuRadioItemsData;
28 
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
36 #   else
37 #       define WINCE_WITHOUT_COMMANDBAR
38 #   endif
39 #endif
40 
41 
42 #include "wx/arrstr.h"
43 
44 // ----------------------------------------------------------------------------
45 // Menu
46 // ----------------------------------------------------------------------------
47 
48 class WXDLLIMPEXP_CORE wxMenu : public wxMenuBase
49 {
50 public:
51     // ctors & dtor
52     wxMenu(const wxString& title, long style = 0)
wxMenuBase(title,style)53         : wxMenuBase(title, style) { Init(); }
54 
wxMenuBase(style)55     wxMenu(long style = 0) : wxMenuBase(style) { Init(); }
56 
57     virtual ~wxMenu();
58 
59     virtual void Break();
60 
61     virtual void SetTitle(const wxString& title);
62 
63     // MSW-only methods
64     // ----------------
65 
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.
MSWNewFromHMENU(WXHMENU hMenu)68     static wxMenu *MSWNewFromHMENU(WXHMENU hMenu) { return new wxMenu(hMenu); }
69 
70 #if wxABI_VERSION >= 30002
71     // Detaches HMENU so that it isn't deleted when this object is destroyed.
72     // Don't use this object after calling this method.
MSWDetachHMENU()73     WXHMENU MSWDetachHMENU() { WXHMENU m = m_hMenu; m_hMenu = NULL; return m; }
74 #endif
75 
76     // implementation only from now on
77     // -------------------------------
78 
79     bool MSWCommand(WXUINT param, WXWORD id);
80 
81     // get the native menu handle
GetHMenu()82     WXHMENU GetHMenu() const { return m_hMenu; }
83 
84     // Return the start and end position of the radio group to which the item
85     // at the given position belongs. Returns false if there is no radio group
86     // containing this position.
87     bool MSWGetRadioGroupRange(int pos, int *start, int *end) const;
88 
89 #if wxUSE_ACCEL
90     // called by wxMenuBar to build its accel table from the accels of all menus
HasAccels()91     bool HasAccels() const { return !m_accels.empty(); }
GetAccelCount()92     size_t GetAccelCount() const { return m_accels.size(); }
93     size_t CopyAccels(wxAcceleratorEntry *accels) const;
94 
95     // called by wxMenuItem when its accels changes
96     void UpdateAccel(wxMenuItem *item);
97 #if wxABI_VERSION >= 30003
98     void RemoveAccel(wxMenuItem *item);
99 #endif
100 
101     // helper used by wxMenu itself (returns the index in m_accels)
102     int FindAccel(int id) const;
103 
104     // used only by wxMDIParentFrame currently but could be useful elsewhere:
105     // returns a new accelerator table with accelerators for just this menu
106     // (shouldn't be called if we don't have any accelerators)
107     wxAcceleratorTable *CreateAccelTable() const;
108 #endif // wxUSE_ACCEL
109 
110 #if wxUSE_OWNER_DRAWN
111 
GetMaxAccelWidth()112     int GetMaxAccelWidth()
113     {
114         if (m_maxAccelWidth == -1)
115             CalculateMaxAccelWidth();
116         return m_maxAccelWidth;
117     }
118 
ResetMaxAccelWidth()119     void ResetMaxAccelWidth()
120     {
121         m_maxAccelWidth = -1;
122     }
123 
124     // get the menu with given handle (recursively)
125     wxMenu* MSWGetMenu(WXHMENU hMenu);
126 
127 private:
128     void CalculateMaxAccelWidth();
129 
130 #endif // wxUSE_OWNER_DRAWN
131 
132 protected:
133     virtual wxMenuItem* DoAppend(wxMenuItem *item);
134     virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
135     virtual wxMenuItem* DoRemove(wxMenuItem *item);
136 
137 private:
138     // This constructor is private, use MSWNewFromHMENU() to use it.
139     wxMenu(WXHMENU hMenu);
140 
141     // Common part of all ctors, it doesn't create a new HMENU.
142     void InitNoCreate();
143 
144     // Common part of all ctors except of the one above taking a native menu
145     // handler: calls InitNoCreate() and also creates a new menu.
146     void Init();
147 
148     // common part of Append/Insert (behaves as Append is pos == (size_t)-1)
149     bool DoInsertOrAppend(wxMenuItem *item, size_t pos = (size_t)-1);
150 
151 
152     // This variable contains the description of the radio item groups and
153     // allows to find whether an item at the given position is part of the
154     // group and also where its group starts and ends.
155     //
156     // It is initially NULL and only allocated if we have any radio items.
157     wxMenuRadioItemsData *m_radioData;
158 
159     // if true, insert a breal before appending the next item
160     bool m_doBreak;
161 
162     // the menu handle of this menu
163     WXHMENU m_hMenu;
164 
165 #if wxUSE_ACCEL
166     // the accelerators for our menu items
167     wxAcceleratorArray m_accels;
168 #endif // wxUSE_ACCEL
169 
170 #if wxUSE_OWNER_DRAWN
171     // true if the menu has any ownerdrawn items
172     bool m_ownerDrawn;
173 
174     // the max width of menu items bitmaps
175     int m_maxBitmapWidth;
176 
177     // the max width of menu items accels
178     int m_maxAccelWidth;
179 #endif // wxUSE_OWNER_DRAWN
180 
181     DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenu)
182 };
183 
184 // ----------------------------------------------------------------------------
185 // Menu Bar (a la Windows)
186 // ----------------------------------------------------------------------------
187 
188 class WXDLLIMPEXP_CORE wxMenuBar : public wxMenuBarBase
189 {
190 public:
191     // ctors & dtor
192         // default constructor
193     wxMenuBar();
194         // unused under MSW
195     wxMenuBar(long style);
196         // menubar takes ownership of the menus arrays but copies the titles
197     wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style = 0);
198     virtual ~wxMenuBar();
199 
200     // menubar construction
201     virtual bool Append( wxMenu *menu, const wxString &title );
202     virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
203     virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
204     virtual wxMenu *Remove(size_t pos);
205 
206     virtual void EnableTop( size_t pos, bool flag );
207     virtual bool IsEnabledTop(size_t pos) const;
208     virtual void SetMenuLabel( size_t pos, const wxString& label );
209     virtual wxString GetMenuLabel( size_t pos ) const;
210 
211     // implementation from now on
212     WXHMENU Create();
213     virtual void Detach();
214     virtual void Attach(wxFrame *frame);
215 
216 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
217     // Under WinCE, a menubar is owned by the frame's toolbar
SetToolBar(wxToolBar * toolBar)218     void SetToolBar(wxToolBar* toolBar) { m_toolBar = toolBar; }
GetToolBar()219     wxToolBar* GetToolBar() const { return m_toolBar; }
220 #endif
221 
222 #ifdef WINCE_WITH_COMMANDBAR
GetCommandBar()223     WXHWND GetCommandBar() const { return m_commandBar; }
224     bool AddAdornments(long style);
225 #endif
226 
227 #if wxUSE_ACCEL
228     // update the accel table (must be called after adding/deleting a menu)
229     void RebuildAccelTable();
230 #endif // wxUSE_ACCEL
231 
232         // get the menu handle
GetHMenu()233     WXHMENU GetHMenu() const { return m_hMenu; }
234 
235     // if the menubar is modified, the display is not updated automatically,
236     // call this function to update it (m_menuBarFrame should be !NULL)
237     void Refresh();
238 
239     // To avoid compile warning
240     void Refresh( bool eraseBackground,
241                           const wxRect *rect = (const wxRect *) NULL ) { wxWindow::Refresh(eraseBackground, rect); }
242 
243     // get the menu with given handle (recursively)
244     wxMenu* MSWGetMenu(WXHMENU hMenu);
245 
246 protected:
247     // common part of all ctors
248     void Init();
249 
250     WXHMENU       m_hMenu;
251 
252     // Return the MSW position for a wxMenu which is sometimes different from
253     // the wxWidgets position.
254     int MSWPositionForWxMenu(wxMenu *menu, int wxpos);
255 
256 #if defined(__WXWINCE__) && wxUSE_TOOLBAR
257     wxToolBar*  m_toolBar;
258 #endif
259 
260 #ifdef WINCE_WITH_COMMANDBAR
261     WXHWND      m_commandBar;
262     bool        m_adornmentsAdded;
263 #endif
264 
265 private:
266     DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenuBar)
267 };
268 
269 #endif // _WX_MENU_H_
270