]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/combobox_native.cpp
Added support for saving greyscale TIFF images.
[wxWidgets.git] / src / motif / combobox_native.cpp
index 9c11990fe3f4d6c65196c357c1dad3ef3601ef9a..9fbb5241a209021156ba7b5260d695cf70c5d016 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        combobox_native.cpp
+// Name:        src/motif/combobox_native.cpp
 // Purpose:     wxComboBox class
 // Author:      Julian Smart, Ian Brown
 // Modified by:
@@ -9,12 +9,17 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#include "wx/setup.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
 
 #if wxUSE_COMBOBOX
 
 #include "wx/combobox.h"
 
+#ifndef WX_PRECOMP
+    #include "wx/arrstr.h"
+#endif
+
 #ifdef __VMS__
 #pragma message disable nosimpint
 #endif
@@ -75,6 +80,7 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
 {
     if( !CreateControl( parent, id, pos, size, style, validator, name ) )
         return false;
+    PreCreation();
 
     Widget parentWidget = (Widget) parent->GetClientWidget();
 
@@ -83,10 +89,12 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
                   ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
     // default to wxCB_DROPDOWN
                                               XmDROP_DOWN_COMBO_BOX;
+    if( cb_type == XmDROP_DOWN_COMBO_BOX )
+        SetWindowStyle( style | wxCB_DROPDOWN );
 
     Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
         xmComboBoxWidgetClass, parentWidget,
-       XmNcomboBoxType, cb_type,
+        XmNcomboBoxType, cb_type,
         NULL);
 
     m_mainWidget = (Widget) buttonWidget;
@@ -99,8 +107,6 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
 
     SetValue(value);
 
-    ChangeFont(false);
-
     XtAddCallback (buttonWidget, XmNselectionCallback,
                    (XtCallbackProc) wxComboBoxCallback,
                    (XtPointer) this);
@@ -108,17 +114,46 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
                    (XtCallbackProc) wxComboBoxCallback,
                    (XtPointer) this);
 
-    SetCanAddEventHandler(true);
+    wxSize best = GetBestSize();
+    if( size.x != wxDefaultCoord ) best.x = size.x;
+    if( size.y != wxDefaultCoord ) best.y = size.y;
+
+    PostCreation();
     AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
-                  pos.x, pos.y, size.x, size.y);
+                  pos.x, pos.y, best.x, best.y);
 
-    XtVaSetValues (GetXmList(this),
-                   XmNvisibleItemCount, 10,
-                   NULL);
+    return true;
+}
+
+bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
+                        const wxString& value,
+                        const wxPoint& pos,
+                        const wxSize& size,
+                        const wxArrayString& choices,
+                        long style,
+                        const wxValidator& validator,
+                        const wxString& name)
+{
+    wxCArrayString chs(choices);
+    return Create(parent, id, value, pos, size, chs.GetCount(),
+                  chs.GetStrings(), style, validator, name);
+}
 
-    ChangeBackgroundColour();
+void wxComboBox::AdjustDropDownListSize()
+{
+    int newListCount = -1, itemCount = GetCount();
+    const int MAX = 12;
 
-    return true;
+    if( !itemCount )
+        newListCount = 1;
+    else if( itemCount < MAX )
+        newListCount = itemCount;
+    else
+        newListCount = MAX;
+
+    XtVaSetValues( GetXmList(this),
+                   XmNvisibleItemCount, newListCount,
+                   NULL );
 }
 
 wxComboBox::~wxComboBox()
@@ -126,26 +161,15 @@ wxComboBox::~wxComboBox()
     DetachWidget((Widget) m_mainWidget); // Removes event handlers
     XtDestroyWidget((Widget) m_mainWidget);
     m_mainWidget = (WXWidget) 0;
-    if ( HasClientObjectData() )
-        m_clientDataDict.DestroyData();
 }
 
-void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
+void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
 {
     // Necessary so it doesn't call wxChoice::SetSize
     wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
 }
 
-wxString wxComboBox::GetValue() const
-{
-    char* s = XmTextGetString (GetXmText (this));
-    wxString str(s);
-    if (s)
-        XtFree (s);
-    return str;
-}
-
-void wxComboBox::SetString(int n, const wxString& s)
+void wxComboBox::SetString(unsigned int n, const wxString& s)
 {
     wxXmString text(s);
     Widget listBox = GetXmList(this);
@@ -161,23 +185,34 @@ void wxComboBox::SetValue(const wxString& value)
     m_inSetValue = true;
 
     XtVaSetValues( GetXmText(this),
-                   XmNvalue, (char *)value.c_str(),
+                   XmNvalue, (const char*)value.mb_str(),
                    NULL);
 
     m_inSetValue = false;
 }
 
