]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/combocmn.cpp | |
3 | // Purpose: wxComboCtrlBase | |
4 | // Author: Jaakko Salli | |
5 | // Modified by: | |
6 | // Created: Apr-30-2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Jaakko Salli | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_COMBOCTRL | |
27 | ||
28 | #include "wx/combobox.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/log.h" | |
32 | #include "wx/dcclient.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/dialog.h" | |
35 | #include "wx/timer.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/tooltip.h" | |
39 | ||
40 | #include "wx/combo.h" | |
41 | ||
42 | ||
43 | ||
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
48 | ||
49 | #define BMP_BUTTON_MARGIN 4 | |
50 | ||
51 | #define DEFAULT_POPUP_HEIGHT 400 | |
52 | ||
53 | #define DEFAULT_TEXT_INDENT 3 | |
54 | ||
55 | #define COMBO_MARGIN 2 // spacing right of wxTextCtrl | |
56 | ||
57 | ||
58 | #if defined(__WXMSW__) | |
59 | ||
60 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
61 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
62 | // native controls work on it like normal. | |
63 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
64 | #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered | |
65 | #define FOCUS_RING 0 // No focus ring on wxMSW | |
66 | ||
67 | //#undef wxUSE_POPUPWIN | |
68 | //#define wxUSE_POPUPWIN 0 | |
69 | ||
70 | #elif defined(__WXGTK__) | |
71 | ||
72 | // NB: It is not recommended to use wxDialog as popup on wxGTK, because of | |
73 | // this bug: If wxDialog is hidden, its position becomes corrupt | |
74 | // between hide and next show, but without internal coordinates being | |
75 | // reflected (or something like that - atleast commenting out ->Hide() | |
76 | // seemed to eliminate the position change). | |
77 | ||
78 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
79 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
80 | // native controls work on it like normal. | |
81 | #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window. | |
82 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
83 | #define FOCUS_RING 0 // No focus ring on wxGTK | |
84 | ||
85 | #elif defined(__WXMAC__) | |
86 | ||
87 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
88 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
89 | // native controls work on it like normal. | |
90 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
91 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
92 | #define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display | |
93 | ||
94 | #undef DEFAULT_DROPBUTTON_WIDTH | |
95 | #define DEFAULT_DROPBUTTON_WIDTH 22 | |
96 | #undef COMBO_MARGIN | |
97 | #define COMBO_MARGIN FOCUS_RING | |
98 | ||
99 | #else | |
100 | ||
101 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
102 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
103 | // native controls work on it like normal. | |
104 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
105 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
106 | #define FOCUS_RING 0 | |
107 | ||
108 | #endif | |
109 | ||
110 | ||
111 | // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless | |
112 | // what the wxUSE_POPUPWIN says. | |
113 | // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2) | |
114 | #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__) | |
115 | #undef wxUSE_POPUPWIN | |
116 | #define wxUSE_POPUPWIN 0 | |
117 | #endif | |
118 | ||
119 | ||
120 | #if wxUSE_POPUPWIN | |
121 | #include "wx/popupwin.h" | |
122 | #else | |
123 | #undef USE_TRANSIENT_POPUP | |
124 | #define USE_TRANSIENT_POPUP 0 | |
125 | #endif | |
126 | ||
127 | ||
128 | // Define different types of popup windows | |
129 | enum | |
130 | { | |
131 | POPUPWIN_NONE = 0, | |
132 | POPUPWIN_WXPOPUPTRANSIENTWINDOW = 1, | |
133 | POPUPWIN_WXPOPUPWINDOW = 2, | |
134 | POPUPWIN_WXDIALOG = 3 | |
135 | }; | |
136 | ||
137 | ||
138 | #if USE_TRANSIENT_POPUP | |
139 | // wxPopupTransientWindow is implemented | |
140 | ||
141 | #define wxComboPopupWindowBase wxPopupTransientWindow | |
142 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW | |
143 | #define USES_WXPOPUPTRANSIENTWINDOW 1 | |
144 | ||
145 | #if TRANSIENT_POPUPWIN_IS_PERFECT | |
146 | // | |
147 | #elif POPUPWIN_IS_PERFECT | |
148 | #define wxComboPopupWindowBase2 wxPopupWindow | |
149 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW | |
150 | #define USES_WXPOPUPWINDOW 1 | |
151 | #else | |
152 | #define wxComboPopupWindowBase2 wxDialog | |
153 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
154 | #define USES_WXDIALOG 1 | |
155 | #endif | |
156 | ||
157 | #elif wxUSE_POPUPWIN | |
158 | // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented | |
159 | ||
160 | #define wxComboPopupWindowBase wxPopupWindow | |
161 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW | |
162 | #define USES_WXPOPUPWINDOW 1 | |
163 | ||
164 | #if !POPUPWIN_IS_PERFECT | |
165 | #define wxComboPopupWindowBase2 wxDialog | |
166 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
167 | #define USES_WXDIALOG 1 | |
168 | #endif | |
169 | ||
170 | #else | |
171 | // wxPopupWindow is not implemented | |
172 | ||
173 | #define wxComboPopupWindowBase wxDialog | |
174 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
175 | #define USES_WXDIALOG 1 | |
176 | ||
177 | #endif | |
178 | ||
179 | ||
180 | #ifndef USES_WXPOPUPTRANSIENTWINDOW | |
181 | #define USES_WXPOPUPTRANSIENTWINDOW 0 | |
182 | #endif | |
183 | ||
184 | #ifndef USES_WXPOPUPWINDOW | |
185 | #define USES_WXPOPUPWINDOW 0 | |
186 | #endif | |
187 | ||
188 | #ifndef USES_WXDIALOG | |
189 | #define USES_WXDIALOG 0 | |
190 | #endif | |
191 | ||
192 | ||
193 | #if USES_WXPOPUPWINDOW | |
194 | #define INSTALL_TOPLEV_HANDLER 1 | |
195 | #else | |
196 | #define INSTALL_TOPLEV_HANDLER 0 | |
197 | #endif | |
198 | ||
199 | ||
200 | // | |
201 | // ** TODO ** | |
202 | // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow) | |
203 | // | |
204 | ||
205 | ||
206 | // ---------------------------------------------------------------------------- | |
207 | // wxComboFrameEventHandler takes care of hiding the popup when events happen | |
208 | // in its top level parent. | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | #if INSTALL_TOPLEV_HANDLER | |
212 | ||
213 | // | |
214 | // This will no longer be necessary after wxTransientPopupWindow | |
215 | // works well on all platforms. | |
216 | // | |
217 | ||
218 | class wxComboFrameEventHandler : public wxEvtHandler | |
219 | { | |
220 | public: | |
221 | wxComboFrameEventHandler( wxComboCtrlBase* pCb ); | |
222 | virtual ~wxComboFrameEventHandler(); | |
223 | ||
224 | void OnPopup(); | |
225 | ||
226 | void OnIdle( wxIdleEvent& event ); | |
227 | void OnMouseEvent( wxMouseEvent& event ); | |
228 | void OnActivate( wxActivateEvent& event ); | |
229 | void OnResize( wxSizeEvent& event ); | |
230 | void OnMove( wxMoveEvent& event ); | |
231 | void OnMenuEvent( wxMenuEvent& event ); | |
232 | void OnClose( wxCloseEvent& event ); | |
233 | ||
234 | protected: | |
235 | wxWindow* m_focusStart; | |
236 | wxComboCtrlBase* m_combo; | |
237 | ||
238 | private: | |
239 | DECLARE_EVENT_TABLE() | |
240 | }; | |
241 | ||
242 | BEGIN_EVENT_TABLE(wxComboFrameEventHandler, wxEvtHandler) | |
243 | EVT_IDLE(wxComboFrameEventHandler::OnIdle) | |
244 | EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
245 | EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
246 | EVT_SIZE(wxComboFrameEventHandler::OnResize) | |
247 | EVT_MOVE(wxComboFrameEventHandler::OnMove) | |
248 | EVT_MENU_HIGHLIGHT(wxID_ANY,wxComboFrameEventHandler::OnMenuEvent) | |
249 | EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent) | |
250 | EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate) | |
251 | EVT_CLOSE(wxComboFrameEventHandler::OnClose) | |
252 | END_EVENT_TABLE() | |
253 | ||
254 | wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo ) | |
255 | : wxEvtHandler() | |
256 | { | |
257 | m_combo = combo; | |
258 | } | |
259 | ||
260 | wxComboFrameEventHandler::~wxComboFrameEventHandler() | |
261 | { | |
262 | } | |
263 | ||
264 | void wxComboFrameEventHandler::OnPopup() | |
265 | { | |
266 | m_focusStart = ::wxWindow::FindFocus(); | |
267 | } | |
268 | ||
269 | void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event ) | |
270 | { | |
271 | wxWindow* winFocused = ::wxWindow::FindFocus(); | |
272 | ||
273 | wxWindow* popup = m_combo->GetPopupControl()->GetControl(); | |
274 | wxWindow* winpopup = m_combo->GetPopupWindow(); | |
275 | ||
276 | if ( | |
277 | winFocused != m_focusStart && | |
278 | winFocused != popup && | |
279 | winFocused->GetParent() != popup && | |
280 | winFocused != winpopup && | |
281 | winFocused->GetParent() != winpopup && | |
282 | winFocused != m_combo && | |
283 | winFocused != m_combo->GetButton() // GTK (atleast) requires this | |
284 | ) | |
285 | { | |
286 | m_combo->HidePopup(); | |
287 | } | |
288 | ||
289 | event.Skip(); | |
290 | } | |
291 | ||
292 | void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event ) | |
293 | { | |
294 | m_combo->HidePopup(); | |
295 | event.Skip(); | |
296 | } | |
297 | ||
298 | void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
299 | { | |
300 | m_combo->HidePopup(); | |
301 | event.Skip(); | |
302 | } | |
303 | ||
304 | void wxComboFrameEventHandler::OnClose( wxCloseEvent& event ) | |
305 | { | |
306 | m_combo->HidePopup(); | |
307 | event.Skip(); | |
308 | } | |
309 | ||
310 | void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event ) | |
311 | { | |
312 | m_combo->HidePopup(); | |
313 | event.Skip(); | |
314 | } | |
315 | ||
316 | void wxComboFrameEventHandler::OnResize( wxSizeEvent& event ) | |
317 | { | |
318 | m_combo->HidePopup(); | |
319 | event.Skip(); | |
320 | } | |
321 | ||
322 | void wxComboFrameEventHandler::OnMove( wxMoveEvent& event ) | |
323 | { | |
324 | m_combo->HidePopup(); | |
325 | event.Skip(); | |
326 | } | |
327 | ||
328 | #endif // INSTALL_TOPLEV_HANDLER | |
329 | ||
330 | // ---------------------------------------------------------------------------- | |
331 | // wxComboPopupWindow is, in essence, wxPopupWindow customized for | |
332 | // wxComboCtrl. | |
333 | // ---------------------------------------------------------------------------- | |
334 | ||
335 | class wxComboPopupWindow : public wxComboPopupWindowBase | |
336 | { | |
337 | public: | |
338 | ||
339 | wxComboPopupWindow( wxComboCtrlBase *parent, | |
340 | int style ) | |
341 | #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW | |
342 | : wxComboPopupWindowBase(parent,style) | |
343 | #else | |
344 | : wxComboPopupWindowBase(parent, | |
345 | wxID_ANY, | |
346 | wxEmptyString, | |
347 | wxPoint(-21,-21), | |
348 | wxSize(20,20), | |
349 | style) | |
350 | #endif | |
351 | { | |
352 | m_inShow = 0; | |
353 | } | |
354 | ||
355 | #if USES_WXPOPUPTRANSIENTWINDOW | |
356 | virtual bool Show( bool show ); | |
357 | virtual bool ProcessLeftDown(wxMouseEvent& event); | |
358 | protected: | |
359 | virtual void OnDismiss(); | |
360 | #endif | |
361 | ||
362 | private: | |
363 | wxByte m_inShow; | |
364 | }; | |
365 | ||
366 | ||
367 | #if USES_WXPOPUPTRANSIENTWINDOW | |
368 | bool wxComboPopupWindow::Show( bool show ) | |
369 | { | |
370 | // Guard against recursion | |
371 | if ( m_inShow ) | |
372 | return wxComboPopupWindowBase::Show(show); | |
373 | ||
374 | m_inShow++; | |
375 | ||
376 | wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow)) ); | |
377 | ||
378 | wxPopupTransientWindow* ptw = (wxPopupTransientWindow*) this; | |
379 | wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent(); | |
380 | ||
381 | if ( show != ptw->IsShown() ) | |
382 | { | |
383 | if ( show ) | |
384 | ptw->Popup(combo->GetPopupControl()->GetControl()); | |
385 | else | |
386 | ptw->Dismiss(); | |
387 | } | |
388 | ||
389 | m_inShow--; | |
390 | ||
391 | return true; | |
392 | } | |
393 | ||
394 | bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent& event) | |
395 | { | |
396 | return wxPopupTransientWindow::ProcessLeftDown(event); | |
397 | } | |
398 | ||
399 | // First thing that happens when a transient popup closes is that this method gets called. | |
400 | void wxComboPopupWindow::OnDismiss() | |
401 | { | |
402 | wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent(); | |
403 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxComboCtrlBase)), | |
404 | wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") ); | |
405 | ||
406 | combo->OnPopupDismiss(); | |
407 | } | |
408 | #endif // USES_WXPOPUPTRANSIENTWINDOW | |
409 | ||
410 | ||
411 | // ---------------------------------------------------------------------------- | |
412 | // wxComboPopupWindowEvtHandler does bulk of the custom event handling | |
413 | // of a popup window. It is separate so we can have different types | |
414 | // of popup windows. | |
415 | // ---------------------------------------------------------------------------- | |
416 | ||
417 | class wxComboPopupWindowEvtHandler : public wxEvtHandler | |
418 | { | |
419 | public: | |
420 | ||
421 | wxComboPopupWindowEvtHandler( wxComboCtrlBase *parent ) | |
422 | { | |
423 | m_combo = parent; | |
424 | } | |
425 | ||
426 | void OnSizeEvent( wxSizeEvent& event ); | |
427 | void OnKeyEvent(wxKeyEvent& event); | |
428 | #if USES_WXDIALOG | |
429 | void OnActivate( wxActivateEvent& event ); | |
430 | #endif | |
431 | ||
432 | private: | |
433 | wxComboCtrlBase* m_combo; | |
434 | ||
435 | DECLARE_EVENT_TABLE() | |
436 | }; | |
437 | ||
438 | ||
439 | BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler, wxEvtHandler) | |
440 | EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent) | |
441 | EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent) | |
442 | #if USES_WXDIALOG | |
443 | EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate) | |
444 | #endif | |
445 | EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent) | |
446 | END_EVENT_TABLE() | |
447 | ||
448 | ||
449 | void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent& WXUNUSED(event) ) | |
450 | { | |
451 | // Block the event so that the popup control does not get auto-resized. | |
452 | } | |
453 | ||
454 | void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent& event ) | |
455 | { | |
456 | // Relay keyboard event to the main child controls | |
457 | wxWindowList children = m_combo->GetPopupWindow()->GetChildren(); | |
458 | wxWindowList::iterator node = children.begin(); | |
459 | wxWindow* child = (wxWindow*)*node; | |
460 | child->AddPendingEvent(event); | |
461 | } | |
462 | ||
463 | #if USES_WXDIALOG | |
464 | void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent& event ) | |
465 | { | |
466 | if ( !event.GetActive() ) | |
467 | { | |
468 | // Tell combo control that we are dismissed. | |
469 | m_combo->HidePopup(); | |
470 | ||
471 | event.Skip(); | |
472 | } | |
473 | } | |
474 | #endif | |
475 | ||
476 | ||
477 | // ---------------------------------------------------------------------------- | |
478 | // wxComboPopup | |
479 | // | |
480 | // ---------------------------------------------------------------------------- | |
481 | ||
482 | wxComboPopup::~wxComboPopup() | |
483 | { | |
484 | } | |
485 | ||
486 | void wxComboPopup::OnPopup() | |
487 | { | |
488 | } | |
489 | ||
490 | void wxComboPopup::OnDismiss() | |
491 | { | |
492 | } | |
493 | ||
494 | wxSize wxComboPopup::GetAdjustedSize( int minWidth, | |
495 | int prefHeight, | |
496 | int WXUNUSED(maxHeight) ) | |
497 | { | |
498 | return wxSize(minWidth,prefHeight); | |
499 | } | |
500 | ||
501 | void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo, | |
502 | wxDC& dc, const wxRect& rect ) | |
503 | { | |
504 | if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl | |
505 | { | |
506 | combo->PrepareBackground(dc,rect,0); | |
507 | ||
508 | dc.DrawText( combo->GetValue(), | |
509 | rect.x + combo->GetTextIndent(), | |
510 | (rect.height-dc.GetCharHeight())/2 + rect.y ); | |
511 | } | |
512 | } | |
513 | ||
514 | void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect ) | |
515 | { | |
516 | DefaultPaintComboControl(m_combo,dc,rect); | |
517 | } | |
518 | ||
519 | void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event ) | |
520 | { | |
521 | event.Skip(); | |
522 | } | |
523 | ||
524 | void wxComboPopup::OnComboDoubleClick() | |
525 | { | |
526 | } | |
527 | ||
528 | void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) ) | |
529 | { | |
530 | } | |
531 | ||
532 | bool wxComboPopup::LazyCreate() | |
533 | { | |
534 | return false; | |
535 | } | |
536 | ||
537 | void wxComboPopup::Dismiss() | |
538 | { | |
539 | m_combo->HidePopup(); | |
540 | } | |
541 | ||
542 | // ---------------------------------------------------------------------------- | |
543 | // input handling | |
544 | // ---------------------------------------------------------------------------- | |
545 | ||
546 | // | |
547 | // This is pushed to the event handler queue of the child textctrl. | |
548 | // | |
549 | class wxComboBoxExtraInputHandler : public wxEvtHandler | |
550 | { | |
551 | public: | |
552 | ||
553 | wxComboBoxExtraInputHandler( wxComboCtrlBase* combo ) | |
554 | : wxEvtHandler() | |
555 | { | |
556 | m_combo = combo; | |
557 | } | |
558 | virtual ~wxComboBoxExtraInputHandler() { } | |
559 | void OnKey(wxKeyEvent& event); | |
560 | void OnFocus(wxFocusEvent& event); | |
561 | ||
562 | protected: | |
563 | wxComboCtrlBase* m_combo; | |
564 | ||
565 | private: | |
566 | DECLARE_EVENT_TABLE() | |
567 | }; | |
568 | ||
569 | ||
570 | BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler) | |
571 | EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey) | |
572 | EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus) | |
573 | END_EVENT_TABLE() | |
574 | ||
575 | ||
576 | void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event) | |
577 | { | |
578 | // Let the wxComboCtrl event handler have a go first. | |
579 | wxComboCtrlBase* combo = m_combo; | |
580 | wxObject* prevObj = event.GetEventObject(); | |
581 | ||
582 | event.SetId(combo->GetId()); | |
583 | event.SetEventObject(combo); | |
584 | combo->GetEventHandler()->ProcessEvent(event); | |
585 | ||
586 | event.SetId(((wxWindow*)prevObj)->GetId()); | |
587 | event.SetEventObject(prevObj); | |
588 | } | |
589 | ||
590 | void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event) | |
591 | { | |
592 | // FIXME: This code does run when control is clicked, | |
593 | // yet on Windows it doesn't select all the text. | |
594 | if ( !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) ) | |
595 | { | |
596 | if ( m_combo->GetTextCtrl() ) | |
597 | m_combo->GetTextCtrl()->SelectAll(); | |
598 | else | |
599 | m_combo->SetSelection(-1,-1); | |
600 | } | |
601 | ||
602 | // Send focus indication to parent. | |
603 | // NB: This is needed for cases where the textctrl gets focus | |
604 | // instead of its parent. While this may trigger multiple | |
605 | // wxEVT_SET_FOCUSes (since m_text->SetFocus is called | |
606 | // from combo's focus event handler), they should be quite | |
607 | // harmless. | |
608 | wxFocusEvent evt2(wxEVT_SET_FOCUS,m_combo->GetId()); | |
609 | evt2.SetEventObject(m_combo); | |
610 | m_combo->GetEventHandler()->ProcessEvent(evt2); | |
611 | ||
612 | event.Skip(); | |
613 | } | |
614 | ||
615 | ||
616 | // | |
617 | // This is pushed to the event handler queue of the control in popup. | |
618 | // | |
619 | ||
620 | class wxComboPopupExtraEventHandler : public wxEvtHandler | |
621 | { | |
622 | public: | |
623 | ||
624 | wxComboPopupExtraEventHandler( wxComboCtrlBase* combo ) | |
625 | : wxEvtHandler() | |
626 | { | |
627 | m_combo = combo; | |
628 | m_beenInside = false; | |
629 | } | |
630 | virtual ~wxComboPopupExtraEventHandler() { } | |
631 | ||
632 | void OnMouseEvent( wxMouseEvent& event ); | |
633 | ||
634 | // Called from wxComboCtrlBase::OnPopupDismiss | |
635 | void OnPopupDismiss() | |
636 | { | |
637 | m_beenInside = false; | |
638 | } | |
639 | ||
640 | protected: | |
641 | wxComboCtrlBase* m_combo; | |
642 | ||
643 | bool m_beenInside; | |
644 | ||
645 | private: | |
646 | DECLARE_EVENT_TABLE() | |
647 | }; | |
648 | ||
649 | ||
650 | BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler) | |
651 | EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent) | |
652 | END_EVENT_TABLE() | |
653 | ||
654 | ||
655 | void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
656 | { | |
657 | wxPoint pt = event.GetPosition(); | |
658 | wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize(); | |
659 | int evtType = event.GetEventType(); | |
660 | bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y; | |
661 | ||
662 | if ( evtType == wxEVT_MOTION || | |
663 | evtType == wxEVT_LEFT_DOWN || | |
664 | evtType == wxEVT_RIGHT_DOWN ) | |
665 | { | |
666 | // Block motion and click events outside the popup | |
667 | if ( !isInside || !m_combo->IsPopupShown() ) | |
668 | { | |
669 | event.Skip(false); | |
670 | return; | |
671 | } | |
672 | } | |
673 | else if ( evtType == wxEVT_LEFT_UP ) | |
674 | { | |
675 | if ( !m_combo->IsPopupShown() ) | |
676 | { | |
677 | event.Skip(false); | |
678 | return; | |
679 | } | |
680 | ||
681 | if ( !m_beenInside ) | |
682 | { | |
683 | if ( isInside ) | |
684 | { | |
685 | m_beenInside = true; | |
686 | } | |
687 | else | |
688 | { | |
689 | // | |
690 | // Some mouse events to popup that happen outside it, before cursor | |
691 | // has been inside the popu, need to be ignored by it but relayed to | |
692 | // the dropbutton. | |
693 | // | |
694 | wxWindow* btn = m_combo->GetButton(); | |
695 | if ( btn ) | |
696 | btn->GetEventHandler()->AddPendingEvent(event); | |
697 | else | |
698 | m_combo->GetEventHandler()->AddPendingEvent(event); | |
699 | ||
700 | return; | |
701 | } | |
702 | ||
703 | event.Skip(); | |
704 | } | |
705 | } | |
706 | ||
707 | event.Skip(); | |
708 | } | |
709 | ||
710 | // ---------------------------------------------------------------------------- | |
711 | // wxComboCtrlBase | |
712 | // ---------------------------------------------------------------------------- | |
713 | ||
714 | ||
715 | BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl) | |
716 | EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
717 | EVT_SIZE(wxComboCtrlBase::OnSizeEvent) | |
718 | EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
719 | EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
720 | EVT_IDLE(wxComboCtrlBase::OnIdleEvent) | |
721 | //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent) | |
722 | EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent) | |
723 | EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
724 | EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged) | |
725 | END_EVENT_TABLE() | |
726 | ||
727 | ||
728 | IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl) | |
729 | ||
730 | void wxComboCtrlBase::Init() | |
731 | { | |
732 | m_winPopup = (wxWindow *)NULL; | |
733 | m_popup = (wxWindow *)NULL; | |
734 | m_popupWinState = Hidden; | |
735 | m_btn = (wxWindow*) NULL; | |
736 | m_text = (wxTextCtrl*) NULL; | |
737 | m_popupInterface = (wxComboPopup*) NULL; | |
738 | ||
739 | m_popupExtraHandler = (wxEvtHandler*) NULL; | |
740 | m_textEvtHandler = (wxEvtHandler*) NULL; | |
741 | ||
742 | #if INSTALL_TOPLEV_HANDLER | |
743 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
744 | #endif | |
745 | ||
746 | m_mainCtrlWnd = this; | |
747 | ||
748 | m_heightPopup = -1; | |
749 | m_widthMinPopup = -1; | |
750 | m_anchorSide = 0; | |
751 | m_widthCustomPaint = 0; | |
752 | m_widthCustomBorder = 0; | |
753 | ||
754 | m_btnState = 0; | |
755 | m_btnWidDefault = 0; | |
756 | m_blankButtonBg = false; | |
757 | m_ignoreEvtText = 0; | |
758 | m_popupWinType = POPUPWIN_NONE; | |
759 | m_btnWid = m_btnHei = -1; | |
760 | m_btnSide = wxRIGHT; | |
761 | m_btnSpacingX = 0; | |
762 | ||
763 | m_extLeft = 0; | |
764 | m_extRight = 0; | |
765 | m_absIndent = -1; | |
766 | m_iFlags = 0; | |
767 | m_timeCanAcceptClick = 0; | |
768 | ||
769 | m_resetFocus = false; | |
770 | } | |
771 | ||
772 | bool wxComboCtrlBase::Create(wxWindow *parent, | |
773 | wxWindowID id, | |
774 | const wxString& value, | |
775 | const wxPoint& pos, | |
776 | const wxSize& size, | |
777 | long style, | |
778 | const wxValidator& validator, | |
779 | const wxString& name) | |
780 | { | |
781 | if ( !wxControl::Create(parent, | |
782 | id, | |
783 | pos, | |
784 | size, | |
785 | style | wxWANTS_CHARS, | |
786 | validator, | |
787 | name) ) | |
788 | return false; | |
789 | ||
790 | m_valueString = value; | |
791 | ||
792 | // Get colours | |
793 | OnThemeChange(); | |
794 | m_absIndent = GetNativeTextIndent(); | |
795 | ||
796 | m_iFlags |= wxCC_IFLAG_CREATED; | |
797 | ||
798 | // If x and y indicate valid size, wxSizeEvent won't be | |
799 | // emitted automatically, so we need to add artifical one. | |
800 | if ( size.x > 0 && size.y > 0 ) | |
801 | { | |
802 | wxSizeEvent evt(size,GetId()); | |
803 | GetEventHandler()->AddPendingEvent(evt); | |
804 | } | |
805 | ||
806 | return true; | |
807 | } | |
808 | ||
809 | void wxComboCtrlBase::InstallInputHandlers() | |
810 | { | |
811 | if ( m_text ) | |
812 | { | |
813 | m_textEvtHandler = new wxComboBoxExtraInputHandler(this); | |
814 | m_text->PushEventHandler(m_textEvtHandler); | |
815 | } | |
816 | } | |
817 | ||
818 | void | |
819 | wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) | |
820 | { | |
821 | if ( !(m_windowStyle & wxCB_READONLY) ) | |
822 | { | |
823 | if ( m_text ) | |
824 | m_text->Destroy(); | |
825 | ||
826 | // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is | |
827 | // not used by the wxPropertyGrid and therefore the tab is processed by | |
828 | // looking at ancestors to see if they have wxTAB_TRAVERSAL. The | |
829 | // navigation event is then sent to the wrong window. | |
830 | style |= wxTE_PROCESS_TAB; | |
831 | ||
832 | if ( HasFlag(wxTE_PROCESS_ENTER) ) | |
833 | style |= wxTE_PROCESS_ENTER; | |
834 | ||
835 | // Ignore EVT_TEXT generated by the constructor (but only | |
836 | // if the event redirector already exists) | |
837 | // NB: This must be " = 1" instead of "++"; | |
838 | if ( m_textEvtHandler ) | |
839 | m_ignoreEvtText = 1; | |
840 | else | |
841 | m_ignoreEvtText = 0; | |
842 | ||
843 | m_text = new wxTextCtrl(this, wxID_ANY, m_valueString, | |
844 | wxDefaultPosition, wxSize(10,-1), | |
845 | style, validator); | |
846 | } | |
847 | } | |
848 | ||
849 | void wxComboCtrlBase::OnThemeChange() | |
850 | { | |
851 | // Leave the default bg on the Mac so the area used by the focus ring will | |
852 | // be the correct colour and themed brush. Instead we'll use | |
853 | // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed. | |
854 | #ifndef __WXMAC__ | |
855 | SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
856 | #endif | |
857 | } | |
858 | ||
859 | wxComboCtrlBase::~wxComboCtrlBase() | |
860 | { | |
861 | if ( HasCapture() ) | |
862 | ReleaseMouse(); | |
863 | ||
864 | #if INSTALL_TOPLEV_HANDLER | |
865 | delete ((wxComboFrameEventHandler*)m_toplevEvtHandler); | |
866 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
867 | #endif | |
868 | ||
869 | DestroyPopup(); | |
870 | ||
871 | if ( m_text ) | |
872 | m_text->RemoveEventHandler(m_textEvtHandler); | |
873 | ||
874 | delete m_textEvtHandler; | |
875 | } | |
876 | ||
877 | ||
878 | // ---------------------------------------------------------------------------- | |
879 | // geometry stuff | |
880 | // ---------------------------------------------------------------------------- | |
881 | ||
882 | // Recalculates button and textctrl areas | |
883 | void wxComboCtrlBase::CalculateAreas( int btnWidth ) | |
884 | { | |
885 | wxSize sz = GetClientSize(); | |
886 | int customBorder = m_widthCustomBorder; | |
887 | int btnBorder; // border for button only | |
888 | ||
889 | // check if button should really be outside the border: we'll do it it if | |
890 | // its platform default or bitmap+pushbutton background is used, but not if | |
891 | // there is vertical size adjustment or horizontal spacing. | |
892 | if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) || | |
893 | (m_bmpNormal.Ok() && m_blankButtonBg) ) && | |
894 | m_btnSpacingX == 0 && | |
895 | m_btnHei <= 0 ) | |
896 | { | |
897 | m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE; | |
898 | btnBorder = 0; | |
899 | } | |
900 | else | |
901 | { | |
902 | m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE); | |
903 | btnBorder = customBorder; | |
904 | } | |
905 | ||
906 | // Defaul indentation | |
907 | if ( m_absIndent < 0 ) | |
908 | m_absIndent = GetNativeTextIndent(); | |
909 | ||
910 | int butWidth = btnWidth; | |
911 | ||
912 | if ( butWidth <= 0 ) | |
913 | butWidth = m_btnWidDefault; | |
914 | else | |
915 | m_btnWidDefault = butWidth; | |
916 | ||
917 | if ( butWidth <= 0 ) | |
918 | return; | |
919 | ||
920 | int butHeight = sz.y - btnBorder*2; | |
921 | ||
922 | // Adjust button width | |
923 | if ( m_btnWid > 0 ) | |
924 | butWidth = m_btnWid; | |
925 | else | |
926 | { | |
927 | // Adjust button width to match aspect ratio | |
928 | // (but only if control is smaller than best size). | |
929 | int bestHeight = GetBestSize().y; | |
930 | int height = GetSize().y; | |
931 | ||
932 | if ( height < bestHeight ) | |
933 | { | |
934 | // Make very small buttons square, as it makes | |
935 | // them accommodate arrow image better and still | |
936 | // looks decent. | |
937 | if ( height > 18 ) | |
938 | butWidth = (height*butWidth)/bestHeight; | |
939 | else | |
940 | butWidth = butHeight; | |
941 | } | |
942 | } | |
943 | ||
944 | // Adjust button height | |
945 | if ( m_btnHei > 0 ) | |
946 | butHeight = m_btnHei; | |
947 | ||
948 | // Use size of normal bitmap if... | |
949 | // It is larger | |
950 | // OR | |
951 | // button width is set to default and blank button bg is not drawn | |
952 | if ( m_bmpNormal.Ok() ) | |
953 | { | |
954 | int bmpReqWidth = m_bmpNormal.GetWidth(); | |
955 | int bmpReqHeight = m_bmpNormal.GetHeight(); | |
956 | ||
957 | // If drawing blank button background, we need to add some margin. | |
958 | if ( m_blankButtonBg ) | |
959 | { | |
960 | bmpReqWidth += BMP_BUTTON_MARGIN*2; | |
961 | bmpReqHeight += BMP_BUTTON_MARGIN*2; | |
962 | } | |
963 | ||
964 | if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) ) | |
965 | butWidth = bmpReqWidth; | |
966 | if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) ) | |
967 | butHeight = bmpReqHeight; | |
968 | ||
969 | // Need to fix height? | |
970 | if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 ) | |
971 | { | |
972 | int newY = butHeight+(customBorder*2); | |
973 | SetClientSize(wxDefaultCoord,newY); | |
974 | sz.y = newY; | |
975 | } | |
976 | } | |
977 | ||
978 | int butAreaWid = butWidth + (m_btnSpacingX*2); | |
979 | ||
980 | m_btnSize.x = butWidth; | |
981 | m_btnSize.y = butHeight; | |
982 | ||
983 | m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder ); | |
984 | m_btnArea.y = btnBorder + FOCUS_RING; | |
985 | m_btnArea.width = butAreaWid; | |
986 | m_btnArea.height = sz.y - ((btnBorder+FOCUS_RING)*2); | |
987 | ||
988 | m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder + FOCUS_RING; | |
989 | m_tcArea.y = customBorder + FOCUS_RING; | |
990 | m_tcArea.width = sz.x - butAreaWid - (customBorder*2) - (FOCUS_RING*2); | |
991 | m_tcArea.height = sz.y - ((customBorder+FOCUS_RING)*2); | |
992 | ||
993 | /* | |
994 | if ( m_text ) | |
995 | { | |
996 | ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) + | |
997 | wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height)); | |
998 | } | |
999 | */ | |
1000 | } | |
1001 | ||
1002 | void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust ) | |
1003 | { | |
1004 | if ( !m_text ) | |
1005 | return; | |
1006 | ||
1007 | #if !TEXTCTRL_TEXT_CENTERED | |
1008 | ||
1009 | wxSize sz = GetClientSize(); | |
1010 | ||
1011 | int customBorder = m_widthCustomBorder; | |
1012 | if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER ) | |
1013 | { | |
1014 | // Centre textctrl | |
1015 | int tcSizeY = m_text->GetBestSize().y; | |
1016 | int diff = sz.y - tcSizeY; | |
1017 | int y = textCtrlYAdjust + (diff/2); | |
1018 | ||
1019 | if ( y < customBorder ) | |
1020 | y = customBorder; | |
1021 | ||
1022 | m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust, | |
1023 | y, | |
1024 | m_tcArea.width - COMBO_MARGIN - | |
1025 | (textCtrlXAdjust + m_widthCustomPaint + m_absIndent), | |
1026 | -1 ); | |
1027 | ||
1028 | // Make sure textctrl doesn't exceed the bottom custom border | |
1029 | wxSize tsz = m_text->GetSize(); | |
1030 | diff = (y + tsz.y) - (sz.y - customBorder); | |
1031 | if ( diff >= 0 ) | |
1032 | { | |
1033 | tsz.y = tsz.y - diff - 1; | |
1034 | m_text->SetSize(tsz); | |
1035 | } | |
1036 | } | |
1037 | else | |
1038 | #else // TEXTCTRL_TEXT_CENTERED | |
1039 | wxUnusedVar(textCtrlXAdjust); | |
1040 | wxUnusedVar(textCtrlYAdjust); | |
1041 | #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED | |
1042 | { | |
1043 | // If it has border, have textctrl will the entire text field. | |
1044 | m_text->SetSize( m_tcArea.x + m_widthCustomPaint, | |
1045 | m_tcArea.y, | |
1046 | m_tcArea.width - m_widthCustomPaint, | |
1047 | m_tcArea.height ); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | wxSize wxComboCtrlBase::DoGetBestSize() const | |
1052 | { | |
1053 | wxSize sizeText(150,0); | |
1054 | ||
1055 | if ( m_text ) | |
1056 | sizeText = m_text->GetBestSize(); | |
1057 | ||
1058 | // TODO: Better method to calculate close-to-native control height. | |
1059 | ||
1060 | int fhei; | |
1061 | if ( m_font.Ok() ) | |
1062 | fhei = (m_font.GetPointSize()*2) + 5; | |
1063 | else if ( wxNORMAL_FONT->Ok() ) | |
1064 | fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5; | |
1065 | else | |
1066 | fhei = sizeText.y + 4; | |
1067 | ||
1068 | // Need to force height to accomodate bitmap? | |
1069 | int btnSizeY = m_btnSize.y; | |
1070 | if ( m_bmpNormal.Ok() && fhei < btnSizeY ) | |
1071 | fhei = btnSizeY; | |
1072 | ||
1073 | // Control height doesn't depend on border | |
1074 | /* | |
1075 | // Add border | |
1076 | int border = m_windowStyle & wxBORDER_MASK; | |
1077 | if ( border == wxSIMPLE_BORDER ) | |
1078 | fhei += 2; | |
1079 | else if ( border == wxNO_BORDER ) | |
1080 | fhei += (m_widthCustomBorder*2); | |
1081 | else | |
1082 | // Sunken etc. | |
1083 | fhei += 4; | |
1084 | */ | |
1085 | ||
1086 | // Final adjustments | |
1087 | #ifdef __WXGTK__ | |
1088 | fhei += 1; | |
1089 | #endif | |
1090 | ||
1091 | #ifdef __WXMAC__ | |
1092 | // these are the numbers from the HIG: | |
1093 | switch ( m_windowVariant ) | |
1094 | { | |
1095 | case wxWINDOW_VARIANT_NORMAL: | |
1096 | default : | |
1097 | fhei = 22; | |
1098 | break; | |
1099 | case wxWINDOW_VARIANT_SMALL: | |
1100 | fhei = 19; | |
1101 | break; | |
1102 | case wxWINDOW_VARIANT_MINI: | |
1103 | fhei = 15; | |
1104 | break; | |
1105 | } | |
1106 | #endif | |
1107 | ||
1108 | fhei += 2 * FOCUS_RING; | |
1109 | int width = sizeText.x + FOCUS_RING + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH; | |
1110 | ||
1111 | wxSize ret(width, fhei); | |
1112 | CacheBestSize(ret); | |
1113 | return ret; | |
1114 | } | |
1115 | ||
1116 | void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event ) | |
1117 | { | |
1118 | if ( !IsCreated() ) | |
1119 | return; | |
1120 | ||
1121 | // defined by actual wxComboCtrls | |
1122 | OnResize(); | |
1123 | ||
1124 | event.Skip(); | |
1125 | } | |
1126 | ||
1127 | // ---------------------------------------------------------------------------- | |
1128 | // standard operations | |
1129 | // ---------------------------------------------------------------------------- | |
1130 | ||
1131 | bool wxComboCtrlBase::Enable(bool enable) | |
1132 | { | |
1133 | if ( !wxControl::Enable(enable) ) | |
1134 | return false; | |
1135 | ||
1136 | if ( m_btn ) | |
1137 | m_btn->Enable(enable); | |
1138 | if ( m_text ) | |
1139 | m_text->Enable(enable); | |
1140 | ||
1141 | return true; | |
1142 | } | |
1143 | ||
1144 | bool wxComboCtrlBase::Show(bool show) | |
1145 | { | |
1146 | if ( !wxControl::Show(show) ) | |
1147 | return false; | |
1148 | ||
1149 | if (m_btn) | |
1150 | m_btn->Show(show); | |
1151 | ||
1152 | if (m_text) | |
1153 | m_text->Show(show); | |
1154 | ||
1155 | return true; | |
1156 | } | |
1157 | ||
1158 | bool wxComboCtrlBase::SetFont ( const wxFont& font ) | |
1159 | { | |
1160 | if ( !wxControl::SetFont(font) ) | |
1161 | return false; | |
1162 | ||
1163 | if (m_text) | |
1164 | m_text->SetFont(font); | |
1165 | ||
1166 | return true; | |
1167 | } | |
1168 | ||
1169 | #if wxUSE_TOOLTIPS | |
1170 | void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip) | |
1171 | { | |
1172 | wxControl::DoSetToolTip(tooltip); | |
1173 | ||
1174 | // Set tool tip for button and text box | |
1175 | if ( tooltip ) | |
1176 | { | |
1177 | const wxString &tip = tooltip->GetTip(); | |
1178 | if ( m_text ) m_text->SetToolTip(tip); | |
1179 | if ( m_btn ) m_btn->SetToolTip(tip); | |
1180 | } | |
1181 | else | |
1182 | { | |
1183 | if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL ); | |
1184 | if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL ); | |
1185 | } | |
1186 | } | |
1187 | #endif // wxUSE_TOOLTIPS | |
1188 | ||
1189 | #if wxUSE_VALIDATORS | |
1190 | void wxComboCtrlBase::SetValidator(const wxValidator& validator) | |
1191 | { | |
1192 | wxTextCtrl* textCtrl = GetTextCtrl(); | |
1193 | ||
1194 | if ( textCtrl ) | |
1195 | textCtrl->SetValidator( validator ); | |
1196 | } | |
1197 | ||
1198 | wxValidator* wxComboCtrlBase::GetValidator() | |
1199 | { | |
1200 | wxTextCtrl* textCtrl = GetTextCtrl(); | |
1201 | ||
1202 | if ( textCtrl ) | |
1203 | return textCtrl->GetValidator(); | |
1204 | ||
1205 | return wxControl::GetValidator(); | |
1206 | } | |
1207 | #endif // wxUSE_VALIDATORS | |
1208 | ||
1209 | // ---------------------------------------------------------------------------- | |
1210 | // painting | |
1211 | // ---------------------------------------------------------------------------- | |
1212 | ||
1213 | #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__) | |
1214 | // prepare combo box background on area in a way typical on platform | |
1215 | void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const | |
1216 | { | |
1217 | wxSize sz = GetClientSize(); | |
1218 | bool isEnabled; | |
1219 | bool isFocused; // also selected | |
1220 | ||
1221 | // For smaller size control (and for disabled background) use less spacing | |
1222 | int focusSpacingX; | |
1223 | int focusSpacingY; | |
1224 | ||
1225 | if ( !(flags & wxCONTROL_ISSUBMENU) ) | |
1226 | { | |
1227 | // Drawing control | |
1228 | isEnabled = IsEnabled(); | |
1229 | isFocused = ShouldDrawFocus(); | |
1230 | ||
1231 | // Windows-style: for smaller size control (and for disabled background) use less spacing | |
1232 | focusSpacingX = isEnabled ? 2 : 1; | |
1233 | focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1; | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | // Drawing a list item | |
1238 | isEnabled = true; // they are never disabled | |
1239 | isFocused = flags & wxCONTROL_SELECTED ? true : false; | |
1240 | ||
1241 | focusSpacingX = 0; | |
1242 | focusSpacingY = 0; | |
1243 | } | |
1244 | ||
1245 | // Set the background sub-rectangle for selection, disabled etc | |
1246 | wxRect selRect(rect); | |
1247 | selRect.y += focusSpacingY; | |
1248 | selRect.height -= (focusSpacingY*2); | |
1249 | ||
1250 | int wcp = 0; | |
1251 | ||
1252 | if ( !(flags & wxCONTROL_ISSUBMENU) ) | |
1253 | wcp += m_widthCustomPaint; | |
1254 | ||
1255 | selRect.x += wcp + focusSpacingX; | |
1256 | selRect.width -= wcp + (focusSpacingX*2); | |
1257 | ||
1258 | wxColour bgCol; | |
1259 | ||
1260 | if ( isEnabled ) | |
1261 | { | |
1262 | // If popup is hidden and this control is focused, | |
1263 | // then draw the focus-indicator (selbgcolor background etc.). | |
1264 | if ( isFocused ) | |
1265 | { | |
1266 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) ); | |
1267 | bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
1268 | } | |
1269 | else | |
1270 | { | |
1271 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) ); | |
1272 | #ifndef __WXMAC__ // see note in OnThemeChange | |
1273 | bgCol = GetBackgroundColour(); | |
1274 | #else | |
1275 | bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
1276 | #endif | |
1277 | } | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) ); | |
1282 | #ifndef __WXMAC__ // see note in OnThemeChange | |
1283 | bgCol = GetBackgroundColour(); | |
1284 | #else | |
1285 | bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
1286 | #endif | |
1287 | } | |
1288 | ||
1289 | dc.SetBrush( bgCol ); | |
1290 | dc.SetPen( bgCol ); | |
1291 | dc.DrawRectangle( selRect ); | |
1292 | ||
1293 | // Don't clip exactly to the selection rectangle so we can draw | |
1294 | // to the non-selected area in front of it. | |
1295 | wxRect clipRect(rect.x,rect.y, | |
1296 | (selRect.x+selRect.width)-rect.x,rect.height); | |
1297 | dc.SetClippingRegion(clipRect); | |
1298 | } | |
1299 | #else | |
1300 | // Save the library size a bit for platforms that re-implement this. | |
1301 | void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const | |
1302 | { | |
1303 | } | |
1304 | #endif | |
1305 | ||
1306 | void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int paintBg ) | |
1307 | { | |
1308 | int drawState = m_btnState; | |
1309 | ||
1310 | #ifdef __WXGTK__ | |
1311 | if ( GetPopupWindowState() >= Animating ) | |
1312 | drawState |= wxCONTROL_PRESSED; | |
1313 | #endif | |
1314 | ||
1315 | wxRect drawRect(rect.x+m_btnSpacingX, | |
1316 | rect.y+((rect.height-m_btnSize.y)/2), | |
1317 | m_btnSize.x, | |
1318 | m_btnSize.y); | |
1319 | ||
1320 | // Make sure area is not larger than the control | |
1321 | if ( drawRect.y < rect.y ) | |
1322 | drawRect.y = rect.y; | |
1323 | if ( drawRect.height > rect.height ) | |
1324 | drawRect.height = rect.height; | |
1325 | ||
1326 | bool enabled = IsEnabled(); | |
1327 | ||
1328 | if ( !enabled ) | |
1329 | drawState |= wxCONTROL_DISABLED; | |
1330 | ||
1331 | if ( !m_bmpNormal.Ok() ) | |
1332 | { | |
1333 | // Need to clear button background even if m_btn is present | |
1334 | if ( paintBg ) | |
1335 | { | |
1336 | wxColour bgCol; | |
1337 | ||
1338 | if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE ) | |
1339 | bgCol = GetParent()->GetBackgroundColour(); | |
1340 | else | |
1341 | bgCol = GetBackgroundColour(); | |
1342 | ||
1343 | dc.SetBrush(bgCol); | |
1344 | dc.SetPen(bgCol); | |
1345 | dc.DrawRectangle(rect); | |
1346 | } | |
1347 | ||
1348 | // Draw standard button | |
1349 | wxRendererNative::Get().DrawComboBoxDropButton(this, | |
1350 | dc, | |
1351 | drawRect, | |
1352 | drawState); | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | // Draw bitmap | |
1357 | ||
1358 | wxBitmap* pBmp; | |
1359 | ||
1360 | if ( !enabled ) | |
1361 | pBmp = &m_bmpDisabled; | |
1362 | else if ( m_btnState & wxCONTROL_PRESSED ) | |
1363 | pBmp = &m_bmpPressed; | |
1364 | else if ( m_btnState & wxCONTROL_CURRENT ) | |
1365 | pBmp = &m_bmpHover; | |
1366 | else | |
1367 | pBmp = &m_bmpNormal; | |
1368 | ||
1369 | if ( m_blankButtonBg ) | |
1370 | { | |
1371 | // If using blank button background, we need to clear its background | |
1372 | // with button face colour instead of colour for rest of the control. | |
1373 | if ( paintBg ) | |
1374 | { | |
1375 | wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
1376 | //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
1377 | dc.SetPen(bgCol); | |
1378 | dc.SetBrush(bgCol); | |
1379 | dc.DrawRectangle(rect); | |
1380 | } | |
1381 | ||
1382 | wxRendererNative::Get().DrawPushButton(this, | |
1383 | dc, | |
1384 | drawRect, | |
1385 | drawState); | |
1386 | ||
1387 | } | |
1388 | else | |
1389 | ||
1390 | { | |
1391 | // Need to clear button background even if m_btn is present | |
1392 | // (assume non-button background was cleared just before this call so brushes are good) | |
1393 | if ( paintBg ) | |
1394 | dc.DrawRectangle(rect); | |
1395 | } | |
1396 | ||
1397 | // Draw bitmap centered in drawRect | |
1398 | dc.DrawBitmap(*pBmp, | |
1399 | drawRect.x + (drawRect.width-pBmp->GetWidth())/2, | |
1400 | drawRect.y + (drawRect.height-pBmp->GetHeight())/2, | |
1401 | true); | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | void wxComboCtrlBase::RecalcAndRefresh() | |
1406 | { | |
1407 | if ( IsCreated() ) | |
1408 | { | |
1409 | wxSizeEvent evt(GetSize(),GetId()); | |
1410 | GetEventHandler()->ProcessEvent(evt); | |
1411 | Refresh(); | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | // ---------------------------------------------------------------------------- | |
1416 | // miscellaneous event handlers | |
1417 | // ---------------------------------------------------------------------------- | |
1418 | ||
1419 | void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event) | |
1420 | { | |
1421 | if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED ) | |
1422 | { | |
1423 | if ( m_ignoreEvtText > 0 ) | |
1424 | { | |
1425 | m_ignoreEvtText--; | |
1426 | return; | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | // Change event id, object and string before relaying it forward | |
1431 | event.SetId(GetId()); | |
1432 | wxString s = event.GetString(); | |
1433 | event.SetEventObject(this); | |
1434 | event.SetString(s); | |
1435 | event.Skip(); | |
1436 | } | |
1437 | ||
1438 | // call if cursor is on button area or mouse is captured for the button | |
1439 | bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event, | |
1440 | int flags ) | |
1441 | { | |
1442 | int type = event.GetEventType(); | |
1443 | ||
1444 | if ( type == wxEVT_MOTION ) | |
1445 | { | |
1446 | if ( flags & wxCC_MF_ON_BUTTON ) | |
1447 | { | |
1448 | if ( !(m_btnState & wxCONTROL_CURRENT) ) | |
1449 | { | |
1450 | // Mouse hover begins | |
1451 | m_btnState |= wxCONTROL_CURRENT; | |
1452 | if ( HasCapture() ) // Retain pressed state. | |
1453 | m_btnState |= wxCONTROL_PRESSED; | |
1454 | Refresh(); | |
1455 | } | |
1456 | } | |
1457 | else if ( (m_btnState & wxCONTROL_CURRENT) ) | |
1458 | { | |
1459 | // Mouse hover ends | |
1460 | m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED); | |
1461 | Refresh(); | |
1462 | } | |
1463 | } | |
1464 | else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK ) | |
1465 | { | |
1466 | if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) ) | |
1467 | { | |
1468 | m_btnState |= wxCONTROL_PRESSED; | |
1469 | Refresh(); | |
1470 | ||
1471 | if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) ) | |
1472 | OnButtonClick(); | |
1473 | else | |
1474 | // If showing popup now, do not capture mouse or there will be interference | |
1475 | CaptureMouse(); | |
1476 | } | |
1477 | } | |
1478 | else if ( type == wxEVT_LEFT_UP ) | |
1479 | { | |
1480 | ||
1481 | // Only accept event if mouse was left-press was previously accepted | |
1482 | if ( HasCapture() ) | |
1483 | ReleaseMouse(); | |
1484 | ||
1485 | if ( m_btnState & wxCONTROL_PRESSED ) | |
1486 | { | |
1487 | // If mouse was inside, fire the click event. | |
1488 | if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP ) | |
1489 | { | |
1490 | if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) ) | |
1491 | OnButtonClick(); | |
1492 | } | |
1493 | ||
1494 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1495 | Refresh(); | |
1496 | } | |
1497 | } | |
1498 | else if ( type == wxEVT_LEAVE_WINDOW ) | |
1499 | { | |
1500 | if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) ) | |
1501 | { | |
1502 | m_btnState &= ~(wxCONTROL_CURRENT); | |
1503 | ||
1504 | // Mouse hover ends | |
1505 | if ( IsPopupWindowState(Hidden) ) | |
1506 | { | |
1507 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1508 | Refresh(); | |
1509 | } | |
1510 | } | |
1511 | } | |
1512 | else | |
1513 | return false; | |
1514 | ||
1515 | return true; | |
1516 | } | |
1517 | ||
1518 | // returns true if event was consumed or filtered | |
1519 | bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event, | |
1520 | int WXUNUSED(flags) ) | |
1521 | { | |
1522 | wxLongLong t = ::wxGetLocalTimeMillis(); | |
1523 | int evtType = event.GetEventType(); | |
1524 | ||
1525 | #if USES_WXPOPUPWINDOW || USES_WXDIALOG | |
1526 | if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW ) | |
1527 | { | |
1528 | if ( IsPopupWindowState(Visible) && | |
1529 | ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) ) | |
1530 | { | |
1531 | HidePopup(); | |
1532 | return true; | |
1533 | } | |
1534 | } | |
1535 | #endif | |
1536 | ||
1537 | // Filter out clicks on button immediately after popup dismiss (Windows like behaviour) | |
1538 | if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick ) | |
1539 | { | |
1540 | event.SetEventType(0); | |
1541 | return true; | |
1542 | } | |
1543 | ||
1544 | return false; | |
1545 | } | |
1546 | ||
1547 | void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event ) | |
1548 | { | |
1549 | int evtType = event.GetEventType(); | |
1550 | ||
1551 | if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) && | |
1552 | (m_windowStyle & wxCB_READONLY) ) | |
1553 | { | |
1554 | if ( GetPopupWindowState() >= Animating ) | |
1555 | { | |
1556 | #if USES_WXPOPUPWINDOW | |
1557 | // Click here always hides the popup. | |
1558 | if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW ) | |
1559 | HidePopup(); | |
1560 | #endif | |
1561 | } | |
1562 | else | |
1563 | { | |
1564 | if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) ) | |
1565 | { | |
1566 | // In read-only mode, clicking the text is the | |
1567 | // same as clicking the button. | |
1568 | OnButtonClick(); | |
1569 | } | |
1570 | else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK ) | |
1571 | { | |
1572 | //if ( m_popupInterface->CycleValue() ) | |
1573 | // Refresh(); | |
1574 | if ( m_popupInterface ) | |
1575 | m_popupInterface->OnComboDoubleClick(); | |
1576 | } | |
1577 | } | |
1578 | } | |
1579 | else | |
1580 | if ( IsPopupShown() ) | |
1581 | { | |
1582 | // relay (some) mouse events to the popup | |
1583 | if ( evtType == wxEVT_MOUSEWHEEL ) | |
1584 | m_popup->AddPendingEvent(event); | |
1585 | } | |
1586 | else if ( evtType ) | |
1587 | event.Skip(); | |
1588 | } | |
1589 | ||
1590 | void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event) | |
1591 | { | |
1592 | if ( IsPopupShown() ) | |
1593 | { | |
1594 | // pass it to the popped up control | |
1595 | GetPopupControl()->GetControl()->AddPendingEvent(event); | |
1596 | } | |
1597 | else // no popup | |
1598 | { | |
1599 | int keycode = event.GetKeyCode(); | |
1600 | ||
1601 | if ( keycode == WXK_TAB ) | |
1602 | { | |
1603 | wxNavigationKeyEvent evt; | |
1604 | ||
1605 | wxWindow* mainCtrl = GetMainWindowOfCompositeControl(); | |
1606 | ||
1607 | evt.SetFlags(wxNavigationKeyEvent::FromTab| | |
1608 | (!event.ShiftDown() ? wxNavigationKeyEvent::IsForward | |
1609 | : wxNavigationKeyEvent::IsBackward)); | |
1610 | evt.SetEventObject(mainCtrl); | |
1611 | evt.SetCurrentFocus(mainCtrl); | |
1612 | mainCtrl->GetParent()->GetEventHandler()->AddPendingEvent(evt); | |
1613 | return; | |
1614 | } | |
1615 | ||
1616 | if ( IsKeyPopupToggle(event) ) | |
1617 | { | |
1618 | OnButtonClick(); | |
1619 | return; | |
1620 | } | |
1621 | ||
1622 | int comboStyle = GetWindowStyle(); | |
1623 | wxComboPopup* popupInterface = GetPopupControl(); | |
1624 | ||
1625 | if ( !popupInterface ) | |
1626 | { | |
1627 | event.Skip(); | |
1628 | return; | |
1629 | } | |
1630 | ||
1631 | if ( (comboStyle & wxCB_READONLY) || | |
1632 | (keycode != WXK_RIGHT && keycode != WXK_LEFT) ) | |
1633 | { | |
1634 | popupInterface->OnComboKeyEvent(event); | |
1635 | } | |
1636 | else | |
1637 | event.Skip(); | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event ) | |
1642 | { | |
1643 | if ( event.GetEventType() == wxEVT_SET_FOCUS ) | |
1644 | { | |
1645 | wxWindow* tc = GetTextCtrl(); | |
1646 | if ( tc && tc != DoFindFocus() ) | |
1647 | #ifdef __WXMAC__ | |
1648 | m_resetFocus = true; | |
1649 | #else | |
1650 | tc->SetFocus(); | |
1651 | #endif | |
1652 | } | |
1653 | ||
1654 | Refresh(); | |
1655 | } | |
1656 | ||
1657 | void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) ) | |
1658 | { | |
1659 | if ( m_resetFocus ) | |
1660 | { | |
1661 | m_resetFocus = false; | |
1662 | wxWindow* tc = GetTextCtrl(); | |
1663 | if ( tc ) | |
1664 | tc->SetFocus(); | |
1665 | } | |
1666 | } | |
1667 | ||
1668 | void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
1669 | { | |
1670 | OnThemeChange(); | |
1671 | // indentation may also have changed | |
1672 | if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) ) | |
1673 | m_absIndent = GetNativeTextIndent(); | |
1674 | RecalcAndRefresh(); | |
1675 | } | |
1676 | ||
1677 | // ---------------------------------------------------------------------------- | |
1678 | // popup handling | |
1679 | // ---------------------------------------------------------------------------- | |
1680 | ||
1681 | // Create popup window and the child control | |
1682 | void wxComboCtrlBase::CreatePopup() | |
1683 | { | |
1684 | wxComboPopup* popupInterface = m_popupInterface; | |
1685 | wxWindow* popup; | |
1686 | ||
1687 | if ( !m_winPopup ) | |
1688 | { | |
1689 | #ifdef wxComboPopupWindowBase2 | |
1690 | if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP ) | |
1691 | { | |
1692 | #if !USES_WXDIALOG | |
1693 | m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER ); | |
1694 | #else | |
1695 | m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString, | |
1696 | wxPoint(-21,-21), wxSize(20, 20), | |
1697 | wxNO_BORDER ); | |
1698 | #endif | |
1699 | m_popupWinType = SECONDARY_POPUP_TYPE; | |
1700 | } | |
1701 | else | |
1702 | #endif | |
1703 | { | |
1704 | m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER ); | |
1705 | m_popupWinType = PRIMARY_POPUP_TYPE; | |
1706 | } | |
1707 | m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this); | |
1708 | m_winPopup->PushEventHandler(m_popupWinEvtHandler); | |
1709 | } | |
1710 | ||
1711 | popupInterface->Create(m_winPopup); | |
1712 | m_popup = popup = popupInterface->GetControl(); | |
1713 | ||
1714 | m_popupExtraHandler = new wxComboPopupExtraEventHandler(this); | |
1715 | popup->PushEventHandler( m_popupExtraHandler ); | |
1716 | ||
1717 | // This may be helpful on some platforms | |
1718 | // (eg. it bypasses a wxGTK popupwindow bug where | |
1719 | // window is not initially hidden when it should be) | |
1720 | m_winPopup->Hide(); | |
1721 | ||
1722 | popupInterface->m_iFlags |= wxCP_IFLAG_CREATED; | |
1723 | } | |
1724 | ||
1725 | // Destroy popup window and the child control | |
1726 | void wxComboCtrlBase::DestroyPopup() | |
1727 | { | |
1728 | HidePopup(); | |
1729 | ||
1730 | if ( m_popup ) | |
1731 | m_popup->RemoveEventHandler(m_popupExtraHandler); | |
1732 | ||
1733 | delete m_popupExtraHandler; | |
1734 | ||
1735 | delete m_popupInterface; | |
1736 | ||
1737 | if ( m_winPopup ) | |
1738 | { | |
1739 | m_winPopup->RemoveEventHandler(m_popupWinEvtHandler); | |
1740 | delete m_popupWinEvtHandler; | |
1741 | m_popupWinEvtHandler = NULL; | |
1742 | m_winPopup->Destroy(); | |
1743 | } | |
1744 | ||
1745 | m_popupExtraHandler = (wxEvtHandler*) NULL; | |
1746 | m_popupInterface = (wxComboPopup*) NULL; | |
1747 | m_winPopup = (wxWindow*) NULL; | |
1748 | m_popup = (wxWindow*) NULL; | |
1749 | } | |
1750 | ||
1751 | void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface) | |
1752 | { | |
1753 | wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") ); | |
1754 | ||
1755 | DestroyPopup(); | |
1756 | ||
1757 | iface->InitBase(this); | |
1758 | iface->Init(); | |
1759 | ||
1760 | m_popupInterface = iface; | |
1761 | ||
1762 | if ( !iface->LazyCreate() ) | |
1763 | { | |
1764 | CreatePopup(); | |
1765 | } | |
1766 | else | |
1767 | { | |
1768 | m_popup = (wxWindow*) NULL; | |
1769 | } | |
1770 | ||
1771 | // This must be done after creation | |
1772 | if ( m_valueString.length() ) | |
1773 | { | |
1774 | iface->SetStringValue(m_valueString); | |
1775 | //Refresh(); | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | // Ensures there is atleast the default popup | |
1780 | void wxComboCtrlBase::EnsurePopupControl() | |
1781 | { | |
1782 | if ( !m_popupInterface ) | |
1783 | SetPopupControl(NULL); | |
1784 | } | |
1785 | ||
1786 | void wxComboCtrlBase::OnButtonClick() | |
1787 | { | |
1788 | // Derived classes can override this method for totally custom | |
1789 | // popup action | |
1790 | ShowPopup(); | |
1791 | } | |
1792 | ||
1793 | void wxComboCtrlBase::ShowPopup() | |
1794 | { | |
1795 | EnsurePopupControl(); | |
1796 | wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") ); | |
1797 | ||
1798 | if ( IsPopupWindowState(Animating) ) | |
1799 | return; | |
1800 | ||
1801 | SetFocus(); | |
1802 | ||
1803 | // Space above and below | |
1804 | int screenHeight; | |
1805 | wxPoint scrPos; | |
1806 | int spaceAbove; | |
1807 | int spaceBelow; | |
1808 | int maxHeightPopup; | |
1809 | wxSize ctrlSz = GetSize(); | |
1810 | ||
1811 | screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ); | |
1812 | scrPos = GetParent()->ClientToScreen(GetPosition()); | |
1813 | ||
1814 | spaceAbove = scrPos.y; | |
1815 | spaceBelow = screenHeight - spaceAbove - ctrlSz.y; | |
1816 | ||
1817 | maxHeightPopup = spaceBelow; | |
1818 | if ( spaceAbove > spaceBelow ) | |
1819 | maxHeightPopup = spaceAbove; | |
1820 | ||
1821 | // Width | |
1822 | int widthPopup = ctrlSz.x + m_extLeft + m_extRight; | |
1823 | ||
1824 | if ( widthPopup < m_widthMinPopup ) | |
1825 | widthPopup = m_widthMinPopup; | |
1826 | ||
1827 | wxWindow* winPopup = m_winPopup; | |
1828 | wxWindow* popup; | |
1829 | ||
1830 | // Need to disable tab traversal of parent | |
1831 | // | |
1832 | // NB: This is to fix a bug in wxMSW. In theory it could also be fixed | |
1833 | // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage | |
1834 | // that if transient popup is open, then tab traversal is to be ignored. | |
1835 | // However, I think this code would still be needed for cases where | |
1836 | // transient popup doesn't work yet (wxWinCE?). | |
1837 | wxWindow* parent = GetParent(); | |
1838 | int parentFlags = parent->GetWindowStyle(); | |
1839 | if ( parentFlags & wxTAB_TRAVERSAL ) | |
1840 | { | |
1841 | parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) ); | |
1842 | m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL; | |
1843 | } | |
1844 | ||
1845 | if ( !winPopup ) | |
1846 | { | |
1847 | CreatePopup(); | |
1848 | winPopup = m_winPopup; | |
1849 | popup = m_popup; | |
1850 | } | |
1851 | else | |
1852 | { | |
1853 | popup = m_popup; | |
1854 | } | |
1855 | ||
1856 | winPopup->Enable(); | |
1857 | ||
1858 | wxASSERT( !m_popup || m_popup == popup ); // Consistency check. | |
1859 | ||
1860 | wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup, | |
1861 | m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup, | |
1862 | maxHeightPopup); | |
1863 | ||
1864 | popup->SetSize(adjustedSize); | |
1865 | popup->Move(0,0); | |
1866 | m_popupInterface->OnPopup(); | |
1867 | ||
1868 | // | |
1869 | // Reposition and resize popup window | |
1870 | // | |
1871 | ||
1872 | wxSize szp = popup->GetSize(); | |
1873 | ||
1874 | int popupX; | |
1875 | int popupY = scrPos.y + ctrlSz.y; | |
1876 | ||
1877 | // Default anchor is wxLEFT | |
1878 | int anchorSide = m_anchorSide; | |
1879 | if ( !anchorSide ) | |
1880 | anchorSide = wxLEFT; | |
1881 | ||
1882 | int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x; | |
1883 | int leftX = scrPos.x - m_extLeft; | |
1884 | int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ); | |
1885 | ||
1886 | // If there is not enough horizontal space, anchor on the other side. | |
1887 | // If there is no space even then, place the popup at x 0. | |
1888 | if ( anchorSide == wxRIGHT ) | |
1889 | { | |
1890 | if ( rightX < 0 ) | |
1891 | { | |
1892 | if ( (leftX+szp.x) < screenWidth ) | |
1893 | anchorSide = wxLEFT; | |
1894 | else | |
1895 | anchorSide = 0; | |
1896 | } | |
1897 | } | |
1898 | else | |
1899 | { | |
1900 | if ( (leftX+szp.x) >= screenWidth ) | |
1901 | { | |
1902 | if ( rightX >= 0 ) | |
1903 | anchorSide = wxRIGHT; | |
1904 | else | |
1905 | anchorSide = 0; | |
1906 | } | |
1907 | } | |
1908 | ||
1909 | // Select x coordinate according to the anchor side | |
1910 | if ( anchorSide == wxRIGHT ) | |
1911 | popupX = rightX; | |
1912 | else if ( anchorSide == wxLEFT ) | |
1913 | popupX = leftX; | |
1914 | else | |
1915 | popupX = 0; | |
1916 | ||
1917 | int showFlags = CanDeferShow; | |
1918 | ||
1919 | if ( spaceBelow < szp.y ) | |
1920 | { | |
1921 | popupY = scrPos.y - szp.y; | |
1922 | showFlags |= ShowAbove; | |
1923 | } | |
1924 | ||
1925 | #if INSTALL_TOPLEV_HANDLER | |
1926 | // Put top level window event handler into place | |
1927 | if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW ) | |
1928 | { | |
1929 | if ( !m_toplevEvtHandler ) | |
1930 | m_toplevEvtHandler = new wxComboFrameEventHandler(this); | |
1931 | ||
1932 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
1933 | wxASSERT( toplev ); | |
1934 | ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup(); | |
1935 | toplev->PushEventHandler( m_toplevEvtHandler ); | |
1936 | } | |
1937 | #endif | |
1938 | ||
1939 | // Set string selection (must be this way instead of SetStringSelection) | |
1940 | if ( m_text ) | |
1941 | { | |
1942 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
1943 | m_text->SelectAll(); | |
1944 | ||
1945 | m_popupInterface->SetStringValue( m_text->GetValue() ); | |
1946 | } | |
1947 | else | |
1948 | { | |
1949 | // This is neede since focus/selection indication may change when popup is shown | |
1950 | Refresh(); | |
1951 | } | |
1952 | ||
1953 | // This must be after SetStringValue | |
1954 | m_popupWinState = Animating; | |
1955 | ||
1956 | wxRect popupWinRect( popupX, popupY, szp.x, szp.y ); | |
1957 | ||
1958 | m_popup = popup; | |
1959 | if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) || | |
1960 | AnimateShow( popupWinRect, showFlags ) ) | |
1961 | { | |
1962 | DoShowPopup( popupWinRect, showFlags ); | |
1963 | } | |
1964 | } | |
1965 | ||
1966 | bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) ) | |
1967 | { | |
1968 | return true; | |
1969 | } | |
1970 | ||
1971 | void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) ) | |
1972 | { | |
1973 | wxWindow* winPopup = m_winPopup; | |
1974 | ||
1975 | if ( IsPopupWindowState(Animating) ) | |
1976 | { | |
1977 | // Make sure the popup window is shown in the right position. | |
1978 | // Should not matter even if animation already did this. | |
1979 | ||
1980 | // Some platforms (GTK) may like SetSize and Move to be separate | |
1981 | // (though the bug was probably fixed). | |
1982 | winPopup->SetSize( rect ); | |
1983 | ||
1984 | winPopup->Show(); | |
1985 | ||
1986 | m_popupWinState = Visible; | |
1987 | } | |
1988 | else if ( IsPopupWindowState(Hidden) ) | |
1989 | { | |
1990 | // Animation was aborted | |
1991 | ||
1992 | wxASSERT( !winPopup->IsShown() ); | |
1993 | ||
1994 | m_popupWinState = Hidden; | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | void wxComboCtrlBase::OnPopupDismiss() | |
1999 | { | |
2000 | // Just in case, avoid double dismiss | |
2001 | if ( IsPopupWindowState(Hidden) ) | |
2002 | return; | |
2003 | ||
2004 | // This must be set before focus - otherwise there will be recursive | |
2005 | // OnPopupDismisses. | |
2006 | m_popupWinState = Hidden; | |
2007 | ||
2008 | //SetFocus(); | |
2009 | m_winPopup->Disable(); | |
2010 | ||
2011 | // Inform popup control itself | |
2012 | m_popupInterface->OnDismiss(); | |
2013 | ||
2014 | if ( m_popupExtraHandler ) | |
2015 | ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss(); | |
2016 | ||
2017 | #if INSTALL_TOPLEV_HANDLER | |
2018 | // Remove top level window event handler | |
2019 | if ( m_toplevEvtHandler ) | |
2020 | { | |
2021 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
2022 | if ( toplev ) | |
2023 | toplev->RemoveEventHandler( m_toplevEvtHandler ); | |
2024 | } | |
2025 | #endif | |
2026 | ||
2027 | m_timeCanAcceptClick = ::wxGetLocalTimeMillis(); | |
2028 | ||
2029 | if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW ) | |
2030 | m_timeCanAcceptClick += 150; | |
2031 | ||
2032 | // If cursor not on dropdown button, then clear its state | |
2033 | // (technically not required by all ports, but do it for all just in case) | |
2034 | if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) ) | |
2035 | m_btnState = 0; | |
2036 | ||
2037 | // Return parent's tab traversal flag. | |
2038 | // See ShowPopup for notes. | |
2039 | if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL ) | |
2040 | { | |
2041 | wxWindow* parent = GetParent(); | |
2042 | parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL ); | |
2043 | m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL); | |
2044 | } | |
2045 | ||
2046 | // refresh control (necessary even if m_text) | |
2047 | Refresh(); | |
2048 | ||
2049 | SetFocus(); | |
2050 | } | |
2051 | ||
2052 | void wxComboCtrlBase::HidePopup() | |
2053 | { | |
2054 | // Should be able to call this without popup interface | |
2055 | if ( IsPopupWindowState(Hidden) ) | |
2056 | return; | |
2057 | ||
2058 | // transfer value and show it in textctrl, if any | |
2059 | if ( !IsPopupWindowState(Animating) ) | |
2060 | SetValue( m_popupInterface->GetStringValue() ); | |
2061 | ||
2062 | m_winPopup->Hide(); | |
2063 | ||
2064 | OnPopupDismiss(); | |
2065 | } | |
2066 | ||
2067 | // ---------------------------------------------------------------------------- | |
2068 | // customization methods | |
2069 | // ---------------------------------------------------------------------------- | |
2070 | ||
2071 | void wxComboCtrlBase::SetButtonPosition( int width, int height, | |
2072 | int side, int spacingX ) | |
2073 | { | |
2074 | m_btnWid = width; | |
2075 | m_btnHei = height; | |
2076 | m_btnSide = side; | |
2077 | m_btnSpacingX = spacingX; | |
2078 | ||
2079 | RecalcAndRefresh(); | |
2080 | } | |
2081 | ||
2082 | wxSize wxComboCtrlBase::GetButtonSize() | |
2083 | { | |
2084 | if ( m_btnSize.x > 0 ) | |
2085 | return m_btnSize; | |
2086 | ||
2087 | wxSize retSize(m_btnWid,m_btnHei); | |
2088 | ||
2089 | // Need to call CalculateAreas now if button size is | |
2090 | // is not explicitly specified. | |
2091 | if ( retSize.x <= 0 || retSize.y <= 0) | |
2092 | { | |
2093 | OnResize(); | |
2094 | ||
2095 | retSize = m_btnSize; | |
2096 | } | |
2097 | ||
2098 | return retSize; | |
2099 | } | |
2100 | ||
2101 | void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal, | |
2102 | bool blankButtonBg, | |
2103 | const wxBitmap& bmpPressed, | |
2104 | const wxBitmap& bmpHover, | |
2105 | const wxBitmap& bmpDisabled ) | |
2106 | { | |
2107 | m_bmpNormal = bmpNormal; | |
2108 | m_blankButtonBg = blankButtonBg; | |
2109 | ||
2110 | if ( bmpPressed.Ok() ) | |
2111 | m_bmpPressed = bmpPressed; | |
2112 | else | |
2113 | m_bmpPressed = bmpNormal; | |
2114 | ||
2115 | if ( bmpHover.Ok() ) | |
2116 | m_bmpHover = bmpHover; | |
2117 | else | |
2118 | m_bmpHover = bmpNormal; | |
2119 | ||
2120 | if ( bmpDisabled.Ok() ) | |
2121 | m_bmpDisabled = bmpDisabled; | |
2122 | else | |
2123 | m_bmpDisabled = bmpNormal; | |
2124 | ||
2125 | RecalcAndRefresh(); | |
2126 | } | |
2127 | ||
2128 | void wxComboCtrlBase::SetCustomPaintWidth( int width ) | |
2129 | { | |
2130 | if ( m_text ) | |
2131 | { | |
2132 | // move textctrl accordingly | |
2133 | wxRect r = m_text->GetRect(); | |
2134 | int inc = width - m_widthCustomPaint; | |
2135 | r.x += inc; | |
2136 | r.width -= inc; | |
2137 | m_text->SetSize( r ); | |
2138 | } | |
2139 | ||
2140 | m_widthCustomPaint = width; | |
2141 | ||
2142 | RecalcAndRefresh(); | |
2143 | } | |
2144 | ||
2145 | void wxComboCtrlBase::SetTextIndent( int indent ) | |
2146 | { | |
2147 | if ( indent < 0 ) | |
2148 | { | |
2149 | m_absIndent = GetNativeTextIndent(); | |
2150 | m_iFlags &= ~(wxCC_IFLAG_INDENT_SET); | |
2151 | } | |
2152 | else | |
2153 | { | |
2154 | m_absIndent = indent; | |
2155 | m_iFlags |= wxCC_IFLAG_INDENT_SET; | |
2156 | } | |
2157 | ||
2158 | RecalcAndRefresh(); | |
2159 | } | |
2160 | ||
2161 | wxCoord wxComboCtrlBase::GetNativeTextIndent() const | |
2162 | { | |
2163 | return DEFAULT_TEXT_INDENT; | |
2164 | } | |
2165 | ||
2166 | // ---------------------------------------------------------------------------- | |
2167 | // methods forwarded to wxTextCtrl | |
2168 | // ---------------------------------------------------------------------------- | |
2169 | ||
2170 | wxString wxComboCtrlBase::GetValue() const | |
2171 | { | |
2172 | if ( m_text ) | |
2173 | return m_text->GetValue(); | |
2174 | return m_valueString; | |
2175 | } | |
2176 | ||
2177 | void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent) | |
2178 | { | |
2179 | if ( m_text ) | |
2180 | { | |
2181 | if ( !withEvent ) | |
2182 | m_ignoreEvtText++; | |
2183 | ||
2184 | m_text->SetValue(value); | |
2185 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
2186 | m_text->SelectAll(); | |
2187 | } | |
2188 | ||
2189 | m_valueString = value; | |
2190 | ||
2191 | Refresh(); | |
2192 | ||
2193 | // Since wxComboPopup may want to paint the combo as well, we need | |
2194 | // to set the string value here (as well as sometimes in ShowPopup). | |
2195 | if ( m_valueString != value && m_popupInterface ) | |
2196 | { | |
2197 | m_popupInterface->SetStringValue(value); | |
2198 | } | |
2199 | } | |
2200 | ||
2201 | void wxComboCtrlBase::SetValue(const wxString& value) | |
2202 | { | |
2203 | SetValueWithEvent(value, false); | |
2204 | } | |
2205 | ||
2206 | // In this SetValue variant wxComboPopup::SetStringValue is not called | |
2207 | void wxComboCtrlBase::SetText(const wxString& value) | |
2208 | { | |
2209 | // Unlike in SetValue(), this must be called here or | |
2210 | // the behaviour will no be consistent in readonlys. | |
2211 | EnsurePopupControl(); | |
2212 | ||
2213 | m_valueString = value; | |
2214 | ||
2215 | if ( m_text ) | |
2216 | { | |
2217 | m_ignoreEvtText++; | |
2218 | m_text->SetValue( value ); | |
2219 | } | |
2220 | ||
2221 | Refresh(); | |
2222 | } | |
2223 | ||
2224 | void wxComboCtrlBase::Copy() | |
2225 | { | |
2226 | if ( m_text ) | |
2227 | m_text->Copy(); | |
2228 | } | |
2229 | ||
2230 | void wxComboCtrlBase::Cut() | |
2231 | { | |
2232 | if ( m_text ) | |
2233 | m_text->Cut(); | |
2234 | } | |
2235 | ||
2236 | void wxComboCtrlBase::Paste() | |
2237 | { | |
2238 | if ( m_text ) | |
2239 | m_text->Paste(); | |
2240 | } | |
2241 | ||
2242 | void wxComboCtrlBase::SetInsertionPoint(long pos) | |
2243 | { | |
2244 | if ( m_text ) | |
2245 | m_text->SetInsertionPoint(pos); | |
2246 | } | |
2247 | ||
2248 | void wxComboCtrlBase::SetInsertionPointEnd() | |
2249 | { | |
2250 | if ( m_text ) | |
2251 | m_text->SetInsertionPointEnd(); | |
2252 | } | |
2253 | ||
2254 | long wxComboCtrlBase::GetInsertionPoint() const | |
2255 | { | |
2256 | if ( m_text ) | |
2257 | return m_text->GetInsertionPoint(); | |
2258 | ||
2259 | return 0; | |
2260 | } | |
2261 | ||
2262 | long wxComboCtrlBase::GetLastPosition() const | |
2263 | { | |
2264 | if ( m_text ) | |
2265 | return m_text->GetLastPosition(); | |
2266 | ||
2267 | return 0; | |
2268 | } | |
2269 | ||
2270 | void wxComboCtrlBase::Replace(long from, long to, const wxString& value) | |
2271 | { | |
2272 | if ( m_text ) | |
2273 | m_text->Replace(from, to, value); | |
2274 | } | |
2275 | ||
2276 | void wxComboCtrlBase::Remove(long from, long to) | |
2277 | { | |
2278 | if ( m_text ) | |
2279 | m_text->Remove(from, to); | |
2280 | } | |
2281 | ||
2282 | void wxComboCtrlBase::SetSelection(long from, long to) | |
2283 | { | |
2284 | if ( m_text ) | |
2285 | m_text->SetSelection(from, to); | |
2286 | } | |
2287 | ||
2288 | void wxComboCtrlBase::Undo() | |
2289 | { | |
2290 | if ( m_text ) | |
2291 | m_text->Undo(); | |
2292 | } | |
2293 | ||
2294 | #endif // wxUSE_COMBOCTRL |