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
) )
84 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
86 int cb_type
= ( style
& wxCB_SIMPLE
) ? XmCOMBO_BOX
:
87 ( style
& wxCB_READONLY
) ? XmDROP_DOWN_LIST
:
88 ( style
& wxCB_DROPDOWN
) ? XmDROP_DOWN_COMBO_BOX
:
89 // default to wxCB_DROPDOWN
90 XmDROP_DOWN_COMBO_BOX
;
91 if( cb_type
== XmDROP_DOWN_COMBO_BOX
)
92 SetWindowStyle( style
| wxCB_DROPDOWN
);
94 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
95 xmComboBoxWidgetClass
, parentWidget
,
96 XmNcomboBoxType
, cb_type
,
99 m_mainWidget
= (Widget
) buttonWidget
;
102 for ( i
= 0; i
< n
; ++i
)
103 Append( choices
[i
] );
105 XtManageChild (buttonWidget
);
111 XtAddCallback (buttonWidget
, XmNselectionCallback
,
112 (XtCallbackProc
) wxComboBoxCallback
,
114 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
115 (XtCallbackProc
) wxComboBoxCallback
,
118 wxSize best
= GetBestSize();
119 if( size
.x
!= wxDefaultCoord
) best
.x
= size
.x
;
120 if( size
.y
!= wxDefaultCoord
) best
.y
= size
.y
;
122 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
123 pos
.x
, pos
.y
, best
.x
, best
.y
);
125 ChangeBackgroundColour();
130 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
131 const wxString
& value
,
134 const wxArrayString
& choices
,
136 const wxValidator
& validator
,
137 const wxString
& name
)
139 wxCArrayString
chs(choices
);
140 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
141 chs
.GetStrings(), style
, validator
, name
);
144 void wxComboBox::AdjustDropDownListSize()
146 int newListCount
= -1, itemCount
= GetCount();
151 else if( itemCount
< MAX
)
152 newListCount
= itemCount
;
156 XtVaSetValues( GetXmList(this),
157 XmNvisibleItemCount
, newListCount
,
161 wxComboBox::~wxComboBox()
163 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
164 XtDestroyWidget((Widget
) m_mainWidget
);
165 m_mainWidget
= (WXWidget
) 0;
166 if ( HasClientObjectData() )
167 m_clientDataDict
.DestroyData();
170 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
172 // Necessary so it doesn't call wxChoice::SetSize
173 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
176 wxString
wxComboBox::GetValue() const
178 char* s
= XmTextGetString (GetXmText (this));
185 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
188 Widget listBox
= GetXmList(this);
190 // delete the item and add it again.
191 // FIXME isn't there a way to change it in place?
192 XmListDeletePos (listBox
, n
+1);
193 XmListAddItem (listBox
, text(), n
+1);
196 void wxComboBox::SetValue(const wxString
& value
)
200 XtVaSetValues( GetXmText(this),
201 XmNvalue
, value
.mb_str(),
204 m_inSetValue
= false;
207 int wxComboBox::DoAppend(const wxString
& item
)
209 wxXmString
str( item
.c_str() );
210 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
212 AdjustDropDownListSize();
214 return GetCount() - 1;
217 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
219 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
220 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
222 if (pos
== GetCount())
223 return DoAppend(item
);
225 wxXmString
str( item
.c_str() );
226 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
228 AdjustDropDownListSize();
230 return GetCount() - 1;
233 void wxComboBox::Delete(unsigned int n
)
235 #ifdef LESSTIF_VERSION
236 XmListDeletePos (GetXmList(this), n
+ 1);
238 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
241 m_clientDataDict
.Delete(n
, HasClientObjectData());
244 AdjustDropDownListSize();
247 void wxComboBox::Clear()
249 #ifdef LESSTIF_VERSION
250 XmListDeleteAllItems (GetXmList(this));
252 while(m_noStrings
> 0)
254 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
258 if ( HasClientObjectData() )
259 m_clientDataDict
.DestroyData();
261 AdjustDropDownListSize();
264 void wxComboBox::SetSelection (int n
)
266 m_inSetSelection
= true;
268 #if wxCHECK_LESSTIF()
269 XmListSelectPos (GetXmList(this), n
+ 1, false);
270 SetValue(GetString(n
));
273 wxXmString
str(GetString(n
).c_str());
274 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
276 XtVaSetValues( (Widget
)m_mainWidget
,
277 XmNselectedPosition
, n
,
281 m_inSetSelection
= false;
284 int wxComboBox::GetSelection (void) const
286 return wxDoGetSelectionInList( GetXmList( this ) );
289 wxString
wxComboBox::GetString(unsigned int n
) const
291 return wxDoGetStringInList( GetXmList(this), n
);
294 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
296 // FIXME: back to base class for not supported value of bCase
298 return wxDoFindStringInList( GetXmList( this ), s
);
301 // Clipboard operations
302 void wxComboBox::Copy()
304 XmTextCopy( GetXmText(this), CurrentTime
);
307 void wxComboBox::Cut()
309 XmTextCut( GetXmText(this), CurrentTime
);
312 void wxComboBox::Paste()
314 XmTextPaste( GetXmText(this) );
317 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
322 void wxComboBox::SetInsertionPoint(long pos
)
324 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
327 void wxComboBox::SetInsertionPointEnd()
329 SetInsertionPoint( GetLastPosition() );
332 long wxComboBox::GetInsertionPoint() const
334 return (long)XmTextGetInsertionPosition( GetXmText(this) );
337 wxTextPos
wxComboBox::GetLastPosition() const
339 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
343 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
345 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
346 wxConstCast(value
.mb_str(), char) );
349 void wxComboBox::Remove(long from
, long to
)
351 SetSelection( from
, to
);
352 XmTextRemove( GetXmText(this) );
355 void wxComboBox::SetSelection(long from
, long to
)
358 to
= GetLastPosition();
360 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
361 (XmTextPosition
)to
, (Time
)0 );
364 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
365 XmComboBoxCallbackStruct
* cbs
)
367 wxComboBox
*item
= (wxComboBox
*) clientData
;
369 if( item
->m_inSetSelection
) return;
375 case XmCR_SINGLE_SELECT
:
376 case XmCR_BROWSE_SELECT
:
379 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
381 int idx
= cbs
->item_position
;
383 event
.SetString( item
->GetString (idx
) );
384 if ( item
->HasClientObjectData() )
385 event
.SetClientObject( item
->GetClientObject(idx
) );
386 else if ( item
->HasClientUntypedData() )
387 event
.SetClientData( item
->GetClientData(idx
) );
388 event
.SetExtraLong(true);
389 event
.SetEventObject(item
);
390 item
->GetEventHandler()->ProcessEvent(event
);
393 case XmCR_VALUE_CHANGED
:
395 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
397 event
.SetString( item
->GetValue() );
398 event
.SetExtraLong(true);
399 event
.SetEventObject(item
);
400 item
->GetEventHandler()->ProcessEvent(event
);
408 void wxComboBox::ChangeFont(bool keepOriginalSize
)
412 wxDoChangeFont( GetXmText(this), m_font
);
413 wxDoChangeFont( GetXmList(this), m_font
);
416 // Don't use the base class wxChoice's ChangeFont
417 wxWindow::ChangeFont(keepOriginalSize
);
420 void wxComboBox::ChangeBackgroundColour()
422 wxWindow::ChangeBackgroundColour();
425 void wxComboBox::ChangeForegroundColour()
427 wxWindow::ChangeForegroundColour();
430 wxSize
wxComboBox::DoGetBestSize() const
432 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
433 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
435 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
437 XtVaGetValues( (Widget
)m_mainWidget
,
438 XmNarrowSize
, &arrowW
,
439 XmNarrowSpacing
, &arrowS
,
440 XmNhighlightThickness
, &highlight
,
441 XmNmarginWidth
, &xmargin
,
442 XmNmarginHeight
, &ymargin
,
443 XmNshadowThickness
, &shadow
,
446 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
447 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
450 // FIXME arbitrary constants
451 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
452 + 2 * shadow
+ 2 * xmargin
,
453 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
456 return wxWindow::DoGetBestSize();
459 #endif // XmVersion >= 2000
461 #endif // wxUSE_COMBOBOX