]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
adapting keycode field usage for EVT_CHAR in unicode to MSW variant (full unicode...
[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"
584ad2a3 18#include "wx/arrstr.h"
9b1bd0c6
MB
19
20#ifdef __VMS__
21#pragma message disable nosimpint
22#endif
23#include <Xm/Xm.h>
24#ifdef __VMS__
25#pragma message enable nosimpint
26#endif
27
28// use the new, shiny combobox for Motif 2.x
29#if (XmVersion >= 2000)
30
100f9289
MB
31#ifdef __VMS__
32#pragma message disable nosimpint
33#endif
9b1bd0c6
MB
34#include <Xm/ComboBox.h>
35#include <Xm/Text.h>
36#include <Xm/List.h>
100f9289
MB
37#ifdef __VMS__
38#pragma message enable nosimpint
39#endif
9b1bd0c6
MB
40
41#include "wx/motif/private.h"
42
43// utility
44static Widget GetXmList( const wxComboBox* cb )
45{
46 Widget ret;
47 XtVaGetValues( (Widget)cb->GetMainWidget(),
48 XmNlist, &ret,
49 NULL );
50
51 return ret;
52}
53
54static Widget GetXmText( const wxComboBox* cb )
55{
56 Widget ret;
57 XtVaGetValues( (Widget)cb->GetMainWidget(),
58 XmNtextField, &ret,
59 NULL );
60
61 return ret;
62}
63
64void wxComboBoxCallback (Widget w, XtPointer clientData,
65 XmComboBoxCallbackStruct * cbs);
66
67IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
68
69bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
70 const wxString& value,
71 const wxPoint& pos,
72 const wxSize& size,
73 int n, const wxString choices[],
74 long style,
75 const wxValidator& validator,
76 const wxString& name)
77{
78 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
79 return false;
80
81 Widget parentWidget = (Widget) parent->GetClientWidget();
82
83 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
84 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
85 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
86 // default to wxCB_DROPDOWN
87 XmDROP_DOWN_COMBO_BOX;
e1aae528
MB
88 if( cb_type == XmDROP_DOWN_COMBO_BOX )
89 SetWindowStyle( style | wxCB_DROPDOWN );
9b1bd0c6
MB
90
91 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
92 xmComboBoxWidgetClass, parentWidget,
7d8268a1 93 XmNcomboBoxType, cb_type,
9b1bd0c6
MB
94 NULL);
95
96 m_mainWidget = (Widget) buttonWidget;
97
98 int i;
99 for ( i = 0; i < n; ++i)
100 Append( choices[i] );
101
102 XtManageChild (buttonWidget);
103
104 SetValue(value);
105
106 ChangeFont(false);
107
108 XtAddCallback (buttonWidget, XmNselectionCallback,
109 (XtCallbackProc) wxComboBoxCallback,
110 (XtPointer) this);
111 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
112 (XtCallbackProc) wxComboBoxCallback,
113 (XtPointer) this);
114
e1aae528 115 wxSize best = GetBestSize();
9b5f1895
WS
116 if( size.x != wxDefaultCoord ) best.x = size.x;
117 if( size.y != wxDefaultCoord ) best.y = size.y;
e1aae528 118
9b1bd0c6 119 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
e1aae528 120 pos.x, pos.y, best.x, best.y);
9b1bd0c6
MB
121
122 ChangeBackgroundColour();
123
124 return true;
125}
126
584ad2a3
MB
127bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
128 const wxString& value,
129 const wxPoint& pos,
130 const wxSize& size,
131 const wxArrayString& choices,
132 long style,
133 const wxValidator& validator,
134 const wxString& name)
135{
136 wxCArrayString chs(choices);
7d8268a1 137 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
138 chs.GetStrings(), style, validator, name);
139}
140
e1aae528
MB
141void wxComboBox::AdjustDropDownListSize()
142{
143 int newListCount = -1, itemCount = GetCount();
144 const int MAX = 12;
145
146 if( !itemCount )
147 newListCount = 1;
148 else if( itemCount < MAX )
149 newListCount = itemCount;
150 else
151 newListCount = MAX;
152
153 XtVaSetValues( GetXmList(this),
154 XmNvisibleItemCount, newListCount,
155 NULL );
156}
157
9b1bd0c6
MB
158wxComboBox::~wxComboBox()
159{
160 DetachWidget((Widget) m_mainWidget); // Removes event handlers
161 XtDestroyWidget((Widget) m_mainWidget);
162 m_mainWidget = (WXWidget) 0;
163 if ( HasClientObjectData() )
164 m_clientDataDict.DestroyData();
165}
166
55034339 167void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
9b1bd0c6
MB
168{
169 // Necessary so it doesn't call wxChoice::SetSize
170 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
171}
172
173wxString wxComboBox::GetValue() const
174{
175 char* s = XmTextGetString (GetXmText (this));
176 wxString str(s);
177 if (s)
178 XtFree (s);
179 return str;
180}
181
182void wxComboBox::SetString(int n, const wxString& s)
183{
184 wxXmString text(s);
185 Widget listBox = GetXmList(this);
186
187 // delete the item and add it again.
188 // FIXME isn't there a way to change it in place?
189 XmListDeletePos (listBox, n+1);
190 XmListAddItem (listBox, text(), n+1);
191}
192
193void wxComboBox::SetValue(const wxString& value)
194{
195 m_inSetValue = true;
196
d8d18184
MB
197 // Fix crash; probably an OpenMotif bug
198 const char* val = value.c_str() ? value.c_str() : "";
9b1bd0c6 199 XtVaSetValues( GetXmText(this),
d8d18184 200 XmNvalue, wxConstCast(val, char),
9b1bd0c6
MB
201 NULL);
202
203 m_inSetValue = false;
204}
205
206int wxComboBox::DoAppend(const wxString& item)
207{
208 wxXmString str( item.c_str() );
209 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
9b1bd0c6 210 m_noStrings ++;
e1aae528 211 AdjustDropDownListSize();
9b1bd0c6
MB
212
213 return GetCount() - 1;
214}
215
243dbf1a
VZ
216int wxComboBox::DoInsert(const wxString& item, int pos)
217{
218 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
219 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
220
221 if (pos == GetCount())
222 return DoAppend(item);
223
224 wxXmString str( item.c_str() );
225 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False);
226 m_noStrings ++;
227 AdjustDropDownListSize();
228
229 return GetCount() - 1;
230}
231
9b1bd0c6
MB
232void wxComboBox::Delete(int n)
233{
234#ifdef LESSTIF_VERSION
235 XmListDeletePos (GetXmList(this), n + 1);
236#else
237 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
238#endif
239
9b1bd0c6
MB
240 m_clientDataDict.Delete(n, HasClientObjectData());
241 m_noStrings--;
e1aae528
MB
242
243 AdjustDropDownListSize();
9b1bd0c6
MB
244}
245
246void wxComboBox::Clear()
247{
248#ifdef LESSTIF_VERSION
249 XmListDeleteAllItems (GetXmList(this));
250#else
251 while(m_noStrings > 0)
252 {
7d8268a1 253 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
9b1bd0c6 254 }
e1aae528 255#endif
9b1bd0c6
MB
256
257 if ( HasClientObjectData() )
258 m_clientDataDict.DestroyData();
259 m_noStrings = 0;
e1aae528 260 AdjustDropDownListSize();
9b1bd0c6
MB
261}
262
263void wxComboBox::SetSelection (int n)
264{
d8d18184
MB
265 m_inSetSelection = true;
266
d40708e2 267#if wxCHECK_LESSTIF()
9b1bd0c6
MB
268 XmListSelectPos (GetXmList(this), n + 1, false);
269 SetValue(GetString(n));
270#else
d40708e2 271#if 0
9b1bd0c6
MB
272 wxXmString str( GetString(n).c_str() );
273 XmComboBoxSelectItem((Widget) m_mainWidget, str());
d40708e2 274#endif
9b1bd0c6 275 XtVaSetValues( (Widget)m_mainWidget,
d40708e2 276 XmNselectedPosition, n,
9b1bd0c6
MB
277 NULL );
278#endif
d8d18184
MB
279
280 m_inSetSelection = false;
9b1bd0c6
MB
281}
282
283int wxComboBox::GetSelection (void) const
284{
285 return wxDoGetSelectionInList( GetXmList( this ) );
286}
287
288wxString wxComboBox::GetString(int n) const
289{
e1aae528 290 return wxDoGetStringInList( GetXmList(this), n );
9b1bd0c6
MB
291}
292
55034339 293int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
9b1bd0c6 294{
11e62fe6
WS
295 // FIXME: back to base class for not supported value of bCase
296
9b1bd0c6
MB
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