]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox.cpp
do NOT hard code univ sources in x11 port
[wxWidgets.git] / src / motif / combobox.cpp
CommitLineData
4bb6408c
JS
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
31528cd3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.h"
14#endif
15
16#include "wx/combobox.h"
17
89c7e962
JS
18#if wxUSE_COMBOBOX
19
338dd992
JJ
20#ifdef __VMS__
21#pragma message disable nosimpint
22#endif
89c7e962 23#include <Xm/Xm.h>
338dd992
JJ
24#ifdef __VMS__
25#pragma message enable nosimpint
26#endif
8704bf74 27#include "xmcombo/xmcombo.h"
89c7e962
JS
28
29void wxComboBoxCallback (Widget w, XtPointer clientData,
2d120f83 30 XmComboBoxSelectionCallbackStruct * cbs);
89c7e962 31
4bb6408c 32IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
4bb6408c
JS
33
34bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
35 const wxString& value,
36 const wxPoint& pos,
37 const wxSize& size,
38 int n, const wxString choices[],
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
4bb6408c
JS
42{
43 SetName(name);
44 SetValidator(validator);
45 m_noStrings = n;
46 m_windowStyle = style;
94b49b93
JS
47 // m_backgroundColour = parent->GetBackgroundColour();
48 m_backgroundColour = * wxWHITE;
0d57be45 49 m_foregroundColour = parent->GetForegroundColour();
31528cd3 50
4bb6408c 51 if (parent) parent->AddChild(this);
31528cd3 52
4bb6408c 53 if ( id == -1 )
2d120f83 54 m_windowId = (int)NewControlId();
4bb6408c 55 else
2d120f83 56 m_windowId = id;
31528cd3 57
89c7e962 58 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3
VZ
59
60 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
2d120f83
JS
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);
31528cd3 69
89c7e962
JS
70 int i;
71 for (i = 0; i < n; i++)
72 {
73 XmString str = XmStringCreateLtoR((char*) (const char*) choices[i], XmSTRING_DEFAULT_CHARSET);
74 XmComboBoxAddItem(buttonWidget, str, 0);
75 XmStringFree(str);
76 m_stringList.Add(choices[i]);
77 }
78 m_noStrings = n;
31528cd3 79
89c7e962 80 m_mainWidget = (Widget) buttonWidget;
31528cd3 81
89c7e962 82 XtManageChild (buttonWidget);
31528cd3 83
89c7e962 84 SetValue(value);
31528cd3 85
da175b2c 86 m_font = parent->GetFont();
4b5f3fe6 87 ChangeFont(FALSE);
31528cd3 88
f6bcfd97
BP
89 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
90 (XtPointer) this);
91 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
92 (XtPointer) this);
93
89c7e962
JS
94 SetCanAddEventHandler(TRUE);
95 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
31528cd3 96
0d57be45 97 ChangeBackgroundColour();
31528cd3 98
4bb6408c
JS
99 return TRUE;
100}
101
8aa04e8b
JS
102wxComboBox::~wxComboBox()
103{
104 DetachWidget((Widget) m_mainWidget); // Removes event handlers
105 XtDestroyWidget((Widget) m_mainWidget);
106 m_mainWidget = (WXWidget) 0;
107}
108
bfc6fde4 109void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
8aa04e8b
JS
110{
111 // Necessary so it doesn't call wxChoice::SetSize
bfc6fde4 112 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
8aa04e8b
JS
113}
114
4bb6408c
JS
115wxString wxComboBox::GetValue() const
116{
89c7e962
JS
117 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
118 if (s)
119 {
120 wxString str(s);
121 XtFree (s);
122 return str;
123 }
124 else
125 return wxEmptyString;
4bb6408c
JS
126}
127
128void wxComboBox::SetValue(const wxString& value)
129{
89c7e962
JS
130 m_inSetValue = TRUE;
131 if (!value.IsNull())
132 XmComboBoxSetString ((Widget) m_mainWidget, (char*) (const char*) value);
133 m_inSetValue = FALSE;
4bb6408c
JS
134}
135
8aa04e8b
JS
136void wxComboBox::Append(const wxString& item)
137{
138 XmString str = XmStringCreateLtoR((char*) (const char*) item, XmSTRING_DEFAULT_CHARSET);
139 XmComboBoxAddItem((Widget) m_mainWidget, str, 0);
140 m_stringList.Add(item);
141 XmStringFree(str);
142 m_noStrings ++;
143}
144
145void wxComboBox::Delete(int n)
146{
147 XmComboBoxDeletePos((Widget) m_mainWidget, n-1);
148 wxNode *node = m_stringList.Nth(n);
149 if (node)
150 {
2d120f83
JS
151 delete[] (char *)node->Data();
152 delete node;
8aa04e8b 153 }
f6bcfd97
BP
154 node = m_clientList.Nth( n );
155 if (node)
156 {
157 if ( HasClientObjectData() )
158 {
159 delete (wxClientData *)node->Data();
160 }
161 delete node;
162 }
163
8aa04e8b
JS
164 m_noStrings--;
165}
166
167void wxComboBox::Clear()
168{
169 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
170 m_stringList.Clear();
f6bcfd97
BP
171
172 if ( HasClientObjectData() )
173 {
174 // destroy the data (due to Robert's idea of using wxList<wxObject>
175 // and not wxList<wxClientData> we can't just say
176 // m_clientList.DeleteContents(TRUE) - this would crash!
177 wxNode *node = m_clientList.First();
178 while ( node )
179 {
180 delete (wxClientData *)node->Data();
181 node = node->Next();
182 }
183 }
184 m_clientList.Clear();
185 m_noStrings = 0;
8aa04e8b
JS
186}
187
188void wxComboBox::SetSelection (int n)
189{
190 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
191}
192
193int wxComboBox::GetSelection (void) const
194{
2d120f83
JS
195 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
196 if (sel == 0)
197 return -1;
198 else
199 return sel - 1;
8aa04e8b
JS
200}
201
202wxString wxComboBox::GetString(int n) const
203{
204 wxNode *node = m_stringList.Nth (n);
205 if (node)
2d120f83 206 return wxString((char *) node->Data ());
8aa04e8b 207 else
2d120f83 208 return wxEmptyString;
8aa04e8b
JS
209}
210
211wxString wxComboBox::GetStringSelection() const
212{
213 int sel = GetSelection();
214 if (sel == -1)
215 return wxEmptyString;
216 else
217 return GetString(sel);
218}
219
220bool wxComboBox::SetStringSelection(const wxString& sel)
221{
222 int n = FindString(sel);
223 if (n == -1)
224 return FALSE;
225 else
226 {
227 SetSelection(n);
228 return TRUE;
229 }
230}
231
232int wxComboBox::FindString(const wxString& s) const
233{
2d120f83
JS
234 int *pos_list = NULL;
235 int count = 0;
236 XmString text = XmStringCreateSimple ((char*) (const char*) s);
237 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
238 text, &pos_list, &count) != 0);
31528cd3 239
2d120f83 240 XmStringFree(text);
31528cd3 241
2d120f83
JS
242 if (found && count > 0)
243 {
244 int pos = pos_list[0] - 1;
245 free(pos_list);
246 return pos;
247 }
31528cd3 248
2d120f83 249 return -1;
8aa04e8b
JS
250}
251
4bb6408c
JS
252// Clipboard operations
253void wxComboBox::Copy()
254{
89c7e962 255 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
256}
257
258void wxComboBox::Cut()
259{
89c7e962 260 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
261}
262
263void wxComboBox::Paste()
264{
89c7e962 265 XmComboBoxPaste((Widget) m_mainWidget);
4bb6408c
JS
266}
267
f9e02ac7 268void wxComboBox::SetEditable(bool WXUNUSED(editable))
4bb6408c
JS
269{
270 // TODO
271}
272
273void wxComboBox::SetInsertionPoint(long pos)
274{
89c7e962 275 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
276}
277
278void wxComboBox::SetInsertionPointEnd()
279{
89c7e962
JS
280 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
281 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
4bb6408c
JS
282}
283
284long wxComboBox::GetInsertionPoint() const
285{
89c7e962 286 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
4bb6408c
JS
287}
288
289long wxComboBox::GetLastPosition() const
290{
89c7e962 291 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
4bb6408c
JS
292}
293
294void wxComboBox::Replace(long from, long to, const wxString& value)
295{
89c7e962 296 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
2d120f83 297 (char*) (const char*) value);
4bb6408c
JS
298}
299
300void wxComboBox::Remove(long from, long to)
301{
89c7e962 302 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 303 (Time) 0);
89c7e962 304 XmComboBoxRemove ((Widget) m_mainWidget);
4bb6408c
JS
305}
306
307void wxComboBox::SetSelection(long from, long to)
308{
89c7e962 309 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 310 (Time) 0);
89c7e962
JS
311}
312
f9e02ac7 313void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 314 XmComboBoxSelectionCallbackStruct * cbs)
89c7e962
JS
315{
316 wxComboBox *item = (wxComboBox *) clientData;
31528cd3 317
89c7e962
JS
318 switch (cbs->reason)
319 {
2d120f83
JS
320 case XmCR_SINGLE_SELECT:
321 case XmCR_BROWSE_SELECT:
89c7e962
JS
322 {
323 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
2d120f83 324 event.m_commandInt = cbs->index - 1;
31528cd3 325 // event.m_commandString = item->GetString (event.m_commandInt);
2d120f83
JS
326 event.m_extraLong = TRUE;
327 event.SetEventObject(item);
328 item->ProcessCommand (event);
329 break;
89c7e962 330 }
2d120f83 331 case XmCR_VALUE_CHANGED:
89c7e962
JS
332 {
333 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
2d120f83 334 event.m_commandInt = -1;
31528cd3 335 // event.m_commandString = item->GetValue();
2d120f83
JS
336 event.m_extraLong = TRUE;
337 event.SetEventObject(item);
338 item->ProcessCommand (event);
89c7e962
JS
339 break;
340 }
2d120f83
JS
341 default:
342 break;
89c7e962 343 }
4bb6408c
JS
344}
345
4b5f3fe6 346void wxComboBox::ChangeFont(bool keepOriginalSize)
0d57be45 347{
321db4b6 348 // Don't use the base class wxChoice's ChangeFont
4b5f3fe6 349 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
350}
351
352void wxComboBox::ChangeBackgroundColour()
353{
321db4b6 354 wxWindow::ChangeBackgroundColour();
0d57be45
JS
355}
356
357void wxComboBox::ChangeForegroundColour()
358{
321db4b6 359 wxWindow::ChangeBackgroundColour();
0d57be45
JS
360}
361
89c7e962
JS
362#endif
363