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