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
, (char*)value
.c_str() );
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
, (XmTextPosition
) to
,
257 (char*) (const char*) value
);
260 void wxComboBox::Remove(long from
, long to
)
262 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
264 XmComboBoxRemove ((Widget
) m_mainWidget
);
267 void wxComboBox::SetSelection(long from
, long to
)
269 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
273 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
274 XmComboBoxSelectionCallbackStruct
* cbs
)
276 wxComboBox
*item
= (wxComboBox
*) clientData
;
280 case XmCR_SINGLE_SELECT
:
281 case XmCR_BROWSE_SELECT
:
283 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
285 event
.m_commandInt
= cbs
->index
- 1;
286 event
.m_commandString
= item
->GetString (event
.m_commandInt
);
287 if ( item
->HasClientObjectData() )
288 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
289 else if ( item
->HasClientUntypedData() )
290 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
291 event
.m_extraLong
= TRUE
;
292 event
.SetEventObject(item
);
293 item
->ProcessCommand (event
);
296 case XmCR_VALUE_CHANGED
:
298 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
299 event
.m_commandInt
= -1;
300 event
.m_commandString
= item
->GetValue();
301 event
.m_extraLong
= TRUE
;
302 event
.SetEventObject(item
);
303 item
->ProcessCommand (event
);
311 void wxComboBox::ChangeFont(bool keepOriginalSize
)
313 // Don't use the base class wxChoice's ChangeFont
314 wxWindow::ChangeFont(keepOriginalSize
);
317 void wxComboBox::ChangeBackgroundColour()
319 wxWindow::ChangeBackgroundColour();
322 void wxComboBox::ChangeForegroundColour()
324 wxWindow::ChangeForegroundColour();
327 wxSize
wxComboBox::DoGetBestSize() const
329 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
330 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
332 wxSize items
= GetItemsSize();
333 // FIXME arbitrary constants
334 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
338 return wxWindow::DoGetBestSize();
341 #endif // XmVersion < 2000
343 #endif // wxUSE_COMBOBOX