1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox_native.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart, Ian Brown
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/combobox.h"
19 #pragma message disable nosimpint
23 #pragma message enable nosimpint
26 // use the new, shiny combobox for Motif 2.x
27 #if (XmVersion >= 2000)
30 #pragma message disable nosimpint
32 #include <Xm/ComboBox.h>
36 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
42 static Widget
GetXmList( const wxComboBox
* cb
)
45 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
52 static Widget
GetXmText( const wxComboBox
* cb
)
55 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
62 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
63 XmComboBoxCallbackStruct
* cbs
);
65 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
67 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
68 const wxString
& value
,
71 int n
, const wxString choices
[],
73 const wxValidator
& validator
,
76 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
79 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
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
;
86 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
87 SetWindowStyle( style
| wxCB_DROPDOWN
);
89 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
90 xmComboBoxWidgetClass
, parentWidget
,
91 XmNcomboBoxType
, cb_type
,
94 m_mainWidget
= (Widget
) buttonWidget
;
97 for ( i
= 0; i
< n
; ++i
)
100 XtManageChild (buttonWidget
);
106 XtAddCallback (buttonWidget
, XmNselectionCallback
,
107 (XtCallbackProc
) wxComboBoxCallback
,
109 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
110 (XtCallbackProc
) wxComboBoxCallback
,
113 wxSize best
= GetBestSize();
114 if( size
.x
!= -1 ) best
.x
= size
.x
;
115 if( size
.y
!= -1 ) best
.y
= size
.y
;
117 SetCanAddEventHandler(true);
118 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
119 pos
.x
, pos
.y
, best
.x
, best
.y
);
121 ChangeBackgroundColour();
126 void wxComboBox::AdjustDropDownListSize()
128 int newListCount
= -1, itemCount
= GetCount();
133 else if( itemCount
< MAX
)
134 newListCount
= itemCount
;
138 XtVaSetValues( GetXmList(this),
139 XmNvisibleItemCount
, newListCount
,
143 wxComboBox::~wxComboBox()
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();
152 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
154 // Necessary so it doesn't call wxChoice::SetSize
155 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
158 wxString
wxComboBox::GetValue() const
160 char* s
= XmTextGetString (GetXmText (this));
167 void wxComboBox::SetString(int n
, const wxString
& s
)
170 Widget listBox
= GetXmList(this);
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);
178 void wxComboBox::SetValue(const wxString
& value
)
182 XtVaSetValues( GetXmText(this),
183 XmNvalue
, wxConstCast(value
.c_str(), char),
186 m_inSetValue
= false;
189 int wxComboBox::DoAppend(const wxString
& item
)
191 wxXmString
str( item
.c_str() );
192 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
194 AdjustDropDownListSize();
196 return GetCount() - 1;
199 void wxComboBox::Delete(int n
)
201 #ifdef LESSTIF_VERSION
202 XmListDeletePos (GetXmList(this), n
+ 1);
204 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
207 m_clientDataDict
.Delete(n
, HasClientObjectData());
210 AdjustDropDownListSize();
213 void wxComboBox::Clear()
215 #ifdef LESSTIF_VERSION
216 XmListDeleteAllItems (GetXmList(this));
218 while(m_noStrings
> 0)
220 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
224 if ( HasClientObjectData() )
225 m_clientDataDict
.DestroyData();
227 AdjustDropDownListSize();
230 void wxComboBox::SetSelection (int n
)
232 #if wxCHECK_LESSTIF()
233 XmListSelectPos (GetXmList(this), n
+ 1, false);
234 SetValue(GetString(n
));
237 wxXmString
str( GetString(n
).c_str() );
238 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
240 XtVaSetValues( (Widget
)m_mainWidget
,
241 XmNselectedPosition
, n
,
246 int wxComboBox::GetSelection (void) const
248 return wxDoGetSelectionInList( GetXmList( this ) );
251 wxString
wxComboBox::GetString(int n
) const
253 return wxDoGetStringInList( GetXmList(this), n
);
256 int wxComboBox::FindString(const wxString
& s
) const
258 return wxDoFindStringInList( GetXmList( this ), s
);
261 // Clipboard operations
262 void wxComboBox::Copy()
264 XmTextCopy( GetXmText(this), CurrentTime
);
267 void wxComboBox::Cut()
269 XmTextCut( GetXmText(this), CurrentTime
);
272 void wxComboBox::Paste()
274 XmTextPaste( GetXmText(this) );
277 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
282 void wxComboBox::SetInsertionPoint(long pos
)
284 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
287 void wxComboBox::SetInsertionPointEnd()
289 SetInsertionPoint( GetLastPosition() );
292 long wxComboBox::GetInsertionPoint() const
294 return (long)XmTextGetInsertionPosition( GetXmText(this) );
297 long wxComboBox::GetLastPosition() const
299 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
303 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
305 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
306 wxConstCast(value
.c_str(), char) );
309 void wxComboBox::Remove(long from
, long to
)
311 SetSelection( from
, to
);
312 XmTextRemove( GetXmText(this) );
315 void wxComboBox::SetSelection(long from
, long to
)
318 to
= GetLastPosition();
320 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
321 (XmTextPosition
)to
, (Time
)0 );
324 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
325 XmComboBoxCallbackStruct
* cbs
)
327 wxComboBox
*item
= (wxComboBox
*) clientData
;
333 case XmCR_SINGLE_SELECT
:
334 case XmCR_BROWSE_SELECT
:
337 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
339 int idx
= cbs
->item_position
- 1;
340 event
.m_commandInt
= idx
;
341 event
.m_commandString
= item
->GetString (idx
);
342 if ( item
->HasClientObjectData() )
343 event
.SetClientObject( item
->GetClientObject(idx
) );
344 else if ( item
->HasClientUntypedData() )
345 event
.SetClientData( item
->GetClientData(idx
) );
346 event
.m_extraLong
= true;
347 event
.SetEventObject(item
);
348 item
->ProcessCommand (event
);
351 case XmCR_VALUE_CHANGED
:
353 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
354 event
.m_commandInt
= -1;
355 event
.m_commandString
= item
->GetValue();
356 event
.m_extraLong
= true;
357 event
.SetEventObject(item
);
358 item
->ProcessCommand (event
);
366 void wxComboBox::ChangeFont(bool keepOriginalSize
)
370 wxDoChangeFont( GetXmText(this), m_font
);
371 wxDoChangeFont( GetXmList(this), m_font
);
374 // Don't use the base class wxChoice's ChangeFont
375 wxWindow::ChangeFont(keepOriginalSize
);
378 void wxComboBox::ChangeBackgroundColour()
380 wxWindow::ChangeBackgroundColour();
383 void wxComboBox::ChangeForegroundColour()
385 wxWindow::ChangeForegroundColour();
388 wxSize
wxComboBox::DoGetBestSize() const
390 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
391 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
393 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
395 XtVaGetValues( (Widget
)m_mainWidget
,
396 XmNarrowSize
, &arrowW
,
397 XmNarrowSpacing
, &arrowS
,
398 XmNhighlightThickness
, &highlight
,
399 XmNmarginWidth
, &xmargin
,
400 XmNmarginHeight
, &ymargin
,
401 XmNshadowThickness
, &shadow
,
404 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
405 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
408 // FIXME arbitrary constants
409 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
410 + 2 * shadow
+ 2 * xmargin
,
411 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
414 return wxWindow::DoGetBestSize();
417 #endif // XmVersion >= 2000
419 #endif // wxUSE_COMBOBOX