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