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