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