]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: wxComboBox class | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
465605e0 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "combobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/combobox.h" | |
03e11df5 | 17 | #include "wx/menu.h" |
519cb848 | 18 | #include "wx/mac/uma.h" |
e9576ca5 | 19 | |
2f1ae414 | 20 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 21 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) |
2f1ae414 | 22 | #endif |
e9576ca5 | 23 | |
12f31626 SC |
24 | // composite combobox implementation by Dan "Bud" Keith bud@otsys.com |
25 | ||
519cb848 | 26 | |
0e03d1fa | 27 | static int nextPopUpMenuId = 1000 ; |
892e461e SC |
28 | MenuHandle NewUniqueMenu() |
29 | { | |
30 | MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ; | |
31 | nextPopUpMenuId++ ; | |
32 | return handle ; | |
33 | } | |
519cb848 | 34 | |
12f31626 SC |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // constants | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // the margin between the text control and the choice | |
41 | static const wxCoord MARGIN = 2; | |
42 | static const int POPUPWIDTH = 18; | |
43 | static const int POPUPHEIGHT = 23; | |
44 | ||
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // wxComboBoxText: text control forwards events to combobox | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | class wxComboBoxText : public wxTextCtrl | |
51 | { | |
52 | public: | |
53 | wxComboBoxText( wxComboBox * cb ) | |
327788ac | 54 | : wxTextCtrl( cb , 1 ) |
12f31626 SC |
55 | { |
56 | m_cb = cb; | |
57 | } | |
58 | ||
59 | protected: | |
60 | void OnTextChange( wxCommandEvent& event ) | |
61 | { | |
62 | wxString s = GetValue(); | |
22a70443 RR |
63 | |
64 | if (!s.IsEmpty()) | |
65 | m_cb->DelegateTextChanged( s ); | |
12f31626 SC |
66 | |
67 | event.Skip(); | |
68 | } | |
69 | ||
70 | private: | |
71 | wxComboBox *m_cb; | |
72 | ||
73 | DECLARE_EVENT_TABLE() | |
74 | }; | |
75 | ||
76 | BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl) | |
77 | EVT_TEXT(-1, wxComboBoxText::OnTextChange) | |
78 | END_EVENT_TABLE() | |
79 | ||
80 | class wxComboBoxChoice : public wxChoice | |
81 | { | |
82 | public: | |
83 | wxComboBoxChoice(wxComboBox *cb, int style) | |
327788ac | 84 | : wxChoice( cb , 1 ) |
12f31626 SC |
85 | { |
86 | m_cb = cb; | |
87 | } | |
88 | ||
89 | protected: | |
90 | void OnChoice( wxCommandEvent& e ) | |
91 | { | |
92 | wxString s = e.GetString(); | |
93 | ||
94 | m_cb->DelegateChoice( s ); | |
95 | } | |
96 | ||
97 | private: | |
98 | wxComboBox *m_cb; | |
99 | ||
100 | DECLARE_EVENT_TABLE() | |
101 | }; | |
102 | ||
103 | BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) | |
104 | EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) | |
105 | END_EVENT_TABLE() | |
106 | ||
12f31626 SC |
107 | wxComboBox::~wxComboBox() |
108 | { | |
f5bb2251 | 109 | // delete the controls now, don't leave them alive even though they would |
12f31626 SC |
110 | // still be eventually deleted by our parent - but it will be too late, the |
111 | // user code expects them to be gone now | |
f5bb2251 GD |
112 | if (m_text != NULL) { |
113 | delete m_text; | |
114 | m_text = NULL; | |
115 | } | |
116 | if (m_choice != NULL) { | |
117 | delete m_choice; | |
118 | m_choice = NULL; | |
119 | } | |
12f31626 SC |
120 | } |
121 | ||
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // geometry | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | wxSize wxComboBox::DoGetBestSize() const | |
128 | { | |
129 | wxSize size = m_choice->GetBestSize(); | |
130 | ||
d99937cd | 131 | if ( m_text != NULL ) |
12f31626 SC |
132 | { |
133 | wxSize sizeText = m_text->GetBestSize(); | |
134 | ||
135 | size.x = POPUPWIDTH + sizeText.x + MARGIN; | |
136 | } | |
137 | ||
138 | return size; | |
139 | } | |
140 | ||
141 | void wxComboBox::DoMoveWindow(int x, int y, int width, int height) { | |
142 | height = POPUPHEIGHT; | |
143 | ||
144 | wxControl::DoMoveWindow(x, y, width, height); | |
145 | ||
d99937cd | 146 | if ( m_text == NULL ) |
12f31626 | 147 | { |
327788ac | 148 | m_choice->SetSize(0, 0 , width, -1); |
12f31626 SC |
149 | } |
150 | else | |
151 | { | |
152 | wxCoord wText = width - POPUPWIDTH; | |
327788ac SC |
153 | m_text->SetSize(0, 0, wText, height); |
154 | m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1); | |
12f31626 SC |
155 | } |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // operations forwarded to the subcontrols | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | bool wxComboBox::Enable(bool enable) | |
165 | { | |
166 | if ( !wxControl::Enable(enable) ) | |
167 | return FALSE; | |
168 | ||
12f31626 SC |
169 | return TRUE; |
170 | } | |
171 | ||
172 | bool wxComboBox::Show(bool show) | |
173 | { | |
174 | if ( !wxControl::Show(show) ) | |
175 | return FALSE; | |
176 | ||
12f31626 SC |
177 | return TRUE; |
178 | } | |
179 | ||
d99937cd GD |
180 | void wxComboBox::SetFocus() |
181 | { | |
182 | if ( m_text != NULL) { | |
183 | m_text->SetFocus(); | |
184 | } | |
185 | } | |
465605e0 | 186 | |
12f31626 | 187 | |
d99937cd GD |
188 | void wxComboBox::DelegateTextChanged( const wxString& value ) |
189 | { | |
12f31626 SC |
190 | } |
191 | ||
192 | ||
193 | void wxComboBox::DelegateChoice( const wxString& value ) | |
194 | { | |
195 | SetStringSelection( value ); | |
196 | } | |
197 | ||
198 | ||
e9576ca5 SC |
199 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, |
200 | const wxString& value, | |
201 | const wxPoint& pos, | |
202 | const wxSize& size, | |
465605e0 RR |
203 | int n, const wxString choices[], |
204 | long style, | |
e9576ca5 SC |
205 | const wxValidator& validator, |
206 | const wxString& name) | |
207 | { | |
12f31626 SC |
208 | |
209 | Rect bounds ; | |
210 | Str255 title ; | |
211 | ||
327788ac | 212 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style , |
12f31626 SC |
213 | wxDefaultValidator, name) ) |
214 | { | |
215 | return FALSE; | |
216 | } | |
217 | ||
327788ac | 218 | m_choice = new wxComboBoxChoice(this, style ); |
12f31626 SC |
219 | |
220 | wxSize csize = size; | |
221 | if ( style & wxCB_READONLY ) | |
222 | { | |
d99937cd | 223 | m_text = NULL; |
12f31626 SC |
224 | } |
225 | else | |
226 | { | |
227 | m_text = new wxComboBoxText(this); | |
228 | if ( size.y == -1 ) { | |
229 | csize.y = m_text->GetSize().y ; | |
230 | } | |
231 | } | |
232 | ||
233 | DoSetSize(pos.x, pos.y, csize.x, csize.y); | |
327788ac | 234 | |
12f31626 SC |
235 | for ( int i = 0 ; i < n ; i++ ) |
236 | { | |
465605e0 | 237 | m_choice->DoAppend( choices[ i ] ); |
12f31626 SC |
238 | } |
239 | ||
12f31626 | 240 | return TRUE; |
e9576ca5 SC |
241 | } |
242 | ||
243 | wxString wxComboBox::GetValue() const | |
244 | { | |
12f31626 SC |
245 | wxString result; |
246 | ||
d99937cd | 247 | if ( m_text == NULL ) |
12f31626 SC |
248 | { |
249 | result = m_choice->GetString( m_choice->GetSelection() ); | |
250 | } | |
251 | else | |
252 | { | |
253 | result = m_text->GetValue(); | |
254 | } | |
255 | ||
256 | return result; | |
e9576ca5 SC |
257 | } |
258 | ||
259 | void wxComboBox::SetValue(const wxString& value) | |
260 | { | |
519cb848 | 261 | SetStringSelection( value ) ; |
e9576ca5 SC |
262 | } |
263 | ||
264 | // Clipboard operations | |
265 | void wxComboBox::Copy() | |
266 | { | |
d99937cd | 267 | if ( m_text != NULL ) |
12f31626 SC |
268 | { |
269 | m_text->Copy(); | |
270 | } | |
e9576ca5 SC |
271 | } |
272 | ||
273 | void wxComboBox::Cut() | |
274 | { | |
d99937cd | 275 | if ( m_text != NULL ) |
12f31626 SC |
276 | { |
277 | m_text->Cut(); | |
278 | } | |
e9576ca5 SC |
279 | } |
280 | ||
281 | void wxComboBox::Paste() | |
282 | { | |
d99937cd | 283 | if ( m_text != NULL ) |
12f31626 SC |
284 | { |
285 | m_text->Paste(); | |
286 | } | |
e9576ca5 SC |
287 | } |
288 | ||
289 | void wxComboBox::SetEditable(bool editable) | |
290 | { | |
d99937cd | 291 | if ( ( m_text == NULL ) && editable ) |
12f31626 SC |
292 | { |
293 | m_text = new wxComboBoxText( this ); | |
294 | } | |
d99937cd | 295 | else if ( ( m_text != NULL ) && !editable ) |
12f31626 SC |
296 | { |
297 | delete m_text; | |
d99937cd | 298 | m_text = NULL; |
12f31626 SC |
299 | } |
300 | ||
301 | int currentX, currentY; | |
302 | GetPosition( ¤tX, ¤tY ); | |
303 | ||
304 | int currentW, currentH; | |
305 | GetSize( ¤tW, ¤tH ); | |
306 | ||
307 | DoMoveWindow( currentX, currentY, currentW, currentH ); | |
e9576ca5 SC |
308 | } |
309 | ||
310 | void wxComboBox::SetInsertionPoint(long pos) | |
311 | { | |
312 | // TODO | |
313 | } | |
314 | ||
315 | void wxComboBox::SetInsertionPointEnd() | |
316 | { | |
317 | // TODO | |
318 | } | |
319 | ||
320 | long wxComboBox::GetInsertionPoint() const | |
321 | { | |
322 | // TODO | |
323 | return 0; | |
324 | } | |
325 | ||
326 | long wxComboBox::GetLastPosition() const | |
327 | { | |
328 | // TODO | |
329 | return 0; | |
330 | } | |
331 | ||
332 | void wxComboBox::Replace(long from, long to, const wxString& value) | |
333 | { | |
334 | // TODO | |
335 | } | |
336 | ||
337 | void wxComboBox::Remove(long from, long to) | |
338 | { | |
339 | // TODO | |
340 | } | |
341 | ||
342 | void wxComboBox::SetSelection(long from, long to) | |
343 | { | |
344 | // TODO | |
345 | } | |
346 | ||
347 | void wxComboBox::Append(const wxString& item) | |
348 | { | |
22a70443 RR |
349 | // I am not sure what other ports do, |
350 | // but wxMac chokes on empty entries. | |
351 | ||
352 | if (!item.IsEmpty()) | |
353 | m_choice->DoAppend( item ); | |
e9576ca5 SC |
354 | } |
355 | ||
356 | void wxComboBox::Delete(int n) | |
357 | { | |
12f31626 | 358 | m_choice->Delete( n ); |
e9576ca5 SC |
359 | } |
360 | ||
361 | void wxComboBox::Clear() | |
362 | { | |
12f31626 | 363 | m_choice->Clear(); |
e9576ca5 SC |
364 | } |
365 | ||
366 | int wxComboBox::GetSelection() const | |
367 | { | |
12f31626 | 368 | return m_choice->GetSelection(); |
e9576ca5 SC |
369 | } |
370 | ||
371 | void wxComboBox::SetSelection(int n) | |
372 | { | |
12f31626 SC |
373 | m_choice->SetSelection( n ); |
374 | ||
d99937cd | 375 | if ( m_text != NULL ) |
12f31626 SC |
376 | { |
377 | m_text->SetValue( GetString( n ) ); | |
378 | } | |
e9576ca5 SC |
379 | } |
380 | ||
381 | int wxComboBox::FindString(const wxString& s) const | |
382 | { | |
12f31626 | 383 | return m_choice->FindString( s ); |
e9576ca5 SC |
384 | } |
385 | ||
386 | wxString wxComboBox::GetString(int n) const | |
387 | { | |
12f31626 | 388 | return m_choice->GetString( n ); |
e9576ca5 SC |
389 | } |
390 | ||
391 | wxString wxComboBox::GetStringSelection() const | |
392 | { | |
519cb848 SC |
393 | int sel = GetSelection (); |
394 | if (sel > -1) | |
395 | return wxString(this->GetString (sel)); | |
396 | else | |
397 | return wxString(""); | |
e9576ca5 SC |
398 | } |
399 | ||
400 | bool wxComboBox::SetStringSelection(const wxString& sel) | |
401 | { | |
519cb848 SC |
402 | int s = FindString (sel); |
403 | if (s > -1) | |
404 | { | |
405 | SetSelection (s); | |
406 | return TRUE; | |
407 | } | |
408 | else | |
409 | return FALSE; | |
410 | } | |
411 | ||
76a5e5d2 | 412 | void wxComboBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
519cb848 SC |
413 | { |
414 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
465605e0 | 415 | event.SetInt(GetSelection()); |
519cb848 | 416 | event.SetEventObject(this); |
0a67a93b | 417 | event.SetString(GetStringSelection()); |
519cb848 | 418 | ProcessCommand(event); |
e9576ca5 | 419 | } |
519cb848 | 420 |