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