]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/menuitem.cpp
Fixes for semicolons, commas and wxSTRING_MAXLEN from Wlodek Szafran.
[wxWidgets.git] / src / motif / menuitem.cpp
index 01ed0ccdaa0bb99e6b04e39c505cda7f918409cf..2b57e57d6accc64011b89a9b28b69c92c11c87b7 100644 (file)
@@ -1,5 +1,5 @@
 ///////////////////////////////////////////////////////////////////////////////
-// Name:        menuitem.cpp
+// Name:        src/motif/menuitem.cpp
 // Purpose:     wxMenuItem implementation
 // Author:      Julian Smart
 // Modified by:
 // declarations
 // ============================================================================
 
-#ifdef __GNUG__
-    #pragma implementation "menuitem.h"
-#endif
-
 // ----------------------------------------------------------------------------
 // headers
 // ----------------------------------------------------------------------------
 
-#include "wx/menu.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
 #include "wx/menuitem.h"
-#include "wx/utils.h"
-#include "wx/frame.h"
+#include "wx/stockitem.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/utils.h"
+    #include "wx/frame.h"
+    #include "wx/menu.h"
+#endif
 
 #ifdef __VMS__
 #pragma message disable nosimpint
@@ -60,7 +63,7 @@ static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer p
 // dynamic classes implementation
 // ----------------------------------------------------------------------------
 
-    IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
+IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
 
 // ----------------------------------------------------------------------------
 // wxMenuItem
@@ -69,23 +72,14 @@ static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer p
 // ctor & dtor
 // -----------
 
-wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
-                       const wxString& strName, const wxString& strHelp,
-                       bool bCheckable,
+wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
+                       int id,
+                       const wxString& strName,
+                       const wxString& strHelp,
+                       wxItemKind kind,
                        wxMenu *pSubMenu)
+          : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
 {
-    wxASSERT_MSG( pParentMenu != NULL, wxT("menuitem should have a menu") );
-
-    // common init
-    m_parentMenu  = pParentMenu;
-    m_subMenu     = pSubMenu;
-    m_id          = id;
-    m_isEnabled   = TRUE;
-    m_isChecked   = FALSE;
-    m_help        = strHelp;
-    m_isCheckable = bCheckable;
-    m_text        = strName;
-
     // Motif-specific
     m_menuBar      = NULL;
     m_buttonWidget = (WXWidget) NULL;
@@ -96,24 +90,12 @@ wxMenuItem::~wxMenuItem()
 {
 }
 
-// misc
-// ----
-
-// delete the sub menu
-void wxMenuItem::DeleteSubMenu()
-{
-    wxASSERT( m_subMenu != NULL );
-
-    delete m_subMenu;
-    m_subMenu = NULL;
-}
-
 // change item state
 // -----------------
 
 void wxMenuItem::Enable(bool bDoEnable)
 {
-    if ( m_isChecked != bDoEnable )
+    if ( m_isEnabled != bDoEnable )
     {
         if ( !IsSubMenu() )
         {
@@ -154,12 +136,12 @@ void wxMenuItem::Check(bool bDoCheck)
     }
 }
 
-wxString wxMenuItem::GetLabel() const
+/* static */
+wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
 {
-    return wxStripMenuCodes(m_text);
+    return wxStripMenuCodes(text);
 }
 
-
 // ----------------------------------------------------------------------------
 // wxMenuItemBase
 // ----------------------------------------------------------------------------
@@ -168,41 +150,56 @@ wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
                                 int id,
                                 const wxString& name,
                                 const wxString& help,
-                                bool isCheckable,
+                                wxItemKind kind,
                                 wxMenu *subMenu)
 {
-    return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
+    return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
 }
 
 // ----------------------------------------------------------------------------
 // Motif-specific
 // ----------------------------------------------------------------------------
 
-void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
+void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
+                             wxMenu * topMenu, size_t index)
 {
     m_menuBar = menuBar;
     m_topMenu = topMenu;
 
-    if (GetId() == -2)
+    if (GetId() == -3)
     {
-        // Id=-2 identifies a Title item.
+        // Id=-3 identifies a Title item.
         m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
             (wxStripMenuCodes(m_text),
             xmLabelGadgetClass, (Widget) menu, NULL);
     }
-    else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
+    else if (!IsSeparator() && !m_subMenu)
     {
-        wxString strName = wxStripMenuCodes(m_text);
+        wxString txt = m_text;
+
+        if (m_text.IsEmpty())
+        {
+            wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
+            txt = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC);
+        }
+
+        wxString strName = wxStripMenuCodes(txt);
         if (IsCheckable())
         {
             m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
                 xmToggleButtonGadgetClass, (Widget) menu,
+#ifdef XmNpositionIndex
+                XmNpositionIndex, index,
+#endif
                 NULL);
             XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
         }
         else
             m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
             xmPushButtonGadgetClass, (Widget) menu,
+#ifdef XmNpositionIndex
+            XmNpositionIndex, index,
+#endif
             NULL);
         char mnem = wxFindMnemonic (m_text);
         if (mnem != 0)
@@ -242,14 +239,18 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen
             (XtCallbackProc) wxMenuItemDisarmCallback,
             (XtPointer) this);
     }
-    else if (GetId() == -1)
+    else if (IsSeparator())
     {
         m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
-            xmSeparatorGadgetClass, (Widget) menu, NULL);
+            xmSeparatorGadgetClass, (Widget) menu,
+#ifndef XmNpositionIndex
+            XmNpositionIndex, index,
+#endif
+            NULL);
     }
     else if (m_subMenu)
     {
-        m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
+        m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
         m_subMenu->SetButtonWidget(m_buttonWidget);
         XtAddCallback ((Widget) m_buttonWidget,
             XmNcascadingCallback,
@@ -262,12 +263,12 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen
 
 void wxMenuItem::DestroyItem(bool full)
 {
-    if (GetId() == -2)
+    if (GetId() == -3)
     {
         ;      // Nothing
 
     }
-    else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
+    else if (!m_text.empty() && !m_subMenu)
     {
         if (m_buttonWidget)
         {
@@ -283,7 +284,7 @@ void wxMenuItem::DestroyItem(bool full)
                 wxMenuItemDisarmCallback, (XtPointer) this);
         }
     }
-    else if (GetId() == -1)
+    else if (GetId() == wxID_SEPARATOR)
     {
         ;      // Nothing
 
@@ -345,29 +346,42 @@ void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
     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);
+            Boolean isChecked = false;
+            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();
         }
     }
 }
@@ -383,7 +397,8 @@ void wxMenuItemArmCallback (Widget WXUNUSED(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);
         }
     }
 }
@@ -402,8 +417,8 @@ wxMenuItemDisarmCallback (Widget WXUNUSED(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);
         }
     }
 }
-