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