]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/menuitem.cpp
Native wxControl::DoGetBestSize() implementation
[wxWidgets.git] / src / motif / menuitem.cpp
index f44459aaaef598997870a9994c51d4dc3e99a927..e632f72dea7141138884c4b4d427fb7dae617bc9 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
-// headers & declarations
+// declarations
 // ============================================================================
 
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
+    #pragma implementation "menuitem.h"
+#endif
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#include "wx/defs.h"
+
 #include "wx/menu.h"
 #include "wx/menuitem.h"
 #include "wx/utils.h"
 #include "wx/frame.h"
 
+#ifdef __VMS__
+#pragma message disable nosimpint
+#endif
 #include <Xm/Label.h>
 #include <Xm/LabelG.h>
 #include <Xm/CascadeBG.h>
 #include <Xm/ToggleB.h>
 #include <Xm/ToggleBG.h>
 #include <Xm/RowColumn.h>
+#ifdef __VMS__
+#pragma message enable nosimpint
+#endif
 
 #include "wx/motif/private.h"
 
-void wxMenuItemCallback (Widget w, XtPointer clientData,
-                         XtPointer ptr);
-void wxMenuItemArmCallback (Widget w, XtPointer clientData,
-                            XtPointer ptr);
-void wxMenuItemDisarmCallback (Widget w, XtPointer clientData,
-                               XtPointer ptr);
+// ----------------------------------------------------------------------------
+// functions prototypes
+// ----------------------------------------------------------------------------
+
+static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr);
+static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr);
+static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer ptr);
 
 // ============================================================================
 // implementation
@@ -45,9 +65,7 @@ void wxMenuItemDisarmCallback (Widget w, XtPointer clientData,
 // dynamic classes implementation
 // ----------------------------------------------------------------------------
 
-#if !USE_SHARED_LIBRARY
 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
-#endif  //USE_SHARED_LIBRARY
 
 // ----------------------------------------------------------------------------
 // wxMenuItem
@@ -56,53 +74,34 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
 // ctor & dtor
 // -----------
 
-wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
-                       const wxString& strName, const wxString& strHelp,
-                       bool bCheckable,
-                       wxMenu *pSubMenu) :
-m_strHelp(strHelp),
-m_bCheckable(bCheckable),
-m_strName(strName)
+wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
+                       int id,
+                       const wxString& strName,
+                       const wxString& strHelp,
+                       wxItemKind kind,
+                       wxMenu *pSubMenu)
+          : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
 {
-    wxASSERT( pParentMenu != NULL );
-
-    m_pParentMenu = pParentMenu;
-    m_pSubMenu    = pSubMenu;
-    m_idItem      = id;
-    m_bEnabled    = TRUE;
-    m_bChecked    = FALSE;
-
-    //// Motif-specific
-    m_menuBar = NULL;
+    // Motif-specific
+    m_menuBar      = NULL;
     m_buttonWidget = (WXWidget) NULL;
-    m_topMenu = NULL;
+    m_topMenu      = NULL;
 }
 
 wxMenuItem::~wxMenuItem()
 {
 }
 
-// misc
-// ----
-
-// delete the sub menu
-void wxMenuItem::DeleteSubMenu()
-{
-    wxASSERT( m_pSubMenu != NULL );
-
-    delete m_pSubMenu;
-    m_pSubMenu = NULL;
-}
-
 // change item state
 // -----------------
 
 void wxMenuItem::Enable(bool bDoEnable)
 {
-    if ( m_bEnabled != bDoEnable )
+    if ( m_isEnabled != bDoEnable )
     {
-        if ( m_pSubMenu == NULL )
-        {     // normal menu item
+        if ( !IsSubMenu() )
+        {
+            // normal menu item
             if (m_buttonWidget)
                 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
         }
@@ -114,7 +113,7 @@ void wxMenuItem::Enable(bool bDoEnable)
                 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
         }
 
-        m_bEnabled = bDoEnable;
+        wxMenuItemBase::Enable(bDoEnable);
     }
 }
 
