]>
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; | |
83 | ||
84 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
85 | ||
86 | int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX : | |
87 | ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST : | |
88 | ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX : | |
89 | // default to wxCB_DROPDOWN | |
90 | XmDROP_DOWN_COMBO_BOX; | |
e1aae528 MB |
91 | if( cb_type == XmDROP_DOWN_COMBO_BOX ) |
92 | SetWindowStyle( style | wxCB_DROPDOWN ); | |
9b1bd0c6 MB |
93 | |
94 | Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(), | |
95 | xmComboBoxWidgetClass, parentWidget, | |
7d8268a1 | 96 | XmNcomboBoxType, cb_type, |
9b1bd0c6 MB |
97 | NULL); |
98 | ||
99 | m_mainWidget = (Widget) buttonWidget; | |
100 | ||
101 | int i; | |
102 | for ( i = 0; i < n; ++i) | |
103 | Append( choices[i] ); | |
104 | ||
105 | XtManageChild (buttonWidget); | |
106 | ||
107 | SetValue(value); | |
108 | ||
109 | ChangeFont(false); | |
110 | ||
111 | XtAddCallback (buttonWidget, XmNselectionCallback, | |
112 | (XtCallbackProc) wxComboBoxCallback, | |
113 | (XtPointer) this); | |
114 | XtAddCallback (GetXmText(this), XmNvalueChangedCallback, | |
115 | (XtCallbackProc) wxComboBoxCallback, | |
116 | (XtPointer) this); | |
117 | ||
e1aae528 | 118 | wxSize best = GetBestSize(); |
9b5f1895 WS |
119 | if( size.x != wxDefaultCoord ) best.x = size.x; |
120 | if( size.y != wxDefaultCoord ) best.y = size.y; | |
e1aae528 | 121 | |
9b1bd0c6 | 122 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
e1aae528 | 123 | pos.x, pos.y, best.x, best.y); |
9b1bd0c6 MB |
124 | |
125 | ChangeBackgroundColour(); | |
126 | ||
127 | return true; | |
128 | } | |
129 | ||
584ad2a3 MB |
130 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
131 | const wxString& value, | |
132 | const wxPoint& pos, | |
133 | const wxSize& size, | |
134 | const wxArrayString& choices, | |
135 | long style, | |
136 | const wxValidator& validator, | |
137 | const wxString& name) | |
138 | { | |
139 | wxCArrayString chs(choices); | |
7d8268a1 | 140 | return Create(parent, id, value, pos, size, chs.GetCount(), |
584ad2a3 MB |
141 | chs.GetStrings(), style, validator, name); |
142 | } | |
143 | ||
e1aae528 MB |
144 | void wxComboBox::AdjustDropDownListSize() |
145 | { | |
146 | int newListCount = -1, itemCount = GetCount(); | |
147 | const int MAX = 12; | |
148 | ||
149 | if( !itemCount ) | |
150 | newListCount = 1; | |
151 | else if( itemCount < MAX ) | |
152 | newListCount = itemCount; | |
153 | else | |
154 | newListCount = MAX; | |
155 | ||
156 | XtVaSetValues( GetXmList(this), | |
157 | XmNvisibleItemCount, newListCount, | |
158 | NULL ); | |
159 | } | |
160 | ||
9b1bd0c6 MB |
161 | wxComboBox::~wxComboBox() |
162 | { | |
163 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
164 | XtDestroyWidget((Widget) m_mainWidget); | |
165 | m_mainWidget = (WXWidget) 0; | |
166 | if ( HasClientObjectData() ) | |
167 | m_clientDataDict.DestroyData(); | |
168 | } | |
169 | ||
55034339 | 170 | void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags) |
9b1bd0c6 MB |
171 | { |
172 | // Necessary so it doesn't call wxChoice::SetSize | |
173 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
174 | } | |
175 | ||
176 | wxString wxComboBox::GetValue() const | |
177 | { | |
178 | char* s = XmTextGetString (GetXmText (this)); | |
179 | wxString str(s); | |
180 | if (s) | |
181 | XtFree (s); | |
182 | return str; | |
183 | } | |
184 | ||
aa61d352 | 185 | void wxComboBox::SetString(unsigned int n, const wxString& s) |
9b1bd0c6 MB |
186 | { |
187 | wxXmString text(s); | |
188 | Widget listBox = GetXmList(this); | |
189 | ||
190 | // delete the item and add it again. | |
191 | // FIXME isn't there a way to change it in place? | |
192 | XmListDeletePos (listBox, n+1); | |
193 | XmListAddItem (listBox, text(), n+1); | |
194 | } | |
195 | ||
196 | void wxComboBox::SetValue(const wxString& value) | |
197 | { | |
198 | m_inSetValue = true; | |
199 | ||
200 | XtVaSetValues( GetXmText(this), | |
9a183206 | 201 | XmNvalue, value.mb_str(), |
9b1bd0c6 MB |
202 | NULL); |
203 | ||
204 | m_inSetValue = false; | |
205 | } | |
206 | ||
207 | int wxComboBox::DoAppend(const wxString& item) | |
208 | { | |
209 | wxXmString str( item.c_str() ); | |
210 | XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False); | |
9b1bd0c6 | 211 | m_noStrings ++; |
e1aae528 | 212 | AdjustDropDownListSize(); |
9b1bd0c6 MB |
213 | |
214 | return GetCount() - 1; | |
215 | } | |
216 | ||
aa61d352 | 217 | int wxComboBox::DoInsert(const wxString& item, unsigned int pos) |
243dbf1a VZ |
218 | { |
219 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
36e14c2b | 220 | wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index")); |
243dbf1a | 221 | |
aa61d352 | 222 | if (pos == GetCount()) |
243dbf1a VZ |
223 | return DoAppend(item); |
224 | ||
225 | wxXmString str( item.c_str() ); | |
226 | XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False); | |
227 | m_noStrings ++; | |
228 | AdjustDropDownListSize(); | |
229 | ||
230 | return GetCount() - 1; | |
231 | } | |
232 | ||
aa61d352 | 233 | void wxComboBox::Delete(unsigned int n) |
9b1bd0c6 MB |
234 | { |
235 | #ifdef LESSTIF_VERSION | |
236 | XmListDeletePos (GetXmList(this), n + 1); | |
237 | #else | |
238 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
239 | #endif | |
240 | ||
9b1bd0c6 MB |
241 | m_clientDataDict.Delete(n, HasClientObjectData()); |
242 | m_noStrings--; | |
e1aae528 MB |
243 | |
244 | AdjustDropDownListSize(); | |
9b1bd0c6 MB |
245 | } |
246 | ||
247 | void wxComboBox::Clear() | |
248 | { | |
249 | #ifdef LESSTIF_VERSION | |
250 | XmListDeleteAllItems (GetXmList(this)); | |
251 | #else | |
252 | while(m_noStrings > 0) | |
253 | { | |
7d8268a1 | 254 | XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--); |
9b1bd0c6 | 255 | } |
e1aae528 | 256 | #endif |
9b1bd0c6 MB |
257 | |
258 | if ( HasClientObjectData() ) | |
259 | m_clientDataDict.DestroyData(); | |
260 | m_noStrings = 0; | |
e1aae528 | 261 | AdjustDropDownListSize(); |
9b1bd0c6 MB |
262 | } |
263 | ||
264 | void wxComboBox::SetSelection (int n) | |
265 | { | |
d8d18184 MB |
266 | m_inSetSelection = true; |
267 | ||
d40708e2 | 268 | #if wxCHECK_LESSTIF() |
9b1bd0c6 MB |
269 | XmListSelectPos (GetXmList(this), n + 1, false); |
270 | SetValue(GetString(n)); | |
271 | #else | |
d40708e2 | 272 | #if 0 |
aa61d352 | 273 | wxXmString str(GetString(n).c_str()); |
9b1bd0c6 | 274 | XmComboBoxSelectItem((Widget) m_mainWidget, str()); |
d40708e2 | 275 | #endif |
9b1bd0c6 | 276 | XtVaSetValues( (Widget)m_mainWidget, |
d40708e2 | 277 | XmNselectedPosition, n, |
9b1bd0c6 MB |
278 | NULL ); |
279 | #endif | |
d8d18184 MB |
280 | |
281 | m_inSetSelection = false; | |
9b1bd0c6 MB |
282 | } |
283 | ||
284 | int wxComboBox::GetSelection (void) const | |
285 | { | |
286 | return wxDoGetSelectionInList( GetXmList( this ) ); | |
287 | } | |
288 | ||
aa61d352 | 289 | wxString wxComboBox::GetString(unsigned int n) const |
9b1bd0c6 | 290 | { |
e1aae528 | 291 | return wxDoGetStringInList( GetXmList(this), n ); |
9b1bd0c6 MB |
292 | } |
293 | ||
55034339 | 294 | int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const |
9b1bd0c6 | 295 | { |
11e62fe6 WS |
296 | // FIXME: back to base class for not supported value of bCase |
297 | ||
9b1bd0c6 MB |
298 | return wxDoFindStringInList( GetXmList( this ), s ); |
299 | } | |
300 | ||
301 | // Clipboard operations | |
302 | void wxComboBox::Copy() | |
303 | { | |
100f9289 | 304 | XmTextCopy( GetXmText(this), CurrentTime ); |
9b1bd0c6 MB |
305 | } |
306 | ||
307 | void wxComboBox::Cut() | |
308 | { | |
100f9289 | 309 | XmTextCut( GetXmText(this), CurrentTime ); |
9b1bd0c6 MB |
310 | } |
311 | ||
312 | void wxComboBox::Paste() | |
313 | { | |
100f9289 | 314 | XmTextPaste( GetXmText(this) ); |
9b1bd0c6 MB |
315 | } |
316 | ||
317 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) | |
318 | { | |
319 | // TODO | |
320 | } | |
321 | ||
322 | void wxComboBox::SetInsertionPoint(long pos) | |
323 | { | |
100f9289 | 324 | XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos ); |
9b1bd0c6 MB |
325 | } |
326 | ||
327 | void wxComboBox::SetInsertionPointEnd() | |
328 | { | |
100f9289 | 329 | SetInsertionPoint( GetLastPosition() ); |
9b1bd0c6 MB |
330 | } |
331 | ||
332 | long wxComboBox::GetInsertionPoint() const | |
333 | { | |
100f9289 | 334 | return (long)XmTextGetInsertionPosition( GetXmText(this) ); |
9b1bd0c6 MB |
335 | } |
336 | ||
7d8268a1 | 337 | wxTextPos wxComboBox::GetLastPosition() const |
9b1bd0c6 | 338 | { |
100f9289 MB |
339 | XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) ); |
340 | return (long)pos; | |
9b1bd0c6 MB |
341 | } |
342 | ||
343 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
100f9289 MB |
344 | { |
345 | XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to, | |
9b135950 | 346 | wxConstCast(value.mb_str(), char) ); |
9b1bd0c6 MB |
347 | } |
348 | ||
349 | void wxComboBox::Remove(long from, long to) | |
350 | { | |
100f9289 MB |
351 | SetSelection( from, to ); |
352 | XmTextRemove( GetXmText(this) ); | |
9b1bd0c6 MB |
353 | } |
354 | ||
355 | void wxComboBox::SetSelection(long from, long to) | |
356 | { | |
100f9289 MB |
357 | if( to == -1 ) |
358 | to = GetLastPosition(); | |
359 | ||
360 | XmTextSetSelection( GetXmText(this), (XmTextPosition)from, | |
361 | (XmTextPosition)to, (Time)0 ); | |
9b1bd0c6 MB |
362 | } |
363 | ||
364 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, | |
365 | XmComboBoxCallbackStruct * cbs) | |
366 | { | |
367 | wxComboBox *item = (wxComboBox *) clientData; | |
368 | ||
d8d18184 MB |
369 | if( item->m_inSetSelection ) return; |
370 | ||
9b1bd0c6 MB |
371 | switch (cbs->reason) |
372 | { | |
373 | case XmCR_SELECT: | |
374 | #if 0 | |
375 | case XmCR_SINGLE_SELECT: | |
376 | case XmCR_BROWSE_SELECT: | |
377 | #endif | |
378 | { | |
379 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, | |
380 | item->GetId()); | |
d8d18184 | 381 | int idx = cbs->item_position; |
687706f5 KH |
382 | event.SetInt(idx); |
383 | event.SetString( item->GetString (idx) ); | |
9b1bd0c6 MB |
384 | if ( item->HasClientObjectData() ) |
385 | event.SetClientObject( item->GetClientObject(idx) ); | |
386 | else if ( item->HasClientUntypedData() ) | |
387 | event.SetClientData( item->GetClientData(idx) ); | |
687706f5 | 388 | event.SetExtraLong(true); |
9b1bd0c6 | 389 | event.SetEventObject(item); |
d8d18184 | 390 | item->GetEventHandler()->ProcessEvent(event); |
9b1bd0c6 MB |
391 | break; |
392 | } | |
393 | case XmCR_VALUE_CHANGED: | |
394 | { | |
395 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
687706f5 KH |
396 | event.SetInt(-1); |
397 | event.SetString( item->GetValue() ); | |
398 | event.SetExtraLong(true); | |
9b1bd0c6 | 399 | event.SetEventObject(item); |
d8d18184 | 400 | item->GetEventHandler()->ProcessEvent(event); |
9b1bd0c6 MB |
401 | break; |
402 | } | |
403 | default: | |
404 | break; | |
405 | } | |
406 | } | |
407 | ||
408 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
409 | { | |
e1aae528 MB |
410 | if( m_font.Ok() ) |
411 | { | |
412 | wxDoChangeFont( GetXmText(this), m_font ); | |
413 | wxDoChangeFont( GetXmList(this), m_font ); | |
414 | } | |
415 | ||
9b1bd0c6 MB |
416 | // Don't use the base class wxChoice's ChangeFont |
417 | wxWindow::ChangeFont(keepOriginalSize); | |
418 | } | |
419 | ||
420 | void wxComboBox::ChangeBackgroundColour() | |
421 | { | |
422 | wxWindow::ChangeBackgroundColour(); | |
423 | } | |
424 | ||
425 | void wxComboBox::ChangeForegroundColour() | |
426 | { | |
427 | wxWindow::ChangeForegroundColour(); | |
428 | } | |
429 | ||
430 | wxSize wxComboBox::DoGetBestSize() const | |
431 | { | |
e1aae528 MB |
432 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || |
433 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
434 | { | |
435 | Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow; | |
436 | ||
437 | XtVaGetValues( (Widget)m_mainWidget, | |
438 | XmNarrowSize, &arrowW, | |
439 | XmNarrowSpacing, &arrowS, | |
440 | XmNhighlightThickness, &highlight, | |
441 | XmNmarginWidth, &xmargin, | |
442 | XmNmarginHeight, &ymargin, | |
443 | XmNshadowThickness, &shadow, | |
444 | NULL ); | |
445 | ||
446 | wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this ); | |
447 | wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this), | |
448 | this ); | |
449 | ||
450 | // FIXME arbitrary constants | |
451 | return wxSize( listSize.x + arrowW + arrowS + 2 * highlight | |
452 | + 2 * shadow + 2 * xmargin , | |
453 | textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow ); | |
454 | } | |
455 | else | |
456 | return wxWindow::DoGetBestSize(); | |
9b1bd0c6 MB |
457 | } |
458 | ||
459 | #endif // XmVersion >= 2000 | |
460 | ||
461 | #endif // wxUSE_COMBOBOX |