]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c626a8b7 | 21 | #pragma implementation "menu.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c626a8b7 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_MENUS |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
c626a8b7 VZ |
34 | #include "wx/frame.h" |
35 | #include "wx/menu.h" | |
36 | #include "wx/utils.h" | |
0c589ad0 | 37 | #include "wx/intl.h" |
717a57c2 | 38 | #include "wx/log.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
47d67540 | 41 | #if wxUSE_OWNER_DRAWN |
c626a8b7 | 42 | #include "wx/ownerdrw.h" |
2bda0e17 KB |
43 | #endif |
44 | ||
45 | #include "wx/msw/private.h" | |
2bda0e17 | 46 | |
39d2f9a7 JS |
47 | #ifdef __WXWINCE__ |
48 | #include <windows.h> | |
49 | #include <windowsx.h> | |
50 | #include <tchar.h> | |
51 | #include <ole2.h> | |
eae4425d | 52 | #include <shellapi.h> |
39d2f9a7 | 53 | #include <commctrl.h> |
781a24e8 | 54 | #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__) |
39d2f9a7 | 55 | #include <aygshell.h> |
39d2f9a7 JS |
56 | #endif |
57 | ||
2d36b3d8 JS |
58 | #include "wx/msw/wince/missing.h" |
59 | ||
39d2f9a7 JS |
60 | #endif |
61 | ||
2bda0e17 | 62 | // other standard headers |
2bda0e17 KB |
63 | #include <string.h> |
64 | ||
c626a8b7 VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // global variables | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | extern wxMenu *wxCurrentPopupMenu; | |
70 | ||
b8d3a4f1 VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // constants | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // the (popup) menu title has this special id | |
c25f373e | 76 | static const int idMenuTitle = -3; |
b8d3a4f1 VZ |
77 | |
78 | // ---------------------------------------------------------------------------- | |
0472ece7 | 79 | // private functions |
b8d3a4f1 | 80 | // ---------------------------------------------------------------------------- |
c626a8b7 | 81 | |
0472ece7 VZ |
82 | // make the given menu item default |
83 | static void SetDefaultMenuItem(HMENU hmenu, UINT id) | |
84 | { | |
4676948b | 85 | #ifndef __WXWINCE__ |
0472ece7 VZ |
86 | MENUITEMINFO mii; |
87 | wxZeroMemory(mii); | |
88 | mii.cbSize = sizeof(MENUITEMINFO); | |
89 | mii.fMask = MIIM_STATE; | |
90 | mii.fState = MFS_DEFAULT; | |
91 | ||
92 | if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) ) | |
93 | { | |
94 | wxLogLastError(wxT("SetMenuItemInfo")); | |
95 | } | |
665b71b1 WS |
96 | #else |
97 | wxUnusedVar(hmenu); | |
98 | wxUnusedVar(id); | |
4676948b JS |
99 | #endif |
100 | } | |
101 | ||
102 | #ifdef __WXWINCE__ | |
103 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) | |
104 | { | |
105 | MENUITEMINFO info; | |
106 | wxZeroMemory(info); | |
107 | info.cbSize = sizeof(info); | |
108 | info.fMask = MIIM_STATE; | |
109 | if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) ) | |
110 | wxLogLastError(wxT("GetMenuItemInfo")); | |
111 | return info.fState; | |
0472ece7 | 112 | } |
4676948b | 113 | #endif |
2bda0e17 KB |
114 | |
115 | // ============================================================================ | |
116 | // implementation | |
117 | // ============================================================================ | |
118 | ||
e70b4f10 SC |
119 | #include <wx/listimpl.cpp> |
120 | ||
121 | WX_DEFINE_LIST( wxMenuInfoList ) ; | |
122 | ||
123 | #if wxUSE_EXTENDED_RTTI | |
124 | ||
125 | WX_DEFINE_FLAGS( wxMenuStyle ) | |
126 | ||
3ff066a4 SC |
127 | wxBEGIN_FLAGS( wxMenuStyle ) |
128 | wxFLAGS_MEMBER(wxMENU_TEAROFF) | |
129 | wxEND_FLAGS( wxMenuStyle ) | |
e70b4f10 SC |
130 | |
131 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h") | |
132 | ||
3ff066a4 | 133 | wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ; |
e70b4f10 SC |
134 | |
135 | template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value) | |
136 | { | |
137 | wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ; | |
138 | } | |
139 | ||
3ff066a4 | 140 | wxBEGIN_PROPERTIES_TABLE(wxMenu) |
665b71b1 | 141 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent) |
3ff066a4 | 142 | wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
af498247 | 143 | wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 SC |
144 | wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
145 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 146 | |
3ff066a4 SC |
147 | wxBEGIN_HANDLERS_TABLE(wxMenu) |
148 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 149 | |
3ff066a4 | 150 | wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle ) |
e70b4f10 SC |
151 | |
152 | WX_DEFINE_FLAGS( wxMenuBarStyle ) | |
153 | ||
3ff066a4 SC |
154 | wxBEGIN_FLAGS( wxMenuBarStyle ) |
155 | wxFLAGS_MEMBER(wxMB_DOCKABLE) | |
156 | wxEND_FLAGS( wxMenuBarStyle ) | |
e70b4f10 SC |
157 | |
158 | // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise | |
2341cf5f | 159 | bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & ) |
e70b4f10 SC |
160 | { |
161 | return true ; | |
162 | } | |
163 | ||
164 | IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback) | |
165 | ||
166 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" ) | |
167 | ||
3ff066a4 | 168 | wxBEGIN_PROPERTIES_TABLE(wxMenuInfo) |
af498247 | 169 | wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
3ff066a4 SC |
170 | wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
171 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 172 | |
3ff066a4 SC |
173 | wxBEGIN_HANDLERS_TABLE(wxMenuInfo) |
174 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 175 | |
3ff066a4 | 176 | wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title ) |
e70b4f10 | 177 | |
3ff066a4 | 178 | wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ; |
e70b4f10 SC |
179 | |
180 | template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value) | |
181 | { | |
182 | wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ; | |
183 | } | |
184 | ||
3ff066a4 SC |
185 | wxBEGIN_PROPERTIES_TABLE(wxMenuBar) |
186 | wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
187 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 188 | |
3ff066a4 SC |
189 | wxBEGIN_HANDLERS_TABLE(wxMenuBar) |
190 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 191 | |
3ff066a4 | 192 | wxCONSTRUCTOR_DUMMY( wxMenuBar ) |
e70b4f10 SC |
193 | |
194 | #else | |
0472ece7 VZ |
195 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
196 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) | |
e70b4f10 SC |
197 | IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject) |
198 | #endif | |
0472ece7 | 199 | |
e70b4f10 SC |
200 | const wxMenuInfoList& wxMenuBar::GetMenuInfos() const |
201 | { | |
202 | wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ; | |
203 | WX_CLEAR_LIST( wxMenuInfoList , *list ) ; | |
204 | for( size_t i = 0 ; i < GetMenuCount() ; ++i ) | |
205 | { | |
206 | wxMenuInfo* info = new wxMenuInfo() ; | |
207 | info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetLabelTop(i) ) ; | |
208 | list->Append( info ) ; | |
209 | } | |
210 | return m_menuInfos ; | |
211 | } | |
066f1b7a | 212 | |
c2dcfdef VZ |
213 | // --------------------------------------------------------------------------- |
214 | // wxMenu construction, adding and removing menu items | |
215 | // --------------------------------------------------------------------------- | |
2bda0e17 KB |
216 | |
217 | // Construct a menu with optional title (then use append) | |
717a57c2 | 218 | void wxMenu::Init() |
c626a8b7 | 219 | { |
ad9bb75f | 220 | m_doBreak = FALSE; |
0472ece7 | 221 | m_startRadioGroup = -1; |
c626a8b7 | 222 | |
717a57c2 VZ |
223 | // create the menu |
224 | m_hMenu = (WXHMENU)CreatePopupMenu(); | |
225 | if ( !m_hMenu ) | |
226 | { | |
f6bcfd97 | 227 | wxLogLastError(wxT("CreatePopupMenu")); |
717a57c2 VZ |
228 | } |
229 | ||
230 | // if we have a title, insert it in the beginning of the menu | |
c626a8b7 VZ |
231 | if ( !!m_title ) |
232 | { | |
ad9bb75f VZ |
233 | Append(idMenuTitle, m_title); |
234 | AppendSeparator(); | |
c626a8b7 | 235 | } |
2bda0e17 KB |
236 | } |
237 | ||
238 | // The wxWindow destructor will take care of deleting the submenus. | |
b8d3a4f1 | 239 | wxMenu::~wxMenu() |
2bda0e17 | 240 | { |
717a57c2 VZ |
241 | // we should free Windows resources only if Windows doesn't do it for us |
242 | // which happens if we're attached to a menubar or a submenu of another | |
243 | // menu | |
244 | if ( !IsAttached() && !GetParent() ) | |
c2dcfdef | 245 | { |
ad9bb75f VZ |
246 | if ( !::DestroyMenu(GetHmenu()) ) |
247 | { | |
f6bcfd97 | 248 | wxLogLastError(wxT("DestroyMenu")); |
ad9bb75f | 249 | } |
c2dcfdef | 250 | } |
c626a8b7 | 251 | |
ad9bb75f VZ |
252 | #if wxUSE_ACCEL |
253 | // delete accels | |
254 | WX_CLEAR_ARRAY(m_accels); | |
255 | #endif // wxUSE_ACCEL | |
2bda0e17 KB |
256 | } |
257 | ||
b8d3a4f1 | 258 | void wxMenu::Break() |
2bda0e17 | 259 | { |
717a57c2 | 260 | // this will take effect during the next call to Append() |
c2dcfdef | 261 | m_doBreak = TRUE; |
2bda0e17 KB |
262 | } |
263 | ||
0472ece7 VZ |
264 | void wxMenu::Attach(wxMenuBarBase *menubar) |
265 | { | |
266 | wxMenuBase::Attach(menubar); | |
267 | ||
268 | EndRadioGroup(); | |
269 | } | |
270 | ||
717a57c2 VZ |
271 | #if wxUSE_ACCEL |
272 | ||
273 | int wxMenu::FindAccel(int id) const | |
274 | { | |
275 | size_t n, count = m_accels.GetCount(); | |
276 | for ( n = 0; n < count; n++ ) | |
277 | { | |
278 | if ( m_accels[n]->m_command == id ) | |
279 | return n; | |
280 | } | |
281 | ||
282 | return wxNOT_FOUND; | |
283 | } | |
284 | ||
285 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
2bda0e17 | 286 | { |
f6bcfd97 | 287 | if ( item->IsSubMenu() ) |
717a57c2 | 288 | { |
f6bcfd97 | 289 | wxMenu *submenu = item->GetSubMenu(); |
222ed1d6 | 290 | wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst(); |
f6bcfd97 BP |
291 | while ( node ) |
292 | { | |
293 | UpdateAccel(node->GetData()); | |
294 | ||
295 | node = node->GetNext(); | |
296 | } | |
717a57c2 | 297 | } |
f6bcfd97 | 298 | else if ( !item->IsSeparator() ) |
717a57c2 | 299 | { |
f6bcfd97 BP |
300 | // find the (new) accel for this item |
301 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
717a57c2 | 302 | if ( accel ) |
f6bcfd97 BP |
303 | accel->m_command = item->GetId(); |
304 | ||
305 | // find the old one | |
306 | int n = FindAccel(item->GetId()); | |
307 | if ( n == wxNOT_FOUND ) | |
308 | { | |
309 | // no old, add new if any | |
310 | if ( accel ) | |
311 | m_accels.Add(accel); | |
312 | else | |
313 | return; // skipping RebuildAccelTable() below | |
314 | } | |
717a57c2 | 315 | else |
f6bcfd97 BP |
316 | { |
317 | // replace old with new or just remove the old one if no new | |
318 | delete m_accels[n]; | |
319 | if ( accel ) | |
320 | m_accels[n] = accel; | |
321 | else | |
b54e41c5 | 322 | m_accels.RemoveAt(n); |
f6bcfd97 | 323 | } |
717a57c2 | 324 | |
f6bcfd97 BP |
325 | if ( IsAttached() ) |
326 | { | |
4224f059 | 327 | GetMenuBar()->RebuildAccelTable(); |
f6bcfd97 | 328 | } |
42e69d6b | 329 | } |
f6bcfd97 | 330 | //else: it is a separator, they can't have accels, nothing to do |
717a57c2 VZ |
331 | } |
332 | ||
333 | #endif // wxUSE_ACCEL | |
334 | ||
335 | // append a new item or submenu to the menu | |
336 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
337 | { | |
338 | #if wxUSE_ACCEL | |
339 | UpdateAccel(pItem); | |
d427503c | 340 | #endif // wxUSE_ACCEL |
42e69d6b | 341 | |
c626a8b7 | 342 | UINT flags = 0; |
2bda0e17 | 343 | |
c2dcfdef VZ |
344 | // if "Break" has just been called, insert a menu break before this item |
345 | // (and don't forget to reset the flag) | |
c626a8b7 VZ |
346 | if ( m_doBreak ) { |
347 | flags |= MF_MENUBREAK; | |
348 | m_doBreak = FALSE; | |
349 | } | |
350 | ||
351 | if ( pItem->IsSeparator() ) { | |
352 | flags |= MF_SEPARATOR; | |
353 | } | |
2bda0e17 | 354 | |
c2dcfdef VZ |
355 | // id is the numeric id for normal menu items and HMENU for submenus as |
356 | // required by ::AppendMenu() API | |
c626a8b7 | 357 | UINT id; |
c2dcfdef VZ |
358 | wxMenu *submenu = pItem->GetSubMenu(); |
359 | if ( submenu != NULL ) { | |
717a57c2 VZ |
360 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); |
361 | ||
362 | submenu->SetParent(this); | |
2bda0e17 | 363 | |
c2dcfdef | 364 | id = (UINT)submenu->GetHMenu(); |
2bda0e17 | 365 | |
c626a8b7 VZ |
366 | flags |= MF_POPUP; |
367 | } | |
368 | else { | |
369 | id = pItem->GetId(); | |
370 | } | |
2bda0e17 | 371 | |
39d2f9a7 JS |
372 | #ifdef __WXWINCE__ |
373 | wxString strippedString; | |
374 | #endif | |
375 | ||
837e5743 | 376 | LPCTSTR pData; |
2bda0e17 | 377 | |
47d67540 | 378 | #if wxUSE_OWNER_DRAWN |
c626a8b7 VZ |
379 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? |
380 | // item draws itself, pass pointer to it in data parameter | |
381 | flags |= MF_OWNERDRAW; | |
837e5743 | 382 | pData = (LPCTSTR)pItem; |
c626a8b7 VZ |
383 | } |
384 | else | |
2bda0e17 | 385 | #endif |
c626a8b7 VZ |
386 | { |
387 | // menu is just a normal string (passed in data parameter) | |
388 | flags |= MF_STRING; | |
8fb3a512 | 389 | |
39d2f9a7 JS |
390 | #ifdef __WXWINCE__ |
391 | strippedString = wxStripMenuCodes(pItem->GetText()); | |
392 | pData = (wxChar*)strippedString.c_str(); | |
393 | #else | |
f5166ed4 | 394 | pData = (wxChar*)pItem->GetText().c_str(); |
39d2f9a7 | 395 | #endif |
c626a8b7 | 396 | } |
2bda0e17 | 397 | |
717a57c2 VZ |
398 | BOOL ok; |
399 | if ( pos == (size_t)-1 ) | |
400 | { | |
401 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); | |
402 | } | |
403 | else | |
404 | { | |
405 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); | |
406 | } | |
407 | ||
408 | if ( !ok ) | |
c626a8b7 | 409 | { |
f6bcfd97 | 410 | wxLogLastError(wxT("Insert or AppendMenu")); |
717a57c2 VZ |
411 | |
412 | return FALSE; | |
c626a8b7 | 413 | } |
42e69d6b | 414 | |
0472ece7 VZ |
415 | // if we just appended the title, highlight it |
416 | #ifdef __WIN32__ | |
417 | if ( (int)id == idMenuTitle ) | |
418 | { | |
419 | // visually select the menu title | |
420 | SetDefaultMenuItem(GetHmenu(), id); | |
421 | } | |
42e69d6b VZ |
422 | #endif // __WIN32__ |
423 | ||
0472ece7 | 424 | // if we're already attached to the menubar, we must update it |
4224f059 | 425 | if ( IsAttached() && GetMenuBar()->IsAttached() ) |
0472ece7 | 426 | { |
4224f059 | 427 | GetMenuBar()->Refresh(); |
0472ece7 VZ |
428 | } |
429 | ||
430 | return TRUE; | |
431 | } | |
432 | ||
433 | void wxMenu::EndRadioGroup() | |
434 | { | |
0472ece7 VZ |
435 | // we're not inside a radio group any longer |
436 | m_startRadioGroup = -1; | |
2bda0e17 KB |
437 | } |
438 | ||
9add9367 | 439 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) |
2bda0e17 | 440 | { |
9add9367 | 441 | wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") ); |
0472ece7 | 442 | |
be15b995 VZ |
443 | bool check = FALSE; |
444 | ||
546bfbea | 445 | if ( item->GetKind() == wxITEM_RADIO ) |
0472ece7 | 446 | { |
be15b995 VZ |
447 | int count = GetMenuItemCount(); |
448 | ||
0472ece7 VZ |
449 | if ( m_startRadioGroup == -1 ) |
450 | { | |
451 | // start a new radio group | |
be15b995 VZ |
452 | m_startRadioGroup = count; |
453 | ||
454 | // for now it has just one element | |
455 | item->SetAsRadioGroupStart(); | |
456 | item->SetRadioGroupEnd(m_startRadioGroup); | |
457 | ||
458 | // ensure that we have a checked item in the radio group | |
459 | check = TRUE; | |
460 | } | |
461 | else // extend the current radio group | |
462 | { | |
463 | // we need to update its end item | |
464 | item->SetRadioGroupStart(m_startRadioGroup); | |
222ed1d6 | 465 | wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup); |
be15b995 VZ |
466 | |
467 | if ( node ) | |
468 | { | |
469 | node->GetData()->SetRadioGroupEnd(count); | |
470 | } | |
471 | else | |
472 | { | |
473 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
474 | } | |
0472ece7 VZ |
475 | } |
476 | } | |
477 | else // not a radio item | |
478 | { | |
479 | EndRadioGroup(); | |
480 | } | |
481 | ||
be15b995 VZ |
482 | if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) |
483 | { | |
9add9367 | 484 | return NULL; |
be15b995 VZ |
485 | } |
486 | ||
487 | if ( check ) | |
488 | { | |
489 | // check the item initially | |
490 | item->Check(TRUE); | |
491 | } | |
492 | ||
9add9367 | 493 | return item; |
2bda0e17 KB |
494 | } |
495 | ||
9add9367 | 496 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
2bda0e17 | 497 | { |
9add9367 RD |
498 | if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos)) |
499 | return item; | |
500 | else | |
501 | return NULL; | |
2bda0e17 KB |
502 | } |
503 | ||
717a57c2 | 504 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
2bda0e17 | 505 | { |
717a57c2 VZ |
506 | // we need to find the items position in the child list |
507 | size_t pos; | |
222ed1d6 | 508 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 | 509 | for ( pos = 0; node; pos++ ) |
c626a8b7 | 510 | { |
717a57c2 | 511 | if ( node->GetData() == item ) |
c626a8b7 | 512 | break; |
717a57c2 VZ |
513 | |
514 | node = node->GetNext(); | |
c626a8b7 VZ |
515 | } |
516 | ||
717a57c2 VZ |
517 | // DoRemove() (unlike Remove) can only be called for existing item! |
518 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
c626a8b7 | 519 | |
717a57c2 VZ |
520 | #if wxUSE_ACCEL |
521 | // remove the corresponding accel from the accel table | |
522 | int n = FindAccel(item->GetId()); | |
523 | if ( n != wxNOT_FOUND ) | |
524 | { | |
525 | delete m_accels[n]; | |
c626a8b7 | 526 | |
b54e41c5 | 527 | m_accels.RemoveAt(n); |
c626a8b7 | 528 | } |
717a57c2 VZ |
529 | //else: this item doesn't have an accel, nothing to do |
530 | #endif // wxUSE_ACCEL | |
531 | ||
532 | // remove the item from the menu | |
533 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
534 | { | |
f6bcfd97 | 535 | wxLogLastError(wxT("RemoveMenu")); |
c626a8b7 VZ |
536 | } |
537 | ||
4224f059 | 538 | if ( IsAttached() && GetMenuBar()->IsAttached() ) |
717a57c2 VZ |
539 | { |
540 | // otherwise, the chane won't be visible | |
4224f059 | 541 | GetMenuBar()->Refresh(); |
717a57c2 | 542 | } |
2bda0e17 | 543 | |
717a57c2 VZ |
544 | // and from internal data structures |
545 | return wxMenuBase::DoRemove(item); | |
546 | } | |
d427503c | 547 | |
42e69d6b VZ |
548 | // --------------------------------------------------------------------------- |
549 | // accelerator helpers | |
550 | // --------------------------------------------------------------------------- | |
551 | ||
717a57c2 VZ |
552 | #if wxUSE_ACCEL |
553 | ||
42e69d6b VZ |
554 | // create the wxAcceleratorEntries for our accels and put them into provided |
555 | // array - return the number of accels we have | |
556 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
557 | { | |
558 | size_t count = GetAccelCount(); | |
559 | for ( size_t n = 0; n < count; n++ ) | |
560 | { | |
974e8d94 | 561 | *accels++ = *m_accels[n]; |
42e69d6b VZ |
562 | } |
563 | ||
564 | return count; | |
565 | } | |
566 | ||
d427503c VZ |
567 | #endif // wxUSE_ACCEL |
568 | ||
c2dcfdef | 569 | // --------------------------------------------------------------------------- |
717a57c2 | 570 | // set wxMenu title |
c2dcfdef VZ |
571 | // --------------------------------------------------------------------------- |
572 | ||
2bda0e17 KB |
573 | void wxMenu::SetTitle(const wxString& label) |
574 | { | |
c626a8b7 VZ |
575 | bool hasNoTitle = m_title.IsEmpty(); |
576 | m_title = label; | |
b8d3a4f1 | 577 | |
c50f1fb9 | 578 | HMENU hMenu = GetHmenu(); |
b8d3a4f1 | 579 | |
c626a8b7 | 580 | if ( hasNoTitle ) |
b8d3a4f1 | 581 | { |
c626a8b7 VZ |
582 | if ( !label.IsEmpty() ) |
583 | { | |
717a57c2 VZ |
584 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, |
585 | (unsigned)idMenuTitle, m_title) || | |
586 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
c626a8b7 | 587 | { |
f6bcfd97 | 588 | wxLogLastError(wxT("InsertMenu")); |
c626a8b7 VZ |
589 | } |
590 | } | |
b8d3a4f1 VZ |
591 | } |
592 | else | |
593 | { | |
c626a8b7 VZ |
594 | if ( label.IsEmpty() ) |
595 | { | |
596 | // remove the title and the separator after it | |
597 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
598 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
599 | { | |
f6bcfd97 | 600 | wxLogLastError(wxT("RemoveMenu")); |
c626a8b7 VZ |
601 | } |
602 | } | |
603 | else | |
604 | { | |
605 | // modify the title | |
4676948b JS |
606 | #ifdef __WXWINCE__ |
607 | MENUITEMINFO info; | |
608 | wxZeroMemory(info); | |
609 | info.cbSize = sizeof(info); | |
610 | info.fMask = MIIM_TYPE; | |
611 | info.fType = MFT_STRING; | |
612 | info.cch = m_title.Length(); | |
613 | info.dwTypeData = (LPTSTR) m_title.c_str(); | |
614 | if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) ) | |
615 | { | |
616 | wxLogLastError(wxT("SetMenuItemInfo")); | |
617 | } | |
618 | #else | |
c626a8b7 | 619 | if ( !ModifyMenu(hMenu, 0u, |
717a57c2 VZ |
620 | MF_BYPOSITION | MF_STRING, |
621 | (unsigned)idMenuTitle, m_title) ) | |
c626a8b7 | 622 | { |
f6bcfd97 | 623 | wxLogLastError(wxT("ModifyMenu")); |
c626a8b7 | 624 | } |
4676948b | 625 | #endif |
c626a8b7 | 626 | } |
b8d3a4f1 | 627 | } |
b8d3a4f1 | 628 | |
42e69d6b | 629 | #ifdef __WIN32__ |
c626a8b7 VZ |
630 | // put the title string in bold face |
631 | if ( !m_title.IsEmpty() ) | |
a3f4e9e8 | 632 | { |
0472ece7 | 633 | SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); |
a3f4e9e8 | 634 | } |
717a57c2 | 635 | #endif // Win32 |
2bda0e17 KB |
636 | } |
637 | ||
c2dcfdef VZ |
638 | // --------------------------------------------------------------------------- |
639 | // event processing | |
640 | // --------------------------------------------------------------------------- | |
2bda0e17 | 641 | |
debe6624 | 642 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) |
2bda0e17 | 643 | { |
a3f4e9e8 VZ |
644 | // ignore commands from the menu title |
645 | ||
646 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
647 | if ( id != (WXWORD)idMenuTitle ) | |
648 | { | |
3ca6a5f0 BP |
649 | // VZ: previosuly, the command int was set to id too which was quite |
650 | // useless anyhow (as it could be retrieved using GetId()) and | |
651 | // uncompatible with wxGTK, so now we use the command int instead | |
652 | // to pass the checked status | |
4676948b JS |
653 | UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ; |
654 | SendEvent(id, menuState & MF_CHECKED); | |
a3f4e9e8 VZ |
655 | } |
656 | ||
657 | return TRUE; | |
2bda0e17 KB |
658 | } |
659 | ||
c2dcfdef VZ |
660 | // --------------------------------------------------------------------------- |
661 | // other | |
662 | // --------------------------------------------------------------------------- | |
2bda0e17 | 663 | |
717a57c2 VZ |
664 | wxWindow *wxMenu::GetWindow() const |
665 | { | |
666 | if ( m_invokingWindow != NULL ) | |
667 | return m_invokingWindow; | |
4224f059 DE |
668 | else if ( GetMenuBar() != NULL) |
669 | return GetMenuBar()->GetFrame(); | |
717a57c2 VZ |
670 | |
671 | return NULL; | |
c2dcfdef VZ |
672 | } |
673 | ||
674 | // --------------------------------------------------------------------------- | |
2bda0e17 | 675 | // Menu Bar |
c2dcfdef VZ |
676 | // --------------------------------------------------------------------------- |
677 | ||
678 | void wxMenuBar::Init() | |
2bda0e17 | 679 | { |
c626a8b7 | 680 | m_eventHandler = this; |
c626a8b7 | 681 | m_hMenu = 0; |
3d487566 | 682 | #if wxUSE_TOOLBAR && defined(__WXWINCE__) |
39d2f9a7 | 683 | m_toolBar = NULL; |
a96b4743 JS |
684 | #endif |
685 | // Not using a combined wxToolBar/wxMenuBar? then use | |
686 | // a commandbar in WinCE .NET just to implement the | |
687 | // menubar. | |
3d487566 | 688 | #if defined(WINCE_WITH_COMMANDBAR) |
a96b4743 | 689 | m_commandBar = NULL; |
a9928e9d | 690 | m_adornmentsAdded = false; |
39d2f9a7 | 691 | #endif |
cba2db0c | 692 | } |
2bda0e17 | 693 | |
c2dcfdef VZ |
694 | wxMenuBar::wxMenuBar() |
695 | { | |
696 | Init(); | |
697 | } | |
698 | ||
cba2db0c JS |
699 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) |
700 | { | |
c2dcfdef | 701 | Init(); |
2bda0e17 KB |
702 | } |
703 | ||
c2dcfdef | 704 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) |
2bda0e17 | 705 | { |
c2dcfdef VZ |
706 | Init(); |
707 | ||
a8cfd0cb | 708 | m_titles.Alloc(count); |
c2dcfdef | 709 | |
a8cfd0cb VZ |
710 | for ( int i = 0; i < count; i++ ) |
711 | { | |
712 | m_menus.Append(menus[i]); | |
713 | m_titles.Add(titles[i]); | |
2bda0e17 | 714 | |
a8cfd0cb VZ |
715 | menus[i]->Attach(this); |
716 | } | |
2bda0e17 KB |
717 | } |
718 | ||
b8d3a4f1 | 719 | wxMenuBar::~wxMenuBar() |
2bda0e17 | 720 | { |
a96b4743 | 721 | // In Windows CE (not .NET), the menubar is always associated |
39d2f9a7 | 722 | // with a toolbar, which destroys the menu implicitly. |
3d487566 | 723 | #if defined(WINCE_WITHOUT_COMMANDBAR) |
bf95a04f JS |
724 | if (GetToolBar()) |
725 | GetToolBar()->SetMenuBar(NULL); | |
726 | #else | |
7a0363dd JS |
727 | // we should free Windows resources only if Windows doesn't do it for us |
728 | // which happens if we're attached to a frame | |
729 | if (m_hMenu && !IsAttached()) | |
730 | { | |
3d487566 | 731 | #if defined(WINCE_WITH_COMMANDBAR) |
a96b4743 JS |
732 | ::DestroyWindow((HWND) m_commandBar); |
733 | m_commandBar = (WXHWND) NULL; | |
734 | #else | |
7a0363dd | 735 | ::DestroyMenu((HMENU)m_hMenu); |
a96b4743 | 736 | #endif |
7a0363dd JS |
737 | m_hMenu = (WXHMENU)NULL; |
738 | } | |
39d2f9a7 | 739 | #endif |
c2dcfdef | 740 | } |
2bda0e17 | 741 | |
c2dcfdef VZ |
742 | // --------------------------------------------------------------------------- |
743 | // wxMenuBar helpers | |
744 | // --------------------------------------------------------------------------- | |
745 | ||
746 | void wxMenuBar::Refresh() | |
747 | { | |
065de612 | 748 | wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); |
c2dcfdef | 749 | |
3fd239fa | 750 | #if defined(WINCE_WITHOUT_COMMANDBAR) |
39d2f9a7 JS |
751 | if (GetToolBar()) |
752 | { | |
753 | CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0); | |
754 | } | |
3fd239fa | 755 | #elif defined(WINCE_WITH_COMMANDBAR) |
a96b4743 JS |
756 | if (m_commandBar) |
757 | DrawMenuBar((HWND) m_commandBar); | |
39d2f9a7 | 758 | #else |
1e6feb95 | 759 | DrawMenuBar(GetHwndOf(GetFrame())); |
39d2f9a7 | 760 | #endif |
c2dcfdef VZ |
761 | } |
762 | ||
763 | WXHMENU wxMenuBar::Create() | |
764 | { | |
beb471b5 JS |
765 | // Note: this totally doesn't work on Smartphone, |
766 | // since you have to use resources. | |
767 | // We'll have to find another way to add a menu | |
768 | // by changing/adding menu items to an existing menu. | |
3d487566 | 769 | #if defined(WINCE_WITHOUT_COMMANDBAR) |
39d2f9a7 JS |
770 | if ( m_hMenu != 0 ) |
771 | return m_hMenu; | |
772 | ||
773 | if (!GetToolBar()) | |
774 | return 0; | |
775 | ||
776 | HWND hCommandBar = (HWND) GetToolBar()->GetHWND(); | |
777 | HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0); | |
778 | if (hMenu) | |
779 | { | |
780 | TBBUTTON tbButton; | |
781 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
782 | tbButton.iBitmap = I_IMAGENONE; | |
783 | tbButton.fsState = TBSTATE_ENABLED; | |
784 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
785 | ||
786 | size_t i; | |
787 | for (i = 0; i < GetMenuCount(); i++) | |
788 | { | |
789 | HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ; | |
790 | tbButton.dwData = (DWORD)hPopupMenu; | |
791 | wxString label = wxStripMenuCodes(GetLabelTop(i)); | |
792 | tbButton.iString = (int) label.c_str(); | |
beb471b5 JS |
793 | |
794 | int position = i; | |
39d2f9a7 JS |
795 | |
796 | tbButton.idCommand = NewControlId(); | |
beb471b5 | 797 | if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton)) |
39d2f9a7 JS |
798 | { |
799 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
800 | } | |
801 | } | |
802 | } | |
803 | m_hMenu = (WXHMENU) hMenu; | |
804 | return m_hMenu; | |
805 | #else | |
3ca6a5f0 | 806 | if ( m_hMenu != 0 ) |
717a57c2 | 807 | return m_hMenu; |
1cf27c63 | 808 | |
c2dcfdef | 809 | m_hMenu = (WXHMENU)::CreateMenu(); |
2bda0e17 | 810 | |
c2dcfdef | 811 | if ( !m_hMenu ) |
c626a8b7 | 812 | { |
f6bcfd97 | 813 | wxLogLastError(wxT("CreateMenu")); |
c626a8b7 | 814 | } |
c2dcfdef | 815 | else |
c626a8b7 | 816 | { |
222ed1d6 MB |
817 | size_t count = GetMenuCount(), i; |
818 | wxMenuList::iterator it; | |
819 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
c2dcfdef VZ |
820 | { |
821 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
222ed1d6 | 822 | (UINT)(*it)->GetHMenu(), |
c2dcfdef VZ |
823 | m_titles[i]) ) |
824 | { | |
f6bcfd97 | 825 | wxLogLastError(wxT("AppendMenu")); |
c2dcfdef VZ |
826 | } |
827 | } | |
c626a8b7 | 828 | } |
c626a8b7 | 829 | |
c2dcfdef | 830 | return m_hMenu; |
39d2f9a7 | 831 | #endif |
2bda0e17 KB |
832 | } |
833 | ||
b2c5f143 DE |
834 | int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos) |
835 | { | |
836 | wxASSERT(menu); | |
837 | wxASSERT(menu->GetHMenu()); | |
838 | wxASSERT(m_hMenu); | |
8cc4850c JS |
839 | |
840 | #if defined(__WXWINCE__) | |
841 | int totalMSWItems = GetMenuCount(); | |
842 | #else | |
b2c5f143 | 843 | int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu); |
8cc4850c JS |
844 | #endif |
845 | ||
b2c5f143 DE |
846 | int i; // For old C++ compatibility |
847 | for(i=wxpos; i<totalMSWItems; i++) | |
848 | { | |
849 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
850 | return i; | |
851 | } | |
852 | for(i=0; i<wxpos; i++) | |
853 | { | |
854 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
855 | return i; | |
856 | } | |
857 | wxFAIL; | |
858 | return -1; | |
859 | } | |
860 | ||
c2dcfdef | 861 | // --------------------------------------------------------------------------- |
3dfac970 | 862 | // wxMenuBar functions to work with the top level submenus |
c2dcfdef VZ |
863 | // --------------------------------------------------------------------------- |
864 | ||
3dfac970 VZ |
865 | // NB: we don't support owner drawn top level items for now, if we do these |
866 | // functions would have to be changed to use wxMenuItem as well | |
2bda0e17 | 867 | |
a8cfd0cb | 868 | void wxMenuBar::EnableTop(size_t pos, bool enable) |
2bda0e17 | 869 | { |
717a57c2 | 870 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); |
b2c5f143 | 871 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
717a57c2 VZ |
872 | |
873 | int flag = enable ? MF_ENABLED : MF_GRAYED; | |
2bda0e17 | 874 | |
b2c5f143 | 875 | EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag); |
adc6fb16 VZ |
876 | |
877 | Refresh(); | |
2bda0e17 KB |
878 | } |
879 | ||
a8cfd0cb | 880 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) |
2bda0e17 | 881 | { |
717a57c2 VZ |
882 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
883 | ||
884 | m_titles[pos] = label; | |
885 | ||
886 | if ( !IsAttached() ) | |
887 | { | |
888 | return; | |
889 | } | |
890 | //else: have to modify the existing menu | |
891 | ||
b2c5f143 DE |
892 | int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos); |
893 | ||
8cd85069 | 894 | UINT id; |
b2c5f143 | 895 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION); |
c2dcfdef | 896 | if ( flagsOld == 0xFFFFFFFF ) |
c626a8b7 | 897 | { |
223d09f6 | 898 | wxLogLastError(wxT("GetMenuState")); |
c2dcfdef VZ |
899 | |
900 | return; | |
901 | } | |
902 | ||
903 | if ( flagsOld & MF_POPUP ) | |
904 | { | |
905 | // HIBYTE contains the number of items in the submenu in this case | |
ad9bb75f | 906 | flagsOld &= 0xff; |
b2c5f143 | 907 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos); |
c626a8b7 VZ |
908 | } |
909 | else | |
8cd85069 VZ |
910 | { |
911 | id = pos; | |
912 | } | |
913 | ||
4676948b JS |
914 | #ifdef __WXWINCE__ |
915 | MENUITEMINFO info; | |
916 | wxZeroMemory(info); | |
917 | info.cbSize = sizeof(info); | |
918 | info.fMask = MIIM_TYPE; | |
919 | info.fType = MFT_STRING; | |
920 | info.cch = label.Length(); | |
921 | info.dwTypeData = (LPTSTR) label.c_str(); | |
922 | if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) ) | |
923 | { | |
924 | wxLogLastError(wxT("SetMenuItemInfo")); | |
925 | } | |
926 | ||
927 | #else | |
b2c5f143 | 928 | if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld, |
4676948b | 929 | id, label) == (int)0xFFFFFFFF ) |
c2dcfdef | 930 | { |
f6bcfd97 | 931 | wxLogLastError(wxT("ModifyMenu")); |
c2dcfdef | 932 | } |
4676948b | 933 | #endif |
717a57c2 VZ |
934 | |
935 | Refresh(); | |
2bda0e17 KB |
936 | } |
937 | ||
a8cfd0cb | 938 | wxString wxMenuBar::GetLabelTop(size_t pos) const |
2bda0e17 | 939 | { |
717a57c2 VZ |
940 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, |
941 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
8cd85069 | 942 | |
4a1c207c | 943 | return wxMenuItem::GetLabelFromText(m_titles[pos]); |
2bda0e17 KB |
944 | } |
945 | ||
ad9bb75f VZ |
946 | // --------------------------------------------------------------------------- |
947 | // wxMenuBar construction | |
948 | // --------------------------------------------------------------------------- | |
949 | ||
a8cfd0cb | 950 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) |
1cf27c63 | 951 | { |
ad9bb75f VZ |
952 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); |
953 | if ( !menuOld ) | |
f7f50f49 VZ |
954 | return NULL; |
955 | ||
ad9bb75f | 956 | m_titles[pos] = title; |
a8cfd0cb | 957 | |
ad9bb75f | 958 | if ( IsAttached() ) |
a8cfd0cb | 959 | { |
b2c5f143 DE |
960 | int mswpos = MSWPositionForWxMenu(menuOld,pos); |
961 | ||
ad9bb75f | 962 | // can't use ModifyMenu() because it deletes the submenu it replaces |
b2c5f143 | 963 | if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) ) |
a8cfd0cb | 964 | { |
f6bcfd97 | 965 | wxLogLastError(wxT("RemoveMenu")); |
a8cfd0cb | 966 | } |
1cf27c63 | 967 | |
b2c5f143 | 968 | if ( !::InsertMenu(GetHmenu(), (UINT)mswpos, |
ad9bb75f VZ |
969 | MF_BYPOSITION | MF_POPUP | MF_STRING, |
970 | (UINT)GetHmenuOf(menu), title) ) | |
971 | { | |
f6bcfd97 | 972 | wxLogLastError(wxT("InsertMenu")); |
ad9bb75f VZ |
973 | } |
974 | ||
717a57c2 VZ |
975 | #if wxUSE_ACCEL |
976 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
977 | { | |
978 | // need to rebuild accell table | |
979 | RebuildAccelTable(); | |
980 | } | |
981 | #endif // wxUSE_ACCEL | |
982 | ||
ad9bb75f | 983 | Refresh(); |
a8cfd0cb | 984 | } |
ad9bb75f VZ |
985 | |
986 | return menuOld; | |
1cf27c63 UM |
987 | } |
988 | ||
a8cfd0cb | 989 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
1cf27c63 | 990 | { |
b2c5f143 DE |
991 | // Find out which MSW item before which we'll be inserting before |
992 | // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu. | |
3d27b574 DE |
993 | // If IsAttached() is false this won't be used anyway |
994 | int mswpos = (!IsAttached() || (pos == m_menus.GetCount())) | |
b2c5f143 DE |
995 | ? -1 // append the menu |
996 | : MSWPositionForWxMenu(GetMenu(pos),pos); | |
997 | ||
ad9bb75f | 998 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
a8cfd0cb | 999 | return FALSE; |
1cf27c63 | 1000 | |
ad9bb75f VZ |
1001 | m_titles.Insert(title, pos); |
1002 | ||
ad9bb75f VZ |
1003 | if ( IsAttached() ) |
1004 | { | |
3d487566 | 1005 | #if defined(WINCE_WITHOUT_COMMANDAR) |
39d2f9a7 JS |
1006 | if (!GetToolBar()) |
1007 | return FALSE; | |
1008 | TBBUTTON tbButton; | |
1009 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1010 | tbButton.iBitmap = I_IMAGENONE; | |
1011 | tbButton.fsState = TBSTATE_ENABLED; | |
1012 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1013 | ||
1014 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1015 | tbButton.dwData = (DWORD)hPopupMenu; | |
1016 | wxString label = wxStripMenuCodes(title); | |
1017 | tbButton.iString = (int) label.c_str(); | |
1018 | ||
1019 | tbButton.idCommand = NewControlId(); | |
1020 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1021 | { | |
1022 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1023 | return FALSE; | |
1024 | } | |
1025 | #else | |
b2c5f143 | 1026 | if ( !::InsertMenu(GetHmenu(), mswpos, |
ad9bb75f VZ |
1027 | MF_BYPOSITION | MF_POPUP | MF_STRING, |
1028 | (UINT)GetHmenuOf(menu), title) ) | |
1029 | { | |
f6bcfd97 | 1030 | wxLogLastError(wxT("InsertMenu")); |
ad9bb75f | 1031 | } |
39d2f9a7 | 1032 | #endif |
717a57c2 VZ |
1033 | #if wxUSE_ACCEL |
1034 | if ( menu->HasAccels() ) | |
1035 | { | |
1036 | // need to rebuild accell table | |
1037 | RebuildAccelTable(); | |
1038 | } | |
1039 | #endif // wxUSE_ACCEL | |
1040 | ||
ad9bb75f | 1041 | Refresh(); |
a8cfd0cb | 1042 | } |
ad9bb75f VZ |
1043 | |
1044 | return TRUE; | |
1cf27c63 UM |
1045 | } |
1046 | ||
ad9bb75f | 1047 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) |
2bda0e17 | 1048 | { |
ad9bb75f VZ |
1049 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; |
1050 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
2bda0e17 | 1051 | |
717a57c2 VZ |
1052 | if ( !wxMenuBarBase::Append(menu, title) ) |
1053 | return FALSE; | |
1054 | ||
717a57c2 VZ |
1055 | m_titles.Add(title); |
1056 | ||
ad9bb75f VZ |
1057 | if ( IsAttached() ) |
1058 | { | |
3d487566 | 1059 | #if defined(WINCE_WITHOUT_COMMANDAR) |
39d2f9a7 JS |
1060 | if (!GetToolBar()) |
1061 | return FALSE; | |
1062 | TBBUTTON tbButton; | |
1063 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1064 | tbButton.iBitmap = I_IMAGENONE; | |
1065 | tbButton.fsState = TBSTATE_ENABLED; | |
1066 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1067 | ||
1068 | size_t pos = GetMenuCount(); | |
1069 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1070 | tbButton.dwData = (DWORD)hPopupMenu; | |
1071 | wxString label = wxStripMenuCodes(title); | |
1072 | tbButton.iString = (int) label.c_str(); | |
1073 | ||
1074 | tbButton.idCommand = NewControlId(); | |
1075 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1076 | { | |
1077 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1078 | return FALSE; | |
1079 | } | |
1080 | #else | |
ad9bb75f VZ |
1081 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, |
1082 | (UINT)submenu, title) ) | |
1083 | { | |
1084 | wxLogLastError(wxT("AppendMenu")); | |
1085 | } | |
39d2f9a7 | 1086 | #endif |
ad9bb75f | 1087 | |
717a57c2 VZ |
1088 | #if wxUSE_ACCEL |
1089 | if ( menu->HasAccels() ) | |
1090 | { | |
39d2f9a7 | 1091 | // need to rebuild accelerator table |
717a57c2 VZ |
1092 | RebuildAccelTable(); |
1093 | } | |
1094 | #endif // wxUSE_ACCEL | |
1095 | ||
ad9bb75f VZ |
1096 | Refresh(); |
1097 | } | |
2bda0e17 | 1098 | |
a8cfd0cb | 1099 | return TRUE; |
2bda0e17 KB |
1100 | } |
1101 | ||
a8cfd0cb | 1102 | wxMenu *wxMenuBar::Remove(size_t pos) |
2bda0e17 | 1103 | { |
a8cfd0cb VZ |
1104 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
1105 | if ( !menu ) | |
1106 | return NULL; | |
c626a8b7 | 1107 | |
ad9bb75f VZ |
1108 | if ( IsAttached() ) |
1109 | { | |
3d487566 | 1110 | #if defined(WINCE_WITHOUT_COMMANDAR) |
39d2f9a7 JS |
1111 | if (GetToolBar()) |
1112 | { | |
1113 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0)) | |
1114 | { | |
1115 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
1116 | } | |
1117 | } | |
1118 | #else | |
b2c5f143 | 1119 | if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) ) |
ad9bb75f | 1120 | { |
f6bcfd97 | 1121 | wxLogLastError(wxT("RemoveMenu")); |
ad9bb75f | 1122 | } |
39d2f9a7 | 1123 | #endif |
c4053ed3 | 1124 | |
717a57c2 VZ |
1125 | #if wxUSE_ACCEL |
1126 | if ( menu->HasAccels() ) | |
1127 | { | |
1128 | // need to rebuild accell table | |
1129 | RebuildAccelTable(); | |
1130 | } | |
1131 | #endif // wxUSE_ACCEL | |
1132 | ||
ad9bb75f VZ |
1133 | Refresh(); |
1134 | } | |
2bda0e17 | 1135 | |
c4053ed3 RN |
1136 | |
1137 | m_titles.RemoveAt(pos); | |
2bda0e17 | 1138 | |
a8cfd0cb | 1139 | return menu; |
2bda0e17 KB |
1140 | } |
1141 | ||
d427503c | 1142 | #if wxUSE_ACCEL |
717a57c2 VZ |
1143 | |
1144 | void wxMenuBar::RebuildAccelTable() | |
1145 | { | |
1146 | // merge the accelerators of all menus into one accel table | |
42e69d6b | 1147 | size_t nAccelCount = 0; |
a8cfd0cb | 1148 | size_t i, count = GetMenuCount(); |
222ed1d6 MB |
1149 | wxMenuList::iterator it; |
1150 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
42e69d6b | 1151 | { |
222ed1d6 | 1152 | nAccelCount += (*it)->GetAccelCount(); |
42e69d6b VZ |
1153 | } |
1154 | ||
5df1250b | 1155 | if ( nAccelCount ) |
42e69d6b | 1156 | { |
5df1250b | 1157 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; |
42e69d6b | 1158 | |
5df1250b | 1159 | nAccelCount = 0; |
222ed1d6 | 1160 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) |
5df1250b | 1161 | { |
222ed1d6 | 1162 | nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]); |
5df1250b VZ |
1163 | } |
1164 | ||
1165 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
42e69d6b | 1166 | |
5df1250b VZ |
1167 | delete [] accelEntries; |
1168 | } | |
717a57c2 VZ |
1169 | } |
1170 | ||
1171 | #endif // wxUSE_ACCEL | |
1172 | ||
1173 | void wxMenuBar::Attach(wxFrame *frame) | |
1174 | { | |
1e6feb95 | 1175 | wxMenuBarBase::Attach(frame); |
717a57c2 | 1176 | |
3fd239fa | 1177 | #if defined(WINCE_WITH_COMMANDBAR) |
a96b4743 JS |
1178 | if (!m_hMenu) |
1179 | this->Create(); | |
a96b4743 JS |
1180 | if (!m_commandBar) |
1181 | m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId()); | |
1182 | if (m_commandBar) | |
1183 | { | |
1184 | if (m_hMenu) | |
1185 | { | |
1186 | if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0)) | |
1187 | { | |
1188 | wxLogLastError(wxT("CommandBar_InsertMenubarEx")); | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | #endif | |
a96b4743 | 1193 | |
717a57c2 VZ |
1194 | #if wxUSE_ACCEL |
1195 | RebuildAccelTable(); | |
d427503c | 1196 | #endif // wxUSE_ACCEL |
42e69d6b VZ |
1197 | } |
1198 | ||
3fd239fa | 1199 | #if defined(WINCE_WITH_COMMANDBAR) |
a9928e9d JS |
1200 | bool wxMenuBar::AddAdornments(long style) |
1201 | { | |
1202 | if (m_adornmentsAdded || !m_commandBar) | |
1203 | return false; | |
1204 | ||
1205 | if (style & wxCLOSE_BOX) | |
1206 | { | |
1207 | if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0)) | |
1208 | wxLogLastError(wxT("CommandBar_AddAdornments")); | |
1209 | else | |
1210 | return true; | |
1211 | } | |
1212 | return false; | |
1213 | } | |
1214 | #endif | |
1215 | ||
1cf27c63 UM |
1216 | void wxMenuBar::Detach() |
1217 | { | |
1e6feb95 | 1218 | wxMenuBarBase::Detach(); |
2bda0e17 KB |
1219 | } |
1220 | ||
1e6feb95 | 1221 | #endif // wxUSE_MENUS |