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