]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
PCH-less build fix
[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 }
115
116 void wxComboBox::DoSetSize(int x, int y,
117 int width, int WXUNUSED(height),
118 int sizeFlags)
119 {
120 // Necessary so it doesn't call wxChoice::SetSize
121 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
122 }
123
124 wxString wxComboBox::GetValue() const
125 {
126 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
127 if (s)
128 {
129 wxString str(s);
130 XtFree (s);
131 return str;
132 }
133 else
134 return wxEmptyString;
135 }
136
137 void wxComboBox::SetValue(const wxString& value)
138 {
139 if( !value.empty() )
140 {
141 m_inSetValue = true;
142 XmComboBoxSetString((Widget)m_mainWidget, value.char_str());
143 m_inSetValue = false;
144 }
145 }
146
147 void wxComboBox::SetString(unsigned int WXUNUSED(n), const wxString& WXUNUSED(s))
148 {
149 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
150 }
151
152 // TODO auto-sorting is not supported by the code
153 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
154 unsigned int pos,
155 void **clientData,
156 wxClientDataType type)
157 {
158 const unsigned int numItems = items.GetCount();
159
160 AllocClientData(numItems);
161 for( unsigned int i = 0; i < numItems; ++i, ++pos )
162 {
163 wxXmString str( items[i].c_str() );
164 XmComboBoxAddItem((Widget) m_mainWidget, str(), GetMotifPosition(pos));
165 wxChar* copy = wxStrcpy(new wxChar[items[i].length() + 1], items[i].c_str());
166 m_stringList.Insert(pos, copy);
167 m_noStrings ++;
168 InsertNewItemClientData(pos, clientData, i, type);
169 }
170
171 return pos - 1;
172 }
173
174 void wxComboBox::DoDeleteOneItem(unsigned int n)
175 {
176 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
177 wxStringList::Node *node = m_stringList.Item(n);
178 if (node)
179 {
180 delete[] node->GetData();
181 delete node;
182 }
183 wxControlWithItems::DoDeleteOneItem(n);
184 m_noStrings--;
185 }
186
187 void wxComboBox::DoClear()
188 {
189 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
190 m_stringList.Clear();
191
192 wxControlWithItems::DoClear();
193 m_noStrings = 0;
194 }
195
196 void wxComboBox::SetSelection (int n)
197 {
198 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
199 }
200
201 int wxComboBox::GetSelection (void) const
202 {
203 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
204 if (sel == 0)
205 return -1;
206 else
207 return sel - 1;
208 }
209
210 wxString wxComboBox::GetString(unsigned int n) const
211 {
212 wxStringList::Node *node = m_stringList.Item(n);
213 if (node)
214 return wxString(node->GetData ());
215 else
216 return wxEmptyString;
217 }
218
219 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
220 {
221 // FIXME: back to base class for not supported value of bCase
222
223 int *pos_list = NULL;
224 int count = 0;
225 wxXmString text( s );
226 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
227 text(), &pos_list, &count) != 0);
228
229 if (found && count > 0)
230 {
231 int pos = pos_list[0] - 1;
232 free(pos_list);
233 return pos;
234 }
235
236 return wxNOT_FOUND;
237 }
238
239 // Clipboard operations
240 void wxComboBox::Copy()
241 {
242 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
243 }
244
245 void wxComboBox::Cut()
246 {
247 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
248 }
249
250 void wxComboBox::Paste()
251 {
252 XmComboBoxPaste((Widget) m_mainWidget);
253 }
254
255 void wxComboBox::SetEditable(bool WXUNUSED(editable))
256 {
257 // TODO
258 }
259
260 void wxComboBox::SetInsertionPoint(long pos)
261 {
262 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
263 }
264
265 void wxComboBox::SetInsertionPointEnd()
266 {
267 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
268 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
269 }
270
271 long wxComboBox::GetInsertionPoint() const
272 {
273 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
274 }
275
276 wxTextPos wxComboBox::GetLastPosition() const
277 {
278 return (wxTextPos) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
279 }
280
281 void wxComboBox::Replace(long from, long to, const wxString& value)
282 {
283 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
284 (XmTextPosition) to,
285 value.char_str());
286 }
287
288 void wxComboBox::Remove(long from, long to)
289 {
290 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
291 (Time) 0);
292 XmComboBoxRemove ((Widget) m_mainWidget);
293 }
294
295 void wxComboBox::SetSelection(long from, long to)
296 {
297 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
298 (Time) 0);
299 }
300
301 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
302 XmComboBoxSelectionCallbackStruct * cbs)
303 {
304 wxComboBox *item = (wxComboBox *) clientData;
305
306 switch (cbs->reason)
307 {
308 case XmCR_SINGLE_SELECT:
309 case XmCR_BROWSE_SELECT:
310 {
311 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
312 item->GetId());
313 event.SetInt(cbs->index - 1);
314 event.SetString( item->GetString ( event.GetInt() ) );
315 if ( item->HasClientObjectData() )
316 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
317 else if ( item->HasClientUntypedData() )
318 event.SetClientData( item->GetClientData(cbs->index - 1) );
319 event.SetExtraLong(true);
320 event.SetEventObject(item);
321 item->ProcessCommand (event);
322 break;
323 }
324 case XmCR_VALUE_CHANGED:
325 {
326 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
327 event.SetInt(-1);
328 event.SetString( item->GetValue() );
329 event.SetExtraLong(true);
330 event.SetEventObject(item);
331 item->ProcessCommand (event);
332 break;
333 }
334 default:
335 break;
336 }
337 }
338
339 void wxComboBox::ChangeFont(bool keepOriginalSize)
340 {
341 // Don't use the base class wxChoice's ChangeFont
342 wxWindow::ChangeFont(keepOriginalSize);
343 }
344
345 void wxComboBox::ChangeBackgroundColour()
346 {
347 wxWindow::ChangeBackgroundColour();
348 }
349
350 void wxComboBox::ChangeForegroundColour()
351 {
352 wxWindow::ChangeForegroundColour();
353 }
354
355 wxSize wxComboBox::DoGetBestSize() const
356 {
357 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
358 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
359 {
360 wxSize items = GetItemsSize();
361 // FIXME arbitrary constants
362 return wxSize( ( items.x ? items.x + 50 : 120 ),
363 items.y + 10 );
364 }
365 else
366 return wxWindow::DoGetBestSize();
367 }
368
369 #endif // XmVersion < 2000
370
371 #endif // wxUSE_COMBOBOX