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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/combobox.h"
20 #include "wx/arrstr.h"
23 #pragma message disable nosimpint
27 #pragma message enable nosimpint
30 // use the new, shiny combobox for Motif 2.x
31 #if (XmVersion >= 2000)
34 #pragma message disable nosimpint
36 #include <Xm/ComboBox.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
46 static Widget
GetXmList( const wxComboBox
* cb
)
49 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
56 static Widget
GetXmText( const wxComboBox
* cb
)
59 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
66 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
67 XmComboBoxCallbackStruct
* cbs
);
69 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
71 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxString
& value
,
75 int n
, const wxString choices
[],
77 const wxValidator
& validator
,
80 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
83 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
85 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
86 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
87 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
88 // default to wxCB_DROPDOWN
89 XmDROP_DOWN_COMBO_BOX
;
90 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
91 SetWindowStyle( style
| wxCB_DROPDOWN
);
93 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
94 xmComboBoxWidgetClass
, parentWidget
,
95 XmNcomboBoxType
, cb_type
,
98 m_mainWidget
= (Widget
) buttonWidget
;
101 for ( i
= 0; i
< n
; ++i
)
102 Append( choices
[i
] );
104 XtManageChild (buttonWidget
);
110 XtAddCallback (buttonWidget
, XmNselectionCallback
,
111 (XtCallbackProc
) wxComboBoxCallback
,
113 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
114 (XtCallbackProc
) wxComboBoxCallback
,
117 wxSize best
= GetBestSize();
118 if( size
.x
!= -1 ) best
.x
= size
.x
;
119 if( size
.y
!= -1 ) best
.y
= size
.y
;
121 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
122 pos
.x
, pos
.y
, best
.x
, best
.y
);
124 ChangeBackgroundColour();
129 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
130 const wxString
& value
,
133 const wxArrayString
& choices
,
135 const wxValidator
& validator
,
136 const wxString
& name
)
138 wxCArrayString
chs(choices
);
139 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
140 chs
.GetStrings(), style
, validator
, name
);
143 void wxComboBox::AdjustDropDownListSize()
145 int newListCount
= -1, itemCount
= GetCount();
150 else if( itemCount
< MAX
)
151 newListCount
= itemCount
;
155 XtVaSetValues( GetXmList(this),
156 XmNvisibleItemCount
, newListCount
,
160 wxComboBox::~wxComboBox()
162 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
163 XtDestroyWidget((Widget
) m_mainWidget
);
164 m_mainWidget
= (WXWidget
) 0;
165 if ( HasClientObjectData() )
166 m_clientDataDict
.DestroyData();
169 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
171 // Necessary so it doesn't call wxChoice::SetSize
172 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
175 wxString
wxComboBox::GetValue() const
177 char* s
= XmTextGetString (GetXmText (this));
184 void wxComboBox::SetString(int n
, const wxString
& s
)
187 Widget listBox
= GetXmList(this);
189 // delete the item and add it again.
190 // FIXME isn't there a way to change it in place?
191 XmListDeletePos (listBox
, n
+1);
192 XmListAddItem (listBox
, text(), n
+1);
195 void wxComboBox::SetValue(const wxString
& value
)
199 // Fix crash; probably an OpenMotif bug
200 const char* val
= value
.c_str() ? value
.c_str() : "";
201 XtVaSetValues( GetXmText(this),
202 XmNvalue
, wxConstCast(val
, char),
205 m_inSetValue
= false;
208 int wxComboBox::DoAppend(const wxString
& item
)
210 wxXmString
str( item
.c_str() );
211 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
213 AdjustDropDownListSize();
215 return GetCount() - 1;
218 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
220 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
221 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
223 if (pos
== GetCount())
224 return DoAppend(item
);
226 wxXmString
str( item
.c_str() );
227 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
229 AdjustDropDownListSize();
231 return GetCount() - 1;
234 void wxComboBox::Delete(int n
)
236 #ifdef LESSTIF_VERSION
237 XmListDeletePos (GetXmList(this), n
+ 1);
239 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
242 m_clientDataDict
.Delete(n
, HasClientObjectData());
245 AdjustDropDownListSize();
248 void wxComboBox::Clear()
250 #ifdef LESSTIF_VERSION
251 XmListDeleteAllItems (GetXmList(this));
253 while(m_noStrings
> 0)
255 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
259 if ( HasClientObjectData() )
260 m_clientDataDict
.DestroyData();
262 AdjustDropDownListSize();
265 void wxComboBox::SetSelection (int n
)
267 m_inSetSelection
= true;
269 #if wxCHECK_LESSTIF()
270 XmListSelectPos (GetXmList(this), n
+ 1, false);
271 SetValue(GetString(n
));
274 wxXmString
str( GetString(n
).c_str() );
275 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
277 XtVaSetValues( (Widget
)m_mainWidget
,
278 XmNselectedPosition
, n
,
282 m_inSetSelection
= false;
285 int wxComboBox::GetSelection (void) const
287 return wxDoGetSelectionInList( GetXmList( this ) );
290 wxString
wxComboBox::GetString(int n
) const
292 return wxDoGetStringInList( GetXmList(this), n
);
295 int wxComboBox::FindString(const wxString
& s
) const
297 return wxDoFindStringInList( GetXmList( this ), s
);
300 // Clipboard operations
301 void wxComboBox::Copy()
303 XmTextCopy( GetXmText(this), CurrentTime
);
306 void wxComboBox::Cut()
308 XmTextCut( GetXmText(this), CurrentTime
);
311 void wxComboBox::Paste()
313 XmTextPaste( GetXmText(this) );
316 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
321 void wxComboBox::SetInsertionPoint(long pos
)
323 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
326 void wxComboBox::SetInsertionPointEnd()
328 SetInsertionPoint( GetLastPosition() );
331 long wxComboBox::GetInsertionPoint() const
333 return (long)XmTextGetInsertionPosition( GetXmText(this) );
336 long wxComboBox::GetLastPosition() const
338 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
342 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
344 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
345 wxConstCast(value
.c_str(), char) );
348 void wxComboBox::Remove(long from
, long to
)
350 SetSelection( from
, to
);
351 XmTextRemove( GetXmText(this) );
354 void wxComboBox::SetSelection(long from
, long to
)
357 to
= GetLastPosition();
359 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
360 (XmTextPosition
)to
, (Time
)0 );
363 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
364 XmComboBoxCallbackStruct
* cbs
)
366 wxComboBox
*item
= (wxComboBox
*) clientData
;
368 if( item
->m_inSetSelection
) return;
374 case XmCR_SINGLE_SELECT
:
375 case XmCR_BROWSE_SELECT
:
378 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
380 int idx
= cbs
->item_position
;
381 event
.m_commandInt
= idx
;
382 event
.m_commandString
= item
->GetString (idx
);
383 if ( item
->HasClientObjectData() )
384 event
.SetClientObject( item
->GetClientObject(idx
) );
385 else if ( item
->HasClientUntypedData() )
386 event
.SetClientData( item
->GetClientData(idx
) );
387 event
.m_extraLong
= true;
388 event
.SetEventObject(item
);
389 item
->GetEventHandler()->ProcessEvent(event
);
392 case XmCR_VALUE_CHANGED
:
394 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
395 event
.m_commandInt
= -1;
396 event
.m_commandString
= item
->GetValue();
397 event
.m_extraLong
= true;
398 event
.SetEventObject(item
);
399 item
->GetEventHandler()->ProcessEvent(event
);
407 void wxComboBox::ChangeFont(bool keepOriginalSize
)
411 wxDoChangeFont( GetXmText(this), m_font
);
412 wxDoChangeFont( GetXmList(this), m_font
);
415 // Don't use the base class wxChoice's ChangeFont
416 wxWindow::ChangeFont(keepOriginalSize
);
419 void wxComboBox::ChangeBackgroundColour()
421 wxWindow::ChangeBackgroundColour();
424 void wxComboBox::ChangeForegroundColour()
426 wxWindow::ChangeForegroundColour();
429 wxSize
wxComboBox::DoGetBestSize() const
431 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
432 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
434 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
436 XtVaGetValues( (Widget
)m_mainWidget
,
437 XmNarrowSize
, &arrowW
,
438 XmNarrowSpacing
, &arrowS
,
439 XmNhighlightThickness
, &highlight
,
440 XmNmarginWidth
, &xmargin
,
441 XmNmarginHeight
, &ymargin
,
442 XmNshadowThickness
, &shadow
,
445 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
446 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
449 // FIXME arbitrary constants
450 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
451 + 2 * shadow
+ 2 * xmargin
,
452 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
455 return wxWindow::DoGetBestSize();
458 #endif // XmVersion >= 2000
460 #endif // wxUSE_COMBOBOX