]> git.saurik.com Git - wxWidgets.git/blame - src/motif/combobox.cpp
don't use wxVector<wxDataFormat> from wx/clipbrd.h as wxDataFormat definition is...
[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;
105fbe1f 54 PreCreation();
31528cd3 55
aa61d352 56 m_noStrings = n;
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 {
ec75d791
MB
73 wxXmString str( choices[i] );
74 XmComboBoxAddItem(buttonWidget, str(), 0);
89c7e962
JS
75 m_stringList.Add(choices[i]);
76 }
31528cd3 77
89c7e962 78 m_mainWidget = (Widget) buttonWidget;
31528cd3 79
89c7e962 80 XtManageChild (buttonWidget);
31528cd3 81
89c7e962 82 SetValue(value);
31528cd3 83
f6bcfd97
BP
84 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
85 (XtPointer) this);
86 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
87 (XtPointer) this);
88
105fbe1f 89 PostCreation();
89c7e962 90 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
31528cd3 91
7d8268a1 92 return true;
4bb6408c
JS
93}
94
584ad2a3
MB
95bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
96 const wxString& value,
97 const wxPoint& pos,
98 const wxSize& size,
99 const wxArrayString& choices,
100 long style,
101 const wxValidator& validator,
102 const wxString& name)
103{
104 wxCArrayString chs(choices);
7d8268a1 105 return Create(parent, id, value, pos, size, chs.GetCount(),
584ad2a3
MB
106 chs.GetStrings(), style, validator, name);
107}
108
8aa04e8b
JS
109wxComboBox::~wxComboBox()
110{
111 DetachWidget((Widget) m_mainWidget); // Removes event handlers
112 XtDestroyWidget((Widget) m_mainWidget);
113 m_mainWidget = (WXWidget) 0;
114}
115
a7bdad33
VZ
116void wxComboBox::DoSetSize(int x, int y,
117 int width, int WXUNUSED(height),
118 int sizeFlags)
8aa04e8b
JS
119{
120 // Necessary so it doesn't call wxChoice::SetSize
ec75d791 121 wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags);
8aa04e8b
JS
122}
123
76c32e7b
JJ
124#if 0
125// Already defined in include/motif/combobox.h
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 137}
76c32e7b 138#endif
4bb6408c
JS
139
140void wxComboBox::SetValue(const wxString& value)
141{
ec75d791 142 if( !value.empty() )
b044829c
VZ
143 {
144 m_inSetValue = true;
145 XmComboBoxSetString((Widget)m_mainWidget, value.char_str());
146 m_inSetValue = false;
147 }
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
a236aa20
VZ
155// TODO auto-sorting is not supported by the code
156int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
157 unsigned int pos,
158 void **clientData,
159 wxClientDataType type)
8aa04e8b 160{
a236aa20 161 const unsigned int numItems = items.GetCount();
ec75d791 162
a236aa20
VZ
163 AllocClientData(numItems);
164 for( unsigned int i = 0; i < numItems; ++i, ++pos )
165 {
166 wxXmString str( items[i].c_str() );
167 XmComboBoxAddItem((Widget) m_mainWidget, str(), GetMotifPosition(pos));
168 wxChar* copy = wxStrcpy(new wxChar[items[i].length() + 1], items[i].c_str());
169 m_stringList.Insert(pos, copy);
170 m_noStrings ++;
171 InsertNewItemClientData(pos, clientData, i, type);
172 }
243dbf1a 173
a236aa20 174 return pos - 1;
243dbf1a
VZ
175}
176
a236aa20 177void wxComboBox::DoDeleteOneItem(unsigned int n)
8aa04e8b 178{
ec75d791 179 XmComboBoxDeletePos((Widget) m_mainWidget, n+1);
58045daa 180 m_stringArray.RemoveAt(n);
a236aa20 181 wxControlWithItems::DoDeleteOneItem(n);
8aa04e8b
JS
182 m_noStrings--;
183}
184
76c32e7b 185void wxComboBox::Clear()
8aa04e8b
JS
186{
187 XmComboBoxDeleteAllItems((Widget) m_mainWidget);
188 m_stringList.Clear();
f6bcfd97 189
a236aa20 190 wxControlWithItems::DoClear();
f6bcfd97 191 m_noStrings = 0;
8aa04e8b
JS
192}
193
194void wxComboBox::SetSelection (int n)
195{
196 XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False);
197}
198
199int wxComboBox::GetSelection (void) const
200{
2d120f83
JS
201 int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget);
202 if (sel == 0)
203 return -1;
204 else
205 return sel - 1;
8aa04e8b
JS
206}
207
aa61d352 208wxString wxComboBox::GetString(unsigned int n) const
8aa04e8b 209{
58045daa 210 return m_stringArray[n];
8aa04e8b
JS
211}
212
a7bdad33 213int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
8aa04e8b 214{
11e62fe6
WS
215 // FIXME: back to base class for not supported value of bCase
216
2d120f83
JS
217 int *pos_list = NULL;
218 int count = 0;
ec75d791 219 wxXmString text( s );
2d120f83 220 bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget,
ec75d791 221 text(), &pos_list, &count) != 0);
31528cd3 222
2d120f83
JS
223 if (found && count > 0)
224 {
225 int pos = pos_list[0] - 1;
226 free(pos_list);
227 return pos;
228 }
31528cd3 229
11e62fe6 230 return wxNOT_FOUND;
8aa04e8b
JS
231}
232
f9e02ac7 233void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
2d120f83 234 XmComboBoxSelectionCallbackStruct * cbs)
89c7e962
JS
235{
236 wxComboBox *item = (wxComboBox *) clientData;
31528cd3 237
89c7e962
JS
238 switch (cbs->reason)
239 {
2d120f83
JS
240 case XmCR_SINGLE_SELECT:
241 case XmCR_BROWSE_SELECT:
89c7e962 242 {
9b1bd0c6
MB
243 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED,
244 item->GetId());
687706f5
KH
245 event.SetInt(cbs->index - 1);
246 event.SetString( item->GetString ( event.GetInt() ) );
ec75d791
MB
247 if ( item->HasClientObjectData() )
248 event.SetClientObject( item->GetClientObject(cbs->index - 1) );
249 else if ( item->HasClientUntypedData() )
250 event.SetClientData( item->GetClientData(cbs->index - 1) );
96be256b 251 event.SetExtraLong(true);
2d120f83
JS
252 event.SetEventObject(item);
253 item->ProcessCommand (event);
254 break;
89c7e962 255 }
2d120f83 256 case XmCR_VALUE_CHANGED:
89c7e962
JS
257 {
258 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
687706f5
KH
259 event.SetInt(-1);
260 event.SetString( item->GetValue() );
96be256b 261 event.SetExtraLong(true);
2d120f83
JS
262 event.SetEventObject(item);
263 item->ProcessCommand (event);
89c7e962
JS
264 break;
265 }
2d120f83
JS
266 default:
267 break;
89c7e962 268 }
4bb6408c
JS
269}
270
4b5f3fe6 271void wxComboBox::ChangeFont(bool keepOriginalSize)
0d57be45 272{
321db4b6 273 // Don't use the base class wxChoice's ChangeFont
4b5f3fe6 274 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
275}
276
277void wxComboBox::ChangeBackgroundColour()
278{
321db4b6 279 wxWindow::ChangeBackgroundColour();
0d57be45
JS
280}
281
282void wxComboBox::ChangeForegroundColour()
283{
ec75d791
MB
284 wxWindow::ChangeForegroundColour();
285}
286
287wxSize wxComboBox::DoGetBestSize() const
288{
289 if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN ||
290 (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY )
291 {
292 wxSize items = GetItemsSize();
293 // FIXME arbitrary constants
294 return wxSize( ( items.x ? items.x + 50 : 120 ),
295 items.y + 10 );
296 }
297 else
298 return wxWindow::DoGetBestSize();
0d57be45
JS
299}
300
978c6e41
VZ
301WXWidget wxComboBox::GetTextWidget() const
302{
303 return (WXWidget)XmComboBoxGetEditWidget((Widget) m_mainWidget);
304}
305
9b1bd0c6 306#endif // XmVersion < 2000
89c7e962 307
9b1bd0c6 308#endif // wxUSE_COMBOBOX