]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
added wxUSE_MDI (but forgot to commit before)
[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 // 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 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) const
296 {
297 return wxDoFindStringInList( GetXmList( this ), s );
298 }
299
300 // Clipboard operations
301 void wxComboBox::Copy()
302 {
303 XmTextCopy( GetXmText(this), CurrentTime );
304 }
305
306 void wxComboBox::Cut()
307 {
308 XmTextCut( GetXmText(this), CurrentTime );
309 }
310
311 void wxComboBox::Paste()
312 {
313 XmTextPaste( GetXmText(this) );
314 }
315
316 void wxComboBox::SetEditable(bool WXUNUSED(editable))
317 {
318 // TODO
319 }
320
321 void wxComboBox::SetInsertionPoint(long pos)
322 {
323 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
324 }
325
326 void wxComboBox::SetInsertionPointEnd()
327 {
328 SetInsertionPoint( GetLastPosition() );
329 }
330
331 long wxComboBox::GetInsertionPoint() const
332 {
333 return (long)XmTextGetInsertionPosition( GetXmText(this) );
334 }
335
336 long wxComboBox::GetLastPosition() const
337 {
338 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
339 return (long)pos;
340 }
341
342 void wxComboBox::Replace(long from, long to, const wxString& value)
343 {
344 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
345 wxConstCast(value.c_str(), char) );
346 }
347
348 void wxComboBox::Remove(long from, long to)
349 {
350 SetSelection( from, to );
351 XmTextRemove( GetXmText(this) );
352 }
353
354 void wxComboBox::SetSelection(long from, long to)
355 {
356 if( to == -1 )
357 to = GetLastPosition();
358
359 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
360 (XmTextPosition)to, (Time)0 );
361 }
362
363 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
364 XmComboBoxCallbackStruct * cbs)
365 {
366 wxComboBox *item = (wxComboBox *) clientData;
367
368 if( item->m_inSetSelection ) return;
369
370 switch (cbs->reason)
371 {
372 case XmCR_SELECT:
373 #if 0
374 case XmCR_SINGLE_SELECT:
375 case XmCR_BROWSE_SELECT:
376 #endif
377 {
378 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
379 item->GetId());
380 int idx = cbs->item_position;
381 event.m_commandInt = idx;
382 event.m_commandString = item->GetString (idx);
383 if ( item->HasClientObjectData() )
384 event.SetClientObject( item->GetClientObject(idx) );
385 else if ( item->HasClientUntypedData() )
386 event.SetClientData( item->GetClientData(idx) );
387 event.m_extraLong = true;
388 event.SetEventObject(item);
389 item->GetEventHandler()->ProcessEvent(event);
390 break;
391 }
392 case XmCR_VALUE_CHANGED:
393 {
394 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
395 event.m_commandInt = -1;
396 event.m_commandString = item->GetValue();
397 event.m_extraLong = true;
398 event.SetEventObject(item);
399 item->GetEventHandler()->ProcessEvent(event);
400 break;
401 }
402 default:
403 break;
404 }
405 }
406
407 void wxComboBox::ChangeFont(bool keepOriginalSize)
408 {
409 if( m_font.Ok() )
410 {
411 wxDoChangeFont( GetXmText(this), m_font );
412 wxDoChangeFont( GetXmList(this), m_font );
413 }
414
415 // Don't use the base class wxChoice's ChangeFont
416 wxWindow::ChangeFont(keepOriginalSize);
417 }
418
419 void wxComboBox::ChangeBackgroundColour()
420 {
421 wxWindow::ChangeBackgroundColour();
422 }
423
424 void wxComboBox::ChangeForegroundColour()
425 {
426 wxWindow::ChangeForegroundColour();
427 }
428
429 wxSize wxComboBox::DoGetBestSize() const
430 {
431 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
432 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
433 {
434 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
435
436 XtVaGetValues( (Widget)m_mainWidget,
437 XmNarrowSize, &arrowW,
438 XmNarrowSpacing, &arrowS,
439 XmNhighlightThickness, &highlight,
440 XmNmarginWidth, &xmargin,
441 XmNmarginHeight, &ymargin,
442 XmNshadowThickness, &shadow,
443 NULL );
444
445 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
446 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
447 this );
448
449 // FIXME arbitrary constants
450 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
451 + 2 * shadow + 2 * xmargin ,
452 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
453 }
454 else
455 return wxWindow::DoGetBestSize();
456 }
457
458 #endif // XmVersion >= 2000
459
460 #endif // wxUSE_COMBOBOX