1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox_native.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart, Ian Brown
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/combobox.h"
20 #include "wx/arrstr.h"
24 #pragma message disable nosimpint
28 #pragma message enable nosimpint
31 // use the new, shiny combobox for Motif 2.x
32 #if (XmVersion >= 2000)
35 #pragma message disable nosimpint
37 #include <Xm/ComboBox.h>
41 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
47 static Widget
GetXmList( const wxComboBox
* cb
)
50 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
57 static Widget
GetXmText( const wxComboBox
* cb
)
60 XtVaGetValues( (Widget
)cb
->GetMainWidget(),
67 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
68 XmComboBoxCallbackStruct
* cbs
);
70 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxString
& value
,
74 int n
, const wxString choices
[],
76 const wxValidator
& validator
,
79 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
83 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
85 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
86 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
87 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
88 // default to wxCB_DROPDOWN
89 XmDROP_DOWN_COMBO_BOX
;
90 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
91 SetWindowStyle( style
| wxCB_DROPDOWN
);
93 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
94 xmComboBoxWidgetClass
, parentWidget
,
95 XmNcomboBoxType
, cb_type
,
98 m_mainWidget
= (Widget
) buttonWidget
;
101 for ( i
= 0; i
< n
; ++i
)
102 Append( choices
[i
] );
104 XtManageChild (buttonWidget
);
108 XtAddCallback (buttonWidget
, XmNselectionCallback
,
109 (XtCallbackProc
) wxComboBoxCallback
,
111 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
112 (XtCallbackProc
) wxComboBoxCallback
,
115 wxSize best
= GetBestSize();
116 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
117 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
120 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
121 pos
.x
, pos
.y
, best
.x
, best
.y
);
126 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
127 const wxString
& value
,
130 const wxArrayString
& choices
,
132 const wxValidator
& validator
,
133 const wxString
& name
)
135 wxCArrayString
chs(choices
);
136 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
137 chs
.GetStrings(), style
, validator
, name
);
140 void wxComboBox::AdjustDropDownListSize()
142 int newListCount
= -1, itemCount
= GetCount();
147 else if( itemCount
< MAX
)
148 newListCount
= itemCount
;
152 XtVaSetValues( GetXmList(this),
153 XmNvisibleItemCount
, newListCount
,
157 wxComboBox::~wxComboBox()
159 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
160 XtDestroyWidget((Widget
) m_mainWidget
);
161 m_mainWidget
= (WXWidget
) 0;
164 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
166 // Necessary so it doesn't call wxChoice::SetSize
167 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
170 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
173 Widget listBox
= GetXmList(this);
175 // delete the item and add it again.
176 // FIXME isn't there a way to change it in place?
177 XmListDeletePos (listBox
, n
+1);
178 XmListAddItem (listBox
, text(), n
+1);
181 void wxComboBox::SetValue(const wxString
& value
)
185 XtVaSetValues( GetXmText(this),
186 XmNvalue
, (const char*)value
.mb_str(),
189 m_inSetValue
= false;
192 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
194 void **clientData
, wxClientDataType type
)
196 const unsigned int numItems
= items
.GetCount();
198 AllocClientData(numItems
);
199 for ( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
201 wxXmString
str( items
[i
].c_str() );
202 XmComboBoxAddItem((Widget
) m_mainWidget
, str(),
203 GetMotifPosition(pos
), False
);
205 InsertNewItemClientData(pos
, clientData
, i
, type
);
208 AdjustDropDownListSize();
213 void wxComboBox::DoDeleteOneItem(unsigned int n
)
215 #ifdef LESSTIF_VERSION
216 XmListDeletePos (GetXmList(this), n
+ 1);
218 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
221 wxControlWithItems::DoDeleteOneItem(n
);
222 m_stringArray
.RemoveAt(size_t(n
));
224 AdjustDropDownListSize();
227 void wxComboBox::Clear()
229 #ifdef LESSTIF_VERSION
230 XmListDeleteAllItems (GetXmList(this));
232 size_t n
= m_stringArray
.GetCount();
235 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
--);
239 m_stringArray
.Clear();
240 AdjustDropDownListSize();
242 wxTextEntry::Clear();
245 void wxComboBox::SetSelection (int n
)
247 m_inSetSelection
= true;
249 #if wxCHECK_LESSTIF()
250 XmListSelectPos (GetXmList(this), n
+ 1, false);
251 SetValue(GetString(n
));
254 wxXmString
str(GetString(n
).c_str());
255 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
257 XtVaSetValues( (Widget
)m_mainWidget
,
258 XmNselectedPosition
, n
,
262 m_inSetSelection
= false;
265 int wxComboBox::GetSelection() const
267 return wxDoGetSelectionInList( GetXmList( this ) );
270 wxString
wxComboBox::GetString(unsigned int n
) const
272 return wxDoGetStringInList( GetXmList(this), n
);
275 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
277 // FIXME: back to base class for not supported value of bCase
279 return wxDoFindStringInList( GetXmList( this ), s
);
282 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
283 XmComboBoxCallbackStruct
* cbs
)
285 wxComboBox
*item
= (wxComboBox
*) clientData
;
287 if( item
->m_inSetSelection
) return;
293 case XmCR_SINGLE_SELECT
:
294 case XmCR_BROWSE_SELECT
:
297 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
299 int idx
= cbs
->item_position
;
301 event
.SetString( item
->GetString (idx
) );
302 if ( item
->HasClientObjectData() )
303 event
.SetClientObject( item
->GetClientObject(idx
) );
304 else if ( item
->HasClientUntypedData() )
305 event
.SetClientData( item
->GetClientData(idx
) );
306 event
.SetExtraLong(true);
307 event
.SetEventObject(item
);
308 item
->HandleWindowEvent(event
);
311 case XmCR_VALUE_CHANGED
:
313 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
315 event
.SetString( item
->GetValue() );
316 event
.SetExtraLong(true);
317 event
.SetEventObject(item
);
318 item
->HandleWindowEvent(event
);
326 void wxComboBox::ChangeFont(bool keepOriginalSize
)
328 if( m_font
.IsOk() && m_mainWidget
!= NULL
)
330 wxDoChangeFont( GetXmText(this), m_font
);
331 wxDoChangeFont( GetXmList(this), m_font
);
334 // Don't use the base class wxChoice's ChangeFont
335 wxWindow::ChangeFont(keepOriginalSize
);
338 void wxComboBox::ChangeBackgroundColour()
340 wxWindow::ChangeBackgroundColour();
343 void wxComboBox::ChangeForegroundColour()
345 wxWindow::ChangeForegroundColour();
348 wxSize
wxComboBox::DoGetBestSize() const
350 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
351 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
353 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
355 XtVaGetValues( (Widget
)m_mainWidget
,
356 XmNarrowSize
, &arrowW
,
357 XmNarrowSpacing
, &arrowS
,
358 XmNhighlightThickness
, &highlight
,
359 XmNmarginWidth
, &xmargin
,
360 XmNmarginHeight
, &ymargin
,
361 XmNshadowThickness
, &shadow
,
364 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
365 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
368 // FIXME arbitrary constants
369 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
370 + 2 * shadow
+ 2 * xmargin
,
371 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
374 return wxWindow::DoGetBestSize();
377 WXWidget
wxComboBox::GetTextWidget() const
379 return (WXWidget
)GetXmText(this);
382 #endif // XmVersion >= 2000
384 #endif // wxUSE_COMBOBOX