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