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