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