1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
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 old, GPL'd combobox
31 #if (XmVersion < 2000)
33 #include "xmcombo/xmcombo.h"
35 #include "wx/motif/private.h"
37 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
38 XmComboBoxSelectionCallbackStruct
* cbs
);
40 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
42 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
43 const wxString
& value
,
46 int n
, const wxString choices
[],
48 const wxValidator
& validator
,
51 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
56 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
58 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
59 xmComboBoxWidgetClass
, parentWidget
,
63 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
64 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
65 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
69 for (i
= 0; i
< n
; i
++)
71 wxXmString
str( choices
[i
] );
72 XmComboBoxAddItem(buttonWidget
, str(), 0);
73 m_stringList
.Add(choices
[i
]);
76 m_mainWidget
= (Widget
) buttonWidget
;
78 XtManageChild (buttonWidget
);
84 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
86 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
89 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
91 ChangeBackgroundColour();
96 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
97 const wxString
& value
,
100 const wxArrayString
& choices
,
102 const wxValidator
& validator
,
103 const wxString
& name
)
105 wxCArrayString
chs(choices
);
106 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
107 chs
.GetStrings(), style
, validator
, name
);
110 wxComboBox::~wxComboBox()
112 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
113 XtDestroyWidget((Widget
) m_mainWidget
);
114 m_mainWidget
= (WXWidget
) 0;
115 if ( HasClientObjectData() )
116 m_clientDataDict
.DestroyData();
119 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
121 // Necessary so it doesn't call wxChoice::SetSize
122 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
125 wxString
wxComboBox::GetValue() const
127 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
135 return wxEmptyString
;
138 void wxComboBox::SetValue(const wxString
& value
)
142 XmComboBoxSetString( (Widget
)m_mainWidget
,
143 wxConstCast(value
.c_str(), char) );
144 m_inSetValue
= false;
147 void wxComboBox::SetString(int n
, const wxString
& s
)
149 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
152 int wxComboBox::DoAppend(const wxString
& item
)
154 wxXmString
str( item
.c_str() );
155 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
156 m_stringList
.Add(item
);
159 return GetCount() - 1;
162 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
164 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
165 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
167 if (pos
== GetCount())
168 return DoAppend(item
);
170 wxXmString
str( item
.c_str() );
171 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1);
172 wxChar
* copy
= wxStrcpy(new wxChar
[item
.length() + 1], item
.c_str());
173 m_stringList
.Insert(pos
, copy
);
179 void wxComboBox::Delete(int n
)
181 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 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
195 m_stringList
.Clear();
197 if ( HasClientObjectData() )
198 m_clientDataDict
.DestroyData();
202 void wxComboBox::SetSelection (int n
)
204 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
207 int wxComboBox::GetSelection (void) const
209 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
216 wxString
wxComboBox::GetString(int n
) const
218 wxStringList::Node
*node
= m_stringList
.Item(n
);
220 return wxString(node
->GetData ());
222 return wxEmptyString
;
225 int wxComboBox::FindString(const wxString
& s
) const
227 int *pos_list
= NULL
;
229 wxXmString
text( s
);
230 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
231 text(), &pos_list
, &count
) != 0);
233 if (found
&& count
> 0)
235 int pos
= pos_list
[0] - 1;
243 // Clipboard operations
244 void wxComboBox::Copy()
246 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
249 void wxComboBox::Cut()
251 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
254 void wxComboBox::Paste()
256 XmComboBoxPaste((Widget
) m_mainWidget
);
259 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
264 void wxComboBox::SetInsertionPoint(long pos
)
266 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
269 void wxComboBox::SetInsertionPointEnd()
271 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
272 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
275 long wxComboBox::GetInsertionPoint() const
277 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
280 wxTextPos
wxComboBox::GetLastPosition() const
282 return (wxTextPos
) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
285 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
287 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
289 wxConstCast(value
.c_str(), char));
292 void wxComboBox::Remove(long from
, long to
)
294 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
296 XmComboBoxRemove ((Widget
) m_mainWidget
);
299 void wxComboBox::SetSelection(long from
, long to
)
301 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
305 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
306 XmComboBoxSelectionCallbackStruct
* cbs
)
308 wxComboBox
*item
= (wxComboBox
*) clientData
;
312 case XmCR_SINGLE_SELECT
:
313 case XmCR_BROWSE_SELECT
:
315 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
317 event
.SetInt(cbs
->index
- 1);
318 event
.SetString( item
->GetString ( event
.GetInt() ) );
319 if ( item
->HasClientObjectData() )
320 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
321 else if ( item
->HasClientUntypedData() )
322 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
323 event
.SetExtraLong(true);
324 event
.SetEventObject(item
);
325 item
->ProcessCommand (event
);
328 case XmCR_VALUE_CHANGED
:
330 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
332 event
.SetString( item
->GetValue() );
333 event
.SetExtraLong(true);
334 event
.SetEventObject(item
);
335 item
->ProcessCommand (event
);
343 void wxComboBox::ChangeFont(bool keepOriginalSize
)
345 // Don't use the base class wxChoice's ChangeFont
346 wxWindow::ChangeFont(keepOriginalSize
);
349 void wxComboBox::ChangeBackgroundColour()
351 wxWindow::ChangeBackgroundColour();
354 void wxComboBox::ChangeForegroundColour()
356 wxWindow::ChangeForegroundColour();
359 wxSize
wxComboBox::DoGetBestSize() const
361 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
362 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
364 wxSize items
= GetItemsSize();
365 // FIXME arbitrary constants
366 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
370 return wxWindow::DoGetBestSize();
373 #endif // XmVersion < 2000
375 #endif // wxUSE_COMBOBOX