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