]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | ||
13 | // ============================================================================ | |
14 | // headers & declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // wxWindows headers | |
18 | // ----------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menu.h" | |
22 | #pragma implementation "menuitem.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/menu.h" | |
26 | #include "wx/menuitem.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/utils.h" | |
29 | #include "wx/app.h" | |
30 | #include "wx/frame.h" | |
31 | ||
32 | #include <Xm/Label.h> | |
33 | #include <Xm/LabelG.h> | |
34 | #include <Xm/CascadeBG.h> | |
35 | #include <Xm/CascadeB.h> | |
36 | #include <Xm/SeparatoG.h> | |
37 | #include <Xm/PushBG.h> | |
38 | #include <Xm/ToggleB.h> | |
39 | #include <Xm/ToggleBG.h> | |
40 | #include <Xm/RowColumn.h> | |
41 | ||
42 | #include "wx/motif/private.h" | |
43 | ||
44 | // other standard headers | |
45 | // ---------------------- | |
46 | #include <string.h> | |
47 | ||
48 | #if !USE_SHARED_LIBRARY | |
49 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
50 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
51 | #endif | |
52 | ||
53 | // ============================================================================ | |
54 | // implementation | |
55 | // ============================================================================ | |
56 | ||
57 | // Menus | |
58 | ||
59 | // Construct a menu with optional title (then use append) | |
60 | wxMenu::wxMenu(const wxString& title, const wxFunction func) | |
61 | { | |
62 | m_title = title; | |
63 | m_parent = (wxEvtHandler*) NULL; | |
64 | m_eventHandler = this; | |
65 | m_noItems = 0; | |
66 | m_menuBar = NULL; | |
67 | ||
68 | //// Motif-specific members | |
69 | m_numColumns = 1; | |
70 | m_menuWidget = (WXWidget) NULL; | |
71 | m_popupShell = (WXWidget) NULL; | |
72 | m_buttonWidget = (WXWidget) NULL; | |
73 | m_menuId = 0; | |
74 | m_topLevelMenu = (wxMenu*) NULL; | |
75 | m_ownedByMenuBar = FALSE; | |
76 | m_menuParent = (wxMenu*) NULL; | |
77 | ||
78 | if (m_title != "") | |
79 | { | |
80 | Append(ID_SEPARATOR, m_title) ; | |
81 | AppendSeparator() ; | |
82 | } | |
83 | ||
84 | Callback(func); | |
85 | } | |
86 | ||
87 | // The wxWindow destructor will take care of deleting the submenus. | |
88 | wxMenu::~wxMenu() | |
89 | { | |
90 | if (m_menuWidget) | |
91 | { | |
92 | if (m_menuParent) | |
93 | DestroyMenu(TRUE); | |
94 | else | |
95 | DestroyMenu(FALSE); | |
96 | } | |
97 | ||
98 | // Not sure if this is right | |
99 | if (m_menuParent && m_menuBar) | |
100 | { | |
101 | m_menuParent = NULL; | |
102 | // m_menuBar = NULL; | |
103 | } | |
104 | ||
105 | wxNode *node = m_menuItems.First(); | |
106 | while (node) | |
107 | { | |
108 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
109 | ||
110 | /* | |
111 | if (item->GetSubMenu()) | |
112 | item->DeleteSubMenu(); | |
113 | */ | |
114 | ||
115 | wxNode *next = node->Next(); | |
116 | delete item; | |
117 | delete node; | |
118 | node = next; | |
119 | } | |
120 | } | |
121 | ||
122 | void wxMenu::Break() | |
123 | { | |
124 | m_numColumns ++; | |
125 | } | |
126 | ||
127 | // function appends a new item or submenu to the menu | |
128 | void wxMenu::Append(wxMenuItem *pItem) | |
129 | { | |
130 | wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" ); | |
131 | ||
132 | m_menuItems.Append(pItem); | |
133 | ||
134 | if (m_menuWidget) | |
135 | pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append | |
136 | ||
137 | m_noItems++; | |
138 | } | |
139 | ||
140 | void wxMenu::AppendSeparator() | |
141 | { | |
142 | Append(new wxMenuItem(this, ID_SEPARATOR)); | |
143 | } | |
144 | ||
145 | // Pullright item | |
146 | // N.B.: difference between old and new code. | |
147 | // Old code stores subMenu in 'children' for later deletion, | |
148 | // as well as in m_menuItems, whereas we only store it in | |
149 | // m_menuItems here. What implications does this have? | |
150 | ||
151 | void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu, | |
152 | const wxString& helpString) | |
153 | { | |
154 | Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu)); | |
155 | ||
156 | subMenu->m_topLevelMenu = m_topLevelMenu; | |
157 | } | |
158 | ||
159 | // Ordinary menu item | |
160 | void wxMenu::Append(int id, const wxString& label, | |
161 | const wxString& helpString, bool checkable) | |
162 | { | |
163 | // 'checkable' parameter is useless for Windows. | |
164 | Append(new wxMenuItem(this, id, label, helpString, checkable)); | |
165 | } | |
166 | ||
167 | void wxMenu::Delete(int id) | |
168 | { | |
169 | wxNode *node; | |
170 | wxMenuItem *item; | |
171 | int pos; | |
172 | ||
173 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) | |
174 | { | |
175 | item = (wxMenuItem *)node->Data(); | |
176 | if (item->GetId() == id) | |
177 | break; | |
178 | } | |
179 | ||
180 | if (!node) | |
181 | return; | |
182 | ||
183 | item->DestroyItem(TRUE); | |
184 | ||
185 | // See also old code - don't know if this is needed (seems redundant). | |
186 | /* | |
187 | if (item->GetSubMenu()) { | |
188 | item->subMenu->top_level_menu = item->GetSubMenu(); | |
189 | item->subMenu->window_parent = NULL; | |
190 | children->DeleteObject(item->GetSubMenu()); | |
191 | } | |
192 | */ | |
193 | ||
194 | m_menuItems.DeleteNode(node); | |
195 | delete item; | |
196 | } | |
197 | ||
198 | void wxMenu::Enable(int id, bool flag) | |
199 | { | |
200 | wxMenuItem *item = FindItemForId(id); | |
201 | wxCHECK_RET( item != NULL, "can't enable non-existing menu item" ); | |
202 | ||
203 | item->Enable(flag); | |
204 | } | |
205 | ||
206 | bool wxMenu::Enabled(int Id) const | |
207 | { | |
208 | wxMenuItem *item = FindItemForId(Id); | |
209 | wxCHECK( item != NULL, FALSE ); | |
210 | ||
211 | return item->IsEnabled(); | |
212 | } | |
213 | ||
214 | void wxMenu::Check(int Id, bool Flag) | |
215 | { | |
216 | wxMenuItem *item = FindItemForId(Id); | |
217 | wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" ); | |
218 | ||
219 | item->Check(Flag); | |
220 | } | |
221 | ||
222 | bool wxMenu::Checked(int id) const | |
223 | { | |
224 | wxMenuItem *item = FindItemForId(id); | |
225 | wxCHECK( item != NULL, FALSE ); | |
226 | ||
227 | return item->IsChecked(); | |
228 | } | |
229 | ||
230 | void wxMenu::SetTitle(const wxString& label) | |
231 | { | |
232 | m_title = label ; | |
233 | ||
234 | wxNode *node = m_menuItems.First (); | |
235 | if (!node) | |
236 | return; | |
237 | ||
238 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
239 | Widget widget = (Widget) item->GetButtonWidget(); | |
240 | if (!widget) | |
241 | return; | |
242 | ||
243 | XmString title_str = XmStringCreateSimple ((char*) (const char*) label); | |
244 | XtVaSetValues (widget, | |
245 | XmNlabelString, title_str, | |
246 | NULL); | |
247 | // TODO: should we delete title_str now? | |
248 | } | |
249 | ||
250 | const wxString wxMenu::GetTitle() const | |
251 | { | |
252 | return m_title; | |
253 | } | |
254 | ||
255 | void wxMenu::SetLabel(int id, const wxString& label) | |
256 | { | |
257 | wxMenuItem *item = FindItemForId(id); | |
258 | if (item == (wxMenuItem*) NULL) | |
259 | return; | |
260 | ||
261 | item->SetLabel(label); | |
262 | } | |
263 | ||
264 | wxString wxMenu::GetLabel(int id) const | |
265 | { | |
266 | wxMenuItem *it = NULL; | |
267 | WXWidget w = FindMenuItem (id, &it); | |
268 | if (w) | |
269 | { | |
270 | XmString text; | |
271 | char *s; | |
272 | XtVaGetValues ((Widget) w, | |
273 | XmNlabelString, &text, | |
274 | NULL); | |
275 | ||
276 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
277 | { | |
278 | wxString str(s); | |
279 | XtFree (s); | |
280 | return str; | |
281 | } | |
282 | else | |
283 | { | |
284 | XmStringFree (text); | |
285 | return wxEmptyString; | |
286 | } | |
287 | } | |
288 | else | |
289 | return wxEmptyString; | |
290 | } | |
291 | ||
292 | // Finds the item id matching the given string, -1 if not found. | |
293 | int wxMenu::FindItem (const wxString& itemString) const | |
294 | { | |
295 | char buf1[200]; | |
296 | char buf2[200]; | |
297 | wxStripMenuCodes ((char *)(const char *)itemString, buf1); | |
298 | ||
299 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
300 | { | |
301 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
302 | if (item->GetSubMenu()) | |
303 | { | |
304 | int ans = item->GetSubMenu()->FindItem(itemString); | |
305 | if (ans > -1) | |
306 | return ans; | |
307 | } | |
308 | if ( !item->IsSeparator() ) | |
309 | { | |
310 | wxStripMenuCodes((char *)item->GetName().c_str(), buf2); | |
311 | if (strcmp(buf1, buf2) == 0) | |
312 | return item->GetId(); | |
313 | } | |
314 | } | |
315 | ||
316 | return -1; | |
317 | } | |
318 | ||
319 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const | |
320 | { | |
321 | if (itemMenu) | |
322 | *itemMenu = NULL; | |
323 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
324 | { | |
325 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
326 | ||
327 | if (item->GetId() == itemId) | |
328 | { | |
329 | if (itemMenu) | |
330 | *itemMenu = (wxMenu *) this; | |
331 | return item; | |
332 | } | |
333 | ||
334 | if (item->GetSubMenu()) | |
335 | { | |
336 | wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu); | |
337 | if (ans) | |
338 | return ans; | |
339 | } | |
340 | } | |
341 | ||
342 | if (itemMenu) | |
343 | *itemMenu = NULL; | |
344 | return NULL; | |
345 | } | |
346 | ||
347 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) | |
348 | { | |
349 | wxMenuItem *item = FindItemForId (itemId); | |
350 | if (item) | |
351 | item->SetHelp(helpString); | |
352 | } | |
353 | ||
354 | wxString wxMenu::GetHelpString (int itemId) const | |
355 | { | |
356 | wxMenuItem *item = FindItemForId (itemId); | |
357 | wxString str(""); | |
358 | return (item == NULL) ? str : item->GetHelp(); | |
359 | } | |
360 | ||
361 | void wxMenu::ProcessCommand(wxCommandEvent & event) | |
362 | { | |
363 | bool processed = FALSE; | |
364 | ||
365 | // Try a callback | |
366 | if (m_callback) | |
367 | { | |
368 | (void) (*(m_callback)) (*this, event); | |
369 | processed = TRUE; | |
370 | } | |
371 | ||
372 | // Try the menu's event handler | |
373 | if ( !processed && GetEventHandler()) | |
374 | { | |
375 | processed = GetEventHandler()->ProcessEvent(event); | |
376 | } | |
377 | /* TODO | |
378 | // Try the window the menu was popped up from (and up | |
379 | // through the hierarchy) | |
380 | if ( !processed && GetInvokingWindow()) | |
381 | processed = GetInvokingWindow()->ProcessEvent(event); | |
382 | */ | |
383 | } | |
384 | ||
385 | bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) | |
386 | { | |
387 | Widget widget = (Widget) GetMainWidget(); | |
388 | ||
389 | /* The menuId field seems to be usused, so we'll use it to | |
390 | indicate whether a menu is popped up or not: | |
391 | 0: Not currently created as a popup | |
392 | -1: Created as a popup, but not active | |
393 | 1: Active popup. | |
394 | */ | |
395 | ||
396 | if (menu->GetParent() && (menu->GetId() != -1)) | |
397 | return FALSE; | |
398 | ||
399 | if (menu->GetMainWidget()) { | |
400 | menu->DestroyMenu(TRUE); | |
401 | } | |
402 | ||
403 | wxWindow *parent = this; | |
404 | ||
405 | menu->SetId(1); /* Mark as popped-up */ | |
406 | menu->CreateMenu(NULL, widget, menu); | |
407 | // menu->SetParent(parent); | |
408 | // parent->children->Append(menu); // Store menu for later deletion | |
409 | ||
410 | Widget menuWidget = (Widget) menu->GetMainWidget(); | |
411 | ||
412 | int rootX = 0; | |
413 | int rootY = 0; | |
414 | ||
415 | int deviceX = x; | |
416 | int deviceY = y; | |
417 | /* | |
418 | if (this->IsKindOf(CLASSINFO(wxCanvas))) | |
419 | { | |
420 | wxCanvas *canvas = (wxCanvas *) this; | |
421 | deviceX = canvas->GetDC ()->LogicalToDeviceX (x); | |
422 | deviceY = canvas->GetDC ()->LogicalToDeviceY (y); | |
423 | } | |
424 | */ | |
425 | ||
426 | Display *display = XtDisplay (widget); | |
427 | Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget)); | |
428 | Window thisWindow = XtWindow (widget); | |
429 | Window childWindow; | |
430 | XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY, | |
431 | &rootX, &rootY, &childWindow); | |
432 | ||
433 | XButtonPressedEvent event; | |
434 | event.type = ButtonPress; | |
435 | event.button = 1; | |
436 | ||
437 | event.x = deviceX; | |
438 | event.y = deviceY; | |
439 | ||
440 | event.x_root = rootX; | |
441 | event.y_root = rootY; | |
442 | ||
443 | XmMenuPosition (menuWidget, &event); | |
444 | XtManageChild (menuWidget); | |
445 | ||
446 | return TRUE; | |
447 | } | |
448 | ||
449 | // Menu Bar | |
450 | wxMenuBar::wxMenuBar() | |
451 | { | |
452 | m_eventHandler = this; | |
453 | m_menuCount = 0; | |
454 | m_menus = NULL; | |
455 | m_titles = NULL; | |
456 | m_menuBarFrame = NULL; | |
457 | } | |
458 | ||
459 | wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[]) | |
460 | { | |
461 | m_eventHandler = this; | |
462 | m_menuCount = n; | |
463 | m_menus = menus; | |
464 | m_titles = new wxString[n]; | |
465 | int i; | |
466 | for ( i = 0; i < n; i++ ) | |
467 | m_titles[i] = titles[i]; | |
468 | m_menuBarFrame = NULL; | |
469 | } | |
470 | ||
471 | wxMenuBar::~wxMenuBar() | |
472 | { | |
473 | int i; | |
474 | for (i = 0; i < m_menuCount; i++) | |
475 | { | |
476 | delete m_menus[i]; | |
477 | } | |
478 | delete[] m_menus; | |
479 | delete[] m_titles; | |
480 | } | |
481 | ||
482 | // Must only be used AFTER menu has been attached to frame, | |
483 | // otherwise use individual menus to enable/disable items | |
484 | void wxMenuBar::Enable(int id, bool flag) | |
485 | { | |
486 | wxMenu *itemMenu = NULL; | |
487 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
488 | if (!item) | |
489 | return; | |
490 | item->Enable(flag); | |
491 | } | |
492 | ||
493 | void wxMenuBar::EnableTop(int pos, bool flag) | |
494 | { | |
495 | // TODO | |
496 | } | |
497 | ||
498 | // Must only be used AFTER menu has been attached to frame, | |
499 | // otherwise use individual menus | |
500 | void wxMenuBar::Check(int id, bool flag) | |
501 | { | |
502 | wxMenu *itemMenu = NULL; | |
503 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
504 | if (!item) | |
505 | return; | |
506 | ||
507 | if (!item->IsCheckable()) | |
508 | return ; | |
509 | ||
510 | item->Check(flag); | |
511 | } | |
512 | ||
513 | bool wxMenuBar::Checked(int id) const | |
514 | { | |
515 | wxMenu *itemMenu = NULL; | |
516 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
517 | if (!item) | |
518 | return FALSE; | |
519 | ||
520 | return item->IsChecked(); | |
521 | } | |
522 | ||
523 | bool wxMenuBar::Enabled(int id) const | |
524 | { | |
525 | wxMenu *itemMenu = NULL; | |
526 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
527 | if (!item) | |
528 | return FALSE; | |
529 | ||
530 | return item->IsEnabled(); | |
531 | } | |
532 | ||
533 | void wxMenuBar::SetLabel(int id, const wxString& label) | |
534 | { | |
535 | wxMenu *itemMenu = NULL; | |
536 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
537 | ||
538 | if (!item) | |
539 | return; | |
540 | ||
541 | item->SetLabel(label); | |
542 | } | |
543 | ||
544 | wxString wxMenuBar::GetLabel(int id) const | |
545 | { | |
546 | wxMenu *itemMenu = NULL; | |
547 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
548 | ||
549 | if (!item) | |
550 | return wxString(""); | |
551 | ||
552 | return item->GetLabel(); | |
553 | } | |
554 | ||
555 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
556 | { | |
557 | wxASSERT( (pos < m_menuCount) ); | |
558 | ||
559 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
560 | if (w) | |
561 | { | |
562 | XmString label_str = XmStringCreateSimple ((char*) (const char*) label); | |
563 | XtVaSetValues (w, | |
564 | XmNlabelString, label_str, | |
565 | NULL); | |
566 | XmStringFree (label_str); | |
567 | } | |
568 | } | |
569 | ||
570 | wxString wxMenuBar::GetLabelTop(int pos) const | |
571 | { | |
572 | wxASSERT( (pos < m_menuCount) ); | |
573 | ||
574 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
575 | if (w) | |
576 | { | |
577 | XmString text; | |
578 | char *s; | |
579 | XtVaGetValues (w, | |
580 | XmNlabelString, &text, | |
581 | NULL); | |
582 | ||
583 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
584 | { | |
585 | wxString str(s); | |
586 | XtFree (s); | |
587 | return str; | |
588 | } | |
589 | else | |
590 | { | |
591 | return wxEmptyString; | |
592 | } | |
593 | } | |
594 | else | |
595 | return wxEmptyString; | |
596 | ||
597 | } | |
598 | ||
599 | bool wxMenuBar::OnDelete(wxMenu *menu, int pos) | |
600 | { | |
601 | // Only applies to dynamic deletion (when set in frame) | |
602 | if (!m_menuBarFrame) | |
603 | return TRUE; | |
604 | ||
605 | menu->DestroyMenu(TRUE); | |
606 | return TRUE; | |
607 | } | |
608 | ||
609 | bool wxMenuBar::OnAppend(wxMenu *menu, const char *title) | |
610 | { | |
611 | // Only applies to dynamic append (when set in frame) | |
612 | if (!m_menuBarFrame) | |
613 | return TRUE; | |
614 | ||
615 | // Probably should be an assert here | |
616 | if (menu->GetParent()) | |
617 | return FALSE; | |
618 | ||
619 | // Has already been appended | |
620 | if (menu->GetButtonWidget()) | |
621 | return FALSE; | |
622 | ||
623 | WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE); | |
624 | menu->SetButtonWidget(w); | |
625 | ||
626 | return TRUE; | |
627 | } | |
628 | ||
629 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) | |
630 | { | |
631 | if (!OnAppend(menu, title)) | |
632 | return; | |
633 | ||
634 | m_menuCount ++; | |
635 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
636 | wxString *new_titles = new wxString[m_menuCount]; | |
637 | int i; | |
638 | ||
639 | for (i = 0; i < m_menuCount - 1; i++) | |
640 | { | |
641 | new_menus[i] = m_menus[i]; | |
642 | m_menus[i] = NULL; | |
643 | new_titles[i] = m_titles[i]; | |
644 | m_titles[i] = ""; | |
645 | } | |
646 | if (m_menus) | |
647 | { | |
648 | delete[]m_menus; | |
649 | delete[]m_titles; | |
650 | } | |
651 | m_menus = new_menus; | |
652 | m_titles = new_titles; | |
653 | ||
654 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
655 | m_titles[m_menuCount - 1] = title; | |
656 | ||
657 | menu->SetMenuBar(this); | |
658 | menu->SetParent(this); | |
659 | } | |
660 | ||
661 | void wxMenuBar::Delete(wxMenu * menu, int i) | |
662 | { | |
663 | int j; | |
664 | int ii = (int) i; | |
665 | ||
666 | if (menu != 0) | |
667 | { | |
668 | for (ii = 0; ii < m_menuCount; ii++) | |
669 | { | |
670 | if (m_menus[ii] == menu) | |
671 | break; | |
672 | } | |
673 | if (ii >= m_menuCount) | |
674 | return; | |
675 | } else | |
676 | { | |
677 | if (ii < 0 || ii >= m_menuCount) | |
678 | return; | |
679 | menu = m_menus[ii]; | |
680 | } | |
681 | ||
682 | if (!OnDelete(menu, ii)) | |
683 | return; | |
684 | ||
685 | menu->SetParent((wxEvtHandler*) NULL); | |
686 | ||
687 | -- m_menuCount; | |
688 | for (j = ii; j < m_menuCount; j++) | |
689 | { | |
690 | m_menus[j] = m_menus[j + 1]; | |
691 | m_titles[j] = m_titles[j + 1]; | |
692 | } | |
693 | } | |
694 | ||
695 | // Find the menu menuString, item itemString, and return the item id. | |
696 | // Returns -1 if none found. | |
697 | int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const | |
698 | { | |
699 | char buf1[200]; | |
700 | char buf2[200]; | |
701 | wxStripMenuCodes ((char *)(const char *)menuString, buf1); | |
702 | int i; | |
703 | for (i = 0; i < m_menuCount; i++) | |
704 | { | |
705 | wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); | |
706 | if (strcmp (buf1, buf2) == 0) | |
707 | return m_menus[i]->FindItem (itemString); | |
708 | } | |
709 | return -1; | |
710 | } | |
711 | ||
712 | wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const | |
713 | { | |
714 | if (itemMenu) | |
715 | *itemMenu = NULL; | |
716 | ||
717 | wxMenuItem *item = NULL; | |
718 | int i; | |
719 | for (i = 0; i < m_menuCount; i++) | |
720 | if ((item = m_menus[i]->FindItemForId (id, itemMenu))) | |
721 | return item; | |
722 | return NULL; | |
723 | } | |
724 | ||
725 | void wxMenuBar::SetHelpString (int id, const wxString& helpString) | |
726 | { | |
727 | int i; | |
728 | for (i = 0; i < m_menuCount; i++) | |
729 | { | |
730 | if (m_menus[i]->FindItemForId (id)) | |
731 | { | |
732 | m_menus[i]->SetHelpString (id, helpString); | |
733 | return; | |
734 | } | |
735 | } | |
736 | } | |
737 | ||
738 | wxString wxMenuBar::GetHelpString (int id) const | |
739 | { | |
740 | int i; | |
741 | for (i = 0; i < m_menuCount; i++) | |
742 | { | |
743 | if (m_menus[i]->FindItemForId (id)) | |
744 | return wxString(m_menus[i]->GetHelpString (id)); | |
745 | } | |
746 | return wxString(""); | |
747 | } | |
748 | ||
749 | //// Motif-specific | |
750 | ||
751 | extern wxApp *wxTheApp; | |
752 | static XtWorkProcId WorkProcMenuId; | |
753 | ||
754 | /* Since PopupMenu under Motif stills grab right mouse button events | |
755 | * after it was closed, we need to delete the associated widgets to | |
756 | * allow next PopUpMenu to appear... | |
757 | */ | |
758 | ||
759 | int PostDeletionOfMenu( XtPointer* clientData ) | |
760 | { | |
761 | XtRemoveWorkProc(WorkProcMenuId); | |
762 | wxMenu *menu = (wxMenu *)clientData; | |
763 | ||
764 | if (menu->GetMainWidget()) { | |
765 | wxList& list = menu->GetParent()->GetItems(); | |
766 | list.DeleteObject(menu); | |
767 | menu->DestroyMenu(TRUE); | |
768 | } | |
769 | /* Mark as no longer popped up */ | |
770 | menu->m_menuId = -1; | |
771 | return TRUE; | |
772 | } | |
773 | ||
774 | void | |
775 | wxMenuPopdownCallback(Widget w, XtPointer clientData, | |
776 | XtPointer ptr) | |
777 | { | |
778 | wxMenu *menu = (wxMenu *)clientData; | |
779 | ||
780 | // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr> | |
781 | /* Since Callbacks of MenuItems are not yet processed, we put a | |
782 | * background job which will be done when system will be idle. | |
783 | * What awful hack!! :( | |
784 | */ | |
785 | ||
786 | WorkProcMenuId = XtAppAddWorkProc( | |
787 | (XtAppContext) wxTheApp->GetAppContext(), | |
788 | (XtWorkProc) PostDeletionOfMenu, | |
789 | (XtPointer) menu ); | |
790 | // Apparently not found in Motif headers | |
791 | // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL ); | |
792 | } | |
793 | ||
794 | /* | |
795 | * Create a popup or pulldown menu. | |
796 | * Submenus of a popup will be pulldown. | |
797 | * | |
798 | */ | |
799 | ||
800 | WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown) | |
801 | { | |
802 | Widget menu = (Widget) 0; | |
803 | Widget buttonWidget = (Widget) 0; | |
804 | Arg args[5]; | |
805 | XtSetArg (args[0], XmNnumColumns, m_numColumns); | |
806 | XtSetArg (args[1], XmNpacking, XmPACK_COLUMN); | |
807 | ||
808 | if (!pullDown) | |
809 | { | |
810 | menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2); | |
811 | XtAddCallback(menu, | |
812 | XmNunmapCallback, | |
813 | (XtCallbackProc)wxMenuPopdownCallback, | |
814 | (XtPointer)this); | |
815 | } | |
816 | else | |
817 | { | |
818 | char mnem = wxFindMnemonic (title); | |
819 | wxStripMenuCodes ((char*) (const char*) title, wxBuffer); | |
820 | ||
821 | menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2); | |
822 | ||
823 | XmString label_str = XmStringCreateSimple (wxBuffer); | |
824 | buttonWidget = XtVaCreateManagedWidget (wxBuffer, | |
825 | #if wxUSE_GADGETS | |
826 | xmCascadeButtonGadgetClass, (Widget) parent, | |
827 | #else | |
828 | xmCascadeButtonWidgetClass, (Widget) parent, | |
829 | #endif | |
830 | XmNlabelString, label_str, | |
831 | XmNsubMenuId, menu, | |
832 | NULL); | |
833 | ||
834 | if (mnem != 0) | |
835 | XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL); | |
836 | ||
837 | XmStringFree (label_str); | |
838 | } | |
839 | ||
840 | m_menuWidget = (WXWidget) menu; | |
841 | ||
842 | m_menuBar = menuBar; | |
843 | m_topLevelMenu = topMenu; | |
844 | ||
845 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
846 | { | |
847 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
848 | item->CreateItem (menu, menuBar, topMenu); | |
849 | } | |
850 | ||
851 | return buttonWidget; | |
852 | } | |
853 | ||
854 | // Destroys the Motif implementation of the menu, | |
855 | // but maintains the wxWindows data structures so we can | |
856 | // do a CreateMenu again. | |
857 | void wxMenu::DestroyMenu (bool full) | |
858 | { | |
859 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
860 | { | |
861 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
862 | item->SetMenuBar((wxMenuBar*) NULL); | |
863 | ||
864 | item->DestroyItem(full); | |
865 | } // for() | |
866 | ||
867 | if (m_buttonWidget) | |
868 | { | |
869 | if (full) | |
870 | { | |
871 | XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL); | |
872 | XtDestroyWidget ((Widget) m_buttonWidget); | |
873 | m_buttonWidget = (WXWidget) 0; | |
874 | } | |
875 | } | |
876 | if (m_menuWidget && full) | |
877 | { | |
878 | XtDestroyWidget((Widget) m_menuWidget); | |
879 | m_menuWidget = (WXWidget) NULL; | |
880 | } | |
881 | } | |
882 | ||
883 | WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const | |
884 | { | |
885 | if (id == m_menuId) | |
886 | { | |
887 | if (it) | |
888 | *it = (wxMenuItem*) NULL; | |
889 | return m_buttonWidget; | |
890 | } | |
891 | ||
892 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
893 | { | |
894 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
895 | if (item->GetId() == id) | |
896 | { | |
897 | if (it) | |
898 | *it = item; | |
899 | return item->GetButtonWidget(); | |
900 | } | |
901 | ||
902 | if (item->GetSubMenu()) | |
903 | { | |
904 | WXWidget w = item->GetSubMenu()->FindMenuItem (id, it); | |
905 | if (w) | |
906 | { | |
907 | return w; | |
908 | } | |
909 | } | |
910 | } // for() | |
911 | ||
912 | if (it) | |
913 | *it = (wxMenuItem*) NULL; | |
914 | return (WXWidget) NULL; | |
915 | } |