]> git.saurik.com Git - wxWidgets.git/blob - src/common/combocmn.cpp
added clipboard events (wxEVT_COMMAND_TEXT_COPY/CUT/PASTE) and implemented them for...
[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::DefaultPaintComboControl( wxComboControlBase* combo,
385 wxDC& dc, const wxRect& rect )
386 {
387 if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl
388 {
389 combo->DrawFocusBackground(dc,rect,0);
390
391 dc.DrawText( combo->GetValue(),
392 rect.x + combo->GetTextIndent(),
393 (rect.height-dc.GetCharHeight())/2 + rect.y );
394 }
395 }
396
397 void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
398 {
399 DefaultPaintComboControl(m_combo,dc,rect);
400 }
401
402 void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
403 {
404 event.Skip();
405 }
406
407 void wxComboPopup::OnComboDoubleClick()
408 {
409 }
410
411 void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) )
412 {
413 }
414
415 bool wxComboPopup::LazyCreate()
416 {
417 return false;
418 }
419
420 void wxComboPopup::Dismiss()
421 {
422 m_combo->HidePopup();
423 }
424
425 // ----------------------------------------------------------------------------
426 // input handling
427 // ----------------------------------------------------------------------------
428
429 //
430 // This is pushed to the event handler queue of either combo box
431 // or its textctrl (latter if not readonly combo).
432 //
433 class wxComboBoxExtraInputHandler : public wxEvtHandler
434 {
435 public:
436
437 wxComboBoxExtraInputHandler( wxComboControlBase* combo )
438 : wxEvtHandler()
439 {
440 m_combo = combo;
441 }
442 ~wxComboBoxExtraInputHandler() { }
443 void OnKey(wxKeyEvent& event);
444 void OnFocus(wxFocusEvent& event);
445
446 protected:
447 wxComboControlBase* m_combo;
448
449 private:
450 DECLARE_EVENT_TABLE()
451 };
452
453
454 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler)
455 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey)
456 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
457 END_EVENT_TABLE()
458
459
460 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event)
461 {
462 int keycode = event.GetKeyCode();
463
464 if ( keycode == WXK_TAB )
465 {
466 wxNavigationKeyEvent evt;
467 evt.SetFlags(wxNavigationKeyEvent::FromTab|
468 (!event.ShiftDown()?wxNavigationKeyEvent::IsForward:
469 wxNavigationKeyEvent::IsBackward));
470 evt.SetEventObject(m_combo);
471 m_combo->GetParent()->GetEventHandler()->AddPendingEvent(evt);
472 return;
473 }
474
475 if ( m_combo->IsPopupShown() )
476 {
477 // pass it to the popped up control
478 m_combo->GetPopupControl()->GetControl()->AddPendingEvent(event);
479 }
480 else // no popup
481 {
482 int comboStyle = m_combo->GetWindowStyle();
483 wxComboPopup* popupInterface = m_combo->GetPopupControl();
484
485 if ( !popupInterface )
486 {
487 event.Skip();
488 return;
489 }
490
491 if ( (comboStyle & wxCB_READONLY) ||
492 ( keycode != WXK_RIGHT && keycode != WXK_LEFT )
493 )
494 {
495 // Alternate keys: UP and DOWN show the popup instead of cycling
496 if ( (comboStyle & wxCC_ALT_KEYS) )
497 {
498 if ( keycode == WXK_UP || keycode == WXK_DOWN )
499 {
500 m_combo->OnButtonClick();
501 return;
502 }
503 else
504 event.Skip();
505 }
506 else
507 popupInterface->OnComboKeyEvent(event);
508 }
509 else
510 event.Skip();
511 }
512 }
513
514 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event)
515 {
516 // FIXME: This code does run when control is clicked,
517 // yet on Windows it doesn't select all the text.
518 if ( !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) )
519 {
520 if ( m_combo->GetTextCtrl() )
521 m_combo->GetTextCtrl()->SelectAll();
522 else
523 m_combo->SetSelection(-1,-1);
524 }
525
526 if ( event.GetId() != m_combo->GetId() )
527 {
528 // Add textctrl set focus events as combo set focus events
529 // NOTE: Simply changing the event and skipping didn't seem
530 // to do the trick.
531 wxFocusEvent evt2(wxEVT_SET_FOCUS,m_combo->GetId());
532 evt2.SetEventObject(m_combo);
533 m_combo->GetEventHandler()->ProcessEvent(evt2);
534 }
535 else
536 event.Skip();
537
538 event.Skip();
539 }
540
541
542 //
543 // This is pushed to the event handler queue of the control in popup.
544 //
545
546 class wxComboPopupExtraEventHandler : public wxEvtHandler
547 {
548 public:
549
550 wxComboPopupExtraEventHandler( wxComboControlBase* combo )
551 : wxEvtHandler()
552 {
553 m_combo = combo;
554 m_beenInside = false;
555 }
556 ~wxComboPopupExtraEventHandler() { }
557
558 void OnMouseEvent( wxMouseEvent& event );
559
560 // Called from wxComboControlBase::OnPopupDismiss
561 void OnPopupDismiss()
562 {
563 m_beenInside = false;
564 }
565
566 protected:
567 wxComboControlBase* m_combo;
568
569 bool m_beenInside;
570
571 private:
572 DECLARE_EVENT_TABLE()
573 };
574
575
576 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler)
577 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent)
578 END_EVENT_TABLE()
579
580
581 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event )
582 {
583 wxPoint pt = event.GetPosition();
584 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
585 int evtType = event.GetEventType();
586 bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y;
587
588 if ( evtType == wxEVT_MOTION ||
589 evtType == wxEVT_LEFT_DOWN ||
590 evtType == wxEVT_RIGHT_DOWN )
591 {
592 // Block motion and click events outside the popup
593 if ( !isInside )
594 {
595 event.Skip(false);
596 return;
597 }
598 }
599 else if ( evtType == wxEVT_LEFT_UP )
600 {
601 // Don't let left-down events in if outside
602 if ( evtType == wxEVT_LEFT_DOWN )
603 {
604 if ( !isInside )
605 return;
606 }
607
608 if ( !m_beenInside )
609 {
610 if ( isInside )
611 {
612 m_beenInside = true;
613 }
614 else
615 {
616 //
617 // Some mouse events to popup that happen outside it, before cursor
618 // has been inside the popu, need to be ignored by it but relayed to
619 // the dropbutton.
620 //
621 wxWindow* btn = m_combo->GetButton();
622 if ( btn )
623 btn->GetEventHandler()->AddPendingEvent(event);
624 else
625 m_combo->GetEventHandler()->AddPendingEvent(event);
626
627 return;
628 }
629
630 event.Skip();
631 }
632 }
633
634 event.Skip();
635 }
636
637 // ----------------------------------------------------------------------------
638 // wxComboControlBase
639 // ----------------------------------------------------------------------------
640
641
642 BEGIN_EVENT_TABLE(wxComboControlBase, wxControl)
643 EVT_TEXT(wxID_ANY,wxComboControlBase::OnTextCtrlEvent)
644 EVT_SIZE(wxComboControlBase::OnSizeEvent)
645 EVT_SET_FOCUS(wxComboControlBase::OnFocusEvent)
646 EVT_KILL_FOCUS(wxComboControlBase::OnFocusEvent)
647 //EVT_BUTTON(wxID_ANY,wxComboControlBase::OnButtonClickEvent)
648 EVT_TEXT_ENTER(wxID_ANY,wxComboControlBase::OnTextCtrlEvent)
649 EVT_SYS_COLOUR_CHANGED(wxComboControlBase::OnSysColourChanged)
650 END_EVENT_TABLE()
651
652
653 IMPLEMENT_ABSTRACT_CLASS(wxComboControlBase, wxControl)
654
655 // Have global double buffer - should be enough for multiple combos
656 static wxBitmap* gs_doubleBuffer = (wxBitmap*) NULL;
657
658 void wxComboControlBase::Init()
659 {
660 m_winPopup = (wxWindow *)NULL;
661 m_popup = (wxWindow *)NULL;
662 m_isPopupShown = false;
663 m_btn = (wxWindow*) NULL;
664 m_text = (wxTextCtrl*) NULL;
665 m_popupInterface = (wxComboPopup*) NULL;
666
667 m_extraEvtHandler = (wxEvtHandler*) NULL;
668 m_popupExtraHandler = (wxEvtHandler*) NULL;
669 m_textEvtHandler = (wxEvtHandler*) NULL;
670
671 #if INSTALL_TOPLEV_HANDLER
672 m_toplevEvtHandler = (wxEvtHandler*) NULL;
673 #endif
674
675 m_heightPopup = -1;
676 m_widthMinPopup = -1;
677 m_anchorSide = 0;
678 m_widthCustomPaint = 0;
679 m_widthCustomBorder = 0;
680
681 m_btnState = 0;
682 m_btnWidDefault = 0;
683 m_blankButtonBg = false;
684 m_btnWid = m_btnHei = 0;
685 m_btnSide = wxRIGHT;
686 m_btnSpacingX = 0;
687
688 m_extLeft = 0;
689 m_extRight = 0;
690 m_absIndent = -1;
691 m_iFlags = 0;
692 m_downReceived = false;
693 m_timeCanAcceptClick = 0;
694 }
695
696 bool wxComboControlBase::Create(wxWindow *parent,
697 wxWindowID id,
698 const wxString& value,
699 const wxPoint& pos,
700 const wxSize& size,
701 long style,
702 const wxValidator& validator,
703 const wxString& name)
704 {
705 if ( !wxControl::Create(parent,
706 id,
707 pos,
708 size,
709 style | wxWANTS_CHARS,
710 validator,
711 name) )
712 return false;
713
714 m_valueString = value;
715
716 // Get colours
717 OnThemeChange();
718 m_absIndent = GetNativeTextIndent();
719
720 return true;
721 }
722
723 void wxComboControlBase::InstallInputHandlers( bool alsoTextCtrl )
724 {
725 if ( m_text && alsoTextCtrl )
726 {
727 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
728 m_text->PushEventHandler(m_textEvtHandler);
729 }
730
731 wxComboBoxExtraInputHandler* inputHandler = new wxComboBoxExtraInputHandler(this);
732 PushEventHandler(inputHandler);
733 m_extraEvtHandler = inputHandler;
734 }
735
736 void wxComboControlBase::CreateTextCtrl( int extraStyle, const wxValidator& validator )
737 {
738 if ( !(m_windowStyle & wxCB_READONLY) )
739 {
740 m_text = new wxTextCtrl(this,
741 12345,
742 m_valueString,
743 wxDefaultPosition,
744 wxDefaultSize,
745 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
746 // not used by the wxPropertyGrid and therefore the tab is
747 // processed by looking at ancestors to see if they have
748 // wxTAB_TRAVERSAL. The navigation event is then sent to
749 // the wrong window.
750 wxTE_PROCESS_TAB |
751 extraStyle,
752 validator);
753
754 // This is required for some platforms (GTK+ atleast)
755 m_text->SetSizeHints(2,4);
756 }
757 }
758
759 void wxComboControlBase::OnThemeChange()
760 {
761 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
762 }
763
764 wxComboControlBase::~wxComboControlBase()
765 {
766 if ( HasCapture() )
767 ReleaseMouse();
768
769 delete gs_doubleBuffer;
770 gs_doubleBuffer = (wxBitmap*) NULL;
771
772 #if INSTALL_TOPLEV_HANDLER
773 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
774 m_toplevEvtHandler = (wxEvtHandler*) NULL;
775 #endif
776
777 if ( m_popup )
778 m_popup->RemoveEventHandler(m_popupExtraHandler);
779
780 delete m_popupExtraHandler;
781
782 HidePopup();
783
784 delete m_popupInterface;
785 delete m_winPopup;
786
787 RemoveEventHandler(m_extraEvtHandler);
788
789 if ( m_text )
790 m_text->RemoveEventHandler(m_textEvtHandler);
791
792 delete m_textEvtHandler;
793 delete m_extraEvtHandler;
794 }
795
796
797 // ----------------------------------------------------------------------------
798 // geometry stuff
799 // ----------------------------------------------------------------------------
800
801 // Recalculates button and textctrl areas
802 void wxComboControlBase::CalculateAreas( int btnWidth )
803 {
804 wxSize sz = GetClientSize();
805 int customBorder = m_widthCustomBorder;
806 bool buttonOutside;
807 int btnBorder; // border for button only
808
809 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) || m_blankButtonBg ) &&
810 m_btnSpacingX == 0 && m_btnWid == 0 && m_btnHei == 0 &&
811 (!m_bmpNormal.Ok() || m_blankButtonBg) )
812 {
813 buttonOutside = true;
814 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
815 btnBorder = 0;
816 }
817 else
818 {
819 buttonOutside = false;
820 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
821 btnBorder = customBorder;
822 }
823
824 // Defaul indentation
825 if ( m_absIndent < 0 )
826 m_absIndent = GetNativeTextIndent();
827
828 int butWidth = btnWidth;
829
830 if ( butWidth <= 0 )
831 butWidth = m_btnWidDefault;
832 else
833 m_btnWidDefault = butWidth;
834
835 if ( butWidth <= 0 )
836 return;
837
838 // Adjust button width
839 if ( m_btnWid < 0 )
840 butWidth += m_btnWid;
841 else if ( m_btnWid > 0 )
842 butWidth = m_btnWid;
843
844 int butHeight = sz.y;
845
846 butHeight -= btnBorder*2;
847
848 // Adjust button height
849 if ( m_btnHei < 0 )
850 butHeight += m_btnHei;
851 else if ( m_btnHei > 0 )
852 butHeight = m_btnHei;
853
854 // Use size of normal bitmap if...
855 // It is larger
856 // OR
857 // button width is set to default and blank button bg is not drawn
858 if ( m_bmpNormal.Ok() )
859 {
860 int bmpReqWidth = m_bmpNormal.GetWidth();
861 int bmpReqHeight = m_bmpNormal.GetHeight();
862
863 // If drawing blank button background, we need to add some margin.
864 if ( m_blankButtonBg )
865 {
866 bmpReqWidth += BMP_BUTTON_MARGIN*2;
867 bmpReqHeight += BMP_BUTTON_MARGIN*2;
868 }
869
870 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
871 butWidth = bmpReqWidth;
872 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
873 butHeight = bmpReqHeight;
874
875 // Need to fix height?
876 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
877 {
878 int newY = butHeight+(customBorder*2);
879 SetClientSize(-1,newY);
880 sz.y = newY;
881 }
882 }
883
884 int butAreaWid = butWidth + (m_btnSpacingX*2);
885
886 m_btnSize.x = butWidth;
887 m_btnSize.y = butHeight;
888
889 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
890 m_btnArea.y = btnBorder;
891 m_btnArea.width = butAreaWid;
892 m_btnArea.height = sz.y - (btnBorder*2);
893
894 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder;
895 m_tcArea.y = customBorder;
896 m_tcArea.width = sz.x - butAreaWid - (customBorder*2);
897 m_tcArea.height = sz.y - (customBorder*2);
898
899 /*
900 if ( m_text )
901 {
902 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
903 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
904 }
905 */
906 }
907
908 void wxComboControlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
909 {
910 if ( !m_text )
911 return;
912
913 wxSize sz = GetClientSize();
914 int customBorder = m_widthCustomBorder;
915
916 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
917 {
918 // Centre textctrl
919 int tcSizeY = m_text->GetBestSize().y;
920 int diff = sz.y - tcSizeY;
921 int y = textCtrlYAdjust + (diff/2);
922
923 if ( y < customBorder )
924 y = customBorder;
925
926 m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust,
927 y,
928 m_tcArea.width - COMBO_MARGIN -
929 (textCtrlXAdjust + m_widthCustomPaint + m_absIndent),
930 -1 );
931
932 // Make sure textctrl doesn't exceed the bottom custom border
933 wxSize tsz = m_text->GetSize();
934 diff = (y + tsz.y) - (sz.y - customBorder);
935 if ( diff >= 0 )
936 {
937 tsz.y = tsz.y - diff - 1;
938 m_text->SetSize(tsz);
939 }
940 }
941 else
942 {
943 m_text->SetSize( m_tcArea.x,
944 0,
945 sz.x - m_btnArea.x - m_widthCustomPaint - customBorder,
946 sz.y );
947 }
948 }
949
950 wxSize wxComboControlBase::DoGetBestSize() const
951 {
952 wxSize sizeText(150,0);
953
954 if ( m_text )
955 sizeText = m_text->GetBestSize();
956
957 // TODO: Better method to calculate close-to-native control height.
958
959 int fhei;
960 if ( m_font.Ok() )
961 fhei = (m_font.GetPointSize()*2) + 5;
962 else if ( wxNORMAL_FONT->Ok() )
963 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
964 else
965 fhei = sizeText.y + 4;
966
967 // Need to force height to accomodate bitmap?
968 int btnSizeY = m_btnSize.y;
969 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
970 fhei = btnSizeY;
971
972 // Control height doesn't depend on border
973 /*
974 // Add border
975 int border = m_windowStyle & wxBORDER_MASK;
976 if ( border == wxSIMPLE_BORDER )
977 fhei += 2;
978 else if ( border == wxNO_BORDER )
979 fhei += (m_widthCustomBorder*2);
980 else
981 // Sunken etc.
982 fhei += 4;
983 */
984
985 // Final adjustments
986 #ifdef __WXGTK__
987 fhei += 1;
988 #endif
989
990 wxSize ret(sizeText.x + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH,
991 fhei);
992
993 CacheBestSize(ret);
994 return ret;
995 }
996
997 void wxComboControlBase::DoMoveWindow(int x, int y, int width, int height)
998 {
999 // SetSize is called last in create, so it marks the end of creation
1000 m_iFlags |= wxCC_IFLAG_CREATED;
1001
1002 wxControl::DoMoveWindow(x, y, width, height);
1003 }
1004
1005 void wxComboControlBase::OnSizeEvent( wxSizeEvent& event )
1006 {
1007 if ( !IsCreated() )
1008 return;
1009
1010 // defined by actual wxComboControls
1011 OnResize();
1012
1013 event.Skip();
1014 }
1015
1016 // ----------------------------------------------------------------------------
1017 // standard operations
1018 // ----------------------------------------------------------------------------
1019
1020 bool wxComboControlBase::Enable(bool enable)
1021 {
1022 if ( !wxControl::Enable(enable) )
1023 return false;
1024
1025 if ( m_btn )
1026 m_btn->Enable(enable);
1027 if ( m_text )
1028 m_text->Enable(enable);
1029
1030 return true;
1031 }
1032
1033 bool wxComboControlBase::Show(bool show)
1034 {
1035 if ( !wxControl::Show(show) )
1036 return false;
1037
1038 if (m_btn)
1039 m_btn->Show(show);
1040
1041 if (m_text)
1042 m_text->Show(show);
1043
1044 return true;
1045 }
1046
1047 bool wxComboControlBase::SetFont ( const wxFont& font )
1048 {
1049 if ( !wxControl::SetFont(font) )
1050 return false;
1051
1052 if (m_text)
1053 m_text->SetFont(font);
1054
1055 return true;
1056 }
1057
1058 #if wxUSE_TOOLTIPS
1059 void wxComboControlBase::DoSetToolTip(wxToolTip *tooltip)
1060 {
1061 wxControl::DoSetToolTip(tooltip);
1062
1063 // Set tool tip for button and text box
1064 if ( tooltip )
1065 {
1066 const wxString &tip = tooltip->GetTip();
1067 if ( m_text ) m_text->SetToolTip(tip);
1068 if ( m_btn ) m_btn->SetToolTip(tip);
1069 }
1070 else
1071 {
1072 if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL );
1073 if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL );
1074 }
1075 }
1076 #endif // wxUSE_TOOLTIPS
1077
1078 // ----------------------------------------------------------------------------
1079 // painting
1080 // ----------------------------------------------------------------------------
1081
1082 // draw focus background on area in a way typical on platform
1083 void wxComboControlBase::DrawFocusBackground( wxDC& dc, const wxRect& rect, int flags )
1084 {
1085 wxSize sz = GetClientSize();
1086 bool isEnabled;
1087 bool isFocused; // also selected
1088
1089 // For smaller size control (and for disabled background) use less spacing
1090 int focusSpacingX;
1091 int focusSpacingY;
1092
1093 if ( !(flags & wxCONTROL_ISSUBMENU) )
1094 {
1095 // Drawing control
1096 isEnabled = IsEnabled();
1097 isFocused = ShouldDrawFocus();
1098
1099 // Windows-style: for smaller size control (and for disabled background) use less spacing
1100 focusSpacingX = isEnabled ? 2 : 1;
1101 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1102 }
1103 else
1104 {
1105 // Drawing a list item
1106 isEnabled = true; // they are never disabled
1107 isFocused = flags & wxCONTROL_SELECTED ? true : false;
1108
1109 focusSpacingX = 0;
1110 focusSpacingY = 0;
1111 }
1112
1113 // Set the background sub-rectangle for selection, disabled etc
1114 wxRect selRect(rect);
1115 selRect.y += focusSpacingY;
1116 selRect.height -= (focusSpacingY*2);
1117 selRect.x += m_widthCustomPaint + focusSpacingX;
1118 selRect.width -= m_widthCustomPaint + (focusSpacingX*2);
1119
1120 wxColour bgCol;
1121
1122 if ( isEnabled )
1123 {
1124 // If popup is hidden and this control is focused,
1125 // then draw the focus-indicator (selbgcolor background etc.).
1126 if ( isFocused )
1127 {
1128 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
1129 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1130 }
1131 else
1132 {
1133 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
1134 bgCol = GetBackgroundColour();
1135 }
1136 }
1137 else
1138 {
1139 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) );
1140 bgCol = GetBackgroundColour();
1141 }
1142
1143 dc.SetBrush( bgCol );
1144 dc.SetPen( bgCol );
1145 dc.DrawRectangle( selRect );
1146 }
1147
1148 void wxComboControlBase::DrawButton( wxDC& dc, const wxRect& rect, bool paintBg )
1149 {
1150 int drawState = m_btnState;
1151
1152 #ifdef __WXGTK__
1153 if ( m_isPopupShown )
1154 drawState |= wxCONTROL_PRESSED;
1155 #endif
1156
1157 wxRect drawRect(rect.x+m_btnSpacingX,
1158 rect.y+((rect.height-m_btnSize.y)/2),
1159 m_btnSize.x,
1160 m_btnSize.y);
1161
1162 // Make sure area is not larger than the control
1163 if ( drawRect.y < rect.y )
1164 drawRect.y = rect.y;
1165 if ( drawRect.height > rect.height )
1166 drawRect.height = rect.height;
1167
1168 bool enabled = IsEnabled();
1169
1170 if ( !enabled )
1171 drawState |= wxCONTROL_DISABLED;
1172
1173 if ( !m_bmpNormal.Ok() )
1174 {
1175 // Need to clear button background even if m_btn is present
1176 // (assume non-button background was cleared just before this call so brushes are good)
1177 if ( paintBg )
1178 dc.DrawRectangle(rect);
1179
1180 // Draw standard button
1181 wxRendererNative::Get().DrawComboBoxDropButton(this,
1182 dc,
1183 drawRect,
1184 drawState);
1185 }
1186 else
1187 {
1188 // Draw bitmap
1189
1190 wxBitmap* pBmp;
1191
1192 if ( !enabled )
1193 pBmp = &m_bmpDisabled;
1194 else if ( m_btnState & wxCONTROL_PRESSED )
1195 pBmp = &m_bmpPressed;
1196 else if ( m_btnState & wxCONTROL_CURRENT )
1197 pBmp = &m_bmpHover;
1198 else
1199 pBmp = &m_bmpNormal;
1200
1201 if ( m_blankButtonBg )
1202 {
1203 // If using blank button background, we need to clear its background
1204 // with button face colour instead of colour for rest of the control.
1205 if ( paintBg )
1206 {
1207 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1208 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1209 dc.SetPen(bgCol);
1210 dc.SetBrush(bgCol);
1211 dc.DrawRectangle(rect);
1212 }
1213
1214 wxRendererNative::Get().DrawPushButton(this,
1215 dc,
1216 drawRect,
1217 drawState);
1218
1219 }
1220 else
1221
1222 {
1223 // Need to clear button background even if m_btn is present
1224 // (assume non-button background was cleared just before this call so brushes are good)
1225 if ( paintBg )
1226 dc.DrawRectangle(rect);
1227 }
1228
1229 // Draw bitmap centered in drawRect
1230 dc.DrawBitmap(*pBmp,
1231 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1232 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1233 true);
1234 }
1235 }
1236
1237 void wxComboControlBase::RecalcAndRefresh()
1238 {
1239 if ( IsCreated() )
1240 {
1241 wxSizeEvent evt(GetSize(),GetId());
1242 GetEventHandler()->ProcessEvent(evt);
1243 Refresh();
1244 }
1245 }
1246
1247 wxBitmap& wxComboControlBase::GetBufferBitmap( const wxSize& sz ) const
1248 {
1249 // If size is larger, recalculate double buffer bitmap
1250 if ( !gs_doubleBuffer ||
1251 sz.x > gs_doubleBuffer->GetWidth() ||
1252 sz.y > gs_doubleBuffer->GetHeight() )
1253 {
1254 delete gs_doubleBuffer;
1255 gs_doubleBuffer = new wxBitmap(sz.x+25,sz.y);
1256 }
1257 return *gs_doubleBuffer;
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 wxCHECK_RET( iface, wxT("no popup interface set for wxComboControl") );
1523
1524 delete m_popupInterface;
1525 delete m_winPopup;
1526
1527 iface->InitBase(this);
1528 iface->Init();
1529
1530 m_popupInterface = iface;
1531
1532 if ( !iface->LazyCreate() || m_winPopup )
1533 {
1534 CreatePopup();
1535 }
1536 else
1537 {
1538 m_popup = (wxWindow*) NULL;
1539 }
1540
1541 // This must be done after creation
1542 if ( m_valueString.length() )
1543 {
1544 iface->SetStringValue(m_valueString);
1545 //Refresh();
1546 }
1547 }
1548
1549 // Ensures there is atleast the default popup
1550 void wxComboControlBase::EnsurePopupControl()
1551 {
1552 if ( !m_popupInterface )
1553 SetPopupControl(NULL);
1554 }
1555
1556 void wxComboControlBase::OnButtonClick()
1557 {
1558 // Derived classes can override this method for totally custom
1559 // popup action
1560 ShowPopup();
1561 }
1562
1563 void wxComboControlBase::ShowPopup()
1564 {
1565 EnsurePopupControl();
1566 wxCHECK_RET( !IsPopupShown(), wxT("popup window already shown") );
1567
1568 SetFocus();
1569
1570 // Space above and below
1571 int screenHeight;
1572 wxPoint scrPos;
1573 int spaceAbove;
1574 int spaceBelow;
1575 int maxHeightPopup;
1576 wxSize ctrlSz = GetSize();
1577
1578 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
1579 scrPos = GetParent()->ClientToScreen(GetPosition());
1580
1581 spaceAbove = scrPos.y;
1582 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
1583
1584 maxHeightPopup = spaceBelow;
1585 if ( spaceAbove > spaceBelow )
1586 maxHeightPopup = spaceAbove;
1587
1588 // Width
1589 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
1590
1591 if ( widthPopup < m_widthMinPopup )
1592 widthPopup = m_widthMinPopup;
1593
1594 wxWindow* winPopup = m_winPopup;
1595 wxWindow* popup;
1596
1597 // Need to disable tab traversal of parent
1598 //
1599 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1600 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1601 // that if transient popup is open, then tab traversal is to be ignored.
1602 // However, I think this code would still be needed for cases where
1603 // transient popup doesn't work yet (wxWinCE?).
1604 wxWindow* parent = GetParent();
1605 int parentFlags = parent->GetWindowStyle();
1606 if ( parentFlags & wxTAB_TRAVERSAL )
1607 {
1608 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
1609 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
1610 }
1611
1612 if ( !winPopup )
1613 {
1614 CreatePopup();
1615 winPopup = m_winPopup;
1616 popup = m_popup;
1617 }
1618 else
1619 {
1620 popup = m_popup;
1621 }
1622
1623 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
1624
1625 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
1626 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
1627 maxHeightPopup);
1628
1629 popup->SetSize(adjustedSize);
1630 popup->Move(0,0);
1631 m_popupInterface->OnPopup();
1632
1633 //
1634 // Reposition and resize popup window
1635 //
1636
1637 wxSize szp = popup->GetSize();
1638
1639 int popupX;
1640 int popupY = scrPos.y + ctrlSz.y;
1641
1642 int anchorSide = m_anchorSide;
1643 if ( !anchorSide )
1644 anchorSide = m_btnSide;
1645
1646 // Anchor popup to the side the dropbutton is on
1647 if ( anchorSide == wxRIGHT )
1648 popupX = scrPos.x + ctrlSz.x + m_extRight- szp.x;
1649 else
1650 popupX = scrPos.x - m_extLeft;
1651
1652 if ( spaceBelow < szp.y )
1653 {
1654 popupY = scrPos.y - szp.y;
1655 }
1656
1657 // Move to position
1658 //wxLogDebug(wxT("popup scheduled position1: %i,%i"),ptp.x,ptp.y);
1659 //wxLogDebug(wxT("popup position1: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1660
1661 // Some platforms (GTK) may need these two to be separate
1662 winPopup->SetSize( szp.x, szp.y );
1663 winPopup->Move( popupX, popupY );
1664
1665 //wxLogDebug(wxT("popup position2: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1666
1667 m_popup = popup;
1668
1669 // Set string selection (must be this way instead of SetStringSelection)
1670 if ( m_text )
1671 {
1672 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1673 m_text->SelectAll();
1674
1675 m_popupInterface->SetStringValue( m_text->GetValue() );
1676 }
1677 else
1678 {
1679 // This is neede since focus/selection indication may change when popup is shown
1680 Refresh();
1681 }
1682
1683 // This must be after SetStringValue
1684 m_isPopupShown = true;
1685
1686 // Show it
1687 #if USE_TRANSIENT_POPUP
1688 ((wxPopupTransientWindow*)winPopup)->Popup(popup);
1689 #else
1690 winPopup->Show();
1691 #endif
1692
1693 #if INSTALL_TOPLEV_HANDLER
1694 // Put top level window event handler into place
1695 if ( !m_toplevEvtHandler )
1696 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
1697
1698 wxWindow* toplev = ::wxGetTopLevelParent( this );
1699 wxASSERT( toplev );
1700 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
1701 toplev->PushEventHandler( m_toplevEvtHandler );
1702 #endif
1703
1704 }
1705
1706 void wxComboControlBase::OnPopupDismiss()
1707 {
1708 // Just in case, avoid double dismiss
1709 if ( !m_isPopupShown )
1710 return;
1711
1712 // *Must* set this before focus etc.
1713 m_isPopupShown = false;
1714
1715 // Inform popup control itself
1716 m_popupInterface->OnDismiss();
1717
1718 if ( m_popupExtraHandler )
1719 ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss();
1720
1721 #if INSTALL_TOPLEV_HANDLER
1722 // Remove top level window event handler
1723 if ( m_toplevEvtHandler )
1724 {
1725 wxWindow* toplev = ::wxGetTopLevelParent( this );
1726 if ( toplev )
1727 toplev->RemoveEventHandler( m_toplevEvtHandler );
1728 }
1729 #endif
1730
1731 m_timeCanAcceptClick = ::wxGetLocalTimeMillis() + 150;
1732
1733 // If cursor not on dropdown button, then clear its state
1734 // (technically not required by all ports, but do it for all just in case)
1735 if ( !m_btnArea.Inside(ScreenToClient(::wxGetMousePosition())) )
1736 m_btnState = 0;
1737
1738 // Return parent's tab traversal flag.
1739 // See ShowPopup for notes.
1740 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
1741 {
1742 wxWindow* parent = GetParent();
1743 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
1744 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
1745 }
1746
1747 // refresh control (necessary even if m_text)
1748 Refresh();
1749
1750 #if !wxUSE_POPUPWIN
1751 SetFocus();
1752 #endif
1753
1754 }
1755
1756 void wxComboControlBase::HidePopup()
1757 {
1758 // Should be able to call this without popup interface
1759 //wxCHECK_RET( m_popupInterface, _T("no popup interface") );
1760 if ( !m_isPopupShown )
1761 return;
1762
1763 // transfer value and show it in textctrl, if any
1764 SetValue( m_popupInterface->GetStringValue() );
1765
1766 #if USE_TRANSIENT_POPUP
1767 ((wxPopupTransientWindow*)m_winPopup)->Dismiss();
1768 #else
1769 m_winPopup->Hide();
1770 #endif
1771
1772 OnPopupDismiss();
1773 }
1774
1775 // ----------------------------------------------------------------------------
1776 // customization methods
1777 // ----------------------------------------------------------------------------
1778
1779 void wxComboControlBase::SetButtonPosition( int width, int height,
1780 int side, int spacingX )
1781 {
1782 m_btnWid = width;
1783 m_btnHei = height;
1784 m_btnSide = side;
1785 m_btnSpacingX = spacingX;
1786
1787 RecalcAndRefresh();
1788 }
1789
1790 void wxComboControlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
1791 bool blankButtonBg,
1792 const wxBitmap& bmpPressed,
1793 const wxBitmap& bmpHover,
1794 const wxBitmap& bmpDisabled )
1795 {
1796 m_bmpNormal = bmpNormal;
1797 m_blankButtonBg = blankButtonBg;
1798
1799 if ( bmpPressed.Ok() )
1800 m_bmpPressed = bmpPressed;
1801 else
1802 m_bmpPressed = bmpNormal;
1803
1804 if ( bmpHover.Ok() )
1805 m_bmpHover = bmpHover;
1806 else
1807 m_bmpHover = bmpNormal;
1808
1809 if ( bmpDisabled.Ok() )
1810 m_bmpDisabled = bmpDisabled;
1811 else
1812 m_bmpDisabled = bmpNormal;
1813
1814 RecalcAndRefresh();
1815 }
1816
1817 void wxComboControlBase::SetCustomPaintWidth( int width )
1818 {
1819 if ( m_text )
1820 {
1821 // move textctrl accordingly
1822 wxRect r = m_text->GetRect();
1823 int inc = width - m_widthCustomPaint;
1824 r.x += inc;
1825 r.width -= inc;
1826 m_text->SetSize( r );
1827 }
1828
1829 m_widthCustomPaint = width;
1830
1831 RecalcAndRefresh();
1832 }
1833
1834 void wxComboControlBase::SetTextIndent( int indent )
1835 {
1836 if ( indent < 0 )
1837 {
1838 m_absIndent = GetNativeTextIndent();
1839 m_iFlags &= ~(wxCC_IFLAG_INDENT_SET);
1840 }
1841 else
1842 {
1843 m_absIndent = indent;
1844 m_iFlags |= wxCC_IFLAG_INDENT_SET;
1845 }
1846
1847 RecalcAndRefresh();
1848 }
1849
1850 wxCoord wxComboControlBase::GetNativeTextIndent() const
1851 {
1852 return DEFAULT_TEXT_INDENT;
1853 }
1854
1855 // ----------------------------------------------------------------------------
1856 // methods forwarded to wxTextCtrl
1857 // ----------------------------------------------------------------------------
1858
1859 wxString wxComboControlBase::GetValue() const
1860 {
1861 if ( m_text )
1862 return m_text->GetValue();
1863 return m_valueString;
1864 }
1865
1866 void wxComboControlBase::SetValue(const wxString& value)
1867 {
1868 if ( m_text )
1869 {
1870 m_text->SetValue(value);
1871 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1872 m_text->SelectAll();
1873 }
1874
1875 m_valueString = value;
1876
1877 Refresh();
1878
1879 // Since wxComboPopup may want to paint the combo as well, we need
1880 // to set the string value here (as well as sometimes in ShowPopup).
1881 if ( m_valueString != value && m_popupInterface )
1882 {
1883 m_popupInterface->SetStringValue(value);
1884 }
1885 }
1886
1887 // In this SetValue variant wxComboPopup::SetStringValue is not called
1888 void wxComboControlBase::SetText(const wxString& value)
1889 {
1890 // Unlike in SetValue(), this must be called here or
1891 // the behaviour will no be consistent in readonlys.
1892 EnsurePopupControl();
1893
1894 m_valueString = value;
1895
1896 Refresh();
1897 }
1898
1899 void wxComboControlBase::Copy()
1900 {
1901 if ( m_text )
1902 m_text->Copy();
1903 }
1904
1905 void wxComboControlBase::Cut()
1906 {
1907 if ( m_text )
1908 m_text->Cut();
1909 }
1910
1911 void wxComboControlBase::Paste()
1912 {
1913 if ( m_text )
1914 m_text->Paste();
1915 }
1916
1917 void wxComboControlBase::SetInsertionPoint(long pos)
1918 {
1919 if ( m_text )
1920 m_text->SetInsertionPoint(pos);
1921 }
1922
1923 void wxComboControlBase::SetInsertionPointEnd()
1924 {
1925 if ( m_text )
1926 m_text->SetInsertionPointEnd();
1927 }
1928
1929 long wxComboControlBase::GetInsertionPoint() const
1930 {
1931 if ( m_text )
1932 return m_text->GetInsertionPoint();
1933
1934 return 0;
1935 }
1936
1937 long wxComboControlBase::GetLastPosition() const
1938 {
1939 if ( m_text )
1940 return m_text->GetLastPosition();
1941
1942 return 0;
1943 }
1944
1945 void wxComboControlBase::Replace(long from, long to, const wxString& value)
1946 {
1947 if ( m_text )
1948 m_text->Replace(from, to, value);
1949 }
1950
1951 void wxComboControlBase::Remove(long from, long to)
1952 {
1953 if ( m_text )
1954 m_text->Remove(from, to);
1955 }
1956
1957 void wxComboControlBase::SetSelection(long from, long to)
1958 {
1959 if ( m_text )
1960 m_text->SetSelection(from, to);
1961 }
1962
1963 void wxComboControlBase::Undo()
1964 {
1965 if ( m_text )
1966 m_text->Undo();
1967 }
1968
1969 #endif // wxUSE_COMBOCONTROL