]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
Added InitMouseEvent helper method (like wxMSW)
[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 SetCanAddEventHandler(TRUE);
90 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
91
92 ChangeBackgroundColour();
93
94 return TRUE;
95 }
96
97 wxComboBox::~wxComboBox()
98 {
99 DetachWidget((Widget) m_mainWidget); // Removes event handlers
100 XtDestroyWidget((Widget) m_mainWidget);
101 m_mainWidget = (WXWidget) 0;
102 if ( HasClientObjectData() )
103 m_clientDataDict.DestroyData();
104 }
105
106 void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
107 {
108 // Necessary so it doesn't call wxChoice::SetSize
109 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
110 }
111
112 wxString wxComboBox::GetValue() const
113 {
114 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
115 if (s)
116 {
117 wxString str(s);
118 XtFree (s);
119 return str;
120 }
121 else
122 return wxEmptyString;
123 }
124
125 void wxComboBox::SetValue(const wxString& value)
126 {
127 m_inSetValue = TRUE;
128 if( !value.empty() )
129 XmComboBoxSetString( (Widget)m_mainWidget,
130 wxConstCast(value.c_str(), char) );
131 m_inSetValue = FALSE;
132 }
133
134 void wxComboBox::SetString(int n, const wxString& s)
135 {
136 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
137 }
138
139 int wxComboBox::DoAppend(const wxString& item)
140 {
141 wxXmString str( item.c_str() );
142 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
143 m_stringList.Add(item);
144 m_noStrings ++;
145
146 return GetCount() - 1;
147 }
148
149 void wxComboBox::Delete(int n)
150 {
151 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
152 wxStringList::Node *node = m_stringList.Item(n);
153 if (node)
154 {
155 delete[] node->GetData();
156 delete node;
157 }
158 m_clientDataDict.Delete(n, HasClientObjectData());
159 m_noStrings--;
160 }
161
162 void wxComboBox::Clear()
163 {
164 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
165 m_stringList.Clear();
166
167 if ( HasClientObjectData() )
168 m_clientDataDict.DestroyData();
169 m_noStrings = 0;
170 }
171
172 void wxComboBox::SetSelection (int n)
173 {
174 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
175 }
176
177 int wxComboBox::GetSelection (void) const
178 {
179 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
180 if (sel == 0)
181 return -1;
182 else
183 return sel - 1;
184 }
185
186 wxString wxComboBox::GetString(int n) const
187 {
188 wxStringList::Node *node = m_stringList.Item(n);
189 if (node)
190 return wxString(node->GetData ());
191 else
192 return wxEmptyString;
193 }
194
195 int wxComboBox::FindString(const wxString& s) const
196 {
197 int *pos_list = NULL;
198 int count = 0;
199 wxXmString text( s );
200 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
201 text(), &pos_list, &count) != 0);
202
203 if (found && count > 0)
204 {
205 int pos = pos_list[0] - 1;
206 free(pos_list);
207 return pos;
208 }
209
210 return -1;
211 }
212
213 // Clipboard operations
214 void wxComboBox::Copy()
215 {
216 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
217 }
218
219 void wxComboBox::Cut()
220 {
221 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
222 }
223
224 void wxComboBox::Paste()
225 {
226 XmComboBoxPaste((Widget) m_mainWidget);
227 }
228
229 void wxComboBox::SetEditable(bool WXUNUSED(editable))
230 {
231 // TODO
232 }
233
234 void wxComboBox::SetInsertionPoint(long pos)
235 {
236 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
237 }
238
239 void wxComboBox::SetInsertionPointEnd()
240 {
241 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
242 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
243 }
244
245 long wxComboBox::GetInsertionPoint() const
246 {
247 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
248 }
249
250 long wxComboBox::GetLastPosition() const
251 {
252 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
253 }
254
255 void wxComboBox::Replace(long from, long to, const wxString& value)
256 {
257 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
258 (XmTextPosition) to,
259 wxConstCast(value.c_str(), char));
260 }
261
262 void wxComboBox::Remove(long from, long to)
263 {
264 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
265 (Time) 0);
266 XmComboBoxRemove ((Widget) m_mainWidget);
267 }
268
269 void wxComboBox::SetSelection(long from, long to)
270 {
271 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
272 (Time) 0);
273 }
274
275 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
276 XmComboBoxSelectionCallbackStruct * cbs)
277 {
278 wxComboBox *item = (wxComboBox *) clientData;
279
280 switch (cbs->reason)
281 {
282 case XmCR_SINGLE_SELECT:
283 case XmCR_BROWSE_SELECT:
284 {
285 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
286 item->GetId());
287 event.m_commandInt = cbs->index - 1;
288 event.m_commandString = item->GetString (event.m_commandInt);
289 if ( item->HasClientObjectData() )
290 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
291 else if ( item->HasClientUntypedData() )
292 event.SetClientData( item->GetClientData(cbs->index - 1) );
293 event.m_extraLong = TRUE;
294 event.SetEventObject(item);
295 item->ProcessCommand (event);
296 break;
297 }
298 case XmCR_VALUE_CHANGED:
299 {
300 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
301 event.m_commandInt = -1;
302 event.m_commandString = item->GetValue();
303 event.m_extraLong = TRUE;
304 event.SetEventObject(item);
305 item->ProcessCommand (event);
306 break;
307 }
308 default:
309 break;
310 }
311 }
312
313 void wxComboBox::ChangeFont(bool keepOriginalSize)
314 {
315 // Don't use the base class wxChoice's ChangeFont
316 wxWindow::ChangeFont(keepOriginalSize);
317 }
318
319 void wxComboBox::ChangeBackgroundColour()
320 {
321 wxWindow::ChangeBackgroundColour();
322 }
323
324 void wxComboBox::ChangeForegroundColour()
325 {
326 wxWindow::ChangeForegroundColour();
327 }
328
329 wxSize wxComboBox::DoGetBestSize() const
330 {
331 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
332 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
333 {
334 wxSize items = GetItemsSize();
335 // FIXME arbitrary constants
336 return wxSize( ( items.x ? items.x + 50 : 120 ),
337 items.y + 10 );
338 }
339 else
340 return wxWindow::DoGetBestSize();
341 }
342
343 #endif // XmVersion < 2000
344
345 #endif // wxUSE_COMBOBOX