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"
17 #include "wx/combobox.h"
20 #include "wx/arrstr.h"
24 #pragma message disable nosimpint
28 #pragma message enable nosimpint
31 // use the old, GPL'd combobox
32 #if (XmVersion < 2000)
34 #include "xmcombo/xmcombo.h"
36 #include "wx/motif/private.h"
38 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
39 XmComboBoxSelectionCallbackStruct
* cbs
);
41 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
43 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
44 const wxString
& value
,
47 int n
, const wxString choices
[],
49 const wxValidator
& validator
,
52 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
57 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
59 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
60 xmComboBoxWidgetClass
, parentWidget
,
64 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
65 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
66 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
70 for (i
= 0; i
< n
; i
++)
72 wxXmString
str( choices
[i
] );
73 XmComboBoxAddItem(buttonWidget
, str(), 0);
74 m_stringList
.Add(choices
[i
]);
77 m_mainWidget
= (Widget
) buttonWidget
;
79 XtManageChild (buttonWidget
);
85 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
87 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
90 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
92 ChangeBackgroundColour();
97 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
98 const wxString
& value
,
101 const wxArrayString
& choices
,
103 const wxValidator
& validator
,
104 const wxString
& name
)
106 wxCArrayString
chs(choices
);
107 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
108 chs
.GetStrings(), style
, validator
, name
);
111 wxComboBox::~wxComboBox()
113 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
114 XtDestroyWidget((Widget
) m_mainWidget
);
115 m_mainWidget
= (WXWidget
) 0;
116 if ( HasClientObjectData() )
117 m_clientDataDict
.DestroyData();
120 void wxComboBox::DoSetSize(int x
, int y
,
121 int width
, int WXUNUSED(height
),
124 // Necessary so it doesn't call wxChoice::SetSize
125 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
128 wxString
wxComboBox::GetValue() const
130 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
138 return wxEmptyString
;
141 void wxComboBox::SetValue(const wxString
& value
)
145 XmComboBoxSetString( (Widget
)m_mainWidget
,
146 wxConstCast(value
.c_str(), char) );
147 m_inSetValue
= false;
150 void wxComboBox::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
152 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
155 int wxComboBox::DoAppend(const wxString
& item
)
157 wxXmString
str( item
.c_str() );
158 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
159 m_stringList
.Add(item
);
162 return GetCount() - 1;
165 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
167 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
168 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
170 if (pos
== GetCount())
171 return DoAppend(item
);
173 wxXmString
str( item
.c_str() );
174 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1);
175 wxChar
* copy
= wxStrcpy(new wxChar
[item
.length() + 1], item
.c_str());
176 m_stringList
.Insert(pos
, copy
);
182 void wxComboBox::Delete(unsigned int n
)
184 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
185 wxStringList::Node
*node
= m_stringList
.Item(n
);
188 delete[] node
->GetData();
191 m_clientDataDict
.Delete(n
, HasClientObjectData());
195 void wxComboBox::Clear()
197 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
198 m_stringList
.Clear();
200 if ( HasClientObjectData() )
201 m_clientDataDict
.DestroyData();
205 void wxComboBox::SetSelection (int n
)
207 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
210 int wxComboBox::GetSelection (void) const
212 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
219 wxString
wxComboBox::GetString(unsigned int n
) const
221 wxStringList::Node
*node
= m_stringList
.Item(n
);
223 return wxString(node
->GetData ());
225 return wxEmptyString
;
228 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
230 // FIXME: back to base class for not supported value of bCase
232 int *pos_list
= NULL
;
234 wxXmString
text( s
);
235 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
236 text(), &pos_list
, &count
) != 0);
238 if (found
&& count
> 0)
240 int pos
= pos_list
[0] - 1;
248 // Clipboard operations
249 void wxComboBox::Copy()
251 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
254 void wxComboBox::Cut()
256 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
259 void wxComboBox::Paste()
261 XmComboBoxPaste((Widget
) m_mainWidget
);
264 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
269 void wxComboBox::SetInsertionPoint(long pos
)
271 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
274 void wxComboBox::SetInsertionPointEnd()
276 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
277 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
280 long wxComboBox::GetInsertionPoint() const
282 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
285 wxTextPos
wxComboBox::GetLastPosition() const
287 return (wxTextPos
) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
290 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
292 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
,
294 wxConstCast(value
.c_str(), char));
297 void wxComboBox::Remove(long from
, long to
)
299 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
301 XmComboBoxRemove ((Widget
) m_mainWidget
);
304 void wxComboBox::SetSelection(long from
, long to
)
306 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
310 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
311 XmComboBoxSelectionCallbackStruct
* cbs
)
313 wxComboBox
*item
= (wxComboBox
*) clientData
;
317 case XmCR_SINGLE_SELECT
:
318 case XmCR_BROWSE_SELECT
:
320 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
322 event
.SetInt(cbs
->index
- 1);
323 event
.SetString( item
->GetString ( event
.GetInt() ) );
324 if ( item
->HasClientObjectData() )
325 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
326 else if ( item
->HasClientUntypedData() )
327 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
328 event
.SetExtraLong(true);
329 event
.SetEventObject(item
);
330 item
->ProcessCommand (event
);
333 case XmCR_VALUE_CHANGED
:
335 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
337 event
.SetString( item
->GetValue() );
338 event
.SetExtraLong(true);
339 event
.SetEventObject(item
);
340 item
->ProcessCommand (event
);
348 void wxComboBox::ChangeFont(bool keepOriginalSize
)
350 // Don't use the base class wxChoice's ChangeFont
351 wxWindow::ChangeFont(keepOriginalSize
);
354 void wxComboBox::ChangeBackgroundColour()
356 wxWindow::ChangeBackgroundColour();
359 void wxComboBox::ChangeForegroundColour()
361 wxWindow::ChangeForegroundColour();
364 wxSize
wxComboBox::DoGetBestSize() const
366 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
367 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
369 wxSize items
= GetItemsSize();
370 // FIXME arbitrary constants
371 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
375 return wxWindow::DoGetBestSize();
378 #endif // XmVersion < 2000
380 #endif // wxUSE_COMBOBOX