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