]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menu.cpp
More Motif additions: mdi and sashtest samples now just about work!
[wxWidgets.git] / src / motif / menu.cpp
CommitLineData
4bb6408c
JS
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"
50414e24
JS
29#include "wx/app.h"
30#include "wx/frame.h"
4bb6408c
JS
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
50414e24
JS
42#include "wx/motif/private.h"
43
4bb6408c
JS
44// other standard headers
45// ----------------------
46#include <string.h>
47
4bb6408c
JS
48#if !USE_SHARED_LIBRARY
49IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
50IMPLEMENT_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)
60wxMenu::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;
50414e24 74 m_topLevelMenu = (wxMenu*) NULL;
4bb6408c
JS
75 m_ownedByMenuBar = FALSE;
76 m_menuParent = (wxMenu*) NULL;
77
78 if (m_title != "")
79 {
50414e24 80 Append(ID_SEPARATOR, m_title) ;
4bb6408c
JS
81 AppendSeparator() ;
82 }
83
84 Callback(func);
4bb6408c
JS
85}
86
87// The wxWindow destructor will take care of deleting the submenus.
88wxMenu::~wxMenu()
89{
50414e24
JS
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 }
4bb6408c
JS
104
105 wxNode *node = m_menuItems.First();
106 while (node)
107 {
108 wxMenuItem *item = (wxMenuItem *)node->Data();
109
50414e24 110 /*
4bb6408c
JS
111 if (item->GetSubMenu())
112 item->DeleteSubMenu();
50414e24 113 */
4bb6408c
JS
114
115 wxNode *next = node->Next();
116 delete item;
117 delete node;
118 node = next;
119 }
120}
121
122void wxMenu::Break()
123{
50414e24 124 m_numColumns ++;
4bb6408c
JS
125}
126
127// function appends a new item or submenu to the menu
128void wxMenu::Append(wxMenuItem *pItem)
129{
4bb6408c
JS
130 wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
131
132 m_menuItems.Append(pItem);
133
50414e24
JS
134 if (m_menuWidget)
135 pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append
136
4bb6408c
JS
137 m_noItems++;
138}
139
140void wxMenu::AppendSeparator()
141{
4bb6408c
JS
142 Append(new wxMenuItem(this, ID_SEPARATOR));
143}
144
145// Pullright item
50414e24
JS
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
151void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu,
4bb6408c
JS
152 const wxString& helpString)
153{
50414e24
JS
154 Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu));
155
156 subMenu->m_topLevelMenu = m_topLevelMenu;
4bb6408c
JS
157}
158
159// Ordinary menu item
50414e24 160void wxMenu::Append(int id, const wxString& label,
4bb6408c
JS
161 const wxString& helpString, bool checkable)
162{
163 // 'checkable' parameter is useless for Windows.
50414e24 164 Append(new wxMenuItem(this, id, label, helpString, checkable));
4bb6408c
JS
165}
166
167void wxMenu::Delete(int id)
168{
169 wxNode *node;
170 wxMenuItem *item;
171 int pos;
172
50414e24
JS
173 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
174 {
4bb6408c
JS
175 item = (wxMenuItem *)node->Data();
176 if (item->GetId() == id)
177 break;
178 }
179
180 if (!node)
181 return;
182
50414e24
JS
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
4bb6408c
JS
194 m_menuItems.DeleteNode(node);
195 delete item;
4bb6408c
JS
196}
197
50414e24 198void wxMenu::Enable(int id, bool flag)
4bb6408c 199{
50414e24 200 wxMenuItem *item = FindItemForId(id);
4bb6408c
JS
201 wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
202
50414e24 203 item->Enable(flag);
4bb6408c
JS
204}
205
206bool wxMenu::Enabled(int Id) const
207{
208 wxMenuItem *item = FindItemForId(Id);
209 wxCHECK( item != NULL, FALSE );
210
211 return item->IsEnabled();
212}
213
214void 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
50414e24 222bool wxMenu::Checked(int id) const
4bb6408c 223{
50414e24 224 wxMenuItem *item = FindItemForId(id);
4bb6408c
JS
225 wxCHECK( item != NULL, FALSE );
226
227 return item->IsChecked();
228}
229
230void wxMenu::SetTitle(const wxString& label)
231{
232 m_title = label ;
50414e24
JS
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?
4bb6408c
JS
248}
249
250const wxString wxMenu::GetTitle() const
251{
252 return m_title;
253}
254
255void wxMenu::SetLabel(int id, const wxString& label)
256{
50414e24
JS
257 wxMenuItem *item = FindItemForId(id);
258 if (item == (wxMenuItem*) NULL)
259 return;
4bb6408c 260
50414e24 261 item->SetLabel(label);
4bb6408c
JS
262}
263
50414e24 264wxString wxMenu::GetLabel(int id) const
4bb6408c 265{
50414e24
JS
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;
4bb6408c
JS
290}
291
292// Finds the item id matching the given string, -1 if not found.
293int 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
319wxMenuItem *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
347void wxMenu::SetHelpString(int itemId, const wxString& helpString)
348{
349 wxMenuItem *item = FindItemForId (itemId);
350 if (item)
351 item->SetHelp(helpString);
352}
353
354wxString wxMenu::GetHelpString (int itemId) const
355{
356 wxMenuItem *item = FindItemForId (itemId);
357 wxString str("");
358 return (item == NULL) ? str : item->GetHelp();
359}
360
361void 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
385bool wxWindow::PopupMenu(wxMenu *menu, int x, int y)
386{
50414e24
JS
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))
4bb6408c 397 return FALSE;
50414e24
JS
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;
4bb6408c
JS
447}
448
449// Menu Bar
450wxMenuBar::wxMenuBar()
451{
452 m_eventHandler = this;
453 m_menuCount = 0;
454 m_menus = NULL;
455 m_titles = NULL;
456 m_menuBarFrame = NULL;
4bb6408c
JS
457}
458
459wxMenuBar::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;
4bb6408c
JS
469}
470
471wxMenuBar::~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;
4bb6408c
JS
480}
481
482// Must only be used AFTER menu has been attached to frame,
483// otherwise use individual menus to enable/disable items
484void wxMenuBar::Enable(int id, bool flag)
485{
486 wxMenu *itemMenu = NULL;
487 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
488 if (!item)
489 return;
50414e24 490 item->Enable(flag);
4bb6408c
JS
491}
492
493void 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
500void 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
50414e24 510 item->Check(flag);
4bb6408c
JS
511}
512
513bool wxMenuBar::Checked(int id) const
514{
515 wxMenu *itemMenu = NULL;
516 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
517 if (!item)
518 return FALSE;
519
50414e24 520 return item->IsChecked();
4bb6408c
JS
521}
522
523bool wxMenuBar::Enabled(int id) const
524{
525 wxMenu *itemMenu = NULL;
526 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
527 if (!item)
528 return FALSE;
529
50414e24 530 return item->IsEnabled();
4bb6408c
JS
531}
532
4bb6408c
JS
533void 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
50414e24 541 item->SetLabel(label);
4bb6408c
JS
542}
543
544wxString wxMenuBar::GetLabel(int id) const
545{
546 wxMenu *itemMenu = NULL;
547 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
548
549 if (!item)
550 return wxString("");
551
50414e24 552 return item->GetLabel();
4bb6408c
JS
553}
554
555void wxMenuBar::SetLabelTop(int pos, const wxString& label)
556{
50414e24
JS
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 }
4bb6408c
JS
568}
569
570wxString wxMenuBar::GetLabelTop(int pos) const
571{
50414e24
JS
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
4bb6408c
JS
597}
598
50414e24 599bool wxMenuBar::OnDelete(wxMenu *menu, int pos)
4bb6408c 600{
50414e24
JS
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;
4bb6408c
JS
607}
608
50414e24 609bool wxMenuBar::OnAppend(wxMenu *menu, const char *title)
4bb6408c 610{
50414e24
JS
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;
4bb6408c
JS
627}
628
629void 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
50414e24
JS
657 menu->SetMenuBar(this);
658 menu->SetParent(this);
4bb6408c
JS
659}
660
661void 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
50414e24 685 menu->SetParent((wxEvtHandler*) NULL);
4bb6408c
JS
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.
697int 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
50414e24 712wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const
4bb6408c
JS
713{
714 if (itemMenu)
715 *itemMenu = NULL;
716
717 wxMenuItem *item = NULL;
718 int i;
719 for (i = 0; i < m_menuCount; i++)
50414e24 720 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
4bb6408c
JS
721 return item;
722 return NULL;
723}
724
50414e24 725void wxMenuBar::SetHelpString (int id, const wxString& helpString)
4bb6408c
JS
726{
727 int i;
728 for (i = 0; i < m_menuCount; i++)
729 {
50414e24 730 if (m_menus[i]->FindItemForId (id))
4bb6408c 731 {
50414e24 732 m_menus[i]->SetHelpString (id, helpString);
4bb6408c
JS
733 return;
734 }
735 }
736}
737
50414e24 738wxString wxMenuBar::GetHelpString (int id) const
4bb6408c
JS
739{
740 int i;
741 for (i = 0; i < m_menuCount; i++)
742 {
50414e24
JS
743 if (m_menus[i]->FindItemForId (id))
744 return wxString(m_menus[i]->GetHelpString (id));
4bb6408c
JS
745 }
746 return wxString("");
747}
748
50414e24
JS
749//// Motif-specific
750
751extern wxApp *wxTheApp;
752static 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
759int 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
774void
775wxMenuPopdownCallback(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
800WXWidget 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,
47d67540 825#if wxUSE_GADGETS
50414e24
JS
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.
857void 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
883WXWidget 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 }
4bb6408c 891
50414e24
JS
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}