]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
Allow app to call SetWindowVariant before creation of control
[wxWidgets.git] / src / motif / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/setup.h"
17
18 #if wxUSE_COMBOBOX
19
20 #include "wx/combobox.h"
21 #include "wx/arrstr.h"
22
23 #ifdef __VMS__
24 #pragma message disable nosimpint
25 #endif
26 #include <Xm/Xm.h>
27 #ifdef __VMS__
28 #pragma message enable nosimpint
29 #endif
30
31 // use the old, GPL'd combobox
32 #if (XmVersion < 2000)
33
34 #include "xmcombo/xmcombo.h"
35
36 #include "wx/motif/private.h"
37
38 void wxComboBoxCallback (Widget w, XtPointer clientData,
39 XmComboBoxSelectionCallbackStruct * cbs);
40
41 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
42
43 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
44 const wxString& value,
45 const wxPoint& pos,
46 const wxSize& size,
47 int n, const wxString choices[],
48 long style,
49 const wxValidator& validator,
50 const wxString& name)
51 {
52 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
53 return FALSE;
54
55 m_noStrings = n;
56
57 Widget parentWidget = (Widget) parent->GetClientWidget();
58
59 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
60 xmComboBoxWidgetClass, parentWidget,
61 XmNmarginHeight, 0,
62 XmNmarginWidth, 0,
63 XmNshowLabel, False,
64 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
65 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
66 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
67 NULL);
68
69 int i;
70 for (i = 0; i < n; i++)
71 {
72 wxXmString str( choices[i] );
73 XmComboBoxAddItem(buttonWidget, str(), 0);
74 m_stringList.Add(choices[i]);
75 }
76
77 m_mainWidget = (Widget) buttonWidget;
78
79 XtManageChild (buttonWidget);
80
81 SetValue(value);
82
83 ChangeFont(FALSE);
84
85 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
86 (XtPointer) this);
87 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
88 (XtPointer) this);
89
90 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
91
92 ChangeBackgroundColour();
93
94 return TRUE;
95 }
96
97 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
98 const wxString& value,
99 const wxPoint& pos,
100 const wxSize& size,
101 const wxArrayString& choices,
102 long style,
103 const wxValidator& validator,
104 const wxString& name)
105 {
106 wxCArrayString chs(choices);
107 return Create(parent, id, value, pos, size, chs.GetCount(),
108 chs.GetStrings(), style, validator, name);
109 }
110
111 wxComboBox::~wxComboBox()
112 {
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();
118 }
119
120 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
121 {
122 // Necessary so it doesn't call wxChoice::SetSize
123 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
124 }
125
126 wxString wxComboBox::GetValue() const
127 {
128 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
129 if (s)
130 {
131 wxString str(s);
132 XtFree (s);
133 return str;
134 }
135 else
136 return wxEmptyString;
137 }
138
139 void wxComboBox::SetValue(const wxString& value)
140 {
141 m_inSetValue = TRUE;
142 if( !value.empty() )
143 XmComboBoxSetString( (Widget)m_mainWidget,
144 wxConstCast(value.c_str(), char) );
145 m_inSetValue = FALSE;
146 }
147
148 void wxComboBox::SetString(int n, const wxString& s)
149 {
150 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
151 }
152
153 int wxComboBox::DoAppend(const wxString& item)
154 {
155 wxXmString str( item.c_str() );
156 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
157 m_stringList.Add(item);
158 m_noStrings ++;
159
160 return GetCount() - 1;
161 }
162
163 int wxComboBox::DoInsert(const wxString& item, int pos)
164 {
165 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
166 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
167
168 if (pos == GetCount())
169 return DoAppend(item);
170
171 wxXmString str( item.c_str() );
172 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1);
173 wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str());
174 m_stringList.Insert(pos, copy);
175 m_noStrings ++;
176
177 return pos;
178 }
179
180 void wxComboBox::Delete(int n)
181 {
182 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
183 wxStringList::Node *node = m_stringList.Item(n);
184 if (node)
185 {
186 delete[] node->GetData();
187 delete node;
188 }
189 m_clientDataDict.Delete(n, HasClientObjectData());
190 m_noStrings--;
191 }
192
193 void wxComboBox::Clear()
194 {
195 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
196 m_stringList.Clear();
197
198 if ( HasClientObjectData() )
199 m_clientDataDict.DestroyData();
200 m_noStrings = 0;
201 }
202
203 void wxComboBox::SetSelection (int n)
204 {
205 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
206 }
207
208 int wxComboBox::GetSelection (void) const
209 {
210 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
211 if (sel == 0)
212 return -1;
213 else
214 return sel - 1;
215 }
216
217 wxString wxComboBox::GetString(int n) const
218 {
219 wxStringList::Node *node = m_stringList.Item(n);
220 if (node)
221 return wxString(node->GetData ());
222 else
223 return wxEmptyString;
224 }
225
226 int wxComboBox::FindString(const wxString& s) const
227 {
228 int *pos_list = NULL;
229 int count = 0;
230 wxXmString text( s );
231 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
232 text(), &pos_list, &count) != 0);
233
234 if (found && count > 0)
235 {
236 int pos = pos_list[0] - 1;
237 free(pos_list);
238 return pos;
239 }
240
241 return -1;
242 }
243
244 // Clipboard operations
245 void wxComboBox::Copy()
246 {
247 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
248 }
249
250 void wxComboBox::Cut()
251 {
252 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
253 }
254
255 void wxComboBox::Paste()
256 {
257 XmComboBoxPaste((Widget) m_mainWidget);
258 }
259
260 void wxComboBox::SetEditable(bool WXUNUSED(editable))
261 {
262 // TODO
263 }
264
265 void wxComboBox::SetInsertionPoint(long pos)
266 {
267 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
268 }
269
270 void wxComboBox::SetInsertionPointEnd()
271 {
272 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
273 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
274 }
275
276 long wxComboBox::GetInsertionPoint() const
277 {
278 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
279 }
280
281 long wxComboBox::GetLastPosition() const
282 {
283 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
284 }
285
286 void wxComboBox::Replace(long from, long to, const wxString& value)
287 {
288 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
289 (XmTextPosition) to,
290 wxConstCast(value.c_str(), char));
291 }
292
293 void wxComboBox::Remove(long from, long to)
294 {
295 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
296 (Time) 0);
297 XmComboBoxRemove ((Widget) m_mainWidget);
298 }
299
300 void wxComboBox::SetSelection(long from, long to)
301 {
302 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
303 (Time) 0);
304 }
305
306 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
307 XmComboBoxSelectionCallbackStruct * cbs)
308 {
309 wxComboBox *item = (wxComboBox *) clientData;
310
311 switch (cbs->reason)
312 {
313 case XmCR_SINGLE_SELECT:
314 case XmCR_BROWSE_SELECT:
315 {
316 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
317 item->GetId());
318 event.m_commandInt = cbs->index - 1;
319 event.m_commandString = item->GetString (event.m_commandInt);
320 if ( item->HasClientObjectData() )
321 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
322 else if ( item->HasClientUntypedData() )
323 event.SetClientData( item->GetClientData(cbs->index - 1) );
324 event.m_extraLong = TRUE;
325 event.SetEventObject(item);
326 item->ProcessCommand (event);
327 break;
328 }
329 case XmCR_VALUE_CHANGED:
330 {
331 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
332 event.m_commandInt = -1;
333 event.m_commandString = item->GetValue();
334 event.m_extraLong = TRUE;
335 event.SetEventObject(item);
336 item->ProcessCommand (event);
337 break;
338 }
339 default:
340 break;
341 }
342 }
343
344 void wxComboBox::ChangeFont(bool keepOriginalSize)
345 {
346 // Don't use the base class wxChoice's ChangeFont
347 wxWindow::ChangeFont(keepOriginalSize);
348 }
349
350 void wxComboBox::ChangeBackgroundColour()
351 {
352 wxWindow::ChangeBackgroundColour();
353 }
354
355 void wxComboBox::ChangeForegroundColour()
356 {
357 wxWindow::ChangeForegroundColour();
358 }
359
360 wxSize wxComboBox::DoGetBestSize() const
361 {
362 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
363 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
364 {
365 wxSize items = GetItemsSize();
366 // FIXME arbitrary constants
367 return wxSize( ( items.x ? items.x + 50 : 120 ),
368 items.y + 10 );
369 }
370 else
371 return wxWindow::DoGetBestSize();
372 }
373
374 #endif // XmVersion < 2000
375
376 #endif // wxUSE_COMBOBOX