]>
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/app.h" | |
21 | #include "wx/frame.h" | |
22 | #include "wx/menu.h" | |
23 | #include "wx/utils.h" | |
24 | #include "wx/intl.h" | |
25 | #include "wx/log.h" | |
26 | #endif | |
27 | ||
28 | #if wxUSE_OWNER_DRAWN | |
29 | #include "wx/ownerdrw.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/os2/private.h" | |
33 | ||
34 | // other standard headers | |
35 | #include <string.h> | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // global variables | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | extern wxMenu* wxCurrentPopupMenu; | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // | |
48 | // The (popup) menu title has this special id | |
49 | // | |
50 | static const int idMenuTitle = -2; | |
51 | ||
52 | // | |
53 | // The unique ID for Menus | |
54 | // | |
55 | USHORT wxMenu::m_nextMenuId = 0; | |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // macros | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
62 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
63 | ||
64 | // ============================================================================ | |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
68 | // --------------------------------------------------------------------------- | |
69 | // wxMenu construction, adding and removing menu items | |
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | // | |
73 | // Construct a menu with optional title (then use append) | |
74 | // | |
75 | void wxMenu::Init() | |
76 | { | |
77 | m_bDoBreak = FALSE; | |
78 | m_nStartRadioGroup = -1; | |
79 | ||
80 | // | |
81 | // Create the menu (to be used as a submenu or a popup) | |
82 | // | |
83 | if ((m_hMenu = ::WinCreateWindow( HWND_DESKTOP | |
84 | ,WC_MENU | |
85 | ,"Menu" | |
86 | ,0L | |
87 | ,0L | |
88 | ,0L | |
89 | ,0L | |
90 | ,0L | |
91 | ,NULLHANDLE | |
92 | ,HWND_TOP | |
93 | ,0L | |
94 | ,NULL | |
95 | ,NULL | |
96 | )) == 0) | |
97 | { | |
98 | wxLogLastError("WinLoadMenu"); | |
99 | } | |
100 | m_vMenuData.iPosition = 0; | |
101 | m_vMenuData.afStyle = MIS_SUBMENU | MIS_TEXT; | |
102 | m_vMenuData.afAttribute = (USHORT)0; | |
103 | m_vMenuData.id = m_nextMenuId++; | |
104 | m_vMenuData.hwndSubMenu = m_hMenu; | |
105 | m_vMenuData.hItem = NULLHANDLE; | |
106 | ||
107 | // | |
108 | // If we have a title, insert it in the beginning of the menu | |
109 | // | |
110 | if (!m_title.IsEmpty()) | |
111 | { | |
112 | Append( idMenuTitle | |
113 | ,m_title | |
114 | ,wxEmptyString | |
115 | ,wxITEM_NORMAL | |
116 | ); | |
117 | AppendSeparator(); | |
118 | } | |
119 | } // end of wxMenu::Init | |
120 | ||
121 | // | |
122 | // The wxWindow destructor will take care of deleting the submenus. | |
123 | // | |
124 | wxMenu::~wxMenu() | |
125 | { | |
126 | // | |
127 | // We should free PM resources only if PM doesn't do it for us | |
128 | // which happens if we're attached to a menubar or a submenu of another | |
129 | // menu | |
130 | if (!IsAttached() && !GetParent()) | |
131 | { | |
132 | if (!::WinDestroyWindow((HWND)GetHmenu()) ) | |
133 | { | |
134 | wxLogLastError("WinDestroyWindow"); | |
135 | } | |
136 | } | |
137 | ||
138 | #if wxUSE_ACCEL | |
139 | // | |
140 | // Delete accels | |
141 | // | |
142 | WX_CLEAR_ARRAY(m_vAccels); | |
143 | #endif // wxUSE_ACCEL | |
144 | } // end of wxMenu::~wxMenu | |
145 | ||
146 | void wxMenu::Break() | |
147 | { | |
148 | // this will take effect during the next call to Append() | |
149 | m_bDoBreak = TRUE; | |
150 | } // end of wxMenu::Break | |
151 | ||
152 | void wxMenu::Attach( | |
153 | wxMenuBarBase* pMenubar | |
154 | ) | |
155 | { | |
156 | wxMenuBase::Attach(pMenubar); | |
157 | EndRadioGroup(); | |
158 | } // end of wxMenu::Break; | |
159 | ||
160 | #if wxUSE_ACCEL | |
161 | ||
162 | int wxMenu::FindAccel( | |
163 | int nId | |
164 | ) const | |
165 | { | |
166 | size_t n; | |
167 | size_t nCount = m_vAccels.GetCount(); | |
168 | ||
169 | for (n = 0; n < nCount; n++) | |
170 | if (m_vAccels[n]->m_command == nId) | |
171 | return n; | |
172 | return wxNOT_FOUND; | |
173 | } // end of wxMenu::FindAccel | |
174 | ||
175 | void wxMenu::UpdateAccel( | |
176 | wxMenuItem* pItem | |
177 | ) | |
178 | { | |
179 | if (pItem->IsSubMenu()) | |
180 | { | |
181 | wxMenu* pSubmenu = pItem->GetSubMenu(); | |
182 | wxMenuItemList::Node* pNode = pSubmenu->GetMenuItems().GetFirst(); | |
183 | ||
184 | while (pNode) | |
185 | { | |
186 | UpdateAccel(pNode->GetData()); | |
187 | pNode = pNode->GetNext(); | |
188 | } | |
189 | } | |
190 | else if (!pItem->IsSeparator()) | |
191 | { | |
192 | // | |
193 | // Find the (new) accel for this item | |
194 | // | |
195 | wxAcceleratorEntry* pAccel = wxGetAccelFromString(pItem->GetText()); | |
196 | ||
197 | if (pAccel) | |
198 | pAccel->m_command = pItem->GetId(); | |
199 | ||
200 | // | |
201 | // Find the old one | |
202 | // | |
203 | size_t n = FindAccel(pItem->GetId()); | |
204 | ||
205 | if (n == wxNOT_FOUND) | |
206 | { | |
207 | // | |
208 | // No old, add new if any | |
209 | // | |
210 | if (pAccel) | |
211 | m_vAccels.Add(pAccel); | |
212 | else | |
213 | return; | |
214 | } | |
215 | else | |
216 | { | |
217 | // | |
218 | // Replace old with new or just remove the old one if no new | |
219 | // | |
220 | delete m_vAccels[n]; | |
221 | if (pAccel) | |
222 | m_vAccels[n] = pAccel; | |
223 | else | |
224 | m_vAccels.RemoveAt(n); | |
225 | } | |
226 | ||
227 | if (IsAttached()) | |
228 | { | |
229 | m_menuBar->RebuildAccelTable(); | |
230 | } | |
231 | } | |
232 | } // wxMenu::UpdateAccel | |
233 | ||
234 | #endif // wxUSE_ACCEL | |
235 | ||
236 | // | |
237 | // Append a new item or submenu to the menu | |
238 | // | |
239 | bool wxMenu::DoInsertOrAppend( | |
240 | wxMenuItem* pItem | |
241 | , size_t nPos | |
242 | ) | |
243 | { | |
244 | wxMenu* pSubmenu = pItem->GetSubMenu(); | |
245 | MENUITEM& rItem = (pSubmenu != NULL)?pSubmenu->m_vMenuData: | |
246 | pItem->m_vMenuData; | |
247 | ||
248 | ERRORID vError; | |
249 | wxString sError; | |
250 | char zMsg[128]; | |
251 | ||
252 | #if wxUSE_ACCEL | |
253 | UpdateAccel(pItem); | |
254 | #endif // wxUSE_ACCEL | |
255 | ||
256 | // | |
257 | // If "Break" has just been called, insert a menu break before this item | |
258 | // (and don't forget to reset the flag) | |
259 | // | |
260 | if (m_bDoBreak) | |
261 | { | |
262 | rItem.afStyle |= MIS_BREAK; | |
263 | m_bDoBreak = FALSE; | |
264 | } | |
265 | ||
266 | // | |
267 | // Id is the numeric id for normal menu items and HMENU for submenus as | |
268 | // required by ::MM_INSERTITEM message API | |
269 | // | |
270 | if (pSubmenu != NULL) | |
271 | { | |
272 | wxASSERT_MSG(pSubmenu->GetHMenu(), wxT("invalid submenu")); | |
273 | pSubmenu->SetParent(this); | |
274 | ||
275 | rItem.iPosition = 0; // submenus have a 0 position | |
276 | rItem.id = (USHORT)pSubmenu->GetHMenu(); | |
277 | rItem.afStyle |= MIS_SUBMENU | MIS_TEXT; | |
278 | } | |
279 | else | |
280 | { | |
281 | rItem.id = pItem->GetId(); | |
282 | } | |
283 | ||
284 | BYTE* pData; | |
285 | ||
286 | #if wxUSE_OWNER_DRAWN | |
287 | if (pItem->IsOwnerDrawn()) | |
288 | { | |
289 | // | |
290 | // Want to get {Measure|Draw}Item messages? | |
291 | // item draws itself, passing pointer to data doesn't work in OS/2 | |
292 | // Will eventually need to set the image handle somewhere into vItem.hItem | |
293 | // | |
294 | rItem.afStyle |= MIS_OWNERDRAW; | |
295 | pData = (BYTE*)NULL; | |
296 | rItem.hItem = (HBITMAP)pItem->GetBitmap().GetHBITMAP(); | |
297 | pItem->m_vMenuData.afStyle = rItem.afStyle; | |
298 | pItem->m_vMenuData.hItem = rItem.hItem; | |
299 | } | |
300 | else | |
301 | #endif | |
302 | if (pItem->IsSeparator()) | |
303 | { | |
304 | rItem.afStyle = MIS_SEPARATOR; | |
305 | } | |
306 | else | |
307 | { | |
308 | // | |
309 | // Menu is just a normal string (passed in data parameter) | |
310 | // | |
311 | rItem.afStyle |= MIS_TEXT; | |
312 | pData = (char*)pItem->GetText().c_str(); | |
313 | } | |
314 | ||
315 | if (nPos == (size_t)-1) | |
316 | { | |
317 | rItem.iPosition = MIT_END; | |
318 | } | |
319 | else | |
320 | { | |
321 | rItem.iPosition = nPos; | |
322 | } | |
323 | ||
324 | APIRET rc; | |
325 | ||
326 | rc = (APIRET)::WinSendMsg( GetHmenu() | |
327 | ,MM_INSERTITEM | |
328 | ,(MPARAM)&rItem | |
329 | ,(MPARAM)pData | |
330 | ); | |
331 | #if wxUSE_OWNER_DRAWN | |
332 | if (pItem->IsOwnerDrawn()) | |
333 | { | |
334 | BOOL rc; | |
335 | MENUITEM vMenuItem; | |
336 | ||
337 | ::WinSendMsg( GetHmenu() | |
338 | ,MM_QUERYITEM | |
339 | ,MPFROM2SHORT( (USHORT)pItem->GetId() | |
340 | ,(USHORT)(FALSE) | |
341 | ) | |
342 | ,&vMenuItem | |
343 | ); | |
344 | } | |
345 | #endif | |
346 | if (rc == MIT_MEMERROR || rc == MIT_ERROR) | |
347 | { | |
348 | vError = ::WinGetLastError(vHabmain); | |
349 | sError = wxPMErrorToStr(vError); | |
350 | wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str()); | |
351 | wxLogLastError("Insert or AppendMenu"); | |
352 | return FALSE; | |
353 | } | |
354 | else | |
355 | { | |
356 | // | |
357 | // If we're already attached to the menubar, we must update it | |
358 | // | |
359 | if (IsAttached() && m_menuBar->IsAttached()) | |
360 | { | |
361 | m_menuBar->Refresh(); | |
362 | } | |
363 | return TRUE; | |
364 | } | |
365 | return FALSE; | |
366 | } // end of wxMenu::DoInsertOrAppend | |
367 | ||
368 | void wxMenu::EndRadioGroup() | |
369 | { | |
370 | // | |
371 | // We're not inside a radio group any longer | |
372 | // | |
373 | m_nStartRadioGroup = -1; | |
374 | } // end of wxMenu::EndRadioGroup | |
375 | ||
376 | wxMenuItem* wxMenu::DoAppend( | |
377 | wxMenuItem* pItem | |
378 | ) | |
379 | { | |
380 | wxCHECK_MSG( pItem, NULL, _T("NULL item in wxMenu::DoAppend") ); | |
381 | ||
382 | bool bCheck = FALSE; | |
383 | ||
384 | if (pItem->GetKind() == wxITEM_RADIO) | |
385 | { | |
386 | int nCount = GetMenuItemCount(); | |
387 | ||
388 | if (m_nStartRadioGroup == -1) | |
389 | { | |
390 | // | |
391 | // Start a new radio group | |
392 | // | |
393 | m_nStartRadioGroup = nCount; | |
394 | ||
395 | // | |
396 | // For now it has just one element | |
397 | // | |
398 | pItem->SetAsRadioGroupStart(); | |
399 | pItem->SetRadioGroupEnd(m_nStartRadioGroup); | |
400 | ||
401 | // | |
402 | // Ensure that we have a checked item in the radio group | |
403 | // | |
404 | bCheck = TRUE; | |
405 | } | |
406 | else // extend the current radio group | |
407 | { | |
408 | // | |
409 | // We need to update its end item | |
410 | // | |
411 | pItem->SetRadioGroupStart(m_nStartRadioGroup); | |
412 | ||
413 | wxMenuItemList::Node* pNode = GetMenuItems().Item(m_nStartRadioGroup); | |
414 | ||
415 | if (pNode) | |
416 | { | |
417 | pNode->GetData()->SetRadioGroupEnd(nCount); | |
418 | } | |
419 | else | |
420 | { | |
421 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
422 | } | |
423 | } | |
424 | } | |
425 | else // not a radio item | |
426 | { | |
427 | EndRadioGroup(); | |
428 | } | |
429 | ||
430 | if (!wxMenuBase::DoAppend(pItem) || !DoInsertOrAppend(pItem)) | |
431 | { | |
432 | return NULL; | |
433 | } | |
434 | if (bCheck) | |
435 | { | |
436 | // | |
437 | // Check the item initially | |
438 | // | |
439 | pItem->Check(TRUE); | |
440 | } | |
441 | return pItem; | |
442 | } // end of wxMenu::DoAppend | |
443 | ||
444 | wxMenuItem* wxMenu::DoInsert( | |
445 | size_t nPos | |
446 | , wxMenuItem* pItem | |
447 | ) | |
448 | { | |
449 | if ( wxMenuBase::DoInsert( nPos | |
450 | ,pItem) && | |
451 | DoInsertOrAppend( pItem | |
452 | ,nPos | |
453 | )) | |
454 | return pItem; | |
455 | else | |
456 | return NULL; | |
457 | } // end of wxMenu::DoInsert | |
458 | ||
459 | wxMenuItem* wxMenu::DoRemove( | |
460 | wxMenuItem* pItem | |
461 | ) | |
462 | { | |
463 | // | |
464 | // We need to find the items position in the child list | |
465 | // | |
466 | size_t nPos; | |
467 | wxMenuItemList::Node* pNode = GetMenuItems().GetFirst(); | |
468 | ||
469 | for (nPos = 0; pNode; nPos++) | |
470 | { | |
471 | if (pNode->GetData() == pItem) | |
472 | break; | |
473 | pNode = pNode->GetNext(); | |
474 | } | |
475 | ||
476 | // | |
477 | // DoRemove() (unlike Remove) can only be called for existing item! | |
478 | // | |
479 | wxCHECK_MSG(pNode, NULL, wxT("bug in wxMenu::Remove logic")); | |
480 | ||
481 | #if wxUSE_ACCEL | |
482 | // | |
483 | // Remove the corresponding accel from the accel table | |
484 | // | |
485 | int n = FindAccel(pItem->GetId()); | |
486 | ||
487 | if (n != wxNOT_FOUND) | |
488 | { | |
489 | delete m_vAccels[n]; | |
490 | m_vAccels.RemoveAt(n); | |
491 | } | |
492 | ||
493 | #endif // wxUSE_ACCEL | |
494 | // | |
495 | // Remove the item from the menu | |
496 | // | |
497 | ::WinSendMsg( GetHmenu() | |
498 | ,MM_REMOVEITEM | |
499 | ,MPFROM2SHORT(pItem->GetId(), TRUE) | |
500 | ,(MPARAM)0 | |
501 | ); | |
502 | if (IsAttached() && m_menuBar->IsAttached()) | |
503 | { | |
504 | // | |
505 | // Otherwise, the chane won't be visible | |
506 | // | |
507 | m_menuBar->Refresh(); | |
508 | } | |
509 | ||
510 | // | |
511 | // And from internal data structures | |
512 | // | |
513 | return wxMenuBase::DoRemove(pItem); | |
514 | } // end of wxMenu::DoRemove | |
515 | ||
516 | // --------------------------------------------------------------------------- | |
517 | // accelerator helpers | |
518 | // --------------------------------------------------------------------------- | |
519 | ||
520 | #if wxUSE_ACCEL | |
521 | ||
522 | // | |
523 | // Create the wxAcceleratorEntries for our accels and put them into provided | |
524 | // array - return the number of accels we have | |
525 | // | |
526 | size_t wxMenu::CopyAccels( | |
527 | wxAcceleratorEntry* pAccels | |
528 | ) const | |
529 | { | |
530 | size_t nCount = GetAccelCount(); | |
531 | ||
532 | for (size_t n = 0; n < nCount; n++) | |
533 | { | |
534 | *pAccels++ = *m_vAccels[n]; | |
535 | } | |
536 | return nCount; | |
537 | } // end of wxMenu::CopyAccels | |
538 | ||
539 | #endif // wxUSE_ACCEL | |
540 | ||
541 | // --------------------------------------------------------------------------- | |
542 | // set wxMenu title | |
543 | // --------------------------------------------------------------------------- | |
544 | ||
545 | void wxMenu::SetTitle( | |
546 | const wxString& rLabel | |
547 | ) | |
548 | { | |
549 | bool bHasNoTitle = m_title.IsEmpty(); | |
550 | HWND hMenu = GetHmenu(); | |
551 | ||
552 | m_title = rLabel; | |
553 | if (bHasNoTitle) | |
554 | { | |
555 | if (!rLabel.IsEmpty()) | |
556 | { | |
557 | if (!::WinSetWindowText(hMenu, rLabel.c_str())) | |
558 | { | |
559 | wxLogLastError("SetMenuTitle"); | |
560 | } | |
561 | } | |
562 | } | |
563 | else | |
564 | { | |
565 | if (rLabel.IsEmpty() ) | |
566 | { | |
567 | ::WinSendMsg( GetHmenu() | |
568 | ,MM_REMOVEITEM | |
569 | ,MPFROM2SHORT(hMenu, TRUE) | |
570 | ,(MPARAM)0 | |
571 | ); | |
572 | } | |
573 | else | |
574 | { | |
575 | // | |
576 | // Modify the title | |
577 | // | |
578 | if (!::WinSetWindowText(hMenu, rLabel.c_str())) | |
579 | { | |
580 | wxLogLastError("SetMenuTitle"); | |
581 | } | |
582 | } | |
583 | } | |
584 | } // end of wxMenu::SetTitle | |
585 | ||
586 | // --------------------------------------------------------------------------- | |
587 | // event processing | |
588 | // --------------------------------------------------------------------------- | |
589 | ||
590 | bool wxMenu::OS2Command( | |
591 | WXUINT WXUNUSED(uParam) | |
592 | , WXWORD vId | |
593 | ) | |
594 | { | |
595 | // | |
596 | // Ignore commands from the menu title | |
597 | // | |
598 | ||
599 | if (vId != (WXWORD)idMenuTitle) | |
600 | { | |
601 | SendEvent( vId | |
602 | ,(int)::WinSendMsg( GetHmenu() | |
603 | ,MM_QUERYITEMATTR | |
604 | ,(MPARAM)vId | |
605 | ,(MPARAM)MIA_CHECKED | |
606 | ) | |
607 | ); | |
608 | } | |
609 | return TRUE; | |
610 | } // end of wxMenu::OS2Command | |
611 | ||
612 | // --------------------------------------------------------------------------- | |
613 | // other | |
614 | // --------------------------------------------------------------------------- | |
615 | ||
616 | wxWindow* wxMenu::GetWindow() const | |
617 | { | |
618 | if (m_invokingWindow != NULL) | |
619 | return m_invokingWindow; | |
620 | else if ( m_menuBar != NULL) | |
621 | return m_menuBar->GetFrame(); | |
622 | ||
623 | return NULL; | |
624 | } // end of wxMenu::GetWindow | |
625 | ||
626 | // recursive search for item by id | |
627 | wxMenuItem* wxMenu::FindItem( | |
628 | int nItemId | |
629 | , ULONG hItem | |
630 | , wxMenu** ppItemMenu | |
631 | ) const | |
632 | { | |
633 | if ( ppItemMenu ) | |
634 | *ppItemMenu = NULL; | |
635 | ||
636 | wxMenuItem* pItem = NULL; | |
637 | ||
638 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
639 | node && !pItem; | |
640 | node = node->GetNext() ) | |
641 | { | |
642 | pItem = node->GetData(); | |
643 | ||
644 | if ( pItem->GetId() == nItemId && pItem->m_vMenuData.hItem == hItem) | |
645 | { | |
646 | if ( ppItemMenu ) | |
647 | *ppItemMenu = (wxMenu *)this; | |
648 | } | |
649 | else if ( pItem->IsSubMenu() ) | |
650 | { | |
651 | pItem = pItem->GetSubMenu()->FindItem( nItemId | |
652 | ,hItem | |
653 | ,ppItemMenu | |
654 | ); | |
655 | if (pItem) | |
656 | break; | |
657 | } | |
658 | else | |
659 | { | |
660 | // don't exit the loop | |
661 | pItem = NULL; | |
662 | } | |
663 | } | |
664 | return pItem; | |
665 | } // end of wxMenu::FindItem | |
666 | ||
667 | // --------------------------------------------------------------------------- | |
668 | // Menu Bar | |
669 | // --------------------------------------------------------------------------- | |
670 | ||
671 | void wxMenuBar::Init() | |
672 | { | |
673 | m_eventHandler = this; | |
674 | m_menuBarFrame = NULL; | |
675 | m_hMenu = 0; | |
676 | } // end of wxMenuBar::Init | |
677 | ||
678 | wxMenuBar::wxMenuBar() | |
679 | { | |
680 | Init(); | |
681 | } // end of wxMenuBar::wxMenuBar | |
682 | ||
683 | wxMenuBar::wxMenuBar( | |
684 | long WXUNUSED(lStyle) | |
685 | ) | |
686 | { | |
687 | Init(); | |
688 | } // end of wxMenuBar::wxMenuBar | |
689 | ||
690 | wxMenuBar::wxMenuBar( | |
691 | int nCount | |
692 | , wxMenu* vMenus[] | |
693 | , const wxString sTitles[] | |
694 | ) | |
695 | { | |
696 | Init(); | |
697 | ||
698 | m_titles.Alloc(nCount); | |
699 | for ( int i = 0; i < nCount; i++ ) | |
700 | { | |
701 | m_menus.Append(vMenus[i]); | |
702 | m_titles.Add(sTitles[i]); | |
703 | vMenus[i]->Attach(this); | |
704 | } | |
705 | } // end of wxMenuBar::wxMenuBar | |
706 | ||
707 | wxMenuBar::~wxMenuBar() | |
708 | { | |
709 | // | |
710 | // We should free PM's resources only if PM doesn't do it for us | |
711 | // which happens if we're attached to a frame | |
712 | // | |
713 | if (m_hMenu && !IsAttached()) | |
714 | { | |
715 | ::WinDestroyWindow((HMENU)m_hMenu); | |
716 | m_hMenu = (WXHMENU)NULL; | |
717 | } | |
718 | } // end of wxMenuBar::~wxMenuBar | |
719 | ||
720 | // --------------------------------------------------------------------------- | |
721 | // wxMenuBar helpers | |
722 | // --------------------------------------------------------------------------- | |
723 | ||
724 | void wxMenuBar::Refresh() | |
725 | { | |
726 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
727 | ||
728 | WinSendMsg(GetWinHwnd(m_menuBarFrame), WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0); | |
729 | } // end of wxMenuBar::Refresh | |
730 | ||
731 | WXHMENU wxMenuBar::Create() | |
732 | { | |
733 | MENUITEM vItem; | |
734 | HWND hFrame; | |
735 | ||
736 | if (m_hMenu != 0 ) | |
737 | return m_hMenu; | |
738 | ||
739 | wxCHECK_MSG(!m_hMenu, TRUE, wxT("menubar already created")); | |
740 | ||
741 | // | |
742 | // Menubars should be associated with a frame otherwise they are popups | |
743 | // | |
744 | if (m_menuBarFrame != NULL) | |
745 | hFrame = GetWinHwnd(m_menuBarFrame); | |
746 | else | |
747 | hFrame = HWND_DESKTOP; | |
748 | // | |
749 | // Create an empty menu and then fill it with insertions | |
750 | // | |
751 | if ((m_hMenu = ::WinCreateWindow( hFrame | |
752 | ,WC_MENU | |
753 | ,(PSZ)NULL | |
754 | ,MS_ACTIONBAR | WS_SYNCPAINT | WS_VISIBLE | |
755 | ,0L | |
756 | ,0L | |
757 | ,0L | |
758 | ,0L | |
759 | ,hFrame | |
760 | ,HWND_TOP | |
761 | ,FID_MENU | |
762 | ,NULL | |
763 | ,NULL | |
764 | )) == 0) | |
765 | { | |
766 | wxLogLastError("WinLoadMenu"); | |
767 | } | |
768 | else | |
769 | { | |
770 | size_t nCount = GetMenuCount(); | |
771 | ||
772 | for (size_t i = 0; i < nCount; i++) | |
773 | { | |
774 | APIRET rc; | |
775 | ERRORID vError; | |
776 | wxString sError; | |
777 | HWND hSubMenu; | |
778 | ||
779 | // | |
780 | // Set the parent and owner of the submenues to be the menubar, not the desktop | |
781 | // | |
782 | hSubMenu = m_menus[i]->m_vMenuData.hwndSubMenu; | |
783 | if (!::WinSetParent(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu, FALSE)) | |
784 | { | |
785 | vError = ::WinGetLastError(vHabmain); | |
786 | sError = wxPMErrorToStr(vError); | |
787 | wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str()); | |
788 | return NULLHANDLE; | |
789 | } | |
790 | ||
791 | if (!::WinSetOwner(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu)) | |
792 | { | |
793 | vError = ::WinGetLastError(vHabmain); | |
794 | sError = wxPMErrorToStr(vError); | |
795 | wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str()); | |
796 | return NULLHANDLE; | |
797 | } | |
798 | ||
799 | m_menus[i]->m_vMenuData.iPosition = i; | |
800 | ||
801 | rc = (APIRET)::WinSendMsg(m_hMenu, MM_INSERTITEM, (MPARAM)&m_menus[i]->m_vMenuData, (MPARAM)m_titles[i].c_str()); | |
802 | if (rc == MIT_MEMERROR || rc == MIT_ERROR) | |
803 | { | |
804 | vError = ::WinGetLastError(vHabmain); | |
805 | sError = wxPMErrorToStr(vError); | |
806 | wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str()); | |
807 | return NULLHANDLE; | |
808 | } | |
809 | } | |
810 | } | |
811 | return m_hMenu; | |
812 | } // end of wxMenuBar::Create | |
813 | ||
814 | // --------------------------------------------------------------------------- | |
815 | // wxMenuBar functions to work with the top level submenus | |
816 | // --------------------------------------------------------------------------- | |
817 | ||
818 | // | |
819 | // NB: we don't support owner drawn top level items for now, if we do these | |
820 | // functions would have to be changed to use wxMenuItem as well | |
821 | // | |
822 | void wxMenuBar::EnableTop( | |
823 | size_t nPos | |
824 | , bool bEnable | |
825 | ) | |
826 | { | |
827 | wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars")); | |
828 | USHORT uFlag = 0; | |
829 | SHORT nId; | |
830 | ||
831 | if(!bEnable) | |
832 | uFlag = MIA_DISABLED; | |
833 | ||
834 | nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0)); | |
835 | if (nId == MIT_ERROR) | |
836 | { | |
837 | wxLogLastError("LogLastError"); | |
838 | return; | |
839 | } | |
840 | ::WinSendMsg((HWND)m_hMenu, MM_SETITEMATTR, MPFROM2SHORT(nId, TRUE), MPFROM2SHORT(MIA_DISABLED, uFlag)); | |
841 | Refresh(); | |
842 | } // end of wxMenuBar::EnableTop | |
843 | ||
844 | void wxMenuBar::SetLabelTop( | |
845 | size_t nPos | |
846 | , const wxString& rLabel | |
847 | ) | |
848 | { | |
849 | SHORT nId; | |
850 | MENUITEM vItem; | |
851 | ||
852 | wxCHECK_RET(nPos < GetMenuCount(), wxT("invalid menu index")); | |
853 | m_titles[nPos] = rLabel; | |
854 | ||
855 | if (!IsAttached()) | |
856 | { | |
857 | return; | |
858 | } | |
859 | ||
860 | nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0)); | |
861 | if (nId == MIT_ERROR) | |
862 | { | |
863 | wxLogLastError("LogLastError"); | |
864 | return; | |
865 | } | |
866 | if(!::WinSendMsg( (HWND)m_hMenu | |
867 | ,MM_QUERYITEM | |
868 | ,MPFROM2SHORT(nId, TRUE) | |
869 | ,MPARAM(&vItem) | |
870 | )) | |
871 | { | |
872 | wxLogLastError("QueryItem"); | |
873 | } | |
874 | nId = vItem.id; | |
875 | ||
876 | if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT, MPFROMSHORT(nId), (MPARAM)rLabel.c_str())); | |
877 | { | |
878 | wxLogLastError("ModifyMenu"); | |
879 | } | |
880 | Refresh(); | |
881 | } // end of wxMenuBar::SetLabelTop | |
882 | ||
883 | wxString wxMenuBar::GetLabelTop( | |
884 | size_t nPos | |
885 | ) const | |
886 | { | |
887 | wxCHECK_MSG( nPos < GetMenuCount(), wxEmptyString, | |
888 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
889 | return m_titles[nPos]; | |
890 | } // end of wxMenuBar::GetLabelTop | |
891 | ||
892 | // --------------------------------------------------------------------------- | |
893 | // wxMenuBar construction | |
894 | // --------------------------------------------------------------------------- | |
895 | ||
896 | wxMenu* wxMenuBar::Replace( | |
897 | size_t nPos | |
898 | , wxMenu* pMenu | |
899 | , const wxString& rTitle | |
900 | ) | |
901 | { | |
902 | SHORT nId; | |
903 | wxString sTitle = wxPMTextToLabel(rTitle); | |
904 | wxMenu* pMenuOld = wxMenuBarBase::Replace( nPos | |
905 | ,pMenu | |
906 | ,sTitle | |
907 | ); | |
908 | ||
909 | ||
910 | nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0)); | |
911 | if (nId == MIT_ERROR) | |
912 | { | |
913 | wxLogLastError("LogLastError"); | |
914 | return NULL; | |
915 | } | |
916 | if (!pMenuOld) | |
917 | return NULL; | |
918 | m_titles[nPos] = sTitle; | |
919 | if (IsAttached()) | |
920 | { | |
921 | ::WinSendMsg((HWND)m_hMenu, MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0); | |
922 | ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str()); | |
923 | ||
924 | #if wxUSE_ACCEL | |
925 | if (pMenuOld->HasAccels() || pMenu->HasAccels()) | |
926 | { | |
927 | // | |
928 | // Need to rebuild accell table | |
929 | // | |
930 | RebuildAccelTable(); | |
931 | } | |
932 | #endif // wxUSE_ACCEL | |
933 | Refresh(); | |
934 | } | |
935 | return pMenuOld; | |
936 | } // end of wxMenuBar::Replace | |
937 | ||
938 | bool wxMenuBar::Insert( | |
939 | size_t nPos | |
940 | , wxMenu* pMenu | |
941 | , const wxString& rTitle | |
942 | ) | |
943 | { | |
944 | wxString sTitle = wxPMTextToLabel(rTitle); | |
945 | ||
946 | if (!wxMenuBarBase::Insert( nPos | |
947 | ,pMenu | |
948 | ,sTitle | |
949 | )) | |
950 | return FALSE; | |
951 | ||
952 | m_titles.Insert( sTitle | |
953 | ,nPos | |
954 | ); | |
955 | ||
956 | if (IsAttached()) | |
957 | { | |
958 | pMenu->m_vMenuData.iPosition = nPos; | |
959 | ::WinSendMsg( (HWND)m_hMenu | |
960 | ,MM_INSERTITEM | |
961 | ,(MPARAM)&pMenu->m_vMenuData | |
962 | ,(MPARAM)sTitle.c_str() | |
963 | ); | |
964 | #if wxUSE_ACCEL | |
965 | if (pMenu->HasAccels()) | |
966 | { | |
967 | // need to rebuild accell table | |
968 | RebuildAccelTable(); | |
969 | } | |
970 | #endif // wxUSE_ACCEL | |
971 | Refresh(); | |
972 | } | |
973 | return TRUE; | |
974 | } // end of wxMenuBar::Insert | |
975 | ||
976 | bool wxMenuBar::Append( | |
977 | wxMenu* pMenu | |
978 | , const wxString& rsTitle | |
979 | ) | |
980 | { | |
981 | WXHMENU hSubmenu = pMenu ? pMenu->GetHMenu() : 0; | |
982 | ||
983 | wxCHECK_MSG(hSubmenu, FALSE, wxT("can't append invalid menu to menubar")); | |
984 | ||
985 | wxString sTitle = wxPMTextToLabel(rsTitle); | |
986 | ||
987 | if (!wxMenuBarBase::Append(pMenu, sTitle)) | |
988 | return FALSE; | |
989 | ||
990 | m_titles.Add(sTitle); | |
991 | ||
992 | if ( IsAttached() ) | |
993 | { | |
994 | pMenu->m_vMenuData.iPosition = MIT_END; | |
995 | ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str()); | |
996 | #if wxUSE_ACCEL | |
997 | if (pMenu->HasAccels()) | |
998 | { | |
999 | // | |
1000 | // Need to rebuild accell table | |
1001 | // | |
1002 | RebuildAccelTable(); | |
1003 | } | |
1004 | #endif // wxUSE_ACCEL | |
1005 | Refresh(); | |
1006 | } | |
1007 | return TRUE; | |
1008 | } // end of wxMenuBar::Append | |
1009 | ||
1010 | wxMenu* wxMenuBar::Remove( | |
1011 | size_t nPos | |
1012 | ) | |
1013 | { | |
1014 | wxMenu* pMenu = wxMenuBarBase::Remove(nPos); | |
1015 | SHORT nId; | |
1016 | ||
1017 | if (!pMenu) | |
1018 | return NULL; | |
1019 | ||
1020 | nId = SHORT1FROMMR(::WinSendMsg( (HWND)GetHmenu() | |
1021 | ,MM_ITEMIDFROMPOSITION | |
1022 | ,MPFROMSHORT(nPos) | |
1023 | ,(MPARAM)0) | |
1024 | ); | |
1025 | if (nId == MIT_ERROR) | |
1026 | { | |
1027 | wxLogLastError("LogLastError"); | |
1028 | return NULL; | |
1029 | } | |
1030 | if (IsAttached()) | |
1031 | { | |
1032 | ::WinSendMsg( (HWND)GetHmenu() | |
1033 | ,MM_REMOVEITEM | |
1034 | ,MPFROM2SHORT(nId, TRUE) | |
1035 | ,(MPARAM)0 | |
1036 | ); | |
1037 | ||
1038 | #if wxUSE_ACCEL | |
1039 | if (pMenu->HasAccels()) | |
1040 | { | |
1041 | // | |
1042 | // Need to rebuild accell table | |
1043 | // | |
1044 | RebuildAccelTable(); | |
1045 | } | |
1046 | #endif // wxUSE_ACCEL | |
1047 | Refresh(); | |
1048 | } | |
1049 | m_titles.Remove(nPos); | |
1050 | return pMenu; | |
1051 | } // end of wxMenuBar::Remove | |
1052 | ||
1053 | #if wxUSE_ACCEL | |
1054 | ||
1055 | void wxMenuBar::RebuildAccelTable() | |
1056 | { | |
1057 | // | |
1058 | // Merge the accelerators of all menus into one accel table | |
1059 | // | |
1060 | size_t nAccelCount = 0; | |
1061 | size_t i; | |
1062 | size_t nCount = GetMenuCount(); | |
1063 | ||
1064 | for (i = 0; i < nCount; i++) | |
1065 | { | |
1066 | nAccelCount += m_menus[i]->GetAccelCount(); | |
1067 | } | |
1068 | ||
1069 | if (nAccelCount) | |
1070 | { | |
1071 | wxAcceleratorEntry* pAccelEntries = new wxAcceleratorEntry[nAccelCount]; | |
1072 | ||
1073 | nAccelCount = 0; | |
1074 | for (i = 0; i < nCount; i++) | |
1075 | { | |
1076 | nAccelCount += m_menus[i]->CopyAccels(&pAccelEntries[nAccelCount]); | |
1077 | } | |
1078 | m_vAccelTable = wxAcceleratorTable( nAccelCount | |
1079 | ,pAccelEntries | |
1080 | ); | |
1081 | delete [] pAccelEntries; | |
1082 | } | |
1083 | } // end of wxMenuBar::RebuildAccelTable | |
1084 | ||
1085 | #endif // wxUSE_ACCEL | |
1086 | ||
1087 | void wxMenuBar::Attach( | |
1088 | wxFrame* pFrame | |
1089 | ) | |
1090 | { | |
1091 | wxMenuBarBase::Attach(pFrame); | |
1092 | ||
1093 | #if wxUSE_ACCEL | |
1094 | RebuildAccelTable(); | |
1095 | // | |
1096 | // Ensure the accelerator table is set to the frame (not the client!) | |
1097 | // | |
1098 | if (!::WinSetAccelTable( vHabmain | |
1099 | ,m_vAccelTable.GetHACCEL() | |
1100 | ,(HWND)pFrame->GetFrame() | |
1101 | )) | |
1102 | wxLogLastError("WinSetAccelTable"); | |
1103 | #endif // wxUSE_ACCEL | |
1104 | } // end of wxMenuBar::Attach | |
1105 | ||
1106 | void wxMenuBar::Detach() | |
1107 | { | |
1108 | ::WinDestroyWindow((HWND)m_hMenu); | |
1109 | m_hMenu = (WXHMENU)NULL; | |
1110 | m_menuBarFrame = NULL; | |
1111 | } // end of wxMenuBar::Detach | |
1112 | ||
1113 | // --------------------------------------------------------------------------- | |
1114 | // wxMenuBar searching for menu items | |
1115 | // --------------------------------------------------------------------------- | |
1116 | ||
1117 | // | |
1118 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
1119 | // | |
1120 | int wxMenuBar::FindMenuItem( | |
1121 | const wxString& rMenuString | |
1122 | , const wxString& rItemString | |
1123 | ) const | |
1124 | { | |
1125 | wxString sMenuLabel = wxStripMenuCodes(rMenuString); | |
1126 | size_t nCount = GetMenuCount(); | |
1127 | ||
1128 | for (size_t i = 0; i < nCount; i++) | |
1129 | { | |
1130 | wxString sTitle = wxStripMenuCodes(m_titles[i]); | |
1131 | ||
1132 | if (rMenuString == sTitle) | |
1133 | return m_menus[i]->FindItem(rItemString); | |
1134 | } | |
1135 | return wxNOT_FOUND; | |
1136 | } // end of wxMenuBar::FindMenuItem | |
1137 | ||
1138 | wxMenuItem* wxMenuBar::FindItem( | |
1139 | int nId | |
1140 | , wxMenu** ppItemMenu | |
1141 | ) const | |
1142 | { | |
1143 | if (ppItemMenu) | |
1144 | *ppItemMenu = NULL; | |
1145 | ||
1146 | wxMenuItem* pItem = NULL; | |
1147 | size_t nCount = GetMenuCount(); | |
1148 | ||
1149 | for (size_t i = 0; !pItem && (i < nCount); i++) | |
1150 | { | |
1151 | pItem = m_menus[i]->FindItem( nId | |
1152 | ,ppItemMenu | |
1153 | ); | |
1154 | } | |
1155 | return pItem; | |
1156 | } // end of wxMenuBar::FindItem | |
1157 | ||
1158 | wxMenuItem* wxMenuBar::FindItem( | |
1159 | int nId | |
1160 | , ULONG hItem | |
1161 | , wxMenu** ppItemMenu | |
1162 | ) const | |
1163 | { | |
1164 | if (ppItemMenu) | |
1165 | *ppItemMenu = NULL; | |
1166 | ||
1167 | wxMenuItem* pItem = NULL; | |
1168 | size_t nCount = GetMenuCount(); | |
1169 | ||
1170 | for (size_t i = 0; !pItem && (i < nCount); i++) | |
1171 | { | |
1172 | pItem = m_menus[i]->FindItem( nId | |
1173 | ,hItem | |
1174 | ,ppItemMenu | |
1175 | ); | |
1176 | } | |
1177 | return pItem; | |
1178 | } // end of wxMenuBar::FindItem | |
1179 |