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