]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
use a filename, not URL, when quering its modification time
[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 #if wxUSE_COMBOBOX
16
17 #include "wx/combobox.h"
18 #include "wx/arrstr.h"
19
20 #ifdef __VMS__
21 #pragma message disable nosimpint
22 #endif
23 #include <Xm/Xm.h>
24 #ifdef __VMS__
25 #pragma message enable nosimpint
26 #endif
27
28 // use the new, shiny combobox for Motif 2.x
29 #if (XmVersion >= 2000)
30
31 #ifdef __VMS__
32 #pragma message disable nosimpint
33 #endif
34 #include <Xm/ComboBox.h>
35 #include <Xm/Text.h>
36 #include <Xm/List.h>
37 #ifdef __VMS__
38 #pragma message enable nosimpint
39 #endif
40
41 #include "wx/motif/private.h"
42
43 // utility
44 static Widget GetXmList( const wxComboBox* cb )
45 {
46 Widget ret;
47 XtVaGetValues( (Widget)cb->GetMainWidget(),
48 XmNlist, &ret,
49 NULL );
50
51 return ret;
52 }
53
54 static Widget GetXmText( const wxComboBox* cb )
55 {
56 Widget ret;
57 XtVaGetValues( (Widget)cb->GetMainWidget(),
58 XmNtextField, &ret,
59 NULL );
60
61 return ret;
62 }
63
64 void wxComboBoxCallback (Widget w, XtPointer clientData,
65 XmComboBoxCallbackStruct * cbs);
66
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
68
69 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
70 const wxString& value,
71 const wxPoint& pos,
72 const wxSize& size,
73 int n, const wxString choices[],
74 long style,
75 const wxValidator& validator,
76 const wxString& name)
77 {
78 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
79 return false;
80
81 Widget parentWidget = (Widget) parent->GetClientWidget();
82
83 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
84 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
85 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
86 // default to wxCB_DROPDOWN
87 XmDROP_DOWN_COMBO_BOX;
88 if( cb_type == XmDROP_DOWN_COMBO_BOX )
89 SetWindowStyle( style | wxCB_DROPDOWN );
90
91 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
92 xmComboBoxWidgetClass, parentWidget,
93 XmNcomboBoxType, cb_type,
94 NULL);
95
96 m_mainWidget = (Widget) buttonWidget;
97
98 int i;
99 for ( i = 0; i < n; ++i)
100 Append( choices[i] );
101
102 XtManageChild (buttonWidget);
103
104 SetValue(value);
105
106 ChangeFont(false);
107
108 XtAddCallback (buttonWidget, XmNselectionCallback,
109 (XtCallbackProc) wxComboBoxCallback,
110 (XtPointer) this);
111 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
112 (XtCallbackProc) wxComboBoxCallback,
113 (XtPointer) this);
114
115 wxSize best = GetBestSize();
116 if( size.x != wxDefaultCoord ) best.x = size.x;
117 if( size.y != wxDefaultCoord ) best.y = size.y;
118
119 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
120 pos.x, pos.y, best.x, best.y);
121
122 ChangeBackgroundColour();
123
124 return true;
125 }
126
127 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
128 const wxString& value,
129 const wxPoint& pos,
130 const wxSize& size,
131 const wxArrayString& choices,
132 long style,
133 const wxValidator& validator,
134 const wxString& name)
135 {
136 wxCArrayString chs(choices);
137 return Create(parent, id, value, pos, size, chs.GetCount(),
138 chs.GetStrings(), style, validator, name);
139 }
140
141 void wxComboBox::AdjustDropDownListSize()
142 {
143 int newListCount = -1, itemCount = GetCount();
144 const int MAX = 12;
145
146 if( !itemCount )
147 newListCount = 1;
148 else if( itemCount < MAX )
149 newListCount = itemCount;
150 else
151 newListCount = MAX;
152
153 XtVaSetValues( GetXmList(this),
154 XmNvisibleItemCount, newListCount,
155 NULL );
156 }
157
158 wxComboBox::~wxComboBox()
159 {
160 DetachWidget((Widget) m_mainWidget); // Removes event handlers
161 XtDestroyWidget((Widget) m_mainWidget);
162 m_mainWidget = (WXWidget) 0;
163 if ( HasClientObjectData() )
164 m_clientDataDict.DestroyData();
165 }
166
167 void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
168 {
169 // Necessary so it doesn't call wxChoice::SetSize
170 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
171 }
172
173 wxString wxComboBox::GetValue() const
174 {
175 char* s = XmTextGetString (GetXmText (this));
176 wxString str(s);
177 if (s)
178 XtFree (s);
179 return str;
180 }
181
182 void wxComboBox::SetString(unsigned int n, const wxString& s)
183 {
184 wxXmString text(s);
185 Widget listBox = GetXmList(this);
186
187 // delete the item and add it again.
188 // FIXME isn't there a way to change it in place?
189 XmListDeletePos (listBox, n+1);
190 XmListAddItem (listBox, text(), n+1);
191 }
192
193 void wxComboBox::SetValue(const wxString& value)
194 {
195 m_inSetValue = true;
196
197 // Fix crash; probably an OpenMotif bug
198 const char* val = value.c_str() ? value.c_str() : "";
199 XtVaSetValues( GetXmText(this),
200 XmNvalue, wxConstCast(val, char),
201 NULL);
202
203 m_inSetValue = false;
204 }
205
206 int wxComboBox::DoAppend(const wxString& item)
207 {
208 wxXmString str( item.c_str() );
209 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
210 m_noStrings ++;
211 AdjustDropDownListSize();
212
213 return GetCount() - 1;
214 }
215
216 int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
217 {
218 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
219 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
220
221 if (pos == GetCount())
222 return DoAppend(item);
223
224 wxXmString str( item.c_str() );
225 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False);
226 m_noStrings ++;
227 AdjustDropDownListSize();
228
229 return GetCount() - 1;
230 }
231
232 void wxComboBox::Delete(unsigned int n)
233 {
234 #ifdef LESSTIF_VERSION
235 XmListDeletePos (GetXmList(this), n + 1);
236 #else
237 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
238 #endif
239
240 m_clientDataDict.Delete(n, HasClientObjectData());
241 m_noStrings--;
242
243 AdjustDropDownListSize();
244 }
245
246 void wxComboBox::Clear()
247 {
248 #ifdef LESSTIF_VERSION
249 XmListDeleteAllItems (GetXmList(this));
250 #else
251 while(m_noStrings > 0)
252 {
253 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
254 }
255 #endif
256
257 if ( HasClientObjectData() )
258 m_clientDataDict.DestroyData();
259 m_noStrings = 0;
260 AdjustDropDownListSize();
261 }
262
263 void wxComboBox::SetSelection (int n)
264 {
265 m_inSetSelection = true;
266
267 #if wxCHECK_LESSTIF()
268 XmListSelectPos (GetXmList(this), n + 1, false);
269 SetValue(GetString(n));
270 #else
271 #if 0
272 wxXmString str(GetString(n).c_str());
273 XmComboBoxSelectItem((Widget) m_mainWidget, str());
274 #endif
275 XtVaSetValues( (Widget)m_mainWidget,
276 XmNselectedPosition, n,
277 NULL );
278 #endif
279
280 m_inSetSelection = false;
281 }
282
283 int wxComboBox::GetSelection (void) const
284 {
285 return wxDoGetSelectionInList( GetXmList( this ) );
286 }
287
288 wxString wxComboBox::GetString(unsigned int n) const
289 {
290 return wxDoGetStringInList( GetXmList(this), n );
291 }
292
293 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
294 {
295 // FIXME: back to base class for not supported value of bCase
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 wxTextPos 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.SetInt(idx);
382 event.SetString( 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.SetExtraLong(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.SetInt(-1);
396 event.SetString( item->GetValue() );
397 event.SetExtraLong(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