]> git.saurik.com Git - wxWidgets.git/blob - src/common/combocmn.cpp
Commit wxComboCtrl clipping patch from J. Salli
[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/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/dialog.h"
35 #include "wx/timer.h"
36 #endif
37
38 #include "wx/dcbuffer.h"
39 #include "wx/tooltip.h"
40
41 #include "wx/combo.h"
42
43
44
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // Milliseconds to wait for two mouse-ups after focus inorder
49 // to trigger a double-click.
50 #define DOUBLE_CLICK_CONVERSION_TRESHOLD 500
51
52 #define DEFAULT_DROPBUTTON_WIDTH 19
53
54 #define BMP_BUTTON_MARGIN 4
55
56 #define DEFAULT_POPUP_HEIGHT 200
57
58 #define DEFAULT_TEXT_INDENT 3
59
60 #define COMBO_MARGIN 2 // spacing right of wxTextCtrl
61
62
63 #if defined(__WXMSW__)
64
65 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
66
67 //#undef wxUSE_POPUPWIN
68 //#define wxUSE_POPUPWIN 0
69
70 #elif defined(__WXGTK__)
71
72 #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
73
74 #elif defined(__WXMAC__)
75
76 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
77
78 #else
79
80 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
81
82 #endif
83
84
85 // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
86 // what the wxUSE_POPUPWIN says.
87 // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
88 #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__)
89 #undef wxUSE_POPUPWIN
90 #define wxUSE_POPUPWIN 0
91 #endif
92
93
94 #if wxUSE_POPUPWIN
95 #include "wx/popupwin.h"
96 #else
97 #undef USE_TRANSIENT_POPUP
98 #define USE_TRANSIENT_POPUP 0
99 #endif
100
101
102 #if USE_TRANSIENT_POPUP
103
104 #define wxComboPopupWindowBase wxPopupTransientWindow
105 #define INSTALL_TOPLEV_HANDLER 0
106
107 #elif wxUSE_POPUPWIN
108
109 #define wxComboPopupWindowBase wxPopupWindow
110 #define INSTALL_TOPLEV_HANDLER 1
111
112 #else
113
114 #define wxComboPopupWindowBase wxDialog
115 #define INSTALL_TOPLEV_HANDLER 0 // Doesn't need since can monitor active event
116
117 #endif
118
119
120
121 //
122 // ** TODO **
123 // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
124 //
125
126
127 // ----------------------------------------------------------------------------
128 // wxComboFrameEventHandler takes care of hiding the popup when events happen
129 // in its top level parent.
130 // ----------------------------------------------------------------------------
131
132 #if INSTALL_TOPLEV_HANDLER
133
134 //
135 // This will no longer be necessary after wxTransientPopupWindow
136 // works well on all platforms.
137 //
138
139 class wxComboFrameEventHandler : public wxEvtHandler
140 {
141 public:
142 wxComboFrameEventHandler( wxComboCtrlBase* pCb );
143 ~wxComboFrameEventHandler();
144
145 void OnPopup();
146
147 void OnIdle( wxIdleEvent& event );
148 void OnMouseEvent( wxMouseEvent& event );
149 void OnActivate( wxActivateEvent& event );
150 void OnResize( wxSizeEvent& event );
151 void OnMove( wxMoveEvent& event );
152 void OnMenuEvent( wxMenuEvent& event );
153 void OnClose( wxCloseEvent& event );
154
155 protected:
156 wxWindow* m_focusStart;
157 wxComboCtrlBase* m_combo;
158
159 private:
160 DECLARE_EVENT_TABLE()
161 };
162
163 BEGIN_EVENT_TABLE(wxComboFrameEventHandler, wxEvtHandler)
164 EVT_IDLE(wxComboFrameEventHandler::OnIdle)
165 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent)
166 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent)
167 EVT_SIZE(wxComboFrameEventHandler::OnResize)
168 EVT_MOVE(wxComboFrameEventHandler::OnMove)
169 EVT_MENU_HIGHLIGHT(wxID_ANY,wxComboFrameEventHandler::OnMenuEvent)
170 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent)
171 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate)
172 EVT_CLOSE(wxComboFrameEventHandler::OnClose)
173 END_EVENT_TABLE()
174
175 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo )
176 : wxEvtHandler()
177 {
178 m_combo = combo;
179 }
180
181 wxComboFrameEventHandler::~wxComboFrameEventHandler()
182 {
183 }
184
185 void wxComboFrameEventHandler::OnPopup()
186 {
187 m_focusStart = ::wxWindow::FindFocus();
188 }
189
190 void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event )
191 {
192 wxWindow* winFocused = ::wxWindow::FindFocus();
193
194 wxWindow* popup = m_combo->GetPopupControl()->GetControl();
195 wxWindow* winpopup = m_combo->GetPopupWindow();
196
197 if (
198 winFocused != m_focusStart &&
199 winFocused != popup &&
200 winFocused->GetParent() != popup &&
201 winFocused != winpopup &&
202 winFocused->GetParent() != winpopup &&
203 winFocused != m_combo &&
204 winFocused != m_combo->GetButton() // GTK (atleast) requires this
205 )
206 {
207 m_combo->HidePopup();
208 }
209
210 event.Skip();
211 }
212
213 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event )
214 {
215 m_combo->HidePopup();
216 event.Skip();
217 }
218
219 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event )
220 {
221 m_combo->HidePopup();
222 event.Skip();
223 }
224
225 void wxComboFrameEventHandler::OnClose( wxCloseEvent& event )
226 {
227 m_combo->HidePopup();
228 event.Skip();
229 }
230
231 void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event )
232 {
233 m_combo->HidePopup();
234 event.Skip();
235 }
236
237 void wxComboFrameEventHandler::OnResize( wxSizeEvent& event )
238 {
239 m_combo->HidePopup();
240 event.Skip();
241 }
242
243 void wxComboFrameEventHandler::OnMove( wxMoveEvent& event )
244 {
245 m_combo->HidePopup();
246 event.Skip();
247 }
248
249 #endif // INSTALL_TOPLEV_HANDLER
250
251 // ----------------------------------------------------------------------------
252 // wxComboPopupWindow is wxPopupWindow customized for
253 // wxComboCtrl.
254 // ----------------------------------------------------------------------------
255
256 class wxComboPopupWindow : public wxComboPopupWindowBase
257 {
258 public:
259
260 wxComboPopupWindow( wxComboCtrlBase *parent, int style = wxBORDER_NONE );
261
262 #if USE_TRANSIENT_POPUP
263 virtual bool ProcessLeftDown(wxMouseEvent& event);
264 #endif
265
266 void OnKeyEvent(wxKeyEvent& event);
267
268 void OnMouseEvent( wxMouseEvent& event );
269 #if !wxUSE_POPUPWIN
270 void OnActivate( wxActivateEvent& event );
271 #endif
272
273 protected:
274
275 #if USE_TRANSIENT_POPUP
276 virtual void OnDismiss();
277 #endif
278
279 private:
280 DECLARE_EVENT_TABLE()
281 };
282
283
284 BEGIN_EVENT_TABLE(wxComboPopupWindow, wxComboPopupWindowBase)
285 EVT_MOUSE_EVENTS(wxComboPopupWindow::OnMouseEvent)
286 #if !wxUSE_POPUPWIN
287 EVT_ACTIVATE(wxComboPopupWindow::OnActivate)
288 #endif
289 EVT_KEY_DOWN(wxComboPopupWindow::OnKeyEvent)
290 EVT_KEY_UP(wxComboPopupWindow::OnKeyEvent)
291 END_EVENT_TABLE()
292
293
294 wxComboPopupWindow::wxComboPopupWindow( wxComboCtrlBase *parent,
295 int style )
296 #if wxUSE_POPUPWIN
297 : wxComboPopupWindowBase(parent,style)
298 #else
299 : wxComboPopupWindowBase(parent,
300 wxID_ANY,
301 wxEmptyString,
302 wxPoint(-21,-21),
303 wxSize(20,20),
304 style)
305 #endif
306 {
307 }
308
309 void wxComboPopupWindow::OnKeyEvent( wxKeyEvent& event )
310 {
311 // Relay keyboard event to the main child controls
312 // (just skipping may just cause the popup to close)
313 wxWindowList children = GetChildren();
314 wxWindowList::iterator node = children.begin();
315 wxWindow* child = (wxWindow*)*node;
316 child->AddPendingEvent(event);
317 }
318
319 void wxComboPopupWindow::OnMouseEvent( wxMouseEvent& event )
320 {
321 event.Skip();
322 }
323
324 #if !wxUSE_POPUPWIN
325 void wxComboPopupWindow::OnActivate( wxActivateEvent& event )
326 {
327 if ( !event.GetActive() )
328 {
329 // Tell combo control that we are dismissed.
330 wxComboCtrl* combo = (wxComboCtrl*) GetParent();
331 wxASSERT( combo );
332 wxASSERT( combo->IsKindOf(CLASSINFO(wxComboCtrl)) );
333
334 combo->HidePopup();
335
336 event.Skip();
337 }
338 }
339 #endif
340
341 #if USE_TRANSIENT_POPUP
342 bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent& event )
343 {
344 return wxComboPopupWindowBase::ProcessLeftDown(event);
345 }
346 #endif
347
348 #if USE_TRANSIENT_POPUP
349 // First thing that happens when a transient popup closes is that this method gets called.
350 void wxComboPopupWindow::OnDismiss()
351 {
352 wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent();
353 wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxComboCtrlBase)),
354 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
355
356 combo->OnPopupDismiss();
357 }
358 #endif
359
360 // ----------------------------------------------------------------------------
361 // wxComboPopup
362 //
363 // ----------------------------------------------------------------------------
364
365 wxComboPopup::~wxComboPopup()
366 {
367 }
368
369 void wxComboPopup::OnPopup()
370 {
371 }
372
373 void wxComboPopup::OnDismiss()
374 {
375 }
376
377 wxSize wxComboPopup::GetAdjustedSize( int minWidth,
378 int prefHeight,
379 int WXUNUSED(maxHeight) )
380 {
381 return wxSize(minWidth,prefHeight);
382 }
383
384 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo,
385 wxDC& dc, const wxRect& rect )
386 {
387 if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl
388 {
389 combo->PrepareBackground(dc,rect,0);
390
391 dc.DrawText( combo->GetValue(),
392 rect.x + combo->GetTextIndent(),
393 (rect.height-dc.GetCharHeight())/2 + rect.y );
394 }
395 }
396
397 void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
398 {
399 DefaultPaintComboControl(m_combo,dc,rect);
400 }
401
402 void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
403 {
404 event.Skip();
405 }
406
407 void wxComboPopup::OnComboDoubleClick()
408 {
409 }
410
411 void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) )
412 {
413 }
414
415 bool wxComboPopup::LazyCreate()
416 {
417 return false;
418 }
419
420 void wxComboPopup::Dismiss()
421 {
422 m_combo->HidePopup();
423 }
424
425 // ----------------------------------------------------------------------------
426 // input handling
427 // ----------------------------------------------------------------------------
428
429 //
430 // This is pushed to the event handler queue of the child textctrl.
431 //
432 class wxComboBoxExtraInputHandler : public wxEvtHandler
433 {
434 public:
435
436 wxComboBoxExtraInputHandler( wxComboCtrlBase* combo )
437 : wxEvtHandler()
438 {
439 m_combo = combo;
440 }
441 ~wxComboBoxExtraInputHandler() { }
442 void OnKey(wxKeyEvent& event);
443 void OnFocus(wxFocusEvent& event);
444
445 protected:
446 wxComboCtrlBase* m_combo;
447
448 private:
449 DECLARE_EVENT_TABLE()
450 };
451
452
453 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler)
454 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey)
455 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
456 END_EVENT_TABLE()
457
458
459 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event)
460 {
461 // Let the wxComboCtrl event handler have a go first.
462 wxComboCtrlBase* combo = m_combo;
463 wxObject* prevObj = event.GetEventObject();
464
465 event.SetId(combo->GetId());
466 event.SetEventObject(combo);
467 combo->GetEventHandler()->ProcessEvent(event);
468
469 event.SetId(((wxWindow*)prevObj)->GetId());
470 event.SetEventObject(prevObj);
471 }
472
473 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event)
474 {
475 // FIXME: This code does run when control is clicked,
476 // yet on Windows it doesn't select all the text.
477 if ( !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) )
478 {
479 if ( m_combo->GetTextCtrl() )
480 m_combo->GetTextCtrl()->SelectAll();
481 else
482 m_combo->SetSelection(-1,-1);
483 }
484
485 // Send focus indication to parent.
486 // NB: This is needed for cases where the textctrl gets focus
487 // instead of its parent. While this may trigger multiple
488 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
489 // from combo's focus event handler), they should be quite
490 // harmless.
491 wxFocusEvent evt2(wxEVT_SET_FOCUS,m_combo->GetId());
492 evt2.SetEventObject(m_combo);
493 m_combo->GetEventHandler()->ProcessEvent(evt2);
494
495 event.Skip();
496 }
497
498
499 //
500 // This is pushed to the event handler queue of the control in popup.
501 //
502
503 class wxComboPopupExtraEventHandler : public wxEvtHandler
504 {
505 public:
506
507 wxComboPopupExtraEventHandler( wxComboCtrlBase* combo )
508 : wxEvtHandler()
509 {
510 m_combo = combo;
511 m_beenInside = false;
512 }
513 ~wxComboPopupExtraEventHandler() { }
514
515 void OnMouseEvent( wxMouseEvent& event );
516
517 // Called from wxComboCtrlBase::OnPopupDismiss
518 void OnPopupDismiss()
519 {
520 m_beenInside = false;
521 }
522
523 protected:
524 wxComboCtrlBase* m_combo;
525
526 bool m_beenInside;
527
528 private:
529 DECLARE_EVENT_TABLE()
530 };
531
532
533 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler)
534 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent)
535 END_EVENT_TABLE()
536
537
538 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event )
539 {
540 wxPoint pt = event.GetPosition();
541 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
542 int evtType = event.GetEventType();
543 bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y;
544
545 if ( evtType == wxEVT_MOTION ||
546 evtType == wxEVT_LEFT_DOWN ||
547 evtType == wxEVT_RIGHT_DOWN )
548 {
549 // Block motion and click events outside the popup
550 if ( !isInside )
551 {
552 event.Skip(false);
553 return;
554 }
555 }
556 else if ( evtType == wxEVT_LEFT_UP )
557 {
558 // Don't let left-down events in if outside
559 if ( evtType == wxEVT_LEFT_DOWN )
560 {
561 if ( !isInside )
562 return;
563 }
564
565 if ( !m_beenInside )
566 {
567 if ( isInside )
568 {
569 m_beenInside = true;
570 }
571 else
572 {
573 //
574 // Some mouse events to popup that happen outside it, before cursor
575 // has been inside the popu, need to be ignored by it but relayed to
576 // the dropbutton.
577 //
578 wxWindow* btn = m_combo->GetButton();
579 if ( btn )
580 btn->GetEventHandler()->AddPendingEvent(event);
581 else
582 m_combo->GetEventHandler()->AddPendingEvent(event);
583
584 return;
585 }
586
587 event.Skip();
588 }
589 }
590
591 event.Skip();
592 }
593
594 // ----------------------------------------------------------------------------
595 // wxComboCtrlBase
596 // ----------------------------------------------------------------------------
597
598
599 BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl)
600 EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
601 EVT_SIZE(wxComboCtrlBase::OnSizeEvent)
602 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent)
603 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent)
604 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
605 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent)
606 EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
607 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged)
608 END_EVENT_TABLE()
609
610
611 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl)
612
613 // Have global double buffer - should be enough for multiple combos
614 static wxBitmap* gs_doubleBuffer = (wxBitmap*) NULL;
615
616 void wxComboCtrlBase::Init()
617 {
618 m_winPopup = (wxWindow *)NULL;
619 m_popup = (wxWindow *)NULL;
620 m_isPopupShown = false;
621 m_btn = (wxWindow*) NULL;
622 m_text = (wxTextCtrl*) NULL;
623 m_popupInterface = (wxComboPopup*) NULL;
624
625 m_popupExtraHandler = (wxEvtHandler*) NULL;
626 m_textEvtHandler = (wxEvtHandler*) NULL;
627
628 #if INSTALL_TOPLEV_HANDLER
629 m_toplevEvtHandler = (wxEvtHandler*) NULL;
630 #endif
631
632 m_heightPopup = -1;
633 m_widthMinPopup = -1;
634 m_anchorSide = 0;
635 m_widthCustomPaint = 0;
636 m_widthCustomBorder = 0;
637
638 m_btnState = 0;
639 m_btnWidDefault = 0;
640 m_blankButtonBg = false;
641 m_btnWid = m_btnHei = -1;
642 m_btnSide = wxRIGHT;
643 m_btnSpacingX = 0;
644
645 m_extLeft = 0;
646 m_extRight = 0;
647 m_absIndent = -1;
648 m_iFlags = 0;
649 m_downReceived = false;
650 m_timeCanAcceptClick = 0;
651 }
652
653 bool wxComboCtrlBase::Create(wxWindow *parent,
654 wxWindowID id,
655 const wxString& value,
656 const wxPoint& pos,
657 const wxSize& size,
658 long style,
659 const wxValidator& validator,
660 const wxString& name)
661 {
662 if ( !wxControl::Create(parent,
663 id,
664 pos,
665 size,
666 style | wxWANTS_CHARS,
667 validator,
668 name) )
669 return false;
670
671 m_valueString = value;
672
673 // Get colours
674 OnThemeChange();
675 m_absIndent = GetNativeTextIndent();
676
677 m_iFlags |= wxCC_IFLAG_CREATED;
678
679 // If x and y indicate valid size, wxSizeEvent won't be
680 // emitted automatically, so we need to add artifical one.
681 if ( size.x > 0 && size.y > 0 )
682 {
683 wxSizeEvent evt(size,GetId());
684 GetEventHandler()->AddPendingEvent(evt);
685 }
686
687 return true;
688 }
689
690 void wxComboCtrlBase::InstallInputHandlers()
691 {
692 if ( m_text )
693 {
694 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
695 m_text->PushEventHandler(m_textEvtHandler);
696 }
697 }
698
699 void
700 wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator)
701 {
702 if ( !(m_windowStyle & wxCB_READONLY) )
703 {
704 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
705 // not used by the wxPropertyGrid and therefore the tab is processed by
706 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
707 // navigation event is then sent to the wrong window.
708 style |= wxTE_PROCESS_TAB;
709
710 if ( HasFlag(wxTE_PROCESS_ENTER) )
711 style |= wxTE_PROCESS_ENTER;
712
713 m_text = new wxTextCtrl(this, wxID_ANY, m_valueString,
714 wxDefaultPosition, wxDefaultSize,
715 style, validator);
716
717 // This is required for some platforms (GTK+ atleast)
718 m_text->SetSizeHints(2,4);
719 }
720 }
721
722 void wxComboCtrlBase::OnThemeChange()
723 {
724 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
725 }
726
727 wxComboCtrlBase::~wxComboCtrlBase()
728 {
729 if ( HasCapture() )
730 ReleaseMouse();
731
732 delete gs_doubleBuffer;
733 gs_doubleBuffer = (wxBitmap*) NULL;
734
735 #if INSTALL_TOPLEV_HANDLER
736 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
737 m_toplevEvtHandler = (wxEvtHandler*) NULL;
738 #endif
739
740 DestroyPopup();
741
742 if ( m_text )
743 m_text->RemoveEventHandler(m_textEvtHandler);
744
745 delete m_textEvtHandler;
746 }
747
748
749 // ----------------------------------------------------------------------------
750 // geometry stuff
751 // ----------------------------------------------------------------------------
752
753 // Recalculates button and textctrl areas
754 void wxComboCtrlBase::CalculateAreas( int btnWidth )
755 {
756 wxSize sz = GetClientSize();
757 int customBorder = m_widthCustomBorder;
758 int btnBorder; // border for button only
759
760 // check if button should really be outside the border: we'll do it it if
761 // its platform default or bitmap+pushbutton background is used, but not if
762 // there is vertical size adjustment or horizontal spacing.
763 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
764 (m_bmpNormal.Ok() && m_blankButtonBg) ) &&
765 m_btnSpacingX == 0 &&
766 m_btnHei <= 0 )
767 {
768 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
769 btnBorder = 0;
770 }
771 else
772 {
773 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
774 btnBorder = customBorder;
775 }
776
777 // Defaul indentation
778 if ( m_absIndent < 0 )
779 m_absIndent = GetNativeTextIndent();
780
781 int butWidth = btnWidth;
782
783 if ( butWidth <= 0 )
784 butWidth = m_btnWidDefault;
785 else
786 m_btnWidDefault = butWidth;
787
788 if ( butWidth <= 0 )
789 return;
790
791 int butHeight = sz.y - btnBorder*2;
792
793 // Adjust button width
794 if ( m_btnWid > 0 )
795 butWidth = m_btnWid;
796 else
797 {
798 // Adjust button width to match aspect ratio
799 // (but only if control is smaller than best size).
800 int bestHeight = GetBestSize().y;
801 int height = GetSize().y;
802
803 if ( height < bestHeight )
804 {
805 // Make very small buttons square, as it makes
806 // them accommodate arrow image better and still
807 // looks decent.
808 if ( height > 18 )
809 butWidth = (height*butWidth)/bestHeight;
810 else
811 butWidth = butHeight;
812 }
813 }
814
815 // Adjust button height
816 if ( m_btnHei > 0 )
817 butHeight = m_btnHei;
818
819 // Use size of normal bitmap if...
820 // It is larger
821 // OR
822 // button width is set to default and blank button bg is not drawn
823 if ( m_bmpNormal.Ok() )
824 {
825 int bmpReqWidth = m_bmpNormal.GetWidth();
826 int bmpReqHeight = m_bmpNormal.GetHeight();
827
828 // If drawing blank button background, we need to add some margin.
829 if ( m_blankButtonBg )
830 {
831 bmpReqWidth += BMP_BUTTON_MARGIN*2;
832 bmpReqHeight += BMP_BUTTON_MARGIN*2;
833 }
834
835 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
836 butWidth = bmpReqWidth;
837 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
838 butHeight = bmpReqHeight;
839
840 // Need to fix height?
841 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
842 {
843 int newY = butHeight+(customBorder*2);
844 SetClientSize(wxDefaultCoord,newY);
845 sz.y = newY;
846 }
847 }
848
849 int butAreaWid = butWidth + (m_btnSpacingX*2);
850
851 m_btnSize.x = butWidth;
852 m_btnSize.y = butHeight;
853
854 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
855 m_btnArea.y = btnBorder;
856 m_btnArea.width = butAreaWid;
857 m_btnArea.height = sz.y - (btnBorder*2);
858
859 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder;
860 m_tcArea.y = customBorder;
861 m_tcArea.width = sz.x - butAreaWid - (customBorder*2);
862 m_tcArea.height = sz.y - (customBorder*2);
863
864 /*
865 if ( m_text )
866 {
867 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
868 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
869 }
870 */
871 }
872
873 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
874 {
875 if ( !m_text )
876 return;
877
878 wxSize sz = GetClientSize();
879 int customBorder = m_widthCustomBorder;
880
881 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
882 {
883 // Centre textctrl
884 int tcSizeY = m_text->GetBestSize().y;
885 int diff = sz.y - tcSizeY;
886 int y = textCtrlYAdjust + (diff/2);
887
888 if ( y < customBorder )
889 y = customBorder;
890
891 m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust,
892 y,
893 m_tcArea.width - COMBO_MARGIN -
894 (textCtrlXAdjust + m_widthCustomPaint + m_absIndent),
895 -1 );
896
897 // Make sure textctrl doesn't exceed the bottom custom border
898 wxSize tsz = m_text->GetSize();
899 diff = (y + tsz.y) - (sz.y - customBorder);
900 if ( diff >= 0 )
901 {
902 tsz.y = tsz.y - diff - 1;
903 m_text->SetSize(tsz);
904 }
905 }
906 else
907 {
908 m_text->SetSize( m_tcArea.x,
909 0,
910 sz.x - m_btnArea.x - m_widthCustomPaint - customBorder,
911 sz.y );
912 }
913 }
914
915 wxSize wxComboCtrlBase::DoGetBestSize() const
916 {
917 wxSize sizeText(150,0);
918
919 if ( m_text )
920 sizeText = m_text->GetBestSize();
921
922 // TODO: Better method to calculate close-to-native control height.
923
924 int fhei;
925 if ( m_font.Ok() )
926 fhei = (m_font.GetPointSize()*2) + 5;
927 else if ( wxNORMAL_FONT->Ok() )
928 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
929 else
930 fhei = sizeText.y + 4;
931
932 // Need to force height to accomodate bitmap?
933 int btnSizeY = m_btnSize.y;
934 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
935 fhei = btnSizeY;
936
937 // Control height doesn't depend on border
938 /*
939 // Add border
940 int border = m_windowStyle & wxBORDER_MASK;
941 if ( border == wxSIMPLE_BORDER )
942 fhei += 2;
943 else if ( border == wxNO_BORDER )
944 fhei += (m_widthCustomBorder*2);
945 else
946 // Sunken etc.
947 fhei += 4;
948 */
949
950 // Final adjustments
951 #ifdef __WXGTK__
952 fhei += 1;
953 #endif
954
955 wxSize ret(sizeText.x + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH,
956 fhei);
957
958 CacheBestSize(ret);
959 return ret;
960 }
961
962 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
963 {
964 if ( !IsCreated() )
965 return;
966
967 // defined by actual wxComboCtrls
968 OnResize();
969
970 event.Skip();
971 }
972
973 // ----------------------------------------------------------------------------
974 // standard operations
975 // ----------------------------------------------------------------------------
976
977 bool wxComboCtrlBase::Enable(bool enable)
978 {
979 if ( !wxControl::Enable(enable) )
980 return false;
981
982 if ( m_btn )
983 m_btn->Enable(enable);
984 if ( m_text )
985 m_text->Enable(enable);
986
987 return true;
988 }
989
990 bool wxComboCtrlBase::Show(bool show)
991 {
992 if ( !wxControl::Show(show) )
993 return false;
994
995 if (m_btn)
996 m_btn->Show(show);
997
998 if (m_text)
999 m_text->Show(show);
1000
1001 return true;
1002 }
1003
1004 bool wxComboCtrlBase::SetFont ( const wxFont& font )
1005 {
1006 if ( !wxControl::SetFont(font) )
1007 return false;
1008
1009 if (m_text)
1010 m_text->SetFont(font);
1011
1012 return true;
1013 }
1014
1015 #if wxUSE_TOOLTIPS
1016 void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
1017 {
1018 wxControl::DoSetToolTip(tooltip);
1019
1020 // Set tool tip for button and text box
1021 if ( tooltip )
1022 {
1023 const wxString &tip = tooltip->GetTip();
1024 if ( m_text ) m_text->SetToolTip(tip);
1025 if ( m_btn ) m_btn->SetToolTip(tip);
1026 }
1027 else
1028 {
1029 if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL );
1030 if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL );
1031 }
1032 }
1033 #endif // wxUSE_TOOLTIPS
1034
1035 // ----------------------------------------------------------------------------
1036 // painting
1037 // ----------------------------------------------------------------------------
1038
1039 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1040 // prepare combo box background on area in a way typical on platform
1041 void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
1042 {
1043 wxSize sz = GetClientSize();
1044 bool isEnabled;
1045 bool isFocused; // also selected
1046
1047 // For smaller size control (and for disabled background) use less spacing
1048 int focusSpacingX;
1049 int focusSpacingY;
1050
1051 if ( !(flags & wxCONTROL_ISSUBMENU) )
1052 {
1053 // Drawing control
1054 isEnabled = IsEnabled();
1055 isFocused = ShouldDrawFocus();
1056
1057 // Windows-style: for smaller size control (and for disabled background) use less spacing
1058 focusSpacingX = isEnabled ? 2 : 1;
1059 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1060 }
1061 else
1062 {
1063 // Drawing a list item
1064 isEnabled = true; // they are never disabled
1065 isFocused = flags & wxCONTROL_SELECTED ? true : false;
1066
1067 focusSpacingX = 0;
1068 focusSpacingY = 0;
1069 }
1070
1071 // Set the background sub-rectangle for selection, disabled etc
1072 wxRect selRect(rect);
1073 selRect.y += focusSpacingY;
1074 selRect.height -= (focusSpacingY*2);
1075
1076 int wcp = 0;
1077
1078 if ( !(flags & wxCONTROL_ISSUBMENU) )
1079 wcp += m_widthCustomPaint;
1080
1081 selRect.x += wcp + focusSpacingX;
1082 selRect.width -= wcp + (focusSpacingX*2);
1083
1084 wxColour bgCol;
1085
1086 if ( isEnabled )
1087 {
1088 // If popup is hidden and this control is focused,
1089 // then draw the focus-indicator (selbgcolor background etc.).
1090 if ( isFocused )
1091 {
1092 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
1093 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1094 }
1095 else
1096 {
1097 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
1098 bgCol = GetBackgroundColour();
1099 }
1100 }
1101 else
1102 {
1103 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) );
1104 bgCol = GetBackgroundColour();
1105 }
1106
1107 dc.SetBrush( bgCol );
1108 dc.SetPen( bgCol );
1109 dc.DrawRectangle( selRect );
1110
1111 // Don't clip exactly to the selection rectangle so we can draw
1112 // to the non-selected area in front of it.
1113 wxRect clipRect(rect.x,rect.y,
1114 (selRect.x+selRect.width)-rect.x,rect.height);
1115 dc.SetClippingRegion(clipRect);
1116 }
1117 #else
1118 // Save the library size a bit for platforms that re-implement this.
1119 void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1120 {
1121 }
1122 #endif
1123
1124 void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, bool paintBg )
1125 {
1126 int drawState = m_btnState;
1127
1128 #ifdef __WXGTK__
1129 if ( m_isPopupShown )
1130 drawState |= wxCONTROL_PRESSED;
1131 #endif
1132
1133 wxRect drawRect(rect.x+m_btnSpacingX,
1134 rect.y+((rect.height-m_btnSize.y)/2),
1135 m_btnSize.x,
1136 m_btnSize.y);
1137
1138 // Make sure area is not larger than the control
1139 if ( drawRect.y < rect.y )
1140 drawRect.y = rect.y;
1141 if ( drawRect.height > rect.height )
1142 drawRect.height = rect.height;
1143
1144 bool enabled = IsEnabled();
1145
1146 if ( !enabled )
1147 drawState |= wxCONTROL_DISABLED;
1148
1149 if ( !m_bmpNormal.Ok() )
1150 {
1151 // Need to clear button background even if m_btn is present
1152 if ( paintBg )
1153 {
1154 wxColour bgCol;
1155
1156 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1157 bgCol = GetParent()->GetBackgroundColour();
1158 else
1159 bgCol = GetBackgroundColour();
1160
1161 dc.SetBrush(bgCol);
1162 dc.SetPen(bgCol);
1163 dc.DrawRectangle(rect);
1164 }
1165
1166 // Draw standard button
1167 wxRendererNative::Get().DrawComboBoxDropButton(this,
1168 dc,
1169 drawRect,
1170 drawState);
1171 }
1172 else
1173 {
1174 // Draw bitmap
1175
1176 wxBitmap* pBmp;
1177
1178 if ( !enabled )
1179 pBmp = &m_bmpDisabled;
1180 else if ( m_btnState & wxCONTROL_PRESSED )
1181 pBmp = &m_bmpPressed;
1182 else if ( m_btnState & wxCONTROL_CURRENT )
1183 pBmp = &m_bmpHover;
1184 else
1185 pBmp = &m_bmpNormal;
1186
1187 if ( m_blankButtonBg )
1188 {
1189 // If using blank button background, we need to clear its background
1190 // with button face colour instead of colour for rest of the control.
1191 if ( paintBg )
1192 {
1193 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1194 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1195 dc.SetPen(bgCol);
1196 dc.SetBrush(bgCol);
1197 dc.DrawRectangle(rect);
1198 }
1199
1200 wxRendererNative::Get().DrawPushButton(this,
1201 dc,
1202 drawRect,
1203 drawState);
1204
1205 }
1206 else
1207
1208 {
1209 // Need to clear button background even if m_btn is present
1210 // (assume non-button background was cleared just before this call so brushes are good)
1211 if ( paintBg )
1212 dc.DrawRectangle(rect);
1213 }
1214
1215 // Draw bitmap centered in drawRect
1216 dc.DrawBitmap(*pBmp,
1217 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1218 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1219 true);
1220 }
1221 }
1222
1223 void wxComboCtrlBase::RecalcAndRefresh()
1224 {
1225 if ( IsCreated() )
1226 {
1227 wxSizeEvent evt(GetSize(),GetId());
1228 GetEventHandler()->ProcessEvent(evt);
1229 Refresh();
1230 }
1231 }
1232
1233 wxBitmap& wxComboCtrlBase::GetBufferBitmap( const wxSize& sz ) const
1234 {
1235 // If size is larger, recalculate double buffer bitmap
1236 if ( !gs_doubleBuffer ||
1237 sz.x > gs_doubleBuffer->GetWidth() ||
1238 sz.y > gs_doubleBuffer->GetHeight() )
1239 {
1240 delete gs_doubleBuffer;
1241 gs_doubleBuffer = new wxBitmap(sz.x+25,sz.y);
1242 }
1243 return *gs_doubleBuffer;
1244 }
1245
1246 // ----------------------------------------------------------------------------
1247 // miscellaneous event handlers
1248 // ----------------------------------------------------------------------------
1249
1250 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
1251 {
1252 // Change event id, object and string before relaying it forward
1253 event.SetId(GetId());
1254 wxString s = event.GetString();
1255 event.SetEventObject(this);
1256 event.SetString(s);
1257 event.Skip();
1258 }
1259
1260 // call if cursor is on button area or mouse is captured for the button
1261 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
1262 int flags )
1263 {
1264 int type = event.GetEventType();
1265
1266 if ( type == wxEVT_MOTION )
1267 {
1268 if ( flags & wxCC_MF_ON_BUTTON )
1269 {
1270 if ( !(m_btnState & wxCONTROL_CURRENT) )
1271 {
1272 // Mouse hover begins
1273 m_btnState |= wxCONTROL_CURRENT;
1274 if ( HasCapture() ) // Retain pressed state.
1275 m_btnState |= wxCONTROL_PRESSED;
1276 Refresh();
1277 }
1278 }
1279 else if ( (m_btnState & wxCONTROL_CURRENT) )
1280 {
1281 // Mouse hover ends
1282 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1283 Refresh();
1284 }
1285 }
1286 else if ( type == wxEVT_LEFT_DOWN )
1287 {
1288 // Only accept event if it wasn't right after popup dismiss
1289 //if ( ::wxGetLocalTimeMillis() > m_timeCanClick )
1290 {
1291 // Need to test this, because it might be outside.
1292 if ( flags & wxCC_MF_ON_BUTTON )
1293 {
1294 m_btnState |= wxCONTROL_PRESSED;
1295 Refresh();
1296
1297 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1298 OnButtonClick();
1299 else
1300 // If showing popup now, do not capture mouse or there will be interference
1301 CaptureMouse();
1302 }
1303 }
1304 /*else
1305 {
1306 m_btnState = 0;
1307 }*/
1308 }
1309 else if ( type == wxEVT_LEFT_UP )
1310 {
1311
1312 // Only accept event if mouse was left-press was previously accepted
1313 if ( HasCapture() )
1314 ReleaseMouse();
1315
1316 if ( m_btnState & wxCONTROL_PRESSED )
1317 {
1318 // If mouse was inside, fire the click event.
1319 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1320 {
1321 if ( flags & wxCC_MF_ON_BUTTON )
1322 OnButtonClick();
1323 }
1324
1325 m_btnState &= ~(wxCONTROL_PRESSED);
1326 Refresh();
1327 }
1328 }
1329 else if ( type == wxEVT_LEAVE_WINDOW )
1330 {
1331 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1332 {
1333 m_btnState &= ~(wxCONTROL_CURRENT);
1334
1335 // Mouse hover ends
1336 if ( !m_isPopupShown )
1337 {
1338 m_btnState &= ~(wxCONTROL_PRESSED);
1339 Refresh();
1340 }
1341 }
1342 }
1343 else
1344 return false;
1345
1346 return true;
1347 }
1348
1349 // Conversion to double-clicks and some basic filtering
1350 // returns true if event was consumed or filtered
1351 //bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea )
1352 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
1353 int flags )
1354 {
1355 wxLongLong t = ::wxGetLocalTimeMillis();
1356 int evtType = event.GetEventType();
1357
1358 #if !USE_TRANSIENT_POPUP
1359 if ( m_isPopupShown &&
1360 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1361 {
1362 HidePopup();
1363 return true;
1364 }
1365 #endif
1366
1367 //
1368 // Generate our own double-clicks
1369 // (to allow on-focus dc-event on double-clicks instead of triple-clicks)
1370 if ( (m_windowStyle & wxCC_SPECIAL_DCLICK) &&
1371 !m_isPopupShown &&
1372 //!(handlerFlags & wxCC_MF_ON_BUTTON) )
1373 !(flags & wxCC_MF_ON_BUTTON) )
1374 {
1375 if ( evtType == wxEVT_LEFT_DOWN )
1376 {
1377 // Set value to avoid up-events without corresponding downs
1378 m_downReceived = true;
1379 }
1380 else if ( evtType == wxEVT_LEFT_DCLICK )
1381 {
1382 // We'll make our own double-clicks
1383 //evtType = 0;
1384 event.SetEventType(0);
1385 return true;
1386 }
1387 else if ( evtType == wxEVT_LEFT_UP )
1388 {
1389 if ( m_downReceived || m_timeLastMouseUp == 1 )
1390 {
1391 wxLongLong timeFromLastUp = (t-m_timeLastMouseUp);
1392
1393 if ( timeFromLastUp < DOUBLE_CLICK_CONVERSION_TRESHOLD )
1394 {
1395 //type = wxEVT_LEFT_DCLICK;
1396 event.SetEventType(wxEVT_LEFT_DCLICK);
1397 m_timeLastMouseUp = 1;
1398 }
1399 else
1400 {
1401 m_timeLastMouseUp = t;
1402 }
1403
1404 //m_downReceived = false;
1405 }
1406 }
1407 }
1408
1409 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1410 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1411 {
1412 event.SetEventType(0);
1413 return true;
1414 }
1415
1416 return false;
1417 }
1418
1419 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
1420 {
1421 int evtType = event.GetEventType();
1422
1423 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1424 (m_windowStyle & wxCB_READONLY) )
1425 {
1426 if ( m_isPopupShown )
1427 {
1428 #if !wxUSE_POPUPWIN
1429 // Normally do nothing - evt handler should close it for us
1430 #elif !USE_TRANSIENT_POPUP
1431 // Click here always hides the popup.
1432 HidePopup();
1433 #endif
1434 }
1435 else
1436 {
1437 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1438 {
1439 // In read-only mode, clicking the text is the
1440 // same as clicking the button.
1441 OnButtonClick();
1442 }
1443 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1444 {
1445 //if ( m_popupInterface->CycleValue() )
1446 // Refresh();
1447 if ( m_popupInterface )
1448 m_popupInterface->OnComboDoubleClick();
1449 }
1450 }
1451 }
1452 else
1453 if ( m_isPopupShown )
1454 {
1455 // relay (some) mouse events to the popup
1456 if ( evtType == wxEVT_MOUSEWHEEL )
1457 m_popup->AddPendingEvent(event);
1458 }
1459 else if ( evtType )
1460 event.Skip();
1461 }
1462
1463 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
1464 {
1465 if ( IsPopupShown() )
1466 {
1467 // pass it to the popped up control
1468 GetPopupControl()->GetControl()->AddPendingEvent(event);
1469 }
1470 else // no popup
1471 {
1472 int keycode = event.GetKeyCode();
1473
1474 if ( keycode == WXK_TAB )
1475 {
1476 wxNavigationKeyEvent evt;
1477 evt.SetFlags(wxNavigationKeyEvent::FromTab|
1478 (!event.ShiftDown() ? wxNavigationKeyEvent::IsForward
1479 : wxNavigationKeyEvent::IsBackward));
1480 evt.SetEventObject(this);
1481 GetParent()->GetEventHandler()->AddPendingEvent(evt);
1482 return;
1483 }
1484
1485 if ( IsKeyPopupToggle(event) )
1486 {
1487 OnButtonClick();
1488 return;
1489 }
1490
1491 int comboStyle = GetWindowStyle();
1492 wxComboPopup* popupInterface = GetPopupControl();
1493
1494 if ( !popupInterface )
1495 {
1496 event.Skip();
1497 return;
1498 }
1499
1500 if ( (comboStyle & wxCB_READONLY) ||
1501 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
1502 {
1503 popupInterface->OnComboKeyEvent(event);
1504 }
1505 else
1506 event.Skip();
1507 }
1508 }
1509
1510 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1511 {
1512 if ( event.GetEventType() == wxEVT_SET_FOCUS )
1513 {
1514 // First click is the first part of double-click
1515 // Some platforms don't generate down-less mouse up-event
1516 // (Windows does, GTK+2 doesn't), so that's why we have
1517 // to do this.
1518 m_timeLastMouseUp = ::wxGetLocalTimeMillis();
1519
1520 if ( m_text && m_text != ::wxWindow::FindFocus() )
1521 m_text->SetFocus();
1522 }
1523
1524 Refresh();
1525 }
1526
1527 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
1528 {
1529 OnThemeChange();
1530 // indentation may also have changed
1531 if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) )
1532 m_absIndent = GetNativeTextIndent();
1533 RecalcAndRefresh();
1534 }
1535
1536 // ----------------------------------------------------------------------------
1537 // popup handling
1538 // ----------------------------------------------------------------------------
1539
1540 // Create popup window and the child control
1541 void wxComboCtrlBase::CreatePopup()
1542 {
1543 wxComboPopup* popupInterface = m_popupInterface;
1544 wxWindow* popup;
1545
1546 if ( !m_winPopup )
1547 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
1548
1549 popupInterface->Create(m_winPopup);
1550 m_popup = popup = popupInterface->GetControl();
1551
1552 m_popupExtraHandler = new wxComboPopupExtraEventHandler(this);
1553 popup->PushEventHandler( m_popupExtraHandler );
1554
1555 // This may be helpful on some platforms
1556 // (eg. it bypasses a wxGTK popupwindow bug where
1557 // window is not initially hidden when it should be)
1558 m_winPopup->Hide();
1559
1560 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
1561 }
1562
1563 // Destroy popup window and the child control
1564 void wxComboCtrlBase::DestroyPopup()
1565 {
1566 HidePopup();
1567
1568 if ( m_popup )
1569 m_popup->RemoveEventHandler(m_popupExtraHandler);
1570
1571 delete m_popupExtraHandler;
1572
1573 delete m_popupInterface;
1574
1575 if ( m_winPopup )
1576 m_winPopup->Destroy();
1577
1578 m_popupExtraHandler = (wxEvtHandler*) NULL;
1579 m_popupInterface = (wxComboPopup*) NULL;
1580 m_winPopup = (wxWindow*) NULL;
1581 m_popup = (wxWindow*) NULL;
1582 }
1583
1584 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
1585 {
1586 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
1587
1588 DestroyPopup();
1589
1590 iface->InitBase(this);
1591 iface->Init();
1592
1593 m_popupInterface = iface;
1594
1595 if ( !iface->LazyCreate() )
1596 {
1597 CreatePopup();
1598 }
1599 else
1600 {
1601 m_popup = (wxWindow*) NULL;
1602 }
1603
1604 // This must be done after creation
1605 if ( m_valueString.length() )
1606 {
1607 iface->SetStringValue(m_valueString);
1608 //Refresh();
1609 }
1610 }
1611
1612 // Ensures there is atleast the default popup
1613 void wxComboCtrlBase::EnsurePopupControl()
1614 {
1615 if ( !m_popupInterface )
1616 SetPopupControl(NULL);
1617 }
1618
1619 void wxComboCtrlBase::OnButtonClick()
1620 {
1621 // Derived classes can override this method for totally custom
1622 // popup action
1623 ShowPopup();
1624 }
1625
1626 void wxComboCtrlBase::ShowPopup()
1627 {
1628 EnsurePopupControl();
1629 wxCHECK_RET( !IsPopupShown(), wxT("popup window already shown") );
1630
1631 SetFocus();
1632
1633 // Space above and below
1634 int screenHeight;
1635 wxPoint scrPos;
1636 int spaceAbove;
1637 int spaceBelow;
1638 int maxHeightPopup;
1639 wxSize ctrlSz = GetSize();
1640
1641 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
1642 scrPos = GetParent()->ClientToScreen(GetPosition());
1643
1644 spaceAbove = scrPos.y;
1645 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
1646
1647 maxHeightPopup = spaceBelow;
1648 if ( spaceAbove > spaceBelow )
1649 maxHeightPopup = spaceAbove;
1650
1651 // Width
1652 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
1653
1654 if ( widthPopup < m_widthMinPopup )
1655 widthPopup = m_widthMinPopup;
1656
1657 wxWindow* winPopup = m_winPopup;
1658 wxWindow* popup;
1659
1660 // Need to disable tab traversal of parent
1661 //
1662 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1663 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1664 // that if transient popup is open, then tab traversal is to be ignored.
1665 // However, I think this code would still be needed for cases where
1666 // transient popup doesn't work yet (wxWinCE?).
1667 wxWindow* parent = GetParent();
1668 int parentFlags = parent->GetWindowStyle();
1669 if ( parentFlags & wxTAB_TRAVERSAL )
1670 {
1671 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
1672 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
1673 }
1674
1675 if ( !winPopup )
1676 {
1677 CreatePopup();
1678 winPopup = m_winPopup;
1679 popup = m_popup;
1680 }
1681 else
1682 {
1683 popup = m_popup;
1684 }
1685
1686 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
1687
1688 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
1689 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
1690 maxHeightPopup);
1691
1692 popup->SetSize(adjustedSize);
1693 popup->Move(0,0);
1694 m_popupInterface->OnPopup();
1695
1696 //
1697 // Reposition and resize popup window
1698 //
1699
1700 wxSize szp = popup->GetSize();
1701
1702 int popupX;
1703 int popupY = scrPos.y + ctrlSz.y;
1704
1705 // Default anchor is wxLEFT
1706 int anchorSide = m_anchorSide;
1707 if ( !anchorSide )
1708 anchorSide = wxLEFT;
1709
1710 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
1711 int leftX = scrPos.x - m_extLeft;
1712 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
1713
1714 // If there is not enough horizontal space, anchor on the other side.
1715 // If there is no space even then, place the popup at x 0.
1716 if ( anchorSide == wxRIGHT )
1717 {
1718 if ( rightX < 0 )
1719 {
1720 if ( (leftX+szp.x) < screenWidth )
1721 anchorSide = wxLEFT;
1722 else
1723 anchorSide = 0;
1724 }
1725 }
1726 else
1727 {
1728 if ( (leftX+szp.x) >= screenWidth )
1729 {
1730 if ( rightX >= 0 )
1731 anchorSide = wxRIGHT;
1732 else
1733 anchorSide = 0;
1734 }
1735 }
1736
1737 // Select x coordinate according to the anchor side
1738 if ( anchorSide == wxRIGHT )
1739 popupX = rightX;
1740 else if ( anchorSide == wxLEFT )
1741 popupX = leftX;
1742 else
1743 popupX = 0;
1744
1745 if ( spaceBelow < szp.y )
1746 {
1747 popupY = scrPos.y - szp.y;
1748 }
1749
1750 // Move to position
1751 //wxLogDebug(wxT("popup scheduled position1: %i,%i"),ptp.x,ptp.y);
1752 //wxLogDebug(wxT("popup position1: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1753
1754 // Some platforms (GTK) may need these two to be separate
1755 winPopup->SetSize( szp.x, szp.y );
1756 winPopup->Move( popupX, popupY );
1757
1758 //wxLogDebug(wxT("popup position2: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1759
1760 m_popup = popup;
1761
1762 // Set string selection (must be this way instead of SetStringSelection)
1763 if ( m_text )
1764 {
1765 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1766 m_text->SelectAll();
1767
1768 m_popupInterface->SetStringValue( m_text->GetValue() );
1769 }
1770 else
1771 {
1772 // This is neede since focus/selection indication may change when popup is shown
1773 Refresh();
1774 }
1775
1776 // This must be after SetStringValue
1777 m_isPopupShown = true;
1778
1779 // Show it
1780 #if USE_TRANSIENT_POPUP
1781 ((wxPopupTransientWindow*)winPopup)->Popup(popup);
1782 #else
1783 winPopup->Show();
1784 #endif
1785
1786 #if INSTALL_TOPLEV_HANDLER
1787 // Put top level window event handler into place
1788 if ( !m_toplevEvtHandler )
1789 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
1790
1791 wxWindow* toplev = ::wxGetTopLevelParent( this );
1792 wxASSERT( toplev );
1793 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
1794 toplev->PushEventHandler( m_toplevEvtHandler );
1795 #endif
1796
1797 }
1798
1799 void wxComboCtrlBase::OnPopupDismiss()
1800 {
1801 // Just in case, avoid double dismiss
1802 if ( !m_isPopupShown )
1803 return;
1804
1805 // *Must* set this before focus etc.
1806 m_isPopupShown = false;
1807
1808 // Inform popup control itself
1809 m_popupInterface->OnDismiss();
1810
1811 if ( m_popupExtraHandler )
1812 ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss();
1813
1814 #if INSTALL_TOPLEV_HANDLER
1815 // Remove top level window event handler
1816 if ( m_toplevEvtHandler )
1817 {
1818 wxWindow* toplev = ::wxGetTopLevelParent( this );
1819 if ( toplev )
1820 toplev->RemoveEventHandler( m_toplevEvtHandler );
1821 }
1822 #endif
1823
1824 m_timeCanAcceptClick = ::wxGetLocalTimeMillis() + 150;
1825
1826 // If cursor not on dropdown button, then clear its state
1827 // (technically not required by all ports, but do it for all just in case)
1828 if ( !m_btnArea.Inside(ScreenToClient(::wxGetMousePosition())) )
1829 m_btnState = 0;
1830
1831 // Return parent's tab traversal flag.
1832 // See ShowPopup for notes.
1833 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
1834 {
1835 wxWindow* parent = GetParent();
1836 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
1837 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
1838 }
1839
1840 // refresh control (necessary even if m_text)
1841 Refresh();
1842
1843 #if !wxUSE_POPUPWIN
1844 SetFocus();
1845 #endif
1846
1847 }
1848
1849 void wxComboCtrlBase::HidePopup()
1850 {
1851 // Should be able to call this without popup interface
1852 //wxCHECK_RET( m_popupInterface, _T("no popup interface") );
1853 if ( !m_isPopupShown )
1854 return;
1855
1856 // transfer value and show it in textctrl, if any
1857 SetValue( m_popupInterface->GetStringValue() );
1858
1859 #if USE_TRANSIENT_POPUP
1860 ((wxPopupTransientWindow*)m_winPopup)->Dismiss();
1861 #else
1862 m_winPopup->Hide();
1863 #endif
1864
1865 OnPopupDismiss();
1866 }
1867
1868 // ----------------------------------------------------------------------------
1869 // customization methods
1870 // ----------------------------------------------------------------------------
1871
1872 void wxComboCtrlBase::SetButtonPosition( int width, int height,
1873 int side, int spacingX )
1874 {
1875 m_btnWid = width;
1876 m_btnHei = height;
1877 m_btnSide = side;
1878 m_btnSpacingX = spacingX;
1879
1880 RecalcAndRefresh();
1881 }
1882
1883 wxSize wxComboCtrlBase::GetButtonSize()
1884 {
1885 if ( m_btnSize.x > 0 )
1886 return m_btnSize;
1887
1888 wxSize retSize(m_btnWid,m_btnHei);
1889
1890 // Need to call CalculateAreas now if button size is
1891 // is not explicitly specified.
1892 if ( retSize.x <= 0 || retSize.y <= 0)
1893 {
1894 OnResize();
1895
1896 retSize = m_btnSize;
1897 }
1898
1899 return retSize;
1900 }
1901
1902 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
1903 bool blankButtonBg,
1904 const wxBitmap& bmpPressed,
1905 const wxBitmap& bmpHover,
1906 const wxBitmap& bmpDisabled )
1907 {
1908 m_bmpNormal = bmpNormal;
1909 m_blankButtonBg = blankButtonBg;
1910
1911 if ( bmpPressed.Ok() )
1912 m_bmpPressed = bmpPressed;
1913 else
1914 m_bmpPressed = bmpNormal;
1915
1916 if ( bmpHover.Ok() )
1917 m_bmpHover = bmpHover;
1918 else
1919 m_bmpHover = bmpNormal;
1920
1921 if ( bmpDisabled.Ok() )
1922 m_bmpDisabled = bmpDisabled;
1923 else
1924 m_bmpDisabled = bmpNormal;
1925
1926 RecalcAndRefresh();
1927 }
1928
1929 void wxComboCtrlBase::SetCustomPaintWidth( int width )
1930 {
1931 if ( m_text )
1932 {
1933 // move textctrl accordingly
1934 wxRect r = m_text->GetRect();
1935 int inc = width - m_widthCustomPaint;
1936 r.x += inc;
1937 r.width -= inc;
1938 m_text->SetSize( r );
1939 }
1940
1941 m_widthCustomPaint = width;
1942
1943 RecalcAndRefresh();
1944 }
1945
1946 void wxComboCtrlBase::SetTextIndent( int indent )
1947 {
1948 if ( indent < 0 )
1949 {
1950 m_absIndent = GetNativeTextIndent();
1951 m_iFlags &= ~(wxCC_IFLAG_INDENT_SET);
1952 }
1953 else
1954 {
1955 m_absIndent = indent;
1956 m_iFlags |= wxCC_IFLAG_INDENT_SET;
1957 }
1958
1959 RecalcAndRefresh();
1960 }
1961
1962 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
1963 {
1964 return DEFAULT_TEXT_INDENT;
1965 }
1966
1967 // ----------------------------------------------------------------------------
1968 // methods forwarded to wxTextCtrl
1969 // ----------------------------------------------------------------------------
1970
1971 wxString wxComboCtrlBase::GetValue() const
1972 {
1973 if ( m_text )
1974 return m_text->GetValue();
1975 return m_valueString;
1976 }
1977
1978 void wxComboCtrlBase::SetValue(const wxString& value)
1979 {
1980 if ( m_text )
1981 {
1982 m_text->SetValue(value);
1983 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1984 m_text->SelectAll();
1985 }
1986
1987 m_valueString = value;
1988
1989 Refresh();
1990
1991 // Since wxComboPopup may want to paint the combo as well, we need
1992 // to set the string value here (as well as sometimes in ShowPopup).
1993 if ( m_valueString != value && m_popupInterface )
1994 {
1995 m_popupInterface->SetStringValue(value);
1996 }
1997 }
1998
1999 // In this SetValue variant wxComboPopup::SetStringValue is not called
2000 void wxComboCtrlBase::SetText(const wxString& value)
2001 {
2002 // Unlike in SetValue(), this must be called here or
2003 // the behaviour will no be consistent in readonlys.
2004 EnsurePopupControl();
2005
2006 m_valueString = value;
2007
2008 Refresh();
2009 }
2010
2011 void wxComboCtrlBase::Copy()
2012 {
2013 if ( m_text )
2014 m_text->Copy();
2015 }
2016
2017 void wxComboCtrlBase::Cut()
2018 {
2019 if ( m_text )
2020 m_text->Cut();
2021 }
2022
2023 void wxComboCtrlBase::Paste()
2024 {
2025 if ( m_text )
2026 m_text->Paste();
2027 }
2028
2029 void wxComboCtrlBase::SetInsertionPoint(long pos)
2030 {
2031 if ( m_text )
2032 m_text->SetInsertionPoint(pos);
2033 }
2034
2035 void wxComboCtrlBase::SetInsertionPointEnd()
2036 {
2037 if ( m_text )
2038 m_text->SetInsertionPointEnd();
2039 }
2040
2041 long wxComboCtrlBase::GetInsertionPoint() const
2042 {
2043 if ( m_text )
2044 return m_text->GetInsertionPoint();
2045
2046 return 0;
2047 }
2048
2049 long wxComboCtrlBase::GetLastPosition() const
2050 {
2051 if ( m_text )
2052 return m_text->GetLastPosition();
2053
2054 return 0;
2055 }
2056
2057 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2058 {
2059 if ( m_text )
2060 m_text->Replace(from, to, value);
2061 }
2062
2063 void wxComboCtrlBase::Remove(long from, long to)
2064 {
2065 if ( m_text )
2066 m_text->Remove(from, to);
2067 }
2068
2069 void wxComboCtrlBase::SetSelection(long from, long to)
2070 {
2071 if ( m_text )
2072 m_text->SetSelection(from, to);
2073 }
2074
2075 void wxComboCtrlBase::Undo()
2076 {
2077 if ( m_text )
2078 m_text->Undo();
2079 }
2080
2081 #endif // wxUSE_COMBOCTRL