]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox.cpp
Implement wxGetMouseState
[wxWidgets.git] / src / motif / combobox.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/motif/combobox.cpp
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
89c7e962
JS
15#if wxUSE_COMBOBOX
16
9b1bd0c6 17#include "wx/combobox.h"
aaa6d89a
WS
18
19#ifndef WX_PRECOMP
20 #include "wx/arrstr.h"
21#endif
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 52 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
7d8268a1 53 return false;
31528cd3 54
aa61d352 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
7d8268a1 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
7d8268a1 94 return true;
4bb6408c
JS
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);
7d8268a1 107 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
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
a7bdad33
VZ
120void wxComboBox::DoSetSize(int x, int y,
121 int width, int WXUNUSED(height),
122 int sizeFlags)
8aa04e8b
JS
123{
124 // Necessary so it doesn't call wxChoice::SetSize
ec75d791 125 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
8aa04e8b
JS
126}
127
4bb6408c
JS
128wxString wxComboBox::GetValue() const
129{
89c7e962
JS
130 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
131 if (s)
132 {
133 wxString str(s);
134 XtFree (s);
135 return str;
136 }
137 else
138 return wxEmptyString;
4bb6408c
JS
139}
140
141void wxComboBox::SetValue(const wxString& value)
142{
7d8268a1 143 m_inSetValue = true;
ec75d791 144 if( !value.empty() )
d3a80c92
MB
145 XmComboBoxSetString( (Widget)m_mainWidget,
146 wxConstCast(value.c_str(), char) );
7d8268a1 147 m_inSetValue = false;
4bb6408c
JS
148}
149
aa61d352 150void wxComboBox::SetString(unsigned int WXUNUSED(n), const wxString& WXUNUSED(s))
9b1bd0c6
MB
151{
152 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
153}
154
ec75d791 155int wxComboBox::DoAppend(const wxString& item)
8aa04e8b 156{
ec75d791
MB
157 wxXmString str( item.c_str() );
158 XmComboBoxAddItem((Widget) m_mainWidget, str(), 0);
8aa04e8b 159 m_stringList.Add(item);
8aa04e8b 160 m_noStrings ++;
ec75d791
MB
161
162 return GetCount() - 1;
8aa04e8b
JS
163}
164
aa61d352 165int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
243dbf1a
VZ
166{
167 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
8228b893 168 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
243dbf1a
VZ
169
170 if (pos == GetCount())
171 return DoAppend(item);
172
173 wxXmString str( item.c_str() );
174 XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1);
46ec70fb
MB
175 wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str());
176 m_stringList.Insert(pos, copy);
243dbf1a
VZ
177 m_noStrings ++;
178
179 return pos;
180}
181
aa61d352 182void wxComboBox::Delete(unsigned int n)
8aa04e8b 183{
ec75d791 184 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
fd304d98 185 wxStringList::Node *node = m_stringList.Item(n);
8aa04e8b
JS
186 if (node)
187 {
fd304d98 188 delete[] node->GetData();
2d120f83 189 delete node;
8aa04e8b 190 }
ec75d791 191 m_clientDataDict.Delete(n, HasClientObjectData());
8aa04e8b
JS
192 m_noStrings--;
193}
194
195void wxComboBox::Clear()
196{
197 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
198 m_stringList.Clear();
f6bcfd97
BP
199
200 if ( HasClientObjectData() )
ec75d791 201 m_clientDataDict.DestroyData();
f6bcfd97 202 m_noStrings = 0;
8aa04e8b
JS
203}
204
205void wxComboBox::SetSelection (int n)
206{
207 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
208}
209
210int wxComboBox::GetSelection (void) const
211{
2d120f83
JS
212 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
213 if (sel == 0)
214 return -1;
215 else
216 return sel - 1;
8aa04e8b
JS
217}
218
aa61d352 219wxString wxComboBox::GetString(unsigned int n) const
8aa04e8b 220{
fd304d98 221 wxStringList::Node *node = m_stringList.Item(n);
8aa04e8b 222 if (node)
fd304d98 223 return wxString(node->GetData ());
8aa04e8b 224 else
2d120f83 225 return wxEmptyString;
8aa04e8b
JS
226}
227
a7bdad33 228int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
8aa04e8b 229{
11e62fe6
WS
230 // FIXME: back to base class for not supported value of bCase
231
2d120f83
JS
232 int *pos_list = NULL;
233 int count = 0;
ec75d791 234 wxXmString text( s );
2d120f83 235 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
ec75d791 236 text(), &pos_list, &count) != 0);
31528cd3 237
2d120f83
JS
238 if (found && count > 0)
239 {
240 int pos = pos_list[0] - 1;
241 free(pos_list);
242 return pos;
243 }
31528cd3 244
11e62fe6 245 return wxNOT_FOUND;
8aa04e8b
JS
246}
247
4bb6408c
JS
248// Clipboard operations
249void wxComboBox::Copy()
250{
89c7e962 251 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
252}
253
254void wxComboBox::Cut()
255{
89c7e962 256 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
257}
258
259void wxComboBox::Paste()
260{
89c7e962 261 XmComboBoxPaste((Widget) m_mainWidget);
4bb6408c
JS
262}
263
f9e02ac7 264void wxComboBox::SetEditable(bool WXUNUSED(editable))
4bb6408c
JS
265{
266 // TODO
267}
268
269void wxComboBox::SetInsertionPoint(long pos)
270{
89c7e962 271 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
272}
273
274void wxComboBox::SetInsertionPointEnd()
275{
89c7e962
JS
276 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
277 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
4bb6408c
JS
278}
279
280long wxComboBox::GetInsertionPoint() const
281{
89c7e962 282 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
4bb6408c
JS
283}
284
7d8268a1 285wxTextPos wxComboBox::GetLastPosition() const
4bb6408c 286{
7d8268a1 287 return (wxTextPos) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
4bb6408c
JS
288}
289
290void wxComboBox::Replace(long from, long to, const wxString& value)
291{
d3a80c92
MB
292 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from,
293 (XmTextPosition) to,
294 wxConstCast(value.c_str(), char));
4bb6408c
JS
295}
296
297void wxComboBox::Remove(long from, long to)
298{
89c7e962 299 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 300 (Time) 0);
89c7e962 301 XmComboBoxRemove ((Widget) m_mainWidget);
4bb6408c
JS
302}
303
304void wxComboBox::SetSelection(long from, long to)
305{
89c7e962 306 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
31528cd3 307 (Time) 0);
89c7e962
JS
308}
309
f9e02ac7 310void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 311 XmComboBoxSelectionCallbackStruct * cbs)
89c7e962
JS
312{
313 wxComboBox *item = (wxComboBox *) clientData;
31528cd3 314
89c7e962
JS
315 switch (cbs->reason)
316 {
2d120f83
JS
317 case XmCR_SINGLE_SELECT:
318 case XmCR_BROWSE_SELECT:
89c7e962 319 {
9b1bd0c6
MB
320 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
321 item->GetId());
687706f5
KH
322 event.SetInt(cbs->index - 1);
323 event.SetString( item->GetString ( event.GetInt() ) );
ec75d791
MB
324 if ( item->HasClientObjectData() )
325 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
326 else if ( item->HasClientUntypedData() )
327 event.SetClientData( item->GetClientData(cbs->index - 1) );
96be256b 328 event.SetExtraLong(true);
2d120f83
JS
329 event.SetEventObject(item);
330 item->ProcessCommand (event);
331 break;
89c7e962 332 }
2d120f83 333 case XmCR_VALUE_CHANGED:
89c7e962
JS
334 {
335 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
687706f5
KH
336 event.SetInt(-1);
337 event.SetString( item->GetValue() );
96be256b 338 event.SetExtraLong(true);
2d120f83
JS
339 event.SetEventObject(item);
340 item->ProcessCommand (event);
89c7e962
JS
341 break;
342 }
2d120f83
JS
343 default:
344 break;
89c7e962 345 }
4bb6408c
JS
346}
347
4b5f3fe6 348void wxComboBox::ChangeFont(bool keepOriginalSize)
0d57be45 349{
321db4b6 350 // Don't use the base class wxChoice's ChangeFont
4b5f3fe6 351 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
352}
353
354void wxComboBox::ChangeBackgroundColour()
355{
321db4b6 356 wxWindow::ChangeBackgroundColour();
0d57be45
JS
357}
358
359void wxComboBox::ChangeForegroundColour()
360{
ec75d791
MB
361 wxWindow::ChangeForegroundColour();
362}
363
364wxSize wxComboBox::DoGetBestSize() const
365{
366 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
367 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
368 {
369 wxSize items = GetItemsSize();
370 // FIXME arbitrary constants
371 return wxSize( ( items.x ? items.x + 50 : 120 ),
372 items.y + 10 );
373 }
374 else
375 return wxWindow::DoGetBestSize();
0d57be45
JS
376}
377
9b1bd0c6 378#endif // XmVersion < 2000
89c7e962 379
9b1bd0c6 380#endif // wxUSE_COMBOBOX