-int wxComboBox::DoAppend(const wxString& item)
+int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
+                              unsigned int pos,
+                              void **clientData, wxClientDataType type)
 {
-    wxXmString str( item.c_str() );
-    XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
-    m_stringList.Add(item);
-    m_noStrings ++;
+    const unsigned int numItems = items.GetCount();
 
-    return GetCount() - 1;
+    AllocClientData(numItems);
+    for ( unsigned int i = 0; i < numItems; ++i, ++pos )
+    {
+        wxXmString str( items[i].c_str() );
+        XmComboBoxAddItem((Widget) m_mainWidget, str(),
+                          GetMotifPosition(pos), False);
+
+        InsertNewItemClientData(pos, clientData, i, type);
+    }
+
+    AdjustDropDownListSize();
+
+    return pos - 1;
 }
 
-void wxComboBox::Delete(int n)
+void wxComboBox::DoDeleteOneItem(unsigned int n)
 {
 #ifdef LESSTIF_VERSION
     XmListDeletePos (GetXmList(this), n + 1);
@@ -185,14 +220,10 @@ void wxComboBox::Delete(int n)
     XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
 #endif
 
-    wxStringList::Node *node = m_stringList.Item(n);
-    if (node)
-    {
-        delete[] node->GetData();
-        delete node;
-    }
-    m_clientDataDict.Delete(n, HasClientObjectData());
-    m_noStrings--;
+    wxControlWithItems::DoDeleteOneItem(n);
+    m_stringArray.RemoveAt(size_t(n));
+
+    AdjustDropDownListSize();
 }
 
 void wxComboBox::Clear()
@@ -200,116 +231,54 @@ void wxComboBox::Clear()
 #ifdef LESSTIF_VERSION
     XmListDeleteAllItems (GetXmList(this));
 #else
-    while(m_noStrings > 0)
+    size_t n = m_stringArray.GetCount();
+    while(n > 0)
     {
-       XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
+        XmComboBoxDeletePos((Widget) m_mainWidget, n--);
     }
- #endif
+#endif
 
-    m_stringList.Clear();
+    m_stringArray.Clear();
+    AdjustDropDownListSize();
 
-    if ( HasClientObjectData() )
-        m_clientDataDict.DestroyData();
-    m_noStrings = 0;
+    wxTextEntry::Clear();
 }
 
 void wxComboBox::SetSelection (int n)
 {
-#ifdef LESSTIF_VERSION
+    m_inSetSelection = true;
+
+#if wxCHECK_LESSTIF()
     XmListSelectPos (GetXmList(this), n + 1, false);
     SetValue(GetString(n));
 #else
-    wxXmString str( GetString(n).c_str() );
-    XmComboBoxSelectItem((Widget) m_mainWidget, str());
 #if 0
-    // does it work for Motif
+    wxXmString str(GetString(n).c_str());
+    XmComboBoxSelectItem((Widget) m_mainWidget, str());
+#endif
     XtVaSetValues( (Widget)m_mainWidget,
-                   XmNselectedPosition, n + 1,
+                   XmNselectedPosition, n,
                    NULL );
 #endif
-#endif
-}
 
-int wxComboBox::GetSelection (void) const
-{
-    return wxDoGetSelectionInList( GetXmList( this ) );
+    m_inSetSelection = false;
 }
 
-wxString wxComboBox::GetString(int n) const
+int wxComboBox::GetSelection() const
 {
-    wxStringList::Node *node = m_stringList.Item(n);
-    if (node)
-        return wxString(node->GetData ());
-    else
-        return wxEmptyString;
-}
-
-int wxComboBox::FindString(const wxString& s) const
-{
-    return wxDoFindStringInList( GetXmList( this ), s );
-}
-
-// Clipboard operations
-void wxComboBox::Copy()
-{
-    XmTextCopy( GetXmText(this), CurrentTime );
-}
-
-void wxComboBox::Cut()
-{
-    XmTextCut( GetXmText(this), CurrentTime );
-}
-
-void wxComboBox::Paste()
-{
-    XmTextPaste( GetXmText(this) );
-}
-
-void wxComboBox::SetEditable(bool WXUNUSED(editable))
-{
-    // TODO
-}
-
-void wxComboBox::SetInsertionPoint(long pos)
-{
-    XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
-}
-
-void wxComboBox::SetInsertionPointEnd()
-{
-    SetInsertionPoint( GetLastPosition() );
-}
-
-long wxComboBox::GetInsertionPoint() const
-{
-    return (long)XmTextGetInsertionPosition( GetXmText(this) );
-}
-
-long wxComboBox::GetLastPosition() const
-{
-    XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
-    return (long)pos;
-}
-
-void wxComboBox::Replace(long from, long to, const wxString& value)
-{
-    XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
-                   (char*)value.c_str() );
+    return wxDoGetSelectionInList( GetXmList( this ) );
 }
 
