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