]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
201a8aa4adaea0d4ebc44060715408cff651037f
[wxWidgets.git] / src / motif / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_COMBOBOX
16
17 #include "wx/combobox.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/arrstr.h"
21 #endif
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 PreCreation();
55
56 m_noStrings = n;
57
58 Widget parentWidget = (Widget) parent->GetClientWidget();
59
60 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
61 xmComboBoxWidgetClass, parentWidget,
62 XmNmarginHeight, 0,
63 XmNmarginWidth, 0,
64 XmNshowLabel, False,
65 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
66 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
67 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
68 NULL);
69
70 int i;
71 for (i = 0; i < n; i++)
72 {
73 wxXmString str( choices[i] );
74 XmComboBoxAddItem(buttonWidget, str(), 0);
75 m_stringList.Add(choices[i]);
76 }
77
78 m_mainWidget = (Widget) buttonWidget;
79
80 XtManageChild (buttonWidget);
81
82 SetValue(value);
83
84 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
85 (XtPointer) this);
86 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
87 (XtPointer) this);
88
89 PostCreation();
90 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
91
92 return true;
93 }
94
95 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
96 const wxString& value,
97 const wxPoint& pos,
98 const wxSize& size,
99 const wxArrayString& choices,
100 long style,
101 const wxValidator& validator,
102 const wxString& name)
103 {
104 wxCArrayString chs(choices);
105 return Create(parent, id, value, pos, size, chs.GetCount(),
106 chs.GetStrings(), style, validator, name);
107 }
108
109 wxComboBox::~wxComboBox()
110 {
111 DetachWidget((Widget) m_mainWidget); // Removes event handlers
112 XtDestroyWidget((Widget) m_mainWidget);
113 m_mainWidget = (WXWidget) 0;
114 if ( HasClientObjectData() )
115 m_clientDataDict.DestroyData();
116 }
117
118 void wxComboBox::DoSetSize(int x, int y,
119 int width, int WXUNUSED(height),
120 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(unsigned int WXUNUSED(n), const wxString& WXUNUSED(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, unsigned int pos)
164 {
165 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
166 wxCHECK_MSG(IsValidInsert(pos), -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(unsigned 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(unsigned 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, bool WXUNUSED(bCase)) const
227 {
228 // FIXME: back to base class for not supported value of bCase
229
230 int *pos_list = NULL;
231 int count = 0;
232 wxXmString text( s );
233 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
234 text(), &pos_list, &count) != 0);
235
236 if (found && count > 0)
237 {
238 int pos = pos_list[0] - 1;
239 free(pos_list);
240 return pos;
241 }
242
243 return wxNOT_FOUND;
244 }
245
246 // Clipboard operations
247 void wxComboBox::Copy()
248 {
249 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
250 }
251
252 void wxComboBox::Cut()
253 {
254 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
255 }
256
257 void wxComboBox::Paste()
258 {
259 XmComboBoxPaste((Widget) m_mainWidget);
260 }
261
262 void wxComboBox::SetEditable(bool WXUNUSED(editable))
263 {
264 // TODO
265 }
266
267 void wxComboBox::SetInsertionPoint(long pos)
268 {
269 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
270 }
271
272 void wxComboBox::SetInsertionPointEnd()
273 {
274 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
275 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
276 }
277
278 long wxComboBox::GetInsertionPoint() const
279 {
280 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
281 }
282
283 wxTextPos wxComboBox::GetLastPosition() const
284 {
285 return (wxTextPos) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
286 }
287
288 void wxComboBox::Replace(long from, long to, const wxString& value)
289 {
290 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
291 (XmTextPosition) to,
292 wxConstCast(value.c_str(), char));
293 }
294
295 void wxComboBox::Remove(long from, long to)
296 {
297 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
298 (Time) 0);
299 XmComboBoxRemove ((Widget) m_mainWidget);
300 }
301
302 void wxComboBox::SetSelection(long from, long to)
303 {
304 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
305 (Time) 0);
306 }
307
308 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
309 XmComboBoxSelectionCallbackStruct * cbs)
310 {
311 wxComboBox *item = (wxComboBox *) clientData;
312
313 switch (cbs->reason)
314 {
315 case XmCR_SINGLE_SELECT:
316 case XmCR_BROWSE_SELECT:
317 {
318 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
319 item->GetId());
320 event.SetInt(cbs->index - 1);
321 event.SetString( item->GetString ( event.GetInt() ) );
322 if ( item->HasClientObjectData() )
323 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
324 else if ( item->HasClientUntypedData() )
325 event.SetClientData( item->GetClientData(cbs->index - 1) );
326 event.SetExtraLong(true);
327 event.SetEventObject(item);
328 item->ProcessCommand (event);
329 break;
330 }
331 case XmCR_VALUE_CHANGED:
332 {
333 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
334 event.SetInt(-1);
335 event.SetString( item->GetValue() );
336 event.SetExtraLong(true);
337 event.SetEventObject(item);
338 item->ProcessCommand (event);
339 break;
340 }
341 default:
342 break;
343 }
344 }
345
346 void wxComboBox::ChangeFont(bool keepOriginalSize)
347 {
348 // Don't use the base class wxChoice's ChangeFont
349 wxWindow::ChangeFont(keepOriginalSize);
350 }
351
352 void wxComboBox::ChangeBackgroundColour()
353 {
354 wxWindow::ChangeBackgroundColour();
355 }
356
357 void wxComboBox::ChangeForegroundColour()
358 {
359 wxWindow::ChangeForegroundColour();
360 }
361
362 wxSize wxComboBox::DoGetBestSize() const
363 {
364 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
365 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
366 {
367 wxSize items = GetItemsSize();
368 // FIXME arbitrary constants
369 return wxSize( ( items.x ? items.x + 50 : 120 ),
370 items.y + 10 );
371 }
372 else
373 return wxWindow::DoGetBestSize();
374 }
375
376 #endif // XmVersion < 2000
377
378 #endif // wxUSE_COMBOBOX