]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox_native.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart, Ian Brown
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/combobox.h"
19 #include "wx/arrstr.h"
23 #pragma message disable nosimpint
27 #pragma message enable nosimpint
30 // use the new, shiny combobox for Motif 2.x
31 #if (XmVersion >= 2000)
34 #pragma message disable nosimpint
36 #include <Xm/ComboBox.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
46 static Widget
GetXmList( const wxComboBox
* cb
)
49 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
56 static Widget
GetXmText( const wxComboBox
* cb
)
59 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
66 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
67 XmComboBoxCallbackStruct
* cbs
);
69 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
70 const wxString
& value
,
73 int n
, const wxString choices
[],
75 const wxValidator
& validator
,
78 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
82 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
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
;
89 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
90 SetWindowStyle( style
| wxCB_DROPDOWN
);
92 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
93 xmComboBoxWidgetClass
, parentWidget
,
94 XmNcomboBoxType
, cb_type
,
97 m_mainWidget
= (Widget
) buttonWidget
;
100 for ( i
= 0; i
< n
; ++i
)
101 Append( choices
[i
] );
103 XtManageChild (buttonWidget
);
107 XtAddCallback (buttonWidget
, XmNselectionCallback
,
108 (XtCallbackProc
) wxComboBoxCallback
,
110 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
111 (XtCallbackProc
) wxComboBoxCallback
,
114 wxSize best
= GetBestSize();
115 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
116 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
119 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
120 pos
.x
, pos
.y
, best
.x
, best
.y
);
125 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
126 const wxString
& value
,
129 const wxArrayString
& choices
,
131 const wxValidator
& validator
,
132 const wxString
& name
)
134 wxCArrayString
chs(choices
);
135 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
136 chs
.GetStrings(), style
, validator
, name
);
139 void wxComboBox::AdjustDropDownListSize()
141 int newListCount
= -1, itemCount
= GetCount();
146 else if( itemCount
< MAX
)
147 newListCount
= itemCount
;
151 XtVaSetValues( GetXmList(this),
152 XmNvisibleItemCount
, newListCount
,
156 wxComboBox::~wxComboBox()
158 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
159 XtDestroyWidget((Widget
) m_mainWidget
);
160 m_mainWidget
= (WXWidget
) 0;
163 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
165 // Necessary so it doesn't call wxChoice::SetSize
166 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
169 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
172 Widget listBox
= GetXmList(this);
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);
180 void wxComboBox::SetValue(const wxString
& value
)
184 XtVaSetValues( GetXmText(this),
185 XmNvalue
, (const char*)value
.mb_str(),
188 m_inSetValue
= false;
191 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
193 void **clientData
, wxClientDataType type
)
195 const unsigned int numItems
= items
.GetCount();
197 AllocClientData(numItems
);
198 for ( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
200 wxXmString
str( items
[i
].c_str() );
201 XmComboBoxAddItem((Widget
) m_mainWidget
, str(),
202 GetMotifPosition(pos
), False
);
204 InsertNewItemClientData(pos
, clientData
, i
, type
);
207 AdjustDropDownListSize();
212 void wxComboBox::DoDeleteOneItem(unsigned int n
)
214 #ifdef LESSTIF_VERSION
215 XmListDeletePos (GetXmList(this), n
+ 1);
217 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
220 wxControlWithItems::DoDeleteOneItem(n
);
221 m_stringArray
.RemoveAt(size_t(n
));
223 AdjustDropDownListSize();
226 void wxComboBox::Clear()
228 #ifdef LESSTIF_VERSION
229 XmListDeleteAllItems (GetXmList(this));
231 size_t n
= m_stringArray
.GetCount();
234 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
--);
238 m_stringArray
.Clear();
239 AdjustDropDownListSize();
241 wxTextEntry::Clear();
244 void wxComboBox::SetSelection (int n
)
246 m_inSetSelection
= true;
248 #if wxCHECK_LESSTIF()
249 XmListSelectPos (GetXmList(this), n
+ 1, false);
250 SetValue(GetString(n
));
253 wxXmString
str(GetString(n
).c_str());
254 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
256 XtVaSetValues( (Widget
)m_mainWidget
,
257 XmNselectedPosition
, n
,
261 m_inSetSelection
= false;
264 int wxComboBox::GetSelection() const
266 return wxDoGetSelectionInList( GetXmList( this ) );
269 wxString
wxComboBox::GetString(unsigned int n
) const
271 return wxDoGetStringInList( GetXmList(this), n
);
274 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
276 // FIXME: back to base class for not supported value of bCase
278 return wxDoFindStringInList( GetXmList( this ), s
);
281 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
282 XmComboBoxCallbackStruct
* cbs
)
284 wxComboBox
*item
= (wxComboBox
*) clientData
;
286 if( item
->m_inSetSelection
) return;
292 case XmCR_SINGLE_SELECT
:
293 case XmCR_BROWSE_SELECT
:
296 wxCommandEvent
event (wxEVT_COMBOBOX
,
298 int idx
= cbs
->item_position
;
300 event
.SetString( item
->GetString (idx
) );
301 if ( item
->HasClientObjectData() )
302 event
.SetClientObject( item
->GetClientObject(idx
) );
303 else if ( item
->HasClientUntypedData() )
304 event
.SetClientData( item
->GetClientData(idx
) );
305 event
.SetExtraLong(true);
306 event
.SetEventObject(item
);
307 item
->HandleWindowEvent(event
);
310 case XmCR_VALUE_CHANGED
:
312 wxCommandEvent
event (wxEVT_TEXT
, item
->GetId());
314 event
.SetString( item
->GetValue() );
315 event
.SetExtraLong(true);
316 event
.SetEventObject(item
);
317 item
->HandleWindowEvent(event
);
325 void wxComboBox::ChangeFont(bool keepOriginalSize
)
327 if( m_font
.IsOk() && m_mainWidget
!= NULL
)
329 wxDoChangeFont( GetXmText(this), m_font
);
330 wxDoChangeFont( GetXmList(this), m_font
);
333 // Don't use the base class wxChoice's ChangeFont
334 wxWindow::ChangeFont(keepOriginalSize
);
337 void wxComboBox::ChangeBackgroundColour()
339 wxWindow::ChangeBackgroundColour();
342 void wxComboBox::ChangeForegroundColour()
344 wxWindow::ChangeForegroundColour();
347 wxSize
wxComboBox::DoGetBestSize() const
349 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
350 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
352 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
354 XtVaGetValues( (Widget
)m_mainWidget
,
355 XmNarrowSize
, &arrowW
,
356 XmNarrowSpacing
, &arrowS
,
357 XmNhighlightThickness
, &highlight
,
358 XmNmarginWidth
, &xmargin
,
359 XmNmarginHeight
, &ymargin
,
360 XmNshadowThickness
, &shadow
,
363 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
364 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
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
);
373 return wxWindow::DoGetBestSize();
376 WXWidget
wxComboBox::GetTextWidget() const
378 return (WXWidget
)GetXmText(this);
381 #endif // XmVersion >= 2000
383 #endif // wxUSE_COMBOBOX