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