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