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
;
80 Append(ID_SEPARATOR
, m_title
) ;
87 // The wxWindow destructor will take care of deleting the submenus.
98 // Not sure if this is right
99 if (m_menuParent
&& m_menuBar
)
105 wxNode
*node
= m_menuItems
.First();
108 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
111 if (item->GetSubMenu())
112 item->DeleteSubMenu();
115 wxNode
*next
= node
->Next();
127 // function appends a new item or submenu to the menu
128 void wxMenu::Append(wxMenuItem
*pItem
)
130 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
132 m_menuItems
.Append(pItem
);
135 pItem
->CreateItem (m_menuWidget
, m_menuBar
, m_topLevelMenu
); // this is a dynamic Append
140 void wxMenu::AppendSeparator()
142 Append(new wxMenuItem(this, ID_SEPARATOR
));
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?
151 void wxMenu::Append(int id
, const wxString
& label
, wxMenu
*subMenu
,
152 const wxString
& helpString
)
154 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, subMenu
));
156 subMenu
->m_topLevelMenu
= m_topLevelMenu
;
159 // Ordinary menu item
160 void wxMenu::Append(int id
, const wxString
& label
,
161 const wxString
& helpString
, bool checkable
)
163 // 'checkable' parameter is useless for Windows.
164 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
167 void wxMenu::Delete(int id
)
173 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
175 item
= (wxMenuItem
*)node
->Data();
176 if (item
->GetId() == id
)
183 item
->DestroyItem(TRUE
);
185 // See also old code - don't know if this is needed (seems redundant).
187 if (item->GetSubMenu()) {
188 item->subMenu->top_level_menu = item->GetSubMenu();
189 item->subMenu->window_parent = NULL;
190 children->DeleteObject(item->GetSubMenu());
194 m_menuItems
.DeleteNode(node
);
198 void wxMenu::Enable(int id
, bool flag
)
200 wxMenuItem
*item
= FindItemForId(id
);
201 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
206 bool wxMenu::Enabled(int Id
) const
208 wxMenuItem
*item
= FindItemForId(Id
);
209 wxCHECK( item
!= NULL
, FALSE
);
211 return item
->IsEnabled();
214 void wxMenu::Check(int Id
, bool Flag
)
216 wxMenuItem
*item
= FindItemForId(Id
);
217 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
222 bool wxMenu::Checked(int id
) const
224 wxMenuItem
*item
= FindItemForId(id
);
225 wxCHECK( item
!= NULL
, FALSE
);
227 return item
->IsChecked();
230 void wxMenu::SetTitle(const wxString
& label
)
234 wxNode
*node
= m_menuItems
.First ();
238 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
239 Widget widget
= (Widget
) item
->GetButtonWidget();
243 XmString title_str
= XmStringCreateSimple ((char*) (const char*) label
);
244 XtVaSetValues (widget
,
245 XmNlabelString
, title_str
,
247 // TODO: should we delete title_str now?
250 const wxString
wxMenu::GetTitle() const
255 void wxMenu::SetLabel(int id
, const wxString
& label
)
257 wxMenuItem
*item
= FindItemForId(id
);
258 if (item
== (wxMenuItem
*) NULL
)
261 item
->SetLabel(label
);
264 wxString
wxMenu::GetLabel(int id
) const
266 wxMenuItem
*it
= NULL
;
267 WXWidget w
= FindMenuItem (id
, &it
);
272 XtVaGetValues ((Widget
) w
,
273 XmNlabelString
, &text
,
276 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
285 return wxEmptyString
;
289 return wxEmptyString
;
292 // Finds the item id matching the given string, -1 if not found.
293 int wxMenu::FindItem (const wxString
& itemString
) const
297 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
299 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
301 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
302 if (item
->GetSubMenu())
304 int ans
= item
->GetSubMenu()->FindItem(itemString
);
308 if ( !item
->IsSeparator() )
310 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
311 if (strcmp(buf1
, buf2
) == 0)
312 return item
->GetId();
319 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
323 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
325 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
327 if (item
->GetId() == itemId
)
330 *itemMenu
= (wxMenu
*) this;
334 if (item
->GetSubMenu())
336 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
347 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
349 wxMenuItem
*item
= FindItemForId (itemId
);
351 item
->SetHelp(helpString
);
354 wxString
wxMenu::GetHelpString (int itemId
) const
356 wxMenuItem
*item
= FindItemForId (itemId
);
358 return (item
== NULL
) ? str
: item
->GetHelp();
361 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
363 bool processed
= FALSE
;
368 (void) (*(m_callback
)) (*this, event
);
372 // Try the menu's event handler
373 if ( !processed
&& GetEventHandler())
375 processed
= GetEventHandler()->ProcessEvent(event
);
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);
385 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
387 Widget widget
= (Widget
) GetMainWidget();
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
396 if (menu
->GetParent() && (menu
->GetId() != -1))
399 if (menu
->GetMainWidget()) {
400 menu
->DestroyMenu(TRUE
);
403 wxWindow
*parent
= this;
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
410 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
418 if (this->IsKindOf(CLASSINFO(wxCanvas)))
420 wxCanvas *canvas = (wxCanvas *) this;
421 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
422 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
426 Display
*display
= XtDisplay (widget
);
427 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
428 Window thisWindow
= XtWindow (widget
);
430 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
431 &rootX
, &rootY
, &childWindow
);
433 XButtonPressedEvent event
;
434 event
.type
= ButtonPress
;
440 event
.x_root
= rootX
;
441 event
.y_root
= rootY
;
443 XmMenuPosition (menuWidget
, &event
);
444 XtManageChild (menuWidget
);
450 wxMenuBar::wxMenuBar()
452 m_eventHandler
= this;
456 m_menuBarFrame
= NULL
;
459 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
461 m_eventHandler
= this;
464 m_titles
= new wxString
[n
];
466 for ( i
= 0; i
< n
; i
++ )
467 m_titles
[i
] = titles
[i
];
468 m_menuBarFrame
= NULL
;
471 wxMenuBar::~wxMenuBar()
474 for (i
= 0; i
< m_menuCount
; i
++)
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
)
486 wxMenu
*itemMenu
= NULL
;
487 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
493 void wxMenuBar::EnableTop(int pos
, bool flag
)
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
)
502 wxMenu
*itemMenu
= NULL
;
503 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
507 if (!item
->IsCheckable())
513 bool wxMenuBar::Checked(int id
) const
515 wxMenu
*itemMenu
= NULL
;
516 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
520 return item
->IsChecked();
523 bool wxMenuBar::Enabled(int id
) const
525 wxMenu
*itemMenu
= NULL
;
526 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
530 return item
->IsEnabled();
533 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
535 wxMenu
*itemMenu
= NULL
;
536 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
541 item
->SetLabel(label
);
544 wxString
wxMenuBar::GetLabel(int id
) const
546 wxMenu
*itemMenu
= NULL
;
547 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
552 return item
->GetLabel();
555 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
557 wxASSERT( (pos
< m_menuCount
) );
559 Widget w
= (Widget
) m_menus
[pos
]->GetButtonWidget();
562 XmString label_str
= XmStringCreateSimple ((char*) (const char*) label
);
564 XmNlabelString
, label_str
,
566 XmStringFree (label_str
);
570 wxString
wxMenuBar::GetLabelTop(int pos
) const
572 wxASSERT( (pos
< m_menuCount
) );
574 Widget w
= (Widget
) m_menus
[pos
]->GetButtonWidget();
580 XmNlabelString
, &text
,
583 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
591 return wxEmptyString
;
595 return wxEmptyString
;
599 bool wxMenuBar::OnDelete(wxMenu
*menu
, int pos
)
601 // Only applies to dynamic deletion (when set in frame)
605 menu
->DestroyMenu(TRUE
);
609 bool wxMenuBar::OnAppend(wxMenu
*menu
, const char *title
)
611 // Only applies to dynamic append (when set in frame)
615 // Probably should be an assert here
616 if (menu
->GetParent())
619 // Has already been appended
620 if (menu
->GetButtonWidget())
623 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
624 menu
->SetButtonWidget(w
);
629 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
631 if (!OnAppend(menu
, title
))
635 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
636 wxString
*new_titles
= new wxString
[m_menuCount
];
639 for (i
= 0; i
< m_menuCount
- 1; i
++)
641 new_menus
[i
] = m_menus
[i
];
643 new_titles
[i
] = m_titles
[i
];
652 m_titles
= new_titles
;
654 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
655 m_titles
[m_menuCount
- 1] = title
;
657 menu
->SetMenuBar(this);
658 menu
->SetParent(this);
661 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
668 for (ii
= 0; ii
< m_menuCount
; ii
++)
670 if (m_menus
[ii
] == menu
)
673 if (ii
>= m_menuCount
)
677 if (ii
< 0 || ii
>= m_menuCount
)
682 if (!OnDelete(menu
, ii
))
685 menu
->SetParent((wxEvtHandler
*) NULL
);
688 for (j
= ii
; j
< m_menuCount
; j
++)
690 m_menus
[j
] = m_menus
[j
+ 1];
691 m_titles
[j
] = m_titles
[j
+ 1];
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
701 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
703 for (i
= 0; i
< m_menuCount
; i
++)
705 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
706 if (strcmp (buf1
, buf2
) == 0)
707 return m_menus
[i
]->FindItem (itemString
);
712 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
** itemMenu
) const
717 wxMenuItem
*item
= NULL
;
719 for (i
= 0; i
< m_menuCount
; i
++)
720 if ((item
= m_menus
[i
]->FindItemForId (id
, itemMenu
)))
725 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
728 for (i
= 0; i
< m_menuCount
; i
++)
730 if (m_menus
[i
]->FindItemForId (id
))
732 m_menus
[i
]->SetHelpString (id
, helpString
);
738 wxString
wxMenuBar::GetHelpString (int id
) const
741 for (i
= 0; i
< m_menuCount
; i
++)
743 if (m_menus
[i
]->FindItemForId (id
))
744 return wxString(m_menus
[i
]->GetHelpString (id
));
751 extern wxApp
*wxTheApp
;
752 static XtWorkProcId WorkProcMenuId
;
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...
759 int PostDeletionOfMenu( XtPointer
* clientData
)
761 XtRemoveWorkProc(WorkProcMenuId
);
762 wxMenu
*menu
= (wxMenu
*)clientData
;
764 if (menu
->GetMainWidget()) {
765 wxList
& list
= menu
->GetParent()->GetItems();
766 list
.DeleteObject(menu
);
767 menu
->DestroyMenu(TRUE
);
769 /* Mark as no longer popped up */
775 wxMenuPopdownCallback(Widget w
, XtPointer clientData
,
778 wxMenu
*menu
= (wxMenu
*)clientData
;
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!! :(
786 WorkProcMenuId
= XtAppAddWorkProc(
787 (XtAppContext
) wxTheApp
->GetAppContext(),
788 (XtWorkProc
) PostDeletionOfMenu
,
790 // Apparently not found in Motif headers
791 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
795 * Create a popup or pulldown menu.
796 * Submenus of a popup will be pulldown.
800 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
802 Widget menu
= (Widget
) 0;
803 Widget buttonWidget
= (Widget
) 0;
805 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
806 XtSetArg (args
[1], XmNpacking
, XmPACK_COLUMN
);
810 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
813 (XtCallbackProc
)wxMenuPopdownCallback
,
818 char mnem
= wxFindMnemonic (title
);
819 wxStripMenuCodes ((char*) (const char*) title
, wxBuffer
);
821 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
823 XmString label_str
= XmStringCreateSimple (wxBuffer
);
824 buttonWidget
= XtVaCreateManagedWidget (wxBuffer
,
826 xmCascadeButtonGadgetClass
, (Widget
) parent
,
828 xmCascadeButtonWidgetClass
, (Widget
) parent
,
830 XmNlabelString
, label_str
,
835 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
837 XmStringFree (label_str
);
840 m_menuWidget
= (WXWidget
) menu
;
843 m_topLevelMenu
= topMenu
;
845 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
847 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
848 item
->CreateItem (menu
, menuBar
, topMenu
);
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
)
859 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
861 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
862 item
->SetMenuBar((wxMenuBar
*) NULL
);
864 item
->DestroyItem(full
);
871 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
872 XtDestroyWidget ((Widget
) m_buttonWidget
);
873 m_buttonWidget
= (WXWidget
) 0;
876 if (m_menuWidget
&& full
)
878 XtDestroyWidget((Widget
) m_menuWidget
);
879 m_menuWidget
= (WXWidget
) NULL
;
883 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
888 *it
= (wxMenuItem
*) NULL
;
889 return m_buttonWidget
;
892 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
894 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
895 if (item
->GetId() == id
)
899 return item
->GetButtonWidget();
902 if (item
->GetSubMenu())
904 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
913 *it
= (wxMenuItem
*) NULL
;
914 return (WXWidget
) NULL
;