]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
printf() format warning fixes
[wxWidgets.git] / src / motif / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/setup.h"
17
18 #if wxUSE_COMBOBOX
19
20 #include "wx/combobox.h"
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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
41
42 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
43 const wxString& value,
44 const wxPoint& pos,
45 const wxSize& size,
46 int n, const wxString choices[],
47 long style,
48 const wxValidator& validator,
49 const wxString& name)
50 {
51 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
52 return FALSE;
53
54 m_noStrings = n;
55
56 Widget parentWidget = (Widget) parent->GetClientWidget();
57
58 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
59 xmComboBoxWidgetClass, parentWidget,
60 XmNmarginHeight, 0,
61 XmNmarginWidth, 0,
62 XmNshowLabel, False,
63 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
64 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
65 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
66 NULL);
67
68 int i;
69 for (i = 0; i < n; i++)
70 {
71 wxXmString str( choices[i] );
72 XmComboBoxAddItem(buttonWidget, str(), 0);
73 m_stringList.Add(choices[i]);
74 }
75
76 m_mainWidget = (Widget) buttonWidget;
77
78 XtManageChild (buttonWidget);
79
80 SetValue(value);
81
82 ChangeFont(FALSE);
83
84 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
85 (XtPointer) this);
86 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
87 (XtPointer) this);
88
89 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
90
91 ChangeBackgroundColour();
92
93 return TRUE;
94 }
95
96 wxComboBox::~wxComboBox()
97 {
98 DetachWidget((Widget) m_mainWidget); // Removes event handlers
99 XtDestroyWidget((Widget) m_mainWidget);
100 m_mainWidget = (WXWidget) 0;
101 if ( HasClientObjectData() )
102 m_clientDataDict.DestroyData();
103 }
104
105 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
106 {
107 // Necessary so it doesn't call wxChoice::SetSize
108 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
109 }
110
111 wxString wxComboBox::GetValue() const
112 {
113 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
114 if (s)
115 {
116 wxString str(s);
117 XtFree (s);
118 return str;
119 }
120 else
121 return wxEmptyString;
122 }
123
124 void wxComboBox::SetValue(const wxString& value)
125 {
126 m_inSetValue = TRUE;
127 if( !value.empty() )
128 XmComboBoxSetString( (Widget)m_mainWidget,
129 wxConstCast(value.c_str(), char) );
130 m_inSetValue = FALSE;
131 }
132
133 void wxComboBox::SetString(int n, const wxString& s)
134 {
135 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
136 }
137
138 int wxComboBox::DoAppend(const wxString& item)
139 {
140 wxXmString str( item.c_str() );
141 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
142 m_stringList.Add(item);
143 m_noStrings ++;
144
145 return GetCount() - 1;
146 }
147
148 int wxComboBox::DoInsert(const wxString& item, int pos)
149 {
150 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
151 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
152
153 if (pos == GetCount())
154 return DoAppend(item);
155
156 wxXmString str( item.c_str() );
157 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1);
158 m_stringList.Insert(pos, item);
159 m_noStrings ++;
160
161 return pos;
162 }
163
164 void wxComboBox::Delete(int n)
165 {
166 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
167 wxStringList::Node *node = m_stringList.Item(n);
168 if (node)
169 {
170 delete[] node->GetData();
171 delete node;
172 }
173 m_clientDataDict.Delete(n, HasClientObjectData());
174 m_noStrings--;
175 }
176
177 void wxComboBox::Clear()
178 {
179 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
180 m_stringList.Clear();
181
182 if ( HasClientObjectData() )
183 m_clientDataDict.DestroyData();
184 m_noStrings = 0;
185 }
186
187 void wxComboBox::SetSelection (int n)
188 {
189 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
190 }
191
192 int wxComboBox::GetSelection (void) const
193 {
194 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
195 if (sel == 0)
196 return -1;
197 else
198 return sel - 1;
199 }
200
201 wxString wxComboBox::GetString(int n) const
202 {
203 wxStringList::Node *node = m_stringList.Item(n);
204 if (node)
205 return wxString(node->GetData ());
206 else
207 return wxEmptyString;
208 }
209
210 int wxComboBox::FindString(const wxString& s) const
211 {
212 int *pos_list = NULL;
213 int count = 0;
214 wxXmString text( s );
215 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
216 text(), &pos_list, &count) != 0);
217
218 if (found && count > 0)
219 {
220 int pos = pos_list[0] - 1;
221 free(pos_list);
222 return pos;
223 }
224
225 return -1;
226 }
227
228 // Clipboard operations
229 void wxComboBox::Copy()
230 {
231 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
232 }
233
234 void wxComboBox::Cut()
235 {
236 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
237 }
238
239 void wxComboBox::Paste()
240 {
241 XmComboBoxPaste((Widget) m_mainWidget);
242 }
243
244 void wxComboBox::SetEditable(bool WXUNUSED(editable))
245 {
246 // TODO
247 }
248
249 void wxComboBox::SetInsertionPoint(long pos)
250 {
251 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
252 }
253
254 void wxComboBox::SetInsertionPointEnd()
255 {
256 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
257 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
258 }
259
260 long wxComboBox::GetInsertionPoint() const
261 {
262 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
263 }
264
265 long wxComboBox::GetLastPosition() const
266 {
267 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
268 }
269
270 void wxComboBox::Replace(long from, long to, const wxString& value)
271 {
272 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
273 (XmTextPosition) to,
274 wxConstCast(value.c_str(), char));
275 }
276
277 void wxComboBox::Remove(long from, long to)
278 {
279 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
280 (Time) 0);
281 XmComboBoxRemove ((Widget) m_mainWidget);
282 }
283
284 void wxComboBox::SetSelection(long from, long to)
285 {
286 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
287 (Time) 0);
288 }
289
290 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
291 XmComboBoxSelectionCallbackStruct * cbs)
292 {
293 wxComboBox *item = (wxComboBox *) clientData;
294
295 switch (cbs->reason)
296 {
297 case XmCR_SINGLE_SELECT:
298 case XmCR_BROWSE_SELECT:
299 {
300 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
301 item->GetId());
302 event.m_commandInt = cbs->index - 1;
303 event.m_commandString = item->GetString (event.m_commandInt);
304 if ( item->HasClientObjectData() )
305 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
306 else if ( item->HasClientUntypedData() )
307 event.SetClientData( item->GetClientData(cbs->index - 1) );
308 event.m_extraLong = TRUE;
309 event.SetEventObject(item);
310 item->ProcessCommand (event);
311 break;
312 }
313 case XmCR_VALUE_CHANGED:
314 {
315 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
316 event.m_commandInt = -1;
317 event.m_commandString = item->GetValue();
318 event.m_extraLong = TRUE;
319 event.SetEventObject(item);
320 item->ProcessCommand (event);
321 break;
322 }
323 default:
324 break;
325 }
326 }
327
328 void wxComboBox::ChangeFont(bool keepOriginalSize)
329 {
330 // Don't use the base class wxChoice's ChangeFont
331 wxWindow::ChangeFont(keepOriginalSize);
332 }
333
334 void wxComboBox::ChangeBackgroundColour()
335 {
336 wxWindow::ChangeBackgroundColour();
337 }
338
339 void wxComboBox::ChangeForegroundColour()
340 {
341 wxWindow::ChangeForegroundColour();
342 }
343
344 wxSize wxComboBox::DoGetBestSize() const
345 {
346 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
347 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
348 {
349 wxSize items = GetItemsSize();
350 // FIXME arbitrary constants
351 return wxSize( ( items.x ? items.x + 50 : 120 ),
352 items.y + 10 );
353 }
354 else
355 return wxWindow::DoGetBestSize();
356 }
357
358 #endif // XmVersion < 2000
359
360 #endif // wxUSE_COMBOBOX