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