]>
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 | ||
16 | #include "wx/combobox.h" | |
17 | ||
89c7e962 JS |
18 | #if wxUSE_COMBOBOX |
19 | ||
338dd992 JJ |
20 | #ifdef __VMS__ |
21 | #pragma message disable nosimpint | |
22 | #endif | |
89c7e962 | 23 | #include <Xm/Xm.h> |
338dd992 JJ |
24 | #ifdef __VMS__ |
25 | #pragma message enable nosimpint | |
26 | #endif | |
8704bf74 | 27 | #include "xmcombo/xmcombo.h" |
89c7e962 | 28 | |
ec75d791 MB |
29 | #include "wx/motif/private.h" |
30 | ||
89c7e962 | 31 | void wxComboBoxCallback (Widget w, XtPointer clientData, |
2d120f83 | 32 | XmComboBoxSelectionCallbackStruct * cbs); |
89c7e962 | 33 | |
4bb6408c | 34 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
4bb6408c JS |
35 | |
36 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
37 | const wxString& value, |
38 | const wxPoint& pos, | |
39 | const wxSize& size, | |
40 | int n, const wxString choices[], | |
41 | long style, | |
42 | const wxValidator& validator, | |
43 | const wxString& name) | |
4bb6408c | 44 | { |
ec75d791 MB |
45 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
46 | return FALSE; | |
31528cd3 | 47 | |
ec75d791 | 48 | m_noStrings = n; |
31528cd3 | 49 | |
89c7e962 | 50 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
31528cd3 VZ |
51 | |
52 | Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(), | |
2d120f83 JS |
53 | xmComboBoxWidgetClass, parentWidget, |
54 | XmNmarginHeight, 0, | |
55 | XmNmarginWidth, 0, | |
56 | XmNshowLabel, False, | |
57 | XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY), | |
58 | XmNsorted, ((style & wxCB_SORT) == wxCB_SORT), | |
59 | XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE), | |
60 | NULL); | |
31528cd3 | 61 | |
89c7e962 JS |
62 | int i; |
63 | for (i = 0; i < n; i++) | |
64 | { | |
ec75d791 MB |
65 | wxXmString str( choices[i] ); |
66 | XmComboBoxAddItem(buttonWidget, str(), 0); | |
89c7e962 JS |
67 | m_stringList.Add(choices[i]); |
68 | } | |
31528cd3 | 69 | |
89c7e962 | 70 | m_mainWidget = (Widget) buttonWidget; |
31528cd3 | 71 | |
89c7e962 | 72 | XtManageChild (buttonWidget); |
31528cd3 | 73 | |
89c7e962 | 74 | SetValue(value); |
31528cd3 | 75 | |
4b5f3fe6 | 76 | ChangeFont(FALSE); |
31528cd3 | 77 | |
f6bcfd97 BP |
78 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, |
79 | (XtPointer) this); | |
80 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
81 | (XtPointer) this); | |
82 | ||
89c7e962 JS |
83 | SetCanAddEventHandler(TRUE); |
84 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
31528cd3 | 85 | |
0d57be45 | 86 | ChangeBackgroundColour(); |
31528cd3 | 87 | |
4bb6408c JS |
88 | return TRUE; |
89 | } | |
90 | ||
8aa04e8b JS |
91 | wxComboBox::~wxComboBox() |
92 | { | |
93 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
94 | XtDestroyWidget((Widget) m_mainWidget); | |
95 | m_mainWidget = (WXWidget) 0; | |
ec75d791 MB |
96 | if ( HasClientObjectData() ) |
97 | m_clientDataDict.DestroyData(); | |
8aa04e8b JS |
98 | } |
99 | ||
bfc6fde4 | 100 | void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
8aa04e8b JS |
101 | { |
102 | // Necessary so it doesn't call wxChoice::SetSize | |
ec75d791 | 103 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); |
8aa04e8b JS |
104 | } |
105 | ||
4bb6408c JS |
106 | wxString wxComboBox::GetValue() const |
107 | { | |
89c7e962 JS |
108 | char *s = XmComboBoxGetString ((Widget) m_mainWidget); |
109 | if (s) | |
110 | { | |
111 | wxString str(s); | |
112 | XtFree (s); | |
113 | return str; | |
114 | } | |
115 | else | |
116 | return wxEmptyString; | |
4bb6408c JS |
117 | } |
118 | ||
119 | void wxComboBox::SetValue(const wxString& value) | |
120 | { | |
89c7e962 | 121 | m_inSetValue = TRUE; |
ec75d791 MB |
122 | if( !value.empty() ) |
123 | XmComboBoxSetString( (Widget)m_mainWidget, (char*)value.c_str() ); | |
89c7e962 | 124 | m_inSetValue = FALSE; |
4bb6408c JS |
125 | } |
126 | ||
ec75d791 | 127 | int wxComboBox::DoAppend(const wxString& item) |
8aa04e8b | 128 | { |
ec75d791 MB |
129 | wxXmString str( item.c_str() ); |
130 | XmComboBoxAddItem((Widget) m_mainWidget, str(), 0); | |
8aa04e8b | 131 | m_stringList.Add(item); |
8aa04e8b | 132 | m_noStrings ++; |
ec75d791 MB |
133 | |
134 | return GetCount() - 1; | |
8aa04e8b JS |
135 | } |
136 | ||
137 | void wxComboBox::Delete(int n) | |
138 | { | |
ec75d791 | 139 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); |
fd304d98 | 140 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b JS |
141 | if (node) |
142 | { | |
fd304d98 | 143 | delete[] node->GetData(); |
2d120f83 | 144 | delete node; |
8aa04e8b | 145 | } |
ec75d791 | 146 | m_clientDataDict.Delete(n, HasClientObjectData()); |
8aa04e8b JS |
147 | m_noStrings--; |
148 | } | |
149 | ||
150 | void wxComboBox::Clear() | |
151 | { | |
152 | XmComboBoxDeleteAllItems((Widget) m_mainWidget); | |
153 | m_stringList.Clear(); | |
f6bcfd97 BP |
154 | |
155 | if ( HasClientObjectData() ) | |
ec75d791 | 156 | m_clientDataDict.DestroyData(); |
f6bcfd97 | 157 | m_noStrings = 0; |
8aa04e8b JS |
158 | } |
159 | ||
160 | void wxComboBox::SetSelection (int n) | |
161 | { | |
162 | XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False); | |
163 | } | |
164 | ||
165 | int wxComboBox::GetSelection (void) const | |
166 | { | |
2d120f83 JS |
167 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); |
168 | if (sel == 0) | |
169 | return -1; | |
170 | else | |
171 | return sel - 1; | |
8aa04e8b JS |
172 | } |
173 | ||
174 | wxString wxComboBox::GetString(int n) const | |
175 | { | |
fd304d98 | 176 | wxStringList::Node *node = m_stringList.Item(n); |
8aa04e8b | 177 | if (node) |
fd304d98 | 178 | return wxString(node->GetData ()); |
8aa04e8b | 179 | else |
2d120f83 | 180 | return wxEmptyString; |
8aa04e8b JS |
181 | } |
182 | ||
8aa04e8b JS |
183 | int wxComboBox::FindString(const wxString& s) const |
184 | { | |
2d120f83 JS |
185 | int *pos_list = NULL; |
186 | int count = 0; | |
ec75d791 | 187 | wxXmString text( s ); |
2d120f83 | 188 | bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget, |
ec75d791 | 189 | text(), &pos_list, &count) != 0); |
31528cd3 | 190 | |
2d120f83 JS |
191 | if (found && count > 0) |
192 | { | |
193 | int pos = pos_list[0] - 1; | |
194 | free(pos_list); | |
195 | return pos; | |
196 | } | |
31528cd3 | 197 | |
2d120f83 | 198 | return -1; |
8aa04e8b JS |
199 | } |
200 | ||
4bb6408c JS |
201 | // Clipboard operations |
202 | void wxComboBox::Copy() | |
203 | { | |
89c7e962 | 204 | XmComboBoxCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
205 | } |
206 | ||
207 | void wxComboBox::Cut() | |
208 | { | |
89c7e962 | 209 | XmComboBoxCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
210 | } |
211 | ||
212 | void wxComboBox::Paste() | |
213 | { | |
89c7e962 | 214 | XmComboBoxPaste((Widget) m_mainWidget); |
4bb6408c JS |
215 | } |
216 | ||
f9e02ac7 | 217 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) |
4bb6408c JS |
218 | { |
219 | // TODO | |
220 | } | |
221 | ||
222 | void wxComboBox::SetInsertionPoint(long pos) | |
223 | { | |
89c7e962 | 224 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
225 | } |
226 | ||
227 | void wxComboBox::SetInsertionPointEnd() | |
228 | { | |
89c7e962 JS |
229 | XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
230 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1)); | |
4bb6408c JS |
231 | } |
232 | ||
233 | long wxComboBox::GetInsertionPoint() const | |
234 | { | |
89c7e962 | 235 | return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
236 | } |
237 | ||
238 | long wxComboBox::GetLastPosition() const | |
239 | { | |
89c7e962 | 240 | return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
241 | } |
242 | ||
243 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
244 | { | |
89c7e962 | 245 | XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
2d120f83 | 246 | (char*) (const char*) value); |
4bb6408c JS |
247 | } |
248 | ||
249 | void wxComboBox::Remove(long from, long to) | |
250 | { | |
89c7e962 | 251 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
31528cd3 | 252 | (Time) 0); |
89c7e962 | 253 | XmComboBoxRemove ((Widget) m_mainWidget); |
4bb6408c JS |
254 | } |
255 | ||
256 | void wxComboBox::SetSelection(long from, long to) | |
257 | { | |
89c7e962 | 258 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
31528cd3 | 259 | (Time) 0); |
89c7e962 JS |
260 | } |
261 | ||
f9e02ac7 | 262 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
2d120f83 | 263 | XmComboBoxSelectionCallbackStruct * cbs) |
89c7e962 JS |
264 | { |
265 | wxComboBox *item = (wxComboBox *) clientData; | |
31528cd3 | 266 | |
89c7e962 JS |
267 | switch (cbs->reason) |
268 | { | |
2d120f83 JS |
269 | case XmCR_SINGLE_SELECT: |
270 | case XmCR_BROWSE_SELECT: | |
89c7e962 JS |
271 | { |
272 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId()); | |
2d120f83 | 273 | event.m_commandInt = cbs->index - 1; |
ec75d791 MB |
274 | event.m_commandString = item->GetString (event.m_commandInt); |
275 | if ( item->HasClientObjectData() ) | |
276 | event.SetClientObject( item->GetClientObject(cbs->index - 1) ); | |
277 | else if ( item->HasClientUntypedData() ) | |
278 | event.SetClientData( item->GetClientData(cbs->index - 1) ); | |
2d120f83 JS |
279 | event.m_extraLong = TRUE; |
280 | event.SetEventObject(item); | |
281 | item->ProcessCommand (event); | |
282 | break; | |
89c7e962 | 283 | } |
2d120f83 | 284 | case XmCR_VALUE_CHANGED: |
89c7e962 JS |
285 | { |
286 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
2d120f83 | 287 | event.m_commandInt = -1; |
ec75d791 | 288 | event.m_commandString = item->GetValue(); |
2d120f83 JS |
289 | event.m_extraLong = TRUE; |
290 | event.SetEventObject(item); | |
291 | item->ProcessCommand (event); | |
89c7e962 JS |
292 | break; |
293 | } | |
2d120f83 JS |
294 | default: |
295 | break; | |
89c7e962 | 296 | } |
4bb6408c JS |
297 | } |
298 | ||
4b5f3fe6 | 299 | void wxComboBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 300 | { |
321db4b6 | 301 | // Don't use the base class wxChoice's ChangeFont |
4b5f3fe6 | 302 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
303 | } |
304 | ||
305 | void wxComboBox::ChangeBackgroundColour() | |
306 | { | |
321db4b6 | 307 | wxWindow::ChangeBackgroundColour(); |
0d57be45 JS |
308 | } |
309 | ||
310 | void wxComboBox::ChangeForegroundColour() | |
311 | { | |
ec75d791 MB |
312 | wxWindow::ChangeForegroundColour(); |
313 | } | |
314 | ||
315 | wxSize wxComboBox::DoGetBestSize() const | |
316 | { | |
317 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || | |
318 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
319 | { | |
320 | wxSize items = GetItemsSize(); | |
321 | // FIXME arbitrary constants | |
322 | return wxSize( ( items.x ? items.x + 50 : 120 ), | |
323 | items.y + 10 ); | |
324 | } | |
325 | else | |
326 | return wxWindow::DoGetBestSize(); | |
0d57be45 JS |
327 | } |
328 | ||
89c7e962 JS |
329 | #endif |
330 |