@@ -122,17 +121,46 @@ void wxMenuItem::Check(bool bDoCheck)
 {
     wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
 
-    if ( m_bChecked != bDoCheck )
+    if ( m_isChecked != bDoCheck )
     {
-        if (m_buttonWidget && XtIsSubclass ((Widget) m_buttonWidget, xmToggleButtonGadgetClass))
+        if ( m_buttonWidget )
         {
-            XtVaSetValues ( (Widget) m_buttonWidget, XmNset, (Boolean) bDoCheck, NULL);
+            wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
+                                       xmToggleButtonGadgetClass),
+                          wxT("checkable menu item must be a toggle button") );
+
+            XtVaSetValues((Widget)m_buttonWidget,
+                          XmNset, (Boolean)bDoCheck,
+                          NULL);
         }
-        m_bChecked = bDoCheck;
+
+        wxMenuItemBase::Check(bDoCheck);
     }
 }
 
-//// Motif-specific
+/* static */
+wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
+{
+    return wxStripMenuCodes(text);
+}
+
+// ----------------------------------------------------------------------------
+// wxMenuItemBase
+// ----------------------------------------------------------------------------
+
+wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
+                                int id,
+                                const wxString& name,
+                                const wxString& help,
+                                wxItemKind kind,
+                                wxMenu *subMenu)
+{
+    return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
+}
+
+// ----------------------------------------------------------------------------
+// Motif-specific
+// ----------------------------------------------------------------------------
 
 void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
 {
@@ -143,12 +171,12 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen
     {
         // Id=-2 identifies a Title item.
         m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
-            (wxStripMenuCodes(m_strName),
+            (wxStripMenuCodes(m_text),
             xmLabelGadgetClass, (Widget) menu, NULL);
     }
-    else if ((!m_strName.IsNull() && m_strName != "") && (!m_pSubMenu))
+    else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
     {
-        wxString strName = wxStripMenuCodes(m_strName);
+        wxString strName = wxStripMenuCodes(m_text);
         if (IsCheckable())
         {
             m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
@@ -160,13 +188,13 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen
             m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
             xmPushButtonGadgetClass, (Widget) menu,
             NULL);
-        char mnem = wxFindMnemonic (m_strName);
+        char mnem = wxFindMnemonic (m_text);
         if (mnem != 0)
             XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
 
         //// TODO: proper accelerator treatment. What does wxFindAccelerator
         //// look for?
-        strName = m_strName;
+        strName = m_text;
         char *accel = wxFindAccelerator (strName);
         if (accel)
             XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
@@ -203,10 +231,10 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen
         m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
             xmSeparatorGadgetClass, (Widget) menu, NULL);
     }
-    else if (m_pSubMenu)
+    else if (m_subMenu)
     {
-        m_buttonWidget = m_pSubMenu->CreateMenu (menuBar, menu, topMenu, m_strName, TRUE);
-        m_pSubMenu->SetButtonWidget(m_buttonWidget);
+        m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
+        m_subMenu->SetButtonWidget(m_buttonWidget);
         XtAddCallback ((Widget) m_buttonWidget,
             XmNcascadingCallback,
             (XtCallbackProc) wxMenuItemArmCallback,
@@ -220,10 +248,10 @@ void wxMenuItem::DestroyItem(bool full)
 {
     if (GetId() == -2)
     {
-        ;                      // Nothing
+        ;      // Nothing
 
     }
-    else if ((!m_strName.IsNull() && (m_strName != "")) && !m_pSubMenu)
+    else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
     {
         if (m_buttonWidget)
         {
@@ -241,7 +269,7 @@ void wxMenuItem::DestroyItem(bool full)
     }
     else if (GetId() == -1)
     {
-        ;                      // Nothing
+        ;      // Nothing
 
     }
     else if (GetSubMenu())
@@ -251,7 +279,7 @@ void wxMenuItem::DestroyItem(bool full)
             XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
                 wxMenuItemArmCallback, (XtPointer) this);
         }
-        m_pSubMenu->DestroyMenu(full);
+        m_subMenu->DestroyMenu(full);
         if (full)
             m_buttonWidget = NULL;
     }
@@ -263,18 +291,18 @@ void wxMenuItem::DestroyItem(bool full)
     }
 }
 
