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