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