-void wxMenuItem::SetLabel(const wxString& label)
+void wxMenuItem::SetText(const wxString& label)
 {
     char mnem = wxFindMnemonic (label);
     wxString label2 = wxStripMenuCodes(label);
 
-    m_strName = label;
+    m_text = label;
 
     if (m_buttonWidget)
     {
         wxXmString label_str(label2);
         XtVaSetValues ((Widget) m_buttonWidget,
-            XmNlabelString, label_str,
+            XmNlabelString, label_str(),
             NULL);
         if (mnem != 0)
             XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
@@ -291,39 +319,58 @@ void wxMenuItem::SetLabel(const wxString& label)
     }
 }
 
-void wxMenuItemCallback (Widget w, XtPointer clientData,
-                         XtPointer ptr)
+// ----------------------------------------------------------------------------
+// Motif callbacks
+// ----------------------------------------------------------------------------
+
+void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
+                         XtPointer WXUNUSED(ptr))
 {
     wxMenuItem *item = (wxMenuItem *) clientData;
     if (item)
     {
+        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
+        event.SetInt( item->GetId() );
+
         if (item->IsCheckable())
         {
             Boolean isChecked = FALSE;
-            XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL);
-            item->SetChecked(isChecked);
+            XtVaGetValues ((Widget) item->GetButtonWidget(),
+                           XmNset, & isChecked,
+                           NULL);
+
+            // only set the flag, don't actually check anything
+            item->wxMenuItemBase::Check(isChecked);
+            event.SetInt(isChecked);
         }
+
         if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
         {
-            wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
-            commandEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
-            commandEvent.SetInt( item->GetId() );
+            event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
 
-            item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent);
+            item->GetMenuBar()->GetMenuBarFrame()
+                ->GetEventHandler()->ProcessEvent(event);
         }
+        // this is the child of a popup menu
         else if (item->GetTopMenu())
         {
-            wxCommandEvent event (wxEVT_COMMAND_MENU_SELECTED, item->GetId());
             event.SetEventObject(item->GetTopMenu());
-            event.SetInt( item->GetId() );
 
             item->GetTopMenu()->ProcessCommand (event);
+
+            // Since PopupMenu under Motif still grab right mouse
+            // button events after it was closed, we need to delete
+            // the associated widgets to allow next PopUpMenu to
+            // appear; this needs to be done there because doing it in
+            // a WorkProc as before may cause crashes if a menu item causes
+            // the parent window of the menu to be destroyed
+            item->GetTopMenu()->DestroyWidgetAndDetach();
         }
     }
 }
 
-void wxMenuItemArmCallback (Widget w, XtPointer clientData,
-                       XtPointer ptr)
+void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
+                       XtPointer WXUNUSED(ptr))
 {
     wxMenuItem *item = (wxMenuItem *) clientData;
     if (item)
@@ -333,14 +380,15 @@ void wxMenuItemArmCallback (Widget w, XtPointer clientData,
             wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
             menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
 
-            item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
+            item->GetMenuBar()->GetMenuBarFrame()
+                ->GetEventHandler()->ProcessEvent(menuEvent);
         }
     }
 }
 
 void
-wxMenuItemDisarmCallback (Widget w, XtPointer clientData,
-                          XtPointer ptr)
+wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
+                          XtPointer WXUNUSED(ptr))
 {
     wxMenuItem *item = (wxMenuItem *) clientData;
     if (item)
@@ -352,7 +400,8 @@ wxMenuItemDisarmCallback (Widget w, XtPointer clientData,
             wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
             menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
 
-            item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
+            item->GetMenuBar()->GetMenuBarFrame()
+                ->GetEventHandler()->ProcessEvent(menuEvent);
         }
     }
 }