]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
Don't try to open clipboard if already open
[wxWidgets.git] / src / motif / combobox_native.cpp
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 #include "wx/setup.h"
16
17 #if wxUSE_COMBOBOX
18
19 #include "wx/combobox.h"
20 #include "wx/arrstr.h"
21
22 #ifdef __VMS__
23 #pragma message disable nosimpint
24 #endif
25 #include <Xm/Xm.h>
26 #ifdef __VMS__
27 #pragma message enable nosimpint
28 #endif
29
30 // use the new, shiny combobox for Motif 2.x
31 #if (XmVersion >= 2000)
32
33 #ifdef __VMS__
34 #pragma message disable nosimpint
35 #endif
36 #include <Xm/ComboBox.h>
37 #include <Xm/Text.h>
38 #include <Xm/List.h>
39 #ifdef __VMS__
40 #pragma message enable nosimpint
41 #endif
42
43 #include "wx/motif/private.h"
44
45 // utility
46 static Widget GetXmList( const wxComboBox* cb )
47 {
48 Widget ret;
49 XtVaGetValues( (Widget)cb->GetMainWidget(),
50 XmNlist, &ret,
51 NULL );
52
53 return ret;
54 }
55
56 static Widget GetXmText( const wxComboBox* cb )
57 {
58 Widget ret;
59 XtVaGetValues( (Widget)cb->GetMainWidget(),
60 XmNtextField, &ret,
61 NULL );
62
63 return ret;
64 }
65
66 void wxComboBoxCallback (Widget w, XtPointer clientData,
67 XmComboBoxCallbackStruct * cbs);
68
69 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
70
71 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
72 const wxString& value,
73 const wxPoint& pos,
74 const wxSize& size,
75 int n, const wxString choices[],
76 long style,
77 const wxValidator& validator,
78 const wxString& name)
79 {
80 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
81 return false;
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 ChangeFont(false);
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 != -1 ) best.x = size.x;
119 if( size.y != -1 ) best.y = size.y;
120
121 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
122 pos.x, pos.y, best.x, best.y);
123
124 ChangeBackgroundColour();
125
126 return true;
127 }
128
129 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
130 const wxString& value,
131 const wxPoint& pos,
132 const wxSize& size,
133 const wxArrayString& choices,
134 long style,
135 const wxValidator& validator,
136 const wxString& name)
137 {
138 wxCArrayString chs(choices);
139 return Create(parent, id, value, pos, size, chs.GetCount(),
140 chs.GetStrings(), style, validator, name);
141 }
142
143 void wxComboBox::AdjustDropDownListSize()
144 {
145 int newListCount = -1, itemCount = GetCount();
146 const int MAX = 12;
147
148 if( !itemCount )
149 newListCount = 1;
150 else if( itemCount < MAX )
151 newListCount = itemCount;
152 else
153 newListCount = MAX;
154
155 XtVaSetValues( GetXmList(this),
156 XmNvisibleItemCount, newListCount,
157 NULL );
158 }
159
160 wxComboBox::~wxComboBox()
161 {
162 DetachWidget((Widget) m_mainWidget); // Removes event handlers
163 XtDestroyWidget((Widget) m_mainWidget);
164 m_mainWidget = (WXWidget) 0;
165 if ( HasClientObjectData() )
166 m_clientDataDict.DestroyData();
167 }
168
169 void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
170 {
171 // Necessary so it doesn't call wxChoice::SetSize
172 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
173 }
174
175 wxString wxComboBox::GetValue() const
176 {
177 char* s = XmTextGetString (GetXmText (this));
178 wxString str(s);
179 if (s)
180 XtFree (s);
181 return str;
182 }
183
184 void wxComboBox::SetString(int n, const wxString& s)
185 {
186 wxXmString text(s);
187 Widget listBox = GetXmList(this);
188
189 // delete the item and add it again.
190 // FIXME isn't there a way to change it in place?
191 XmListDeletePos (listBox, n+1);
192 XmListAddItem (listBox, text(), n+1);
193 }
194
195 void wxComboBox::SetValue(const wxString& value)
196 {
197 m_inSetValue = true;
198
199 // Fix crash; probably an OpenMotif bug
200 const char* val = value.c_str() ? value.c_str() : "";
201 XtVaSetValues( GetXmText(this),
202 XmNvalue, wxConstCast(val, char),
203 NULL);
204
205 m_inSetValue = false;
206 }
207
208 int wxComboBox::DoAppend(const wxString& item)
209 {
210 wxXmString str( item.c_str() );
211 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
212 m_noStrings ++;
213 AdjustDropDownListSize();
214
215 return GetCount() - 1;
216 }
217
218 int wxComboBox::DoInsert(const wxString& item, int pos)
219 {
220 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
221 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
222
223 if (pos == GetCount())
224 return DoAppend(item);
225
226 wxXmString str( item.c_str() );
227 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False);
228 m_noStrings ++;
229 AdjustDropDownListSize();
230
231 return GetCount() - 1;
232 }
233
234 void wxComboBox::Delete(int n)
235 {
236 #ifdef LESSTIF_VERSION
237 XmListDeletePos (GetXmList(this), n + 1);
238 #else
239 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
240 #endif
241
242 m_clientDataDict.Delete(n, HasClientObjectData());
243 m_noStrings--;
244
245 AdjustDropDownListSize();
246 }
247
248 void wxComboBox::Clear()
249 {
250 #ifdef LESSTIF_VERSION
251 XmListDeleteAllItems (GetXmList(this));
252 #else
253 while(m_noStrings > 0)
254 {
255 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
256 }
257 #endif
258
259 if ( HasClientObjectData() )
260 m_clientDataDict.DestroyData();
261 m_noStrings = 0;
262 AdjustDropDownListSize();
263 }
264
265 void wxComboBox::SetSelection (int n)
266 {
267 m_inSetSelection = true;
268
269 #if wxCHECK_LESSTIF()
270 XmListSelectPos (GetXmList(this), n + 1, false);
271 SetValue(GetString(n));
272 #else
273 #if 0
274 wxXmString str( GetString(n).c_str() );
275 XmComboBoxSelectItem((Widget) m_mainWidget, str());
276 #endif
277 XtVaSetValues( (Widget)m_mainWidget,
278 XmNselectedPosition, n,
279 NULL );
280 #endif
281
282 m_inSetSelection = false;
283 }
284
285 int wxComboBox::GetSelection (void) const
286 {
287 return wxDoGetSelectionInList( GetXmList( this ) );
288 }
289
290 wxString wxComboBox::GetString(int n) const
291 {
292 return wxDoGetStringInList( GetXmList(this), n );
293 }
294
295 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
296 {
297 // FIXME: back to base class for not supported value of bCase
298
299 return wxDoFindStringInList( GetXmList( this ), s );
300 }
301
302 // Clipboard operations
303 void wxComboBox::Copy()
304 {
305 XmTextCopy( GetXmText(this), CurrentTime );
306 }
307
308 void wxComboBox::Cut()
309 {
310 XmTextCut( GetXmText(this), CurrentTime );
311 }
312
313 void wxComboBox::Paste()
314 {
315 XmTextPaste( GetXmText(this) );
316 }
317
318 void wxComboBox::SetEditable(bool WXUNUSED(editable))
319 {
320 // TODO
321 }
322
323 void wxComboBox::SetInsertionPoint(long pos)
324 {
325 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
326 }
327
328 void wxComboBox::SetInsertionPointEnd()
329 {
330 SetInsertionPoint( GetLastPosition() );
331 }
332
333 long wxComboBox::GetInsertionPoint() const
334 {
335 return (long)XmTextGetInsertionPosition( GetXmText(this) );
336 }
337
338 wxTextPos wxComboBox::GetLastPosition() const
339 {
340 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
341 return (long)pos;
342 }
343
344 void wxComboBox::Replace(long from, long to, const wxString& value)
345 {
346 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
347 wxConstCast(value.c_str(), char) );
348 }
349
350 void wxComboBox::Remove(long from, long to)
351 {
352 SetSelection( from, to );
353 XmTextRemove( GetXmText(this) );
354 }
355
356 void wxComboBox::SetSelection(long from, long to)
357 {
358 if( to == -1 )
359 to = GetLastPosition();
360
361 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
362 (XmTextPosition)to, (Time)0 );
363 }
364
365 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
366 XmComboBoxCallbackStruct * cbs)
367 {
368 wxComboBox *item = (wxComboBox *) clientData;
369
370 if( item->m_inSetSelection ) return;
371
372 switch (cbs->reason)
373 {
374 case XmCR_SELECT:
375 #if 0
376 case XmCR_SINGLE_SELECT:
377 case XmCR_BROWSE_SELECT:
378 #endif
379 {
380 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
381 item->GetId());
382 int idx = cbs->item_position;
383 event.SetInt(idx);
384 event.SetString( item->GetString (idx) );
385 if ( item->HasClientObjectData() )
386 event.SetClientObject( item->GetClientObject(idx) );
387 else if ( item->HasClientUntypedData() )
388 event.SetClientData( item->GetClientData(idx) );
389 event.SetExtraLong(true);
390 event.SetEventObject(item);
391 item->GetEventHandler()->ProcessEvent(event);
392 break;
393 }
394 case XmCR_VALUE_CHANGED:
395 {
396 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
397 event.SetInt(-1);
398 event.SetString( item->GetValue() );
399 event.SetExtraLong(true);
400 event.SetEventObject(item);
401 item->GetEventHandler()->ProcessEvent(event);
402 break;
403 }
404 default:
405 break;
406 }
407 }
408
409 void wxComboBox::ChangeFont(bool keepOriginalSize)
410 {
411 if( m_font.Ok() )
412 {
413 wxDoChangeFont( GetXmText(this), m_font );
414 wxDoChangeFont( GetXmList(this), m_font );
415 }
416
417 // Don't use the base class wxChoice's ChangeFont
418 wxWindow::ChangeFont(keepOriginalSize);
419 }
420
421 void wxComboBox::ChangeBackgroundColour()
422 {
423 wxWindow::ChangeBackgroundColour();
424 }
425
426 void wxComboBox::ChangeForegroundColour()
427 {
428 wxWindow::ChangeForegroundColour();
429 }
430
431 wxSize wxComboBox::DoGetBestSize() const
432 {
433 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
434 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
435 {
436 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
437
438 XtVaGetValues( (Widget)m_mainWidget,
439 XmNarrowSize, &arrowW,
440 XmNarrowSpacing, &arrowS,
441 XmNhighlightThickness, &highlight,
442 XmNmarginWidth, &xmargin,
443 XmNmarginHeight, &ymargin,
444 XmNshadowThickness, &shadow,
445 NULL );
446
447 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
448 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
449 this );
450
451 // FIXME arbitrary constants
452 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
453 + 2 * shadow + 2 * xmargin ,
454 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
455 }
456 else
457 return wxWindow::DoGetBestSize();
458 }
459
460 #endif // XmVersion >= 2000
461
462 #endif // wxUSE_COMBOBOX