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