]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
fix building with WXWIN_COMPATIBILITY_2_8 == 0
[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
9b1bd0c6 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
9b1bd0c6
MB
9/////////////////////////////////////////////////////////////////////////////
10
1248b41f
MB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
9b1bd0c6
MB
14#if wxUSE_COMBOBOX
15
16#include "wx/combobox.h"
aaa6d89a
WS
17
18#ifndef WX_PRECOMP
19 #include "wx/arrstr.h"
20#endif
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
9b1bd0c6
MB
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;
105fbe1f 80 PreCreation();
9b1bd0c6
MB
81
82 Widget parentWidget = (Widget) parent->GetClientWidget();
83
84 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
85 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
86 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
87 // default to wxCB_DROPDOWN
88 XmDROP_DOWN_COMBO_BOX;
e1aae528
MB
89 if( cb_type == XmDROP_DOWN_COMBO_BOX )
90 SetWindowStyle( style | wxCB_DROPDOWN );
9b1bd0c6
MB
91
92 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
93 xmComboBoxWidgetClass, parentWidget,
7d8268a1 94 XmNcomboBoxType, cb_type,
9b1bd0c6
MB
95 NULL);
96
97 m_mainWidget = (Widget) buttonWidget;
98
99 int i;
100 for ( i = 0; i < n; ++i)
101 Append( choices[i] );
102
103 XtManageChild (buttonWidget);
104
105 SetValue(value);
106
9b1bd0c6
MB
107 XtAddCallback (buttonWidget, XmNselectionCallback,
108 (XtCallbackProc) wxComboBoxCallback,
109 (XtPointer) this);
110 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
111 (XtCallbackProc) wxComboBoxCallback,
112 (XtPointer) this);
113
e1aae528 114 wxSize best = GetBestSize();
9b5f1895
WS
115 if( size.x != wxDefaultCoord ) best.x = size.x;
116 if( size.y != wxDefaultCoord ) best.y = size.y;
e1aae528 117
105fbe1f 118 PostCreation();
9b1bd0c6 119 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
e1aae528 120 pos.x, pos.y, best.x, best.y);
9b1bd0c6 121
9b1bd0c6
MB
122 return true;
123}
124
584ad2a3
MB
125bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
126 const wxString& value,
127 const wxPoint& pos,
128 const wxSize& size,
129 const wxArrayString& choices,
130 long style,
131 const wxValidator& validator,
132 const wxString& name)
133{
134 wxCArrayString chs(choices);
7d8268a1 135 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
136 chs.GetStrings(), style, validator, name);
137}
138
e1aae528
MB
139void wxComboBox::AdjustDropDownListSize()
140{
141 int newListCount = -1, itemCount = GetCount();
142 const int MAX = 12;
143
144 if( !itemCount )
145 newListCount = 1;
146 else if( itemCount < MAX )
147 newListCount = itemCount;
148 else
149 newListCount = MAX;
150
151 XtVaSetValues( GetXmList(this),
152 XmNvisibleItemCount, newListCount,
153 NULL );
154}
155
9b1bd0c6
MB
156wxComboBox::~wxComboBox()
157{
158 DetachWidget((Widget) m_mainWidget); // Removes event handlers
159 XtDestroyWidget((Widget) m_mainWidget);
160 m_mainWidget = (WXWidget) 0;
9b1bd0c6
MB
161}
162
55034339 163void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
9b1bd0c6
MB
164{
165 // Necessary so it doesn't call wxChoice::SetSize
166 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
167}
168
aa61d352 169void wxComboBox::SetString(unsigned int n, const wxString& s)
9b1bd0c6
MB
170{
171 wxXmString text(s);
172 Widget listBox = GetXmList(this);
173
174 // delete the item and add it again.
175 // FIXME isn't there a way to change it in place?
176 XmListDeletePos (listBox, n+1);
177 XmListAddItem (listBox, text(), n+1);
178}
179
180void wxComboBox::SetValue(const wxString& value)
181{
182 m_inSetValue = true;
183
184 XtVaSetValues( GetXmText(this),
6991087b 185 XmNvalue, (const char*)value.mb_str(),
9b1bd0c6
MB
186 NULL);
187
188 m_inSetValue = false;
189}
190
a236aa20
VZ
191int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
192 unsigned int pos,
193 void **clientData, wxClientDataType type)
9b1bd0c6 194{
a236aa20 195 const unsigned int numItems = items.GetCount();
9b1bd0c6 196
a236aa20
VZ
197 AllocClientData(numItems);
198 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
199 {
200 wxXmString str( items[i].c_str() );
201 XmComboBoxAddItem((Widget) m_mainWidget, str(),
202 GetMotifPosition(pos), False);
58045daa 203
a236aa20
VZ
204 InsertNewItemClientData(pos, clientData, i, type);
205 }
243dbf1a 206
243dbf1a
VZ
207 AdjustDropDownListSize();
208
a236aa20 209 return pos - 1;
243dbf1a
VZ
210}
211
a236aa20 212void wxComboBox::DoDeleteOneItem(unsigned int n)
9b1bd0c6
MB
213{
214#ifdef LESSTIF_VERSION
215 XmListDeletePos (GetXmList(this), n + 1);
216#else
217 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
218#endif
219
a236aa20 220 wxControlWithItems::DoDeleteOneItem(n);
58045daa 221 m_stringArray.RemoveAt(size_t(n));
e1aae528
MB
222
223 AdjustDropDownListSize();
9b1bd0c6
MB
224}
225
978c6e41 226void wxComboBox::Clear()
9b1bd0c6
MB
227{
228#ifdef LESSTIF_VERSION
229 XmListDeleteAllItems (GetXmList(this));
230#else
58045daa
FM
231 size_t n = m_stringArray.GetCount();
232 while(n > 0)
9b1bd0c6 233 {
58045daa 234 XmComboBoxDeletePos((Widget) m_mainWidget, n--);
9b1bd0c6 235 }
e1aae528 236#endif
9b1bd0c6 237
58045daa 238 m_stringArray.Clear();
e1aae528 239 AdjustDropDownListSize();
978c6e41
VZ
240
241 wxTextEntry::Clear();
9b1bd0c6
MB
242}
243
244void wxComboBox::SetSelection (int n)
245{
d8d18184
MB
246 m_inSetSelection = true;
247
d40708e2 248#if wxCHECK_LESSTIF()
9b1bd0c6
MB
249 XmListSelectPos (GetXmList(this), n + 1, false);
250 SetValue(GetString(n));
251#else
d40708e2 252#if 0
aa61d352 253 wxXmString str(GetString(n).c_str());
9b1bd0c6 254 XmComboBoxSelectItem((Widget) m_mainWidget, str());
d40708e2 255#endif
9b1bd0c6 256 XtVaSetValues( (Widget)m_mainWidget,
d40708e2 257 XmNselectedPosition, n,
9b1bd0c6
MB
258 NULL );
259#endif
d8d18184
MB
260
261 m_inSetSelection = false;
9b1bd0c6
MB
262}
263
978c6e41 264int wxComboBox::GetSelection() const
9b1bd0c6
MB
265{
266 return wxDoGetSelectionInList( GetXmList( this ) );
267}
268
aa61d352 269wxString wxComboBox::GetString(unsigned int n) const
9b1bd0c6 270{
e1aae528 271 return wxDoGetStringInList( GetXmList(this), n );
9b1bd0c6
MB
272}
273
55034339 274int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
9b1bd0c6 275{
11e62fe6
WS
276 // FIXME: back to base class for not supported value of bCase
277
9b1bd0c6
MB
278 return wxDoFindStringInList( GetXmList( this ), s );
279}
280
9b1bd0c6
MB
281void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
282 XmComboBoxCallbackStruct * cbs)
283{
284 wxComboBox *item = (wxComboBox *) clientData;
285
d8d18184
MB
286 if( item->m_inSetSelection ) return;
287
9b1bd0c6
MB
288 switch (cbs->reason)
289 {
290 case XmCR_SELECT:
291#if 0
292 case XmCR_SINGLE_SELECT:
293 case XmCR_BROWSE_SELECT:
294#endif
295 {
ce7fe42e 296 wxCommandEvent event (wxEVT_COMBOBOX,
9b1bd0c6 297 item->GetId());
d8d18184 298 int idx = cbs->item_position;
687706f5
KH
299 event.SetInt(idx);
300 event.SetString( item->GetString (idx) );
9b1bd0c6
MB
301 if ( item->HasClientObjectData() )
302 event.SetClientObject( item->GetClientObject(idx) );
303 else if ( item->HasClientUntypedData() )
304 event.SetClientData( item->GetClientData(idx) );
687706f5 305 event.SetExtraLong(true);
9b1bd0c6 306 event.SetEventObject(item);
937013e0 307 item->HandleWindowEvent(event);
9b1bd0c6
MB
308 break;
309 }
310 case XmCR_VALUE_CHANGED:
311 {
ce7fe42e 312 wxCommandEvent event (wxEVT_TEXT, item->GetId());
687706f5
KH
313 event.SetInt(-1);
314 event.SetString( item->GetValue() );
315 event.SetExtraLong(true);
9b1bd0c6 316 event.SetEventObject(item);
937013e0 317 item->HandleWindowEvent(event);
9b1bd0c6
MB
318 break;
319 }
320 default:
321 break;
322 }
323}
324
325void wxComboBox::ChangeFont(bool keepOriginalSize)
326{
a1b806b9 327 if( m_font.IsOk() && m_mainWidget != NULL )
e1aae528
MB
328 {
329 wxDoChangeFont( GetXmText(this), m_font );
330 wxDoChangeFont( GetXmList(this), m_font );
331 }
332
9b1bd0c6
MB
333 // Don't use the base class wxChoice's ChangeFont
334 wxWindow::ChangeFont(keepOriginalSize);
335}
336
337void wxComboBox::ChangeBackgroundColour()
338{
339 wxWindow::ChangeBackgroundColour();
340}
341
342void wxComboBox::ChangeForegroundColour()
343{
344 wxWindow::ChangeForegroundColour();
345}
346
347wxSize wxComboBox::DoGetBestSize() const
348{
e1aae528
MB
349 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
350 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
351 {
352 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
353
354 XtVaGetValues( (Widget)m_mainWidget,
355 XmNarrowSize, &arrowW,
356 XmNarrowSpacing, &arrowS,
357 XmNhighlightThickness, &highlight,
358 XmNmarginWidth, &xmargin,
359 XmNmarginHeight, &ymargin,
360 XmNshadowThickness, &shadow,
361 NULL );
362
363 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
364 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
365 this );
366
367 // FIXME arbitrary constants
368 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
369 + 2 * shadow + 2 * xmargin ,
370 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
371 }
372 else
373 return wxWindow::DoGetBestSize();
9b1bd0c6
MB
374}
375
89954433
VZ
376WXWidget wxComboBox::GetTextWidget() const
377{
378 return (WXWidget)GetXmText(this);
379}
380
9b1bd0c6
MB
381#endif // XmVersion >= 2000
382
383#endif // wxUSE_COMBOBOX