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