]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
added wsock32.lib (patch 880683)
[wxWidgets.git] / src / motif / combobox_native.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox_native.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart, Ian Brown
5 // Modified by:
6 // Created: 01/02/03
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/setup.h"
13
14 #if wxUSE_COMBOBOX
15
16 #include "wx/combobox.h"
17 #include "wx/arrstr.h"
18
19 #ifdef __VMS__
20 #pragma message disable nosimpint
21 #endif
22 #include <Xm/Xm.h>
23 #ifdef __VMS__
24 #pragma message enable nosimpint
25 #endif
26
27 // use the new, shiny combobox for Motif 2.x
28 #if (XmVersion >= 2000)
29
30 #ifdef __VMS__
31 #pragma message disable nosimpint
32 #endif
33 #include <Xm/ComboBox.h>
34 #include <Xm/Text.h>
35 #include <Xm/List.h>
36 #ifdef __VMS__
37 #pragma message enable nosimpint
38 #endif
39
40 #include "wx/motif/private.h"
41
42 // utility
43 static Widget GetXmList( const wxComboBox* cb )
44 {
45 Widget ret;
46 XtVaGetValues( (Widget)cb->GetMainWidget(),
47 XmNlist, &ret,
48 NULL );
49
50 return ret;
51 }
52
53 static Widget GetXmText( const wxComboBox* cb )
54 {
55 Widget ret;
56 XtVaGetValues( (Widget)cb->GetMainWidget(),
57 XmNtextField, &ret,
58 NULL );
59
60 return ret;
61 }
62
63 void wxComboBoxCallback (Widget w, XtPointer clientData,
64 XmComboBoxCallbackStruct * cbs);
65
66 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
67
68 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
69 const wxString& value,
70 const wxPoint& pos,
71 const wxSize& size,
72 int n, const wxString choices[],
73 long style,
74 const wxValidator& validator,
75 const wxString& name)
76 {
77 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
78 return false;
79
80 Widget parentWidget = (Widget) parent->GetClientWidget();
81
82 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
83 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
84 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
85 // default to wxCB_DROPDOWN
86 XmDROP_DOWN_COMBO_BOX;
87 if( cb_type == XmDROP_DOWN_COMBO_BOX )
88 SetWindowStyle( style | wxCB_DROPDOWN );
89
90 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
91 xmComboBoxWidgetClass, parentWidget,
92 XmNcomboBoxType, cb_type,
93 NULL);
94
95 m_mainWidget = (Widget) buttonWidget;
96
97 int i;
98 for ( i = 0; i < n; ++i)
99 Append( choices[i] );
100
101 XtManageChild (buttonWidget);
102
103 SetValue(value);
104
105 ChangeFont(false);
106
107 XtAddCallback (buttonWidget, XmNselectionCallback,
108 (XtCallbackProc) wxComboBoxCallback,
109 (XtPointer) this);
110 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
111 (XtCallbackProc) wxComboBoxCallback,
112 (XtPointer) this);
113
114 wxSize best = GetBestSize();
115 if( size.x != -1 ) best.x = size.x;
116 if( size.y != -1 ) best.y = size.y;
117
118 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
119 pos.x, pos.y, best.x, best.y);
120
121 ChangeBackgroundColour();
122
123 return true;
124 }
125
126 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
127 const wxString& value,
128 const wxPoint& pos,
129 const wxSize& size,
130 const wxArrayString& choices,
131 long style,
132 const wxValidator& validator,
133 const wxString& name)
134 {
135 wxCArrayString chs(choices);
136 return Create(parent, id, value, pos, size, chs.GetCount(),
137 chs.GetStrings(), style, validator, name);
138 }
139
140 void wxComboBox::AdjustDropDownListSize()
141 {
142 int newListCount = -1, itemCount = GetCount();
143 const int MAX = 12;
144
145 if( !itemCount )
146 newListCount = 1;
147 else if( itemCount < MAX )
148 newListCount = itemCount;
149 else
150 newListCount = MAX;
151
152 XtVaSetValues( GetXmList(this),
153 XmNvisibleItemCount, newListCount,
154 NULL );
155 }
156
157 wxComboBox::~wxComboBox()
158 {
159 DetachWidget((Widget) m_mainWidget); // Removes event handlers
160 XtDestroyWidget((Widget) m_mainWidget);
161 m_mainWidget = (WXWidget) 0;
162 if ( HasClientObjectData() )
163 m_clientDataDict.DestroyData();
164 }
165
166 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
167 {
168 // Necessary so it doesn't call wxChoice::SetSize
169 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
170 }
171
172 wxString wxComboBox::GetValue() const
173 {
174 char* s = XmTextGetString (GetXmText (this));
175 wxString str(s);
176 if (s)
177 XtFree (s);
178 return str;
179 }
180
181 void wxComboBox::SetString(int n, const wxString& s)
182 {
183 wxXmString text(s);
184 Widget listBox = GetXmList(this);
185
186 // delete the item and add it again.
187 // FIXME isn't there a way to change it in place?
188 XmListDeletePos (listBox, n+1);
189 XmListAddItem (listBox, text(), n+1);
190 }
191
192 void wxComboBox::SetValue(const wxString& value)
193 {
194 m_inSetValue = true;
195
196 // Fix crash; probably an OpenMotif bug
197 const char* val = value.c_str() ? value.c_str() : "";
198 XtVaSetValues( GetXmText(this),
199 XmNvalue, wxConstCast(val, char),
200 NULL);
201
202 m_inSetValue = false;
203 }
204
205 int wxComboBox::DoAppend(const wxString& item)
206 {
207 wxXmString str( item.c_str() );
208 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
209 m_noStrings ++;
210 AdjustDropDownListSize();
211
212 return GetCount() - 1;
213 }
214
215 int wxComboBox::DoInsert(const wxString& item, int pos)
216 {
217 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
218 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
219
220 if (pos == GetCount())
221 return DoAppend(item);
222
223 wxXmString str( item.c_str() );
224 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False);
225 m_noStrings ++;
226 AdjustDropDownListSize();
227
228 return GetCount() - 1;
229 }
230
231 void wxComboBox::Delete(int n)
232 {
233 #ifdef LESSTIF_VERSION
234 XmListDeletePos (GetXmList(this), n + 1);
235 #else
236 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
237 #endif
238
239 m_clientDataDict.Delete(n, HasClientObjectData());
240 m_noStrings--;
241
242 AdjustDropDownListSize();
243 }
244
245 void wxComboBox::Clear()
246 {
247 #ifdef LESSTIF_VERSION
248 XmListDeleteAllItems (GetXmList(this));
249 #else
250 while(m_noStrings > 0)
251 {
252 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
253 }
254 #endif
255
256 if ( HasClientObjectData() )
257 m_clientDataDict.DestroyData();
258 m_noStrings = 0;
259 AdjustDropDownListSize();
260 }
261
262 void wxComboBox::SetSelection (int n)
263 {
264 m_inSetSelection = true;
265
266 #if wxCHECK_LESSTIF()
267 XmListSelectPos (GetXmList(this), n + 1, false);
268 SetValue(GetString(n));
269 #else
270 #if 0
271 wxXmString str( GetString(n).c_str() );
272 XmComboBoxSelectItem((Widget) m_mainWidget, str());
273 #endif
274 XtVaSetValues( (Widget)m_mainWidget,
275 XmNselectedPosition, n,
276 NULL );
277 #endif
278
279 m_inSetSelection = false;
280 }
281
282 int wxComboBox::GetSelection (void) const
283 {
284 return wxDoGetSelectionInList( GetXmList( this ) );
285 }
286
287 wxString wxComboBox::GetString(int n) const
288 {
289 return wxDoGetStringInList( GetXmList(this), n );
290 }
291
292 int wxComboBox::FindString(const wxString& s) const
293 {
294 return wxDoFindStringInList( GetXmList( this ), s );
295 }
296
297 // Clipboard operations
298 void wxComboBox::Copy()
299 {
300 XmTextCopy( GetXmText(this), CurrentTime );
301 }
302
303 void wxComboBox::Cut()
304 {
305 XmTextCut( GetXmText(this), CurrentTime );
306 }
307
308 void wxComboBox::Paste()
309 {
310 XmTextPaste( GetXmText(this) );
311 }
312
313 void wxComboBox::SetEditable(bool WXUNUSED(editable))
314 {
315 // TODO
316 }
317
318 void wxComboBox::SetInsertionPoint(long pos)
319 {
320 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
321 }
322
323 void wxComboBox::SetInsertionPointEnd()
324 {
325 SetInsertionPoint( GetLastPosition() );
326 }
327
328 long wxComboBox::GetInsertionPoint() const
329 {
330 return (long)XmTextGetInsertionPosition( GetXmText(this) );
331 }
332
333 long wxComboBox::GetLastPosition() const
334 {
335 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
336 return (long)pos;
337 }
338
339 void wxComboBox::Replace(long from, long to, const wxString& value)
340 {
341 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
342 wxConstCast(value.c_str(), char) );
343 }
344
345 void wxComboBox::Remove(long from, long to)
346 {
347 SetSelection( from, to );
348 XmTextRemove( GetXmText(this) );
349 }
350
351 void wxComboBox::SetSelection(long from, long to)
352 {
353 if( to == -1 )
354 to = GetLastPosition();
355
356 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
357 (XmTextPosition)to, (Time)0 );
358 }
359
360 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
361 XmComboBoxCallbackStruct * cbs)
362 {
363 wxComboBox *item = (wxComboBox *) clientData;
364
365 if( item->m_inSetSelection ) return;
366
367 switch (cbs->reason)
368 {
369 case XmCR_SELECT:
370 #if 0
371 case XmCR_SINGLE_SELECT:
372 case XmCR_BROWSE_SELECT:
373 #endif
374 {
375 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
376 item->GetId());
377 int idx = cbs->item_position;
378 event.m_commandInt = idx;
379 event.m_commandString = item->GetString (idx);
380 if ( item->HasClientObjectData() )
381 event.SetClientObject( item->GetClientObject(idx) );
382 else if ( item->HasClientUntypedData() )
383 event.SetClientData( item->GetClientData(idx) );
384 event.m_extraLong = true;
385 event.SetEventObject(item);
386 item->GetEventHandler()->ProcessEvent(event);
387 break;
388 }
389 case XmCR_VALUE_CHANGED:
390 {
391 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
392 event.m_commandInt = -1;
393 event.m_commandString = item->GetValue();
394 event.m_extraLong = true;
395 event.SetEventObject(item);
396 item->GetEventHandler()->ProcessEvent(event);
397 break;
398 }
399 default:
400 break;
401 }
402 }
403
404 void wxComboBox::ChangeFont(bool keepOriginalSize)
405 {
406 if( m_font.Ok() )
407 {
408 wxDoChangeFont( GetXmText(this), m_font );
409 wxDoChangeFont( GetXmList(this), m_font );
410 }
411
412 // Don't use the base class wxChoice's ChangeFont
413 wxWindow::ChangeFont(keepOriginalSize);
414 }
415
416 void wxComboBox::ChangeBackgroundColour()
417 {
418 wxWindow::ChangeBackgroundColour();
419 }
420
421 void wxComboBox::ChangeForegroundColour()
422 {
423 wxWindow::ChangeForegroundColour();
424 }
425
426 wxSize wxComboBox::DoGetBestSize() const
427 {
428 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
429 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
430 {
431 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
432
433 XtVaGetValues( (Widget)m_mainWidget,
434 XmNarrowSize, &arrowW,
435 XmNarrowSpacing, &arrowS,
436 XmNhighlightThickness, &highlight,
437 XmNmarginWidth, &xmargin,
438 XmNmarginHeight, &ymargin,
439 XmNshadowThickness, &shadow,
440 NULL );
441
442 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
443 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
444 this );
445
446 // FIXME arbitrary constants
447 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
448 + 2 * shadow + 2 * xmargin ,
449 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
450 }
451 else
452 return wxWindow::DoGetBestSize();
453 }
454
455 #endif // XmVersion >= 2000
456
457 #endif // wxUSE_COMBOBOX