]> git.saurik.com Git - wxWidgets.git/blob - src/motif/combobox.cpp
fix build for borland 5.0
[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, (char*)value.c_str() );
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 void wxComboBox::Delete(int n)
149 {
150 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
151 wxStringList::Node *node = m_stringList.Item(n);
152 if (node)
153 {
154 delete[] node->GetData();
155 delete node;
156 }
157 m_clientDataDict.Delete(n, HasClientObjectData());
158 m_noStrings--;
159 }
160
161 void wxComboBox::Clear()
162 {
163 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
164 m_stringList.Clear();
165
166 if ( HasClientObjectData() )
167 m_clientDataDict.DestroyData();
168 m_noStrings = 0;
169 }
170
171 void wxComboBox::SetSelection (int n)
172 {
173 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
174 }
175
176 int wxComboBox::GetSelection (void) const
177 {
178 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
179 if (sel == 0)
180 return -1;
181 else
182 return sel - 1;
183 }
184
185 wxString wxComboBox::GetString(int n) const
186 {
187 wxStringList::Node *node = m_stringList.Item(n);
188 if (node)
189 return wxString(node->GetData ());
190 else
191 return wxEmptyString;
192 }
193
194 int wxComboBox::FindString(const wxString& s) const
195 {
196 int *pos_list = NULL;
197 int count = 0;
198 wxXmString text( s );
199 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
200 text(), &pos_list, &count) != 0);
201
202 if (found && count > 0)
203 {
204 int pos = pos_list[0] - 1;
205 free(pos_list);
206 return pos;
207 }
208
209 return -1;
210 }
211
212 // Clipboard operations
213 void wxComboBox::Copy()
214 {
215 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
216 }
217
218 void wxComboBox::Cut()
219 {
220 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
221 }
222
223 void wxComboBox::Paste()
224 {
225 XmComboBoxPaste((Widget) m_mainWidget);
226 }
227
228 void wxComboBox::SetEditable(bool WXUNUSED(editable))
229 {
230 // TODO
231 }
232
233 void wxComboBox::SetInsertionPoint(long pos)
234 {
235 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
236 }
237
238 void wxComboBox::SetInsertionPointEnd()
239 {
240 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
241 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
242 }
243
244 long wxComboBox::GetInsertionPoint() const
245 {
246 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
247 }
248
249 long wxComboBox::GetLastPosition() const
250 {
251 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
252 }
253
254 void wxComboBox::Replace(long from, long to, const wxString& value)
255 {
256 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
257 (char*) (const char*) value);
258 }
259
260 void wxComboBox::Remove(long from, long to)
261 {
262 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
263 (Time) 0);
264 XmComboBoxRemove ((Widget) m_mainWidget);
265 }
266
267 void wxComboBox::SetSelection(long from, long to)
268 {
269 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
270 (Time) 0);
271 }
272
273 void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
274 XmComboBoxSelectionCallbackStruct * cbs)
275 {
276 wxComboBox *item = (wxComboBox *) clientData;
277
278 switch (cbs->reason)
279 {
280 case XmCR_SINGLE_SELECT:
281 case XmCR_BROWSE_SELECT:
282 {
283 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
284 item->GetId());
285 event.m_commandInt = cbs->index - 1;
286 event.m_commandString = item->GetString (event.m_commandInt);
287 if ( item->HasClientObjectData() )
288 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
289 else if ( item->HasClientUntypedData() )
290 event.SetClientData( item->GetClientData(cbs->index - 1) );
291 event.m_extraLong = TRUE;
292 event.SetEventObject(item);
293 item->ProcessCommand (event);
294 break;
295 }
296 case XmCR_VALUE_CHANGED:
297 {
298 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
299 event.m_commandInt = -1;
300 event.m_commandString = item->GetValue();
301 event.m_extraLong = TRUE;
302 event.SetEventObject(item);
303 item->ProcessCommand (event);
304 break;
305 }
306 default:
307 break;
308 }
309 }
310
311 void wxComboBox::ChangeFont(bool keepOriginalSize)
312 {
313 // Don't use the base class wxChoice's ChangeFont
314 wxWindow::ChangeFont(keepOriginalSize);
315 }
316
317 void wxComboBox::ChangeBackgroundColour()
318 {
319 wxWindow::ChangeBackgroundColour();
320 }
321
322 void wxComboBox::ChangeForegroundColour()
323 {
324 wxWindow::ChangeForegroundColour();
325 }
326
327 wxSize wxComboBox::DoGetBestSize() const
328 {
329 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
330 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
331 {
332 wxSize items = GetItemsSize();
333 // FIXME arbitrary constants
334 return wxSize( ( items.x ? items.x + 50 : 120 ),
335 items.y + 10 );
336 }
337 else
338 return wxWindow::DoGetBestSize();
339 }
340
341 #endif // XmVersion < 2000
342
343 #endif // wxUSE_COMBOBOX