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