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