]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
assert if opening more than one find/replace dialog under the same parent -- this...
[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#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
55034339 169void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
9b1bd0c6
MB
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
55034339 295int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
9b1bd0c6 296{
11e62fe6
WS
297 // FIXME: back to base class for not supported value of bCase
298
9b1bd0c6
MB
299 return wxDoFindStringInList( GetXmList( this ), s );
300}
301
302// Clipboard operations
303void wxComboBox::Copy()
304{
100f9289 305 XmTextCopy( GetXmText(this), CurrentTime );
9b1bd0c6
MB
306}
307
308void wxComboBox::Cut()
309{
100f9289 310 XmTextCut( GetXmText(this), CurrentTime );
9b1bd0c6
MB
311}
312
313void wxComboBox::Paste()
314{
100f9289 315 XmTextPaste( GetXmText(this) );
9b1bd0c6
MB
316}
317
318void wxComboBox::SetEditable(bool WXUNUSED(editable))
319{
320 // TODO
321}
322
323void wxComboBox::SetInsertionPoint(long pos)
324{
100f9289 325 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
9b1bd0c6
MB
326}
327
328void wxComboBox::SetInsertionPointEnd()
329{
100f9289 330 SetInsertionPoint( GetLastPosition() );
9b1bd0c6
MB
331}
332
333long wxComboBox::GetInsertionPoint() const
334{
100f9289 335 return (long)XmTextGetInsertionPosition( GetXmText(this) );
9b1bd0c6
MB
336}
337
7d8268a1 338wxTextPos wxComboBox::GetLastPosition() const
9b1bd0c6 339{
100f9289
MB
340 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
341 return (long)pos;
9b1bd0c6
MB
342}
343
344void wxComboBox::Replace(long from, long to, const wxString& value)
100f9289
MB
345{
346 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
d3a80c92 347 wxConstCast(value.c_str(), char) );
9b1bd0c6
MB
348}
349
350void wxComboBox::Remove(long from, long to)
351{
100f9289
MB
352 SetSelection( from, to );
353 XmTextRemove( GetXmText(this) );
9b1bd0c6
MB
354}
355
356void wxComboBox::SetSelection(long from, long to)
357{
100f9289
MB
358 if( to == -1 )
359 to = GetLastPosition();
360
361 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
362 (XmTextPosition)to, (Time)0 );
9b1bd0c6
MB
363}
364
365void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
366 XmComboBoxCallbackStruct * cbs)
367{
368 wxComboBox *item = (wxComboBox *) clientData;
369
d8d18184
MB
370 if( item->m_inSetSelection ) return;
371
9b1bd0c6
MB
372 switch (cbs->reason)
373 {
374 case XmCR_SELECT:
375#if 0
376 case XmCR_SINGLE_SELECT:
377 case XmCR_BROWSE_SELECT:
378#endif
379 {
380 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
381 item->GetId());
d8d18184 382 int idx = cbs->item_position;
687706f5
KH
383 event.SetInt(idx);
384 event.SetString( item->GetString (idx) );
9b1bd0c6
MB
385 if ( item->HasClientObjectData() )
386 event.SetClientObject( item->GetClientObject(idx) );
387 else if ( item->HasClientUntypedData() )
388 event.SetClientData( item->GetClientData(idx) );
687706f5 389 event.SetExtraLong(true);
9b1bd0c6 390 event.SetEventObject(item);
d8d18184 391 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
392 break;
393 }
394 case XmCR_VALUE_CHANGED:
395 {
396 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
687706f5
KH
397 event.SetInt(-1);
398 event.SetString( item->GetValue() );
399 event.SetExtraLong(true);
9b1bd0c6 400 event.SetEventObject(item);
d8d18184 401 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
402 break;
403 }
404 default:
405 break;
406 }
407}
408
409void wxComboBox::ChangeFont(bool keepOriginalSize)
410{
e1aae528
MB
411 if( m_font.Ok() )
412 {
413 wxDoChangeFont( GetXmText(this), m_font );
414 wxDoChangeFont( GetXmList(this), m_font );
415 }
416
9b1bd0c6
MB
417 // Don't use the base class wxChoice's ChangeFont
418 wxWindow::ChangeFont(keepOriginalSize);
419}
420
421void wxComboBox::ChangeBackgroundColour()
422{
423 wxWindow::ChangeBackgroundColour();
424}
425
426void wxComboBox::ChangeForegroundColour()
427{
428 wxWindow::ChangeForegroundColour();
429}
430
431wxSize wxComboBox::DoGetBestSize() const
432{
e1aae528
MB
433 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
434 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
435 {
436 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
437
438 XtVaGetValues( (Widget)m_mainWidget,
439 XmNarrowSize, &arrowW,
440 XmNarrowSpacing, &arrowS,
441 XmNhighlightThickness, &highlight,
442 XmNmarginWidth, &xmargin,
443 XmNmarginHeight, &ymargin,
444 XmNshadowThickness, &shadow,
445 NULL );
446
447 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
448 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
449 this );
450
451 // FIXME arbitrary constants
452 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
453 + 2 * shadow + 2 * xmargin ,
454 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
455 }
456 else
457 return wxWindow::DoGetBestSize();
9b1bd0c6
MB
458}
459
460#endif // XmVersion >= 2000
461
462#endif // wxUSE_COMBOBOX