]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox_native.cpp
Pass events to pushed event handlers.
[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 117 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
e1aae528 118 pos.x, pos.y, best.x, best.y);
9b1bd0c6
MB
119
120 ChangeBackgroundColour();
121
122 return true;
123}
124
e1aae528
MB
125void wxComboBox::AdjustDropDownListSize()
126{
127 int newListCount = -1, itemCount = GetCount();
128 const int MAX = 12;
129
130 if( !itemCount )
131 newListCount = 1;
132 else if( itemCount < MAX )
133 newListCount = itemCount;
134 else
135 newListCount = MAX;
136
137 XtVaSetValues( GetXmList(this),
138 XmNvisibleItemCount, newListCount,
139 NULL );
140}
141
9b1bd0c6
MB
142wxComboBox::~wxComboBox()
143{
144 DetachWidget((Widget) m_mainWidget); // Removes event handlers
145 XtDestroyWidget((Widget) m_mainWidget);
146 m_mainWidget = (WXWidget) 0;
147 if ( HasClientObjectData() )
148 m_clientDataDict.DestroyData();
149}
150
151void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
152{
153 // Necessary so it doesn't call wxChoice::SetSize
154 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
155}
156
157wxString wxComboBox::GetValue() const
158{
159 char* s = XmTextGetString (GetXmText (this));
160 wxString str(s);
161 if (s)
162 XtFree (s);
163 return str;
164}
165
166void wxComboBox::SetString(int n, const wxString& s)
167{
168 wxXmString text(s);
169 Widget listBox = GetXmList(this);
170
171 // delete the item and add it again.
172 // FIXME isn't there a way to change it in place?
173 XmListDeletePos (listBox, n+1);
174 XmListAddItem (listBox, text(), n+1);
175}
176
177void wxComboBox::SetValue(const wxString& value)
178{
179 m_inSetValue = true;
180
d8d18184
MB
181 // Fix crash; probably an OpenMotif bug
182 const char* val = value.c_str() ? value.c_str() : "";
9b1bd0c6 183 XtVaSetValues( GetXmText(this),
d8d18184 184 XmNvalue, wxConstCast(val, char),
9b1bd0c6
MB
185 NULL);
186
187 m_inSetValue = false;
188}
189
190int wxComboBox::DoAppend(const wxString& item)
191{
192 wxXmString str( item.c_str() );
193 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
9b1bd0c6 194 m_noStrings ++;
e1aae528 195 AdjustDropDownListSize();
9b1bd0c6
MB
196
197 return GetCount() - 1;
198}
199
200void wxComboBox::Delete(int n)
201{
202#ifdef LESSTIF_VERSION
203 XmListDeletePos (GetXmList(this), n + 1);
204#else
205 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
206#endif
207
9b1bd0c6
MB
208 m_clientDataDict.Delete(n, HasClientObjectData());
209 m_noStrings--;
e1aae528
MB
210
211 AdjustDropDownListSize();
9b1bd0c6
MB
212}
213
214void wxComboBox::Clear()
215{
216#ifdef LESSTIF_VERSION
217 XmListDeleteAllItems (GetXmList(this));
218#else
219 while(m_noStrings > 0)
220 {
221 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
222 }
e1aae528 223#endif
9b1bd0c6
MB
224
225 if ( HasClientObjectData() )
226 m_clientDataDict.DestroyData();
227 m_noStrings = 0;
e1aae528 228 AdjustDropDownListSize();
9b1bd0c6
MB
229}
230
231void wxComboBox::SetSelection (int n)
232{
d8d18184
MB
233 m_inSetSelection = true;
234
d40708e2 235#if wxCHECK_LESSTIF()
9b1bd0c6
MB
236 XmListSelectPos (GetXmList(this), n + 1, false);
237 SetValue(GetString(n));
238#else
d40708e2 239#if 0
9b1bd0c6
MB
240 wxXmString str( GetString(n).c_str() );
241 XmComboBoxSelectItem((Widget) m_mainWidget, str());
d40708e2 242#endif
9b1bd0c6 243 XtVaSetValues( (Widget)m_mainWidget,
d40708e2 244 XmNselectedPosition, n,
9b1bd0c6
MB
245 NULL );
246#endif
d8d18184
MB
247
248 m_inSetSelection = false;
9b1bd0c6
MB
249}
250
251int wxComboBox::GetSelection (void) const
252{
253 return wxDoGetSelectionInList( GetXmList( this ) );
254}
255
256wxString wxComboBox::GetString(int n) const
257{
e1aae528 258 return wxDoGetStringInList( GetXmList(this), n );
9b1bd0c6
MB
259}
260
261int wxComboBox::FindString(const wxString& s) const
262{
263 return wxDoFindStringInList( GetXmList( this ), s );
264}
265
266// Clipboard operations
267void wxComboBox::Copy()
268{
100f9289 269 XmTextCopy( GetXmText(this), CurrentTime );
9b1bd0c6
MB
270}
271
272void wxComboBox::Cut()
273{
100f9289 274 XmTextCut( GetXmText(this), CurrentTime );
9b1bd0c6
MB
275}
276
277void wxComboBox::Paste()
278{
100f9289 279 XmTextPaste( GetXmText(this) );
9b1bd0c6
MB
280}
281
282void wxComboBox::SetEditable(bool WXUNUSED(editable))
283{
284 // TODO
285}
286
287void wxComboBox::SetInsertionPoint(long pos)
288{
100f9289 289 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
9b1bd0c6
MB
290}
291
292void wxComboBox::SetInsertionPointEnd()
293{
100f9289 294 SetInsertionPoint( GetLastPosition() );
9b1bd0c6
MB
295}
296
297long wxComboBox::GetInsertionPoint() const
298{
100f9289 299 return (long)XmTextGetInsertionPosition( GetXmText(this) );
9b1bd0c6
MB
300}
301
302long wxComboBox::GetLastPosition() const
303{
100f9289
MB
304 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
305 return (long)pos;
9b1bd0c6
MB
306}
307
308void wxComboBox::Replace(long from, long to, const wxString& value)
100f9289
MB
309{
310 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
d3a80c92 311 wxConstCast(value.c_str(), char) );
9b1bd0c6
MB
312}
313
314void wxComboBox::Remove(long from, long to)
315{
100f9289
MB
316 SetSelection( from, to );
317 XmTextRemove( GetXmText(this) );
9b1bd0c6
MB
318}
319
320void wxComboBox::SetSelection(long from, long to)
321{
100f9289
MB
322 if( to == -1 )
323 to = GetLastPosition();
324
325 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
326 (XmTextPosition)to, (Time)0 );
9b1bd0c6
MB
327}
328
329void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
330 XmComboBoxCallbackStruct * cbs)
331{
332 wxComboBox *item = (wxComboBox *) clientData;
333
d8d18184
MB
334 if( item->m_inSetSelection ) return;
335
9b1bd0c6
MB
336 switch (cbs->reason)
337 {
338 case XmCR_SELECT:
339#if 0
340 case XmCR_SINGLE_SELECT:
341 case XmCR_BROWSE_SELECT:
342#endif
343 {
344 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
345 item->GetId());
d8d18184 346 int idx = cbs->item_position;
9b1bd0c6
MB
347 event.m_commandInt = idx;
348 event.m_commandString = item->GetString (idx);
349 if ( item->HasClientObjectData() )
350 event.SetClientObject( item->GetClientObject(idx) );
351 else if ( item->HasClientUntypedData() )
352 event.SetClientData( item->GetClientData(idx) );
353 event.m_extraLong = true;
354 event.SetEventObject(item);
d8d18184 355 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
356 break;
357 }
358 case XmCR_VALUE_CHANGED:
359 {
360 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
361 event.m_commandInt = -1;
362 event.m_commandString = item->GetValue();
363 event.m_extraLong = true;
364 event.SetEventObject(item);
d8d18184 365 item->GetEventHandler()->ProcessEvent(event);
9b1bd0c6
MB
366 break;
367 }
368 default:
369 break;
370 }
371}
372
373void wxComboBox::ChangeFont(bool keepOriginalSize)
374{
e1aae528
MB
375 if( m_font.Ok() )
376 {
377 wxDoChangeFont( GetXmText(this), m_font );
378 wxDoChangeFont( GetXmList(this), m_font );
379 }
380
9b1bd0c6
MB
381 // Don't use the base class wxChoice's ChangeFont
382 wxWindow::ChangeFont(keepOriginalSize);
383}
384
385void wxComboBox::ChangeBackgroundColour()
386{
387 wxWindow::ChangeBackgroundColour();
388}
389
390void wxComboBox::ChangeForegroundColour()
391{
392 wxWindow::ChangeForegroundColour();
393}
394
395wxSize wxComboBox::DoGetBestSize() const
396{
e1aae528
MB
397 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
398 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
399 {
400 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
401
402 XtVaGetValues( (Widget)m_mainWidget,
403 XmNarrowSize, &arrowW,
404 XmNarrowSpacing, &arrowS,
405 XmNhighlightThickness, &highlight,
406 XmNmarginWidth, &xmargin,
407 XmNmarginHeight, &ymargin,
408 XmNshadowThickness, &shadow,
409 NULL );
410
411 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
412 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
413 this );
414
415 // FIXME arbitrary constants
416 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
417 + 2 * shadow + 2 * xmargin ,
418 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
419 }
420 else
421 return wxWindow::DoGetBestSize();
9b1bd0c6
MB
422}
423
424#endif // XmVersion >= 2000
425
426#endif // wxUSE_COMBOBOX