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 void wxComboBox::Delete(int n
)
150 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
151 wxStringList::Node
*node
= m_stringList
.Item(n
);
154 delete[] node
->GetData();
157 m_clientDataDict
.Delete(n
, HasClientObjectData());
161 void wxComboBox::Clear()
163 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
164 m_stringList
.Clear();
166 if ( HasClientObjectData() )
167 m_clientDataDict
.DestroyData();
171 void wxComboBox::SetSelection (int n
)
173 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
176 int wxComboBox::GetSelection (void) const
178 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
185 wxString
wxComboBox::GetString(int n
) const
187 wxStringList::Node
*node
= m_stringList
.Item(n
);
189 return wxString(node
->GetData ());
191 return wxEmptyString
;
194 int wxComboBox::FindString(const wxString
& s
) const
196 int *pos_list
= NULL
;
198 wxXmString
text( s
);
199 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
200 text(), &pos_list
, &count
) != 0);
202 if (found
&& count
> 0)
204 int pos
= pos_list
[0] - 1;
212 // Clipboard operations
213 void wxComboBox::Copy()
215 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
218 void wxComboBox::Cut()
220 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
223 void wxComboBox::Paste()
225 XmComboBoxPaste((Widget
) m_mainWidget
);
228 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
233 void wxComboBox::SetInsertionPoint(long pos
)
235 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
238 void wxComboBox::SetInsertionPointEnd()
240 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
241 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
244 long wxComboBox::GetInsertionPoint() const
246 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
249 long wxComboBox::GetLastPosition() const
251 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
254 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
256 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
258 wxConstCast(value
.c_str(), char));
261 void wxComboBox::Remove(long from
, long to
)
263 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
265 XmComboBoxRemove ((Widget
) m_mainWidget
);
268 void wxComboBox::SetSelection(long from
, long to
)
270 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
274 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
275 XmComboBoxSelectionCallbackStruct
* cbs
)
277 wxComboBox
*item
= (wxComboBox
*) clientData
;
281 case XmCR_SINGLE_SELECT
:
282 case XmCR_BROWSE_SELECT
:
284 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
286 event
.m_commandInt
= cbs
->index
- 1;
287 event
.m_commandString
= item
->GetString (event
.m_commandInt
);
288 if ( item
->HasClientObjectData() )
289 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
290 else if ( item
->HasClientUntypedData() )
291 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
292 event
.m_extraLong
= TRUE
;
293 event
.SetEventObject(item
);
294 item
->ProcessCommand (event
);
297 case XmCR_VALUE_CHANGED
:
299 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
300 event
.m_commandInt
= -1;
301 event
.m_commandString
= item
->GetValue();
302 event
.m_extraLong
= TRUE
;
303 event
.SetEventObject(item
);
304 item
->ProcessCommand (event
);
312 void wxComboBox::ChangeFont(bool keepOriginalSize
)
314 // Don't use the base class wxChoice's ChangeFont
315 wxWindow::ChangeFont(keepOriginalSize
);
318 void wxComboBox::ChangeBackgroundColour()
320 wxWindow::ChangeBackgroundColour();
323 void wxComboBox::ChangeForegroundColour()
325 wxWindow::ChangeForegroundColour();
328 wxSize
wxComboBox::DoGetBestSize() const
330 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
331 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
333 wxSize items
= GetItemsSize();
334 // FIXME arbitrary constants
335 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
339 return wxWindow::DoGetBestSize();
342 #endif // XmVersion < 2000
344 #endif // wxUSE_COMBOBOX