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