]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
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 | ||
9b1bd0c6 | 15 | #include "wx/setup.h" |
4bb6408c | 16 | |
89c7e962 JS |
17 | #if wxUSE_COMBOBOX |
18 | ||
9b1bd0c6 | 19 | #include "wx/combobox.h" |
584ad2a3 | 20 | #include "wx/arrstr.h" |
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 | 37 | void wxComboBoxCallback (Widget w, XtPointer clientData, |
2d120f83 | 38 | XmComboBoxSelectionCallbackStruct * cbs); |
89c7e962 | 39 | |
4bb6408c | 40 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
4bb6408c JS |
41 | |
42 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
43 | const wxString& value, |
44 | const wxPoint& pos, | |
45 | const wxSize& size, | |
46 | int n, const wxString choices[], | |
47 | long style, | |
48 | const wxValidator& validator, | |
49 | const wxString& name) | |
4bb6408c | 50 | { |
ec75d791 | 51 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
7d8268a1 | 52 | return false; |
31528cd3 | 53 | |
ec75d791 | 54 | m_noStrings = n; |
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); | |
89c7e962 JS |
73 | m_stringList.Add(choices[i]); |
74 | } | |
31528cd3 | 75 | |
89c7e962 | 76 | m_mainWidget = (Widget) buttonWidget; |
31528cd3 | 77 | |
89c7e962 | 78 | XtManageChild (buttonWidget); |
31528cd3 | 79 | |
89c7e962 | 80 | SetValue(value); |
31528cd3 | 81 | |
7d8268a1 | 82 | ChangeFont(false); |
31528cd3 | 83 | |
f6bcfd97 BP |
84 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, |
85 | (XtPointer) this); | |
86 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
87 | (XtPointer) this); | |
88 | ||
89c7e962 | 89 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); |
31528cd3 | 90 | |
0d57be45 | 91 | ChangeBackgroundColour(); |
31528cd3 | 92 | |
7d8268a1 | 93 | return true; |
4bb6408c JS |
94 | } |
95 | ||
584ad2a3 MB |
96 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
97 | const wxString& value, | |
98 | const wxPoint& pos, | |
99 | const wxSize& size, | |
100 | const wxArrayString& choices, | |
101 | long style, | |
102 | const wxValidator& validator, | |
103 | const wxString& name) | |
104 | { | |
105 | wxCArrayString chs(choices); | |
7d8268a1 | 106 | return Create(parent, id, value, pos, size, chs.GetCount(), |
584ad2a3 MB |
107 | chs.GetStrings(), style, validator, name); |
108 | } | |
109 | ||
8aa04e8b JS |
110 | wxComboBox::~wxComboBox() |
111 | { | |
112 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
113 | XtDestroyWidget((Widget) m_mainWidget); | |
114 | m_mainWidget = (WXWidget) 0; | |
ec75d791 MB |
115 | if ( HasClientObjectData() ) |
116 | m_clientDataDict.DestroyData(); | |
8aa04e8b JS |
117 | } |
118 | ||
bfc6fde4 | 119 | void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
8aa04e8b JS |
120 | { |
121 | // Necessary so it doesn't call wxChoice::SetSize | |
ec75d791 | 122 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); |
8aa04e8b JS |
123 | } |
124 | ||
4bb6408c JS |
125 | wxString wxComboBox::GetValue() const |
126 | { | |
89c7e962 JS |
127 | char *s = XmComboBoxGetString ((Widget) m_mainWidget); |
128 | if (s) | |
129 | { | |
130 | wxString str(s); | |
131 | XtFree (s); | |
132 | return str; | |
133 | } | |
134 | else | |
135 | return wxEmptyString; | |
4bb6408c JS |
136 | } |
137 | ||
138 | void wxComboBox::SetValue(const wxString& value) | |
139 | { | |
7d8268a1 | 140 | m_inSetValue = true; |
ec75d791 | 141 | if( !value.empty() ) |
d3a80c92 MB |
142 | XmComboBoxSetString( (Widget)m_mainWidget, |
143 | wxConstCast(value.c_str(), char) ); | |
7d8268a1 | 144 | m_inSetValue = false; |
4bb6408c JS |
145 | } |
146 | ||
9b1bd0c6 MB |
147 | void wxComboBox::SetString(int n, const wxString& s) |
148 | { | |
149 | wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") ); | |
150 | } | |
151 | ||
ec75d791 | 152 | int wxComboBox::DoAppend(const wxString& item) |
8aa04e8b | 153 | { |
ec75d791 MB |
154 | wxXmString str( item.c_str() ); |
155 | XmComboBoxAddItem((Widget) m_mainWidget, str(), 0); | |
8aa04e8b | 156 | m_stringList.Add(item); |
8aa04e8b | 157 | m_noStrings ++; |
ec75d791 MB |
158 | |
159 | return GetCount() - 1; | |
8aa04e8b JS |
160 | } |
161 | ||
243dbf1a VZ |
162 | int wxComboBox::DoInsert(const wxString& item, int pos) |
163 | { | |
164 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
165 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
166 | ||
167 | if (pos == GetCount()) | |
168 | return DoAppend(item); | |
169 | ||
170 | wxXmString str( item.c_str() ); | |
171 | XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1); | |
46ec70fb MB |
172 | wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str()); |
173 | m_stringList.Insert(pos, copy); | |
243dbf1a VZ |
174 | m_noStrings ++; |
175 | ||
176 | return pos; | |
177 | } | |
178 | ||
8aa04e8b JS |
179 | void wxComboBox::Delete(int n) |
180 | { | |
ec75d791 | 181 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); |
fd304d98 | 182 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b JS |
183 | if (node) |
184 | { | |
fd304d98 | 185 | delete[] node->GetData(); |
2d120f83 | 186 | delete node; |
8aa04e8b | 187 | } |
ec75d791 | 188 | m_clientDataDict.Delete(n, HasClientObjectData()); |
8aa04e8b JS |
189 | m_noStrings--; |
190 | } | |
191 | ||
192 | void wxComboBox::Clear() | |
193 | { | |
194 | XmComboBoxDeleteAllItems((Widget) m_mainWidget); | |
195 | m_stringList.Clear(); | |
f6bcfd97 BP |
196 | |
197 | if ( HasClientObjectData() ) | |
ec75d791 | 198 | m_clientDataDict.DestroyData(); |
f6bcfd97 | 199 | m_noStrings = 0; |
8aa04e8b JS |
200 | } |
201 | ||
202 | void wxComboBox::SetSelection (int n) | |
203 | { | |
204 | XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False); | |
205 | } | |
206 | ||
207 | int wxComboBox::GetSelection (void) const | |
208 | { | |
2d120f83 JS |
209 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); |
210 | if (sel == 0) | |
211 | return -1; | |
212 | else | |
213 | return sel - 1; | |
8aa04e8b JS |
214 | } |
215 | ||
216 | wxString wxComboBox::GetString(int n) const | |
217 | { | |
fd304d98 | 218 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b | 219 | if (node) |
fd304d98 | 220 | return wxString(node->GetData ()); |
8aa04e8b | 221 | else |
2d120f83 | 222 | return wxEmptyString; |
8aa04e8b JS |
223 | } |
224 | ||
8aa04e8b JS |
225 | int wxComboBox::FindString(const wxString& s) const |
226 | { | |
2d120f83 JS |
227 | int *pos_list = NULL; |
228 | int count = 0; | |
ec75d791 | 229 | wxXmString text( s ); |
2d120f83 | 230 | bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget, |
ec75d791 | 231 | text(), &pos_list, &count) != 0); |
31528cd3 | 232 | |
2d120f83 JS |
233 | if (found && count > 0) |
234 | { | |
235 | int pos = pos_list[0] - 1; | |
236 | free(pos_list); | |
237 | return pos; | |
238 | } | |
31528cd3 | 239 | |
2d120f83 | 240 | return -1; |
8aa04e8b JS |
241 | } |
242 | ||
4bb6408c JS |
243 | // Clipboard operations |
244 | void wxComboBox::Copy() | |
245 | { | |
89c7e962 | 246 | XmComboBoxCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
247 | } |
248 | ||
249 | void wxComboBox::Cut() | |
250 | { | |
89c7e962 | 251 | XmComboBoxCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
252 | } |
253 | ||
254 | void wxComboBox::Paste() | |
255 | { | |
89c7e962 | 256 | XmComboBoxPaste((Widget) m_mainWidget); |
4bb6408c JS |
257 | } |
258 | ||
f9e02ac7 | 259 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) |
4bb6408c JS |
260 | { |
261 | // TODO | |
262 | } | |
263 | ||
264 | void wxComboBox::SetInsertionPoint(long pos) | |
265 | { | |
89c7e962 | 266 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
267 | } |
268 | ||
269 | void wxComboBox::SetInsertionPointEnd() | |
270 | { | |
89c7e962 JS |
271 | XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
272 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1)); | |
4bb6408c JS |
273 | } |
274 | ||
275 | long wxComboBox::GetInsertionPoint() const | |
276 | { | |
89c7e962 | 277 | return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
278 | } |
279 | ||
7d8268a1 | 280 | wxTextPos wxComboBox::GetLastPosition() const |
4bb6408c | 281 | { |
7d8268a1 | 282 | return (wxTextPos) XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
283 | } |
284 | ||
285 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
286 | { | |
d3a80c92 MB |
287 | XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, |
288 | (XmTextPosition) to, | |
289 | wxConstCast(value.c_str(), char)); | |
4bb6408c JS |
290 | } |
291 | ||
292 | void wxComboBox::Remove(long from, long to) | |
293 | { | |
89c7e962 | 294 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
31528cd3 | 295 | (Time) 0); |
89c7e962 | 296 | XmComboBoxRemove ((Widget) m_mainWidget); |
4bb6408c JS |
297 | } |
298 | ||
299 | void wxComboBox::SetSelection(long from, long to) | |
300 | { | |
89c7e962 | 301 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
31528cd3 | 302 | (Time) 0); |
89c7e962 JS |
303 | } |
304 | ||
f9e02ac7 | 305 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
2d120f83 | 306 | XmComboBoxSelectionCallbackStruct * cbs) |
89c7e962 JS |
307 | { |
308 | wxComboBox *item = (wxComboBox *) clientData; | |
31528cd3 | 309 | |
89c7e962 JS |
310 | switch (cbs->reason) |
311 | { | |
2d120f83 JS |
312 | case XmCR_SINGLE_SELECT: |
313 | case XmCR_BROWSE_SELECT: | |
89c7e962 | 314 | { |
9b1bd0c6 MB |
315 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, |
316 | item->GetId()); | |
687706f5 KH |
317 | event.SetInt(cbs->index - 1); |
318 | event.SetString( item->GetString ( event.GetInt() ) ); | |
ec75d791 MB |
319 | if ( item->HasClientObjectData() ) |
320 | event.SetClientObject( item->GetClientObject(cbs->index - 1) ); | |
321 | else if ( item->HasClientUntypedData() ) | |
322 | event.SetClientData( item->GetClientData(cbs->index - 1) ); | |
96be256b | 323 | event.SetExtraLong(true); |
2d120f83 JS |
324 | event.SetEventObject(item); |
325 | item->ProcessCommand (event); | |
326 | break; | |
89c7e962 | 327 | } |
2d120f83 | 328 | case XmCR_VALUE_CHANGED: |
89c7e962 JS |
329 | { |
330 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
687706f5 KH |
331 | event.SetInt(-1); |
332 | event.SetString( item->GetValue() ); | |
96be256b | 333 | event.SetExtraLong(true); |
2d120f83 JS |
334 | event.SetEventObject(item); |
335 | item->ProcessCommand (event); | |
89c7e962 JS |
336 | break; |
337 | } | |
2d120f83 JS |
338 | default: |
339 | break; | |
89c7e962 | 340 | } |
4bb6408c JS |
341 | } |
342 | ||
4b5f3fe6 | 343 | void wxComboBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 344 | { |
321db4b6 | 345 | // Don't use the base class wxChoice's ChangeFont |
4b5f3fe6 | 346 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
347 | } |
348 | ||
349 | void wxComboBox::ChangeBackgroundColour() | |
350 | { | |
321db4b6 | 351 | wxWindow::ChangeBackgroundColour(); |
0d57be45 JS |
352 | } |
353 | ||
354 | void wxComboBox::ChangeForegroundColour() | |
355 | { | |
ec75d791 MB |
356 | wxWindow::ChangeForegroundColour(); |
357 | } | |
358 | ||
359 | wxSize wxComboBox::DoGetBestSize() const | |
360 | { | |
361 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || | |
362 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
363 | { | |
364 | wxSize items = GetItemsSize(); | |
365 | // FIXME arbitrary constants | |
366 | return wxSize( ( items.x ? items.x + 50 : 120 ), | |
367 | items.y + 10 ); | |
368 | } | |
369 | else | |
370 | return wxWindow::DoGetBestSize(); | |
0d57be45 JS |
371 | } |
372 | ||
9b1bd0c6 | 373 | #endif // XmVersion < 2000 |
89c7e962 | 374 | |
9b1bd0c6 | 375 | #endif // wxUSE_COMBOBOX |