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