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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
72 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
73 const wxString
& value
,
76 int n
, const wxString choices
[],
78 const wxValidator
& validator
,
81 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
85 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
87 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
88 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
89 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
90 // default to wxCB_DROPDOWN
91 XmDROP_DOWN_COMBO_BOX
;
92 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
93 SetWindowStyle( style
| wxCB_DROPDOWN
);
95 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
96 xmComboBoxWidgetClass
, parentWidget
,
97 XmNcomboBoxType
, cb_type
,
100 m_mainWidget
= (Widget
) buttonWidget
;
103 for ( i
= 0; i
< n
; ++i
)
104 Append( choices
[i
] );
106 XtManageChild (buttonWidget
);
110 XtAddCallback (buttonWidget
, XmNselectionCallback
,
111 (XtCallbackProc
) wxComboBoxCallback
,
113 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
114 (XtCallbackProc
) wxComboBoxCallback
,
117 wxSize best
= GetBestSize();
118 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
119 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
122 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
123 pos
.x
, pos
.y
, best
.x
, best
.y
);
128 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
129 const wxString
& value
,
132 const wxArrayString
& choices
,
134 const wxValidator
& validator
,
135 const wxString
& name
)
137 wxCArrayString
chs(choices
);
138 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
139 chs
.GetStrings(), style
, validator
, name
);
142 void wxComboBox::AdjustDropDownListSize()
144 int newListCount
= -1, itemCount
= GetCount();
149 else if( itemCount
< MAX
)
150 newListCount
= itemCount
;
154 XtVaSetValues( GetXmList(this),
155 XmNvisibleItemCount
, newListCount
,
159 wxComboBox::~wxComboBox()
161 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
162 XtDestroyWidget((Widget
) m_mainWidget
);
163 m_mainWidget
= (WXWidget
) 0;
166 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
168 // Necessary so it doesn't call wxChoice::SetSize
169 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
172 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
175 Widget listBox
= GetXmList(this);
177 // delete the item and add it again.
178 // FIXME isn't there a way to change it in place?
179 XmListDeletePos (listBox
, n
+1);
180 XmListAddItem (listBox
, text(), n
+1);
183 void wxComboBox::SetValue(const wxString
& value
)
187 XtVaSetValues( GetXmText(this),
188 XmNvalue
, (const char*)value
.mb_str(),
191 m_inSetValue
= false;
194 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
196 void **clientData
, wxClientDataType type
)
198 const unsigned int numItems
= items
.GetCount();
200 AllocClientData(numItems
);
201 for ( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
203 wxXmString
str( items
[i
].c_str() );
204 XmComboBoxAddItem((Widget
) m_mainWidget
, str(),
205 GetMotifPosition(pos
), False
);
207 InsertNewItemClientData(pos
, clientData
, i
, type
);
210 AdjustDropDownListSize();
215 void wxComboBox::DoDeleteOneItem(unsigned int n
)
217 #ifdef LESSTIF_VERSION
218 XmListDeletePos (GetXmList(this), n
+ 1);
220 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
223 wxControlWithItems::DoDeleteOneItem(n
);
226 AdjustDropDownListSize();
229 void wxComboBox::DoClear()
231 #ifdef LESSTIF_VERSION
232 XmListDeleteAllItems (GetXmList(this));
234 while(m_noStrings
> 0)
236 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
240 wxControlWithItems::DoClear();
242 AdjustDropDownListSize();
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 (void) 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 wxComboBox::SetEditable(bool WXUNUSED(editable
))
287 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
288 XmComboBoxCallbackStruct
* cbs
)
290 wxComboBox
*item
= (wxComboBox
*) clientData
;
292 if( item
->m_inSetSelection
) return;
298 case XmCR_SINGLE_SELECT
:
299 case XmCR_BROWSE_SELECT
:
302 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
304 int idx
= cbs
->item_position
;
306 event
.SetString( item
->GetString (idx
) );
307 if ( item
->HasClientObjectData() )
308 event
.SetClientObject( item
->GetClientObject(idx
) );
309 else if ( item
->HasClientUntypedData() )
310 event
.SetClientData( item
->GetClientData(idx
) );
311 event
.SetExtraLong(true);
312 event
.SetEventObject(item
);
313 item
->GetEventHandler()->ProcessEvent(event
);
316 case XmCR_VALUE_CHANGED
:
318 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
320 event
.SetString( item
->GetValue() );
321 event
.SetExtraLong(true);
322 event
.SetEventObject(item
);
323 item
->GetEventHandler()->ProcessEvent(event
);
331 void wxComboBox::ChangeFont(bool keepOriginalSize
)
333 if( m_font
.Ok() && m_mainWidget
!= NULL
)
335 wxDoChangeFont( GetXmText(this), m_font
);
336 wxDoChangeFont( GetXmList(this), m_font
);
339 // Don't use the base class wxChoice's ChangeFont
340 wxWindow::ChangeFont(keepOriginalSize
);
343 void wxComboBox::ChangeBackgroundColour()
345 wxWindow::ChangeBackgroundColour();
348 void wxComboBox::ChangeForegroundColour()
350 wxWindow::ChangeForegroundColour();
353 wxSize
wxComboBox::DoGetBestSize() const
355 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
356 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
358 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
360 XtVaGetValues( (Widget
)m_mainWidget
,
361 XmNarrowSize
, &arrowW
,
362 XmNarrowSpacing
, &arrowS
,
363 XmNhighlightThickness
, &highlight
,
364 XmNmarginWidth
, &xmargin
,
365 XmNmarginHeight
, &ymargin
,
366 XmNshadowThickness
, &shadow
,
369 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
370 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
373 // FIXME arbitrary constants
374 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
375 + 2 * shadow
+ 2 * xmargin
,
376 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
379 return wxWindow::DoGetBestSize();
382 WXWidget
wxComboBox::GetTextWidget() const
384 return (WXWidget
)GetXmText(this);
387 #endif // XmVersion >= 2000
389 #endif // wxUSE_COMBOBOX