]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/10/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "menu.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/frame.h" | |
21 | #include "wx/menu.h" | |
22 | #include "wx/utils.h" | |
23 | #include "wx/intl.h" | |
24 | #include "wx/log.h" | |
25 | #endif | |
26 | ||
27 | #if wxUSE_OWNER_DRAWN | |
28 | #include "wx/ownerdrw.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/os2/private.h" | |
32 | ||
33 | // other standard headers | |
34 | #include <string.h> | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // global variables | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | extern wxMenu *wxCurrentPopupMenu; | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // constants | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // the (popup) menu title has this special id | |
47 | static const int idMenuTitle = -2; | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
54 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
55 | ||
56 | // ============================================================================ | |
57 | // implementation | |
58 | // ============================================================================ | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | // wxMenu construction, adding and removing menu items | |
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | // Construct a menu with optional title (then use append) | |
65 | void wxMenu::Init() | |
66 | { | |
67 | m_doBreak = FALSE; | |
68 | ||
69 | // create the menu | |
70 | m_hMenu = (WXHMENU)0; // CreatePopupMenu(); | |
71 | if ( !m_hMenu ) | |
72 | { | |
73 | wxLogLastError("CreatePopupMenu"); | |
74 | } | |
75 | ||
76 | // if we have a title, insert it in the beginning of the menu | |
77 | if ( !!m_title ) | |
78 | { | |
79 | Append(idMenuTitle, m_title); | |
80 | AppendSeparator(); | |
81 | } | |
82 | } | |
83 | ||
84 | // The wxWindow destructor will take care of deleting the submenus. | |
85 | wxMenu::~wxMenu() | |
86 | { | |
87 | // we should free Windows resources only if Windows doesn't do it for us | |
88 | // which happens if we're attached to a menubar or a submenu of another | |
89 | // menu | |
90 | if ( !IsAttached() && !GetParent() ) | |
91 | { | |
92 | /* | |
93 | if ( !::DestroyMenu(GetHmenu()) ) | |
94 | { | |
95 | wxLogLastError("DestroyMenu"); | |
96 | } | |
97 | */ | |
98 | } | |
99 | ||
100 | #if wxUSE_ACCEL | |
101 | // delete accels | |
102 | WX_CLEAR_ARRAY(m_accels); | |
103 | #endif // wxUSE_ACCEL | |
104 | } | |
105 | ||
106 | void wxMenu::Break() | |
107 | { | |
108 | // this will take effect during the next call to Append() | |
109 | m_doBreak = TRUE; | |
110 | } | |
111 | ||
112 | #if wxUSE_ACCEL | |
113 | ||
114 | int wxMenu::FindAccel(int id) const | |
115 | { | |
116 | size_t n, count = m_accels.GetCount(); | |
117 | for ( n = 0; n < count; n++ ) | |
118 | { | |
119 | if ( m_accels[n]->m_command == id ) | |
120 | return n; | |
121 | } | |
122 | ||
123 | return wxNOT_FOUND; | |
124 | } | |
125 | ||
126 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
127 | { | |
128 | // find the (new) accel for this item | |
129 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
130 | if ( accel ) | |
131 | accel->m_command = item->GetId(); | |
132 | ||
133 | // find the old one | |
134 | int n = FindAccel(item->GetId()); | |
135 | if ( n == wxNOT_FOUND ) | |
136 | { | |
137 | // no old, add new if any | |
138 | if ( accel ) | |
139 | m_accels.Add(accel); | |
140 | else | |
141 | return; // skipping RebuildAccelTable() below | |
142 | } | |
143 | else | |
144 | { | |
145 | // replace old with new or just remove the old one if no new | |
146 | delete m_accels[n]; | |
147 | if ( accel ) | |
148 | m_accels[n] = accel; | |
149 | else | |
150 | m_accels.Remove(n); | |
151 | } | |
152 | ||
153 | if ( IsAttached() ) | |
154 | { | |
155 | m_menuBar->RebuildAccelTable(); | |
156 | } | |
157 | } | |
158 | ||
159 | #endif // wxUSE_ACCEL | |
160 | ||
161 | // append a new item or submenu to the menu | |
162 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
163 | { | |
164 | #if wxUSE_ACCEL | |
165 | UpdateAccel(pItem); | |
166 | #endif // wxUSE_ACCEL | |
167 | ||
168 | UINT flags = 0; | |
169 | // TODO: | |
170 | /* | |
171 | // if "Break" has just been called, insert a menu break before this item | |
172 | // (and don't forget to reset the flag) | |
173 | if ( m_doBreak ) { | |
174 | flags |= MF_MENUBREAK; | |
175 | m_doBreak = FALSE; | |
176 | } | |
177 | ||
178 | if ( pItem->IsSeparator() ) { | |
179 | flags |= MF_SEPARATOR; | |
180 | } | |
181 | ||
182 | // id is the numeric id for normal menu items and HMENU for submenus as | |
183 | // required by ::AppendMenu() API | |
184 | UINT id; | |
185 | wxMenu *submenu = pItem->GetSubMenu(); | |
186 | if ( submenu != NULL ) { | |
187 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); | |
188 | ||
189 | submenu->SetParent(this); | |
190 | ||
191 | id = (UINT)submenu->GetHMenu(); | |
192 | ||
193 | flags |= MF_POPUP; | |
194 | } | |
195 | else { | |
196 | id = pItem->GetId(); | |
197 | } | |
198 | ||
199 | LPCTSTR pData; | |
200 | ||
201 | #if wxUSE_OWNER_DRAWN | |
202 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? | |
203 | // item draws itself, pass pointer to it in data parameter | |
204 | flags |= MF_OWNERDRAW; | |
205 | pData = (LPCTSTR)pItem; | |
206 | } | |
207 | else | |
208 | #endif | |
209 | { | |
210 | // menu is just a normal string (passed in data parameter) | |
211 | flags |= MF_STRING; | |
212 | ||
213 | pData = (char*)pItem->GetText().c_str(); | |
214 | } | |
215 | ||
216 | BOOL ok; | |
217 | if ( pos == (size_t)-1 ) | |
218 | { | |
219 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); | |
220 | } | |
221 | else | |
222 | { | |
223 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); | |
224 | } | |
225 | ||
226 | if ( !ok ) | |
227 | { | |
228 | wxLogLastError("Insert or AppendMenu"); | |
229 | ||
230 | return FALSE; | |
231 | } | |
232 | else | |
233 | { | |
234 | // if we just appended the title, highlight it | |
235 | #ifdef __WIN32__ | |
236 | if ( (int)id == idMenuTitle ) | |
237 | { | |
238 | // visually select the menu title | |
239 | MENUITEMINFO mii; | |
240 | mii.cbSize = sizeof(mii); | |
241 | mii.fMask = MIIM_STATE; | |
242 | mii.fState = MFS_DEFAULT; | |
243 | ||
244 | if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) | |
245 | { | |
246 | wxLogLastError(wxT("SetMenuItemInfo")); | |
247 | } | |
248 | } | |
249 | #endif // __WIN32__ | |
250 | ||
251 | // if we're already attached to the menubar, we must update it | |
252 | if ( IsAttached() ) | |
253 | { | |
254 | m_menuBar->Refresh(); | |
255 | } | |
256 | ||
257 | return TRUE; | |
258 | } | |
259 | */ | |
260 | return FALSE; | |
261 | } | |
262 | ||
263 | bool wxMenu::DoAppend(wxMenuItem *item) | |
264 | { | |
265 | return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item); | |
266 | } | |
267 | ||
268 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
269 | { | |
270 | return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos); | |
271 | } | |
272 | ||
273 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
274 | { | |
275 | // we need to find the items position in the child list | |
276 | size_t pos; | |
277 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
278 | for ( pos = 0; node; pos++ ) | |
279 | { | |
280 | if ( node->GetData() == item ) | |
281 | break; | |
282 | ||
283 | node = node->GetNext(); | |
284 | } | |
285 | ||
286 | // DoRemove() (unlike Remove) can only be called for existing item! | |
287 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
288 | ||
289 | #if wxUSE_ACCEL | |
290 | // remove the corresponding accel from the accel table | |
291 | int n = FindAccel(item->GetId()); | |
292 | if ( n != wxNOT_FOUND ) | |
293 | { | |
294 | delete m_accels[n]; | |
295 | ||
296 | m_accels.Remove(n); | |
297 | } | |
298 | //else: this item doesn't have an accel, nothing to do | |
299 | #endif // wxUSE_ACCEL | |
300 | /* | |
301 | // remove the item from the menu | |
302 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
303 | { | |
304 | wxLogLastError("RemoveMenu"); | |
305 | } | |
306 | */ | |
307 | if ( IsAttached() ) | |
308 | { | |
309 | // otherwise, the chane won't be visible | |
310 | m_menuBar->Refresh(); | |
311 | } | |
312 | ||
313 | // and from internal data structures | |
314 | return wxMenuBase::DoRemove(item); | |
315 | } | |
316 | ||
317 | // --------------------------------------------------------------------------- | |
318 | // accelerator helpers | |
319 | // --------------------------------------------------------------------------- | |
320 | ||
321 | #if wxUSE_ACCEL | |
322 | ||
323 | // create the wxAcceleratorEntries for our accels and put them into provided | |
324 | // array - return the number of accels we have | |
325 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
326 | { | |
327 | size_t count = GetAccelCount(); | |
328 | for ( size_t n = 0; n < count; n++ ) | |
329 | { | |
330 | *accels++ = *m_accels[n]; | |
331 | } | |
332 | ||
333 | return count; | |
334 | } | |
335 | ||
336 | #endif // wxUSE_ACCEL | |
337 | ||
338 | // --------------------------------------------------------------------------- | |
339 | // set wxMenu title | |
340 | // --------------------------------------------------------------------------- | |
341 | ||
342 | void wxMenu::SetTitle(const wxString& label) | |
343 | { | |
344 | bool hasNoTitle = m_title.IsEmpty(); | |
345 | m_title = label; | |
346 | ||
347 | HMENU hMenu = GetHmenu(); | |
348 | ||
349 | if ( hasNoTitle ) | |
350 | { | |
351 | if ( !label.IsEmpty() ) | |
352 | { | |
353 | /* | |
354 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
355 | (unsigned)idMenuTitle, m_title) || | |
356 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
357 | { | |
358 | wxLogLastError("InsertMenu"); | |
359 | } | |
360 | */ | |
361 | } | |
362 | } | |
363 | else | |
364 | { | |
365 | if ( label.IsEmpty() ) | |
366 | { | |
367 | /* | |
368 | // remove the title and the separator after it | |
369 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
370 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
371 | { | |
372 | wxLogLastError("RemoveMenu"); | |
373 | } | |
374 | */ | |
375 | } | |
376 | else | |
377 | { | |
378 | /* | |
379 | // modify the title | |
380 | if ( !ModifyMenu(hMenu, 0u, | |
381 | MF_BYPOSITION | MF_STRING, | |
382 | (unsigned)idMenuTitle, m_title) ) | |
383 | { | |
384 | wxLogLastError("ModifyMenu"); | |
385 | } | |
386 | */ | |
387 | } | |
388 | } | |
389 | /* | |
390 | #ifdef __WIN32__ | |
391 | // put the title string in bold face | |
392 | if ( !m_title.IsEmpty() ) | |
393 | { | |
394 | MENUITEMINFO mii; | |
395 | mii.cbSize = sizeof(mii); | |
396 | mii.fMask = MIIM_STATE; | |
397 | mii.fState = MFS_DEFAULT; | |
398 | ||
399 | if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) | |
400 | { | |
401 | wxLogLastError("SetMenuItemInfo"); | |
402 | } | |
403 | } | |
404 | #endif // Win32 | |
405 | */ | |
406 | } | |
407 | ||
408 | // --------------------------------------------------------------------------- | |
409 | // event processing | |
410 | // --------------------------------------------------------------------------- | |
411 | ||
412 | bool wxMenu::OS2Command(WXUINT WXUNUSED(param), WXWORD id) | |
413 | { | |
414 | // ignore commands from the menu title | |
415 | ||
416 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
417 | if ( id != (WXWORD)idMenuTitle ) | |
418 | { | |
419 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); | |
420 | event.SetEventObject( this ); | |
421 | event.SetId( id ); | |
422 | event.SetInt( id ); | |
423 | ProcessCommand(event); | |
424 | } | |
425 | ||
426 | return TRUE; | |
427 | } | |
428 | ||
429 | bool wxMenu::ProcessCommand(wxCommandEvent & event) | |
430 | { | |
431 | bool processed = FALSE; | |
432 | ||
433 | #if WXWIN_COMPATIBILITY | |
434 | // Try a callback | |
435 | if (m_callback) | |
436 | { | |
437 | (void)(*(m_callback))(*this, event); | |
438 | processed = TRUE; | |
439 | } | |
440 | #endif // WXWIN_COMPATIBILITY | |
441 | ||
442 | // Try the menu's event handler | |
443 | if ( !processed && GetEventHandler()) | |
444 | { | |
445 | processed = GetEventHandler()->ProcessEvent(event); | |
446 | } | |
447 | ||
448 | // Try the window the menu was popped up from (and up through the | |
449 | // hierarchy) | |
450 | wxWindow *win = GetInvokingWindow(); | |
451 | if ( !processed && win ) | |
452 | processed = win->GetEventHandler()->ProcessEvent(event); | |
453 | ||
454 | return processed; | |
455 | } | |
456 | ||
457 | // --------------------------------------------------------------------------- | |
458 | // other | |
459 | // --------------------------------------------------------------------------- | |
460 | ||
461 | void wxMenu::Attach(wxMenuBar *menubar) | |
462 | { | |
463 | // menu can be in at most one menubar because otherwise they would both | |
464 | // delete the menu pointer | |
465 | wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") ); | |
466 | ||
467 | m_menuBar = menubar; | |
468 | } | |
469 | ||
470 | void wxMenu::Detach() | |
471 | { | |
472 | wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") ); | |
473 | ||
474 | m_menuBar = NULL; | |
475 | } | |
476 | ||
477 | wxWindow *wxMenu::GetWindow() const | |
478 | { | |
479 | if ( m_invokingWindow != NULL ) | |
480 | return m_invokingWindow; | |
481 | else if ( m_menuBar != NULL) | |
482 | return m_menuBar->GetFrame(); | |
483 | ||
484 | return NULL; | |
485 | } | |
486 | ||
487 | // --------------------------------------------------------------------------- | |
488 | // Menu Bar | |
489 | // --------------------------------------------------------------------------- | |
490 | ||
491 | void wxMenuBar::Init() | |
492 | { | |
493 | m_eventHandler = this; | |
494 | m_menuBarFrame = NULL; | |
495 | m_hMenu = 0; | |
496 | } | |
497 | ||
498 | wxMenuBar::wxMenuBar() | |
499 | { | |
500 | Init(); | |
501 | } | |
502 | ||
503 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
504 | { | |
505 | Init(); | |
506 | } | |
507 | ||
508 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
509 | { | |
510 | Init(); | |
511 | ||
512 | m_titles.Alloc(count); | |
513 | ||
514 | for ( int i = 0; i < count; i++ ) | |
515 | { | |
516 | m_menus.Append(menus[i]); | |
517 | m_titles.Add(titles[i]); | |
518 | ||
519 | menus[i]->Attach(this); | |
520 | } | |
521 | } | |
522 | ||
523 | wxMenuBar::~wxMenuBar() | |
524 | { | |
525 | } | |
526 | ||
527 | // --------------------------------------------------------------------------- | |
528 | // wxMenuBar helpers | |
529 | // --------------------------------------------------------------------------- | |
530 | ||
531 | void wxMenuBar::Refresh( | |
532 | bool WXUNUSED(bEraseBackground) | |
533 | , const wxRect* WXUNUSED(pRect) | |
534 | ) | |
535 | { | |
536 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
537 | ||
538 | // DrawMenuBar(GetHwndOf(m_menuBarFrame)); | |
539 | } | |
540 | ||
541 | WXHMENU wxMenuBar::Create() | |
542 | { | |
543 | if (m_hMenu != 0 ) | |
544 | return m_hMenu; | |
545 | ||
546 | wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") ); | |
547 | ||
548 | // TODO: | |
549 | /* | |
550 | ||
551 | m_hMenu = (WXHMENU)::CreateMenu(); | |
552 | ||
553 | if ( !m_hMenu ) | |
554 | { | |
555 | wxLogLastError("CreateMenu"); | |
556 | } | |
557 | else | |
558 | { | |
559 | size_t count = GetMenuCount(); | |
560 | for ( size_t i = 0; i < count; i++ ) | |
561 | { | |
562 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
563 | (UINT)m_menus[i]->GetHMenu(), | |
564 | m_titles[i]) ) | |
565 | { | |
566 | wxLogLastError("AppendMenu"); | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | return m_hMenu; | |
572 | */ | |
573 | return (WXHMENU)0; | |
574 | } | |
575 | ||
576 | // --------------------------------------------------------------------------- | |
577 | // wxMenuBar functions to work with the top level submenus | |
578 | // --------------------------------------------------------------------------- | |
579 | ||
580 | // NB: we don't support owner drawn top level items for now, if we do these | |
581 | // functions would have to be changed to use wxMenuItem as well | |
582 | ||
583 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
584 | { | |
585 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); | |
586 | ||
587 | // int flag = enable ? MF_ENABLED : MF_GRAYED; | |
588 | ||
589 | // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); | |
590 | ||
591 | Refresh(); | |
592 | } | |
593 | ||
594 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) | |
595 | { | |
596 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
597 | ||
598 | m_titles[pos] = label; | |
599 | ||
600 | if ( !IsAttached() ) | |
601 | { | |
602 | return; | |
603 | } | |
604 | //else: have to modify the existing menu | |
605 | ||
606 | // TODO: | |
607 | /* | |
608 | UINT id; | |
609 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); | |
610 | if ( flagsOld == 0xFFFFFFFF ) | |
611 | { | |
612 | wxLogLastError(wxT("GetMenuState")); | |
613 | ||
614 | return; | |
615 | } | |
616 | ||
617 | if ( flagsOld & MF_POPUP ) | |
618 | { | |
619 | // HIBYTE contains the number of items in the submenu in this case | |
620 | flagsOld &= 0xff; | |
621 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos); | |
622 | } | |
623 | else | |
624 | { | |
625 | id = pos; | |
626 | } | |
627 | ||
628 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, | |
629 | id, label) == (int)0xFFFFFFFF ) | |
630 | { | |
631 | wxLogLastError("ModifyMenu"); | |
632 | } | |
633 | */ | |
634 | Refresh(); | |
635 | } | |
636 | ||
637 | wxString wxMenuBar::GetLabelTop(size_t pos) const | |
638 | { | |
639 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, | |
640 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
641 | ||
642 | return m_titles[pos]; | |
643 | } | |
644 | ||
645 | int wxMenuBar::FindMenu(const wxString& title) | |
646 | { | |
647 | wxString menuTitle = wxStripMenuCodes(title); | |
648 | ||
649 | size_t count = GetMenuCount(); | |
650 | for ( size_t i = 0; i < count; i++ ) | |
651 | { | |
652 | wxString title = wxStripMenuCodes(m_titles[i]); | |
653 | if ( menuTitle == title ) | |
654 | return i; | |
655 | } | |
656 | ||
657 | return wxNOT_FOUND; | |
658 | ||
659 | } | |
660 | ||
661 | // --------------------------------------------------------------------------- | |
662 | // wxMenuBar construction | |
663 | // --------------------------------------------------------------------------- | |
664 | ||
665 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
666 | { | |
667 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); | |
668 | if ( !menuOld ) | |
669 | return FALSE; | |
670 | m_titles[pos] = title; | |
671 | // TODO: | |
672 | /* | |
673 | if ( IsAttached() ) | |
674 | { | |
675 | // can't use ModifyMenu() because it deletes the submenu it replaces | |
676 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
677 | { | |
678 | wxLogLastError("RemoveMenu"); | |
679 | } | |
680 | ||
681 | if ( !::InsertMenu(GetHmenu(), (UINT)pos, | |
682 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
683 | (UINT)GetHmenuOf(menu), title) ) | |
684 | { | |
685 | wxLogLastError("InsertMenu"); | |
686 | } | |
687 | ||
688 | #if wxUSE_ACCEL | |
689 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
690 | { | |
691 | // need to rebuild accell table | |
692 | RebuildAccelTable(); | |
693 | } | |
694 | #endif // wxUSE_ACCEL | |
695 | ||
696 | Refresh(); | |
697 | } | |
698 | */ | |
699 | return menuOld; | |
700 | } | |
701 | ||
702 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
703 | { | |
704 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
705 | return FALSE; | |
706 | ||
707 | m_titles.Insert(title, pos); | |
708 | ||
709 | menu->Attach(this); | |
710 | // TODO: | |
711 | /* | |
712 | if ( IsAttached() ) | |
713 | { | |
714 | if ( !::InsertMenu(GetHmenu(), pos, | |
715 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
716 | (UINT)GetHmenuOf(menu), title) ) | |
717 | { | |
718 | wxLogLastError("InsertMenu"); | |
719 | } | |
720 | ||
721 | #if wxUSE_ACCEL | |
722 | if ( menu->HasAccels() ) | |
723 | { | |
724 | // need to rebuild accell table | |
725 | RebuildAccelTable(); | |
726 | } | |
727 | #endif // wxUSE_ACCEL | |
728 | ||
729 | Refresh(); | |
730 | } | |
731 | */ | |
732 | return TRUE; | |
733 | } | |
734 | ||
735 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
736 | { | |
737 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
738 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
739 | ||
740 | if ( !wxMenuBarBase::Append(menu, title) ) | |
741 | return FALSE; | |
742 | ||
743 | menu->Attach(this); | |
744 | ||
745 | m_titles.Add(title); | |
746 | // TODO: | |
747 | /* | |
748 | if ( IsAttached() ) | |
749 | { | |
750 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, | |
751 | (UINT)submenu, title) ) | |
752 | { | |
753 | wxLogLastError(wxT("AppendMenu")); | |
754 | } | |
755 | ||
756 | #if wxUSE_ACCEL | |
757 | if ( menu->HasAccels() ) | |
758 | { | |
759 | // need to rebuild accell table | |
760 | RebuildAccelTable(); | |
761 | } | |
762 | #endif // wxUSE_ACCEL | |
763 | ||
764 | Refresh(); | |
765 | } | |
766 | */ | |
767 | return TRUE; | |
768 | } | |
769 | ||
770 | wxMenu *wxMenuBar::Remove(size_t pos) | |
771 | { | |
772 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
773 | if ( !menu ) | |
774 | return NULL; | |
775 | // TODO: | |
776 | /* | |
777 | if ( IsAttached() ) | |
778 | { | |
779 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
780 | { | |
781 | wxLogLastError("RemoveMenu"); | |
782 | } | |
783 | ||
784 | menu->Detach(); | |
785 | ||
786 | #if wxUSE_ACCEL | |
787 | if ( menu->HasAccels() ) | |
788 | { | |
789 | // need to rebuild accell table | |
790 | RebuildAccelTable(); | |
791 | } | |
792 | #endif // wxUSE_ACCEL | |
793 | ||
794 | Refresh(); | |
795 | } | |
796 | ||
797 | m_titles.Remove(pos); | |
798 | */ | |
799 | return menu; | |
800 | } | |
801 | ||
802 | #if wxUSE_ACCEL | |
803 | ||
804 | void wxMenuBar::RebuildAccelTable() | |
805 | { | |
806 | // merge the accelerators of all menus into one accel table | |
807 | size_t nAccelCount = 0; | |
808 | size_t i, count = GetMenuCount(); | |
809 | for ( i = 0; i < count; i++ ) | |
810 | { | |
811 | nAccelCount += m_menus[i]->GetAccelCount(); | |
812 | } | |
813 | ||
814 | if ( nAccelCount ) | |
815 | { | |
816 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; | |
817 | ||
818 | nAccelCount = 0; | |
819 | for ( i = 0; i < count; i++ ) | |
820 | { | |
821 | nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); | |
822 | } | |
823 | ||
824 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
825 | ||
826 | delete [] accelEntries; | |
827 | } | |
828 | } | |
829 | ||
830 | #endif // wxUSE_ACCEL | |
831 | ||
832 | void wxMenuBar::Attach(wxFrame *frame) | |
833 | { | |
834 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
835 | ||
836 | m_menuBarFrame = frame; | |
837 | ||
838 | #if wxUSE_ACCEL | |
839 | RebuildAccelTable(); | |
840 | #endif // wxUSE_ACCEL | |
841 | } | |
842 | ||
843 | void wxMenuBar::Detach() | |
844 | { | |
845 | // ::DestroyMenu((HMENU)m_hMenu); | |
846 | m_hMenu = (WXHMENU)NULL; | |
847 | m_menuBarFrame = NULL; | |
848 | } | |
849 | ||
850 | ||
851 | // --------------------------------------------------------------------------- | |
852 | // wxMenuBar searching for menu items | |
853 | // --------------------------------------------------------------------------- | |
854 | ||
855 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
856 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
857 | const wxString& itemString) const | |
858 | { | |
859 | wxString menuLabel = wxStripMenuCodes(menuString); | |
860 | size_t count = GetMenuCount(); | |
861 | for ( size_t i = 0; i < count; i++ ) | |
862 | { | |
863 | wxString title = wxStripMenuCodes(m_titles[i]); | |
864 | if ( menuString == title ) | |
865 | return m_menus[i]->FindItem(itemString); | |
866 | } | |
867 | ||
868 | return wxNOT_FOUND; | |
869 | } | |
870 | ||
871 | wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const | |
872 | { | |
873 | if ( itemMenu ) | |
874 | *itemMenu = NULL; | |
875 | ||
876 | wxMenuItem *item = NULL; | |
877 | size_t count = GetMenuCount(); | |
878 | for ( size_t i = 0; !item && (i < count); i++ ) | |
879 | { | |
880 | item = m_menus[i]->FindItem(id, itemMenu); | |
881 | } | |
882 | ||
883 | return item; | |
884 | } | |
885 |