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 SetCanAddEventHandler(TRUE
);
90 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
92 ChangeBackgroundColour();
97 wxComboBox::~wxComboBox()
99 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
100 XtDestroyWidget((Widget
) m_mainWidget
);
101 m_mainWidget
= (WXWidget
) 0;
102 if ( HasClientObjectData() )
103 m_clientDataDict
.DestroyData();
106 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
108 // Necessary so it doesn't call wxChoice::SetSize
109 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
112 wxString
wxComboBox::GetValue() const
114 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
122 return wxEmptyString
;
125 void wxComboBox::SetValue(const wxString
& value
)
129 XmComboBoxSetString( (Widget
)m_mainWidget
,
130 wxConstCast(value
.c_str(), char) );
131 m_inSetValue
= FALSE
;
134 void wxComboBox::SetString(int n
, const wxString
& s
)
136 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
139 int wxComboBox::DoAppend(const wxString
& item
)
141 wxXmString
str( item
.c_str() );
142 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
143 m_stringList
.Add(item
);
146 return GetCount() - 1;
149 void wxComboBox::Delete(int n
)
151 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
152 wxStringList::Node
*node
= m_stringList
.Item(n
);
155 delete[] node
->GetData();
158 m_clientDataDict
.Delete(n
, HasClientObjectData());
162 void wxComboBox::Clear()
164 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
165 m_stringList
.Clear();
167 if ( HasClientObjectData() )
168 m_clientDataDict
.DestroyData();
172 void wxComboBox::SetSelection (int n
)
174 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
177 int wxComboBox::GetSelection (void) const
179 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
186 wxString
wxComboBox::GetString(int n
) const
188 wxStringList::Node
*node
= m_stringList
.Item(n
);
190 return wxString(node
->GetData ());
192 return wxEmptyString
;
195 int wxComboBox::FindString(const wxString
& s
) const
197 int *pos_list
= NULL
;
199 wxXmString
text( s
);
200 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
201 text(), &pos_list
, &count
) != 0);
203 if (found
&& count
> 0)
205 int pos
= pos_list
[0] - 1;
213 // Clipboard operations
214 void wxComboBox::Copy()
216 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
219 void wxComboBox::Cut()
221 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
224 void wxComboBox::Paste()
226 XmComboBoxPaste((Widget
) m_mainWidget
);
229 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
234 void wxComboBox::SetInsertionPoint(long pos
)
236 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
239 void wxComboBox::SetInsertionPointEnd()
241 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
242 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
245 long wxComboBox::GetInsertionPoint() const
247 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
250 long wxComboBox::GetLastPosition() const
252 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
255 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
257 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
259 wxConstCast(value
.c_str(), char));
262 void wxComboBox::Remove(long from
, long to
)
264 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
266 XmComboBoxRemove ((Widget
) m_mainWidget
);
269 void wxComboBox::SetSelection(long from
, long to
)
271 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
275 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
276 XmComboBoxSelectionCallbackStruct
* cbs
)
278 wxComboBox
*item
= (wxComboBox
*) clientData
;
282 case XmCR_SINGLE_SELECT
:
283 case XmCR_BROWSE_SELECT
:
285 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
287 event
.m_commandInt
= cbs
->index
- 1;
288 event
.m_commandString
= item
->GetString (event
.m_commandInt
);
289 if ( item
->HasClientObjectData() )
290 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
291 else if ( item
->HasClientUntypedData() )
292 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
293 event
.m_extraLong
= TRUE
;
294 event
.SetEventObject(item
);
295 item
->ProcessCommand (event
);
298 case XmCR_VALUE_CHANGED
:
300 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
301 event
.m_commandInt
= -1;
302 event
.m_commandString
= item
->GetValue();
303 event
.m_extraLong
= TRUE
;
304 event
.SetEventObject(item
);
305 item
->ProcessCommand (event
);
313 void wxComboBox::ChangeFont(bool keepOriginalSize
)
315 // Don't use the base class wxChoice's ChangeFont
316 wxWindow::ChangeFont(keepOriginalSize
);
319 void wxComboBox::ChangeBackgroundColour()
321 wxWindow::ChangeBackgroundColour();
324 void wxComboBox::ChangeForegroundColour()
326 wxWindow::ChangeForegroundColour();
329 wxSize
wxComboBox::DoGetBestSize() const
331 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
332 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
334 wxSize items
= GetItemsSize();
335 // FIXME arbitrary constants
336 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
340 return wxWindow::DoGetBestSize();
343 #endif // XmVersion < 2000
345 #endif // wxUSE_COMBOBOX