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 // Fix crash; probably an OpenMotif bug
201 const char* val
= value
.c_str() ? value
.c_str() : "";
202 XtVaSetValues( GetXmText(this),
203 XmNvalue
, wxConstCast(val
, char),
206 m_inSetValue
= false;
209 int wxComboBox::DoAppend(const wxString
& item
)
211 wxXmString
str( item
.c_str() );
212 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
214 AdjustDropDownListSize();
216 return GetCount() - 1;
219 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
221 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
222 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
224 if (pos
== GetCount())
225 return DoAppend(item
);
227 wxXmString
str( item
.c_str() );
228 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
230 AdjustDropDownListSize();
232 return GetCount() - 1;
235 void wxComboBox::Delete(unsigned int n
)
237 #ifdef LESSTIF_VERSION
238 XmListDeletePos (GetXmList(this), n
+ 1);
240 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
243 m_clientDataDict
.Delete(n
, HasClientObjectData());
246 AdjustDropDownListSize();
249 void wxComboBox::Clear()
251 #ifdef LESSTIF_VERSION
252 XmListDeleteAllItems (GetXmList(this));
254 while(m_noStrings
> 0)
256 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
260 if ( HasClientObjectData() )
261 m_clientDataDict
.DestroyData();
263 AdjustDropDownListSize();
266 void wxComboBox::SetSelection (int n
)
268 m_inSetSelection
= true;
270 #if wxCHECK_LESSTIF()
271 XmListSelectPos (GetXmList(this), n
+ 1, false);
272 SetValue(GetString(n
));
275 wxXmString
str(GetString(n
).c_str());
276 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
278 XtVaSetValues( (Widget
)m_mainWidget
,
279 XmNselectedPosition
, n
,
283 m_inSetSelection
= false;
286 int wxComboBox::GetSelection (void) const
288 return wxDoGetSelectionInList( GetXmList( this ) );
291 wxString
wxComboBox::GetString(unsigned int n
) const
293 return wxDoGetStringInList( GetXmList(this), n
);
296 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
298 // FIXME: back to base class for not supported value of bCase
300 return wxDoFindStringInList( GetXmList( this ), s
);
303 // Clipboard operations
304 void wxComboBox::Copy()
306 XmTextCopy( GetXmText(this), CurrentTime
);
309 void wxComboBox::Cut()
311 XmTextCut( GetXmText(this), CurrentTime
);
314 void wxComboBox::Paste()
316 XmTextPaste( GetXmText(this) );
319 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
324 void wxComboBox::SetInsertionPoint(long pos
)
326 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
329 void wxComboBox::SetInsertionPointEnd()
331 SetInsertionPoint( GetLastPosition() );
334 long wxComboBox::GetInsertionPoint() const
336 return (long)XmTextGetInsertionPosition( GetXmText(this) );
339 wxTextPos
wxComboBox::GetLastPosition() const
341 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
345 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
347 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
348 wxConstCast(value
.c_str(), char) );
351 void wxComboBox::Remove(long from
, long to
)
353 SetSelection( from
, to
);
354 XmTextRemove( GetXmText(this) );
357 void wxComboBox::SetSelection(long from
, long to
)
360 to
= GetLastPosition();
362 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
363 (XmTextPosition
)to
, (Time
)0 );
366 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
367 XmComboBoxCallbackStruct
* cbs
)
369 wxComboBox
*item
= (wxComboBox
*) clientData
;
371 if( item
->m_inSetSelection
) return;
377 case XmCR_SINGLE_SELECT
:
378 case XmCR_BROWSE_SELECT
:
381 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
383 int idx
= cbs
->item_position
;
385 event
.SetString( item
->GetString (idx
) );
386 if ( item
->HasClientObjectData() )
387 event
.SetClientObject( item
->GetClientObject(idx
) );
388 else if ( item
->HasClientUntypedData() )
389 event
.SetClientData( item
->GetClientData(idx
) );
390 event
.SetExtraLong(true);
391 event
.SetEventObject(item
);
392 item
->GetEventHandler()->ProcessEvent(event
);
395 case XmCR_VALUE_CHANGED
:
397 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
399 event
.SetString( item
->GetValue() );
400 event
.SetExtraLong(true);
401 event
.SetEventObject(item
);
402 item
->GetEventHandler()->ProcessEvent(event
);
410 void wxComboBox::ChangeFont(bool keepOriginalSize
)
414 wxDoChangeFont( GetXmText(this), m_font
);
415 wxDoChangeFont( GetXmList(this), m_font
);
418 // Don't use the base class wxChoice's ChangeFont
419 wxWindow::ChangeFont(keepOriginalSize
);
422 void wxComboBox::ChangeBackgroundColour()
424 wxWindow::ChangeBackgroundColour();
427 void wxComboBox::ChangeForegroundColour()
429 wxWindow::ChangeForegroundColour();
432 wxSize
wxComboBox::DoGetBestSize() const
434 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
435 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
437 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
439 XtVaGetValues( (Widget
)m_mainWidget
,
440 XmNarrowSize
, &arrowW
,
441 XmNarrowSpacing
, &arrowS
,
442 XmNhighlightThickness
, &highlight
,
443 XmNmarginWidth
, &xmargin
,
444 XmNmarginHeight
, &ymargin
,
445 XmNshadowThickness
, &shadow
,
448 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
449 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
452 // FIXME arbitrary constants
453 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
454 + 2 * shadow
+ 2 * xmargin
,
455 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
458 return wxWindow::DoGetBestSize();
461 #endif // XmVersion >= 2000
463 #endif // wxUSE_COMBOBOX