]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox.cpp
Commit mentioned fix/hack to enable controls
[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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "combobox.h"
14#endif
15
9b1bd0c6 16#include "wx/setup.h"
4bb6408c 17
89c7e962
JS
18#if wxUSE_COMBOBOX
19
9b1bd0c6 20#include "wx/combobox.h"
584ad2a3 21#include "wx/arrstr.h"
9b1bd0c6 22
338dd992
JJ
23#ifdef __VMS__
24#pragma message disable nosimpint
25#endif
89c7e962 26#include <Xm/Xm.h>
338dd992
JJ
27#ifdef __VMS__
28#pragma message enable nosimpint
29#endif
9b1bd0c6
MB
30
31// use the old, GPL'd combobox
32#if (XmVersion < 2000)
33
8704bf74 34#include "xmcombo/xmcombo.h"
89c7e962 35
ec75d791
MB
36#include "wx/motif/private.h"
37
89c7e962 38void wxComboBoxCallback (Widget w, XtPointer clientData,
2d120f83 39 XmComboBoxSelectionCallbackStruct * cbs);
89c7e962 40
4bb6408c 41IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
4bb6408c
JS
42
43bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
44 const wxString& value,
45 const wxPoint& pos,
46 const wxSize& size,
47 int n, const wxString choices[],
48 long style,
49 const wxValidator& validator,
50 const wxString& name)
4bb6408c 51{
ec75d791
MB
52 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
53 return FALSE;
31528cd3 54
ec75d791 55 m_noStrings = n;
31528cd3 56
89c7e962 57 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3
VZ
58
59 Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(),
2d120f83
JS
60 xmComboBoxWidgetClass, parentWidget,
61 XmNmarginHeight, 0,
62 XmNmarginWidth, 0,
63 XmNshowLabel, False,
64 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
65 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
66 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
67 NULL);
31528cd3 68
89c7e962
JS
69 int i;
70 for (i = 0; i < n; i++)
71 {
ec75d791
MB
72 wxXmString str( choices[i] );
73 XmComboBoxAddItem(buttonWidget, str(), 0);
89c7e962
JS
74 m_stringList.Add(choices[i]);
75 }
31528cd3 76
89c7e962 77 m_mainWidget = (Widget) buttonWidget;
31528cd3 78
89c7e962 79 XtManageChild (buttonWidget);
31528cd3 80
89c7e962 81 SetValue(value);
31528cd3 82
4b5f3fe6 83 ChangeFont(FALSE);
31528cd3 84
f6bcfd97
BP
85 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
86 (XtPointer) this);
87 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
88 (XtPointer) this);
89
89c7e962 90 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
31528cd3 91
0d57be45 92 ChangeBackgroundColour();
31528cd3 93
4bb6408c
JS
94 return TRUE;
95}
96
584ad2a3
MB
97bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
98 const wxString& value,
99 const wxPoint& pos,
100 const wxSize& size,
101 const wxArrayString& choices,
102 long style,
103 const wxValidator& validator,
104 const wxString& name)
105{
106 wxCArrayString chs(choices);
107 return Create(parent, id, value, pos, size, chs.GetCount(),
108 chs.GetStrings(), style, validator, name);
109}
110
8aa04e8b
JS
111wxComboBox::~wxComboBox()
112{
113 DetachWidget((Widget) m_mainWidget); // Removes event handlers
114 XtDestroyWidget((Widget) m_mainWidget);
115 m_mainWidget = (WXWidget) 0;
ec75d791
MB
116 if ( HasClientObjectData() )
117 m_clientDataDict.DestroyData();
8aa04e8b
JS
118}
119
bfc6fde4 120void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
8aa04e8b
JS
121{
122 // Necessary so it doesn't call wxChoice::SetSize
ec75d791 123 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
8aa04e8b
JS
124}
125
4bb6408c
JS
126wxString wxComboBox::GetValue() const
127{
89c7e962
JS
128 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
129 if (s)
130 {
131 wxString str(s);
132 XtFree (s);
133 return str;
134 }
135 else
136 return wxEmptyString;
4bb6408c
JS
137}
138
139void wxComboBox::SetValue(const wxString& value)
140{
89c7e962 141 m_inSetValue = TRUE;
ec75d791 142 if( !value.empty() )
d3a80c92
MB
143 XmComboBoxSetString( (Widget)m_mainWidget,
144 wxConstCast(value.c_str(), char) );
89c7e962 145 m_inSetValue = FALSE;
4bb6408c
JS
146}
147
9b1bd0c6
MB
148void wxComboBox::SetString(int n, const wxString& s)
149{
150 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
151}
152
ec75d791 153int wxComboBox::DoAppend(const wxString& item)
8aa04e8b 154{
ec75d791
MB
155 wxXmString str( item.c_str() );
156 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
8aa04e8b 157 m_stringList.Add(item);
8aa04e8b 158 m_noStrings ++;
ec75d791
MB
159
160 return GetCount() - 1;
8aa04e8b
JS
161}
162
243dbf1a
VZ
163int wxComboBox::DoInsert(const wxString& item, int pos)
164{
165 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
166 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
167
168 if (pos == GetCount())
169 return DoAppend(item);
170
171 wxXmString str( item.c_str() );
172 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1);
46ec70fb
MB
173 wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str());
174 m_stringList.Insert(pos, copy);
243dbf1a
VZ
175 m_noStrings ++;
176
177 return pos;
178}
179
8aa04e8b
JS
180void wxComboBox::Delete(int n)
181{
ec75d791 182 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
fd304d98 183 wxStringList::Node *node = m_stringList.Item(n);
8aa04e8b
JS
184 if (node)
185 {
fd304d98 186 delete[] node->GetData();
2d120f83 187 delete node;
8aa04e8b 188 }
ec75d791 189 m_clientDataDict.Delete(n, HasClientObjectData());
8aa04e8b
JS
190 m_noStrings--;
191}
192
193void wxComboBox::Clear()
194{
195 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
196 m_stringList.Clear();
f6bcfd97
BP
197
198 if ( HasClientObjectData() )
ec75d791 199 m_clientDataDict.DestroyData();
f6bcfd97 200 m_noStrings = 0;
8aa04e8b
JS
201}
202
203void wxComboBox::SetSelection (int n)
204{
205 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
206}
207
208int wxComboBox::GetSelection (void) const
209{
2d120f83
JS
210 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
211 if (sel == 0)
212 return -1;
213 else
214 return sel - 1;
8aa04e8b
JS
215}
216
217wxString wxComboBox::GetString(int n) const
218{
fd304d98 219 wxStringList::Node *node = m_stringList.Item(n);
8aa04e8b 220 if (node)
fd304d98 221 return wxString(node->GetData ());
8aa04e8b 222 else
2d120f83 223 return wxEmptyString;
8aa04e8b
JS
224}
225
8aa04e8b
JS
226int wxComboBox::FindString(const wxString& s) const
227{
2d120f83
JS
228 int *pos_list = NULL;
229 int count = 0;
ec75d791 230 wxXmString text( s );
2d120f83 231 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
ec75d791 232 text(), &pos_list, &count) != 0);
31528cd3 233
2d120f83
JS
234 if (found && count > 0)
235 {
236 int pos = pos_list[0] - 1;
237 free(pos_list);
238 return pos;
239 }
31528cd3 240
2d120f83 241 return -1;
8aa04e8b
JS
242}
243
4bb6408c
JS
244// Clipboard operations
245void wxComboBox::Copy()
246{
89c7e962 247 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
248}
249
250void wxComboBox::Cut()
251{
89c7e962 252 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
253}
254
255void wxComboBox::Paste()
256{
89c7e962 257 XmComboBoxPaste((Widget) m_mainWidget);
4bb6408c
JS
258}
259
f9e02ac7 260void wxComboBox::SetEditable(bool WXUNUSED(editable))
4bb6408c
JS
261{
262 // TODO
263}
264
265void wxComboBox::SetInsertionPoint(long pos)
266{
89c7e962 267 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
268}
269
270void wxComboBox::SetInsertionPointEnd()
271{
89c7e962
JS
272 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
273 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
4bb6408c
JS
274}
275
276long wxComboBox::GetInsertionPoint() const
277{
89c7e962 278 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
4bb6408c
JS
279}
280
281long wxComboBox::GetLastPosition() const
282{
89c7e962 283 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
4bb6408c
JS
284}
285
286void wxComboBox::Replace(long from, long to, const wxString& value)
287{
d3a80c92
MB
288 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
289 (XmTextPosition) to,
290 wxConstCast(value.c_str(), char));
4bb6408c
JS
291}
292
293void wxComboBox::Remove(long from, long to)
294{
89c7e962 295 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 296 (Time) 0);
89c7e962 297 XmComboBoxRemove ((Widget) m_mainWidget);
4bb6408c
JS
298}
299
300void wxComboBox::SetSelection(long from, long to)
301{
89c7e962 302 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 303 (Time) 0);
89c7e962
JS
304}
305
f9e02ac7 306void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 307 XmComboBoxSelectionCallbackStruct * cbs)
89c7e962
JS
308{
309 wxComboBox *item = (wxComboBox *) clientData;
31528cd3 310
89c7e962
JS
311 switch (cbs->reason)
312 {
2d120f83
JS
313 case XmCR_SINGLE_SELECT:
314 case XmCR_BROWSE_SELECT:
89c7e962 315 {
9b1bd0c6
MB
316 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
317 item->GetId());
2d120f83 318 event.m_commandInt = cbs->index - 1;
ec75d791
MB
319 event.m_commandString = item->GetString (event.m_commandInt);
320 if ( item->HasClientObjectData() )
321 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
322 else if ( item->HasClientUntypedData() )
323 event.SetClientData( item->GetClientData(cbs->index - 1) );
2d120f83
JS
324 event.m_extraLong = TRUE;
325 event.SetEventObject(item);
326 item->ProcessCommand (event);
327 break;
89c7e962 328 }
2d120f83 329 case XmCR_VALUE_CHANGED:
89c7e962
JS
330 {
331 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
2d120f83 332 event.m_commandInt = -1;
ec75d791 333 event.m_commandString = item->GetValue();
2d120f83
JS
334 event.m_extraLong = TRUE;
335 event.SetEventObject(item);
336 item->ProcessCommand (event);
89c7e962
JS
337 break;
338 }
2d120f83
JS
339 default:
340 break;
89c7e962 341 }
4bb6408c
JS
342}
343
4b5f3fe6 344void wxComboBox::ChangeFont(bool keepOriginalSize)
0d57be45 345{
321db4b6 346 // Don't use the base class wxChoice's ChangeFont
4b5f3fe6 347 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
348}
349
350void wxComboBox::ChangeBackgroundColour()
351{
321db4b6 352 wxWindow::ChangeBackgroundColour();
0d57be45
JS
353}
354
355void wxComboBox::ChangeForegroundColour()
356{
ec75d791
MB
357 wxWindow::ChangeForegroundColour();
358}
359
360wxSize wxComboBox::DoGetBestSize() const
361{
362 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
363 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
364 {
365 wxSize items = GetItemsSize();
366 // FIXME arbitrary constants
367 return wxSize( ( items.x ? items.x + 50 : 120 ),
368 items.y + 10 );
369 }
370 else
371 return wxWindow::DoGetBestSize();
0d57be45
JS
372}
373
9b1bd0c6 374#endif // XmVersion < 2000
89c7e962 375
9b1bd0c6 376#endif // wxUSE_COMBOBOX