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"
19 #include "wx/combobox.h"
20 #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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
71 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxString
& value
,
75 int n
, const wxString choices
[],
77 const wxValidator
& validator
,
80 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
);
110 XtAddCallback (buttonWidget
, XmNselectionCallback
,
111 (XtCallbackProc
) wxComboBoxCallback
,
113 XtAddCallback (GetXmText(this), XmNvalueChangedCallback
,
114 (XtCallbackProc
) wxComboBoxCallback
,
117 wxSize best
= GetBestSize();
118 if( size
.x
!= -1 ) best
.x
= size
.x
;
119 if( size
.y
!= -1 ) best
.y
= size
.y
;
121 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
122 pos
.x
, pos
.y
, best
.x
, best
.y
);
124 ChangeBackgroundColour();
129 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
130 const wxString
& value
,
133 const wxArrayString
& choices
,
135 const wxValidator
& validator
,
136 const wxString
& name
)
138 wxCArrayString
chs(choices
);
139 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
140 chs
.GetStrings(), style
, validator
, name
);
143 void wxComboBox::AdjustDropDownListSize()
145 int newListCount
= -1, itemCount
= GetCount();
150 else if( itemCount
< MAX
)
151 newListCount
= itemCount
;
155 XtVaSetValues( GetXmList(this),
156 XmNvisibleItemCount
, newListCount
,
160 wxComboBox::~wxComboBox()
162 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
163 XtDestroyWidget((Widget
) m_mainWidget
);
164 m_mainWidget
= (WXWidget
) 0;
165 if ( HasClientObjectData() )
166 m_clientDataDict
.DestroyData();
169 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
171 // Necessary so it doesn't call wxChoice::SetSize
172 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
175 wxString
wxComboBox::GetValue() const
177 char* s
= XmTextGetString (GetXmText (this));
184 void wxComboBox::SetString(int n
, const wxString
& s
)
187 Widget listBox
= GetXmList(this);
189 // delete the item and add it again.
190 // FIXME isn't there a way to change it in place?
191 XmListDeletePos (listBox
, n
+1);
192 XmListAddItem (listBox
, text(), n
+1);
195 void wxComboBox::SetValue(const wxString
& value
)
199 // Fix crash; probably an OpenMotif bug
200 const char* val
= value
.c_str() ? value
.c_str() : "";
201 XtVaSetValues( GetXmText(this),
202 XmNvalue
, wxConstCast(val
, char),
205 m_inSetValue
= false;
208 int wxComboBox::DoAppend(const wxString
& item
)
210 wxXmString
str( item
.c_str() );
211 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
213 AdjustDropDownListSize();
215 return GetCount() - 1;
218 int wxComboBox::DoInsert(const wxString
& item
, int pos
)
220 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
221 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
223 if (pos
== GetCount())
224 return DoAppend(item
);
226 wxXmString
str( item
.c_str() );
227 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
229 AdjustDropDownListSize();
231 return GetCount() - 1;
234 void wxComboBox::Delete(int n
)
236 #ifdef LESSTIF_VERSION
237 XmListDeletePos (GetXmList(this), n
+ 1);
239 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
242 m_clientDataDict
.Delete(n
, HasClientObjectData());
245 AdjustDropDownListSize();
248 void wxComboBox::Clear()
250 #ifdef LESSTIF_VERSION
251 XmListDeleteAllItems (GetXmList(this));
253 while(m_noStrings
> 0)
255 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
259 if ( HasClientObjectData() )
260 m_clientDataDict
.DestroyData();
262 AdjustDropDownListSize();
265 void wxComboBox::SetSelection (int n
)
267 m_inSetSelection
= true;
269 #if wxCHECK_LESSTIF()
270 XmListSelectPos (GetXmList(this), n
+ 1, false);
271 SetValue(GetString(n
));
274 wxXmString
str( GetString(n
).c_str() );
275 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
277 XtVaSetValues( (Widget
)m_mainWidget
,
278 XmNselectedPosition
, n
,
282 m_inSetSelection
= false;
285 int wxComboBox::GetSelection (void) const
287 return wxDoGetSelectionInList( GetXmList( this ) );
290 wxString
wxComboBox::GetString(int n
) const
292 return wxDoGetStringInList( GetXmList(this), n
);
295 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
297 // FIXME: back to base class for not supported value of bCase
299 return wxDoFindStringInList( GetXmList( this ), s
);
302 // Clipboard operations
303 void wxComboBox::Copy()
305 XmTextCopy( GetXmText(this), CurrentTime
);
308 void wxComboBox::Cut()
310 XmTextCut( GetXmText(this), CurrentTime
);
313 void wxComboBox::Paste()
315 XmTextPaste( GetXmText(this) );
318 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
323 void wxComboBox::SetInsertionPoint(long pos
)
325 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
328 void wxComboBox::SetInsertionPointEnd()
330 SetInsertionPoint( GetLastPosition() );
333 long wxComboBox::GetInsertionPoint() const
335 return (long)XmTextGetInsertionPosition( GetXmText(this) );
338 wxTextPos
wxComboBox::GetLastPosition() const
340 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
344 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
346 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
347 wxConstCast(value
.c_str(), char) );
350 void wxComboBox::Remove(long from
, long to
)
352 SetSelection( from
, to
);
353 XmTextRemove( GetXmText(this) );
356 void wxComboBox::SetSelection(long from
, long to
)
359 to
= GetLastPosition();
361 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
362 (XmTextPosition
)to
, (Time
)0 );
365 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
366 XmComboBoxCallbackStruct
* cbs
)
368 wxComboBox
*item
= (wxComboBox
*) clientData
;
370 if( item
->m_inSetSelection
) return;
376 case XmCR_SINGLE_SELECT
:
377 case XmCR_BROWSE_SELECT
:
380 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
382 int idx
= cbs
->item_position
;
384 event
.SetString( item
->GetString (idx
) );
385 if ( item
->HasClientObjectData() )
386 event
.SetClientObject( item
->GetClientObject(idx
) );
387 else if ( item
->HasClientUntypedData() )
388 event
.SetClientData( item
->GetClientData(idx
) );
389 event
.SetExtraLong(true);
390 event
.SetEventObject(item
);
391 item
->GetEventHandler()->ProcessEvent(event
);
394 case XmCR_VALUE_CHANGED
:
396 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
398 event
.SetString( item
->GetValue() );
399 event
.SetExtraLong(true);
400 event
.SetEventObject(item
);
401 item
->GetEventHandler()->ProcessEvent(event
);
409 void wxComboBox::ChangeFont(bool keepOriginalSize
)
413 wxDoChangeFont( GetXmText(this), m_font
);
414 wxDoChangeFont( GetXmList(this), m_font
);
417 // Don't use the base class wxChoice's ChangeFont
418 wxWindow::ChangeFont(keepOriginalSize
);
421 void wxComboBox::ChangeBackgroundColour()
423 wxWindow::ChangeBackgroundColour();
426 void wxComboBox::ChangeForegroundColour()
428 wxWindow::ChangeForegroundColour();
431 wxSize
wxComboBox::DoGetBestSize() const
433 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
434 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
436 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
438 XtVaGetValues( (Widget
)m_mainWidget
,
439 XmNarrowSize
, &arrowW
,
440 XmNarrowSpacing
, &arrowS
,
441 XmNhighlightThickness
, &highlight
,
442 XmNmarginWidth
, &xmargin
,
443 XmNmarginHeight
, &ymargin
,
444 XmNshadowThickness
, &shadow
,
447 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
448 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
451 // FIXME arbitrary constants
452 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
453 + 2 * shadow
+ 2 * xmargin
,
454 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
457 return wxWindow::DoGetBestSize();
460 #endif // XmVersion >= 2000
462 #endif // wxUSE_COMBOBOX