]>
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 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "combobox.h" |
14 | #endif | |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
9b1bd0c6 | 19 | #include "wx/setup.h" |
4bb6408c | 20 | |
89c7e962 JS |
21 | #if wxUSE_COMBOBOX |
22 | ||
9b1bd0c6 | 23 | #include "wx/combobox.h" |
584ad2a3 | 24 | #include "wx/arrstr.h" |
9b1bd0c6 | 25 | |
338dd992 JJ |
26 | #ifdef __VMS__ |
27 | #pragma message disable nosimpint | |
28 | #endif | |
89c7e962 | 29 | #include <Xm/Xm.h> |
338dd992 JJ |
30 | #ifdef __VMS__ |
31 | #pragma message enable nosimpint | |
32 | #endif | |
9b1bd0c6 MB |
33 | |
34 | // use the old, GPL'd combobox | |
35 | #if (XmVersion < 2000) | |
36 | ||
8704bf74 | 37 | #include "xmcombo/xmcombo.h" |
89c7e962 | 38 | |
ec75d791 MB |
39 | #include "wx/motif/private.h" |
40 | ||
89c7e962 | 41 | void wxComboBoxCallback (Widget w, XtPointer clientData, |
2d120f83 | 42 | XmComboBoxSelectionCallbackStruct * cbs); |
89c7e962 | 43 | |
4bb6408c | 44 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
4bb6408c JS |
45 | |
46 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
47 | const wxString& value, |
48 | const wxPoint& pos, | |
49 | const wxSize& size, | |
50 | int n, const wxString choices[], | |
51 | long style, | |
52 | const wxValidator& validator, | |
53 | const wxString& name) | |
4bb6408c | 54 | { |
ec75d791 MB |
55 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
56 | return FALSE; | |
31528cd3 | 57 | |
ec75d791 | 58 | m_noStrings = n; |
31528cd3 | 59 | |
89c7e962 | 60 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
31528cd3 VZ |
61 | |
62 | Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(), | |
2d120f83 JS |
63 | xmComboBoxWidgetClass, parentWidget, |
64 | XmNmarginHeight, 0, | |
65 | XmNmarginWidth, 0, | |
66 | XmNshowLabel, False, | |
67 | XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY), | |
68 | XmNsorted, ((style & wxCB_SORT) == wxCB_SORT), | |
69 | XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE), | |
70 | NULL); | |
31528cd3 | 71 | |
89c7e962 JS |
72 | int i; |
73 | for (i = 0; i < n; i++) | |
74 | { | |
ec75d791 MB |
75 | wxXmString str( choices[i] ); |
76 | XmComboBoxAddItem(buttonWidget, str(), 0); | |
89c7e962 JS |
77 | m_stringList.Add(choices[i]); |
78 | } | |
31528cd3 | 79 | |
89c7e962 | 80 | m_mainWidget = (Widget) buttonWidget; |
31528cd3 | 81 | |
89c7e962 | 82 | XtManageChild (buttonWidget); |
31528cd3 | 83 | |
89c7e962 | 84 | SetValue(value); |
31528cd3 | 85 | |
4b5f3fe6 | 86 | ChangeFont(FALSE); |
31528cd3 | 87 | |
f6bcfd97 BP |
88 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, |
89 | (XtPointer) this); | |
90 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
91 | (XtPointer) this); | |
92 | ||
89c7e962 | 93 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); |
31528cd3 | 94 | |
0d57be45 | 95 | ChangeBackgroundColour(); |
31528cd3 | 96 | |
4bb6408c JS |
97 | return TRUE; |
98 | } | |
99 | ||
584ad2a3 MB |
100 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
101 | const wxString& value, | |
102 | const wxPoint& pos, | |
103 | const wxSize& size, | |
104 | const wxArrayString& choices, | |
105 | long style, | |
106 | const wxValidator& validator, | |
107 | const wxString& name) | |
108 | { | |
109 | wxCArrayString chs(choices); | |
110 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
111 | chs.GetStrings(), style, validator, name); | |
112 | } | |
113 | ||
8aa04e8b JS |
114 | wxComboBox::~wxComboBox() |
115 | { | |
116 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
117 | XtDestroyWidget((Widget) m_mainWidget); | |
118 | m_mainWidget = (WXWidget) 0; | |
ec75d791 MB |
119 | if ( HasClientObjectData() ) |
120 | m_clientDataDict.DestroyData(); | |
8aa04e8b JS |
121 | } |
122 | ||
bfc6fde4 | 123 | void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
8aa04e8b JS |
124 | { |
125 | // Necessary so it doesn't call wxChoice::SetSize | |
ec75d791 | 126 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); |
8aa04e8b JS |
127 | } |
128 | ||
4bb6408c JS |
129 | wxString wxComboBox::GetValue() const |
130 | { | |
89c7e962 JS |
131 | char *s = XmComboBoxGetString ((Widget) m_mainWidget); |
132 | if (s) | |
133 | { | |
134 | wxString str(s); | |
135 | XtFree (s); | |
136 | return str; | |
137 | } | |
138 | else | |
139 | return wxEmptyString; | |
4bb6408c JS |
140 | } |
141 | ||
142 | void wxComboBox::SetValue(const wxString& value) | |
143 | { | |
89c7e962 | 144 | m_inSetValue = TRUE; |
ec75d791 | 145 | if( !value.empty() ) |
d3a80c92 MB |
146 | XmComboBoxSetString( (Widget)m_mainWidget, |
147 | wxConstCast(value.c_str(), char) ); | |
89c7e962 | 148 | m_inSetValue = FALSE; |
4bb6408c JS |
149 | } |
150 | ||
9b1bd0c6 MB |
151 | void wxComboBox::SetString(int n, const wxString& s) |
152 | { | |
153 | wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") ); | |
154 | } | |
155 | ||
ec75d791 | 156 | int wxComboBox::DoAppend(const wxString& item) |
8aa04e8b | 157 | { |
ec75d791 MB |
158 | wxXmString str( item.c_str() ); |
159 | XmComboBoxAddItem((Widget) m_mainWidget, str(), 0); | |
8aa04e8b | 160 | m_stringList.Add(item); |
8aa04e8b | 161 | m_noStrings ++; |
ec75d791 MB |
162 | |
163 | return GetCount() - 1; | |
8aa04e8b JS |
164 | } |
165 | ||
243dbf1a VZ |
166 | int wxComboBox::DoInsert(const wxString& item, int pos) |
167 | { | |
168 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
169 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
170 | ||
171 | if (pos == GetCount()) | |
172 | return DoAppend(item); | |
173 | ||
174 | wxXmString str( item.c_str() ); | |
175 | XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1); | |
46ec70fb MB |
176 | wxChar* copy = wxStrcpy(new wxChar[item.length() + 1], item.c_str()); |
177 | m_stringList.Insert(pos, copy); | |
243dbf1a VZ |
178 | m_noStrings ++; |
179 | ||
180 | return pos; | |
181 | } | |
182 | ||
8aa04e8b JS |
183 | void wxComboBox::Delete(int n) |
184 | { | |
ec75d791 | 185 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); |
fd304d98 | 186 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b JS |
187 | if (node) |
188 | { | |
fd304d98 | 189 | delete[] node->GetData(); |
2d120f83 | 190 | delete node; |
8aa04e8b | 191 | } |
ec75d791 | 192 | m_clientDataDict.Delete(n, HasClientObjectData()); |
8aa04e8b JS |
193 | m_noStrings--; |
194 | } | |
195 | ||
196 | void wxComboBox::Clear() | |
197 | { | |
198 | XmComboBoxDeleteAllItems((Widget) m_mainWidget); | |
199 | m_stringList.Clear(); | |
f6bcfd97 BP |
200 | |
201 | if ( HasClientObjectData() ) | |
ec75d791 | 202 | m_clientDataDict.DestroyData(); |
f6bcfd97 | 203 | m_noStrings = 0; |
8aa04e8b JS |
204 | } |
205 | ||
206 | void wxComboBox::SetSelection (int n) | |
207 | { | |
208 | XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False); | |
209 | } | |
210 | ||
211 | int wxComboBox::GetSelection (void) const | |
212 | { | |
2d120f83 JS |
213 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); |
214 | if (sel == 0) | |
215 | return -1; | |
216 | else | |
217 | return sel - 1; | |
8aa04e8b JS |
218 | } |
219 | ||
220 | wxString wxComboBox::GetString(int n) const | |
221 | { | |
fd304d98 | 222 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b | 223 | if (node) |
fd304d98 | 224 | return wxString(node->GetData ()); |
8aa04e8b | 225 | else |
2d120f83 | 226 | return wxEmptyString; |
8aa04e8b JS |
227 | } |
228 | ||
8aa04e8b JS |
229 | int wxComboBox::FindString(const wxString& s) const |
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 | |
2d120f83 | 244 | return -1; |
8aa04e8b JS |
245 | } |
246 | ||
4bb6408c JS |
247 | // Clipboard operations |
248 | void wxComboBox::Copy() | |
249 | { | |
89c7e962 | 250 | XmComboBoxCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
251 | } |
252 | ||
253 | void wxComboBox::Cut() | |
254 | { | |
89c7e962 | 255 | XmComboBoxCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
256 | } |
257 | ||
258 | void wxComboBox::Paste() | |
259 | { | |
89c7e962 | 260 | XmComboBoxPaste((Widget) m_mainWidget); |
4bb6408c JS |
261 | } |
262 | ||
f9e02ac7 | 263 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) |
4bb6408c JS |
264 | { |
265 | // TODO | |
266 | } | |
267 | ||
268 | void wxComboBox::SetInsertionPoint(long pos) | |
269 | { | |
89c7e962 | 270 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
271 | } |
272 | ||
273 | void wxComboBox::SetInsertionPointEnd() | |
274 | { | |
89c7e962 JS |
275 | XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
276 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1)); | |
4bb6408c JS |
277 | } |
278 | ||
279 | long wxComboBox::GetInsertionPoint() const | |
280 | { | |
89c7e962 | 281 | return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
282 | } |
283 | ||
284 | long wxComboBox::GetLastPosition() const | |
285 | { | |
89c7e962 | 286 | return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
287 | } |
288 | ||
289 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
290 | { | |
d3a80c92 MB |
291 | XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, |
292 | (XmTextPosition) to, | |
293 | wxConstCast(value.c_str(), char)); | |
4bb6408c JS |
294 | } |
295 | ||
296 | void 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 | ||
303 | void 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 | 309 | void 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) ); | |
687706f5 | 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() ); | |
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 | 347 | void 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 | ||
353 | void wxComboBox::ChangeBackgroundColour() | |
354 | { | |
321db4b6 | 355 | wxWindow::ChangeBackgroundColour(); |
0d57be45 JS |
356 | } |
357 | ||
358 | void wxComboBox::ChangeForegroundColour() | |
359 | { | |
ec75d791 MB |
360 | wxWindow::ChangeForegroundColour(); |
361 | } | |
362 | ||
363 | wxSize 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 |