Implemented DoGetBestSize for wxListBox, (native) wxComboBox and
[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 SetCanAddEventHandler(true);
118 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
119 pos.x, pos.y, best.x, best.y);
120
121 ChangeBackgroundColour();
122
123 return true;
124 }
125
126 void wxComboBox::AdjustDropDownListSize()
127 {
128 int newListCount = -1, itemCount = GetCount();
129 const int MAX = 12;
130
131 if( !itemCount )
132 newListCount = 1;
133 else if( itemCount < MAX )
134 newListCount = itemCount;
135 else
136 newListCount = MAX;
137
138 XtVaSetValues( GetXmList(this),
139 XmNvisibleItemCount, newListCount,
140 NULL );
141 }
142
143 wxComboBox::~wxComboBox()
144 {
145 DetachWidget((Widget) m_mainWidget); // Removes event handlers
146 XtDestroyWidget((Widget) m_mainWidget);
147 m_mainWidget = (WXWidget) 0;
148 if ( HasClientObjectData() )
149 m_clientDataDict.DestroyData();
150 }
151
152 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
153 {
154 // Necessary so it doesn't call wxChoice::SetSize
155 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
156 }
157
158 wxString wxComboBox::GetValue() const
159 {
160 char* s = XmTextGetString (GetXmText (this));
161 wxString str(s);
162 if (s)
163 XtFree (s);
164 return str;
165 }
166
167 void wxComboBox::SetString(int n, const wxString& s)
168 {
169 wxXmString text(s);
170 Widget listBox = GetXmList(this);
171
172 // delete the item and add it again.
173 // FIXME isn't there a way to change it in place?
174 XmListDeletePos (listBox, n+1);
175 XmListAddItem (listBox, text(), n+1);
176 }
177
178 void wxComboBox::SetValue(const wxString& value)
179 {
180 m_inSetValue = true;
181
182 XtVaSetValues( GetXmText(this),
183 XmNvalue, (char *)value.c_str(),
184 NULL);
185
186 m_inSetValue = false;
187 }
188
189 int wxComboBox::DoAppend(const wxString& item)
190 {
191 wxXmString str( item.c_str() );
192 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0, False);
193 m_noStrings ++;
194 AdjustDropDownListSize();
195
196 return GetCount() - 1;
197 }
198
199 void wxComboBox::Delete(int n)
200 {
201 #ifdef LESSTIF_VERSION
202 XmListDeletePos (GetXmList(this), n + 1);
203 #else
204 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
205 #endif
206
207 m_clientDataDict.Delete(n, HasClientObjectData());
208 m_noStrings--;
209
210 AdjustDropDownListSize();
211 }
212
213 void wxComboBox::Clear()
214 {
215 #ifdef LESSTIF_VERSION
216 XmListDeleteAllItems (GetXmList(this));
217 #else
218 while(m_noStrings > 0)
219 {
220 XmComboBoxDeletePos((Widget) m_mainWidget, m_noStrings--);
221 }
222 #endif
223
224 if ( HasClientObjectData() )
225 m_clientDataDict.DestroyData();
226 m_noStrings = 0;
227 AdjustDropDownListSize();
228 }
229
230 void wxComboBox::SetSelection (int n)
231 {
232 #ifdef LESSTIF_VERSION
233 XmListSelectPos (GetXmList(this), n + 1, false);
234 SetValue(GetString(n));
235 #else
236 wxXmString str( GetString(n).c_str() );
237 XmComboBoxSelectItem((Widget) m_mainWidget, str());
238 #if 0
239 // does it work for Motif
240 XtVaSetValues( (Widget)m_mainWidget,
241 XmNselectedPosition, n + 1,
242 NULL );
243 #endif
244 #endif
245 }
246
247 int wxComboBox::GetSelection (void) const
248 {
249 return wxDoGetSelectionInList( GetXmList( this ) );
250 }
251
252 wxString wxComboBox::GetString(int n) const
253 {
254 return wxDoGetStringInList( GetXmList(this), n );
255 }
256
257 int wxComboBox::FindString(const wxString& s) const
258 {
259 return wxDoFindStringInList( GetXmList( this ), s );
260 }
261
262 // Clipboard operations
263 void wxComboBox::Copy()
264 {
265 XmTextCopy( GetXmText(this), CurrentTime );
266 }
267
268 void wxComboBox::Cut()
269 {
270 XmTextCut( GetXmText(this), CurrentTime );
271 }
272
273 void wxComboBox::Paste()
274 {
275 XmTextPaste( GetXmText(this) );
276 }
277
278 void wxComboBox::SetEditable(bool WXUNUSED(editable))
279 {
280 // TODO
281 }
282
283 void wxComboBox::SetInsertionPoint(long pos)
284 {
285 XmTextSetInsertionPosition( GetXmText(this), (XmTextPosition)pos );
286 }
287
288 void wxComboBox::SetInsertionPointEnd()
289 {
290 SetInsertionPoint( GetLastPosition() );
291 }
292
293 long wxComboBox::GetInsertionPoint() const
294 {
295 return (long)XmTextGetInsertionPosition( GetXmText(this) );
296 }
297
298 long wxComboBox::GetLastPosition() const
299 {
300 XmTextPosition pos = XmTextGetLastPosition( GetXmText(this) );
301 return (long)pos;
302 }
303
304 void wxComboBox::Replace(long from, long to, const wxString& value)
305 {
306 XmTextReplace( GetXmText(this), (XmTextPosition)from, (XmTextPosition)to,
307 (char*)value.c_str() );
308 }
309
310 void wxComboBox::Remove(long from, long to)
311 {
312 SetSelection( from, to );
313 XmTextRemove( GetXmText(this) );
314 }
315
316 void wxComboBox::SetSelection(long from, long to)
317 {
318 if( to == -1 )
319 to = GetLastPosition();
320
321 XmTextSetSelection( GetXmText(this), (XmTextPosition)from,
322 (XmTextPosition)to, (Time)0 );
323 }
324
325 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
326 XmComboBoxCallbackStruct * cbs)
327 {
328 wxComboBox *item = (wxComboBox *) clientData;
329
330 switch (cbs->reason)
331 {
332 case XmCR_SELECT:
333 #if 0
334 case XmCR_SINGLE_SELECT:
335 case XmCR_BROWSE_SELECT:
336 #endif
337 {
338 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
339 item->GetId());
340 int idx = cbs->item_position - 1;
341 event.m_commandInt = idx;
342 event.m_commandString = item->GetString (idx);
343 if ( item->HasClientObjectData() )
344 event.SetClientObject( item->GetClientObject(idx) );
345 else if ( item->HasClientUntypedData() )
346 event.SetClientData( item->GetClientData(idx) );
347 event.m_extraLong = true;
348 event.SetEventObject(item);
349 item->ProcessCommand (event);
350 break;
351 }
352 case XmCR_VALUE_CHANGED:
353 {
354 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
355 event.m_commandInt = -1;
356 event.m_commandString = item->GetValue();
357 event.m_extraLong = true;
358 event.SetEventObject(item);
359 item->ProcessCommand (event);
360 break;
361 }
362 default:
363 break;
364 }
365 }
366
367 void wxComboBox::ChangeFont(bool keepOriginalSize)
368 {
369 if( m_font.Ok() )
370 {
371 wxDoChangeFont( GetXmText(this), m_font );
372 wxDoChangeFont( GetXmList(this), m_font );
373 }
374
375 // Don't use the base class wxChoice's ChangeFont
376 wxWindow::ChangeFont(keepOriginalSize);
377 }
378
379 void wxComboBox::ChangeBackgroundColour()
380 {
381 wxWindow::ChangeBackgroundColour();
382 }
383
384 void wxComboBox::ChangeForegroundColour()
385 {
386 wxWindow::ChangeForegroundColour();
387 }
388
389 wxSize wxComboBox::DoGetBestSize() const
390 {
391 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
392 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
393 {
394 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
395
396 XtVaGetValues( (Widget)m_mainWidget,
397 XmNarrowSize, &arrowW,
398 XmNarrowSpacing, &arrowS,
399 XmNhighlightThickness, &highlight,
400 XmNmarginWidth, &xmargin,
401 XmNmarginHeight, &ymargin,
402 XmNshadowThickness, &shadow,
403 NULL );
404
405 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
406 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
407 this );
408
409 // FIXME arbitrary constants
410 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
411 + 2 * shadow + 2 * xmargin ,
412 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
413 }
414 else
415 return wxWindow::DoGetBestSize();
416 }
417
418 #endif // XmVersion >= 2000
419
420 #endif // wxUSE_COMBOBOX