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"
19 #pragma message disable nosimpint
23 #pragma message enable nosimpint
26 // use the new, shiny combobox for Motif 2.x
27 #if (XmVersion >= 2000)
29 #include <Xm/ComboBox.h>
33 #include "wx/motif/private.h"
36 static Widget
GetXmList( const wxComboBox
* cb
)
39 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
46 static Widget
GetXmText( const wxComboBox
* cb
)
49 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
56 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
57 XmComboBoxCallbackStruct
* cbs
);
59 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
61 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
62 const wxString
& value
,
65 int n
, const wxString choices
[],
67 const wxValidator
& validator
,
70 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
73 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
75 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
76 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
77 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
78 // default to wxCB_DROPDOWN
79 XmDROP_DOWN_COMBO_BOX
;
81 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
82 xmComboBoxWidgetClass
, parentWidget
,
83 XmNcomboBoxType
, cb_type
,
86 m_mainWidget
= (Widget
) buttonWidget
;
89 for ( i
= 0; i
< n
; ++i
)
92 XtManageChild (buttonWidget
);
98 XtAddCallback (buttonWidget
, XmNselectionCallback
,
99 (XtCallbackProc
) wxComboBoxCallback
,
101 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
102 (XtCallbackProc
) wxComboBoxCallback
,
105 SetCanAddEventHandler(true);
106 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
107 pos
.x
, pos
.y
, size
.x
, size
.y
);
109 XtVaSetValues (GetXmList(this),
110 XmNvisibleItemCount
, 10,
113 ChangeBackgroundColour();
118 wxComboBox::~wxComboBox()
120 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
121 XtDestroyWidget((Widget
) m_mainWidget
);
122 m_mainWidget
= (WXWidget
) 0;
123 if ( HasClientObjectData() )
124 m_clientDataDict
.DestroyData();
127 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
129 // Necessary so it doesn't call wxChoice::SetSize
130 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
133 wxString
wxComboBox::GetValue() const
135 char* s
= XmTextGetString (GetXmText (this));
142 void wxComboBox::SetString(int n
, const wxString
& s
)
145 Widget listBox
= GetXmList(this);
147 // delete the item and add it again.
148 // FIXME isn't there a way to change it in place?
149 XmListDeletePos (listBox
, n
+1);
150 XmListAddItem (listBox
, text(), n
+1);
153 void wxComboBox::SetValue(const wxString
& value
)
157 XtVaSetValues( GetXmText(this),
158 XmNvalue
, (char *)value
.c_str(),
161 m_inSetValue
= false;
164 int wxComboBox::DoAppend(const wxString
& item
)
166 wxXmString
str( item
.c_str() );
167 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
168 m_stringList
.Add(item
);
171 return GetCount() - 1;
174 void wxComboBox::Delete(int n
)
176 #ifdef LESSTIF_VERSION
177 XmListDeletePos (GetXmList(this), n
+ 1);
179 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
182 wxStringList::Node
*node
= m_stringList
.Item(n
);
185 delete[] node
->GetData();
188 m_clientDataDict
.Delete(n
, HasClientObjectData());
192 void wxComboBox::Clear()
194 #ifdef LESSTIF_VERSION
195 XmListDeleteAllItems (GetXmList(this));
197 while(m_noStrings
> 0)
199 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
203 m_stringList
.Clear();
205 if ( HasClientObjectData() )
206 m_clientDataDict
.DestroyData();
210 void wxComboBox::SetSelection (int n
)
212 #ifdef LESSTIF_VERSION
213 XmListSelectPos (GetXmList(this), n
+ 1, false);
214 SetValue(GetString(n
));
216 wxXmString
str( GetString(n
).c_str() );
217 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
219 // does it work for Motif
220 XtVaSetValues( (Widget
)m_mainWidget
,
221 XmNselectedPosition
, n
+ 1,
227 int wxComboBox::GetSelection (void) const
229 return wxDoGetSelectionInList( GetXmList( this ) );
232 wxString
wxComboBox::GetString(int n
) const
234 wxStringList::Node
*node
= m_stringList
.Item(n
);
236 return wxString(node
->GetData ());
238 return wxEmptyString
;
241 int wxComboBox::FindString(const wxString
& s
) const
243 return wxDoFindStringInList( GetXmList( this ), s
);
246 // Clipboard operations
247 void wxComboBox::Copy()
249 // XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
252 void wxComboBox::Cut()
254 // XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
257 void wxComboBox::Paste()
259 // XmComboBoxPaste((Widget) m_mainWidget);
262 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
267 void wxComboBox::SetInsertionPoint(long pos
)
269 // XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
272 void wxComboBox::SetInsertionPointEnd()
274 // XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
275 // XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
278 long wxComboBox::GetInsertionPoint() const
280 // return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
284 long wxComboBox::GetLastPosition() const
286 // return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
290 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
292 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
293 (char*) (const char*) value);
297 void wxComboBox::Remove(long from
, long to
)
300 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
302 XmComboBoxRemove ((Widget) m_mainWidget);
306 void wxComboBox::SetSelection(long from
, long to
)
309 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
314 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
315 XmComboBoxCallbackStruct
* cbs
)
317 wxComboBox
*item
= (wxComboBox
*) clientData
;
323 case XmCR_SINGLE_SELECT
:
324 case XmCR_BROWSE_SELECT
:
327 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
329 int idx
= cbs
->item_position
- 1;
330 event
.m_commandInt
= idx
;
331 event
.m_commandString
= item
->GetString (idx
);
332 if ( item
->HasClientObjectData() )
333 event
.SetClientObject( item
->GetClientObject(idx
) );
334 else if ( item
->HasClientUntypedData() )
335 event
.SetClientData( item
->GetClientData(idx
) );
336 event
.m_extraLong
= true;
337 event
.SetEventObject(item
);
338 item
->ProcessCommand (event
);
341 case XmCR_VALUE_CHANGED
:
343 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
344 event
.m_commandInt
= -1;
345 event
.m_commandString
= item
->GetValue();
346 event
.m_extraLong
= true;
347 event
.SetEventObject(item
);
348 item
->ProcessCommand (event
);
356 void wxComboBox::ChangeFont(bool keepOriginalSize
)
358 // Don't use the base class wxChoice's ChangeFont
359 wxWindow::ChangeFont(keepOriginalSize
);
362 void wxComboBox::ChangeBackgroundColour()
364 wxWindow::ChangeBackgroundColour();
367 void wxComboBox::ChangeForegroundColour()
369 wxWindow::ChangeForegroundColour();
372 wxSize
wxComboBox::DoGetBestSize() const
374 return wxWindow::DoGetBestSize();
377 #endif // XmVersion >= 2000
379 #endif // wxUSE_COMBOBOX