]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox.cpp
made unzip.c accept / and \ in filenames as equal
[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 70 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
2d120f83 71 (XtPointer) this);
89c7e962 72 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
2d120f83 73 (XtPointer) this);
31528cd3 74
89c7e962
JS
75 int i;
76 for (i = 0; i < n; i++)
77 {
78 XmString str = XmStringCreateLtoR((char*) (const char*) choices[i], XmSTRING_DEFAULT_CHARSET);
79 XmComboBoxAddItem(buttonWidget, str, 0);
80 XmStringFree(str);
81 m_stringList.Add(choices[i]);
82 }
83 m_noStrings = n;
31528cd3 84
89c7e962 85 m_mainWidget = (Widget) buttonWidget;
31528cd3 86
89c7e962 87 XtManageChild (buttonWidget);
31528cd3 88
89c7e962 89 SetValue(value);
31528cd3 90
da175b2c 91 m_font = parent->GetFont();
4b5f3fe6 92 ChangeFont(FALSE);
31528cd3 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
JS
153 }
154 m_noStrings--;
155}
156
157void wxComboBox::Clear()
158{
159 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
160 m_stringList.Clear();
161}
162
163void wxComboBox::SetSelection (int n)
164{
165 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
166}
167
168int wxComboBox::GetSelection (void) const
169{
2d120f83
JS
170 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
171 if (sel == 0)
172 return -1;
173 else
174 return sel - 1;
8aa04e8b
JS
175}
176
177wxString wxComboBox::GetString(int n) const
178{
179 wxNode *node = m_stringList.Nth (n);
180 if (node)
2d120f83 181 return wxString((char *) node->Data ());
8aa04e8b 182 else
2d120f83 183 return wxEmptyString;
8aa04e8b
JS
184}
185
186wxString wxComboBox::GetStringSelection() const
187{
188 int sel = GetSelection();
189 if (sel == -1)
190 return wxEmptyString;
191 else
192 return GetString(sel);
193}
194
195bool wxComboBox::SetStringSelection(const wxString& sel)
196{
197 int n = FindString(sel);
198 if (n == -1)
199 return FALSE;
200 else
201 {
202 SetSelection(n);
203 return TRUE;
204 }
205}
206
207int wxComboBox::FindString(const wxString& s) const
208{
2d120f83
JS
209 int *pos_list = NULL;
210 int count = 0;
211 XmString text = XmStringCreateSimple ((char*) (const char*) s);
212 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
213 text, &pos_list, &count) != 0);
31528cd3 214
2d120f83 215 XmStringFree(text);
31528cd3 216
2d120f83
JS
217 if (found && count > 0)
218 {
219 int pos = pos_list[0] - 1;
220 free(pos_list);
221 return pos;
222 }
31528cd3 223
2d120f83 224 return -1;
8aa04e8b
JS
225}
226
4bb6408c
JS
227// Clipboard operations
228void wxComboBox::Copy()
229{
89c7e962 230 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
231}
232
233void wxComboBox::Cut()
234{
89c7e962 235 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
236}
237
238void wxComboBox::Paste()
239{
89c7e962 240 XmComboBoxPaste((Widget) m_mainWidget);
4bb6408c
JS
241}
242
f9e02ac7 243void wxComboBox::SetEditable(bool WXUNUSED(editable))
4bb6408c
JS
244{
245 // TODO
246}
247
248void wxComboBox::SetInsertionPoint(long pos)
249{
89c7e962 250 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
251}
252
253void wxComboBox::SetInsertionPointEnd()
254{
89c7e962
JS
255 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
256 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
4bb6408c
JS
257}
258
259long wxComboBox::GetInsertionPoint() const
260{
89c7e962 261 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
4bb6408c
JS
262}
263
264long wxComboBox::GetLastPosition() const
265{
89c7e962 266 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
4bb6408c
JS
267}
268
269void wxComboBox::Replace(long from, long to, const wxString& value)
270{
89c7e962 271 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
2d120f83 272 (char*) (const char*) value);
4bb6408c
JS
273}
274
275void wxComboBox::Remove(long from, long to)
276{
89c7e962 277 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 278 (Time) 0);
89c7e962 279 XmComboBoxRemove ((Widget) m_mainWidget);
4bb6408c
JS
280}
281
282void wxComboBox::SetSelection(long from, long to)
283{
89c7e962 284 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 285 (Time) 0);
89c7e962
JS
286}
287
f9e02ac7 288void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 289 XmComboBoxSelectionCallbackStruct * cbs)
89c7e962
JS
290{
291 wxComboBox *item = (wxComboBox *) clientData;
31528cd3 292
89c7e962
JS
293 switch (cbs->reason)
294 {
2d120f83
JS
295 case XmCR_SINGLE_SELECT:
296 case XmCR_BROWSE_SELECT:
89c7e962
JS
297 {
298 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
2d120f83 299 event.m_commandInt = cbs->index - 1;
31528cd3 300 // event.m_commandString = item->GetString (event.m_commandInt);
2d120f83
JS
301 event.m_extraLong = TRUE;
302 event.SetEventObject(item);
303 item->ProcessCommand (event);
304 break;
89c7e962 305 }
2d120f83 306 case XmCR_VALUE_CHANGED:
89c7e962
JS
307 {
308 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
2d120f83 309 event.m_commandInt = -1;
31528cd3 310 // event.m_commandString = item->GetValue();
2d120f83
JS
311 event.m_extraLong = TRUE;
312 event.SetEventObject(item);
313 item->ProcessCommand (event);
89c7e962
JS
314 break;
315 }
2d120f83
JS
316 default:
317 break;
89c7e962 318 }
4bb6408c
JS
319}
320
4b5f3fe6 321void wxComboBox::ChangeFont(bool keepOriginalSize)
0d57be45 322{
321db4b6 323 // Don't use the base class wxChoice's ChangeFont
4b5f3fe6 324 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
325}
326
327void wxComboBox::ChangeBackgroundColour()
328{
321db4b6 329 wxWindow::ChangeBackgroundColour();
0d57be45
JS
330}
331
332void wxComboBox::ChangeForegroundColour()
333{
321db4b6 334 wxWindow::ChangeBackgroundColour();
0d57be45
JS
335}
336
89c7e962
JS
337#endif
338