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