-void wxComboBox::Remove(long from, long to)
+wxString wxComboBox::GetString(unsigned int n) const
 {
-    SetSelection( from, to );
-    XmTextRemove( GetXmText(this) );
+    return wxDoGetStringInList( GetXmList(this), n );
 }
 
-void wxComboBox::SetSelection(long from, long to)
+int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
 {
-    if( to == -1 )
-        to = GetLastPosition();
+    // FIXME: back to base class for not supported value of bCase
 
-    XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
-                        (XmTextPosition)to, (Time)0 );
+    return wxDoFindStringInList( GetXmList( this ), s );
 }
 
 void  wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
@@ -317,6 +286,8 @@ void  wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
 {
     wxComboBox *item = (wxComboBox *) clientData;
 
+    if( item->m_inSetSelection ) return;
+
     switch (cbs->reason)
     {
     case XmCR_SELECT:
@@ -327,26 +298,26 @@ void  wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
         {
             wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
                                   item->GetId());
-            int idx = cbs->item_position - 1;
-            event.m_commandInt = idx;
-            event.m_commandString = item->GetString (idx);
+            int idx = cbs->item_position;
+            event.SetInt(idx);
+            event.SetString( item->GetString (idx) );
             if ( item->HasClientObjectData() )
                 event.SetClientObject( item->GetClientObject(idx) );
             else if ( item->HasClientUntypedData() )
                 event.SetClientData( item->GetClientData(idx) );
-            event.m_extraLong = true;
+            event.SetExtraLong(true);
             event.SetEventObject(item);
-            item->ProcessCommand (event);
+            item->HandleWindowEvent(event);
             break;
         }
     case XmCR_VALUE_CHANGED:
         {
             wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
-            event.m_commandInt = -1;
-            event.m_commandString = item->GetValue();
-            event.m_extraLong = true;
+            event.SetInt(-1);
+            event.SetString( item->GetValue() );
+            event.SetExtraLong(true);
             event.SetEventObject(item);
-            item->ProcessCommand (event);
+            item->HandleWindowEvent(event);
             break;
         }
     default:
@@ -356,6 +327,12 @@ void  wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
 
 void wxComboBox::ChangeFont(bool keepOriginalSize)
 {
+    if( m_font.IsOk() && m_mainWidget != NULL )
+    {
+        wxDoChangeFont( GetXmText(this), m_font );
+        wxDoChangeFont( GetXmList(this), m_font );
+    }
+
     // Don't use the base class wxChoice's ChangeFont
     wxWindow::ChangeFont(keepOriginalSize);
 }
@@ -372,7 +349,36 @@ void wxComboBox::ChangeForegroundColour()
 
 wxSize wxComboBox::DoGetBestSize() const
 {
-   return wxWindow::DoGetBestSize();
+    if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
+        (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
+    {
+        Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
+
+        XtVaGetValues( (Widget)m_mainWidget,
+                       XmNarrowSize, &arrowW,
+                       XmNarrowSpacing, &arrowS,
+                       XmNhighlightThickness, &highlight,
+                       XmNmarginWidth, &xmargin,
+                       XmNmarginHeight, &ymargin,
+                       XmNshadowThickness, &shadow,
+                       NULL );
+
+        wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
+        wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
+                                                         this );
+
+        // FIXME arbitrary constants
+        return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
+                                  + 2 * shadow + 2 * xmargin ,
+                       textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
+    }
+    else
+        return wxWindow::DoGetBestSize();
+}
+
+WXWidget wxComboBox::GetTextWidget() const
+{
+    return (WXWidget)GetXmText(this);
 }
 
 #endif // XmVersion >= 2000