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