1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
20 #include "wx/combobox.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 wxComboBox::~wxComboBox()
98 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
99 XtDestroyWidget((Widget
) m_mainWidget
);
100 m_mainWidget
= (WXWidget
) 0;
101 if ( HasClientObjectData() )
102 m_clientDataDict
.DestroyData();
105 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
107 // Necessary so it doesn't call wxChoice::SetSize
108 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
111 wxString
wxComboBox::GetValue() const
113 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
121 return wxEmptyString
;
124 void wxComboBox::SetValue(const wxString
& value
)
128 XmComboBoxSetString( (Widget
)m_mainWidget
,
129 wxConstCast(value
.c_str(), char) );
130 m_inSetValue
= FALSE
;
133 void wxComboBox::SetString(int n
, const wxString
& s
)
135 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
138 int wxComboBox::DoAppend(const wxString
& item
)
140 wxXmString
str( item
.c_str() );
141 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
142 m_stringList
.Add(item
);
145 return GetCount() - 1;
148 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
150 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
151 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
153 if (pos
== GetCount())
154 return DoAppend(item
);
156 wxXmString
str( item
.c_str() );
157 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1);
158 wxChar
* copy
= wxStrcpy(new wxChar
[item
.length() + 1], item
.c_str());
159 m_stringList
.Insert(pos
, copy
);
165 void wxComboBox::Delete(int n
)
167 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
168 wxStringList::Node
*node
= m_stringList
.Item(n
);
171 delete[] node
->GetData();
174 m_clientDataDict
.Delete(n
, HasClientObjectData());
178 void wxComboBox::Clear()
180 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
181 m_stringList
.Clear();
183 if ( HasClientObjectData() )
184 m_clientDataDict
.DestroyData();
188 void wxComboBox::SetSelection (int n
)
190 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
193 int wxComboBox::GetSelection (void) const
195 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
202 wxString
wxComboBox::GetString(int n
) const
204 wxStringList::Node
*node
= m_stringList
.Item(n
);
206 return wxString(node
->GetData ());
208 return wxEmptyString
;
211 int wxComboBox::FindString(const wxString
& s
) const
213 int *pos_list
= NULL
;
215 wxXmString
text( s
);
216 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
217 text(), &pos_list
, &count
) != 0);
219 if (found
&& count
> 0)
221 int pos
= pos_list
[0] - 1;
229 // Clipboard operations
230 void wxComboBox::Copy()
232 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
235 void wxComboBox::Cut()
237 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
240 void wxComboBox::Paste()
242 XmComboBoxPaste((Widget
) m_mainWidget
);
245 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
250 void wxComboBox::SetInsertionPoint(long pos
)
252 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
255 void wxComboBox::SetInsertionPointEnd()
257 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
258 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
261 long wxComboBox::GetInsertionPoint() const
263 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
266 long wxComboBox::GetLastPosition() const
268 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
271 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
273 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
275 wxConstCast(value
.c_str(), char));
278 void wxComboBox::Remove(long from
, long to
)
280 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
282 XmComboBoxRemove ((Widget
) m_mainWidget
);
285 void wxComboBox::SetSelection(long from
, long to
)
287 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
291 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
292 XmComboBoxSelectionCallbackStruct
* cbs
)
294 wxComboBox
*item
= (wxComboBox
*) clientData
;
298 case XmCR_SINGLE_SELECT
:
299 case XmCR_BROWSE_SELECT
:
301 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
303 event
.m_commandInt
= cbs
->index
- 1;
304 event
.m_commandString
= item
->GetString (event
.m_commandInt
);
305 if ( item
->HasClientObjectData() )
306 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
307 else if ( item
->HasClientUntypedData() )
308 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
309 event
.m_extraLong
= TRUE
;
310 event
.SetEventObject(item
);
311 item
->ProcessCommand (event
);
314 case XmCR_VALUE_CHANGED
:
316 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
317 event
.m_commandInt
= -1;
318 event
.m_commandString
= item
->GetValue();
319 event
.m_extraLong
= TRUE
;
320 event
.SetEventObject(item
);
321 item
->ProcessCommand (event
);
329 void wxComboBox::ChangeFont(bool keepOriginalSize
)
331 // Don't use the base class wxChoice's ChangeFont
332 wxWindow::ChangeFont(keepOriginalSize
);
335 void wxComboBox::ChangeBackgroundColour()
337 wxWindow::ChangeBackgroundColour();
340 void wxComboBox::ChangeForegroundColour()
342 wxWindow::ChangeForegroundColour();
345 wxSize
wxComboBox::DoGetBestSize() const
347 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
348 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
350 wxSize items
= GetItemsSize();
351 // FIXME arbitrary constants
352 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
356 return wxWindow::DoGetBestSize();
359 #endif // XmVersion < 2000
361 #endif // wxUSE_COMBOBOX