]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
Check for G_FILENAME_ENCODING=@locale case insensitively.
[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;
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;
e1aae528
MB
91 if( cb_type == XmDROP_DOWN_COMBO_BOX )
92 SetWindowStyle( style | wxCB_DROPDOWN );
9b1bd0c6
MB
93
94 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
95 xmComboBoxWidgetClass, parentWidget,
7d8268a1 96 XmNcomboBoxType, cb_type,
9b1bd0c6
MB
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
e1aae528 118 wxSize best = GetBestSize();
9b5f1895
WS
119 if( size.x != wxDefaultCoord ) best.x = size.x;
120 if( size.y != wxDefaultCoord ) best.y = size.y;
e1aae528 121
9b1bd0c6 122 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
e1aae528 123 pos.x, pos.y, best.x, best.y);
9b1bd0c6
MB
124
125 ChangeBackgroundColour();
126
127 return true;
128}
129
584ad2a3
MB
130bool 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);
7d8268a1 140 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
141 chs.GetStrings(), style, validator, name);
142}
143
e1aae528
MB
144void 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
9b1bd0c6
MB
161wxComboBox::~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
55034339 170void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
9b1bd0c6
MB
171{
172 // Necessary so it doesn't call wxChoice::SetSize
173 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
174}
175
176wxString 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
aa61d352 185void wxComboBox::SetString(unsigned int n, const wxString& s)
9b1bd0c6
MB
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
196void wxComboBox::SetValue(const wxString& value)
197{
198 m_inSetValue = true;
199
d8d18184
MB
200 // Fix crash; probably an OpenMotif bug
201 const char* val = value.c_str() ? value.c_str() : "";
9b1bd0c6 202 XtVaSetValues( GetXmText(this),
d8d18184 203 XmNvalue, wxConstCast(val, char),
9b1bd0c6
MB
204 NULL);
205
206 m_inSetValue = false;
207}
208
209int wxComboBox::DoAppend(const wxString& item)
210{
211 wxXmString str( item.c_str() );
212 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
9b1bd0c6 213 m_noStrings ++;
e1aae528 214 AdjustDropDownListSize();
9b1bd0c6
MB
215
216 return GetCount() - 1;
217}
218
aa61d352 219int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
243dbf1a
VZ
220{
221 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
36e14c2b 222 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
243dbf1a 223
aa61d352 224 if (pos == GetCount())
243dbf1a
VZ
225 return DoAppend(item);
226
227 wxXmString str( item.c_str() );
228 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False);
229 m_noStrings ++;
230 AdjustDropDownListSize();
231
232 return GetCount() - 1;
233}
234
aa61d352 235void wxComboBox::Delete(unsigned int n)
9b1bd0c6
MB
236{
237#ifdef LESSTIF_VERSION
238 XmListDeletePos (GetXmList(this), n + 1);
239#else
240 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
241#endif
242
9b1bd0c6
MB
243 m_clientDataDict.Delete(n, HasClientObjectData());
244 m_noStrings--;
e1aae528
MB
245
246 AdjustDropDownListSize();
9b1bd0c6
MB
247}
248
249void wxComboBox::Clear()
250{
251#ifdef LESSTIF_VERSION
252 XmListDeleteAllItems (GetXmList(this));
253#else
254 while(m_noStrings > 0)
255 {
7d8268a1 256 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
9b1bd0c6 257 }
e1aae528 258#endif
9b1bd0c6
MB
259
260 if ( HasClientObjectData() )
261 m_clientDataDict.DestroyData();
262 m_noStrings = 0;
e1aae528 263 AdjustDropDownListSize();
9b1bd0c6
MB
264}
265
266void wxComboBox::SetSelection (int n)
267{
d8d18184
MB
268 m_inSetSelection = true;
269
d40708e2 270#if wxCHECK_LESSTIF()
9b1bd0c6
MB
271 XmListSelectPos (GetXmList(this), n + 1, false);
272 SetValue(GetString(n));
273#else
d40708e2 274#if 0
aa61d352 275 wxXmString str(GetString(n).c_str());
9b1bd0c6 276 XmComboBoxSelectItem((Widget) m_mainWidget, str());
d40708e2 277#endif
9b1bd0c6 278 XtVaSetValues( (Widget)m_mainWidget,
d40708e2 279 XmNselectedPosition, n,
9b1bd0c6
MB
280 NULL );
281#endif
d8d18184
MB
282
283 m_inSetSelection = false;
9b1bd0c6
MB
284}
285
286int wxComboBox::GetSelection (void) const
287{
288 return wxDoGetSelectionInList( GetXmList( this ) );
289}
290
aa61d352 291wxString wxComboBox::GetString(unsigned int n) const
9b1bd0c6 292{
e1aae528 293 return wxDoGetStringInList( GetXmList(this), n );
9b1bd0c6
MB
294}
295
55034339 296int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
9b1bd0c6 297{
11e62fe6
WS
298 // FIXME: back to base class for not supported value of bCase
299
9b1bd0c6
MB
300 return wxDoFindStringInList( GetXmList( this ), s );
301}
302
303// Clipboard operations
304void wxComboBox::Copy()
305{
100f9289 306 XmTextCopy( GetXmText(this), CurrentTime );
9b1bd0c6
MB
307}
308
309void wxComboBox::Cut()
310{
100f9289 311 XmTextCut( GetXmText(this), CurrentTime );
9b1bd0c6
MB
312}
313
314void wxComboBox::Paste()
315{
100f9289 316 XmTextPaste( GetXmText(this) );
9b1bd0c6
MB
317}
318
319void wxComboBox::SetEditable(bool WXUNUSED(editable))
320{
321 // TODO
322}
323
324void wxComboBox::SetInsertionPoint(long pos)
325{
100f9289 326 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
9b1bd0c6
MB
327}
328
329void wxComboBox::SetInsertionPointEnd()
330{
100f9289 331 SetInsertionPoint( GetLastPosition() );
9b1bd0c6
MB
332}
333
334long wxComboBox::GetInsertionPoint() const
335{
100f9289 336 return (long)XmTextGetInsertionPosition( GetXmText(this) );
9b1bd0c6
MB
337}
338
7d8268a1 339wxTextPos wxComboBox::GetLastPosition() const
9b1bd0c6 340{
100f9289
MB
341 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
342 return (long)pos;
9b1bd0c6
MB
343}
344
345void wxComboBox::Replace(long from, long to, const wxString& value)
100f9289
MB
346{
347 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
d3a80c92 348 wxConstCast(value.c_str(), char) );
9b1bd0c6
MB
349}
350
351void wxComboBox::Remove(long from, long to)
352{
100f9289
MB
353 SetSelection( from, to );
354 XmTextRemove( GetXmText(this) );
9b1bd0c6
MB
355}
356
357void wxComboBox::SetSelection(long from, long to)
358{
100f9289
MB
359 if( to == -1 )
360 to = GetLastPosition();
361
362 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
363 (XmTextPosition)to, (Time)0 );
9b1bd0c6
MB
364}
365
366void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
367 XmComboBoxCallbackStruct * cbs)
368{
369 wxComboBox *item = (wxComboBox *) clientData;
370
d8d18184
MB
371 if( item->m_inSetSelection ) return;
372
9b1bd0c6
MB
373 switch (cbs->reason)
374 {
375 case XmCR_SELECT:
376#if 0
377 case XmCR_SINGLE_SELECT:
378 case XmCR_BROWSE_SELECT:
379#endif
380 {
381 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
382 item->GetId());
d8d18184 383 int idx = cbs->item_position;
687706f5
KH
384 event.SetInt(idx);
385 event.SetString( item->GetString (idx) );
9b1bd0c6
MB
386 if ( item->HasClientObjectData() )
387 event.SetClientObject( item->GetClientObject(idx) );
388 else if ( item->HasClientUntypedData() )
389 event.SetClientData( item->GetClientData(idx) );
687706f5 390 event.SetExtraLong(true);
9b1bd0c6 391 event.SetEventObject(item);
d8d18184 392 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
393 break;
394 }
395 case XmCR_VALUE_CHANGED:
396 {
397 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
687706f5
KH
398 event.SetInt(-1);
399 event.SetString( item->GetValue() );
400 event.SetExtraLong(true);
9b1bd0c6 401 event.SetEventObject(item);
d8d18184 402 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
403 break;
404 }
405 default:
406 break;
407 }
408}
409
410void wxComboBox::ChangeFont(bool keepOriginalSize)
411{
e1aae528
MB
412 if( m_font.Ok() )
413 {
414 wxDoChangeFont( GetXmText(this), m_font );
415 wxDoChangeFont( GetXmList(this), m_font );
416 }
417
9b1bd0c6
MB
418 // Don't use the base class wxChoice's ChangeFont
419 wxWindow::ChangeFont(keepOriginalSize);
420}
421
422void wxComboBox::ChangeBackgroundColour()
423{
424 wxWindow::ChangeBackgroundColour();
425}
426
427void wxComboBox::ChangeForegroundColour()
428{
429 wxWindow::ChangeForegroundColour();
430}
431
432wxSize wxComboBox::DoGetBestSize() const
433{
e1aae528
MB
434 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
435 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
436 {
437 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
438
439 XtVaGetValues( (Widget)m_mainWidget,
440 XmNarrowSize, &arrowW,
441 XmNarrowSpacing, &arrowS,
442 XmNhighlightThickness, &highlight,
443 XmNmarginWidth, &xmargin,
444 XmNmarginHeight, &ymargin,
445 XmNshadowThickness, &shadow,
446 NULL );
447
448 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
449 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
450 this );
451
452 // FIXME arbitrary constants
453 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
454 + 2 * shadow + 2 * xmargin ,
455 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
456 }
457 else
458 return wxWindow::DoGetBestSize();
9b1bd0c6
MB
459}
460
461#endif // XmVersion >= 2000
462
463#endif // wxUSE_COMBOBOX