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