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;
164 if ( HasClientObjectData() )
165 m_clientDataDict
.DestroyData();
168 void wxComboBox::DoSetSize(int x
, int y
, int width
, int WXUNUSED(height
), int sizeFlags
)
170 // Necessary so it doesn't call wxChoice::SetSize
171 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
174 wxString
wxComboBox::GetValue() const
176 char* s
= XmTextGetString (GetXmText (this));
183 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
186 Widget listBox
= GetXmList(this);
188 // delete the item and add it again.
189 // FIXME isn't there a way to change it in place?
190 XmListDeletePos (listBox
, n
+1);
191 XmListAddItem (listBox
, text(), n
+1);
194 void wxComboBox::SetValue(const wxString
& value
)
198 XtVaSetValues( GetXmText(this),
199 XmNvalue
, value
.mb_str(),
202 m_inSetValue
= false;
205 int wxComboBox::DoAppend(const wxString
& item
)
207 wxXmString
str( item
.c_str() );
208 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0, False
);
210 AdjustDropDownListSize();
212 return GetCount() - 1;
215 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
217 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
218 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
220 if (pos
== GetCount())
221 return DoAppend(item
);
223 wxXmString
str( item
.c_str() );
224 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), pos
+1, False
);
226 AdjustDropDownListSize();
228 return GetCount() - 1;
231 void wxComboBox::Delete(unsigned int n
)
233 #ifdef LESSTIF_VERSION
234 XmListDeletePos (GetXmList(this), n
+ 1);
236 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
239 m_clientDataDict
.Delete(n
, HasClientObjectData());
242 AdjustDropDownListSize();
245 void wxComboBox::Clear()
247 #ifdef LESSTIF_VERSION
248 XmListDeleteAllItems (GetXmList(this));
250 while(m_noStrings
> 0)
252 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
256 if ( HasClientObjectData() )
257 m_clientDataDict
.DestroyData();
259 AdjustDropDownListSize();
262 void wxComboBox::SetSelection (int n
)
264 m_inSetSelection
= true;
266 #if wxCHECK_LESSTIF()
267 XmListSelectPos (GetXmList(this), n
+ 1, false);
268 SetValue(GetString(n
));
271 wxXmString
str(GetString(n
).c_str());
272 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
274 XtVaSetValues( (Widget
)m_mainWidget
,
275 XmNselectedPosition
, n
,
279 m_inSetSelection
= false;
282 int wxComboBox::GetSelection (void) const
284 return wxDoGetSelectionInList( GetXmList( this ) );
287 wxString
wxComboBox::GetString(unsigned int n
) const
289 return wxDoGetStringInList( GetXmList(this), n
);
292 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
294 // FIXME: back to base class for not supported value of bCase
296 return wxDoFindStringInList( GetXmList( this ), s
);
299 // Clipboard operations
300 void wxComboBox::Copy()
302 XmTextCopy( GetXmText(this), CurrentTime
);
305 void wxComboBox::Cut()
307 XmTextCut( GetXmText(this), CurrentTime
);
310 void wxComboBox::Paste()
312 XmTextPaste( GetXmText(this) );
315 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
320 void wxComboBox::SetInsertionPoint(long pos
)
322 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
325 void wxComboBox::SetInsertionPointEnd()
327 SetInsertionPoint( GetLastPosition() );
330 long wxComboBox::GetInsertionPoint() const
332 return (long)XmTextGetInsertionPosition( GetXmText(this) );
335 wxTextPos
wxComboBox::GetLastPosition() const
337 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
341 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
343 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
344 wxConstCast(value
.mb_str(), char) );
347 void wxComboBox::Remove(long from
, long to
)
349 SetSelection( from
, to
);
350 XmTextRemove( GetXmText(this) );
353 void wxComboBox::SetSelection(long from
, long to
)
356 to
= GetLastPosition();
358 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
359 (XmTextPosition
)to
, (Time
)0 );
362 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
363 XmComboBoxCallbackStruct
* cbs
)
365 wxComboBox
*item
= (wxComboBox
*) clientData
;
367 if( item
->m_inSetSelection
) return;
373 case XmCR_SINGLE_SELECT
:
374 case XmCR_BROWSE_SELECT
:
377 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
379 int idx
= cbs
->item_position
;
381 event
.SetString( item
->GetString (idx
) );
382 if ( item
->HasClientObjectData() )
383 event
.SetClientObject( item
->GetClientObject(idx
) );
384 else if ( item
->HasClientUntypedData() )
385 event
.SetClientData( item
->GetClientData(idx
) );
386 event
.SetExtraLong(true);
387 event
.SetEventObject(item
);
388 item
->GetEventHandler()->ProcessEvent(event
);
391 case XmCR_VALUE_CHANGED
:
393 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
395 event
.SetString( item
->GetValue() );
396 event
.SetExtraLong(true);
397 event
.SetEventObject(item
);
398 item
->GetEventHandler()->ProcessEvent(event
);
406 void wxComboBox::ChangeFont(bool keepOriginalSize
)
408 if( m_font
.Ok() && m_mainWidget
!= NULL
)
410 wxDoChangeFont( GetXmText(this), m_font
);
411 wxDoChangeFont( GetXmList(this), m_font
);
414 // Don't use the base class wxChoice's ChangeFont
415 wxWindow::ChangeFont(keepOriginalSize
);
418 void wxComboBox::ChangeBackgroundColour()
420 wxWindow::ChangeBackgroundColour();
423 void wxComboBox::ChangeForegroundColour()
425 wxWindow::ChangeForegroundColour();
428 wxSize
wxComboBox::DoGetBestSize() const
430 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
431 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
433 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
435 XtVaGetValues( (Widget
)m_mainWidget
,
436 XmNarrowSize
, &arrowW
,
437 XmNarrowSpacing
, &arrowS
,
438 XmNhighlightThickness
, &highlight
,
439 XmNmarginWidth
, &xmargin
,
440 XmNmarginHeight
, &ymargin
,
441 XmNshadowThickness
, &shadow
,
444 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
445 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
448 // FIXME arbitrary constants
449 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
450 + 2 * shadow
+ 2 * xmargin
,
451 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
454 return wxWindow::DoGetBestSize();
457 #endif // XmVersion >= 2000
459 #endif // wxUSE_COMBOBOX