]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
listbox kbd handling buglet corrected (event.Skip() called even when no
[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 #include <Xm/Xm.h>
21 #include "xmcombo/xmcombo.h"
22
23 void wxComboBoxCallback (Widget w, XtPointer clientData,
24 XmComboBoxSelectionCallbackStruct * cbs);
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
28 #endif
29
30 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
31 const wxString& value,
32 const wxPoint& pos,
33 const wxSize& size,
34 int n, const wxString choices[],
35 long style,
36 const wxValidator& validator,
37 const wxString& name)
38 {
39 SetName(name);
40 SetValidator(validator);
41 m_noStrings = n;
42 m_windowStyle = style;
43 m_backgroundColour = parent->GetBackgroundColour();
44 m_foregroundColour = parent->GetForegroundColour();
45
46 if (parent) parent->AddChild(this);
47
48 if ( id == -1 )
49 m_windowId = (int)NewControlId();
50 else
51 m_windowId = id;
52
53 Widget parentWidget = (Widget) parent->GetClientWidget();
54
55 Widget buttonWidget = XtVaCreateManagedWidget((char*) (const char*) name,
56 xmComboBoxWidgetClass, parentWidget,
57 XmNmarginHeight, 0,
58 XmNmarginWidth, 0,
59 XmNshowLabel, False,
60 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
61 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
62 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
63 NULL);
64
65 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
66 (XtPointer) this);
67 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
68 (XtPointer) this);
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_windowFont = parent->GetFont();
87 ChangeFont(FALSE);
88
89 SetCanAddEventHandler(TRUE);
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 wxComboBox::~wxComboBox()
98 {
99 DetachWidget((Widget) m_mainWidget); // Removes event handlers
100 XtDestroyWidget((Widget) m_mainWidget);
101 m_mainWidget = (WXWidget) 0;
102 }
103
104 void wxComboBox::SetSize(int x, int y, int width, int height, int sizeFlags)
105 {
106 // Necessary so it doesn't call wxChoice::SetSize
107 wxWindow::SetSize(x, y, width, height, sizeFlags);
108 }
109
110 wxString wxComboBox::GetValue() const
111 {
112 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
113 if (s)
114 {
115 wxString str(s);
116 XtFree (s);
117 return str;
118 }
119 else
120 return wxEmptyString;
121 }
122
123 void wxComboBox::SetValue(const wxString& value)
124 {
125 m_inSetValue = TRUE;
126 if (!value.IsNull())
127 XmComboBoxSetString ((Widget) m_mainWidget, (char*) (const char*) value);
128 m_inSetValue = FALSE;
129 }
130
131 void wxComboBox::Append(const wxString& item)
132 {
133 XmString str = XmStringCreateLtoR((char*) (const char*) item, XmSTRING_DEFAULT_CHARSET);
134 XmComboBoxAddItem((Widget) m_mainWidget, str, 0);
135 m_stringList.Add(item);
136 XmStringFree(str);
137 m_noStrings ++;
138 }
139
140 void wxComboBox::Delete(int n)
141 {
142 XmComboBoxDeletePos((Widget) m_mainWidget, n-1);
143 wxNode *node = m_stringList.Nth(n);
144 if (node)
145 {
146 delete[] (char *)node->Data();
147 delete node;
148 }
149 m_noStrings--;
150 }
151
152 void wxComboBox::Clear()
153 {
154 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
155 m_stringList.Clear();
156 }
157
158 void wxComboBox::SetSelection (int n)
159 {
160 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
161 }
162
163 int wxComboBox::GetSelection (void) const
164 {
165 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
166 if (sel == 0)
167 return -1;
168 else
169 return sel - 1;
170 }
171
172 wxString wxComboBox::GetString(int n) const
173 {
174 wxNode *node = m_stringList.Nth (n);
175 if (node)
176 return wxString((char *) node->Data ());
177 else
178 return wxEmptyString;
179 }
180
181 wxString wxComboBox::GetStringSelection() const
182 {
183 int sel = GetSelection();
184 if (sel == -1)
185 return wxEmptyString;
186 else
187 return GetString(sel);
188 }
189
190 bool wxComboBox::SetStringSelection(const wxString& sel)
191 {
192 int n = FindString(sel);
193 if (n == -1)
194 return FALSE;
195 else
196 {
197 SetSelection(n);
198 return TRUE;
199 }
200 }
201
202 int wxComboBox::FindString(const wxString& s) const
203 {
204 int *pos_list = NULL;
205 int count = 0;
206 XmString text = XmStringCreateSimple ((char*) (const char*) s);
207 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
208 text, &pos_list, &count) != 0);
209
210 XmStringFree(text);
211
212 if (found && count > 0)
213 {
214 int pos = pos_list[0] - 1;
215 free(pos_list);
216 return pos;
217 }
218
219 return -1;
220 }
221
222 // Clipboard operations
223 void wxComboBox::Copy()
224 {
225 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
226 }
227
228 void wxComboBox::Cut()
229 {
230 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
231 }
232
233 void wxComboBox::Paste()
234 {
235 XmComboBoxPaste((Widget) m_mainWidget);
236 }
237
238 void wxComboBox::SetEditable(bool WXUNUSED(editable))
239 {
240 // TODO
241 }
242
243 void wxComboBox::SetInsertionPoint(long pos)
244 {
245 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
246 }
247
248 void wxComboBox::SetInsertionPointEnd()
249 {
250 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
251 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
252 }
253
254 long wxComboBox::GetInsertionPoint() const
255 {
256 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
257 }
258
259 long wxComboBox::GetLastPosition() const
260 {
261 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
262 }
263
264 void wxComboBox::Replace(long from, long to, const wxString& value)
265 {
266 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
267 (char*) (const char*) value);
268 }
269
270 void wxComboBox::Remove(long from, long to)
271 {
272 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
273 (Time) 0);
274 XmComboBoxRemove ((Widget) m_mainWidget);
275 }
276
277 void wxComboBox::SetSelection(long from, long to)
278 {
279 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
280 (Time) 0);
281 }
282
283 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
284 XmComboBoxSelectionCallbackStruct * cbs)
285 {
286 wxComboBox *item = (wxComboBox *) clientData;
287
288 switch (cbs->reason)
289 {
290 case XmCR_SINGLE_SELECT:
291 case XmCR_BROWSE_SELECT:
292 {
293 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
294 event.m_commandInt = cbs->index - 1;
295 // event.m_commandString = item->GetString (event.m_commandInt);
296 event.m_extraLong = TRUE;
297 event.SetEventObject(item);
298 item->ProcessCommand (event);
299 break;
300 }
301 case XmCR_VALUE_CHANGED:
302 {
303 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
304 event.m_commandInt = -1;
305 // event.m_commandString = item->GetValue();
306 event.m_extraLong = TRUE;
307 event.SetEventObject(item);
308 item->ProcessCommand (event);
309 break;
310 }
311 default:
312 break;
313 }
314 }
315
316 void wxComboBox::ChangeFont(bool keepOriginalSize)
317 {
318 // Don't use the base class wxChoice's ChangeFont
319 wxWindow::ChangeFont(keepOriginalSize);
320 }
321
322 void wxComboBox::ChangeBackgroundColour()
323 {
324 wxWindow::ChangeBackgroundColour();
325 }
326
327 void wxComboBox::ChangeForegroundColour()
328 {
329 wxWindow::ChangeBackgroundColour();
330 }
331
332 #endif
333