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