X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9b1bd0c6e7ed45535fabc574f69b7670fc28191e..9bb50fd0b04c824fc7ba14c0dc4ba4a2a9a1e6ad:/src/motif/combobox_native.cpp diff --git a/src/motif/combobox_native.cpp b/src/motif/combobox_native.cpp index 499e84cadd..d7f6e74751 100644 --- a/src/motif/combobox_native.cpp +++ b/src/motif/combobox_native.cpp @@ -9,11 +9,15 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + #include "wx/setup.h" #if wxUSE_COMBOBOX #include "wx/combobox.h" +#include "wx/arrstr.h" #ifdef __VMS__ #pragma message disable nosimpint @@ -26,9 +30,15 @@ // use the new, shiny combobox for Motif 2.x #if (XmVersion >= 2000) +#ifdef __VMS__ +#pragma message disable nosimpint +#endif #include #include #include +#ifdef __VMS__ +#pragma message enable nosimpint +#endif #include "wx/motif/private.h" @@ -77,10 +87,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; @@ -102,19 +114,49 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id, (XtCallbackProc) wxComboBoxCallback, (XtPointer) this); - SetCanAddEventHandler(true); - AttachWidget (parent, m_mainWidget, (WXWidget) NULL, - pos.x, pos.y, size.x, size.y); + wxSize best = GetBestSize(); + if( size.x != -1 ) best.x = size.x; + if( size.y != -1 ) best.y = size.y; - XtVaSetValues (GetXmList(this), - XmNvisibleItemCount, 10, - NULL); + AttachWidget (parent, m_mainWidget, (WXWidget) NULL, + pos.x, pos.y, best.x, best.y); ChangeBackgroundColour(); 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); +} + +void wxComboBox::AdjustDropDownListSize() +{ + int newListCount = -1, itemCount = GetCount(); + const int MAX = 12; + + if( !itemCount ) + newListCount = 1; + else if( itemCount < MAX ) + newListCount = itemCount; + else + newListCount = MAX; + + XtVaSetValues( GetXmList(this), + XmNvisibleItemCount, newListCount, + NULL ); +} + wxComboBox::~wxComboBox() { DetachWidget((Widget) m_mainWidget); // Removes event handlers @@ -154,8 +196,10 @@ void wxComboBox::SetValue(const wxString& value) { m_inSetValue = true; + // Fix crash; probably an OpenMotif bug + const char* val = value.c_str() ? value.c_str() : ""; XtVaSetValues( GetXmText(this), - XmNvalue, (char *)value.c_str(), + XmNvalue, wxConstCast(val, char), NULL); m_inSetValue = false; @@ -165,8 +209,24 @@ int wxComboBox::DoAppend(const wxString& item) { wxXmString str( item.c_str() ); XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False); - m_stringList.Add(item); m_noStrings ++; + AdjustDropDownListSize(); + + return GetCount() - 1; +} + +int wxComboBox::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + wxXmString str( item.c_str() ); + XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False); + m_noStrings ++; + AdjustDropDownListSize(); return GetCount() - 1; } @@ -179,14 +239,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--; + + AdjustDropDownListSize(); } void wxComboBox::Clear() @@ -196,32 +252,34 @@ void wxComboBox::Clear() #else while(m_noStrings > 0) { - XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--); + XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--); } - #endif - - m_stringList.Clear(); +#endif if ( HasClientObjectData() ) m_clientDataDict.DestroyData(); m_noStrings = 0; + AdjustDropDownListSize(); } void wxComboBox::SetSelection (int n) { -#ifdef LESSTIF_VERSION + m_inSetSelection = true; + +#if wxCHECK_LESSTIF() XmListSelectPos (GetXmList(this), n + 1, false); SetValue(GetString(n)); #else +#if 0 wxXmString str( GetString(n).c_str() ); XmComboBoxSelectItem((Widget) m_mainWidget, str()); -#if 0 - // does it work for Motif +#endif XtVaSetValues( (Widget)m_mainWidget, - XmNselectedPosition, n + 1, + XmNselectedPosition, n, NULL ); #endif -#endif + + m_inSetSelection = false; } int wxComboBox::GetSelection (void) const @@ -231,11 +289,7 @@ int wxComboBox::GetSelection (void) const wxString wxComboBox::GetString(int n) const { - wxStringList::Node *node = m_stringList.Item(n); - if (node) - return wxString(node->GetData ()); - else - return wxEmptyString; + return wxDoGetStringInList( GetXmList(this), n ); } int wxComboBox::FindString(const wxString& s) const @@ -246,17 +300,17 @@ int wxComboBox::FindString(const wxString& s) const // Clipboard operations void wxComboBox::Copy() { -// XmComboBoxCopy((Widget) m_mainWidget, CurrentTime); + XmTextCopy( GetXmText(this), CurrentTime ); } void wxComboBox::Cut() { -// XmComboBoxCut((Widget) m_mainWidget, CurrentTime); + XmTextCut( GetXmText(this), CurrentTime ); } void wxComboBox::Paste() { -// XmComboBoxPaste((Widget) m_mainWidget); + XmTextPaste( GetXmText(this) ); } void wxComboBox::SetEditable(bool WXUNUSED(editable)) @@ -266,49 +320,44 @@ void wxComboBox::SetEditable(bool WXUNUSED(editable)) void wxComboBox::SetInsertionPoint(long pos) { -// XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); + XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos ); } void wxComboBox::SetInsertionPointEnd() { -// XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget); -// XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1)); + SetInsertionPoint( GetLastPosition() ); } long wxComboBox::GetInsertionPoint() const { -// return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget); - return -1; + return (long)XmTextGetInsertionPosition( GetXmText(this) ); } -long wxComboBox::GetLastPosition() const +wxTextPos wxComboBox::GetLastPosition() const { -// return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget); - return -1; + XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) ); + return (long)pos; } void wxComboBox::Replace(long from, long to, const wxString& value) -{/* - XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, - (char*) (const char*) value); -*/ +{ + XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to, + wxConstCast(value.c_str(), char) ); } void wxComboBox::Remove(long from, long to) { -/* - XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, - (Time) 0); - XmComboBoxRemove ((Widget) m_mainWidget); - */ + SetSelection( from, to ); + XmTextRemove( GetXmText(this) ); } void wxComboBox::SetSelection(long from, long to) { -/* - XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, - (Time) 0); - */ + if( to == -1 ) + to = GetLastPosition(); + + XmTextSetSelection( GetXmText(this), (XmTextPosition)from, + (XmTextPosition)to, (Time)0 ); } void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, @@ -316,6 +365,8 @@ void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, { wxComboBox *item = (wxComboBox *) clientData; + if( item->m_inSetSelection ) return; + switch (cbs->reason) { case XmCR_SELECT: @@ -326,26 +377,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->GetEventHandler()->ProcessEvent(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->GetEventHandler()->ProcessEvent(event); break; } default: @@ -355,6 +406,12 @@ void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, void wxComboBox::ChangeFont(bool keepOriginalSize) { + if( m_font.Ok() ) + { + wxDoChangeFont( GetXmText(this), m_font ); + wxDoChangeFont( GetXmList(this), m_font ); + } + // Don't use the base class wxChoice's ChangeFont wxWindow::ChangeFont(keepOriginalSize); } @@ -371,7 +428,31 @@ 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(); } #endif // XmVersion >= 2000