]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/motif/combobox_native.cpp | |
3 | // Purpose: wxComboBox class | |
4 | // Author: Julian Smart, Ian Brown | |
5 | // Modified by: | |
6 | // Created: 01/02/03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_COMBOBOX | |
16 | ||
17 | #include "wx/combobox.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/arrstr.h" | |
21 | #endif | |
22 | ||
23 | #ifdef __VMS__ | |
24 | #pragma message disable nosimpint | |
25 | #endif | |
26 | #include <Xm/Xm.h> | |
27 | #ifdef __VMS__ | |
28 | #pragma message enable nosimpint | |
29 | #endif | |
30 | ||
31 | // use the new, shiny combobox for Motif 2.x | |
32 | #if (XmVersion >= 2000) | |
33 | ||
34 | #ifdef __VMS__ | |
35 | #pragma message disable nosimpint | |
36 | #endif | |
37 | #include <Xm/ComboBox.h> | |
38 | #include <Xm/Text.h> | |
39 | #include <Xm/List.h> | |
40 | #ifdef __VMS__ | |
41 | #pragma message enable nosimpint | |
42 | #endif | |
43 | ||
44 | #include "wx/motif/private.h" | |
45 | ||
46 | // utility | |
47 | static Widget GetXmList( const wxComboBox* cb ) | |
48 | { | |
49 | Widget ret; | |
50 | XtVaGetValues( (Widget)cb->GetMainWidget(), | |
51 | XmNlist, &ret, | |
52 | NULL ); | |
53 | ||
54 | return ret; | |
55 | } | |
56 | ||
57 | static Widget GetXmText( const wxComboBox* cb ) | |
58 | { | |
59 | Widget ret; | |
60 | XtVaGetValues( (Widget)cb->GetMainWidget(), | |
61 | XmNtextField, &ret, | |
62 | NULL ); | |
63 | ||
64 | return ret; | |
65 | } | |
66 | ||
67 | void wxComboBoxCallback (Widget w, XtPointer clientData, | |
68 | XmComboBoxCallbackStruct * cbs); | |
69 | ||
70 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
71 | const wxString& value, | |
72 | const wxPoint& pos, | |
73 | const wxSize& size, | |
74 | int n, const wxString choices[], | |
75 | long style, | |
76 | const wxValidator& validator, | |
77 | const wxString& name) | |
78 | { | |
79 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) | |
80 | return false; | |
81 | PreCreation(); | |
82 | ||
83 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
84 | ||
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 ); | |
92 | ||
93 | Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(), | |
94 | xmComboBoxWidgetClass, parentWidget, | |
95 | XmNcomboBoxType, cb_type, | |
96 | NULL); | |
97 | ||
98 | m_mainWidget = (Widget) buttonWidget; | |
99 | ||
100 | int i; | |
101 | for ( i = 0; i < n; ++i) | |
102 | Append( choices[i] ); | |
103 | ||
104 | XtManageChild (buttonWidget); | |
105 | ||
106 | SetValue(value); | |
107 | ||
108 | XtAddCallback (buttonWidget, XmNselectionCallback, | |
109 | (XtCallbackProc) wxComboBoxCallback, | |
110 | (XtPointer) this); | |
111 | XtAddCallback (GetXmText(this), XmNvalueChangedCallback, | |
112 | (XtCallbackProc) wxComboBoxCallback, | |
113 | (XtPointer) this); | |
114 | ||
115 | wxSize best = GetBestSize(); | |
116 | if( size.x != wxDefaultCoord ) best.x = size.x; | |
117 | if( size.y != wxDefaultCoord ) best.y = size.y; | |
118 | ||
119 | PostCreation(); | |
120 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, | |
121 | pos.x, pos.y, best.x, best.y); | |
122 | ||
123 | return true; | |
124 | } | |
125 | ||
126 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
127 | const wxString& value, | |
128 | const wxPoint& pos, | |
129 | const wxSize& size, | |
130 | const wxArrayString& choices, | |
131 | long style, | |
132 | const wxValidator& validator, | |
133 | const wxString& name) | |
134 | { | |
135 | wxCArrayString chs(choices); | |
136 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
137 | chs.GetStrings(), style, validator, name); | |
138 | } | |
139 | ||
140 | void wxComboBox::AdjustDropDownListSize() | |
141 | { | |
142 | int newListCount = -1, itemCount = GetCount(); | |
143 | const int MAX = 12; | |
144 | ||
145 | if( !itemCount ) | |
146 | newListCount = 1; | |
147 | else if( itemCount < MAX ) | |
148 | newListCount = itemCount; | |
149 | else | |
150 | newListCount = MAX; | |
151 | ||
152 | XtVaSetValues( GetXmList(this), | |
153 | XmNvisibleItemCount, newListCount, | |
154 | NULL ); | |
155 | } | |
156 | ||
157 | wxComboBox::~wxComboBox() | |
158 | { | |
159 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
160 | XtDestroyWidget((Widget) m_mainWidget); | |
161 | m_mainWidget = (WXWidget) 0; | |
162 | } | |
163 | ||
164 | void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags) | |
165 | { | |
166 | // Necessary so it doesn't call wxChoice::SetSize | |
167 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
168 | } | |
169 | ||
170 | void wxComboBox::SetString(unsigned int n, const wxString& s) | |
171 | { | |
172 | wxXmString text(s); | |
173 | Widget listBox = GetXmList(this); | |
174 | ||
175 | // delete the item and add it again. | |
176 | // FIXME isn't there a way to change it in place? | |
177 | XmListDeletePos (listBox, n+1); | |
178 | XmListAddItem (listBox, text(), n+1); | |
179 | } | |
180 | ||
181 | void wxComboBox::SetValue(const wxString& value) | |
182 | { | |
183 | m_inSetValue = true; | |
184 | ||
185 | XtVaSetValues( GetXmText(this), | |
186 | XmNvalue, (const char*)value.mb_str(), | |
187 | NULL); | |
188 | ||
189 | m_inSetValue = false; | |
190 | } | |
191 | ||
192 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items, | |
193 | unsigned int pos, | |
194 | void **clientData, wxClientDataType type) | |
195 | { | |
196 | const unsigned int numItems = items.GetCount(); | |
197 | ||
198 | AllocClientData(numItems); | |
199 | for ( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
200 | { | |
201 | wxXmString str( items[i].c_str() ); | |
202 | XmComboBoxAddItem((Widget) m_mainWidget, str(), | |
203 | GetMotifPosition(pos), False); | |
204 | ||
205 | InsertNewItemClientData(pos, clientData, i, type); | |
206 | } | |
207 | ||
208 | AdjustDropDownListSize(); | |
209 | ||
210 | return pos - 1; | |
211 | } | |
212 | ||
213 | void wxComboBox::DoDeleteOneItem(unsigned int n) | |
214 | { | |
215 | #ifdef LESSTIF_VERSION | |
216 | XmListDeletePos (GetXmList(this), n + 1); | |
217 | #else | |
218 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
219 | #endif | |
220 | ||
221 | wxControlWithItems::DoDeleteOneItem(n); | |
222 | m_stringArray.RemoveAt(size_t(n)); | |
223 | ||
224 | AdjustDropDownListSize(); | |
225 | } | |
226 | ||
227 | void wxComboBox::Clear() | |
228 | { | |
229 | #ifdef LESSTIF_VERSION | |
230 | XmListDeleteAllItems (GetXmList(this)); | |
231 | #else | |
232 | size_t n = m_stringArray.GetCount(); | |
233 | while(n > 0) | |
234 | { | |
235 | XmComboBoxDeletePos((Widget) m_mainWidget, n--); | |
236 | } | |
237 | #endif | |
238 | ||
239 | m_stringArray.Clear(); | |
240 | AdjustDropDownListSize(); | |
241 | ||
242 | wxTextEntry::Clear(); | |
243 | } | |
244 | ||
245 | void wxComboBox::SetSelection (int n) | |
246 | { | |
247 | m_inSetSelection = true; | |
248 | ||
249 | #if wxCHECK_LESSTIF() | |
250 | XmListSelectPos (GetXmList(this), n + 1, false); | |
251 | SetValue(GetString(n)); | |
252 | #else | |
253 | #if 0 | |
254 | wxXmString str(GetString(n).c_str()); | |
255 | XmComboBoxSelectItem((Widget) m_mainWidget, str()); | |
256 | #endif | |
257 | XtVaSetValues( (Widget)m_mainWidget, | |
258 | XmNselectedPosition, n, | |
259 | NULL ); | |
260 | #endif | |
261 | ||
262 | m_inSetSelection = false; | |
263 | } | |
264 | ||
265 | int wxComboBox::GetSelection() const | |
266 | { | |
267 | return wxDoGetSelectionInList( GetXmList( this ) ); | |
268 | } | |
269 | ||
270 | wxString wxComboBox::GetString(unsigned int n) const | |
271 | { | |
272 | return wxDoGetStringInList( GetXmList(this), n ); | |
273 | } | |
274 | ||
275 | int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const | |
276 | { | |
277 | // FIXME: back to base class for not supported value of bCase | |
278 | ||
279 | return wxDoFindStringInList( GetXmList( this ), s ); | |
280 | } | |
281 | ||
282 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, | |
283 | XmComboBoxCallbackStruct * cbs) | |
284 | { | |
285 | wxComboBox *item = (wxComboBox *) clientData; | |
286 | ||
287 | if( item->m_inSetSelection ) return; | |
288 | ||
289 | switch (cbs->reason) | |
290 | { | |
291 | case XmCR_SELECT: | |
292 | #if 0 | |
293 | case XmCR_SINGLE_SELECT: | |
294 | case XmCR_BROWSE_SELECT: | |
295 | #endif | |
296 | { | |
297 | wxCommandEvent event (wxEVT_COMBOBOX, | |
298 | item->GetId()); | |
299 | int idx = cbs->item_position; | |
300 | event.SetInt(idx); | |
301 | event.SetString( item->GetString (idx) ); | |
302 | if ( item->HasClientObjectData() ) | |
303 | event.SetClientObject( item->GetClientObject(idx) ); | |
304 | else if ( item->HasClientUntypedData() ) | |
305 | event.SetClientData( item->GetClientData(idx) ); | |
306 | event.SetExtraLong(true); | |
307 | event.SetEventObject(item); | |
308 | item->HandleWindowEvent(event); | |
309 | break; | |
310 | } | |
311 | case XmCR_VALUE_CHANGED: | |
312 | { | |
313 | wxCommandEvent event (wxEVT_TEXT, item->GetId()); | |
314 | event.SetInt(-1); | |
315 | event.SetString( item->GetValue() ); | |
316 | event.SetExtraLong(true); | |
317 | event.SetEventObject(item); | |
318 | item->HandleWindowEvent(event); | |
319 | break; | |
320 | } | |
321 | default: | |
322 | break; | |
323 | } | |
324 | } | |
325 | ||
326 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
327 | { | |
328 | if( m_font.IsOk() && m_mainWidget != NULL ) | |
329 | { | |
330 | wxDoChangeFont( GetXmText(this), m_font ); | |
331 | wxDoChangeFont( GetXmList(this), m_font ); | |
332 | } | |
333 | ||
334 | // Don't use the base class wxChoice's ChangeFont | |
335 | wxWindow::ChangeFont(keepOriginalSize); | |
336 | } | |
337 | ||
338 | void wxComboBox::ChangeBackgroundColour() | |
339 | { | |
340 | wxWindow::ChangeBackgroundColour(); | |
341 | } | |
342 | ||
343 | void wxComboBox::ChangeForegroundColour() | |
344 | { | |
345 | wxWindow::ChangeForegroundColour(); | |
346 | } | |
347 | ||
348 | wxSize wxComboBox::DoGetBestSize() const | |
349 | { | |
350 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || | |
351 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
352 | { | |
353 | Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow; | |
354 | ||
355 | XtVaGetValues( (Widget)m_mainWidget, | |
356 | XmNarrowSize, &arrowW, | |
357 | XmNarrowSpacing, &arrowS, | |
358 | XmNhighlightThickness, &highlight, | |
359 | XmNmarginWidth, &xmargin, | |
360 | XmNmarginHeight, &ymargin, | |
361 | XmNshadowThickness, &shadow, | |
362 | NULL ); | |
363 | ||
364 | wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this ); | |
365 | wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this), | |
366 | this ); | |
367 | ||
368 | // FIXME arbitrary constants | |
369 | return wxSize( listSize.x + arrowW + arrowS + 2 * highlight | |
370 | + 2 * shadow + 2 * xmargin , | |
371 | textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow ); | |
372 | } | |
373 | else | |
374 | return wxWindow::DoGetBestSize(); | |
375 | } | |
376 | ||
377 | WXWidget wxComboBox::GetTextWidget() const | |
378 | { | |
379 | return (WXWidget)GetXmText(this); | |
380 | } | |
381 | ||
382 | #endif // XmVersion >= 2000 | |
383 | ||
384 | #endif // wxUSE_COMBOBOX |