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 wxString
wxComboBox::GetValue() const
174 char* s
= XmTextGetString (GetXmText (this));
181 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
184 Widget listBox
= GetXmList(this);
186 // delete the item and add it again.
187 // FIXME isn't there a way to change it in place?
188 XmListDeletePos (listBox
, n
+1);
189 XmListAddItem (listBox
, text(), n
+1);
192 void wxComboBox::SetValue(const wxString
& value
)
196 XtVaSetValues( GetXmText(this),
197 XmNvalue
, (const char*)value
.mb_str(),
200 m_inSetValue
= false;
203 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
205 void **clientData
, wxClientDataType type
)
207 const unsigned int numItems
= items
.GetCount();
209 AllocClientData(numItems
);
210 for ( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
212 wxXmString
str( items
[i
].c_str() );
213 XmComboBoxAddItem((Widget
) m_mainWidget
, str(),
214 GetMotifPosition(pos
), False
);
216 InsertNewItemClientData(pos
, clientData
, i
, type
);
219 AdjustDropDownListSize();
224 void wxComboBox::DoDeleteOneItem(unsigned int n
)
226 #ifdef LESSTIF_VERSION
227 XmListDeletePos (GetXmList(this), n
+ 1);
229 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
232 wxControlWithItems::DoDeleteOneItem(n
);
235 AdjustDropDownListSize();
238 void wxComboBox::DoClear()
240 #ifdef LESSTIF_VERSION
241 XmListDeleteAllItems (GetXmList(this));
243 while(m_noStrings
> 0)
245 XmComboBoxDeletePos((Widget
) m_mainWidget
, m_noStrings
--);
249 wxControlWithItems::DoClear();
251 AdjustDropDownListSize();
254 void wxComboBox::SetSelection (int n
)
256 m_inSetSelection
= true;
258 #if wxCHECK_LESSTIF()
259 XmListSelectPos (GetXmList(this), n
+ 1, false);
260 SetValue(GetString(n
));
263 wxXmString
str(GetString(n
).c_str());
264 XmComboBoxSelectItem((Widget
) m_mainWidget
, str());
266 XtVaSetValues( (Widget
)m_mainWidget
,
267 XmNselectedPosition
, n
,
271 m_inSetSelection
= false;
274 int wxComboBox::GetSelection (void) const
276 return wxDoGetSelectionInList( GetXmList( this ) );
279 wxString
wxComboBox::GetString(unsigned int n
) const
281 return wxDoGetStringInList( GetXmList(this), n
);
284 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
286 // FIXME: back to base class for not supported value of bCase
288 return wxDoFindStringInList( GetXmList( this ), s
);
291 // Clipboard operations
292 void wxComboBox::Copy()
294 XmTextCopy( GetXmText(this), CurrentTime
);
297 void wxComboBox::Cut()
299 XmTextCut( GetXmText(this), CurrentTime
);
302 void wxComboBox::Paste()
304 XmTextPaste( GetXmText(this) );
307 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
312 void wxComboBox::SetInsertionPoint(long pos
)
314 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition
)pos
);
317 void wxComboBox::SetInsertionPointEnd()
319 SetInsertionPoint( GetLastPosition() );
322 long wxComboBox::GetInsertionPoint() const
324 return (long)XmTextGetInsertionPosition( GetXmText(this) );
327 wxTextPos
wxComboBox::GetLastPosition() const
329 XmTextPosition pos
= XmTextGetLastPosition( GetXmText(this) );
333 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
335 XmTextReplace( GetXmText(this), (XmTextPosition
)from
, (XmTextPosition
)to
,
339 void wxComboBox::Remove(long from
, long to
)
341 SetSelection( from
, to
);
342 XmTextRemove( GetXmText(this) );
345 void wxComboBox::SetSelection(long from
, long to
)
348 to
= GetLastPosition();
350 XmTextSetSelection( GetXmText(this), (XmTextPosition
)from
,
351 (XmTextPosition
)to
, (Time
)0 );
354 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
355 XmComboBoxCallbackStruct
* cbs
)
357 wxComboBox
*item
= (wxComboBox
*) clientData
;
359 if( item
->m_inSetSelection
) return;
365 case XmCR_SINGLE_SELECT
:
366 case XmCR_BROWSE_SELECT
:
369 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
371 int idx
= cbs
->item_position
;
373 event
.SetString( item
->GetString (idx
) );
374 if ( item
->HasClientObjectData() )
375 event
.SetClientObject( item
->GetClientObject(idx
) );
376 else if ( item
->HasClientUntypedData() )
377 event
.SetClientData( item
->GetClientData(idx
) );
378 event
.SetExtraLong(true);
379 event
.SetEventObject(item
);
380 item
->GetEventHandler()->ProcessEvent(event
);
383 case XmCR_VALUE_CHANGED
:
385 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
387 event
.SetString( item
->GetValue() );
388 event
.SetExtraLong(true);
389 event
.SetEventObject(item
);
390 item
->GetEventHandler()->ProcessEvent(event
);
398 void wxComboBox::ChangeFont(bool keepOriginalSize
)
400 if( m_font
.Ok() && m_mainWidget
!= NULL
)
402 wxDoChangeFont( GetXmText(this), m_font
);
403 wxDoChangeFont( GetXmList(this), m_font
);
406 // Don't use the base class wxChoice's ChangeFont
407 wxWindow::ChangeFont(keepOriginalSize
);
410 void wxComboBox::ChangeBackgroundColour()
412 wxWindow::ChangeBackgroundColour();
415 void wxComboBox::ChangeForegroundColour()
417 wxWindow::ChangeForegroundColour();
420 wxSize
wxComboBox::DoGetBestSize() const
422 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
423 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
425 Dimension arrowW
, arrowS
, highlight
, xmargin
, ymargin
, shadow
;
427 XtVaGetValues( (Widget
)m_mainWidget
,
428 XmNarrowSize
, &arrowW
,
429 XmNarrowSpacing
, &arrowS
,
430 XmNhighlightThickness
, &highlight
,
431 XmNmarginWidth
, &xmargin
,
432 XmNmarginHeight
, &ymargin
,
433 XmNshadowThickness
, &shadow
,
436 wxSize listSize
= wxDoGetListBoxBestSize( GetXmList(this), this );
437 wxSize textSize
= wxDoGetSingleTextCtrlBestSize( GetXmText(this),
440 // FIXME arbitrary constants
441 return wxSize( listSize
.x
+ arrowW
+ arrowS
+ 2 * highlight
442 + 2 * shadow
+ 2 * xmargin
,
443 textSize
.y
+ 2 * highlight
+ 2 * ymargin
+ 2 * shadow
);
446 return wxWindow::DoGetBestSize();
449 #endif // XmVersion >= 2000
451 #endif // wxUSE_COMBOBOX