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