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