Return after activating already opened document in wxDocManager.
[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_textCtrlStyle = 0;
909 m_timeCanAcceptClick = 0;
910
911 m_resetFocus = false;
912 }
913
914 bool wxComboCtrlBase::Create(wxWindow *parent,
915 wxWindowID id,
916 const wxString& value,
917 const wxPoint& pos,
918 const wxSize& size,
919 long style,
920 const wxValidator& validator,
921 const wxString& name)
922 {
923 if ( !wxControl::Create(parent,
924 id,
925 pos,
926 size,
927 style | wxWANTS_CHARS,
928 validator,
929 name) )
930 return false;
931
932 m_valueString = value;
933
934 // Get colours
935 OnThemeChange();
936 m_marginLeft = GetNativeTextIndent();
937
938 m_iFlags |= wxCC_IFLAG_CREATED;
939
940 // If x and y indicate valid size, wxSizeEvent won't be
941 // emitted automatically, so we need to add artifical one.
942 if ( size.x > 0 && size.y > 0 )
943 {
944 wxSizeEvent evt(size,GetId());
945 GetEventHandler()->AddPendingEvent(evt);
946 }
947
948 return true;
949 }
950
951 void wxComboCtrlBase::InstallInputHandlers()
952 {
953 if ( m_text )
954 {
955 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
956 m_text->PushEventHandler(m_textEvtHandler);
957 }
958 }
959
960 void
961 wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator)
962 {
963 if ( !(m_windowStyle & wxCB_READONLY) )
964 {
965 if ( m_text )
966 m_text->Destroy();
967
968 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
969 // not used by the wxPropertyGrid and therefore the tab is processed by
970 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
971 // navigation event is then sent to the wrong window.
972 style |= wxTE_PROCESS_TAB | m_textCtrlStyle;
973
974 if ( HasFlag(wxTE_PROCESS_ENTER) )
975 style |= wxTE_PROCESS_ENTER;
976
977 // Ignore EVT_TEXT generated by the constructor (but only
978 // if the event redirector already exists)
979 // NB: This must be " = 1" instead of "++";
980 if ( m_textEvtHandler )
981 m_ignoreEvtText = 1;
982 else
983 m_ignoreEvtText = 0;
984
985 m_text = new wxComboCtrlTextCtrl();
986 m_text->Create(this, wxID_ANY, m_valueString,
987 wxDefaultPosition, wxSize(10,-1),
988 style, validator);
989 m_text->SetHint(m_hintText);
990 }
991 }
992
993 void wxComboCtrlBase::OnThemeChange()
994 {
995 // Leave the default bg on the Mac so the area used by the focus ring will
996 // be the correct colour and themed brush. Instead we'll use
997 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
998 #ifndef __WXMAC__
999 #if defined(__WXMSW__) || defined(__WXGTK__)
1000 wxVisualAttributes vattrs = wxComboBox::GetClassDefaultAttributes();
1001 #else
1002 wxVisualAttributes vattrs;
1003 vattrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1004 vattrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1005 #endif
1006
1007 // Only change the colours if application has not specified
1008 // custom ones.
1009 if ( !m_hasFgCol )
1010 {
1011 SetOwnForegroundColour(vattrs.colFg);
1012 m_hasFgCol = false;
1013 }
1014 if ( !m_hasBgCol )
1015 {
1016 SetOwnBackgroundColour(vattrs.colBg);
1017 m_hasBgCol = false;
1018 }
1019 #endif // !__WXMAC__
1020 }
1021
1022 wxComboCtrlBase::~wxComboCtrlBase()
1023 {
1024 if ( HasCapture() )
1025 ReleaseMouse();
1026
1027 #if INSTALL_TOPLEV_HANDLER
1028 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
1029 m_toplevEvtHandler = NULL;
1030 #endif
1031
1032 DestroyPopup();
1033
1034 if ( m_text )
1035 m_text->RemoveEventHandler(m_textEvtHandler);
1036
1037 delete m_textEvtHandler;
1038 }
1039
1040
1041 // ----------------------------------------------------------------------------
1042 // geometry stuff
1043 // ----------------------------------------------------------------------------
1044
1045 // Recalculates button and textctrl areas
1046 void wxComboCtrlBase::CalculateAreas( int btnWidth )
1047 {
1048 wxSize sz = GetClientSize();
1049 int customBorder = m_widthCustomBorder;
1050 int btnBorder; // border for button only
1051
1052 // check if button should really be outside the border: we'll do it it if
1053 // its platform default or bitmap+pushbutton background is used, but not if
1054 // there is vertical size adjustment or horizontal spacing.
1055 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
1056 (m_bmpNormal.Ok() && m_blankButtonBg) ) &&
1057 m_btnSpacingX == 0 &&
1058 m_btnHei <= 0 )
1059 {
1060 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
1061 btnBorder = 0;
1062 }
1063 else if ( (m_iFlags & wxCC_BUTTON_COVERS_BORDER) &&
1064 m_btnSpacingX == 0 && !m_bmpNormal.Ok() )
1065 {
1066 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1067 btnBorder = 0;
1068 }
1069 else
1070 {
1071 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1072 btnBorder = customBorder;
1073 }
1074
1075 // Defaul indentation
1076 if ( m_marginLeft < 0 )
1077 m_marginLeft = GetNativeTextIndent();
1078
1079 int butWidth = btnWidth;
1080
1081 if ( butWidth <= 0 )
1082 butWidth = m_btnWidDefault;
1083 else
1084 m_btnWidDefault = butWidth;
1085
1086 if ( butWidth <= 0 )
1087 return;
1088
1089 int butHeight = sz.y - btnBorder*2;
1090
1091 // Adjust button width
1092 if ( m_btnWid > 0 )
1093 butWidth = m_btnWid;
1094 else
1095 {
1096 // Adjust button width to match aspect ratio
1097 // (but only if control is smaller than best size).
1098 int bestHeight = GetBestSize().y;
1099 int height = GetSize().y;
1100
1101 if ( height < bestHeight )
1102 {
1103 // Make very small buttons square, as it makes
1104 // them accommodate arrow image better and still
1105 // looks decent.
1106 if ( height > 18 )
1107 butWidth = (height*butWidth)/bestHeight;
1108 else
1109 butWidth = butHeight;
1110 }
1111 }
1112
1113 // Adjust button height
1114 if ( m_btnHei > 0 )
1115 butHeight = m_btnHei;
1116
1117 // Use size of normal bitmap if...
1118 // It is larger
1119 // OR
1120 // button width is set to default and blank button bg is not drawn
1121 if ( m_bmpNormal.Ok() )
1122 {
1123 int bmpReqWidth = m_bmpNormal.GetWidth();
1124 int bmpReqHeight = m_bmpNormal.GetHeight();
1125
1126 // If drawing blank button background, we need to add some margin.
1127 if ( m_blankButtonBg )
1128 {
1129 bmpReqWidth += BMP_BUTTON_MARGIN*2;
1130 bmpReqHeight += BMP_BUTTON_MARGIN*2;
1131 }
1132
1133 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
1134 butWidth = bmpReqWidth;
1135 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
1136 butHeight = bmpReqHeight;
1137
1138 // Need to fix height?
1139 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
1140 {
1141 int newY = butHeight+(customBorder*2);
1142 SetClientSize(wxDefaultCoord,newY);
1143 if ( m_bmpNormal.Ok() || m_btnArea.width != butWidth || m_btnArea.height != butHeight )
1144 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1145 else
1146 m_iFlags &= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1147
1148 sz.y = newY;
1149 }
1150 }
1151
1152 int butAreaWid = butWidth + (m_btnSpacingX*2);
1153
1154 m_btnSize.x = butWidth;
1155 m_btnSize.y = butHeight;
1156
1157 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
1158 m_btnArea.y = btnBorder + FOCUS_RING;
1159 m_btnArea.width = butAreaWid;
1160 m_btnArea.height = sz.y - ((btnBorder+FOCUS_RING)*2);
1161
1162 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder + FOCUS_RING;
1163 m_tcArea.y = customBorder + FOCUS_RING;
1164 m_tcArea.width = sz.x - butAreaWid - (customBorder*2) - (FOCUS_RING*2);
1165 m_tcArea.height = sz.y - ((customBorder+FOCUS_RING)*2);
1166
1167 /*
1168 if ( m_text )
1169 {
1170 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1171 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1172 }
1173 */
1174 }
1175
1176 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
1177 {
1178 if ( !m_text )
1179 return;
1180
1181 wxSize sz = GetClientSize();
1182
1183 int customBorder = m_widthCustomBorder;
1184 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
1185 {
1186 int x;
1187
1188 if ( !m_widthCustomPaint )
1189 {
1190 // No special custom paint area - we can use 0 left margin
1191 // with wxTextCtrl.
1192 if ( m_text->SetMargins(0) )
1193 textCtrlXAdjust = 0;
1194 x = m_tcArea.x + m_marginLeft + textCtrlXAdjust;
1195 }
1196 else
1197 {
1198 // There is special custom paint area - it is better to
1199 // use some margin with the wxTextCtrl.
1200 m_text->SetMargins(m_marginLeft);
1201 x = m_tcArea.x + m_widthCustomPaint +
1202 m_marginLeft + textCtrlXAdjust;
1203 }
1204
1205 // Centre textctrl vertically, if needed
1206 #if !TEXTCTRL_TEXT_CENTERED
1207 int tcSizeY = m_text->GetBestSize().y;
1208 int diff0 = sz.y - tcSizeY;
1209 int y = textCtrlYAdjust + (diff0/2);
1210 #else
1211 wxUnusedVar(textCtrlYAdjust);
1212 int y = 0;
1213 #endif
1214
1215 if ( y < customBorder )
1216 y = customBorder;
1217
1218 m_text->SetSize(x,
1219 y,
1220 m_tcArea.width - m_tcArea.x - x,
1221 -1 );
1222
1223 // Make sure textctrl doesn't exceed the bottom custom border
1224 wxSize tsz = m_text->GetSize();
1225 int diff1 = (y + tsz.y) - (sz.y - customBorder);
1226 if ( diff1 >= 0 )
1227 {
1228 tsz.y = tsz.y - diff1 - 1;
1229 m_text->SetSize(tsz);
1230 }
1231 }
1232 else
1233 {
1234 // If it has border, have textctrl fill the entire text field.
1235 m_text->SetSize( m_tcArea.x + m_widthCustomPaint,
1236 m_tcArea.y,
1237 m_tcArea.width - m_widthCustomPaint,
1238 m_tcArea.height );
1239 }
1240 }
1241
1242 wxSize wxComboCtrlBase::DoGetBestSize() const
1243 {
1244 wxSize sizeText(150,0);
1245
1246 if ( m_text )
1247 sizeText = m_text->GetBestSize();
1248
1249 // TODO: Better method to calculate close-to-native control height.
1250
1251 int fhei;
1252 if ( m_font.Ok() )
1253 fhei = (m_font.GetPointSize()*2) + 5;
1254 else if ( wxNORMAL_FONT->Ok() )
1255 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
1256 else
1257 fhei = sizeText.y + 4;
1258
1259 // Need to force height to accomodate bitmap?
1260 int btnSizeY = m_btnSize.y;
1261 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
1262 fhei = btnSizeY;
1263
1264 // Control height doesn't depend on border
1265 /*
1266 // Add border
1267 int border = m_windowStyle & wxBORDER_MASK;
1268 if ( border == wxSIMPLE_BORDER )
1269 fhei += 2;
1270 else if ( border == wxNO_BORDER )
1271 fhei += (m_widthCustomBorder*2);
1272 else
1273 // Sunken etc.
1274 fhei += 4;
1275 */
1276
1277 // Final adjustments
1278 #ifdef __WXGTK__
1279 fhei += 1;
1280 #endif
1281
1282 #ifdef __WXMAC__
1283 // these are the numbers from the HIG:
1284 switch ( m_windowVariant )
1285 {
1286 case wxWINDOW_VARIANT_NORMAL:
1287 default :
1288 fhei = 22;
1289 break;
1290 case wxWINDOW_VARIANT_SMALL:
1291 fhei = 19;
1292 break;
1293 case wxWINDOW_VARIANT_MINI:
1294 fhei = 15;
1295 break;
1296 }
1297 #endif
1298
1299 fhei += 2 * FOCUS_RING;
1300 int width = sizeText.x + FOCUS_RING + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH;
1301
1302 wxSize ret(width, fhei);
1303 CacheBestSize(ret);
1304 return ret;
1305 }
1306
1307 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
1308 {
1309 if ( !IsCreated() )
1310 return;
1311
1312 // defined by actual wxComboCtrls
1313 OnResize();
1314
1315 event.Skip();
1316 }
1317
1318 // ----------------------------------------------------------------------------
1319 // standard operations
1320 // ----------------------------------------------------------------------------
1321
1322 bool wxComboCtrlBase::Enable(bool enable)
1323 {
1324 if ( !wxControl::Enable(enable) )
1325 return false;
1326
1327 if ( m_btn )
1328 m_btn->Enable(enable);
1329 if ( m_text )
1330 m_text->Enable(enable);
1331
1332 Refresh();
1333
1334 return true;
1335 }
1336
1337 bool wxComboCtrlBase::Show(bool show)
1338 {
1339 if ( !wxControl::Show(show) )
1340 return false;
1341
1342 if (m_btn)
1343 m_btn->Show(show);
1344
1345 if (m_text)
1346 m_text->Show(show);
1347
1348 return true;
1349 }
1350
1351 bool wxComboCtrlBase::SetFont ( const wxFont& font )
1352 {
1353 if ( !wxControl::SetFont(font) )
1354 return false;
1355
1356 if ( m_text )
1357 {
1358 // Without hiding the wxTextCtrl there would be some
1359 // visible 'flicker' (at least on Windows XP).
1360 m_text->Hide();
1361 m_text->SetFont(font);
1362 OnResize();
1363 m_text->Show();
1364 }
1365
1366 return true;
1367 }
1368
1369 #if wxUSE_TOOLTIPS
1370 void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
1371 {
1372 wxControl::DoSetToolTip(tooltip);
1373
1374 // Set tool tip for button and text box
1375 if ( tooltip )
1376 {
1377 const wxString &tip = tooltip->GetTip();
1378 if ( m_text ) m_text->SetToolTip(tip);
1379 if ( m_btn ) m_btn->SetToolTip(tip);
1380 }
1381 else
1382 {
1383 if ( m_text ) m_text->SetToolTip( NULL );
1384 if ( m_btn ) m_btn->SetToolTip( NULL );
1385 }
1386 }
1387 #endif // wxUSE_TOOLTIPS
1388
1389 #if wxUSE_VALIDATORS
1390 void wxComboCtrlBase::SetValidator(const wxValidator& validator)
1391 {
1392 wxTextCtrl* textCtrl = GetTextCtrl();
1393
1394 if ( textCtrl )
1395 textCtrl->SetValidator( validator );
1396 else
1397 wxControl::SetValidator( validator );
1398 }
1399
1400 wxValidator* wxComboCtrlBase::GetValidator()
1401 {
1402 wxTextCtrl* textCtrl = GetTextCtrl();
1403
1404 return textCtrl ? textCtrl->GetValidator() : wxControl::GetValidator();
1405 }
1406 #endif // wxUSE_VALIDATORS
1407
1408 bool wxComboCtrlBase::SetForegroundColour(const wxColour& colour)
1409 {
1410 if ( wxControl::SetForegroundColour(colour) )
1411 {
1412 if ( m_text )
1413 m_text->SetForegroundColour(colour);
1414 return true;
1415 }
1416 return false;
1417 }
1418
1419 bool wxComboCtrlBase::SetBackgroundColour(const wxColour& colour)
1420 {
1421 if ( wxControl::SetBackgroundColour(colour) )
1422 {
1423 if ( m_text )
1424 m_text->SetBackgroundColour(colour);
1425 return true;
1426 }
1427 return false;
1428 }
1429 // ----------------------------------------------------------------------------
1430 // painting
1431 // ----------------------------------------------------------------------------
1432
1433 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1434 // prepare combo box background on area in a way typical on platform
1435 void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
1436 {
1437 wxSize sz = GetClientSize();
1438 bool isEnabled;
1439 bool doDrawFocusRect; // also selected
1440
1441 // For smaller size control (and for disabled background) use less spacing
1442 int focusSpacingX;
1443 int focusSpacingY;
1444
1445 if ( !(flags & wxCONTROL_ISSUBMENU) )
1446 {
1447 // Drawing control
1448 isEnabled = IsEnabled();
1449 doDrawFocusRect = ShouldDrawFocus() && !(m_iFlags & wxCC_FULL_BUTTON);
1450
1451 // Windows-style: for smaller size control (and for disabled background) use less spacing
1452 focusSpacingX = isEnabled ? 2 : 1;
1453 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1454 }
1455 else
1456 {
1457 // Drawing a list item
1458 isEnabled = true; // they are never disabled
1459 doDrawFocusRect = (flags & wxCONTROL_SELECTED) != 0;
1460
1461 focusSpacingX = 0;
1462 focusSpacingY = 0;
1463 }
1464
1465 // Set the background sub-rectangle for selection, disabled etc
1466 wxRect selRect(rect);
1467 selRect.y += focusSpacingY;
1468 selRect.height -= (focusSpacingY*2);
1469
1470 int wcp = 0;
1471
1472 if ( !(flags & wxCONTROL_ISSUBMENU) )
1473 wcp += m_widthCustomPaint;
1474
1475 selRect.x += wcp + focusSpacingX;
1476 selRect.width -= wcp + (focusSpacingX*2);
1477
1478 wxColour bgCol;
1479 wxColour fgCol;
1480
1481 bool doDrawSelRect = true;
1482
1483 // Determine foreground colour
1484 if ( isEnabled )
1485 {
1486 if ( doDrawFocusRect )
1487 {
1488 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1489 }
1490 else if ( m_hasFgCol )
1491 {
1492 // Honour the custom foreground colour
1493 fgCol = GetForegroundColour();
1494 }
1495 else
1496 {
1497 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1498 }
1499 }
1500 else
1501 {
1502 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
1503 }
1504
1505 // Determine background colour
1506 if ( isEnabled )
1507 {
1508 if ( doDrawFocusRect )
1509 {
1510 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1511 }
1512 else if ( m_hasBgCol )
1513 {
1514 // Honour the custom background colour
1515 bgCol = GetBackgroundColour();
1516 }
1517 else
1518 {
1519 #ifndef __WXMAC__ // see note in OnThemeChange
1520 doDrawSelRect = false;
1521 bgCol = GetBackgroundColour();
1522 #else
1523 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1524 #endif
1525 }
1526 }
1527 else
1528 {
1529 #ifndef __WXMAC__ // see note in OnThemeChange
1530 bgCol = GetBackgroundColour();
1531 #else
1532 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1533 #endif
1534 }
1535
1536 dc.SetTextForeground( fgCol );
1537 dc.SetBrush( bgCol );
1538 if ( doDrawSelRect )
1539 {
1540 dc.SetPen( bgCol );
1541 dc.DrawRectangle( selRect );
1542 }
1543
1544 // Don't clip exactly to the selection rectangle so we can draw
1545 // to the non-selected area in front of it.
1546 wxRect clipRect(rect.x,rect.y,
1547 (selRect.x+selRect.width)-rect.x,rect.height);
1548 dc.SetClippingRegion(clipRect);
1549 }
1550 #else
1551 // Save the library size a bit for platforms that re-implement this.
1552 void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1553 {
1554 }
1555 #endif
1556
1557 void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags )
1558 {
1559 int drawState = m_btnState;
1560
1561 if ( (m_iFlags & wxCC_BUTTON_STAYS_DOWN) &&
1562 GetPopupWindowState() >= Animating )
1563 drawState |= wxCONTROL_PRESSED;
1564
1565 wxRect drawRect(rect.x+m_btnSpacingX,
1566 rect.y+((rect.height-m_btnSize.y)/2),
1567 m_btnSize.x,
1568 m_btnSize.y);
1569
1570 // Make sure area is not larger than the control
1571 if ( drawRect.y < rect.y )
1572 drawRect.y = rect.y;
1573 if ( drawRect.height > rect.height )
1574 drawRect.height = rect.height;
1575
1576 bool enabled = IsEnabled();
1577
1578 if ( !enabled )
1579 drawState |= wxCONTROL_DISABLED;
1580
1581 if ( !m_bmpNormal.Ok() )
1582 {
1583 if ( flags & Button_BitmapOnly )
1584 return;
1585
1586 // Need to clear button background even if m_btn is present
1587 if ( flags & Button_PaintBackground )
1588 {
1589 wxColour bgCol;
1590
1591 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1592 bgCol = GetParent()->GetBackgroundColour();
1593 else
1594 bgCol = GetBackgroundColour();
1595
1596 dc.SetBrush(bgCol);
1597 dc.SetPen(bgCol);
1598 dc.DrawRectangle(rect);
1599 }
1600
1601 // Draw standard button
1602 wxRendererNative::Get().DrawComboBoxDropButton(this,
1603 dc,
1604 drawRect,
1605 drawState);
1606 }
1607 else
1608 {
1609 // Draw bitmap
1610
1611 wxBitmap* pBmp;
1612
1613 if ( !enabled )
1614 pBmp = &m_bmpDisabled;
1615 else if ( m_btnState & wxCONTROL_PRESSED )
1616 pBmp = &m_bmpPressed;
1617 else if ( m_btnState & wxCONTROL_CURRENT )
1618 pBmp = &m_bmpHover;
1619 else
1620 pBmp = &m_bmpNormal;
1621
1622 if ( m_blankButtonBg )
1623 {
1624 // If using blank button background, we need to clear its background
1625 // with button face colour instead of colour for rest of the control.
1626 if ( flags & Button_PaintBackground )
1627 {
1628 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1629 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1630 dc.SetPen(bgCol);
1631 dc.SetBrush(bgCol);
1632 dc.DrawRectangle(rect);
1633 }
1634
1635 if ( !(flags & Button_BitmapOnly) )
1636 {
1637 wxRendererNative::Get().DrawPushButton(this,
1638 dc,
1639 drawRect,
1640 drawState);
1641 }
1642 }
1643 else
1644
1645 {
1646 // Need to clear button background even if m_btn is present
1647 // (assume non-button background was cleared just before this call so brushes are good)
1648 if ( flags & Button_PaintBackground )
1649 dc.DrawRectangle(rect);
1650 }
1651
1652 // Draw bitmap centered in drawRect
1653 dc.DrawBitmap(*pBmp,
1654 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1655 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1656 true);
1657 }
1658 }
1659
1660 void wxComboCtrlBase::RecalcAndRefresh()
1661 {
1662 if ( IsCreated() )
1663 {
1664 wxSizeEvent evt(GetSize(),GetId());
1665 GetEventHandler()->ProcessEvent(evt);
1666 Refresh();
1667 }
1668 }
1669
1670 // ----------------------------------------------------------------------------
1671 // miscellaneous event handlers
1672 // ----------------------------------------------------------------------------
1673
1674 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
1675 {
1676 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1677 {
1678 if ( m_ignoreEvtText > 0 )
1679 {
1680 m_ignoreEvtText--;
1681 return;
1682 }
1683 }
1684
1685 // Change event id, object and string before relaying it forward
1686 event.SetId(GetId());
1687 wxString s = event.GetString();
1688 event.SetEventObject(this);
1689 event.SetString(s);
1690 event.Skip();
1691 }
1692
1693 // call if cursor is on button area or mouse is captured for the button
1694 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
1695 int flags )
1696 {
1697 int type = event.GetEventType();
1698
1699 if ( type == wxEVT_MOTION )
1700 {
1701 if ( (flags & wxCC_MF_ON_BUTTON) &&
1702 IsPopupWindowState(Hidden) )
1703 {
1704 if ( !(m_btnState & wxCONTROL_CURRENT) )
1705 {
1706 // Mouse hover begins
1707 m_btnState |= wxCONTROL_CURRENT;
1708 if ( HasCapture() ) // Retain pressed state.
1709 m_btnState |= wxCONTROL_PRESSED;
1710 Refresh();
1711 }
1712 }
1713 else if ( (m_btnState & wxCONTROL_CURRENT) )
1714 {
1715 // Mouse hover ends
1716 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1717 Refresh();
1718 }
1719 }
1720 else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK )
1721 {
1722 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1723 {
1724 m_btnState |= wxCONTROL_PRESSED;
1725 Refresh();
1726
1727 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1728 OnButtonClick();
1729 else
1730 // If showing popup now, do not capture mouse or there will be interference
1731 CaptureMouse();
1732 }
1733 }
1734 else if ( type == wxEVT_LEFT_UP )
1735 {
1736
1737 // Only accept event if mouse was left-press was previously accepted
1738 if ( HasCapture() )
1739 ReleaseMouse();
1740
1741 if ( m_btnState & wxCONTROL_PRESSED )
1742 {
1743 // If mouse was inside, fire the click event.
1744 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1745 {
1746 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1747 OnButtonClick();
1748 }
1749
1750 m_btnState &= ~(wxCONTROL_PRESSED);
1751 Refresh();
1752 }
1753 }
1754 else if ( type == wxEVT_LEAVE_WINDOW )
1755 {
1756 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1757 {
1758 m_btnState &= ~(wxCONTROL_CURRENT);
1759
1760 // Mouse hover ends
1761 if ( IsPopupWindowState(Hidden) )
1762 {
1763 m_btnState &= ~(wxCONTROL_PRESSED);
1764 Refresh();
1765 }
1766 }
1767 }
1768 else
1769 return false;
1770
1771 // Never have 'hot' state when popup is being shown
1772 // (this is mostly needed because of the animation).
1773 if ( !IsPopupWindowState(Hidden) )
1774 m_btnState &= ~wxCONTROL_CURRENT;
1775
1776 return true;
1777 }
1778
1779 // returns true if event was consumed or filtered
1780 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
1781 int WXUNUSED(flags) )
1782 {
1783 wxLongLong t = ::wxGetLocalTimeMillis();
1784 int evtType = event.GetEventType();
1785
1786 #if USES_WXPOPUPWINDOW || USES_GENERICTLW
1787 if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW )
1788 {
1789 if ( IsPopupWindowState(Visible) &&
1790 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1791 {
1792 HidePopup(true);
1793 return true;
1794 }
1795 }
1796 #endif
1797
1798 // Filter out clicks on button immediately after popup dismiss
1799 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1800 {
1801 event.SetEventType(0);
1802 return true;
1803 }
1804
1805 return false;
1806 }
1807
1808 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
1809 {
1810 int evtType = event.GetEventType();
1811
1812 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1813 (m_windowStyle & wxCB_READONLY) )
1814 {
1815 if ( GetPopupWindowState() >= Animating )
1816 {
1817 #if USES_WXPOPUPWINDOW
1818 // Click here always hides the popup.
1819 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
1820 HidePopup(true);
1821 #endif
1822 }
1823 else
1824 {
1825 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1826 {
1827 // In read-only mode, clicking the text is the
1828 // same as clicking the button.
1829 OnButtonClick();
1830 }
1831 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1832 {
1833 //if ( m_popupInterface->CycleValue() )
1834 // Refresh();
1835 if ( m_popupInterface )
1836 m_popupInterface->OnComboDoubleClick();
1837 }
1838 }
1839 }
1840 else if ( evtType == wxEVT_MOUSEWHEEL )
1841 {
1842 if ( IsPopupShown() )
1843 {
1844 // relay (some) mouse events to the popup
1845 m_popup->GetEventHandler()->ProcessEvent(event);
1846 }
1847 else if ( event.GetWheelAxis() == 0 &&
1848 event.GetWheelRotation() != 0 &&
1849 event.GetModifiers() == 0 )
1850 {
1851 // Translate mousewheel actions into key up/down. This is
1852 // the simplest way of getting native behaviour: scrolling the
1853 // wheel moves selection up/down by one item.
1854 wxKeyEvent kevent(wxEVT_KEY_DOWN);
1855 kevent.m_keyCode = event.GetWheelRotation() > 0
1856 ? WXK_UP
1857 : WXK_DOWN;
1858 GetEventHandler()->ProcessEvent(kevent);
1859 }
1860 else
1861 {
1862 event.Skip();
1863 }
1864 }
1865 else if ( evtType )
1866 {
1867 event.Skip();
1868 }
1869 }
1870
1871 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
1872 {
1873 if ( IsPopupShown() )
1874 {
1875 // pass it to the popped up control
1876 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
1877 }
1878 else // no popup
1879 {
1880 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
1881
1882 if ( mainCtrl->GetParent()->HasFlag(wxTAB_TRAVERSAL) )
1883 {
1884 if ( mainCtrl->HandleAsNavigationKey(event) )
1885 return;
1886 }
1887
1888 if ( IsKeyPopupToggle(event) )
1889 {
1890 OnButtonClick();
1891 return;
1892 }
1893
1894 int comboStyle = GetWindowStyle();
1895 wxComboPopup* popupInterface = GetPopupControl();
1896
1897 if ( !popupInterface )
1898 {
1899 event.Skip();
1900 return;
1901 }
1902
1903 int keycode = event.GetKeyCode();
1904
1905 if ( (comboStyle & wxCB_READONLY) ||
1906 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
1907 {
1908 popupInterface->OnComboKeyEvent(event);
1909 }
1910 else
1911 event.Skip();
1912 }
1913 }
1914
1915 void wxComboCtrlBase::OnCharEvent(wxKeyEvent& event)
1916 {
1917 if ( IsPopupShown() )
1918 {
1919 // pass it to the popped up control
1920 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
1921 }
1922 else // no popup
1923 {
1924 wxComboPopup* popupInterface = GetPopupControl();
1925 if ( popupInterface )
1926 {
1927 popupInterface->OnComboCharEvent(event);
1928 }
1929 else
1930 {
1931 event.Skip();
1932 }
1933 }
1934 }
1935
1936 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1937 {
1938 if ( event.GetEventType() == wxEVT_SET_FOCUS )
1939 {
1940 wxWindow* tc = GetTextCtrl();
1941 if ( tc && tc != DoFindFocus() )
1942 {
1943 tc->SetFocus();
1944 }
1945 }
1946
1947 Refresh();
1948 }
1949
1950 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) )
1951 {
1952 if ( m_resetFocus )
1953 {
1954 m_resetFocus = false;
1955 wxWindow* tc = GetTextCtrl();
1956 if ( tc )
1957 tc->SetFocus();
1958 }
1959 }
1960
1961 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
1962 {
1963 OnThemeChange();
1964 // left margin may also have changed
1965 if ( !(m_iFlags & wxCC_IFLAG_LEFT_MARGIN_SET) )
1966 m_marginLeft = GetNativeTextIndent();
1967 RecalcAndRefresh();
1968 }
1969
1970 // ----------------------------------------------------------------------------
1971 // popup handling
1972 // ----------------------------------------------------------------------------
1973
1974 // Create popup window and the child control
1975 void wxComboCtrlBase::CreatePopup()
1976 {
1977 wxComboPopup* popupInterface = m_popupInterface;
1978 wxWindow* popup;
1979
1980 if ( !m_winPopup )
1981 {
1982 #ifdef wxComboPopupWindowBase2
1983 if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP )
1984 {
1985 #if !USES_GENERICTLW
1986 m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER );
1987 #else
1988 int tlwFlags = wxNO_BORDER;
1989 #ifdef wxCC_GENERIC_TLW_IS_FRAME
1990 tlwFlags |= wxFRAME_NO_TASKBAR;
1991 #endif
1992
1993 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
1994 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY,
1995 wxPoint(-21,-21), wxSize(20, 20),
1996 tlwFlags );
1997 #else
1998 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString,
1999 wxPoint(-21,-21), wxSize(20, 20),
2000 tlwFlags );
2001 #endif
2002 #endif
2003 m_popupWinType = SECONDARY_POPUP_TYPE;
2004 }
2005 else
2006 #endif // wxComboPopupWindowBase2
2007 {
2008 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
2009 m_popupWinType = PRIMARY_POPUP_TYPE;
2010 }
2011 m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this);
2012 m_winPopup->PushEventHandler(m_popupWinEvtHandler);
2013 }
2014
2015 popupInterface->Create(m_winPopup);
2016 m_popup = popup = popupInterface->GetControl();
2017
2018 m_popupEvtHandler = new wxComboPopupEvtHandler(this);
2019 popup->PushEventHandler( m_popupEvtHandler );
2020
2021 // This may be helpful on some platforms
2022 // (eg. it bypasses a wxGTK popupwindow bug where
2023 // window is not initially hidden when it should be)
2024 m_winPopup->Hide();
2025
2026 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
2027 }
2028
2029 // Destroy popup window and the child control
2030 void wxComboCtrlBase::DestroyPopup()
2031 {
2032 HidePopup(true);
2033
2034 if ( m_popup )
2035 m_popup->RemoveEventHandler(m_popupEvtHandler);
2036
2037 wxDELETE(m_popupEvtHandler);
2038
2039 wxDELETE(m_popupInterface);
2040
2041 if ( m_winPopup )
2042 {
2043 m_winPopup->RemoveEventHandler(m_popupWinEvtHandler);
2044 wxDELETE(m_popupWinEvtHandler);
2045 m_winPopup->Destroy();
2046 m_winPopup = NULL;
2047 }
2048
2049 m_popup = NULL;
2050 }
2051
2052 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
2053 {
2054 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
2055
2056 DestroyPopup();
2057
2058 iface->InitBase(this);
2059 iface->Init();
2060
2061 m_popupInterface = iface;
2062
2063 if ( !iface->LazyCreate() )
2064 {
2065 CreatePopup();
2066 }
2067 else
2068 {
2069 m_popup = NULL;
2070 }
2071
2072 // This must be done after creation
2073 if ( m_valueString.length() )
2074 {
2075 iface->SetStringValue(m_valueString);
2076 //Refresh();
2077 }
2078 }
2079
2080 // Ensures there is atleast the default popup
2081 void wxComboCtrlBase::EnsurePopupControl()
2082 {
2083 if ( !m_popupInterface )
2084 SetPopupControl(NULL);
2085 }
2086
2087 void wxComboCtrlBase::OnButtonClick()
2088 {
2089 // Derived classes can override this method for totally custom
2090 // popup action
2091 if ( !IsPopupWindowState(Visible) )
2092 {
2093 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId());
2094 event.SetEventObject(this);
2095 HandleWindowEvent(event);
2096
2097 ShowPopup();
2098 }
2099 else
2100 {
2101 HidePopup(true);
2102 }
2103 }
2104
2105 void wxComboCtrlBase::ShowPopup()
2106 {
2107 EnsurePopupControl();
2108 wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") );
2109
2110 if ( IsPopupWindowState(Animating) )
2111 return;
2112
2113 SetFocus();
2114
2115 // Space above and below
2116 int screenHeight;
2117 wxPoint scrPos;
2118 int spaceAbove;
2119 int spaceBelow;
2120 int maxHeightPopup;
2121 wxSize ctrlSz = GetSize();
2122
2123 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
2124 scrPos = GetParent()->ClientToScreen(GetPosition());
2125
2126 spaceAbove = scrPos.y;
2127 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
2128
2129 maxHeightPopup = spaceBelow;
2130 if ( spaceAbove > spaceBelow )
2131 maxHeightPopup = spaceAbove;
2132
2133 // Width
2134 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
2135
2136 if ( widthPopup < m_widthMinPopup )
2137 widthPopup = m_widthMinPopup;
2138
2139 wxWindow* winPopup = m_winPopup;
2140 wxWindow* popup;
2141
2142 // Need to disable tab traversal of parent
2143 //
2144 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
2145 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2146 // that if transient popup is open, then tab traversal is to be ignored.
2147 // However, I think this code would still be needed for cases where
2148 // transient popup doesn't work yet (wxWinCE?).
2149 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
2150 wxWindow* parent = mainCtrl->GetParent();
2151 int parentFlags = parent->GetWindowStyle();
2152 if ( parentFlags & wxTAB_TRAVERSAL )
2153 {
2154 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
2155 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
2156 }
2157
2158 if ( !winPopup )
2159 {
2160 CreatePopup();
2161 winPopup = m_winPopup;
2162 popup = m_popup;
2163 }
2164 else
2165 {
2166 popup = m_popup;
2167 }
2168
2169 winPopup->Enable();
2170
2171 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
2172
2173 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
2174 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
2175 maxHeightPopup);
2176
2177 popup->SetSize(adjustedSize);
2178 popup->Move(0,0);
2179 m_popupInterface->OnPopup();
2180
2181 //
2182 // Reposition and resize popup window
2183 //
2184
2185 wxSize szp = popup->GetSize();
2186
2187 int popupX;
2188 int popupY = scrPos.y + ctrlSz.y;
2189
2190 // Default anchor is wxLEFT
2191 int anchorSide = m_anchorSide;
2192 if ( !anchorSide )
2193 anchorSide = wxLEFT;
2194
2195 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
2196 int leftX = scrPos.x - m_extLeft;
2197
2198 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
2199 leftX -= ctrlSz.x;
2200
2201 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
2202
2203 // If there is not enough horizontal space, anchor on the other side.
2204 // If there is no space even then, place the popup at x 0.
2205 if ( anchorSide == wxRIGHT )
2206 {
2207 if ( rightX < 0 )
2208 {
2209 if ( (leftX+szp.x) < screenWidth )
2210 anchorSide = wxLEFT;
2211 else
2212 anchorSide = 0;
2213 }
2214 }
2215 else
2216 {
2217 if ( (leftX+szp.x) >= screenWidth )
2218 {
2219 if ( rightX >= 0 )
2220 anchorSide = wxRIGHT;
2221 else
2222 anchorSide = 0;
2223 }
2224 }
2225
2226 // Select x coordinate according to the anchor side
2227 if ( anchorSide == wxRIGHT )
2228 popupX = rightX;
2229 else if ( anchorSide == wxLEFT )
2230 popupX = leftX;
2231 else
2232 popupX = 0;
2233
2234 int showFlags = CanDeferShow;
2235
2236 if ( spaceBelow < szp.y )
2237 {
2238 popupY = scrPos.y - szp.y;
2239 showFlags |= ShowAbove;
2240 }
2241
2242 #if INSTALL_TOPLEV_HANDLER
2243 // Put top level window event handler into place
2244 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
2245 {
2246 if ( !m_toplevEvtHandler )
2247 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
2248
2249 wxWindow* toplev = ::wxGetTopLevelParent( this );
2250 wxASSERT( toplev );
2251 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
2252 toplev->PushEventHandler( m_toplevEvtHandler );
2253 }
2254 #endif
2255
2256 // Set string selection (must be this way instead of SetStringSelection)
2257 if ( m_text )
2258 {
2259 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2260 m_text->SelectAll();
2261
2262 m_popupInterface->SetStringValue( m_text->GetValue() );
2263 }
2264 else
2265 {
2266 // This is neede since focus/selection indication may change when popup is shown
2267 Refresh();
2268 }
2269
2270 // This must be after SetStringValue
2271 m_popupWinState = Animating;
2272
2273 wxRect popupWinRect( popupX, popupY, szp.x, szp.y );
2274
2275 m_popup = popup;
2276 if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) ||
2277 AnimateShow( popupWinRect, showFlags ) )
2278 {
2279 DoShowPopup( popupWinRect, showFlags );
2280 }
2281 }
2282
2283 bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) )
2284 {
2285 return true;
2286 }
2287
2288 void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) )
2289 {
2290 wxWindow* winPopup = m_winPopup;
2291
2292 if ( IsPopupWindowState(Animating) )
2293 {
2294 // Make sure the popup window is shown in the right position.
2295 // Should not matter even if animation already did this.
2296
2297 // Some platforms (GTK) may like SetSize and Move to be separate
2298 // (though the bug was probably fixed).
2299 winPopup->SetSize( rect );
2300
2301 #if USES_WXPOPUPTRANSIENTWINDOW
2302 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2303 ((wxPopupTransientWindow*)winPopup)->Popup(m_popup);
2304 else
2305 #endif
2306 winPopup->Show();
2307
2308 m_popupWinState = Visible;
2309
2310 // If popup window was a generic top-level window, or the
2311 // wxPopupWindow implemenation on this platform is classified as
2312 // perfect, then we should be able to safely set focus to the popup
2313 // control.
2314 if ( IsPopupWinTypePerfect(m_popupWinType) )
2315 m_popup->SetFocus();
2316 }
2317 else if ( IsPopupWindowState(Hidden) )
2318 {
2319 // Animation was aborted
2320
2321 wxASSERT( !winPopup->IsShown() );
2322
2323 m_popupWinState = Hidden;
2324 }
2325
2326 Refresh();
2327 }
2328
2329 void wxComboCtrlBase::OnPopupDismiss(bool generateEvent)
2330 {
2331 // Just in case, avoid double dismiss
2332 if ( IsPopupWindowState(Hidden) )
2333 return;
2334
2335 // This must be set before focus - otherwise there will be recursive
2336 // OnPopupDismisses.
2337 m_popupWinState = Hidden;
2338
2339 //SetFocus();
2340 m_winPopup->Disable();
2341
2342 // Inform popup control itself
2343 m_popupInterface->OnDismiss();
2344
2345 if ( m_popupEvtHandler )
2346 ((wxComboPopupEvtHandler*)m_popupEvtHandler)->OnPopupDismiss();
2347
2348 #if INSTALL_TOPLEV_HANDLER
2349 // Remove top level window event handler
2350 if ( m_toplevEvtHandler )
2351 {
2352 wxWindow* toplev = ::wxGetTopLevelParent( this );
2353 if ( toplev )
2354 toplev->RemoveEventHandler( m_toplevEvtHandler );
2355 }
2356 #endif
2357
2358 m_timeCanAcceptClick = ::wxGetLocalTimeMillis();
2359
2360 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2361 m_timeCanAcceptClick += 150;
2362
2363 // If cursor not on dropdown button, then clear its state
2364 // (technically not required by all ports, but do it for all just in case)
2365 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
2366 m_btnState = 0;
2367
2368 // Return parent's tab traversal flag.
2369 // See ShowPopup for notes.
2370 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
2371 {
2372 wxWindow* parent = GetParent();
2373 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
2374 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
2375 }
2376
2377 // refresh control (necessary even if m_text)
2378 Refresh();
2379
2380 SetFocus();
2381
2382 if ( generateEvent )
2383 {
2384 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId());
2385 event.SetEventObject(this);
2386 HandleWindowEvent(event);
2387 }
2388 }
2389
2390 void wxComboCtrlBase::HidePopup(bool generateEvent)
2391 {
2392 // Should be able to call this without popup interface
2393 if ( IsPopupWindowState(Hidden) )
2394 return;
2395
2396 // transfer value and show it in textctrl, if any
2397 if ( !IsPopupWindowState(Animating) )
2398 SetValue( m_popupInterface->GetStringValue() );
2399
2400 m_winPopup->Hide();
2401
2402 OnPopupDismiss(generateEvent);
2403 }
2404
2405 // ----------------------------------------------------------------------------
2406 // customization methods
2407 // ----------------------------------------------------------------------------
2408
2409 void wxComboCtrlBase::SetButtonPosition( int width, int height,
2410 int side, int spacingX )
2411 {
2412 m_btnWid = width;
2413 m_btnHei = height;
2414 m_btnSide = side;
2415 m_btnSpacingX = spacingX;
2416
2417 if ( width > 0 || height > 0 || spacingX )
2418 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
2419
2420 RecalcAndRefresh();
2421 }
2422
2423 wxSize wxComboCtrlBase::GetButtonSize()
2424 {
2425 if ( m_btnSize.x > 0 )
2426 return m_btnSize;
2427
2428 wxSize retSize(m_btnWid,m_btnHei);
2429
2430 // Need to call CalculateAreas now if button size is
2431 // is not explicitly specified.
2432 if ( retSize.x <= 0 || retSize.y <= 0)
2433 {
2434 OnResize();
2435
2436 retSize = m_btnSize;
2437 }
2438
2439 return retSize;
2440 }
2441
2442 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
2443 bool blankButtonBg,
2444 const wxBitmap& bmpPressed,
2445 const wxBitmap& bmpHover,
2446 const wxBitmap& bmpDisabled )
2447 {
2448 m_bmpNormal = bmpNormal;
2449 m_blankButtonBg = blankButtonBg;
2450
2451 if ( bmpPressed.Ok() )
2452 m_bmpPressed = bmpPressed;
2453 else
2454 m_bmpPressed = bmpNormal;
2455
2456 if ( bmpHover.Ok() )
2457 m_bmpHover = bmpHover;
2458 else
2459 m_bmpHover = bmpNormal;
2460
2461 if ( bmpDisabled.Ok() )
2462 m_bmpDisabled = bmpDisabled;
2463 else
2464 m_bmpDisabled = bmpNormal;
2465
2466 RecalcAndRefresh();
2467 }
2468
2469 void wxComboCtrlBase::SetCustomPaintWidth( int width )
2470 {
2471 if ( m_text )
2472 {
2473 // move textctrl accordingly
2474 wxRect r = m_text->GetRect();
2475 int inc = width - m_widthCustomPaint;
2476 r.x += inc;
2477 r.width -= inc;
2478 m_text->SetSize( r );
2479 }
2480
2481 m_widthCustomPaint = width;
2482
2483 RecalcAndRefresh();
2484 }
2485
2486 bool wxComboCtrlBase::DoSetMargins(const wxPoint& margins)
2487 {
2488 // For general sanity's sake, we ignore top margin. Instead
2489 // we will always try to center the text vertically.
2490 bool res = true;
2491
2492 if ( margins.x != -1 )
2493 {
2494 m_marginLeft = margins.x;
2495 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2496 }
2497 else
2498 {
2499 m_marginLeft = GetNativeTextIndent();
2500 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2501 }
2502
2503 if ( margins.y != -1 )
2504 {
2505 res = false;
2506 }
2507
2508 RecalcAndRefresh();
2509
2510 return res;
2511 }
2512
2513 wxPoint wxComboCtrlBase::DoGetMargins() const
2514 {
2515 return wxPoint(m_marginLeft, -1);
2516 }
2517
2518 #if WXWIN_COMPATIBILITY_2_8
2519 void wxComboCtrlBase::SetTextIndent( int indent )
2520 {
2521 if ( indent < 0 )
2522 {
2523 m_marginLeft = GetNativeTextIndent();
2524 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2525 }
2526 else
2527 {
2528 m_marginLeft = indent;
2529 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2530 }
2531
2532 RecalcAndRefresh();
2533 }
2534
2535 wxCoord wxComboCtrlBase::GetTextIndent() const
2536 {
2537 return m_marginLeft;
2538 }
2539 #endif
2540
2541 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
2542 {
2543 return DEFAULT_TEXT_INDENT;
2544 }
2545
2546 void wxComboCtrlBase::SetTextCtrlStyle( int style )
2547 {
2548 m_textCtrlStyle = style;
2549
2550 if ( m_text )
2551 m_text->SetWindowStyle(style);
2552 }
2553
2554 // ----------------------------------------------------------------------------
2555 // methods forwarded to wxTextCtrl
2556 // ----------------------------------------------------------------------------
2557
2558 wxString wxComboCtrlBase::GetValue() const
2559 {
2560 if ( m_text )
2561 return m_text->GetValue();
2562 return m_valueString;
2563 }
2564
2565 void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent)
2566 {
2567 if ( m_text )
2568 {
2569 if ( !withEvent )
2570 m_ignoreEvtText++;
2571
2572 m_text->SetValue(value);
2573
2574 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2575 m_text->SelectAll();
2576 }
2577
2578 // Since wxComboPopup may want to paint the combo as well, we need
2579 // to set the string value here (as well as sometimes in ShowPopup).
2580 if ( m_valueString != value )
2581 {
2582 m_valueString = value;
2583
2584 EnsurePopupControl();
2585
2586 if (m_popupInterface)
2587 m_popupInterface->SetStringValue(value);
2588 }
2589
2590 Refresh();
2591 }
2592
2593 void wxComboCtrlBase::SetValue(const wxString& value)
2594 {
2595 SetValueWithEvent(value, false);
2596 }
2597
2598 // In this SetValue variant wxComboPopup::SetStringValue is not called
2599 void wxComboCtrlBase::SetText(const wxString& value)
2600 {
2601 // Unlike in SetValue(), this must be called here or
2602 // the behaviour will no be consistent in readonlys.
2603 EnsurePopupControl();
2604
2605 m_valueString = value;
2606
2607 if ( m_text )
2608 {
2609 m_ignoreEvtText++;
2610 m_text->SetValue( value );
2611 }
2612
2613 Refresh();
2614 }
2615
2616 void wxComboCtrlBase::Copy()
2617 {
2618 if ( m_text )
2619 m_text->Copy();
2620 }
2621
2622 void wxComboCtrlBase::Cut()
2623 {
2624 if ( m_text )
2625 m_text->Cut();
2626 }
2627
2628 void wxComboCtrlBase::Paste()
2629 {
2630 if ( m_text )
2631 m_text->Paste();
2632 }
2633
2634 void wxComboCtrlBase::SetInsertionPoint(long pos)
2635 {
2636 if ( m_text )
2637 m_text->SetInsertionPoint(pos);
2638 }
2639
2640 void wxComboCtrlBase::SetInsertionPointEnd()
2641 {
2642 if ( m_text )
2643 m_text->SetInsertionPointEnd();
2644 }
2645
2646 long wxComboCtrlBase::GetInsertionPoint() const
2647 {
2648 if ( m_text )
2649 return m_text->GetInsertionPoint();
2650
2651 return 0;
2652 }
2653
2654 long wxComboCtrlBase::GetLastPosition() const
2655 {
2656 if ( m_text )
2657 return m_text->GetLastPosition();
2658
2659 return 0;
2660 }
2661
2662 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2663 {
2664 if ( m_text )
2665 m_text->Replace(from, to, value);
2666 }
2667
2668 void wxComboCtrlBase::Remove(long from, long to)
2669 {
2670 if ( m_text )
2671 m_text->Remove(from, to);
2672 }
2673
2674 void wxComboCtrlBase::SetSelection(long from, long to)
2675 {
2676 if ( m_text )
2677 m_text->SetSelection(from, to);
2678 }
2679
2680 void wxComboCtrlBase::Undo()
2681 {
2682 if ( m_text )
2683 m_text->Undo();
2684 }
2685
2686 bool wxComboCtrlBase::SetHint(const wxString& hint)
2687 {
2688 m_hintText = hint;
2689 bool res = true;
2690 if ( m_text )
2691 res = m_text->SetHint(hint);
2692 Refresh();
2693 return res;
2694 }
2695
2696 wxString wxComboCtrlBase::GetHint() const
2697 {
2698 return m_hintText;
2699 }
2700
2701 #endif // wxUSE_COMBOCTRL