]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
Don't let wxSlider send events when programmatically changing
[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 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/combobox.h"
17
18 #if wxUSE_COMBOBOX
19
20 #ifdef __VMS__
21 #pragma message disable nosimpint
22 #endif
23 #include <Xm/Xm.h>
24 #ifdef __VMS__
25 #pragma message enable nosimpint
26 #endif
27 #include "xmcombo/xmcombo.h"
28
29 void wxComboBoxCallback (Widget w, XtPointer clientData,
30 XmComboBoxSelectionCallbackStruct * cbs);
31
32 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
33
34 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
35 const wxString& value,
36 const wxPoint& pos,
37 const wxSize& size,
38 int n, const wxString choices[],
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
42 {
43 SetName(name);
44 SetValidator(validator);
45 m_noStrings = n;
46 m_windowStyle = style;
47 // m_backgroundColour = parent->GetBackgroundColour();
48 m_backgroundColour = * wxWHITE;
49 m_foregroundColour = parent->GetForegroundColour();
50
51 if (parent) parent->AddChild(this);
52
53 if ( id == -1 )
54 m_windowId = (int)NewControlId();
55 else
56 m_windowId = id;
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 XmString str = XmStringCreateLtoR((char*) (const char*) choices[i], XmSTRING_DEFAULT_CHARSET);
74 XmComboBoxAddItem(buttonWidget, str, 0);
75 XmStringFree(str);
76 m_stringList.Add(choices[i]);
77 }
78 m_noStrings = n;
79
80 m_mainWidget = (Widget) buttonWidget;
81
82 XtManageChild (buttonWidget);
83
84 SetValue(value);
85
86 m_font = parent->GetFont();
87 ChangeFont(FALSE);
88
89 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
90 (XtPointer) this);
91 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
92 (XtPointer) this);
93
94 SetCanAddEventHandler(TRUE);
95 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
96
97 ChangeBackgroundColour();
98
99 return TRUE;
100 }
101
102 wxComboBox::~wxComboBox()
103 {
104 DetachWidget((Widget) m_mainWidget); // Removes event handlers
105 XtDestroyWidget((Widget) m_mainWidget);
106 m_mainWidget = (WXWidget) 0;
107 }
108
109 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
110 {
111 // Necessary so it doesn't call wxChoice::SetSize
112 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
113 }
114
115 wxString wxComboBox::GetValue() const
116 {
117 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
118 if (s)
119 {
120 wxString str(s);
121 XtFree (s);
122 return str;
123 }
124 else
125 return wxEmptyString;
126 }
127
128 void wxComboBox::SetValue(const wxString& value)
129 {
130 m_inSetValue = TRUE;
131 if (!value.IsNull())
132 XmComboBoxSetString ((Widget) m_mainWidget, (char*) (const char*) value);
133 m_inSetValue = FALSE;
134 }
135
136 void wxComboBox::Append(const wxString& item)
137 {
138 XmString str = XmStringCreateLtoR((char*) (const char*) item, XmSTRING_DEFAULT_CHARSET);
139 XmComboBoxAddItem((Widget) m_mainWidget, str, 0);
140 m_stringList.Add(item);
141 XmStringFree(str);
142 m_noStrings ++;
143 }
144
145 void wxComboBox::Delete(int n)
146 {
147 XmComboBoxDeletePos((Widget) m_mainWidget, n-1);
148 wxNode *node = m_stringList.Nth(n);
149 if (node)
150 {
151 delete[] (char *)node->Data();
152 delete node;
153 }
154 node = m_clientList.Nth( n );
155 if (node)
156 {
157 if ( HasClientObjectData() )
158 {
159 delete (wxClientData *)node->Data();
160 }
161 delete node;
162 }
163
164 m_noStrings--;
165 }
166
167 void wxComboBox::Clear()
168 {
169 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
170 m_stringList.Clear();
171
172 if ( HasClientObjectData() )
173 {
174 // destroy the data (due to Robert's idea of using wxList<wxObject>
175 // and not wxList<wxClientData> we can't just say
176 // m_clientList.DeleteContents(TRUE) - this would crash!
177 wxNode *node = m_clientList.First();
178 while ( node )
179 {
180 delete (wxClientData *)node->Data();
181 node = node->Next();
182 }
183 }
184 m_clientList.Clear();
185 m_noStrings = 0;
186 }
187
188 void wxComboBox::SetSelection (int n)
189 {
190 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
191 }
192
193 int wxComboBox::GetSelection (void) const
194 {
195 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
196 if (sel == 0)
197 return -1;
198 else
199 return sel - 1;
200 }
201
202 wxString wxComboBox::GetString(int n) const
203 {
204 wxNode *node = m_stringList.Nth (n);
205 if (node)
206 return wxString((char *) node->Data ());
207 else
208 return wxEmptyString;
209 }
210
211 wxString wxComboBox::GetStringSelection() const
212 {
213 int sel = GetSelection();
214 if (sel == -1)
215 return wxEmptyString;
216 else
217 return GetString(sel);
218 }
219
220 bool wxComboBox::SetStringSelection(const wxString& sel)
221 {
222 int n = FindString(sel);
223 if (n == -1)
224 return FALSE;
225 else
226 {
227 SetSelection(n);
228 return TRUE;
229 }
230 }
231
232 int wxComboBox::FindString(const wxString& s) const
233 {
234 int *pos_list = NULL;
235 int count = 0;
236 XmString text = XmStringCreateSimple ((char*) (const char*) s);
237 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
238 text, &pos_list, &count) != 0);
239
240 XmStringFree(text);
241
242 if (found && count > 0)
243 {
244 int pos = pos_list[0] - 1;
245 free(pos_list);
246 return pos;
247 }
248
249 return -1;
250 }
251
252 // Clipboard operations
253 void wxComboBox::Copy()
254 {
255 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
256 }
257
258 void wxComboBox::Cut()
259 {
260 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
261 }
262
263 void wxComboBox::Paste()
264 {
265 XmComboBoxPaste((Widget) m_mainWidget);
266 }
267
268 void wxComboBox::SetEditable(bool WXUNUSED(editable))
269 {
270 // TODO
271 }
272
273 void wxComboBox::SetInsertionPoint(long pos)
274 {
275 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
276 }
277
278 void wxComboBox::SetInsertionPointEnd()
279 {
280 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
281 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
282 }
283
284 long wxComboBox::GetInsertionPoint() const
285 {
286 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
287 }
288
289 long wxComboBox::GetLastPosition() const
290 {
291 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
292 }
293
294 void wxComboBox::Replace(long from, long to, const wxString& value)
295 {
296 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
297 (char*) (const char*) value);
298 }
299
300 void wxComboBox::Remove(long from, long to)
301 {
302 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
303 (Time) 0);
304 XmComboBoxRemove ((Widget) m_mainWidget);
305 }
306
307 void wxComboBox::SetSelection(long from, long to)
308 {
309 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
310 (Time) 0);
311 }
312
313 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
314 XmComboBoxSelectionCallbackStruct * cbs)
315 {
316 wxComboBox *item = (wxComboBox *) clientData;
317
318 switch (cbs->reason)
319 {
320 case XmCR_SINGLE_SELECT:
321 case XmCR_BROWSE_SELECT:
322 {
323 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
324 event.m_commandInt = cbs->index - 1;
325 // event.m_commandString = item->GetString (event.m_commandInt);
326 event.m_extraLong = 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.m_commandInt = -1;
335 // event.m_commandString = item->GetValue();
336 event.m_extraLong = 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::ChangeBackgroundColour();
360 }
361
362 #endif
363