]>
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 | ||
70 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
71 | ||
72 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
73 | const wxString& value, | |
74 | const wxPoint& pos, | |
75 | const wxSize& size, | |
76 | int n, const wxString choices[], | |
77 | long style, | |
78 | const wxValidator& validator, | |
79 | const wxString& name) | |
80 | { | |
81 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) | |
82 | return false; | |
105fbe1f | 83 | PreCreation(); |
9b1bd0c6 MB |
84 | |
85 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
86 | ||
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; | |
e1aae528 MB |
92 | if( cb_type == XmDROP_DOWN_COMBO_BOX ) |
93 | SetWindowStyle( style | wxCB_DROPDOWN ); | |
9b1bd0c6 MB |
94 | |
95 | Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(), | |
96 | xmComboBoxWidgetClass, parentWidget, | |
7d8268a1 | 97 | XmNcomboBoxType, cb_type, |
9b1bd0c6 MB |
98 | NULL); |
99 | ||
100 | m_mainWidget = (Widget) buttonWidget; | |
101 | ||
102 | int i; | |
103 | for ( i = 0; i < n; ++i) | |
104 | Append( choices[i] ); | |
105 | ||
106 | XtManageChild (buttonWidget); | |
107 | ||
108 | SetValue(value); | |
109 | ||
9b1bd0c6 MB |
110 | XtAddCallback (buttonWidget, XmNselectionCallback, |
111 | (XtCallbackProc) wxComboBoxCallback, | |
112 | (XtPointer) this); | |
113 | XtAddCallback (GetXmText(this), XmNvalueChangedCallback, | |
114 | (XtCallbackProc) wxComboBoxCallback, | |
115 | (XtPointer) this); | |
116 | ||
e1aae528 | 117 | wxSize best = GetBestSize(); |
9b5f1895 WS |
118 | if( size.x != wxDefaultCoord ) best.x = size.x; |
119 | if( size.y != wxDefaultCoord ) best.y = size.y; | |
e1aae528 | 120 | |
105fbe1f | 121 | PostCreation(); |
9b1bd0c6 | 122 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
e1aae528 | 123 | pos.x, pos.y, best.x, best.y); |
9b1bd0c6 | 124 | |
9b1bd0c6 MB |
125 | return true; |
126 | } | |
127 | ||
584ad2a3 MB |
128 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
129 | const wxString& value, | |
130 | const wxPoint& pos, | |
131 | const wxSize& size, | |
132 | const wxArrayString& choices, | |
133 | long style, | |
134 | const wxValidator& validator, | |
135 | const wxString& name) | |
136 | { | |
137 | wxCArrayString chs(choices); | |
7d8268a1 | 138 | return Create(parent, id, value, pos, size, chs.GetCount(), |
584ad2a3 MB |
139 | chs.GetStrings(), style, validator, name); |
140 | } | |
141 | ||
e1aae528 MB |
142 | void wxComboBox::AdjustDropDownListSize() |
143 | { | |
144 | int newListCount = -1, itemCount = GetCount(); | |
145 | const int MAX = 12; | |
146 | ||
147 | if( !itemCount ) | |
148 | newListCount = 1; | |
149 | else if( itemCount < MAX ) | |
150 | newListCount = itemCount; | |
151 | else | |
152 | newListCount = MAX; | |
153 | ||
154 | XtVaSetValues( GetXmList(this), | |
155 | XmNvisibleItemCount, newListCount, | |
156 | NULL ); | |
157 | } | |
158 | ||
9b1bd0c6 MB |
159 | wxComboBox::~wxComboBox() |
160 | { | |
161 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
162 | XtDestroyWidget((Widget) m_mainWidget); | |
163 | m_mainWidget = (WXWidget) 0; | |
9b1bd0c6 MB |
164 | } |
165 | ||
55034339 | 166 | void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags) |
9b1bd0c6 MB |
167 | { |
168 | // Necessary so it doesn't call wxChoice::SetSize | |
169 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
170 | } | |
171 | ||
aa61d352 | 172 | void wxComboBox::SetString(unsigned int n, const wxString& s) |
9b1bd0c6 MB |
173 | { |
174 | wxXmString text(s); | |
175 | Widget listBox = GetXmList(this); | |
176 | ||
177 | // delete the item and add it again. | |
178 | // FIXME isn't there a way to change it in place? | |
179 | XmListDeletePos (listBox, n+1); | |
180 | XmListAddItem (listBox, text(), n+1); | |
181 | } | |
182 | ||
183 | void wxComboBox::SetValue(const wxString& value) | |
184 | { | |
185 | m_inSetValue = true; | |
186 | ||
187 | XtVaSetValues( GetXmText(this), | |
6991087b | 188 | XmNvalue, (const char*)value.mb_str(), |
9b1bd0c6 MB |
189 | NULL); |
190 | ||
191 | m_inSetValue = false; | |
192 | } | |
193 | ||
a236aa20 VZ |
194 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items, |
195 | unsigned int pos, | |
196 | void **clientData, wxClientDataType type) | |
9b1bd0c6 | 197 | { |
a236aa20 | 198 | const unsigned int numItems = items.GetCount(); |
9b1bd0c6 | 199 | |
a236aa20 VZ |
200 | AllocClientData(numItems); |
201 | for ( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
202 | { | |
203 | wxXmString str( items[i].c_str() ); | |
204 | XmComboBoxAddItem((Widget) m_mainWidget, str(), | |
205 | GetMotifPosition(pos), False); | |
206 | m_noStrings ++; | |
207 | InsertNewItemClientData(pos, clientData, i, type); | |
208 | } | |
243dbf1a | 209 | |
243dbf1a VZ |
210 | AdjustDropDownListSize(); |
211 | ||
a236aa20 | 212 | return pos - 1; |
243dbf1a VZ |
213 | } |
214 | ||
a236aa20 | 215 | void wxComboBox::DoDeleteOneItem(unsigned int n) |
9b1bd0c6 MB |
216 | { |
217 | #ifdef LESSTIF_VERSION | |
218 | XmListDeletePos (GetXmList(this), n + 1); | |
219 | #else | |
220 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
221 | #endif | |
222 | ||
a236aa20 | 223 | wxControlWithItems::DoDeleteOneItem(n); |
9b1bd0c6 | 224 | m_noStrings--; |
e1aae528 MB |
225 | |
226 | AdjustDropDownListSize(); | |
9b1bd0c6 MB |
227 | } |
228 | ||
978c6e41 | 229 | void wxComboBox::Clear() |
9b1bd0c6 MB |
230 | { |
231 | #ifdef LESSTIF_VERSION | |
232 | XmListDeleteAllItems (GetXmList(this)); | |
233 | #else | |
234 | while(m_noStrings > 0) | |
235 | { | |
7d8268a1 | 236 | XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--); |
9b1bd0c6 | 237 | } |
e1aae528 | 238 | #endif |
9b1bd0c6 | 239 | |
9b1bd0c6 | 240 | m_noStrings = 0; |
e1aae528 | 241 | AdjustDropDownListSize(); |
978c6e41 VZ |
242 | |
243 | wxTextEntry::Clear(); | |
9b1bd0c6 MB |
244 | } |
245 | ||
246 | void wxComboBox::SetSelection (int n) | |
247 | { | |
d8d18184 MB |
248 | m_inSetSelection = true; |
249 | ||
d40708e2 | 250 | #if wxCHECK_LESSTIF() |
9b1bd0c6 MB |
251 | XmListSelectPos (GetXmList(this), n + 1, false); |
252 | SetValue(GetString(n)); | |
253 | #else | |
d40708e2 | 254 | #if 0 |
aa61d352 | 255 | wxXmString str(GetString(n).c_str()); |
9b1bd0c6 | 256 | XmComboBoxSelectItem((Widget) m_mainWidget, str()); |
d40708e2 | 257 | #endif |
9b1bd0c6 | 258 | XtVaSetValues( (Widget)m_mainWidget, |
d40708e2 | 259 | XmNselectedPosition, n, |
9b1bd0c6 MB |
260 | NULL ); |
261 | #endif | |
d8d18184 MB |
262 | |
263 | m_inSetSelection = false; | |
9b1bd0c6 MB |
264 | } |
265 | ||
978c6e41 | 266 | int wxComboBox::GetSelection() const |
9b1bd0c6 MB |
267 | { |
268 | return wxDoGetSelectionInList( GetXmList( this ) ); | |
269 | } | |
270 | ||
aa61d352 | 271 | wxString wxComboBox::GetString(unsigned int n) const |
9b1bd0c6 | 272 | { |
e1aae528 | 273 | return wxDoGetStringInList( GetXmList(this), n ); |
9b1bd0c6 MB |
274 | } |
275 | ||
55034339 | 276 | int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const |
9b1bd0c6 | 277 | { |
11e62fe6 WS |
278 | // FIXME: back to base class for not supported value of bCase |
279 | ||
9b1bd0c6 MB |
280 | return wxDoFindStringInList( GetXmList( this ), s ); |
281 | } | |
282 | ||
9b1bd0c6 MB |
283 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
284 | XmComboBoxCallbackStruct * cbs) | |
285 | { | |
286 | wxComboBox *item = (wxComboBox *) clientData; | |
287 | ||
d8d18184 MB |
288 | if( item->m_inSetSelection ) return; |
289 | ||
9b1bd0c6 MB |
290 | switch (cbs->reason) |
291 | { | |
292 | case XmCR_SELECT: | |
293 | #if 0 | |
294 | case XmCR_SINGLE_SELECT: | |
295 | case XmCR_BROWSE_SELECT: | |
296 | #endif | |
297 | { | |
298 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, | |
299 | item->GetId()); | |
d8d18184 | 300 | int idx = cbs->item_position; |
687706f5 KH |
301 | event.SetInt(idx); |
302 | event.SetString( item->GetString (idx) ); | |
9b1bd0c6 MB |
303 | if ( item->HasClientObjectData() ) |
304 | event.SetClientObject( item->GetClientObject(idx) ); | |
305 | else if ( item->HasClientUntypedData() ) | |
306 | event.SetClientData( item->GetClientData(idx) ); | |
687706f5 | 307 | event.SetExtraLong(true); |
9b1bd0c6 | 308 | event.SetEventObject(item); |
d8d18184 | 309 | item->GetEventHandler()->ProcessEvent(event); |
9b1bd0c6 MB |
310 | break; |
311 | } | |
312 | case XmCR_VALUE_CHANGED: | |
313 | { | |
314 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
687706f5 KH |
315 | event.SetInt(-1); |
316 | event.SetString( item->GetValue() ); | |
317 | event.SetExtraLong(true); | |
9b1bd0c6 | 318 | event.SetEventObject(item); |
d8d18184 | 319 | item->GetEventHandler()->ProcessEvent(event); |
9b1bd0c6 MB |
320 | break; |
321 | } | |
322 | default: | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
327 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
328 | { | |
105fbe1f | 329 | if( m_font.Ok() && m_mainWidget != NULL ) |
e1aae528 MB |
330 | { |
331 | wxDoChangeFont( GetXmText(this), m_font ); | |
332 | wxDoChangeFont( GetXmList(this), m_font ); | |
333 | } | |
334 | ||
9b1bd0c6 MB |
335 | // Don't use the base class wxChoice's ChangeFont |
336 | wxWindow::ChangeFont(keepOriginalSize); | |
337 | } | |
338 | ||
339 | void wxComboBox::ChangeBackgroundColour() | |
340 | { | |
341 | wxWindow::ChangeBackgroundColour(); | |
342 | } | |
343 | ||
344 | void wxComboBox::ChangeForegroundColour() | |
345 | { | |
346 | wxWindow::ChangeForegroundColour(); | |
347 | } | |
348 | ||
349 | wxSize wxComboBox::DoGetBestSize() const | |
350 | { | |
e1aae528 MB |
351 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || |
352 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
353 | { | |
354 | Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow; | |
355 | ||
356 | XtVaGetValues( (Widget)m_mainWidget, | |
357 | XmNarrowSize, &arrowW, | |
358 | XmNarrowSpacing, &arrowS, | |
359 | XmNhighlightThickness, &highlight, | |
360 | XmNmarginWidth, &xmargin, | |
361 | XmNmarginHeight, &ymargin, | |
362 | XmNshadowThickness, &shadow, | |
363 | NULL ); | |
364 | ||
365 | wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this ); | |
366 | wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this), | |
367 | this ); | |
368 | ||
369 | // FIXME arbitrary constants | |
370 | return wxSize( listSize.x + arrowW + arrowS + 2 * highlight | |
371 | + 2 * shadow + 2 * xmargin , | |
372 | textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow ); | |
373 | } | |
374 | else | |
375 | return wxWindow::DoGetBestSize(); | |
9b1bd0c6 MB |
376 | } |
377 | ||
89954433 VZ |
378 | WXWidget wxComboBox::GetTextWidget() const |
379 | { | |
380 | return (WXWidget)GetXmText(this); | |
381 | } | |
382 | ||
9b1bd0c6 MB |
383 | #endif // XmVersion >= 2000 |
384 | ||
385 | #endif // wxUSE_COMBOBOX |