1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox.cpp
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
,
120 int width
, int WXUNUSED(height
),
123 // Necessary so it doesn't call wxChoice::SetSize
124 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
127 wxString
wxComboBox::GetValue() const
129 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
137 return wxEmptyString
;
140 void wxComboBox::SetValue(const wxString
& value
)
144 XmComboBoxSetString( (Widget
)m_mainWidget
,
145 wxConstCast(value
.c_str(), char) );
146 m_inSetValue
= false;
149 void wxComboBox::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
151 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
154 int wxComboBox::DoAppend(const wxString
& item
)
156 wxXmString
str( item
.c_str() );
157 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
158 m_stringList
.Add(item
);
161 return GetCount() - 1;
164 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
166 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
167 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
169 if (pos
== GetCount())
170 return DoAppend(item
);
172 wxXmString
str( item
.c_str() );
173 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1);
174 wxChar
* copy
= wxStrcpy(new wxChar
[item
.length() + 1], item
.c_str());
175 m_stringList
.Insert(pos
, copy
);
181 void wxComboBox::Delete(int n
)
183 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
184 wxStringList::Node
*node
= m_stringList
.Item(n
);
187 delete[] node
->GetData();
190 m_clientDataDict
.Delete(n
, HasClientObjectData());
194 void wxComboBox::Clear()
196 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
197 m_stringList
.Clear();
199 if ( HasClientObjectData() )
200 m_clientDataDict
.DestroyData();
204 void wxComboBox::SetSelection (int n
)
206 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
209 int wxComboBox::GetSelection (void) const
211 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
218 wxString
wxComboBox::GetString(int n
) const
220 wxStringList::Node
*node
= m_stringList
.Item(n
);
222 return wxString(node
->GetData ());
224 return wxEmptyString
;
227 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
229 // FIXME: back to base class for not supported value of bCase
231 int *pos_list
= NULL
;
233 wxXmString
text( s
);
234 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
235 text(), &pos_list
, &count
) != 0);
237 if (found
&& count
> 0)
239 int pos
= pos_list
[0] - 1;
247 // Clipboard operations
248 void wxComboBox::Copy()
250 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
253 void wxComboBox::Cut()
255 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
258 void wxComboBox::Paste()
260 XmComboBoxPaste((Widget
) m_mainWidget
);
263 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
268 void wxComboBox::SetInsertionPoint(long pos
)
270 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
273 void wxComboBox::SetInsertionPointEnd()
275 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
276 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
279 long wxComboBox::GetInsertionPoint() const
281 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
284 wxTextPos
wxComboBox::GetLastPosition() const
286 return (wxTextPos
) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
289 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
291 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
293 wxConstCast(value
.c_str(), char));
296 void wxComboBox::Remove(long from
, long to
)
298 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
300 XmComboBoxRemove ((Widget
) m_mainWidget
);
303 void wxComboBox::SetSelection(long from
, long to
)
305 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
309 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
310 XmComboBoxSelectionCallbackStruct
* cbs
)
312 wxComboBox
*item
= (wxComboBox
*) clientData
;
316 case XmCR_SINGLE_SELECT
:
317 case XmCR_BROWSE_SELECT
:
319 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
321 event
.SetInt(cbs
->index
- 1);
322 event
.SetString( item
->GetString ( event
.GetInt() ) );
323 if ( item
->HasClientObjectData() )
324 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
325 else if ( item
->HasClientUntypedData() )
326 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
327 event
.SetExtraLong(true);
328 event
.SetEventObject(item
);
329 item
->ProcessCommand (event
);
332 case XmCR_VALUE_CHANGED
:
334 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
336 event
.SetString( item
->GetValue() );
337 event
.SetExtraLong(true);
338 event
.SetEventObject(item
);
339 item
->ProcessCommand (event
);
347 void wxComboBox::ChangeFont(bool keepOriginalSize
)
349 // Don't use the base class wxChoice's ChangeFont
350 wxWindow::ChangeFont(keepOriginalSize
);
353 void wxComboBox::ChangeBackgroundColour()
355 wxWindow::ChangeBackgroundColour();
358 void wxComboBox::ChangeForegroundColour()
360 wxWindow::ChangeForegroundColour();
363 wxSize
wxComboBox::DoGetBestSize() const
365 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
366 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
368 wxSize items
= GetItemsSize();
369 // FIXME arbitrary constants
370 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
374 return wxWindow::DoGetBestSize();
377 #endif // XmVersion < 2000
379 #endif // wxUSE_COMBOBOX