1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox_native.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart, Ian Brown
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/combobox.h"
17 #include "wx/arrstr.h"
20 #pragma message disable nosimpint
24 #pragma message enable nosimpint
27 // use the new, shiny combobox for Motif 2.x
28 #if (XmVersion >= 2000)
31 #pragma message disable nosimpint
33 #include <Xm/ComboBox.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
43 static Widget
GetXmList( const wxComboBox
* cb
)
46 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
53 static Widget
GetXmText( const wxComboBox
* cb
)
56 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
63 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
64 XmComboBoxCallbackStruct
* cbs
);
66 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
68 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
69 const wxString
& value
,
72 int n
, const wxString choices
[],
74 const wxValidator
& validator
,
77 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
80 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
82 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
83 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
84 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
85 // default to wxCB_DROPDOWN
86 XmDROP_DOWN_COMBO_BOX
;
87 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
88 SetWindowStyle( style
| wxCB_DROPDOWN
);
90 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
91 xmComboBoxWidgetClass
, parentWidget
,
92 XmNcomboBoxType
, cb_type
,
95 m_mainWidget
= (Widget
) buttonWidget
;
98 for ( i
= 0; i
< n
; ++i
)
101 XtManageChild (buttonWidget
);
107 XtAddCallback (buttonWidget
, XmNselectionCallback
,
108 (XtCallbackProc
) wxComboBoxCallback
,
110 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
111 (XtCallbackProc
) wxComboBoxCallback
,
114 wxSize best
= GetBestSize();
115 if( size
.x
!= -1 ) best
.x
= size
.x
;
116 if( size
.y
!= -1 ) best
.y
= size
.y
;
118 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
119 pos
.x
, pos
.y
, best
.x
, best
.y
);
121 ChangeBackgroundColour();
126 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
127 const wxString
& value
,
130 const wxArrayString
& choices
,
132 const wxValidator
& validator
,
133 const wxString
& name
)
135 wxCArrayString
chs(choices
);
136 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
137 chs
.GetStrings(), style
, validator
, name
);
140 void wxComboBox::AdjustDropDownListSize()
142 int newListCount
= -1, itemCount
= GetCount();
147 else if( itemCount
< MAX
)
148 newListCount
= itemCount
;
152 XtVaSetValues( GetXmList(this),
153 XmNvisibleItemCount
, newListCount
,
157 wxComboBox::~wxComboBox()
159 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
160 XtDestroyWidget((Widget
) m_mainWidget
);
161 m_mainWidget
= (WXWidget
) 0;
162 if ( HasClientObjectData() )
163 m_clientDataDict
.DestroyData();
166 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
168 // Necessary so it doesn't call wxChoice::SetSize
169 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
172 wxString
wxComboBox::GetValue() const
174 char* s
= XmTextGetString (GetXmText (this));
181 void wxComboBox::SetString(int n
, const wxString
& s
)
184 Widget listBox
= GetXmList(this);
186 // delete the item and add it again.
187 // FIXME isn't there a way to change it in place?
188 XmListDeletePos (listBox
, n
+1);
189 XmListAddItem (listBox
, text(), n
+1);
192 void wxComboBox::SetValue(const wxString
& value
)
196 // Fix crash; probably an OpenMotif bug
197 const char* val
= value
.c_str() ? value
.c_str() : "";
198 XtVaSetValues( GetXmText(this),
199 XmNvalue
, wxConstCast(val
, char),
202 m_inSetValue
= false;
205 int wxComboBox::DoAppend(const wxString
& item
)
207 wxXmString
str( item
.c_str() );
208 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
210 AdjustDropDownListSize();
212 return GetCount() - 1;
215 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
217 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
218 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
220 if (pos
== GetCount())
221 return DoAppend(item
);
223 wxXmString
str( item
.c_str() );
224 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
226 AdjustDropDownListSize();
228 return GetCount() - 1;
231 void wxComboBox::Delete(int n
)
233 #ifdef LESSTIF_VERSION
234 XmListDeletePos (GetXmList(this), n
+ 1);
236 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
239 m_clientDataDict
.Delete(n
, HasClientObjectData());
242 AdjustDropDownListSize();
245 void wxComboBox::Clear()
247 #ifdef LESSTIF_VERSION
248 XmListDeleteAllItems (GetXmList(this));
250 while(m_noStrings
> 0)
252 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
256 if ( HasClientObjectData() )
257 m_clientDataDict
.DestroyData();
259 AdjustDropDownListSize();
262 void wxComboBox::SetSelection (int n
)
264 m_inSetSelection
= true;
266 #if wxCHECK_LESSTIF()
267 XmListSelectPos (GetXmList(this), n
+ 1, false);
268 SetValue(GetString(n
));
271 wxXmString
str( GetString(n
).c_str() );
272 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
274 XtVaSetValues( (Widget
)m_mainWidget
,
275 XmNselectedPosition
, n
,
279 m_inSetSelection
= false;
282 int wxComboBox::GetSelection (void) const
284 return wxDoGetSelectionInList( GetXmList( this ) );
287 wxString
wxComboBox::GetString(int n
) const
289 return wxDoGetStringInList( GetXmList(this), n
);
292 int wxComboBox::FindString(const wxString
& s
) const
294 return wxDoFindStringInList( GetXmList( this ), s
);
297 // Clipboard operations
298 void wxComboBox::Copy()
300 XmTextCopy( GetXmText(this), CurrentTime
);
303 void wxComboBox::Cut()
305 XmTextCut( GetXmText(this), CurrentTime
);
308 void wxComboBox::Paste()
310 XmTextPaste( GetXmText(this) );
313 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
318 void wxComboBox::SetInsertionPoint(long pos
)
320 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
323 void wxComboBox::SetInsertionPointEnd()
325 SetInsertionPoint( GetLastPosition() );
328 long wxComboBox::GetInsertionPoint() const
330 return (long)XmTextGetInsertionPosition( GetXmText(this) );
333 long wxComboBox::GetLastPosition() const
335 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
339 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
341 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
342 wxConstCast(value
.c_str(), char) );
345 void wxComboBox::Remove(long from
, long to
)
347 SetSelection( from
, to
);
348 XmTextRemove( GetXmText(this) );
351 void wxComboBox::SetSelection(long from
, long to
)
354 to
= GetLastPosition();
356 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
357 (XmTextPosition
)to
, (Time
)0 );
360 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
361 XmComboBoxCallbackStruct
* cbs
)
363 wxComboBox
*item
= (wxComboBox
*) clientData
;
365 if( item
->m_inSetSelection
) return;
371 case XmCR_SINGLE_SELECT
:
372 case XmCR_BROWSE_SELECT
:
375 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
377 int idx
= cbs
->item_position
;
378 event
.m_commandInt
= idx
;
379 event
.m_commandString
= item
->GetString (idx
);
380 if ( item
->HasClientObjectData() )
381 event
.SetClientObject( item
->GetClientObject(idx
) );
382 else if ( item
->HasClientUntypedData() )
383 event
.SetClientData( item
->GetClientData(idx
) );
384 event
.m_extraLong
= true;
385 event
.SetEventObject(item
);
386 item
->GetEventHandler()->ProcessEvent(event
);
389 case XmCR_VALUE_CHANGED
:
391 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
392 event
.m_commandInt
= -1;
393 event
.m_commandString
= item
->GetValue();
394 event
.m_extraLong
= true;
395 event
.SetEventObject(item
);
396 item
->GetEventHandler()->ProcessEvent(event
);
404 void wxComboBox::ChangeFont(bool keepOriginalSize
)
408 wxDoChangeFont( GetXmText(this), m_font
);
409 wxDoChangeFont( GetXmList(this), m_font
);
412 // Don't use the base class wxChoice's ChangeFont
413 wxWindow::ChangeFont(keepOriginalSize
);
416 void wxComboBox::ChangeBackgroundColour()
418 wxWindow::ChangeBackgroundColour();
421 void wxComboBox::ChangeForegroundColour()
423 wxWindow::ChangeForegroundColour();
426 wxSize
wxComboBox::DoGetBestSize() const
428 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
429 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
431 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
433 XtVaGetValues( (Widget
)m_mainWidget
,
434 XmNarrowSize
, &arrowW
,
435 XmNarrowSpacing
, &arrowS
,
436 XmNhighlightThickness
, &highlight
,
437 XmNmarginWidth
, &xmargin
,
438 XmNmarginHeight
, &ymargin
,
439 XmNshadowThickness
, &shadow
,
442 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
443 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
446 // FIXME arbitrary constants
447 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
448 + 2 * shadow
+ 2 * xmargin
,
449 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
452 return wxWindow::DoGetBestSize();
455 #endif // XmVersion >= 2000
457 #endif // wxUSE_COMBOBOX