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