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