]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox_native.cpp
Catch attempts to create a window with itself as parent.
[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
19 #ifndef WX_PRECOMP
20 #include "wx/arrstr.h"
21 #endif
22
23 #ifdef __VMS__
24 #pragma message disable nosimpint
25 #endif
26 #include <Xm/Xm.h>
27 #ifdef __VMS__
28 #pragma message enable nosimpint
29 #endif
30
31 // use the new, shiny combobox for Motif 2.x
32 #if (XmVersion >= 2000)
33
34 #ifdef __VMS__
35 #pragma message disable nosimpint
36 #endif
37 #include <Xm/ComboBox.h>
38 #include <Xm/Text.h>
39 #include <Xm/List.h>
40 #ifdef __VMS__
41 #pragma message enable nosimpint
42 #endif
43
44 #include "wx/motif/private.h"
45
46 // utility
47 static Widget GetXmList( const wxComboBox* cb )
48 {
49 Widget ret;
50 XtVaGetValues( (Widget)cb->GetMainWidget(),
51 XmNlist, &ret,
52 NULL );
53
54 return ret;
55 }
56
57 static Widget GetXmText( const wxComboBox* cb )
58 {
59 Widget ret;
60 XtVaGetValues( (Widget)cb->GetMainWidget(),
61 XmNtextField, &ret,
62 NULL );
63
64 return ret;
65 }
66
67 void wxComboBoxCallback (Widget w, XtPointer clientData,
68 XmComboBoxCallbackStruct * cbs);
69
70 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
71
72 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
73 const wxString& value,
74 const wxPoint& pos,
75 const wxSize& size,
76 int n, const wxString choices[],
77 long style,
78 const wxValidator& validator,
79 const wxString& name)
80 {
81 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
82 return false;
83 PreCreation();
84
85 Widget parentWidget = (Widget) parent->GetClientWidget();
86
87 int cb_type = ( style & wxCB_SIMPLE ) ? XmCOMBO_BOX :
88 ( style & wxCB_READONLY ) ? XmDROP_DOWN_LIST :
89 ( style & wxCB_DROPDOWN ) ? XmDROP_DOWN_COMBO_BOX :
90 // default to wxCB_DROPDOWN
91 XmDROP_DOWN_COMBO_BOX;
92 if( cb_type == XmDROP_DOWN_COMBO_BOX )
93 SetWindowStyle( style | wxCB_DROPDOWN );
94
95 Widget buttonWidget= XtVaCreateManagedWidget(name.c_str(),
96 xmComboBoxWidgetClass, parentWidget,
97 XmNcomboBoxType, cb_type,
98 NULL);
99
100 m_mainWidget = (Widget) buttonWidget;
101
102 int i;
103 for ( i = 0; i < n; ++i)
104 Append( choices[i] );
105
106 XtManageChild (buttonWidget);
107
108 SetValue(value);
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 != wxDefaultCoord ) best.x = size.x;
119 if( size.y != wxDefaultCoord ) best.y = size.y;
120
121 PostCreation();
122 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
123 pos.x, pos.y, best.x, best.y);
124
125 return true;
126 }
127
128 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
129 const wxString& value,
130 const wxPoint& pos,
131 const wxSize& size,
132 const wxArrayString& choices,
133 long style,
134 const wxValidator& validator,
135 const wxString& name)
136 {
137 wxCArrayString chs(choices);
138 return Create(parent, id, value, pos, size, chs.GetCount(),
139 chs.GetStrings(), style, validator, name);
140 }
141
142 void wxComboBox::AdjustDropDownListSize()
143 {
144 int newListCount = -1, itemCount = GetCount();
145 const int MAX = 12;
146
147 if( !itemCount )
148 newListCount = 1;
149 else if( itemCount < MAX )
150 newListCount = itemCount;
151 else
152 newListCount = MAX;
153
154 XtVaSetValues( GetXmList(this),
155 XmNvisibleItemCount, newListCount,
156 NULL );
157 }
158
159 wxComboBox::~wxComboBox()
160 {
161 DetachWidget((Widget) m_mainWidget); // Removes event handlers
162 XtDestroyWidget((Widget) m_mainWidget);
163 m_mainWidget = (WXWidget) 0;
164 }
165
166 void wxComboBox::DoSetSize(int x, int y, int width, int WXUNUSED(height), int sizeFlags)
167 {
168 // Necessary so it doesn't call wxChoice::SetSize
169 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
170 }
171
172 void wxComboBox::SetString(unsigned int n, const wxString& s)
173 {
174 wxXmString text(s);
175 Widget listBox = GetXmList(this);
176
177 // delete the item and add it again.
178 // FIXME isn't there a way to change it in place?
179 XmListDeletePos (listBox, n+1);
180 XmListAddItem (listBox, text(), n+1);
181 }
182
183 void wxComboBox::SetValue(const wxString& value)
184 {
185 m_inSetValue = true;
186
187 XtVaSetValues( GetXmText(this),
188 XmNvalue, (const char*)value.mb_str(),
189 NULL);
190
191 m_inSetValue = false;
192 }
193
194 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
195 unsigned int pos,
196 void **clientData, wxClientDataType type)
197 {
198 const unsigned int numItems = items.GetCount();
199
200 AllocClientData(numItems);
201 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
202 {
203 wxXmString str( items[i].c_str() );
204 XmComboBoxAddItem((Widget) m_mainWidget, str(),
205 GetMotifPosition(pos), False);
206
207 InsertNewItemClientData(pos, clientData, i, type);
208 }
209
210 AdjustDropDownListSize();
211
212 return pos - 1;
213 }
214
215 void wxComboBox::DoDeleteOneItem(unsigned int n)
216 {
217 #ifdef LESSTIF_VERSION
218 XmListDeletePos (GetXmList(this), n + 1);
219 #else
220 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
221 #endif
222
223 wxControlWithItems::DoDeleteOneItem(n);
224 m_stringArray.RemoveAt(size_t(n));
225
226 AdjustDropDownListSize();
227 }
228
229 void wxComboBox::Clear()
230 {
231 #ifdef LESSTIF_VERSION
232 XmListDeleteAllItems (GetXmList(this));
233 #else
234 size_t n = m_stringArray.GetCount();
235 while(n > 0)
236 {
237 XmComboBoxDeletePos((Widget) m_mainWidget, n--);
238 }
239 #endif
240
241 m_stringArray.Clear();
242 AdjustDropDownListSize();
243
244 wxTextEntry::Clear();
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() const
268 {
269 return wxDoGetSelectionInList( GetXmList( this ) );
270 }
271
272 wxString wxComboBox::GetString(unsigned int n) const
273 {
274 return wxDoGetStringInList( GetXmList(this), n );
275 }
276
277 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
278 {
279 // FIXME: back to base class for not supported value of bCase
280
281 return wxDoFindStringInList( GetXmList( this ), s );
282 }
283
284 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
285 XmComboBoxCallbackStruct * cbs)
286 {
287 wxComboBox *item = (wxComboBox *) clientData;
288
289 if( item->m_inSetSelection ) return;
290
291 switch (cbs->reason)
292 {
293 case XmCR_SELECT:
294 #if 0
295 case XmCR_SINGLE_SELECT:
296 case XmCR_BROWSE_SELECT:
297 #endif
298 {
299 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
300 item->GetId());
301 int idx = cbs->item_position;
302 event.SetInt(idx);
303 event.SetString( item->GetString (idx) );
304 if ( item->HasClientObjectData() )
305 event.SetClientObject( item->GetClientObject(idx) );
306 else if ( item->HasClientUntypedData() )
307 event.SetClientData( item->GetClientData(idx) );
308 event.SetExtraLong(true);
309 event.SetEventObject(item);
310 item->HandleWindowEvent(event);
311 break;
312 }
313 case XmCR_VALUE_CHANGED:
314 {
315 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
316 event.SetInt(-1);
317 event.SetString( item->GetValue() );
318 event.SetExtraLong(true);
319 event.SetEventObject(item);
320 item->HandleWindowEvent(event);
321 break;
322 }
323 default:
324 break;
325 }
326 }
327
328 void wxComboBox::ChangeFont(bool keepOriginalSize)
329 {
330 if( m_font.IsOk() && m_mainWidget != NULL )
331 {
332 wxDoChangeFont( GetXmText(this), m_font );
333 wxDoChangeFont( GetXmList(this), m_font );
334 }
335
336 // Don't use the base class wxChoice's ChangeFont
337 wxWindow::ChangeFont(keepOriginalSize);
338 }
339
340 void wxComboBox::ChangeBackgroundColour()
341 {
342 wxWindow::ChangeBackgroundColour();
343 }
344
345 void wxComboBox::ChangeForegroundColour()
346 {
347 wxWindow::ChangeForegroundColour();
348 }
349
350 wxSize wxComboBox::DoGetBestSize() const
351 {
352 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
353 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
354 {
355 Dimension arrowW, arrowS, highlight, xmargin, ymargin, shadow;
356
357 XtVaGetValues( (Widget)m_mainWidget,
358 XmNarrowSize, &arrowW,
359 XmNarrowSpacing, &arrowS,
360 XmNhighlightThickness, &highlight,
361 XmNmarginWidth, &xmargin,
362 XmNmarginHeight, &ymargin,
363 XmNshadowThickness, &shadow,
364 NULL );
365
366 wxSize listSize = wxDoGetListBoxBestSize( GetXmList(this), this );
367 wxSize textSize = wxDoGetSingleTextCtrlBestSize( GetXmText(this),
368 this );
369
370 // FIXME arbitrary constants
371 return wxSize( listSize.x + arrowW + arrowS + 2 * highlight
372 + 2 * shadow + 2 * xmargin ,
373 textSize.y + 2 * highlight + 2 * ymargin + 2 * shadow );
374 }
375 else
376 return wxWindow::DoGetBestSize();
377 }
378
379 WXWidget wxComboBox::GetTextWidget() const
380 {
381 return (WXWidget)GetXmText(this);
382 }
383
384 #endif // XmVersion >= 2000
385
386 #endif // wxUSE_COMBOBOX