]>
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 <commctrl.h> | |
53 | #include <aygshell.h> | |
54 | ||
55 | #ifndef TBSTYLE_NO_DROPDOWN_ARROW | |
56 | #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080 | |
57 | #endif | |
58 | ||
59 | #endif | |
60 | ||
61 | // other standard headers | |
62 | #include <string.h> | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // global variables | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | extern wxMenu *wxCurrentPopupMenu; | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // constants | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | // the (popup) menu title has this special id | |
75 | static const int idMenuTitle = -2; | |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // private functions | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | // make the given menu item default | |
82 | static void SetDefaultMenuItem(HMENU hmenu, UINT id) | |
83 | { | |
84 | #ifndef __WXWINCE__ | |
85 | MENUITEMINFO mii; | |
86 | wxZeroMemory(mii); | |
87 | mii.cbSize = sizeof(MENUITEMINFO); | |
88 | mii.fMask = MIIM_STATE; | |
89 | mii.fState = MFS_DEFAULT; | |
90 | ||
91 | if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) ) | |
92 | { | |
93 | wxLogLastError(wxT("SetMenuItemInfo")); | |
94 | } | |
95 | #endif | |
96 | } | |
97 | ||
98 | #ifdef __WXWINCE__ | |
99 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) | |
100 | { | |
101 | MENUITEMINFO info; | |
102 | wxZeroMemory(info); | |
103 | info.cbSize = sizeof(info); | |
104 | info.fMask = MIIM_STATE; | |
105 | if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) ) | |
106 | wxLogLastError(wxT("GetMenuItemInfo")); | |
107 | return info.fState; | |
108 | } | |
109 | #endif | |
110 | ||
111 | // ============================================================================ | |
112 | // implementation | |
113 | // ============================================================================ | |
114 | ||
115 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
116 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) | |
117 | ||
118 | /* | |
119 | TODO PROPERTIES | |
120 | wxMenu | |
121 | label | |
122 | help | |
123 | ||
124 | separator | |
125 | break | |
126 | label | |
127 | accel | |
128 | radio | |
129 | checkable | |
130 | help | |
131 | bitmap | |
132 | wxMenuItem | |
133 | */ | |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | // wxMenu construction, adding and removing menu items | |
137 | // --------------------------------------------------------------------------- | |
138 | ||
139 | // Construct a menu with optional title (then use append) | |
140 | void wxMenu::Init() | |
141 | { | |
142 | m_doBreak = FALSE; | |
143 | m_startRadioGroup = -1; | |
144 | ||
145 | // create the menu | |
146 | m_hMenu = (WXHMENU)CreatePopupMenu(); | |
147 | if ( !m_hMenu ) | |
148 | { | |
149 | wxLogLastError(wxT("CreatePopupMenu")); | |
150 | } | |
151 | ||
152 | // if we have a title, insert it in the beginning of the menu | |
153 | if ( !!m_title ) | |
154 | { | |
155 | Append(idMenuTitle, m_title); | |
156 | AppendSeparator(); | |
157 | } | |
158 | } | |
159 | ||
160 | // The wxWindow destructor will take care of deleting the submenus. | |
161 | wxMenu::~wxMenu() | |
162 | { | |
163 | // we should free Windows resources only if Windows doesn't do it for us | |
164 | // which happens if we're attached to a menubar or a submenu of another | |
165 | // menu | |
166 | if ( !IsAttached() && !GetParent() ) | |
167 | { | |
168 | if ( !::DestroyMenu(GetHmenu()) ) | |
169 | { | |
170 | wxLogLastError(wxT("DestroyMenu")); | |
171 | } | |
172 | } | |
173 | ||
174 | #if wxUSE_ACCEL | |
175 | // delete accels | |
176 | WX_CLEAR_ARRAY(m_accels); | |
177 | #endif // wxUSE_ACCEL | |
178 | } | |
179 | ||
180 | void wxMenu::Break() | |
181 | { | |
182 | // this will take effect during the next call to Append() | |
183 | m_doBreak = TRUE; | |
184 | } | |
185 | ||
186 | void wxMenu::Attach(wxMenuBarBase *menubar) | |
187 | { | |
188 | wxMenuBase::Attach(menubar); | |
189 | ||
190 | EndRadioGroup(); | |
191 | } | |
192 | ||
193 | #if wxUSE_ACCEL | |
194 | ||
195 | int wxMenu::FindAccel(int id) const | |
196 | { | |
197 | size_t n, count = m_accels.GetCount(); | |
198 | for ( n = 0; n < count; n++ ) | |
199 | { | |
200 | if ( m_accels[n]->m_command == id ) | |
201 | return n; | |
202 | } | |
203 | ||
204 | return wxNOT_FOUND; | |
205 | } | |
206 | ||
207 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
208 | { | |
209 | if ( item->IsSubMenu() ) | |
210 | { | |
211 | wxMenu *submenu = item->GetSubMenu(); | |
212 | wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst(); | |
213 | while ( node ) | |
214 | { | |
215 | UpdateAccel(node->GetData()); | |
216 | ||
217 | node = node->GetNext(); | |
218 | } | |
219 | } | |
220 | else if ( !item->IsSeparator() ) | |
221 | { | |
222 | // find the (new) accel for this item | |
223 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
224 | if ( accel ) | |
225 | accel->m_command = item->GetId(); | |
226 | ||
227 | // find the old one | |
228 | int n = FindAccel(item->GetId()); | |
229 | if ( n == wxNOT_FOUND ) | |
230 | { | |
231 | // no old, add new if any | |
232 | if ( accel ) | |
233 | m_accels.Add(accel); | |
234 | else | |
235 | return; // skipping RebuildAccelTable() below | |
236 | } | |
237 | else | |
238 | { | |
239 | // replace old with new or just remove the old one if no new | |
240 | delete m_accels[n]; | |
241 | if ( accel ) | |
242 | m_accels[n] = accel; | |
243 | else | |
244 | m_accels.RemoveAt(n); | |
245 | } | |
246 | ||
247 | if ( IsAttached() ) | |
248 | { | |
249 | m_menuBar->RebuildAccelTable(); | |
250 | } | |
251 | } | |
252 | //else: it is a separator, they can't have accels, nothing to do | |
253 | } | |
254 | ||
255 | #endif // wxUSE_ACCEL | |
256 | ||
257 | // append a new item or submenu to the menu | |
258 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
259 | { | |
260 | #if wxUSE_ACCEL | |
261 | UpdateAccel(pItem); | |
262 | #endif // wxUSE_ACCEL | |
263 | ||
264 | UINT flags = 0; | |
265 | ||
266 | // if "Break" has just been called, insert a menu break before this item | |
267 | // (and don't forget to reset the flag) | |
268 | if ( m_doBreak ) { | |
269 | flags |= MF_MENUBREAK; | |
270 | m_doBreak = FALSE; | |
271 | } | |
272 | ||
273 | if ( pItem->IsSeparator() ) { | |
274 | flags |= MF_SEPARATOR; | |
275 | } | |
276 | ||
277 | // id is the numeric id for normal menu items and HMENU for submenus as | |
278 | // required by ::AppendMenu() API | |
279 | UINT id; | |
280 | wxMenu *submenu = pItem->GetSubMenu(); | |
281 | if ( submenu != NULL ) { | |
282 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); | |
283 | ||
284 | submenu->SetParent(this); | |
285 | ||
286 | id = (UINT)submenu->GetHMenu(); | |
287 | ||
288 | flags |= MF_POPUP; | |
289 | } | |
290 | else { | |
291 | id = pItem->GetId(); | |
292 | } | |
293 | ||
294 | #ifdef __WXWINCE__ | |
295 | wxString strippedString; | |
296 | #endif | |
297 | ||
298 | LPCTSTR pData; | |
299 | ||
300 | #if wxUSE_OWNER_DRAWN | |
301 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? | |
302 | // item draws itself, pass pointer to it in data parameter | |
303 | flags |= MF_OWNERDRAW; | |
304 | pData = (LPCTSTR)pItem; | |
305 | } | |
306 | else | |
307 | #endif | |
308 | { | |
309 | // menu is just a normal string (passed in data parameter) | |
310 | flags |= MF_STRING; | |
311 | ||
312 | #ifdef __WXWINCE__ | |
313 | strippedString = wxStripMenuCodes(pItem->GetText()); | |
314 | pData = (wxChar*)strippedString.c_str(); | |
315 | #else | |
316 | pData = (wxChar*)pItem->GetText().c_str(); | |
317 | #endif | |
318 | } | |
319 | ||
320 | BOOL ok; | |
321 | if ( pos == (size_t)-1 ) | |
322 | { | |
323 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); | |
324 | } | |
325 | else | |
326 | { | |
327 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); | |
328 | } | |
329 | ||
330 | if ( !ok ) | |
331 | { | |
332 | wxLogLastError(wxT("Insert or AppendMenu")); | |
333 | ||
334 | return FALSE; | |
335 | } | |
336 | ||
337 | // if we just appended the title, highlight it | |
338 | #ifdef __WIN32__ | |
339 | if ( (int)id == idMenuTitle ) | |
340 | { | |
341 | // visually select the menu title | |
342 | SetDefaultMenuItem(GetHmenu(), id); | |
343 | } | |
344 | #endif // __WIN32__ | |
345 | ||
346 | // if we're already attached to the menubar, we must update it | |
347 | if ( IsAttached() && m_menuBar->IsAttached() ) | |
348 | { | |
349 | m_menuBar->Refresh(); | |
350 | } | |
351 | ||
352 | return TRUE; | |
353 | } | |
354 | ||
355 | void wxMenu::EndRadioGroup() | |
356 | { | |
357 | // we're not inside a radio group any longer | |
358 | m_startRadioGroup = -1; | |
359 | } | |
360 | ||
361 | bool wxMenu::DoAppend(wxMenuItem *item) | |
362 | { | |
363 | wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") ); | |
364 | ||
365 | bool check = FALSE; | |
366 | ||
367 | if ( item->GetKind() == wxITEM_RADIO ) | |
368 | { | |
369 | int count = GetMenuItemCount(); | |
370 | ||
371 | if ( m_startRadioGroup == -1 ) | |
372 | { | |
373 | // start a new radio group | |
374 | m_startRadioGroup = count; | |
375 | ||
376 | // for now it has just one element | |
377 | item->SetAsRadioGroupStart(); | |
378 | item->SetRadioGroupEnd(m_startRadioGroup); | |
379 | ||
380 | // ensure that we have a checked item in the radio group | |
381 | check = TRUE; | |
382 | } | |
383 | else // extend the current radio group | |
384 | { | |
385 | // we need to update its end item | |
386 | item->SetRadioGroupStart(m_startRadioGroup); | |
387 | wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup); | |
388 | ||
389 | if ( node ) | |
390 | { | |
391 | node->GetData()->SetRadioGroupEnd(count); | |
392 | } | |
393 | else | |
394 | { | |
395 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
396 | } | |
397 | } | |
398 | } | |
399 | else // not a radio item | |
400 | { | |
401 | EndRadioGroup(); | |
402 | } | |
403 | ||
404 | if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) | |
405 | { | |
406 | return FALSE; | |
407 | } | |
408 | ||
409 | if ( check ) | |
410 | { | |
411 | // check the item initially | |
412 | item->Check(TRUE); | |
413 | } | |
414 | ||
415 | return TRUE; | |
416 | } | |
417 | ||
418 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
419 | { | |
420 | return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos); | |
421 | } | |
422 | ||
423 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
424 | { | |
425 | // we need to find the items position in the child list | |
426 | size_t pos; | |
427 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
428 | for ( pos = 0; node; pos++ ) | |
429 | { | |
430 | if ( node->GetData() == item ) | |
431 | break; | |
432 | ||
433 | node = node->GetNext(); | |
434 | } | |
435 | ||
436 | // DoRemove() (unlike Remove) can only be called for existing item! | |
437 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
438 | ||
439 | #if wxUSE_ACCEL | |
440 | // remove the corresponding accel from the accel table | |
441 | int n = FindAccel(item->GetId()); | |
442 | if ( n != wxNOT_FOUND ) | |
443 | { | |
444 | delete m_accels[n]; | |
445 | ||
446 | m_accels.RemoveAt(n); | |
447 | } | |
448 | //else: this item doesn't have an accel, nothing to do | |
449 | #endif // wxUSE_ACCEL | |
450 | ||
451 | // remove the item from the menu | |
452 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
453 | { | |
454 | wxLogLastError(wxT("RemoveMenu")); | |
455 | } | |
456 | ||
457 | if ( IsAttached() && m_menuBar->IsAttached() ) | |
458 | { | |
459 | // otherwise, the chane won't be visible | |
460 | m_menuBar->Refresh(); | |
461 | } | |
462 | ||
463 | // and from internal data structures | |
464 | return wxMenuBase::DoRemove(item); | |
465 | } | |
466 | ||
467 | // --------------------------------------------------------------------------- | |
468 | // accelerator helpers | |
469 | // --------------------------------------------------------------------------- | |
470 | ||
471 | #if wxUSE_ACCEL | |
472 | ||
473 | // create the wxAcceleratorEntries for our accels and put them into provided | |
474 | // array - return the number of accels we have | |
475 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
476 | { | |
477 | size_t count = GetAccelCount(); | |
478 | for ( size_t n = 0; n < count; n++ ) | |
479 | { | |
480 | *accels++ = *m_accels[n]; | |
481 | } | |
482 | ||
483 | return count; | |
484 | } | |
485 | ||
486 | #endif // wxUSE_ACCEL | |
487 | ||
488 | // --------------------------------------------------------------------------- | |
489 | // set wxMenu title | |
490 | // --------------------------------------------------------------------------- | |
491 | ||
492 | void wxMenu::SetTitle(const wxString& label) | |
493 | { | |
494 | bool hasNoTitle = m_title.IsEmpty(); | |
495 | m_title = label; | |
496 | ||
497 | HMENU hMenu = GetHmenu(); | |
498 | ||
499 | if ( hasNoTitle ) | |
500 | { | |
501 | if ( !label.IsEmpty() ) | |
502 | { | |
503 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
504 | (unsigned)idMenuTitle, m_title) || | |
505 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
506 | { | |
507 | wxLogLastError(wxT("InsertMenu")); | |
508 | } | |
509 | } | |
510 | } | |
511 | else | |
512 | { | |
513 | if ( label.IsEmpty() ) | |
514 | { | |
515 | // remove the title and the separator after it | |
516 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
517 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
518 | { | |
519 | wxLogLastError(wxT("RemoveMenu")); | |
520 | } | |
521 | } | |
522 | else | |
523 | { | |
524 | // modify the title | |
525 | #ifdef __WXWINCE__ | |
526 | MENUITEMINFO info; | |
527 | wxZeroMemory(info); | |
528 | info.cbSize = sizeof(info); | |
529 | info.fMask = MIIM_TYPE; | |
530 | info.fType = MFT_STRING; | |
531 | info.cch = m_title.Length(); | |
532 | info.dwTypeData = (LPTSTR) m_title.c_str(); | |
533 | if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) ) | |
534 | { | |
535 | wxLogLastError(wxT("SetMenuItemInfo")); | |
536 | } | |
537 | #else | |
538 | if ( !ModifyMenu(hMenu, 0u, | |
539 | MF_BYPOSITION | MF_STRING, | |
540 | (unsigned)idMenuTitle, m_title) ) | |
541 | { | |
542 | wxLogLastError(wxT("ModifyMenu")); | |
543 | } | |
544 | #endif | |
545 | } | |
546 | } | |
547 | ||
548 | #ifdef __WIN32__ | |
549 | // put the title string in bold face | |
550 | if ( !m_title.IsEmpty() ) | |
551 | { | |
552 | SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); | |
553 | } | |
554 | #endif // Win32 | |
555 | } | |
556 | ||
557 | // --------------------------------------------------------------------------- | |
558 | // event processing | |
559 | // --------------------------------------------------------------------------- | |
560 | ||
561 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) | |
562 | { | |
563 | // ignore commands from the menu title | |
564 | ||
565 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
566 | if ( id != (WXWORD)idMenuTitle ) | |
567 | { | |
568 | // VZ: previosuly, the command int was set to id too which was quite | |
569 | // useless anyhow (as it could be retrieved using GetId()) and | |
570 | // uncompatible with wxGTK, so now we use the command int instead | |
571 | // to pass the checked status | |
572 | UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ; | |
573 | SendEvent(id, menuState & MF_CHECKED); | |
574 | } | |
575 | ||
576 | return TRUE; | |
577 | } | |
578 | ||
579 | // --------------------------------------------------------------------------- | |
580 | // other | |
581 | // --------------------------------------------------------------------------- | |
582 | ||
583 | wxWindow *wxMenu::GetWindow() const | |
584 | { | |
585 | if ( m_invokingWindow != NULL ) | |
586 | return m_invokingWindow; | |
587 | else if ( m_menuBar != NULL) | |
588 | return m_menuBar->GetFrame(); | |
589 | ||
590 | return NULL; | |
591 | } | |
592 | ||
593 | // --------------------------------------------------------------------------- | |
594 | // Menu Bar | |
595 | // --------------------------------------------------------------------------- | |
596 | ||
597 | void wxMenuBar::Init() | |
598 | { | |
599 | m_eventHandler = this; | |
600 | m_hMenu = 0; | |
601 | #ifdef __WXWINCE__ | |
602 | m_toolBar = NULL; | |
603 | #endif | |
604 | } | |
605 | ||
606 | wxMenuBar::wxMenuBar() | |
607 | { | |
608 | Init(); | |
609 | } | |
610 | ||
611 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
612 | { | |
613 | Init(); | |
614 | } | |
615 | ||
616 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
617 | { | |
618 | Init(); | |
619 | ||
620 | m_titles.Alloc(count); | |
621 | ||
622 | for ( int i = 0; i < count; i++ ) | |
623 | { | |
624 | m_menus.Append(menus[i]); | |
625 | m_titles.Add(titles[i]); | |
626 | ||
627 | menus[i]->Attach(this); | |
628 | } | |
629 | } | |
630 | ||
631 | wxMenuBar::~wxMenuBar() | |
632 | { | |
633 | // In Windows CE, the menubar is always associated | |
634 | // with a toolbar, which destroys the menu implicitly. | |
635 | #ifdef __WXWINCE__ | |
636 | if (GetToolBar()) | |
637 | GetToolBar()->SetMenuBar(NULL); | |
638 | #else | |
639 | // we should free Windows resources only if Windows doesn't do it for us | |
640 | // which happens if we're attached to a frame | |
641 | if (m_hMenu && !IsAttached()) | |
642 | { | |
643 | ::DestroyMenu((HMENU)m_hMenu); | |
644 | m_hMenu = (WXHMENU)NULL; | |
645 | } | |
646 | #endif | |
647 | } | |
648 | ||
649 | // --------------------------------------------------------------------------- | |
650 | // wxMenuBar helpers | |
651 | // --------------------------------------------------------------------------- | |
652 | ||
653 | void wxMenuBar::Refresh() | |
654 | { | |
655 | wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); | |
656 | ||
657 | #ifdef __WXWINCE__ | |
658 | if (GetToolBar()) | |
659 | { | |
660 | CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0); | |
661 | } | |
662 | #else | |
663 | DrawMenuBar(GetHwndOf(GetFrame())); | |
664 | #endif | |
665 | } | |
666 | ||
667 | WXHMENU wxMenuBar::Create() | |
668 | { | |
669 | // Note: this totally doesn't work on Smartphone, | |
670 | // since you have to use resources. | |
671 | // We'll have to find another way to add a menu | |
672 | // by changing/adding menu items to an existing menu. | |
673 | #ifdef __WXWINCE__ | |
674 | if ( m_hMenu != 0 ) | |
675 | return m_hMenu; | |
676 | ||
677 | if (!GetToolBar()) | |
678 | return 0; | |
679 | ||
680 | HWND hCommandBar = (HWND) GetToolBar()->GetHWND(); | |
681 | HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0); | |
682 | if (hMenu) | |
683 | { | |
684 | TBBUTTON tbButton; | |
685 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
686 | tbButton.iBitmap = I_IMAGENONE; | |
687 | tbButton.fsState = TBSTATE_ENABLED; | |
688 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
689 | ||
690 | size_t i; | |
691 | for (i = 0; i < GetMenuCount(); i++) | |
692 | { | |
693 | HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ; | |
694 | tbButton.dwData = (DWORD)hPopupMenu; | |
695 | wxString label = wxStripMenuCodes(GetLabelTop(i)); | |
696 | tbButton.iString = (int) label.c_str(); | |
697 | ||
698 | int position = i; | |
699 | ||
700 | tbButton.idCommand = NewControlId(); | |
701 | if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton)) | |
702 | { | |
703 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
704 | } | |
705 | } | |
706 | } | |
707 | m_hMenu = (WXHMENU) hMenu; | |
708 | return m_hMenu; | |
709 | #else | |
710 | if ( m_hMenu != 0 ) | |
711 | return m_hMenu; | |
712 | ||
713 | m_hMenu = (WXHMENU)::CreateMenu(); | |
714 | ||
715 | if ( !m_hMenu ) | |
716 | { | |
717 | wxLogLastError(wxT("CreateMenu")); | |
718 | } | |
719 | else | |
720 | { | |
721 | size_t count = GetMenuCount(), i; | |
722 | wxMenuList::iterator it; | |
723 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
724 | { | |
725 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
726 | (UINT)(*it)->GetHMenu(), | |
727 | m_titles[i]) ) | |
728 | { | |
729 | wxLogLastError(wxT("AppendMenu")); | |
730 | } | |
731 | } | |
732 | } | |
733 | ||
734 | return m_hMenu; | |
735 | #endif | |
736 | } | |
737 | ||
738 | // --------------------------------------------------------------------------- | |
739 | // wxMenuBar functions to work with the top level submenus | |
740 | // --------------------------------------------------------------------------- | |
741 | ||
742 | // NB: we don't support owner drawn top level items for now, if we do these | |
743 | // functions would have to be changed to use wxMenuItem as well | |
744 | ||
745 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
746 | { | |
747 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); | |
748 | ||
749 | int flag = enable ? MF_ENABLED : MF_GRAYED; | |
750 | ||
751 | EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); | |
752 | ||
753 | Refresh(); | |
754 | } | |
755 | ||
756 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) | |
757 | { | |
758 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
759 | ||
760 | m_titles[pos] = label; | |
761 | ||
762 | if ( !IsAttached() ) | |
763 | { | |
764 | return; | |
765 | } | |
766 | //else: have to modify the existing menu | |
767 | ||
768 | UINT id; | |
769 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); | |
770 | if ( flagsOld == 0xFFFFFFFF ) | |
771 | { | |
772 | wxLogLastError(wxT("GetMenuState")); | |
773 | ||
774 | return; | |
775 | } | |
776 | ||
777 | if ( flagsOld & MF_POPUP ) | |
778 | { | |
779 | // HIBYTE contains the number of items in the submenu in this case | |
780 | flagsOld &= 0xff; | |
781 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos); | |
782 | } | |
783 | else | |
784 | { | |
785 | id = pos; | |
786 | } | |
787 | ||
788 | #ifdef __WXWINCE__ | |
789 | MENUITEMINFO info; | |
790 | wxZeroMemory(info); | |
791 | info.cbSize = sizeof(info); | |
792 | info.fMask = MIIM_TYPE; | |
793 | info.fType = MFT_STRING; | |
794 | info.cch = label.Length(); | |
795 | info.dwTypeData = (LPTSTR) label.c_str(); | |
796 | if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) ) | |
797 | { | |
798 | wxLogLastError(wxT("SetMenuItemInfo")); | |
799 | } | |
800 | ||
801 | #else | |
802 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, | |
803 | id, label) == (int)0xFFFFFFFF ) | |
804 | { | |
805 | wxLogLastError(wxT("ModifyMenu")); | |
806 | } | |
807 | #endif | |
808 | ||
809 | Refresh(); | |
810 | } | |
811 | ||
812 | wxString wxMenuBar::GetLabelTop(size_t pos) const | |
813 | { | |
814 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, | |
815 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
816 | ||
817 | return m_titles[pos]; | |
818 | } | |
819 | ||
820 | // --------------------------------------------------------------------------- | |
821 | // wxMenuBar construction | |
822 | // --------------------------------------------------------------------------- | |
823 | ||
824 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
825 | { | |
826 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); | |
827 | if ( !menuOld ) | |
828 | return NULL; | |
829 | ||
830 | m_titles[pos] = title; | |
831 | ||
832 | if ( IsAttached() ) | |
833 | { | |
834 | // can't use ModifyMenu() because it deletes the submenu it replaces | |
835 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
836 | { | |
837 | wxLogLastError(wxT("RemoveMenu")); | |
838 | } | |
839 | ||
840 | if ( !::InsertMenu(GetHmenu(), (UINT)pos, | |
841 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
842 | (UINT)GetHmenuOf(menu), title) ) | |
843 | { | |
844 | wxLogLastError(wxT("InsertMenu")); | |
845 | } | |
846 | ||
847 | #if wxUSE_ACCEL | |
848 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
849 | { | |
850 | // need to rebuild accell table | |
851 | RebuildAccelTable(); | |
852 | } | |
853 | #endif // wxUSE_ACCEL | |
854 | ||
855 | Refresh(); | |
856 | } | |
857 | ||
858 | return menuOld; | |
859 | } | |
860 | ||
861 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
862 | { | |
863 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
864 | return FALSE; | |
865 | ||
866 | m_titles.Insert(title, pos); | |
867 | ||
868 | if ( IsAttached() ) | |
869 | { | |
870 | #ifdef __WXWINCE__ | |
871 | if (!GetToolBar()) | |
872 | return FALSE; | |
873 | TBBUTTON tbButton; | |
874 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
875 | tbButton.iBitmap = I_IMAGENONE; | |
876 | tbButton.fsState = TBSTATE_ENABLED; | |
877 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
878 | ||
879 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
880 | tbButton.dwData = (DWORD)hPopupMenu; | |
881 | wxString label = wxStripMenuCodes(title); | |
882 | tbButton.iString = (int) label.c_str(); | |
883 | ||
884 | tbButton.idCommand = NewControlId(); | |
885 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
886 | { | |
887 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
888 | return FALSE; | |
889 | } | |
890 | #else | |
891 | if ( !::InsertMenu(GetHmenu(), pos, | |
892 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
893 | (UINT)GetHmenuOf(menu), title) ) | |
894 | { | |
895 | wxLogLastError(wxT("InsertMenu")); | |
896 | } | |
897 | #endif | |
898 | #if wxUSE_ACCEL | |
899 | if ( menu->HasAccels() ) | |
900 | { | |
901 | // need to rebuild accell table | |
902 | RebuildAccelTable(); | |
903 | } | |
904 | #endif // wxUSE_ACCEL | |
905 | ||
906 | Refresh(); | |
907 | } | |
908 | ||
909 | return TRUE; | |
910 | } | |
911 | ||
912 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
913 | { | |
914 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
915 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
916 | ||
917 | if ( !wxMenuBarBase::Append(menu, title) ) | |
918 | return FALSE; | |
919 | ||
920 | m_titles.Add(title); | |
921 | ||
922 | if ( IsAttached() ) | |
923 | { | |
924 | #ifdef __WXWINCE__ | |
925 | if (!GetToolBar()) | |
926 | return FALSE; | |
927 | TBBUTTON tbButton; | |
928 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
929 | tbButton.iBitmap = I_IMAGENONE; | |
930 | tbButton.fsState = TBSTATE_ENABLED; | |
931 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
932 | ||
933 | size_t pos = GetMenuCount(); | |
934 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
935 | tbButton.dwData = (DWORD)hPopupMenu; | |
936 | wxString label = wxStripMenuCodes(title); | |
937 | tbButton.iString = (int) label.c_str(); | |
938 | ||
939 | tbButton.idCommand = NewControlId(); | |
940 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
941 | { | |
942 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
943 | return FALSE; | |
944 | } | |
945 | #else | |
946 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, | |
947 | (UINT)submenu, title) ) | |
948 | { | |
949 | wxLogLastError(wxT("AppendMenu")); | |
950 | } | |
951 | #endif | |
952 | ||
953 | #if wxUSE_ACCEL | |
954 | if ( menu->HasAccels() ) | |
955 | { | |
956 | // need to rebuild accelerator table | |
957 | RebuildAccelTable(); | |
958 | } | |
959 | #endif // wxUSE_ACCEL | |
960 | ||
961 | Refresh(); | |
962 | } | |
963 | ||
964 | return TRUE; | |
965 | } | |
966 | ||
967 | wxMenu *wxMenuBar::Remove(size_t pos) | |
968 | { | |
969 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
970 | if ( !menu ) | |
971 | return NULL; | |
972 | ||
973 | if ( IsAttached() ) | |
974 | { | |
975 | #ifdef __WXWINCE__ | |
976 | if (GetToolBar()) | |
977 | { | |
978 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0)) | |
979 | { | |
980 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
981 | } | |
982 | } | |
983 | #else | |
984 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
985 | { | |
986 | wxLogLastError(wxT("RemoveMenu")); | |
987 | } | |
988 | #endif | |
989 | #if wxUSE_ACCEL | |
990 | if ( menu->HasAccels() ) | |
991 | { | |
992 | // need to rebuild accell table | |
993 | RebuildAccelTable(); | |
994 | } | |
995 | #endif // wxUSE_ACCEL | |
996 | ||
997 | Refresh(); | |
998 | } | |
999 | ||
1000 | m_titles.RemoveAt(pos); | |
1001 | ||
1002 | return menu; | |
1003 | } | |
1004 | ||
1005 | #if wxUSE_ACCEL | |
1006 | ||
1007 | void wxMenuBar::RebuildAccelTable() | |
1008 | { | |
1009 | // merge the accelerators of all menus into one accel table | |
1010 | size_t nAccelCount = 0; | |
1011 | size_t i, count = GetMenuCount(); | |
1012 | wxMenuList::iterator it; | |
1013 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
1014 | { | |
1015 | nAccelCount += (*it)->GetAccelCount(); | |
1016 | } | |
1017 | ||
1018 | if ( nAccelCount ) | |
1019 | { | |
1020 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; | |
1021 | ||
1022 | nAccelCount = 0; | |
1023 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
1024 | { | |
1025 | nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]); | |
1026 | } | |
1027 | ||
1028 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
1029 | ||
1030 | delete [] accelEntries; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | #endif // wxUSE_ACCEL | |
1035 | ||
1036 | void wxMenuBar::Attach(wxFrame *frame) | |
1037 | { | |
1038 | wxMenuBarBase::Attach(frame); | |
1039 | ||
1040 | #if wxUSE_ACCEL | |
1041 | RebuildAccelTable(); | |
1042 | #endif // wxUSE_ACCEL | |
1043 | } | |
1044 | ||
1045 | void wxMenuBar::Detach() | |
1046 | { | |
1047 | wxMenuBarBase::Detach(); | |
1048 | } | |
1049 | ||
1050 | #endif // wxUSE_MENUS |