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