]>
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 | |
9 | // Licence: wxWindows licence | |
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, | |
24 | XmComboBoxSelectionCallbackStruct * cbs); | |
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, | |
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) | |
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(); |
4bb6408c JS |
46 | |
47 | if (parent) parent->AddChild(this); | |
48 | ||
49 | if ( id == -1 ) | |
50 | m_windowId = (int)NewControlId(); | |
51 | else | |
52 | m_windowId = id; | |
53 | ||
89c7e962 JS |
54 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
55 | ||
56 | Widget buttonWidget = XtVaCreateManagedWidget((char*) (const char*) name, | |
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); | |
65 | ||
66 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, | |
67 | (XtPointer) this); | |
68 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
69 | (XtPointer) this); | |
70 | ||
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; | |
80 | ||
81 | m_mainWidget = (Widget) buttonWidget; | |
82 | ||
83 | XtManageChild (buttonWidget); | |
84 | ||
85 | SetValue(value); | |
86 | ||
4b5f3fe6 JS |
87 | m_windowFont = parent->GetFont(); |
88 | ChangeFont(FALSE); | |
89 | ||
89c7e962 JS |
90 | SetCanAddEventHandler(TRUE); |
91 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
92 | ||
0d57be45 | 93 | ChangeBackgroundColour(); |
4bb6408c JS |
94 | |
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 | ||
105 | void wxComboBox::SetSize(int x, int y, int width, int height, int sizeFlags) | |
106 | { | |
107 | // Necessary so it doesn't call wxChoice::SetSize | |
108 | wxWindow::SetSize(x, y, width, height, sizeFlags); | |
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 | { | |
147 | delete[] (char *)node->Data(); | |
148 | delete node; | |
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 | { | |
166 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); | |
167 | if (sel == 0) | |
168 | return -1; | |
169 | else | |
170 | return sel - 1; | |
171 | } | |
172 | ||
173 | wxString wxComboBox::GetString(int n) const | |
174 | { | |
175 | wxNode *node = m_stringList.Nth (n); | |
176 | if (node) | |
177 | return wxString((char *) node->Data ()); | |
178 | else | |
179 | return wxEmptyString; | |
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 | { | |
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); | |
210 | ||
211 | XmStringFree(text); | |
212 | ||
213 | if (found && count > 0) | |
214 | { | |
215 | int pos = pos_list[0] - 1; | |
216 | free(pos_list); | |
217 | return pos; | |
218 | } | |
219 | ||
220 | return -1; | |
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 JS |
267 | XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
268 | (char*) (const char*) value); | |
4bb6408c JS |
269 | } |
270 | ||
271 | void wxComboBox::Remove(long from, long to) | |
272 | { | |
89c7e962 JS |
273 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
274 | (Time) 0); | |
275 | XmComboBoxRemove ((Widget) m_mainWidget); | |
4bb6408c JS |
276 | } |
277 | ||
278 | void wxComboBox::SetSelection(long from, long to) | |
279 | { | |
89c7e962 JS |
280 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
281 | (Time) 0); | |
282 | } | |
283 | ||
f9e02ac7 | 284 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
89c7e962 JS |
285 | XmComboBoxSelectionCallbackStruct * cbs) |
286 | { | |
287 | wxComboBox *item = (wxComboBox *) clientData; | |
288 | ||
289 | switch (cbs->reason) | |
290 | { | |
291 | case XmCR_SINGLE_SELECT: | |
292 | case XmCR_BROWSE_SELECT: | |
293 | { | |
294 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId()); | |
295 | event.m_commandInt = cbs->index - 1; | |
296 | // event.m_commandString = item->GetString (event.m_commandInt); | |
297 | event.m_extraLong = TRUE; | |
298 | event.SetEventObject(item); | |
299 | item->ProcessCommand (event); | |
300 | break; | |
301 | } | |
302 | case XmCR_VALUE_CHANGED: | |
303 | { | |
304 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
305 | event.m_commandInt = -1; | |
306 | // event.m_commandString = item->GetValue(); | |
307 | event.m_extraLong = TRUE; | |
308 | event.SetEventObject(item); | |
309 | item->ProcessCommand (event); | |
310 | break; | |
311 | } | |
312 | default: | |
313 | break; | |
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 |