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)
30 #pragma message disable nosimpint
32 #include <Xm/ComboBox.h>
36 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
42 static Widget
GetXmList( const wxComboBox
* cb
)
45 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
52 static Widget
GetXmText( const wxComboBox
* cb
)
55 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
62 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
63 XmComboBoxCallbackStruct
* cbs
);
65 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
67 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
68 const wxString
& value
,
71 int n
, const wxString choices
[],
73 const wxValidator
& validator
,
76 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
79 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
81 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
82 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
83 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
84 // default to wxCB_DROPDOWN
85 XmDROP_DOWN_COMBO_BOX
;
87 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
88 xmComboBoxWidgetClass
, parentWidget
,
89 XmNcomboBoxType
, cb_type
,
92 m_mainWidget
= (Widget
) buttonWidget
;
95 for ( i
= 0; i
< n
; ++i
)
98 XtManageChild (buttonWidget
);
104 XtAddCallback (buttonWidget
, XmNselectionCallback
,
105 (XtCallbackProc
) wxComboBoxCallback
,
107 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
108 (XtCallbackProc
) wxComboBoxCallback
,
111 SetCanAddEventHandler(true);
112 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
113 pos
.x
, pos
.y
, size
.x
, size
.y
);
115 XtVaSetValues (GetXmList(this),
116 XmNvisibleItemCount
, 10,
119 ChangeBackgroundColour();
124 wxComboBox::~wxComboBox()
126 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
127 XtDestroyWidget((Widget
) m_mainWidget
);
128 m_mainWidget
= (WXWidget
) 0;
129 if ( HasClientObjectData() )
130 m_clientDataDict
.DestroyData();
133 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
135 // Necessary so it doesn't call wxChoice::SetSize
136 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
139 wxString
wxComboBox::GetValue() const
141 char* s
= XmTextGetString (GetXmText (this));
148 void wxComboBox::SetString(int n
, const wxString
& s
)
151 Widget listBox
= GetXmList(this);
153 // delete the item and add it again.
154 // FIXME isn't there a way to change it in place?
155 XmListDeletePos (listBox
, n
+1);
156 XmListAddItem (listBox
, text(), n
+1);
159 void wxComboBox::SetValue(const wxString
& value
)
163 XtVaSetValues( GetXmText(this),
164 XmNvalue
, (char *)value
.c_str(),
167 m_inSetValue
= false;
170 int wxComboBox::DoAppend(const wxString
& item
)
172 wxXmString
str( item
.c_str() );
173 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
174 m_stringList
.Add(item
);
177 return GetCount() - 1;
180 void wxComboBox::Delete(int n
)
182 #ifdef LESSTIF_VERSION
183 XmListDeletePos (GetXmList(this), n
+ 1);
185 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
188 wxStringList::Node
*node
= m_stringList
.Item(n
);
191 delete[] node
->GetData();
194 m_clientDataDict
.Delete(n
, HasClientObjectData());
198 void wxComboBox::Clear()
200 #ifdef LESSTIF_VERSION
201 XmListDeleteAllItems (GetXmList(this));
203 while(m_noStrings
> 0)
205 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
209 m_stringList
.Clear();
211 if ( HasClientObjectData() )
212 m_clientDataDict
.DestroyData();
216 void wxComboBox::SetSelection (int n
)
218 #ifdef LESSTIF_VERSION
219 XmListSelectPos (GetXmList(this), n
+ 1, false);
220 SetValue(GetString(n
));
222 wxXmString
str( GetString(n
).c_str() );
223 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
225 // does it work for Motif
226 XtVaSetValues( (Widget
)m_mainWidget
,
227 XmNselectedPosition
, n
+ 1,
233 int wxComboBox::GetSelection (void) const
235 return wxDoGetSelectionInList( GetXmList( this ) );
238 wxString
wxComboBox::GetString(int n
) const
240 wxStringList::Node
*node
= m_stringList
.Item(n
);
242 return wxString(node
->GetData ());
244 return wxEmptyString
;
247 int wxComboBox::FindString(const wxString
& s
) const
249 return wxDoFindStringInList( GetXmList( this ), s
);
252 // Clipboard operations
253 void wxComboBox::Copy()
255 XmTextCopy( GetXmText(this), CurrentTime
);
258 void wxComboBox::Cut()
260 XmTextCut( GetXmText(this), CurrentTime
);
263 void wxComboBox::Paste()
265 XmTextPaste( GetXmText(this) );
268 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
273 void wxComboBox::SetInsertionPoint(long pos
)
275 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
278 void wxComboBox::SetInsertionPointEnd()
280 SetInsertionPoint( GetLastPosition() );
283 long wxComboBox::GetInsertionPoint() const
285 return (long)XmTextGetInsertionPosition( GetXmText(this) );
288 long wxComboBox::GetLastPosition() const
290 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
294 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
296 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
297 (char*)value
.c_str() );
300 void wxComboBox::Remove(long from
, long to
)
302 SetSelection( from
, to
);
303 XmTextRemove( GetXmText(this) );
306 void wxComboBox::SetSelection(long from
, long to
)
309 to
= GetLastPosition();
311 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
312 (XmTextPosition
)to
, (Time
)0 );
315 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
316 XmComboBoxCallbackStruct
* cbs
)
318 wxComboBox
*item
= (wxComboBox
*) clientData
;
324 case XmCR_SINGLE_SELECT
:
325 case XmCR_BROWSE_SELECT
:
328 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
330 int idx
= cbs
->item_position
- 1;
331 event
.m_commandInt
= idx
;
332 event
.m_commandString
= item
->GetString (idx
);
333 if ( item
->HasClientObjectData() )
334 event
.SetClientObject( item
->GetClientObject(idx
) );
335 else if ( item
->HasClientUntypedData() )
336 event
.SetClientData( item
->GetClientData(idx
) );
337 event
.m_extraLong
= true;
338 event
.SetEventObject(item
);
339 item
->ProcessCommand (event
);
342 case XmCR_VALUE_CHANGED
:
344 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
345 event
.m_commandInt
= -1;
346 event
.m_commandString
= item
->GetValue();
347 event
.m_extraLong
= true;
348 event
.SetEventObject(item
);
349 item
->ProcessCommand (event
);
357 void wxComboBox::ChangeFont(bool keepOriginalSize
)
359 // Don't use the base class wxChoice's ChangeFont
360 wxWindow::ChangeFont(keepOriginalSize
);
363 void wxComboBox::ChangeBackgroundColour()
365 wxWindow::ChangeBackgroundColour();
368 void wxComboBox::ChangeForegroundColour()
370 wxWindow::ChangeForegroundColour();
373 wxSize
wxComboBox::DoGetBestSize() const
375 return wxWindow::DoGetBestSize();
378 #endif // XmVersion >= 2000
380 #endif // wxUSE_COMBOBOX