]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/motif/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 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_COMBOBOX | |
16 | ||
17 | #include "wx/combobox.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/arrstr.h" | |
21 | #endif | |
22 | ||
23 | #ifdef __VMS__ | |
24 | #pragma message disable nosimpint | |
25 | #endif | |
26 | #include <Xm/Xm.h> | |
27 | #ifdef __VMS__ | |
28 | #pragma message enable nosimpint | |
29 | #endif | |
30 | ||
31 | // use the old, GPL'd combobox | |
32 | #if (XmVersion < 2000) | |
33 | ||
34 | #include "xmcombo/xmcombo.h" | |
35 | ||
36 | #include "wx/motif/private.h" | |
37 | ||
38 | void wxComboBoxCallback (Widget w, XtPointer clientData, | |
39 | XmComboBoxSelectionCallbackStruct * cbs); | |
40 | ||
41 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
42 | ||
43 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
44 | const wxString& value, | |
45 | const wxPoint& pos, | |
46 | const wxSize& size, | |
47 | int n, const wxString choices[], | |
48 | long style, | |
49 | const wxValidator& validator, | |
50 | const wxString& name) | |
51 | { | |
52 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) | |
53 | return false; | |
54 | PreCreation(); | |
55 | ||
56 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
57 | ||
58 | Widget buttonWidget = XtVaCreateManagedWidget(name.c_str(), | |
59 | xmComboBoxWidgetClass, parentWidget, | |
60 | XmNmarginHeight, 0, | |
61 | XmNmarginWidth, 0, | |
62 | XmNshowLabel, False, | |
63 | XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY), | |
64 | XmNsorted, ((style & wxCB_SORT) == wxCB_SORT), | |
65 | XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE), | |
66 | NULL); | |
67 | ||
68 | int i; | |
69 | for (i = 0; i < n; i++) | |
70 | { | |
71 | wxXmString str( choices[i] ); | |
72 | XmComboBoxAddItem(buttonWidget, str(), 0); | |
73 | m_stringArray.Add(choices[i]); | |
74 | } | |
75 | ||
76 | m_mainWidget = (Widget) buttonWidget; | |
77 | ||
78 | XtManageChild (buttonWidget); | |
79 | ||
80 | SetValue(value); | |
81 | ||
82 | XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback, | |
83 | (XtPointer) this); | |
84 | XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback, | |
85 | (XtPointer) this); | |
86 | ||
87 | PostCreation(); | |
88 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
89 | ||
90 | return true; | |
91 | } | |
92 | ||
93 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, | |
94 | const wxString& value, | |
95 | const wxPoint& pos, | |
96 | const wxSize& size, | |
97 | const wxArrayString& choices, | |
98 | long style, | |
99 | const wxValidator& validator, | |
100 | const wxString& name) | |
101 | { | |
102 | wxCArrayString chs(choices); | |
103 | return Create(parent, id, value, pos, size, chs.GetCount(), | |
104 | chs.GetStrings(), style, validator, name); | |
105 | } | |
106 | ||
107 | wxComboBox::~wxComboBox() | |
108 | { | |
109 | DetachWidget((Widget) m_mainWidget); // Removes event handlers | |
110 | XtDestroyWidget((Widget) m_mainWidget); | |
111 | m_mainWidget = (WXWidget) 0; | |
112 | } | |
113 | ||
114 | void wxComboBox::DoSetSize(int x, int y, | |
115 | int width, int WXUNUSED(height), | |
116 | int sizeFlags) | |
117 | { | |
118 | // Necessary so it doesn't call wxChoice::SetSize | |
119 | wxWindow::DoSetSize(x, y, width, DoGetBestSize().y, sizeFlags); | |
120 | } | |
121 | ||
122 | #if 0 | |
123 | // Already defined in include/motif/combobox.h | |
124 | wxString wxComboBox::GetValue() const | |
125 | { | |
126 | char *s = XmComboBoxGetString ((Widget) m_mainWidget); | |
127 | if (s) | |
128 | { | |
129 | wxString str(s); | |
130 | XtFree (s); | |
131 | return str; | |
132 | } | |
133 | else | |
134 | return wxEmptyString; | |
135 | } | |
136 | #endif | |
137 | ||
138 | void wxComboBox::SetValue(const wxString& value) | |
139 | { | |
140 | if( !value.empty() ) | |
141 | { | |
142 | m_inSetValue = true; | |
143 | XmComboBoxSetString((Widget)m_mainWidget, value.char_str()); | |
144 | m_inSetValue = false; | |
145 | } | |
146 | } | |
147 | ||
148 | void wxComboBox::SetString(unsigned int WXUNUSED(n), const wxString& WXUNUSED(s)) | |
149 | { | |
150 | wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") ); | |
151 | } | |
152 | ||
153 | // TODO auto-sorting is not supported by the code | |
154 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items, | |
155 | unsigned int pos, | |
156 | void **clientData, | |
157 | wxClientDataType type) | |
158 | { | |
159 | const unsigned int numItems = items.GetCount(); | |
160 | ||
161 | AllocClientData(numItems); | |
162 | for( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
163 | { | |
164 | wxXmString str( items[i].c_str() ); | |
165 | XmComboBoxAddItem((Widget) m_mainWidget, str(), GetMotifPosition(pos)); | |
166 | m_stringArray.Insert(items[i], pos); | |
167 | InsertNewItemClientData(pos, clientData, i, type); | |
168 | } | |
169 | ||
170 | return pos - 1; | |
171 | } | |
172 | ||
173 | void wxComboBox::DoDeleteOneItem(unsigned int n) | |
174 | { | |
175 | XmComboBoxDeletePos((Widget) m_mainWidget, n+1); | |
176 | m_stringArray.RemoveAt(n); | |
177 | wxControlWithItems::DoDeleteOneItem(n); | |
178 | } | |
179 | ||
180 | void wxComboBox::Clear() | |
181 | { | |
182 | XmComboBoxDeleteAllItems((Widget) m_mainWidget); | |
183 | m_stringArray.Clear(); | |
184 | ||
185 | wxControlWithItems::DoClear(); | |
186 | } | |
187 | ||
188 | void wxComboBox::SetSelection (int n) | |
189 | { | |
190 | XmComboBoxSelectPos((Widget) m_mainWidget, n+1, False); | |
191 | } | |
192 | ||
193 | int wxComboBox::GetSelection (void) const | |
194 | { | |
195 | int sel = XmComboBoxGetSelectedPos((Widget) m_mainWidget); | |
196 | if (sel == 0) | |
197 | return -1; | |
198 | else | |
199 | return sel - 1; | |
200 | } | |
201 | ||
202 | wxString wxComboBox::GetString(unsigned int n) const | |
203 | { | |
204 | return m_stringArray[n]; | |
205 | } | |
206 | ||
207 | int wxComboBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const | |
208 | { | |
209 | // FIXME: back to base class for not supported value of bCase | |
210 | ||
211 | int *pos_list = NULL; | |
212 | int count = 0; | |
213 | wxXmString text( s ); | |
214 | bool found = (XmComboBoxGetMatchPos((Widget) m_mainWidget, | |
215 | text(), &pos_list, &count) != 0); | |
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 wxNOT_FOUND; | |
225 | } | |
226 | ||
227 | void wxComboBoxCallback (Widget WXUNUSED(w), XtPointer clientData, | |
228 | XmComboBoxSelectionCallbackStruct * cbs) | |
229 | { | |
230 | wxComboBox *item = (wxComboBox *) clientData; | |
231 | ||
232 | switch (cbs->reason) | |
233 | { | |
234 | case XmCR_SINGLE_SELECT: | |
235 | case XmCR_BROWSE_SELECT: | |
236 | { | |
237 | wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, | |
238 | item->GetId()); | |
239 | event.SetInt(cbs->index - 1); | |
240 | event.SetString( item->GetString ( event.GetInt() ) ); | |
241 | if ( item->HasClientObjectData() ) | |
242 | event.SetClientObject( item->GetClientObject(cbs->index - 1) ); | |
243 | else if ( item->HasClientUntypedData() ) | |
244 | event.SetClientData( item->GetClientData(cbs->index - 1) ); | |
245 | event.SetExtraLong(true); | |
246 | event.SetEventObject(item); | |
247 | item->ProcessCommand (event); | |
248 | break; | |
249 | } | |
250 | case XmCR_VALUE_CHANGED: | |
251 | { | |
252 | wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId()); | |
253 | event.SetInt(-1); | |
254 | event.SetString( item->GetValue() ); | |
255 | event.SetExtraLong(true); | |
256 | event.SetEventObject(item); | |
257 | item->ProcessCommand (event); | |
258 | break; | |
259 | } | |
260 | default: | |
261 | break; | |
262 | } | |
263 | } | |
264 | ||
265 | void wxComboBox::ChangeFont(bool keepOriginalSize) | |
266 | { | |
267 | // Don't use the base class wxChoice's ChangeFont | |
268 | wxWindow::ChangeFont(keepOriginalSize); | |
269 | } | |
270 | ||
271 | void wxComboBox::ChangeBackgroundColour() | |
272 | { | |
273 | wxWindow::ChangeBackgroundColour(); | |
274 | } | |
275 | ||
276 | void wxComboBox::ChangeForegroundColour() | |
277 | { | |
278 | wxWindow::ChangeForegroundColour(); | |
279 | } | |
280 | ||
281 | wxSize wxComboBox::DoGetBestSize() const | |
282 | { | |
283 | if( (GetWindowStyle() & wxCB_DROPDOWN) == wxCB_DROPDOWN || | |
284 | (GetWindowStyle() & wxCB_READONLY) == wxCB_READONLY ) | |
285 | { | |
286 | wxSize items = GetItemsSize(); | |
287 | // FIXME arbitrary constants | |
288 | return wxSize( ( items.x ? items.x + 50 : 120 ), | |
289 | items.y + 10 ); | |
290 | } | |
291 | else | |
292 | return wxWindow::DoGetBestSize(); | |
293 | } | |
294 | ||
295 | WXWidget wxComboBox::GetTextWidget() const | |
296 | { | |
297 | return (WXWidget)XmComboBoxGetEditWidget((Widget) m_mainWidget); | |
298 | } | |
299 | ||
300 | #endif // XmVersion < 2000 | |
301 | ||
302 | #endif // wxUSE_COMBOBOX |