]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
Fix assorted typos in comments and other non-code.
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_COMBOBOX
15
16 #include "wx/combobox.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/arrstr.h"
20 #endif
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 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 PreCreation();
81
82 Widget parentWidget = (Widget) parent->GetClientWidget();
83
84 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
85 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
86 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
87 // default to wxCB_DROPDOWN
88 XmDROP_DOWN_COMBO_BOX;
89 if( cb_type == XmDROP_DOWN_COMBO_BOX )
90 SetWindowStyle( style | wxCB_DROPDOWN );
91
92 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
93 xmComboBoxWidgetClass, parentWidget,
94 XmNcomboBoxType, cb_type,
95 NULL);
96
97 m_mainWidget = (Widget) buttonWidget;
98
99 int i;
100 for ( i = 0; i < n; ++i)
101 Append( choices[i] );
102
103 XtManageChild (buttonWidget);
104
105 SetValue(value);
106
107 XtAddCallback (buttonWidget, XmNselectionCallback,
108 (XtCallbackProc) wxComboBoxCallback,
109 (XtPointer) this);
110 XtAddCallback (GetXmText(this), XmNvalueChangedCallback,
111 (XtCallbackProc) wxComboBoxCallback,
112 (XtPointer) this);
113
114 wxSize best = GetBestSize();
115 if( size.x != wxDefaultCoord ) best.x = size.x;
116 if( size.y != wxDefaultCoord ) best.y = size.y;
117
118 PostCreation();
119 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
120 pos.x, pos.y, best.x, best.y);
121
122 return true;
123 }
124
125 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
126 const wxString& value,
127 const wxPoint& pos,
128 const wxSize& size,
129 const wxArrayString& choices,
130 long style,
131 const wxValidator& validator,
132 const wxString& name)
133 {
134 wxCArrayString chs(choices);
135 return Create(parent, id, value, pos, size, chs.GetCount(),
136 chs.GetStrings(), style, validator, name);
137 }
138
139 void wxComboBox::AdjustDropDownListSize()
140 {
141 int newListCount = -1, itemCount = GetCount();
142 const int MAX = 12;
143
144 if( !itemCount )
145 newListCount = 1;
146 else if( itemCount < MAX )
147 newListCount = itemCount;
148 else
149 newListCount = MAX;
150
151 XtVaSetValues( GetXmList(this),
152 XmNvisibleItemCount, newListCount,
153 NULL );
154 }
155
156 wxComboBox::~wxComboBox()
157 {
158 DetachWidget((Widget) m_mainWidget); // Removes event handlers
159 XtDestroyWidget((Widget) m_mainWidget);
160 m_mainWidget = (WXWidget) 0;
161 }
162
163 void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
164 {
165 // Necessary so it doesn't call wxChoice::SetSize
166 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
167 }
168
169 void wxComboBox::SetString(unsigned int n, const wxString& s)
170 {
171 wxXmString text(s);
172 Widget listBox = GetXmList(this);
173
174 // delete the item and add it again.
175 // FIXME isn't there a way to change it in place?
176 XmListDeletePos (listBox, n+1);
177 XmListAddItem (listBox, text(), n+1);
178 }
179
180 void wxComboBox::SetValue(const wxString& value)
181 {
182 m_inSetValue = true;
183
184 XtVaSetValues( GetXmText(this),
185 XmNvalue, (const char*)value.mb_str(),
186 NULL);
187
188 m_inSetValue = false;
189 }
190
191 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
192 unsigned int pos,
193 void **clientData, wxClientDataType type)
194 {
195 const unsigned int numItems = items.GetCount();
196
197 AllocClientData(numItems);
198 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
199 {
200 wxXmString str( items[i].c_str() );
201 XmComboBoxAddItem((Widget) m_mainWidget, str(),
202 GetMotifPosition(pos), False);
203
204 InsertNewItemClientData(pos, clientData, i, type);
205 }
206
207 AdjustDropDownListSize();
208
209 return pos - 1;
210 }
211
212 void wxComboBox::DoDeleteOneItem(unsigned int n)
213 {
214 #ifdef LESSTIF_VERSION
215 XmListDeletePos (GetXmList(this), n + 1);
216 #else
217 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
218 #endif
219
220 wxControlWithItems::DoDeleteOneItem(n);
221 m_stringArray.RemoveAt(size_t(n));
222
223 AdjustDropDownListSize();
224 }
225
226 void wxComboBox::Clear()
227 {
228 #ifdef LESSTIF_VERSION
229 XmListDeleteAllItems (GetXmList(this));
230 #else
231 size_t n = m_stringArray.GetCount();
232 while(n > 0)
233 {
234 XmComboBoxDeletePos((Widget) m_mainWidget, n--);
235 }
236 #endif
237
238 m_stringArray.Clear();
239 AdjustDropDownListSize();
240
241 wxTextEntry::Clear();
242 }
243
244 void wxComboBox::SetSelection (int n)
245 {
246 m_inSetSelection = true;
247
248 #if wxCHECK_LESSTIF()
249 XmListSelectPos (GetXmList(this), n + 1, false);
250 SetValue(GetString(n));
251 #else
252 #if 0
253 wxXmString str(GetString(n).c_str());
254 XmComboBoxSelectItem((Widget) m_mainWidget, str());
255 #endif
256 XtVaSetValues( (Widget)m_mainWidget,
257 XmNselectedPosition, n,
258 NULL );
259 #endif
260
261 m_inSetSelection = false;
262 }
263
264 int wxComboBox::GetSelection() const
265 {
266 return wxDoGetSelectionInList( GetXmList( this ) );
267 }
268
269 wxString wxComboBox::GetString(unsigned int n) const
270 {
271 return wxDoGetStringInList( GetXmList(this), n );
272 }
273
274 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
275 {
276 // FIXME: back to base class for not supported value of bCase
277
278 return wxDoFindStringInList( GetXmList( this ), s );
279 }
280
281 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
282 XmComboBoxCallbackStruct * cbs)
283 {
284 wxComboBox *item = (wxComboBox *) clientData;
285
286 if( item->m_inSetSelection ) return;
287
288 switch (cbs->reason)
289 {
290 case XmCR_SELECT:
291 #if 0
292 case XmCR_SINGLE_SELECT:
293 case XmCR_BROWSE_SELECT:
294 #endif
295 {
296 wxCommandEvent event (wxEVT_COMBOBOX,
297 item->GetId());
298 int idx = cbs->item_position;
299 event.SetInt(idx);
300 event.SetString( item->GetString (idx) );
301 if ( item->HasClientObjectData() )
302 event.SetClientObject( item->GetClientObject(idx) );
303 else if ( item->HasClientUntypedData() )
304 event.SetClientData( item->GetClientData(idx) );
305 event.SetExtraLong(true);
306 event.SetEventObject(item);
307 item->HandleWindowEvent(event);
308 break;
309 }
310 case XmCR_VALUE_CHANGED:
311 {
312 wxCommandEvent event (wxEVT_TEXT, item->GetId());
313 event.SetInt(-1);
314 event.SetString( item->GetValue() );
315 event.SetExtraLong(true);
316 event.SetEventObject(item);
317 item->HandleWindowEvent(event);
318 break;
319 }
320 default:
321 break;
322 }
323 }
324
325 void wxComboBox::ChangeFont(bool keepOriginalSize)
326 {
327 if( m_font.IsOk() && m_mainWidget != NULL )
328 {
329 wxDoChangeFont( GetXmText(this), m_font );
330 wxDoChangeFont( GetXmList(this), m_font );
331 }
332
333 // Don't use the base class wxChoice's ChangeFont
334 wxWindow::ChangeFont(keepOriginalSize);
335 }
336
337 void wxComboBox::ChangeBackgroundColour()
338 {
339 wxWindow::ChangeBackgroundColour();
340 }
341
342 void wxComboBox::ChangeForegroundColour()
343 {
344 wxWindow::ChangeForegroundColour();
345 }
346
347 wxSize wxComboBox::DoGetBestSize() const
348 {
349 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
350 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
351 {
352 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
353
354 XtVaGetValues( (Widget)m_mainWidget,
355 XmNarrowSize, &arrowW,
356 XmNarrowSpacing, &arrowS,
357 XmNhighlightThickness, &highlight,
358 XmNmarginWidth, &xmargin,
359 XmNmarginHeight, &ymargin,
360 XmNshadowThickness, &shadow,
361 NULL );
362
363 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
364 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
365 this );
366
367 // FIXME arbitrary constants
368 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
369 + 2 * shadow + 2 * xmargin ,
370 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
371 }
372 else
373 return wxWindow::DoGetBestSize();
374 }
375
376 WXWidget wxComboBox::GetTextWidget() const
377 {
378 return (WXWidget)GetXmText(this);
379 }
380
381 #endif // XmVersion >= 2000
382
383 #endif // wxUSE_COMBOBOX