]>
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 | #include "wx/motif/private.h" | |
30 | ||
31 | void wxComboBoxCallback (Widget w, XtPointer clientData, | |
32 | XmComboBoxSelectionCallbackStruct * cbs); | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
35 | ||
36 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
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) | |
44 | { | |
45 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) | |
46 | return FALSE; | |
47 | ||
48 | m_noStrings = n; | |
49 | ||
50 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
51 | ||
52 | Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(), | |
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); | |
61 | ||
62 | int i; | |
63 | for (i = 0; i < n; i++) | |
64 | { | |
65 | wxXmString str( choices[i] ); | |
66 | XmComboBoxAddItem(buttonWidget, str(), 0); | |
67 | m_stringList.Add(choices[i]); | |
68 | } | |
69 | ||
70 | m_mainWidget = (Widget) buttonWidget; | |
71 | ||
72 | XtManageChild (buttonWidget); | |
73 | ||
74 | SetValue(value); | |
75 | ||
76 | ChangeFont(FALSE); | |
77 | ||
78 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, | |
79 | (XtPointer) this); | |
80 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
81 | (XtPointer) this); | |
82 | ||
83 | SetCanAddEventHandler(TRUE); | |
84 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
85 | ||
86 | ChangeBackgroundColour(); | |
87 | ||
88 | return TRUE; | |
89 | } | |
90 | ||
91 | wxComboBox::~wxComboBox() | |
92 | { | |
93 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
94 | XtDestroyWidget((Widget) m_mainWidget); | |
95 | m_mainWidget = (WXWidget) 0; | |
96 | if ( HasClientObjectData() ) | |
97 | m_clientDataDict.DestroyData(); | |
98 | } | |
99 | ||
100 | void wxComboBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
101 | { | |
102 | // Necessary so it doesn't call wxChoice::SetSize | |
103 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
104 | } | |
105 | ||
106 | wxString wxComboBox::GetValue() const | |
107 | { | |
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; | |
117 | } | |
118 | ||
119 | void wxComboBox::SetValue(const wxString& value) | |
120 | { | |
121 | m_inSetValue = TRUE; | |
122 | if( !value.empty() ) | |
123 | XmComboBoxSetString( (Widget)m_mainWidget, (char*)value.c_str() ); | |
124 | m_inSetValue = FALSE; | |
125 | } | |
126 | ||
127 | int wxComboBox::DoAppend(const wxString& item) | |
128 | { | |
129 | wxXmString str( item.c_str() ); | |
130 | XmComboBoxAddItem((Widget) m_mainWidget, str(), 0); | |
131 | m_stringList.Add(item); | |
132 | m_noStrings ++; | |
133 | ||
134 | return GetCount() - 1; | |
135 | } | |
136 | ||
137 | void wxComboBox::Delete(int n) | |
138 | { | |
139 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
140 | wxNode *node = m_stringList.Nth(n); | |
141 | if (node) | |
142 | { | |
143 | delete[] (char *)node->Data(); | |
144 | delete node; | |
145 | } | |
146 | m_clientDataDict.Delete(n, HasClientObjectData()); | |
147 | m_noStrings--; | |
148 | } | |
149 | ||
150 | void wxComboBox::Clear() | |
151 | { | |
152 | XmComboBoxDeleteAllItems((Widget) m_mainWidget); | |
153 | m_stringList.Clear(); | |
154 | ||
155 | if ( HasClientObjectData() ) | |
156 | m_clientDataDict.DestroyData(); | |
157 | m_noStrings = 0; | |
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 | { | |
167 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); | |
168 | if (sel == 0) | |
169 | return -1; | |
170 | else | |
171 | return sel - 1; | |
172 | } | |
173 | ||
174 | wxString wxComboBox::GetString(int n) const | |
175 | { | |
176 | wxNode *node = m_stringList.Nth(n); | |
177 | if (node) | |
178 | return wxString((char *) node->Data ()); | |
179 | else | |
180 | return wxEmptyString; | |
181 | } | |
182 | ||
183 | int wxComboBox::FindString(const wxString& s) const | |
184 | { | |
185 | int *pos_list = NULL; | |
186 | int count = 0; | |
187 | wxXmString text( s ); | |
188 | bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget, | |
189 | text(), &pos_list, &count) != 0); | |
190 | ||
191 | if (found && count > 0) | |
192 | { | |
193 | int pos = pos_list[0] - 1; | |
194 | free(pos_list); | |
195 | return pos; | |
196 | } | |
197 | ||
198 | return -1; | |
199 | } | |
200 | ||
201 | // Clipboard operations | |
202 | void wxComboBox::Copy() | |
203 | { | |
204 | XmComboBoxCopy((Widget) m_mainWidget, CurrentTime); | |
205 | } | |
206 | ||
207 | void wxComboBox::Cut() | |
208 | { | |
209 | XmComboBoxCut((Widget) m_mainWidget, CurrentTime); | |
210 | } | |
211 | ||
212 | void wxComboBox::Paste() | |
213 | { | |
214 | XmComboBoxPaste((Widget) m_mainWidget); | |
215 | } | |
216 | ||
217 | void wxComboBox::SetEditable(bool WXUNUSED(editable)) | |
218 | { | |
219 | // TODO | |
220 | } | |
221 | ||
222 | void wxComboBox::SetInsertionPoint(long pos) | |
223 | { | |
224 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); | |
225 | } | |
226 | ||
227 | void wxComboBox::SetInsertionPointEnd() | |
228 | { | |
229 | XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget); | |
230 | XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1)); | |
231 | } | |
232 | ||
233 | long wxComboBox::GetInsertionPoint() const | |
234 | { | |
235 | return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget); | |
236 | } | |
237 | ||
238 | long wxComboBox::GetLastPosition() const | |
239 | { | |
240 | return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget); | |
241 | } | |
242 | ||
243 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
244 | { | |
245 | XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, | |
246 | (char*) (const char*) value); | |
247 | } | |
248 | ||
249 | void wxComboBox::Remove(long from, long to) | |
250 | { | |
251 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, | |
252 | (Time) 0); | |
253 | XmComboBoxRemove ((Widget) m_mainWidget); | |
254 | } | |
255 | ||
256 | void wxComboBox::SetSelection(long from, long to) | |
257 | { | |
258 | XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, | |
259 | (Time) 0); | |
260 | } | |
261 | ||
262 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, | |
263 | XmComboBoxSelectionCallbackStruct * cbs) | |
264 | { | |
265 | wxComboBox *item = (wxComboBox *) clientData; | |
266 | ||
267 | switch (cbs->reason) | |
268 | { | |
269 | case XmCR_SINGLE_SELECT: | |
270 | case XmCR_BROWSE_SELECT: | |
271 | { | |
272 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId()); | |
273 | event.m_commandInt = cbs->index - 1; | |
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) ); | |
279 | event.m_extraLong = TRUE; | |
280 | event.SetEventObject(item); | |
281 | item->ProcessCommand (event); | |
282 | break; | |
283 | } | |
284 | case XmCR_VALUE_CHANGED: | |
285 | { | |
286 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
287 | event.m_commandInt = -1; | |
288 | event.m_commandString = item->GetValue(); | |
289 | event.m_extraLong = TRUE; | |
290 | event.SetEventObject(item); | |
291 | item->ProcessCommand (event); | |
292 | break; | |
293 | } | |
294 | default: | |
295 | break; | |
296 | } | |
297 | } | |
298 | ||
299 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
300 | { | |
301 | // Don't use the base class wxChoice's ChangeFont | |
302 | wxWindow::ChangeFont(keepOriginalSize); | |
303 | } | |
304 | ||
305 | void wxComboBox::ChangeBackgroundColour() | |
306 | { | |
307 | wxWindow::ChangeBackgroundColour(); | |
308 | } | |
309 | ||
310 | void wxComboBox::ChangeForegroundColour() | |
311 | { | |
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(); | |
327 | } | |
328 | ||
329 | #endif | |
330 |