]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
fix crash in wxWindowGTK::GTKHandleUnrealize(), closes #14752
[wxWidgets.git] / src / motif / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
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 old, GPL'd combobox
32 #if (XmVersion < 2000)
33
34 #include "xmcombo/xmcombo.h"
35
36 #include "wx/motif/private.h"
37
38 void wxComboBoxCallback (Widget w, XtPointer clientData,
39 XmComboBoxSelectionCallbackStruct * cbs);
40
41 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
42 const wxString& value,
43 const wxPoint& pos,
44 const wxSize& size,
45 int n, const wxString choices[],
46 long style,
47 const wxValidator& validator,
48 const wxString& name)
49 {
50 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
51 return false;
52 PreCreation();
53
54 Widget parentWidget = (Widget) parent->GetClientWidget();
55
56 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
57 xmComboBoxWidgetClass, parentWidget,
58 XmNmarginHeight, 0,
59 XmNmarginWidth, 0,
60 XmNshowLabel, False,
61 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
62 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
63 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
64 NULL);
65
66 int i;
67 for (i = 0; i < n; i++)
68 {
69 wxXmString str( choices[i] );
70 XmComboBoxAddItem(buttonWidget, str(), 0);
71 m_stringArray.Add(choices[i]);
72 }
73
74 m_mainWidget = (Widget) buttonWidget;
75
76 XtManageChild (buttonWidget);
77
78 SetValue(value);
79
80 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
81 (XtPointer) this);
82 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
83 (XtPointer) this);
84
85 PostCreation();
86 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
87
88 return true;
89 }
90
91 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
92 const wxString& value,
93 const wxPoint& pos,
94 const wxSize& size,
95 const wxArrayString& choices,
96 long style,
97 const wxValidator& validator,
98 const wxString& name)
99 {
100 wxCArrayString chs(choices);
101 return Create(parent, id, value, pos, size, chs.GetCount(),
102 chs.GetStrings(), style, validator, name);
103 }
104
105 wxComboBox::~wxComboBox()
106 {
107 DetachWidget((Widget) m_mainWidget); // Removes event handlers
108 XtDestroyWidget((Widget) m_mainWidget);
109 m_mainWidget = (WXWidget) 0;
110 }
111
112 void wxComboBox::DoSetSize(int x, int y,
113 int width, int WXUNUSED(height),
114 int sizeFlags)
115 {
116 // Necessary so it doesn't call wxChoice::SetSize
117 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
118 }
119
120 #if 0
121 // Already defined in include/motif/combobox.h
122 wxString wxComboBox::GetValue() const
123 {
124 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
125 if (s)
126 {
127 wxString str(s);
128 XtFree (s);
129 return str;
130 }
131 else
132 return wxEmptyString;
133 }
134 #endif
135
136 void wxComboBox::SetValue(const wxString& value)
137 {
138 if( !value.empty() )
139 {
140 m_inSetValue = true;
141 XmComboBoxSetString((Widget)m_mainWidget, value.char_str());
142 m_inSetValue = false;
143 }
144 }
145
146 void wxComboBox::SetString(unsigned int WXUNUSED(n), const wxString& WXUNUSED(s))
147 {
148 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
149 }
150
151 // TODO auto-sorting is not supported by the code
152 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
153 unsigned int pos,
154 void **clientData,
155 wxClientDataType type)
156 {
157 const unsigned int numItems = items.GetCount();
158
159 AllocClientData(numItems);
160 for( unsigned int i = 0; i < numItems; ++i, ++pos )
161 {
162 wxXmString str( items[i].c_str() );
163 XmComboBoxAddItem((Widget) m_mainWidget, str(), GetMotifPosition(pos));
164 m_stringArray.Insert(items[i], pos);
165 InsertNewItemClientData(pos, clientData, i, type);
166 }
167
168 return pos - 1;
169 }
170
171 void wxComboBox::DoDeleteOneItem(unsigned int n)
172 {
173 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
174 m_stringArray.RemoveAt(n);
175 wxControlWithItems::DoDeleteOneItem(n);
176 }
177
178 void wxComboBox::Clear()
179 {
180 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
181 m_stringArray.Clear();
182
183 wxControlWithItems::DoClear();
184 }
185
186 void wxComboBox::SetSelection (int n)
187 {
188 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
189 }
190
191 int wxComboBox::GetSelection (void) const
192 {
193 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
194 if (sel == 0)
195 return -1;
196 else
197 return sel - 1;
198 }
199
200 wxString wxComboBox::GetString(unsigned int n) const
201 {
202 return m_stringArray[n];
203 }
204
205 int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
206 {
207 // FIXME: back to base class for not supported value of bCase
208
209 int *pos_list = NULL;
210 int count = 0;
211 wxXmString text( s );
212 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
213 text(), &pos_list, &count) != 0);
214
215 if (found && count > 0)
216 {
217 int pos = pos_list[0] - 1;
218 free(pos_list);
219 return pos;
220 }
221
222 return wxNOT_FOUND;
223 }
224
225 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
226 XmComboBoxSelectionCallbackStruct * cbs)
227 {
228 wxComboBox *item = (wxComboBox *) clientData;
229
230 switch (cbs->reason)
231 {
232 case XmCR_SINGLE_SELECT:
233 case XmCR_BROWSE_SELECT:
234 {
235 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
236 item->GetId());
237 event.SetInt(cbs->index - 1);
238 event.SetString( item->GetString ( event.GetInt() ) );
239 if ( item->HasClientObjectData() )
240 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
241 else if ( item->HasClientUntypedData() )
242 event.SetClientData( item->GetClientData(cbs->index - 1) );
243 event.SetExtraLong(true);
244 event.SetEventObject(item);
245 item->ProcessCommand (event);
246 break;
247 }
248 case XmCR_VALUE_CHANGED:
249 {
250 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
251 event.SetInt(-1);
252 event.SetString( item->GetValue() );
253 event.SetExtraLong(true);
254 event.SetEventObject(item);
255 item->ProcessCommand (event);
256 break;
257 }
258 default:
259 break;
260 }
261 }
262
263 void wxComboBox::ChangeFont(bool keepOriginalSize)
264 {
265 // Don't use the base class wxChoice's ChangeFont
266 wxWindow::ChangeFont(keepOriginalSize);
267 }
268
269 void wxComboBox::ChangeBackgroundColour()
270 {
271 wxWindow::ChangeBackgroundColour();
272 }
273
274 void wxComboBox::ChangeForegroundColour()
275 {
276 wxWindow::ChangeForegroundColour();
277 }
278
279 wxSize wxComboBox::DoGetBestSize() const
280 {
281 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
282 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
283 {
284 wxSize items = GetItemsSize();
285 // FIXME arbitrary constants
286 return wxSize( ( items.x ? items.x + 50 : 120 ),
287 items.y + 10 );
288 }
289 else
290 return wxWindow::DoGetBestSize();
291 }
292
293 WXWidget wxComboBox::GetTextWidget() const
294 {
295 return (WXWidget)XmComboBoxGetEditWidget((Widget) m_mainWidget);
296 }
297
298 #endif // XmVersion < 2000
299
300 #endif // wxUSE_COMBOBOX