]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
added possibility to throw different objects and crash directly from the main frame...
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "combobox.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/setup.h"
20
21 #if wxUSE_COMBOBOX
22
23 #include "wx/combobox.h"
24 #include "wx/arrstr.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/Xm.h>
30 #ifdef __VMS__
31 #pragma message enable nosimpint
32 #endif
33
34 // use the old, GPL'd combobox
35 #if (XmVersion < 2000)
36
37 #include "xmcombo/xmcombo.h"
38
39 #include "wx/motif/private.h"
40
41 void wxComboBoxCallback (Widget w, XtPointer clientData,
42 XmComboBoxSelectionCallbackStruct * cbs);
43
44 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
45
46 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
47 const wxString& value,
48 const wxPoint& pos,
49 const wxSize& size,
50 int n, const wxString choices[],
51 long style,
52 const wxValidator& validator,
53 const wxString& name)
54 {
55 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
56 return false;
57
58 m_noStrings = n;
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 int i;
73 for (i = 0; i < n; i++)
74 {
75 wxXmString str( choices[i] );
76 XmComboBoxAddItem(buttonWidget, str(), 0);
77 m_stringList.Add(choices[i]);
78 }
79
80 m_mainWidget = (Widget) buttonWidget;
81
82 XtManageChild (buttonWidget);
83
84 SetValue(value);
85
86 ChangeFont(false);
87
88 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
89 (XtPointer) this);
90 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
91 (XtPointer) this);
92
93 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
94
95 ChangeBackgroundColour();
96
97 return true;
98 }
99
100 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
101 const wxString& value,
102 const wxPoint& pos,
103 const wxSize& size,
104 const wxArrayString& choices,
105 long style,
106 const wxValidator& validator,
107 const wxString& name)
108 {
109 wxCArrayString chs(choices);
110 return Create(parent, id, value, pos, size, chs.GetCount(),
111 chs.GetStrings(), style, validator, name);
112 }
113
114 wxComboBox::~wxComboBox()
115 {
116 DetachWidget((Widget) m_mainWidget); // Removes event handlers
117 XtDestroyWidget((Widget) m_mainWidget);
118 m_mainWidget = (WXWidget) 0;
119 if ( HasClientObjectData() )
120 m_clientDataDict.DestroyData();
121 }
122
123 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
124 {
125 // Necessary so it doesn't call wxChoice::SetSize
126 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
127 }
128
129 wxString wxComboBox::GetValue() const
130 {
131 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
132 if (s)
133 {
134 wxString str(s);
135 XtFree (s);
136 return str;
137 }
138 else
139 return wxEmptyString;
140 }
141
142 void wxComboBox::SetValue(const wxString& value)
143 {
144 m_inSetValue = true;
145 if( !value.empty() )
146 XmComboBoxSetString( (Widget)m_mainWidget,
147 wxConstCast(value.c_str(), char) );
148 m_inSetValue = false;
149 }
150
151 void wxComboBox::SetString(int n, const wxString& s)
152 {
153 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
154 }
155
156 int wxComboBox::DoAppend(const wxString& item)
157 {
158 wxXmString str( item.c_str() );
159 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
160 m_stringList.Add(item);
161 m_noStrings ++;
162
163 return GetCount() - 1;
164 }
165
166 int wxComboBox::DoInsert(const wxString& item, int pos)
167 {
168 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
169 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
170
171 if (pos == GetCount())
172 return DoAppend(item);
173
174 wxXmString str( item.c_str() );
175 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1);
176 wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str());
177 m_stringList.Insert(pos, copy);
178 m_noStrings ++;
179
180 return pos;
181 }
182
183 void wxComboBox::Delete(int n)
184 {
185 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
186 wxStringList::Node *node = m_stringList.Item(n);
187 if (node)
188 {
189 delete[] node->GetData();
190 delete node;
191 }
192 m_clientDataDict.Delete(n, HasClientObjectData());
193 m_noStrings--;
194 }
195
196 void wxComboBox::Clear()
197 {
198 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
199 m_stringList.Clear();
200
201 if ( HasClientObjectData() )
202 m_clientDataDict.DestroyData();
203 m_noStrings = 0;
204 }
205
206 void wxComboBox::SetSelection (int n)
207 {
208 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
209 }
210
211 int wxComboBox::GetSelection (void) const
212 {
213 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
214 if (sel == 0)
215 return -1;
216 else
217 return sel - 1;
218 }
219
220 wxString wxComboBox::GetString(int n) const
221 {
222 wxStringList::Node *node = m_stringList.Item(n);
223 if (node)
224 return wxString(node->GetData ());
225 else
226 return wxEmptyString;
227 }
228
229 int wxComboBox::FindString(const wxString& s) const
230 {
231 int *pos_list = NULL;
232 int count = 0;
233 wxXmString text( s );
234 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
235 text(), &pos_list, &count) != 0);
236
237 if (found && count > 0)
238 {
239 int pos = pos_list[0] - 1;
240 free(pos_list);
241 return pos;
242 }
243
244 return -1;
245 }
246
247 // Clipboard operations
248 void wxComboBox::Copy()
249 {
250 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
251 }
252
253 void wxComboBox::Cut()
254 {
255 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
256 }
257
258 void wxComboBox::Paste()
259 {
260 XmComboBoxPaste((Widget) m_mainWidget);
261 }
262
263 void wxComboBox::SetEditable(bool WXUNUSED(editable))
264 {
265 // TODO
266 }
267
268 void wxComboBox::SetInsertionPoint(long pos)
269 {
270 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
271 }
272
273 void wxComboBox::SetInsertionPointEnd()
274 {
275 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
276 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
277 }
278
279 long wxComboBox::GetInsertionPoint() const
280 {
281 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
282 }
283
284 wxTextPos wxComboBox::GetLastPosition() const
285 {
286 return (wxTextPos) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
287 }
288
289 void wxComboBox::Replace(long from, long to, const wxString& value)
290 {
291 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
292 (XmTextPosition) to,
293 wxConstCast(value.c_str(), char));
294 }
295
296 void wxComboBox::Remove(long from, long to)
297 {
298 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
299 (Time) 0);
300 XmComboBoxRemove ((Widget) m_mainWidget);
301 }
302
303 void wxComboBox::SetSelection(long from, long to)
304 {
305 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
306 (Time) 0);
307 }
308
309 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
310 XmComboBoxSelectionCallbackStruct * cbs)
311 {
312 wxComboBox *item = (wxComboBox *) clientData;
313
314 switch (cbs->reason)
315 {
316 case XmCR_SINGLE_SELECT:
317 case XmCR_BROWSE_SELECT:
318 {
319 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
320 item->GetId());
321 event.SetInt(cbs->index - 1);
322 event.SetString( item->GetString ( event.GetInt() ) );
323 if ( item->HasClientObjectData() )
324 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
325 else if ( item->HasClientUntypedData() )
326 event.SetClientData( item->GetClientData(cbs->index - 1) );
327 event.SetExtraLong(true);
328 event.SetEventObject(item);
329 item->ProcessCommand (event);
330 break;
331 }
332 case XmCR_VALUE_CHANGED:
333 {
334 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
335 event.SetInt(-1);
336 event.SetString( item->GetValue() );
337 event.SetExtraLong(true);
338 event.SetEventObject(item);
339 item->ProcessCommand (event);
340 break;
341 }
342 default:
343 break;
344 }
345 }
346
347 void wxComboBox::ChangeFont(bool keepOriginalSize)
348 {
349 // Don't use the base class wxChoice's ChangeFont
350 wxWindow::ChangeFont(keepOriginalSize);
351 }
352
353 void wxComboBox::ChangeBackgroundColour()
354 {
355 wxWindow::ChangeBackgroundColour();
356 }
357
358 void wxComboBox::ChangeForegroundColour()
359 {
360 wxWindow::ChangeForegroundColour();
361 }
362
363 wxSize wxComboBox::DoGetBestSize() const
364 {
365 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
366 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
367 {
368 wxSize items = GetItemsSize();
369 // FIXME arbitrary constants
370 return wxSize( ( items.x ? items.x + 50 : 120 ),
371 items.y + 10 );
372 }
373 else
374 return wxWindow::DoGetBestSize();
375 }
376
377 #endif // XmVersion < 2000
378
379 #endif // wxUSE_COMBOBOX