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