]>
Commit | Line | Data |
---|---|---|
9b1bd0c6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/motif/combobox_native.cpp |
9b1bd0c6 MB |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
9b1bd0c6 MB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
9b1bd0c6 MB |
15 | #if wxUSE_COMBOBOX |
16 | ||
17 | #include "wx/combobox.h" | |
aaa6d89a WS |
18 | |
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/arrstr.h" | |
21 | #endif | |
9b1bd0c6 MB |
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 | ||
100f9289 MB |
34 | #ifdef __VMS__ |
35 | #pragma message disable nosimpint | |
36 | #endif | |
9b1bd0c6 MB |
37 | #include <Xm/ComboBox.h> |
38 | #include <Xm/Text.h> | |
39 | #include <Xm/List.h> | |
100f9289 MB |
40 | #ifdef __VMS__ |
41 | #pragma message enable nosimpint | |
42 | #endif | |
9b1bd0c6 MB |
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 | ||
9b1bd0c6 MB |
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; | |
105fbe1f | 81 | PreCreation(); |
9b1bd0c6 MB |
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; | |
e1aae528 MB |
90 | if( cb_type == XmDROP_DOWN_COMBO_BOX ) |
91 | SetWindowStyle( style | wxCB_DROPDOWN ); | |
9b1bd0c6 MB |
92 | |
93 | Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(), | |
94 | xmComboBoxWidgetClass, parentWidget, | |
7d8268a1 | 95 | XmNcomboBoxType, cb_type, |
9b1bd0c6 MB |
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 | ||
9b1bd0c6 MB |
108 | XtAddCallback (buttonWidget, XmNselectionCallback, |
109 | (XtCallbackProc) wxComboBoxCallback, | |
110 | (XtPointer) this); | |
111 | XtAddCallback (GetXmText(this), XmNvalueChangedCallback, | |
112 | (XtCallbackProc) wxComboBoxCallback, | |
113 | (XtPointer) this); | |
114 | ||
e1aae528 | 115 | wxSize best = GetBestSize(); |
9b5f1895 WS |
116 | if( size.x != wxDefaultCoord ) best.x = size.x; |
117 | if( size.y != wxDefaultCoord ) best.y = size.y; | |
e1aae528 | 118 | |
105fbe1f | 119 | PostCreation(); |
9b1bd0c6 | 120 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
e1aae528 | 121 | pos.x, pos.y, best.x, best.y); |
9b1bd0c6 | 122 | |
9b1bd0c6 MB |
123 | return true; |
124 | } | |
125 | ||
584ad2a3 MB |
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); | |
7d8268a1 | 136 | return Create(parent, id, value, pos, size, chs.GetCount(), |
584ad2a3 MB |
137 | chs.GetStrings(), style, validator, name); |
138 | } | |
139 | ||
e1aae528 MB |
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 | ||
9b1bd0c6 MB |
157 | wxComboBox::~wxComboBox() |
158 | { | |
159 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
160 | XtDestroyWidget((Widget) m_mainWidget); | |
161 | m_mainWidget = (WXWidget) 0; | |
9b1bd0c6 MB |
162 | } |
163 | ||
55034339 | 164 | void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags) |
9b1bd0c6 MB |
165 | { |
166 | // Necessary so it doesn't call wxChoice::SetSize | |
167 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
168 | } | |
169 | ||
aa61d352 | 170 | void wxComboBox::SetString(unsigned int n, const wxString& s) |
9b1bd0c6 MB |
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), | |
6991087b | 186 | XmNvalue, (const char*)value.mb_str(), |
9b1bd0c6 MB |
187 | NULL); |
188 | ||
189 | m_inSetValue = false; | |
190 | } | |
191 | ||
a236aa20 VZ |
192 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items, |
193 | unsigned int pos, | |
194 | void **clientData, wxClientDataType type) | |
9b1bd0c6 | 195 | { |
a236aa20 | 196 | const unsigned int numItems = items.GetCount(); |
9b1bd0c6 | 197 | |
a236aa20 VZ |
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); | |
58045daa | 204 | |
a236aa20 VZ |
205 | InsertNewItemClientData(pos, clientData, i, type); |
206 | } | |
243dbf1a | 207 | |
243dbf1a VZ |
208 | AdjustDropDownListSize(); |
209 | ||
a236aa20 | 210 | return pos - 1; |
243dbf1a VZ |
211 | } |
212 | ||
a236aa20 | 213 | void wxComboBox::DoDeleteOneItem(unsigned int n) |
9b1bd0c6 MB |
214 | { |
215 | #ifdef LESSTIF_VERSION | |
216 | XmListDeletePos (GetXmList(this), n + 1); | |
217 | #else | |
218 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
219 | #endif | |
220 | ||
a236aa20 | 221 | wxControlWithItems::DoDeleteOneItem(n); |
58045daa | 222 | m_stringArray.RemoveAt(size_t(n)); |
e1aae528 MB |
223 | |
224 | AdjustDropDownListSize(); | |
9b1bd0c6 MB |
225 | } |
226 | ||
978c6e41 | 227 | void wxComboBox::Clear() |
9b1bd0c6 MB |
228 | { |
229 | #ifdef LESSTIF_VERSION | |
230 | XmListDeleteAllItems (GetXmList(this)); | |
231 | #else | |
58045daa FM |
232 | size_t n = m_stringArray.GetCount(); |
233 | while(n > 0) | |
9b1bd0c6 | 234 | { |
58045daa | 235 | XmComboBoxDeletePos((Widget) m_mainWidget, n--); |
9b1bd0c6 | 236 | } |
e1aae528 | 237 | #endif |
9b1bd0c6 | 238 | |
58045daa | 239 | m_stringArray.Clear(); |
e1aae528 | 240 | AdjustDropDownListSize(); |
978c6e41 VZ |
241 | |
242 | wxTextEntry::Clear(); | |
9b1bd0c6 MB |
243 | } |
244 | ||
245 | void wxComboBox::SetSelection (int n) | |
246 | { | |
d8d18184 MB |
247 | m_inSetSelection = true; |
248 | ||
d40708e2 | 249 | #if wxCHECK_LESSTIF() |
9b1bd0c6 MB |
250 | XmListSelectPos (GetXmList(this), n + 1, false); |
251 | SetValue(GetString(n)); | |
252 | #else | |
d40708e2 | 253 | #if 0 |
aa61d352 | 254 | wxXmString str(GetString(n).c_str()); |
9b1bd0c6 | 255 | XmComboBoxSelectItem((Widget) m_mainWidget, str()); |
d40708e2 | 256 | #endif |
9b1bd0c6 | 257 | XtVaSetValues( (Widget)m_mainWidget, |
d40708e2 | 258 | XmNselectedPosition, n, |
9b1bd0c6 MB |
259 | NULL ); |
260 | #endif | |
d8d18184 MB |
261 | |
262 | m_inSetSelection = false; | |
9b1bd0c6 MB |
263 | } |
264 | ||
978c6e41 | 265 | int wxComboBox::GetSelection() const |
9b1bd0c6 MB |
266 | { |
267 | return wxDoGetSelectionInList( GetXmList( this ) ); | |
268 | } | |
269 | ||
aa61d352 | 270 | wxString wxComboBox::GetString(unsigned int n) const |
9b1bd0c6 | 271 | { |
e1aae528 | 272 | return wxDoGetStringInList( GetXmList(this), n ); |
9b1bd0c6 MB |
273 | } |
274 | ||
55034339 | 275 | int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const |
9b1bd0c6 | 276 | { |
11e62fe6 WS |
277 | // FIXME: back to base class for not supported value of bCase |
278 | ||
9b1bd0c6 MB |
279 | return wxDoFindStringInList( GetXmList( this ), s ); |
280 | } | |
281 | ||
9b1bd0c6 MB |
282 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
283 | XmComboBoxCallbackStruct * cbs) | |
284 | { | |
285 | wxComboBox *item = (wxComboBox *) clientData; | |
286 | ||
d8d18184 MB |
287 | if( item->m_inSetSelection ) return; |
288 | ||
9b1bd0c6 MB |
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 | { | |
ce7fe42e | 297 | wxCommandEvent event (wxEVT_COMBOBOX, |
9b1bd0c6 | 298 | item->GetId()); |
d8d18184 | 299 | int idx = cbs->item_position; |
687706f5 KH |
300 | event.SetInt(idx); |
301 | event.SetString( item->GetString (idx) ); | |
9b1bd0c6 MB |
302 | if ( item->HasClientObjectData() ) |
303 | event.SetClientObject( item->GetClientObject(idx) ); | |
304 | else if ( item->HasClientUntypedData() ) | |
305 | event.SetClientData( item->GetClientData(idx) ); | |
687706f5 | 306 | event.SetExtraLong(true); |
9b1bd0c6 | 307 | event.SetEventObject(item); |
937013e0 | 308 | item->HandleWindowEvent(event); |
9b1bd0c6 MB |
309 | break; |
310 | } | |
311 | case XmCR_VALUE_CHANGED: | |
312 | { | |
ce7fe42e | 313 | wxCommandEvent event (wxEVT_TEXT, item->GetId()); |
687706f5 KH |
314 | event.SetInt(-1); |
315 | event.SetString( item->GetValue() ); | |
316 | event.SetExtraLong(true); | |
9b1bd0c6 | 317 | event.SetEventObject(item); |
937013e0 | 318 | item->HandleWindowEvent(event); |
9b1bd0c6 MB |
319 | break; |
320 | } | |
321 | default: | |
322 | break; | |
323 | } | |
324 | } | |
325 | ||
326 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
327 | { | |
a1b806b9 | 328 | if( m_font.IsOk() && m_mainWidget != NULL ) |
e1aae528 MB |
329 | { |
330 | wxDoChangeFont( GetXmText(this), m_font ); | |
331 | wxDoChangeFont( GetXmList(this), m_font ); | |
332 | } | |
333 | ||
9b1bd0c6 MB |
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 | { | |
e1aae528 MB |
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(); | |
9b1bd0c6 MB |
375 | } |
376 | ||
89954433 VZ |
377 | WXWidget wxComboBox::GetTextWidget() const |
378 | { | |
379 | return (WXWidget)GetXmText(this); | |
380 | } | |
381 | ||
9b1bd0c6 MB |
382 | #endif // XmVersion >= 2000 |
383 | ||
384 | #endif // wxUSE_COMBOBOX |