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