1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
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 old, GPL'd combobox
32 #if (XmVersion < 2000)
34 #include "xmcombo/xmcombo.h"
36 #include "wx/motif/private.h"
38 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
39 XmComboBoxSelectionCallbackStruct
* cbs
);
41 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
43 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
44 const wxString
& value
,
47 int n
, const wxString choices
[],
49 const wxValidator
& validator
,
52 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
56 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
58 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
59 xmComboBoxWidgetClass
, parentWidget
,
63 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
64 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
65 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
69 for (i
= 0; i
< n
; i
++)
71 wxXmString
str( choices
[i
] );
72 XmComboBoxAddItem(buttonWidget
, str(), 0);
73 m_stringArray
.Add(choices
[i
]);
76 m_mainWidget
= (Widget
) buttonWidget
;
78 XtManageChild (buttonWidget
);
82 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
84 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
88 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
93 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxString
& value
,
97 const wxArrayString
& choices
,
99 const wxValidator
& validator
,
100 const wxString
& name
)
102 wxCArrayString
chs(choices
);
103 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
104 chs
.GetStrings(), style
, validator
, name
);
107 wxComboBox::~wxComboBox()
109 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
110 XtDestroyWidget((Widget
) m_mainWidget
);
111 m_mainWidget
= (WXWidget
) 0;
114 void wxComboBox::DoSetSize(int x
, int y
,
115 int width
, int WXUNUSED(height
),
118 // Necessary so it doesn't call wxChoice::SetSize
119 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
123 // Already defined in include/motif/combobox.h
124 wxString
wxComboBox::GetValue() const
126 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
134 return wxEmptyString
;
138 void wxComboBox::SetValue(const wxString
& value
)
143 XmComboBoxSetString((Widget
)m_mainWidget
, value
.char_str());
144 m_inSetValue
= false;
148 void wxComboBox::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
150 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
153 // TODO auto-sorting is not supported by the code
154 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
157 wxClientDataType type
)
159 const unsigned int numItems
= items
.GetCount();
161 AllocClientData(numItems
);
162 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
164 wxXmString
str( items
[i
].c_str() );
165 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), GetMotifPosition(pos
));
166 m_stringArray
.Insert(items
[i
], pos
);
167 InsertNewItemClientData(pos
, clientData
, i
, type
);
173 void wxComboBox::DoDeleteOneItem(unsigned int n
)
175 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
176 m_stringArray
.RemoveAt(n
);
177 wxControlWithItems::DoDeleteOneItem(n
);
180 void wxComboBox::Clear()
182 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
183 m_stringArray
.Clear();
185 wxControlWithItems::DoClear();
188 void wxComboBox::SetSelection (int n
)
190 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
193 int wxComboBox::GetSelection (void) const
195 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
202 wxString
wxComboBox::GetString(unsigned int n
) const
204 return m_stringArray
[n
];
207 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
209 // FIXME: back to base class for not supported value of bCase
211 int *pos_list
= NULL
;
213 wxXmString
text( s
);
214 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
215 text(), &pos_list
, &count
) != 0);
217 if (found
&& count
> 0)
219 int pos
= pos_list
[0] - 1;
227 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
228 XmComboBoxSelectionCallbackStruct
* cbs
)
230 wxComboBox
*item
= (wxComboBox
*) clientData
;
234 case XmCR_SINGLE_SELECT
:
235 case XmCR_BROWSE_SELECT
:
237 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
239 event
.SetInt(cbs
->index
- 1);
240 event
.SetString( item
->GetString ( event
.GetInt() ) );
241 if ( item
->HasClientObjectData() )
242 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
243 else if ( item
->HasClientUntypedData() )
244 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
245 event
.SetExtraLong(true);
246 event
.SetEventObject(item
);
247 item
->ProcessCommand (event
);
250 case XmCR_VALUE_CHANGED
:
252 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
254 event
.SetString( item
->GetValue() );
255 event
.SetExtraLong(true);
256 event
.SetEventObject(item
);
257 item
->ProcessCommand (event
);
265 void wxComboBox::ChangeFont(bool keepOriginalSize
)
267 // Don't use the base class wxChoice's ChangeFont
268 wxWindow::ChangeFont(keepOriginalSize
);
271 void wxComboBox::ChangeBackgroundColour()
273 wxWindow::ChangeBackgroundColour();
276 void wxComboBox::ChangeForegroundColour()
278 wxWindow::ChangeForegroundColour();
281 wxSize
wxComboBox::DoGetBestSize() const
283 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
284 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
286 wxSize items
= GetItemsSize();
287 // FIXME arbitrary constants
288 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
292 return wxWindow::DoGetBestSize();
295 WXWidget
wxComboBox::GetTextWidget() const
297 return (WXWidget
)XmComboBoxGetEditWidget((Widget
) m_mainWidget
);
300 #endif // XmVersion < 2000
302 #endif // wxUSE_COMBOBOX