]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
deTABbed wxQsort()
[wxWidgets.git] / src / motif / combobox_native.cpp
CommitLineData
9b1bd0c6 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/motif/combobox_native.cpp
9b1bd0c6
MB
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
65571936 9// Licence: wxWindows licence
9b1bd0c6
MB
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
9b1bd0c6
MB
15#if wxUSE_COMBOBOX
16
17#include "wx/combobox.h"
aaa6d89a
WS
18
19#ifndef WX_PRECOMP
20 #include "wx/arrstr.h"
21#endif
9b1bd0c6
MB
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
100f9289
MB
34#ifdef __VMS__
35#pragma message disable nosimpint
36#endif
9b1bd0c6
MB
37#include <Xm/ComboBox.h>
38#include <Xm/Text.h>
39#include <Xm/List.h>
100f9289
MB
40#ifdef __VMS__
41#pragma message enable nosimpint
42#endif
9b1bd0c6
MB
43
44#include "wx/motif/private.h"
45
46// utility
47static 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
57static 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
67void wxComboBoxCallback (Widget w, XtPointer clientData,
68 XmComboBoxCallbackStruct * cbs);
69
70IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
71
72bool 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;
105fbe1f 83 PreCreation();
9b1bd0c6
MB
84
85 Widget parentWidget = (Widget) parent->GetClientWidget();
86
87 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
88 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
89 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
90 // default to wxCB_DROPDOWN
91 XmDROP_DOWN_COMBO_BOX;
e1aae528
MB
92 if( cb_type == XmDROP_DOWN_COMBO_BOX )
93 SetWindowStyle( style | wxCB_DROPDOWN );
9b1bd0c6
MB
94
95 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
96 xmComboBoxWidgetClass, parentWidget,
7d8268a1 97 XmNcomboBoxType, cb_type,
9b1bd0c6
MB
98 NULL);
99
100 m_mainWidget = (Widget) buttonWidget;
101
102 int i;
103 for ( i = 0; i < n; ++i)
104 Append( choices[i] );
105
106 XtManageChild (buttonWidget);
107
108 SetValue(value);
109
9b1bd0c6
MB
110 XtAddCallback (buttonWidget, XmNselectionCallback,
111 (XtCallbackProc) wxComboBoxCallback,
112 (XtPointer) this);
113 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
114 (XtCallbackProc) wxComboBoxCallback,
115 (XtPointer) this);
116
e1aae528 117 wxSize best = GetBestSize();
9b5f1895
WS
118 if( size.x != wxDefaultCoord ) best.x = size.x;
119 if( size.y != wxDefaultCoord ) best.y = size.y;
e1aae528 120
105fbe1f 121 PostCreation();
9b1bd0c6 122 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
e1aae528 123 pos.x, pos.y, best.x, best.y);
9b1bd0c6 124
9b1bd0c6
MB
125 return true;
126}
127
584ad2a3
MB
128bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
129 const wxString& value,
130 const wxPoint& pos,
131 const wxSize& size,
132 const wxArrayString& choices,
133 long style,
134 const wxValidator& validator,
135 const wxString& name)
136{
137 wxCArrayString chs(choices);
7d8268a1 138 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
139 chs.GetStrings(), style, validator, name);
140}
141
e1aae528
MB
142void wxComboBox::AdjustDropDownListSize()
143{
144 int newListCount = -1, itemCount = GetCount();
145 const int MAX = 12;
146
147 if( !itemCount )
148 newListCount = 1;
149 else if( itemCount < MAX )
150 newListCount = itemCount;
151 else
152 newListCount = MAX;
153
154 XtVaSetValues( GetXmList(this),
155 XmNvisibleItemCount, newListCount,
156 NULL );
157}
158
9b1bd0c6
MB
159wxComboBox::~wxComboBox()
160{
161 DetachWidget((Widget) m_mainWidget); // Removes event handlers
162 XtDestroyWidget((Widget) m_mainWidget);
163 m_mainWidget = (WXWidget) 0;
9b1bd0c6
MB
164}
165
55034339 166void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
9b1bd0c6
MB
167{
168 // Necessary so it doesn't call wxChoice::SetSize
169 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
170}
171
172wxString 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
aa61d352 181void wxComboBox::SetString(unsigned int n, const wxString& s)
9b1bd0c6
MB
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
192void wxComboBox::SetValue(const wxString& value)
193{
194 m_inSetValue = true;
195
196 XtVaSetValues( GetXmText(this),
6991087b 197 XmNvalue, (const char*)value.mb_str(),
9b1bd0c6
MB
198 NULL);
199
200 m_inSetValue = false;
201}
202
a236aa20
VZ
203int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
204 unsigned int pos,
205 void **clientData, wxClientDataType type)
9b1bd0c6 206{
a236aa20 207 const unsigned int numItems = items.GetCount();
9b1bd0c6 208
a236aa20
VZ
209 AllocClientData(numItems);
210 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
211 {
212 wxXmString str( items[i].c_str() );
213 XmComboBoxAddItem((Widget) m_mainWidget, str(),
214 GetMotifPosition(pos), False);
215 m_noStrings ++;
216 InsertNewItemClientData(pos, clientData, i, type);
217 }
243dbf1a 218
243dbf1a
VZ
219 AdjustDropDownListSize();
220
a236aa20 221 return pos - 1;
243dbf1a
VZ
222}
223
a236aa20 224void wxComboBox::DoDeleteOneItem(unsigned int n)
9b1bd0c6
MB
225{
226#ifdef LESSTIF_VERSION
227 XmListDeletePos (GetXmList(this), n + 1);
228#else
229 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
230#endif
231
a236aa20 232 wxControlWithItems::DoDeleteOneItem(n);
9b1bd0c6 233 m_noStrings--;
e1aae528
MB
234
235 AdjustDropDownListSize();
9b1bd0c6
MB
236}
237
a236aa20 238void wxComboBox::DoClear()
9b1bd0c6
MB
239{
240#ifdef LESSTIF_VERSION
241 XmListDeleteAllItems (GetXmList(this));
242#else
243 while(m_noStrings > 0)
244 {
7d8268a1 245 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
9b1bd0c6 246 }
e1aae528 247#endif
9b1bd0c6 248
a236aa20 249 wxControlWithItems::DoClear();
9b1bd0c6 250 m_noStrings = 0;
e1aae528 251 AdjustDropDownListSize();
9b1bd0c6
MB
252}
253
254void wxComboBox::SetSelection (int n)
255{
d8d18184
MB
256 m_inSetSelection = true;
257
d40708e2 258#if wxCHECK_LESSTIF()
9b1bd0c6
MB
259 XmListSelectPos (GetXmList(this), n + 1, false);
260 SetValue(GetString(n));
261#else
d40708e2 262#if 0
aa61d352 263 wxXmString str(GetString(n).c_str());
9b1bd0c6 264 XmComboBoxSelectItem((Widget) m_mainWidget, str());
d40708e2 265#endif
9b1bd0c6 266 XtVaSetValues( (Widget)m_mainWidget,
d40708e2 267 XmNselectedPosition, n,
9b1bd0c6
MB
268 NULL );
269#endif
d8d18184
MB
270
271 m_inSetSelection = false;
9b1bd0c6
MB
272}
273
274int wxComboBox::GetSelection (void) const
275{
276 return wxDoGetSelectionInList( GetXmList( this ) );
277}
278
aa61d352 279wxString wxComboBox::GetString(unsigned int n) const
9b1bd0c6 280{
e1aae528 281 return wxDoGetStringInList( GetXmList(this), n );
9b1bd0c6
MB
282}
283
55034339 284int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
9b1bd0c6 285{
11e62fe6
WS
286 // FIXME: back to base class for not supported value of bCase
287
9b1bd0c6
MB
288 return wxDoFindStringInList( GetXmList( this ), s );
289}
290
291// Clipboard operations
292void wxComboBox::Copy()
293{
100f9289 294 XmTextCopy( GetXmText(this), CurrentTime );
9b1bd0c6
MB
295}
296
297void wxComboBox::Cut()
298{
100f9289 299 XmTextCut( GetXmText(this), CurrentTime );
9b1bd0c6
MB
300}
301
302void wxComboBox::Paste()
303{
100f9289 304 XmTextPaste( GetXmText(this) );
9b1bd0c6
MB
305}
306
307void wxComboBox::SetEditable(bool WXUNUSED(editable))
308{
309 // TODO
310}
311
312void wxComboBox::SetInsertionPoint(long pos)
313{
100f9289 314 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
9b1bd0c6
MB
315}
316
317void wxComboBox::SetInsertionPointEnd()
318{
100f9289 319 SetInsertionPoint( GetLastPosition() );
9b1bd0c6
MB
320}
321
322long wxComboBox::GetInsertionPoint() const
323{
100f9289 324 return (long)XmTextGetInsertionPosition( GetXmText(this) );
9b1bd0c6
MB
325}
326
7d8268a1 327wxTextPos wxComboBox::GetLastPosition() const
9b1bd0c6 328{
100f9289
MB
329 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
330 return (long)pos;
9b1bd0c6
MB
331}
332
333void wxComboBox::Replace(long from, long to, const wxString& value)
100f9289
MB
334{
335 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
6991087b 336 value.char_str() );
9b1bd0c6
MB
337}
338
339void wxComboBox::Remove(long from, long to)
340{
100f9289
MB
341 SetSelection( from, to );
342 XmTextRemove( GetXmText(this) );
9b1bd0c6
MB
343}
344
345void wxComboBox::SetSelection(long from, long to)
346{
100f9289
MB
347 if( to == -1 )
348 to = GetLastPosition();
349
350 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
351 (XmTextPosition)to, (Time)0 );
9b1bd0c6
MB
352}
353
354void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
355 XmComboBoxCallbackStruct * cbs)
356{
357 wxComboBox *item = (wxComboBox *) clientData;
358
d8d18184
MB
359 if( item->m_inSetSelection ) return;
360
9b1bd0c6
MB
361 switch (cbs->reason)
362 {
363 case XmCR_SELECT:
364#if 0
365 case XmCR_SINGLE_SELECT:
366 case XmCR_BROWSE_SELECT:
367#endif
368 {
369 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
370 item->GetId());
d8d18184 371 int idx = cbs->item_position;
687706f5
KH
372 event.SetInt(idx);
373 event.SetString( item->GetString (idx) );
9b1bd0c6
MB
374 if ( item->HasClientObjectData() )
375 event.SetClientObject( item->GetClientObject(idx) );
376 else if ( item->HasClientUntypedData() )
377 event.SetClientData( item->GetClientData(idx) );
687706f5 378 event.SetExtraLong(true);
9b1bd0c6 379 event.SetEventObject(item);
d8d18184 380 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
381 break;
382 }
383 case XmCR_VALUE_CHANGED:
384 {
385 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
687706f5
KH
386 event.SetInt(-1);
387 event.SetString( item->GetValue() );
388 event.SetExtraLong(true);
9b1bd0c6 389 event.SetEventObject(item);
d8d18184 390 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
391 break;
392 }
393 default:
394 break;
395 }
396}
397
398void wxComboBox::ChangeFont(bool keepOriginalSize)
399{
105fbe1f 400 if( m_font.Ok() && m_mainWidget != NULL )
e1aae528
MB
401 {
402 wxDoChangeFont( GetXmText(this), m_font );
403 wxDoChangeFont( GetXmList(this), m_font );
404 }
405
9b1bd0c6
MB
406 // Don't use the base class wxChoice's ChangeFont
407 wxWindow::ChangeFont(keepOriginalSize);
408}
409
410void wxComboBox::ChangeBackgroundColour()
411{
412 wxWindow::ChangeBackgroundColour();
413}
414
415void wxComboBox::ChangeForegroundColour()
416{
417 wxWindow::ChangeForegroundColour();
418}
419
420wxSize wxComboBox::DoGetBestSize() const
421{
e1aae528
MB
422 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
423 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
424 {
425 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
426
427 XtVaGetValues( (Widget)m_mainWidget,
428 XmNarrowSize, &arrowW,
429 XmNarrowSpacing, &arrowS,
430 XmNhighlightThickness, &highlight,
431 XmNmarginWidth, &xmargin,
432 XmNmarginHeight, &ymargin,
433 XmNshadowThickness, &shadow,
434 NULL );
435
436 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
437 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
438 this );
439
440 // FIXME arbitrary constants
441 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
442 + 2 * shadow + 2 * xmargin ,
443 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
444 }
445 else
446 return wxWindow::DoGetBestSize();
9b1bd0c6
MB
447}
448
449#endif // XmVersion >= 2000
450
451#endif // wxUSE_COMBOBOX