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