1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
14 // headers & declarations
15 // ============================================================================
21 #pragma implementation "menu.h"
22 #pragma implementation "menuitem.h"
26 #include "wx/menuitem.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>
42 #include "wx/motif/private.h"
44 // other standard headers
45 // ----------------------
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
53 // ============================================================================
55 // ============================================================================
59 // Construct a menu with optional title (then use append)
60 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
63 m_parent
= (wxEvtHandler
*) NULL
;
64 m_eventHandler
= this;
68 //// Motif-specific members
70 m_menuWidget
= (WXWidget
) NULL
;
71 m_popupShell
= (WXWidget
) NULL
;
72 m_buttonWidget
= (WXWidget
) NULL
;
74 m_topLevelMenu
= (wxMenu
*) NULL
;
75 m_ownedByMenuBar
= FALSE
;
76 m_menuParent
= (wxMenu
*) NULL
;
77 m_clientData
= (void*) NULL
;
81 Append(ID_SEPARATOR
, m_title
) ;
88 // The wxWindow destructor will take care of deleting the submenus.
99 // Not sure if this is right
100 if (m_menuParent
&& m_menuBar
)
106 wxNode
*node
= m_menuItems
.First();
109 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
112 if (item->GetSubMenu())
113 item->DeleteSubMenu();
116 wxNode
*next
= node
->Next();
128 // function appends a new item or submenu to the menu
129 void wxMenu::Append(wxMenuItem
*pItem
)
131 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
133 m_menuItems
.Append(pItem
);
136 pItem
->CreateItem (m_menuWidget
, m_menuBar
, m_topLevelMenu
); // this is a dynamic Append
141 void wxMenu::AppendSeparator()
143 Append(new wxMenuItem(this, ID_SEPARATOR
));
147 // N.B.: difference between old and new code.
148 // Old code stores subMenu in 'children' for later deletion,
149 // as well as in m_menuItems, whereas we only store it in
150 // m_menuItems here. What implications does this have?
152 void wxMenu::Append(int id
, const wxString
& label
, wxMenu
*subMenu
,
153 const wxString
& helpString
)
155 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, subMenu
));
157 subMenu
->m_topLevelMenu
= m_topLevelMenu
;
160 // Ordinary menu item
161 void wxMenu::Append(int id
, const wxString
& label
,
162 const wxString
& helpString
, bool checkable
)
164 // 'checkable' parameter is useless for Windows.
165 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
168 void wxMenu::Delete(int id
)
174 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
176 item
= (wxMenuItem
*)node
->Data();
177 if (item
->GetId() == id
)
184 item
->DestroyItem(TRUE
);
186 // See also old code - don't know if this is needed (seems redundant).
188 if (item->GetSubMenu()) {
189 item->subMenu->top_level_menu = item->GetSubMenu();
190 item->subMenu->window_parent = NULL;
191 children->DeleteObject(item->GetSubMenu());
195 m_menuItems
.DeleteNode(node
);
199 void wxMenu::Enable(int id
, bool flag
)
201 wxMenuItem
*item
= FindItemForId(id
);
202 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
207 bool wxMenu::Enabled(int Id
) const
209 wxMenuItem
*item
= FindItemForId(Id
);
210 wxCHECK( item
!= NULL
, FALSE
);
212 return item
->IsEnabled();
215 void wxMenu::Check(int Id
, bool Flag
)
217 wxMenuItem
*item
= FindItemForId(Id
);
218 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
223 bool wxMenu::Checked(int id
) const
225 wxMenuItem
*item
= FindItemForId(id
);
226 wxCHECK( item
!= NULL
, FALSE
);
228 return item
->IsChecked();
231 void wxMenu::SetTitle(const wxString
& label
)
235 wxNode
*node
= m_menuItems
.First ();
239 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
240 Widget widget
= (Widget
) item
->GetButtonWidget();
244 XmString title_str
= XmStringCreateSimple ((char*) (const char*) label
);
245 XtVaSetValues (widget
,
246 XmNlabelString
, title_str
,
248 // TODO: should we delete title_str now?
251 const wxString
wxMenu::GetTitle() const
256 void wxMenu::SetLabel(int id
, const wxString
& label
)
258 wxMenuItem
*item
= FindItemForId(id
);
259 if (item
== (wxMenuItem
*) NULL
)
262 item
->SetLabel(label
);
265 wxString
wxMenu::GetLabel(int id
) const
267 wxMenuItem
*it
= NULL
;
268 WXWidget w
= FindMenuItem (id
, &it
);
273 XtVaGetValues ((Widget
) w
,
274 XmNlabelString
, &text
,
277 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
286 return wxEmptyString
;
290 return wxEmptyString
;
293 // Finds the item id matching the given string, -1 if not found.
294 int wxMenu::FindItem (const wxString
& itemString
) const
298 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
300 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
302 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
303 if (item
->GetSubMenu())
305 int ans
= item
->GetSubMenu()->FindItem(itemString
);
309 if ( !item
->IsSeparator() )
311 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
312 if (strcmp(buf1
, buf2
) == 0)
313 return item
->GetId();
320 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
324 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
326 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
328 if (item
->GetId() == itemId
)
331 *itemMenu
= (wxMenu
*) this;
335 if (item
->GetSubMenu())
337 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
348 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
350 wxMenuItem
*item
= FindItemForId (itemId
);
352 item
->SetHelp(helpString
);
355 wxString
wxMenu::GetHelpString (int itemId
) const
357 wxMenuItem
*item
= FindItemForId (itemId
);
359 return (item
== NULL
) ? str
: item
->GetHelp();
362 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
364 bool processed
= FALSE
;
369 (void) (*(m_callback
)) (*this, event
);
373 // Try the menu's event handler
374 if ( !processed
&& GetEventHandler())
376 processed
= GetEventHandler()->ProcessEvent(event
);
379 // Try the window the menu was popped up from (and up
380 // through the hierarchy)
381 if ( !processed && GetInvokingWindow())
382 processed = GetInvokingWindow()->ProcessEvent(event);
386 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
388 Widget widget
= (Widget
) GetMainWidget();
390 /* The menuId field seems to be usused, so we'll use it to
391 indicate whether a menu is popped up or not:
392 0: Not currently created as a popup
393 -1: Created as a popup, but not active
397 if (menu
->GetParent() && (menu
->GetId() != -1))
400 if (menu
->GetMainWidget()) {
401 menu
->DestroyMenu(TRUE
);
404 wxWindow
*parent
= this;
406 menu
->SetId(1); /* Mark as popped-up */
407 menu
->CreateMenu(NULL
, widget
, menu
);
408 // menu->SetParent(parent);
409 // parent->children->Append(menu); // Store menu for later deletion
411 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
419 if (this->IsKindOf(CLASSINFO(wxCanvas)))
421 wxCanvas *canvas = (wxCanvas *) this;
422 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
423 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
427 Display
*display
= XtDisplay (widget
);
428 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
429 Window thisWindow
= XtWindow (widget
);
431 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
432 &rootX
, &rootY
, &childWindow
);
434 XButtonPressedEvent event
;
435 event
.type
= ButtonPress
;
441 event
.x_root
= rootX
;
442 event
.y_root
= rootY
;
444 XmMenuPosition (menuWidget
, &event
);
445 XtManageChild (menuWidget
);
451 wxMenuBar::wxMenuBar()
453 m_eventHandler
= this;
457 m_menuBarFrame
= NULL
;
460 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
462 m_eventHandler
= this;
465 m_titles
= new wxString
[n
];
467 for ( i
= 0; i
< n
; i
++ )
468 m_titles
[i
] = titles
[i
];
469 m_menuBarFrame
= NULL
;
472 wxMenuBar::~wxMenuBar()
475 for (i
= 0; i
< m_menuCount
; i
++)
483 // Must only be used AFTER menu has been attached to frame,
484 // otherwise use individual menus to enable/disable items
485 void wxMenuBar::Enable(int id
, bool flag
)
487 wxMenu
*itemMenu
= NULL
;
488 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
494 void wxMenuBar::EnableTop(int pos
, bool flag
)
499 // Must only be used AFTER menu has been attached to frame,
500 // otherwise use individual menus
501 void wxMenuBar::Check(int id
, bool flag
)
503 wxMenu
*itemMenu
= NULL
;
504 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
508 if (!item
->IsCheckable())
514 bool wxMenuBar::Checked(int id
) const
516 wxMenu
*itemMenu
= NULL
;
517 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
521 return item
->IsChecked();
524 bool wxMenuBar::Enabled(int id
) const
526 wxMenu
*itemMenu
= NULL
;
527 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
531 return item
->IsEnabled();
534 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
536 wxMenu
*itemMenu
= NULL
;
537 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
542 item
->SetLabel(label
);
545 wxString
wxMenuBar::GetLabel(int id
) const
547 wxMenu
*itemMenu
= NULL
;
548 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
553 return item
->GetLabel();
556 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
558 wxASSERT( (pos
< m_menuCount
) );
560 Widget w
= (Widget
) m_menus
[pos
]->GetButtonWidget();
563 XmString label_str
= XmStringCreateSimple ((char*) (const char*) label
);
565 XmNlabelString
, label_str
,
567 XmStringFree (label_str
);
571 wxString
wxMenuBar::GetLabelTop(int pos
) const
573 wxASSERT( (pos
< m_menuCount
) );
575 Widget w
= (Widget
) m_menus
[pos
]->GetButtonWidget();
581 XmNlabelString
, &text
,
584 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
592 return wxEmptyString
;
596 return wxEmptyString
;
600 bool wxMenuBar::OnDelete(wxMenu
*menu
, int pos
)
602 // Only applies to dynamic deletion (when set in frame)
606 menu
->DestroyMenu(TRUE
);
610 bool wxMenuBar::OnAppend(wxMenu
*menu
, const char *title
)
612 // Only applies to dynamic append (when set in frame)
616 // Probably should be an assert here
617 if (menu
->GetParent())
620 // Has already been appended
621 if (menu
->GetButtonWidget())
624 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
625 menu
->SetButtonWidget(w
);
630 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
632 if (!OnAppend(menu
, title
))
636 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
637 wxString
*new_titles
= new wxString
[m_menuCount
];
640 for (i
= 0; i
< m_menuCount
- 1; i
++)
642 new_menus
[i
] = m_menus
[i
];
644 new_titles
[i
] = m_titles
[i
];
653 m_titles
= new_titles
;
655 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
656 m_titles
[m_menuCount
- 1] = title
;
658 menu
->SetMenuBar(this);
659 menu
->SetParent(this);
662 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
669 for (ii
= 0; ii
< m_menuCount
; ii
++)
671 if (m_menus
[ii
] == menu
)
674 if (ii
>= m_menuCount
)
678 if (ii
< 0 || ii
>= m_menuCount
)
683 if (!OnDelete(menu
, ii
))
686 menu
->SetParent((wxEvtHandler
*) NULL
);
689 for (j
= ii
; j
< m_menuCount
; j
++)
691 m_menus
[j
] = m_menus
[j
+ 1];
692 m_titles
[j
] = m_titles
[j
+ 1];
696 // Find the menu menuString, item itemString, and return the item id.
697 // Returns -1 if none found.
698 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
702 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
704 for (i
= 0; i
< m_menuCount
; i
++)
706 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
707 if (strcmp (buf1
, buf2
) == 0)
708 return m_menus
[i
]->FindItem (itemString
);
713 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
** itemMenu
) const
718 wxMenuItem
*item
= NULL
;
720 for (i
= 0; i
< m_menuCount
; i
++)
721 if ((item
= m_menus
[i
]->FindItemForId (id
, itemMenu
)))
726 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
729 for (i
= 0; i
< m_menuCount
; i
++)
731 if (m_menus
[i
]->FindItemForId (id
))
733 m_menus
[i
]->SetHelpString (id
, helpString
);
739 wxString
wxMenuBar::GetHelpString (int id
) const
742 for (i
= 0; i
< m_menuCount
; i
++)
744 if (m_menus
[i
]->FindItemForId (id
))
745 return wxString(m_menus
[i
]->GetHelpString (id
));
752 extern wxApp
*wxTheApp
;
753 static XtWorkProcId WorkProcMenuId
;
755 /* Since PopupMenu under Motif stills grab right mouse button events
756 * after it was closed, we need to delete the associated widgets to
757 * allow next PopUpMenu to appear...
760 int PostDeletionOfMenu( XtPointer
* clientData
)
762 XtRemoveWorkProc(WorkProcMenuId
);
763 wxMenu
*menu
= (wxMenu
*)clientData
;
765 if (menu
->GetMainWidget()) {
766 wxList
& list
= menu
->GetParent()->GetItems();
767 list
.DeleteObject(menu
);
768 menu
->DestroyMenu(TRUE
);
770 /* Mark as no longer popped up */
776 wxMenuPopdownCallback(Widget w
, XtPointer clientData
,
779 wxMenu
*menu
= (wxMenu
*)clientData
;
781 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
782 /* Since Callbacks of MenuItems are not yet processed, we put a
783 * background job which will be done when system will be idle.
784 * What awful hack!! :(
787 WorkProcMenuId
= XtAppAddWorkProc(
788 (XtAppContext
) wxTheApp
->GetAppContext(),
789 (XtWorkProc
) PostDeletionOfMenu
,
791 // Apparently not found in Motif headers
792 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
796 * Create a popup or pulldown menu.
797 * Submenus of a popup will be pulldown.
801 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
803 Widget menu
= (Widget
) 0;
804 Widget buttonWidget
= (Widget
) 0;
806 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
807 XtSetArg (args
[1], XmNpacking
, XmPACK_COLUMN
);
811 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
814 (XtCallbackProc
)wxMenuPopdownCallback
,
819 char mnem
= wxFindMnemonic (title
);
820 wxStripMenuCodes ((char*) (const char*) title
, wxBuffer
);
822 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
824 XmString label_str
= XmStringCreateSimple (wxBuffer
);
825 buttonWidget
= XtVaCreateManagedWidget (wxBuffer
,
827 xmCascadeButtonGadgetClass
, (Widget
) parent
,
829 xmCascadeButtonWidgetClass
, (Widget
) parent
,
831 XmNlabelString
, label_str
,
836 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
838 XmStringFree (label_str
);
841 m_menuWidget
= (WXWidget
) menu
;
844 m_topLevelMenu
= topMenu
;
846 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
848 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
849 item
->CreateItem (menu
, menuBar
, topMenu
);
855 // Destroys the Motif implementation of the menu,
856 // but maintains the wxWindows data structures so we can
857 // do a CreateMenu again.
858 void wxMenu::DestroyMenu (bool full
)
860 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
862 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
863 item
->SetMenuBar((wxMenuBar
*) NULL
);
865 item
->DestroyItem(full
);
872 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
873 XtDestroyWidget ((Widget
) m_buttonWidget
);
874 m_buttonWidget
= (WXWidget
) 0;
877 if (m_menuWidget
&& full
)
879 XtDestroyWidget((Widget
) m_menuWidget
);
880 m_menuWidget
= (WXWidget
) NULL
;
884 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
889 *it
= (wxMenuItem
*) NULL
;
890 return m_buttonWidget
;
893 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
895 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
896 if (item
->GetId() == id
)
900 return item
->GetButtonWidget();
903 if (item
->GetSubMenu())
905 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
914 *it
= (wxMenuItem
*) NULL
;
915 return (WXWidget
) NULL
;