]> git.saurik.com Git - wxWidgets.git/blame - src/common/combocmn.cpp
Remove temporary files created by wxFileName unit test case.
[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
f313deaa 26#if wxUSE_COMBOBOX
a5bbd1cc 27#include "wx/combobox.h"
f313deaa
PC
28extern WXDLLEXPORT_DATA(const char) wxComboBoxNameStr[] = "comboBox";
29#endif
30
31#if wxUSE_COMBOCTRL
a5bbd1cc 32
a340b80d 33#ifndef WX_PRECOMP
46beb2e9 34 #include "wx/app.h"
a340b80d 35 #include "wx/log.h"
a340b80d
VZ
36 #include "wx/dcclient.h"
37 #include "wx/settings.h"
c0badb70 38 #include "wx/timer.h"
0ec1179b 39 #include "wx/textctrl.h"
a340b80d
VZ
40#endif
41
a340b80d 42#include "wx/tooltip.h"
a340b80d
VZ
43
44#include "wx/combo.h"
45
46
28953245
SC
47// ----------------------------------------------------------------------------
48// XTI
49// ----------------------------------------------------------------------------
50
51wxDEFINE_FLAGS( wxComboBoxStyle )
52wxBEGIN_FLAGS( wxComboBoxStyle )
53// new style border flags, we put them first to
54// use them for streaming out
55wxFLAGS_MEMBER(wxBORDER_SIMPLE)
56wxFLAGS_MEMBER(wxBORDER_SUNKEN)
57wxFLAGS_MEMBER(wxBORDER_DOUBLE)
58wxFLAGS_MEMBER(wxBORDER_RAISED)
59wxFLAGS_MEMBER(wxBORDER_STATIC)
60wxFLAGS_MEMBER(wxBORDER_NONE)
61
62// old style border flags
63wxFLAGS_MEMBER(wxSIMPLE_BORDER)
64wxFLAGS_MEMBER(wxSUNKEN_BORDER)
65wxFLAGS_MEMBER(wxDOUBLE_BORDER)
66wxFLAGS_MEMBER(wxRAISED_BORDER)
67wxFLAGS_MEMBER(wxSTATIC_BORDER)
68wxFLAGS_MEMBER(wxBORDER)
69
70// standard window styles
71wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74wxFLAGS_MEMBER(wxWANTS_CHARS)
75wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77wxFLAGS_MEMBER(wxVSCROLL)
78wxFLAGS_MEMBER(wxHSCROLL)
79
80wxFLAGS_MEMBER(wxCB_SIMPLE)
81wxFLAGS_MEMBER(wxCB_SORT)
82wxFLAGS_MEMBER(wxCB_READONLY)
83wxFLAGS_MEMBER(wxCB_DROPDOWN)
84
85wxEND_FLAGS( wxComboBoxStyle )
86
72259abb 87wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxComboBox, wxControl, "wx/combobox.h")
28953245
SC
88
89wxBEGIN_PROPERTIES_TABLE(wxComboBox)
90wxEVENT_PROPERTY( Select, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEvent )
91wxEVENT_PROPERTY( TextEnter, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent )
92
93// TODO DELEGATES
94wxPROPERTY( Font, wxFont, SetFont, GetFont, wxEMPTY_PARAMETER_VALUE, \
95 0 /*flags*/, wxT("Helpstring"), wxT("group"))
96wxPROPERTY_COLLECTION( Choices, wxArrayString, wxString, AppendString, \
97 GetStrings, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
98wxPROPERTY( Value,wxString, SetValue, GetValue, wxEMPTY_PARAMETER_VALUE, \
99 0 /*flags*/, wxT("Helpstring"), wxT("group"))
100wxPROPERTY( Selection,int, SetSelection, GetSelection, wxEMPTY_PARAMETER_VALUE, \
101 0 /*flags*/, wxT("Helpstring"), wxT("group"))
102
103wxPROPERTY_FLAGS( WindowStyle, wxComboBoxStyle, long, SetWindowStyleFlag, \
104 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
105 wxT("Helpstring"), wxT("group")) // style
106wxEND_PROPERTIES_TABLE()
107
108wxEMPTY_HANDLERS_TABLE(wxComboBox)
109
110wxCONSTRUCTOR_5( wxComboBox, wxWindow*, Parent, wxWindowID, Id, \
111 wxString, Value, wxPoint, Position, wxSize, Size )
a340b80d
VZ
112
113// constants
114// ----------------------------------------------------------------------------
115
a340b80d
VZ
116#define DEFAULT_DROPBUTTON_WIDTH 19
117
118#define BMP_BUTTON_MARGIN 4
119
7962f85a 120#define DEFAULT_POPUP_HEIGHT 400
a340b80d
VZ
121
122#define DEFAULT_TEXT_INDENT 3
123
124#define COMBO_MARGIN 2 // spacing right of wxTextCtrl
125
126
127#if defined(__WXMSW__)
128
fd5b4315
JS
129// Let's use wxFrame as a fall-back solution until wxMSW gets wxNonOwnedWindow
130#include "wx/frame.h"
131#define wxCC_GENERIC_TLW_IS_FRAME
132#define wxComboCtrlGenericTLW wxFrame
133
a340b80d 134#define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
06077aaf
VZ
135#define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
136 // native controls work on it like normal.
137#define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
7e4545b8 138#define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
2dfa37d6 139#define FOCUS_RING 0 // No focus ring on wxMSW
a340b80d
VZ
140
141//#undef wxUSE_POPUPWIN
142//#define wxUSE_POPUPWIN 0
143
144#elif defined(__WXGTK__)
145
06077aaf
VZ
146// NB: It is not recommended to use wxDialog as popup on wxGTK, because of
147// this bug: If wxDialog is hidden, its position becomes corrupt
148// between hide and next show, but without internal coordinates being
149// reflected (or something like that - atleast commenting out ->Hide()
150// seemed to eliminate the position change).
151
fd5b4315
JS
152#include "wx/dialog.h"
153#define wxCC_GENERIC_TLW_IS_DIALOG
154#define wxComboCtrlGenericTLW wxDialog
155
b9d0da6f
JJ
156#if defined(__WXGTK20__)
157# include "wx/gtk/private.h"
158#else
159# include "wx/gtk1/private.h"
160#endif
fd5b4315 161
d42b8621 162// NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
4c51a665 163// 'perfect' popup, as it can successfully host child controls even in
d42b8621
JS
164// popups that are shown in modal dialogs.
165
a340b80d 166#define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
d42b8621 167#define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
06077aaf
VZ
168 // native controls work on it like normal.
169#define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
7e4545b8 170#define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
2dfa37d6 171#define FOCUS_RING 0 // No focus ring on wxGTK
a340b80d
VZ
172
173#elif defined(__WXMAC__)
174
fd5b4315
JS
175#include "wx/nonownedwnd.h"
176#define wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
177#define wxComboCtrlGenericTLW wxNonOwnedWindow
178
b48eedfc
RR
179#define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
180#define TRANSIENT_POPUPWIN_IS_PERFECT 1 // wxPopupTransientWindow works, its child can have focus, and common
06077aaf 181 // native controls work on it like normal.
b48eedfc 182#define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window.
7e4545b8 183#define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
2dfa37d6
RD
184#define FOCUS_RING 3 // Reserve room for the textctrl's focus ring to display
185
186#undef DEFAULT_DROPBUTTON_WIDTH
187#define DEFAULT_DROPBUTTON_WIDTH 22
188#undef COMBO_MARGIN
189#define COMBO_MARGIN FOCUS_RING
a340b80d
VZ
190
191#else
192
fd5b4315
JS
193#include "wx/dialog.h"
194#define wxCC_GENERIC_TLW_IS_DIALOG
195#define wxComboCtrlGenericTLW wxDialog
196
a340b80d 197#define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
06077aaf
VZ
198#define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common
199 // native controls work on it like normal.
200#define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window.
7e4545b8 201#define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
2dfa37d6 202#define FOCUS_RING 0
f3fcecd6 203
a340b80d
VZ
204#endif
205
206
207// Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless
208// what the wxUSE_POPUPWIN says.
209// FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2)
b48eedfc 210#if (!defined(__WXMSW__) && !defined(__WXGTK__) && !defined(__WXMAC__)) || defined(__WXWINCE__)
a340b80d
VZ
211#undef wxUSE_POPUPWIN
212#define wxUSE_POPUPWIN 0
213#endif
214
215
216#if wxUSE_POPUPWIN
217 #include "wx/popupwin.h"
218#else
219 #undef USE_TRANSIENT_POPUP
220 #define USE_TRANSIENT_POPUP 0
221#endif
222
223
06077aaf
VZ
224// Define different types of popup windows
225enum
226{
227 POPUPWIN_NONE = 0,
228 POPUPWIN_WXPOPUPTRANSIENTWINDOW = 1,
229 POPUPWIN_WXPOPUPWINDOW = 2,
fd5b4315 230 POPUPWIN_GENERICTLW = 3
06077aaf 231};
a340b80d 232
06077aaf
VZ
233
234#if USE_TRANSIENT_POPUP
235 // wxPopupTransientWindow is implemented
236
237 #define wxComboPopupWindowBase wxPopupTransientWindow
238 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW
239 #define USES_WXPOPUPTRANSIENTWINDOW 1
240
241 #if TRANSIENT_POPUPWIN_IS_PERFECT
242 //
243 #elif POPUPWIN_IS_PERFECT
244 #define wxComboPopupWindowBase2 wxPopupWindow
245 #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
246 #define USES_WXPOPUPWINDOW 1
247 #else
fd5b4315
JS
248 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
249 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
250 #define USES_GENERICTLW 1
06077aaf 251 #endif
a340b80d
VZ
252
253#elif wxUSE_POPUPWIN
06077aaf 254 // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented
a340b80d 255
06077aaf
VZ
256 #define wxComboPopupWindowBase wxPopupWindow
257 #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW
258 #define USES_WXPOPUPWINDOW 1
259
260 #if !POPUPWIN_IS_PERFECT
fd5b4315
JS
261 #define wxComboPopupWindowBase2 wxComboCtrlGenericTLW
262 #define SECONDARY_POPUP_TYPE POPUPWIN_GENERICTLW
263 #define USES_GENERICTLW 1
06077aaf 264 #endif
a340b80d
VZ
265
266#else
06077aaf
VZ
267 // wxPopupWindow is not implemented
268
fd5b4315
JS
269 #define wxComboPopupWindowBase wxComboCtrlGenericTLW
270 #define PRIMARY_POPUP_TYPE POPUPWIN_GENERICTLW
271 #define USES_GENERICTLW 1
06077aaf
VZ
272
273#endif
274
275
276#ifndef USES_WXPOPUPTRANSIENTWINDOW
277 #define USES_WXPOPUPTRANSIENTWINDOW 0
278#endif
a340b80d 279
06077aaf
VZ
280#ifndef USES_WXPOPUPWINDOW
281 #define USES_WXPOPUPWINDOW 0
282#endif
a340b80d 283
fd5b4315
JS
284#ifndef USES_GENERICTLW
285 #define USES_GENERICTLW 0
a340b80d
VZ
286#endif
287
288
06077aaf
VZ
289#if USES_WXPOPUPWINDOW
290 #define INSTALL_TOPLEV_HANDLER 1
291#else
292 #define INSTALL_TOPLEV_HANDLER 0
293#endif
294
a340b80d 295
52999871
JS
296// Returns true if given popup window type can be classified as perfect
297// on this platform.
298static inline bool IsPopupWinTypePerfect( wxByte popupWinType )
299{
300#if POPUPWIN_IS_PERFECT && TRANSIENT_POPUPWIN_IS_PERFECT
301 wxUnusedVar(popupWinType);
302 return true;
303#else
304 return ( popupWinType == POPUPWIN_GENERICTLW
305 #if POPUPWIN_IS_PERFECT
ce00f59b 306 || popupWinType == POPUPWIN_WXPOPUPWINDOW
52999871
JS
307 #endif
308 #if TRANSIENT_POPUPWIN_IS_PERFECT
ce00f59b 309 || popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW
52999871
JS
310 #endif
311 );
312#endif
313}
314
315
a340b80d
VZ
316//
317// ** TODO **
318// * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow)
319//
320
321
322// ----------------------------------------------------------------------------
323// wxComboFrameEventHandler takes care of hiding the popup when events happen
324// in its top level parent.
325// ----------------------------------------------------------------------------
326
327#if INSTALL_TOPLEV_HANDLER
328
329//
330// This will no longer be necessary after wxTransientPopupWindow
331// works well on all platforms.
332//
333
334class wxComboFrameEventHandler : public wxEvtHandler
335{
336public:
a57d600f 337 wxComboFrameEventHandler( wxComboCtrlBase* pCb );
d3c7fc99 338 virtual ~wxComboFrameEventHandler();
a340b80d
VZ
339
340 void OnPopup();
341
342 void OnIdle( wxIdleEvent& event );
343 void OnMouseEvent( wxMouseEvent& event );
344 void OnActivate( wxActivateEvent& event );
345 void OnResize( wxSizeEvent& event );
346 void OnMove( wxMoveEvent& event );
347 void OnMenuEvent( wxMenuEvent& event );
348 void OnClose( wxCloseEvent& event );
349
350protected:
351 wxWindow* m_focusStart;
a57d600f 352 wxComboCtrlBase* m_combo;
a340b80d
VZ
353
354private:
355 DECLARE_EVENT_TABLE()
356};
357
358BEGIN_EVENT_TABLE(wxComboFrameEventHandler, wxEvtHandler)
359 EVT_IDLE(wxComboFrameEventHandler::OnIdle)
360 EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent)
361 EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent)
362 EVT_SIZE(wxComboFrameEventHandler::OnResize)
363 EVT_MOVE(wxComboFrameEventHandler::OnMove)
364 EVT_MENU_HIGHLIGHT(wxID_ANY,wxComboFrameEventHandler::OnMenuEvent)
365 EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent)
366 EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate)
367 EVT_CLOSE(wxComboFrameEventHandler::OnClose)
368END_EVENT_TABLE()
369
a57d600f 370wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo )
a340b80d
VZ
371 : wxEvtHandler()
372{
373 m_combo = combo;
374}
375
376wxComboFrameEventHandler::~wxComboFrameEventHandler()
377{
378}
379
380void wxComboFrameEventHandler::OnPopup()
381{
382 m_focusStart = ::wxWindow::FindFocus();
383}
384
385void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event )
386{
387 wxWindow* winFocused = ::wxWindow::FindFocus();
388
c667b518 389 wxWindow* popup = m_combo->GetPopupControl()->GetControl();
a340b80d
VZ
390 wxWindow* winpopup = m_combo->GetPopupWindow();
391
392 if (
393 winFocused != m_focusStart &&
394 winFocused != popup &&
395 winFocused->GetParent() != popup &&
396 winFocused != winpopup &&
397 winFocused->GetParent() != winpopup &&
398 winFocused != m_combo &&
399 winFocused != m_combo->GetButton() // GTK (atleast) requires this
400 )
401 {
a1d5aa93 402 m_combo->HidePopup(true);
a340b80d
VZ
403 }
404
405 event.Skip();
406}
407
408void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event )
409{
a1d5aa93 410 m_combo->HidePopup(true);
a340b80d
VZ
411 event.Skip();
412}
413
414void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event )
415{
a1d5aa93 416 m_combo->HidePopup(true);
a340b80d
VZ
417 event.Skip();
418}
419
420void wxComboFrameEventHandler::OnClose( wxCloseEvent& event )
421{
a1d5aa93 422 m_combo->HidePopup(true);
a340b80d
VZ
423 event.Skip();
424}
425
426void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event )
427{
a1d5aa93 428 m_combo->HidePopup(true);
a340b80d
VZ
429 event.Skip();
430}
431
432void wxComboFrameEventHandler::OnResize( wxSizeEvent& event )
433{
a1d5aa93 434 m_combo->HidePopup(true);
a340b80d
VZ
435 event.Skip();
436}
437
438void wxComboFrameEventHandler::OnMove( wxMoveEvent& event )
439{
a1d5aa93 440 m_combo->HidePopup(true);
a340b80d
VZ
441 event.Skip();
442}
443
444#endif // INSTALL_TOPLEV_HANDLER
445
446// ----------------------------------------------------------------------------
06077aaf 447// wxComboPopupWindow is, in essence, wxPopupWindow customized for
a57d600f 448// wxComboCtrl.
a340b80d
VZ
449// ----------------------------------------------------------------------------
450
451class wxComboPopupWindow : public wxComboPopupWindowBase
452{
453public:
454
06077aaf
VZ
455 wxComboPopupWindow( wxComboCtrlBase *parent,
456 int style )
457 #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW
458 : wxComboPopupWindowBase(parent,style)
459 #else
460 : wxComboPopupWindowBase(parent,
461 wxID_ANY,
462 wxEmptyString,
463 wxPoint(-21,-21),
464 wxSize(20,20),
465 style)
466 #endif
467 {
974a12f8 468 m_inShow = 0;
06077aaf 469 }
a340b80d 470
06077aaf 471#if USES_WXPOPUPTRANSIENTWINDOW
974a12f8 472 virtual bool Show( bool show );
a340b80d 473 virtual bool ProcessLeftDown(wxMouseEvent& event);
d7e15728 474protected:
06077aaf 475 virtual void OnDismiss();
a340b80d
VZ
476#endif
477
974a12f8
RR
478private:
479 wxByte m_inShow;
06077aaf 480};
a340b80d 481
a340b80d 482
06077aaf 483#if USES_WXPOPUPTRANSIENTWINDOW
974a12f8 484bool wxComboPopupWindow::Show( bool show )
06077aaf 485{
974a12f8
RR
486 // Guard against recursion
487 if ( m_inShow )
488 return wxComboPopupWindowBase::Show(show);
489
490 m_inShow++;
491
492 wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow)) );
493
494 wxPopupTransientWindow* ptw = (wxPopupTransientWindow*) this;
974a12f8
RR
495
496 if ( show != ptw->IsShown() )
497 {
498 if ( show )
c905c0d6
VZ
499 // We used to do wxPopupTransientWindow::Popup here,
500 // but this would hide normal Show, which we are
501 // also going to need.
502 ptw->Show();
974a12f8
RR
503 else
504 ptw->Dismiss();
505 }
506
507 m_inShow--;
508
509 return true;
510}
511
512bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent& event)
513{
514 return wxPopupTransientWindow::ProcessLeftDown(event);
06077aaf 515}
a340b80d 516
06077aaf
VZ
517// First thing that happens when a transient popup closes is that this method gets called.
518void wxComboPopupWindow::OnDismiss()
519{
520 wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent();
521 wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxComboCtrlBase)),
522 wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") );
523
a1d5aa93 524 combo->OnPopupDismiss(true);
06077aaf
VZ
525}
526#endif // USES_WXPOPUPTRANSIENTWINDOW
527
528
529// ----------------------------------------------------------------------------
530// wxComboPopupWindowEvtHandler does bulk of the custom event handling
531// of a popup window. It is separate so we can have different types
532// of popup windows.
533// ----------------------------------------------------------------------------
534
535class wxComboPopupWindowEvtHandler : public wxEvtHandler
536{
537public:
538
539 wxComboPopupWindowEvtHandler( wxComboCtrlBase *parent )
540 {
541 m_combo = parent;
542 }
543
974a12f8 544 void OnSizeEvent( wxSizeEvent& event );
06077aaf 545 void OnKeyEvent(wxKeyEvent& event);
fd5b4315 546#if USES_GENERICTLW
06077aaf 547 void OnActivate( wxActivateEvent& event );
a340b80d
VZ
548#endif
549
550private:
06077aaf
VZ
551 wxComboCtrlBase* m_combo;
552
a340b80d
VZ
553 DECLARE_EVENT_TABLE()
554};
555
556
06077aaf
VZ
557BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler, wxEvtHandler)
558 EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent)
559 EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent)
c765e6fb 560 EVT_CHAR(wxComboPopupWindowEvtHandler::OnKeyEvent)
fd5b4315 561#if USES_GENERICTLW
06077aaf 562 EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate)
a340b80d 563#endif
974a12f8 564 EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent)
a340b80d
VZ
565END_EVENT_TABLE()
566
567
974a12f8
RR
568void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent& WXUNUSED(event) )
569{
570 // Block the event so that the popup control does not get auto-resized.
571}
572
06077aaf 573void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent& event )
a340b80d
VZ
574{
575 // Relay keyboard event to the main child controls
06077aaf 576 wxWindowList children = m_combo->GetPopupWindow()->GetChildren();
a340b80d
VZ
577 wxWindowList::iterator node = children.begin();
578 wxWindow* child = (wxWindow*)*node;
174b5c87 579 child->GetEventHandler()->ProcessEvent(event);
a340b80d
VZ
580}
581
fd5b4315 582#if USES_GENERICTLW
06077aaf 583void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent& event )
a340b80d
VZ
584{
585 if ( !event.GetActive() )
586 {
587 // Tell combo control that we are dismissed.
a1d5aa93 588 m_combo->HidePopup(true);
a340b80d
VZ
589
590 event.Skip();
591 }
592}
593#endif
594
a340b80d
VZ
595
596// ----------------------------------------------------------------------------
597// wxComboPopup
598//
599// ----------------------------------------------------------------------------
600
601wxComboPopup::~wxComboPopup()
602{
603}
604
605void wxComboPopup::OnPopup()
606{
607}
608
609void wxComboPopup::OnDismiss()
610{
611}
612
8c61a9ea
JS
613wxComboCtrl* wxComboPopup::GetComboCtrl() const
614{
615 return wxStaticCast(m_combo, wxComboCtrl);
616}
617
a340b80d
VZ
618wxSize wxComboPopup::GetAdjustedSize( int minWidth,
619 int prefHeight,
620 int WXUNUSED(maxHeight) )
621{
622 return wxSize(minWidth,prefHeight);
623}
624
a57d600f 625void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo,
6d0ce565 626 wxDC& dc, const wxRect& rect )
a340b80d 627{
6d0ce565 628 if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl
a340b80d 629 {
118f5fbd 630 combo->PrepareBackground(dc,rect,0);
a340b80d 631
6d0ce565 632 dc.DrawText( combo->GetValue(),
0847e36e 633 rect.x + combo->m_marginLeft,
6d0ce565 634 (rect.height-dc.GetCharHeight())/2 + rect.y );
a340b80d
VZ
635 }
636}
637
6d0ce565
VZ
638void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
639{
640 DefaultPaintComboControl(m_combo,dc,rect);
641}
642
a340b80d
VZ
643void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
644{
645 event.Skip();
646}
647
c765e6fb
VS
648void wxComboPopup::OnComboCharEvent( wxKeyEvent& event )
649{
650 event.Skip();
651}
652
a340b80d
VZ
653void wxComboPopup::OnComboDoubleClick()
654{
655}
656
657void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) )
658{
659}
660
238b33ab
JS
661bool wxComboPopup::FindItem(const wxString& WXUNUSED(item),
662 wxString* WXUNUSED(trueItem))
663{
664 return true;
665}
666
a340b80d
VZ
667bool wxComboPopup::LazyCreate()
668{
669 return false;
670}
671
672void wxComboPopup::Dismiss()
673{
a1d5aa93 674 m_combo->HidePopup(true);
a340b80d
VZ
675}
676
df3c4a42
JS
677void wxComboPopup::DestroyPopup()
678{
679 // Here we make sure that the popup control's Destroy() gets called.
680 // This is necessary for the wxPersistentWindow to work properly.
681 wxWindow* popupCtrl = GetControl();
682 if ( popupCtrl )
683 {
684 // While all wxComboCtrl examples have m_popupInterface and
685 // popupCtrl as the same class (that will be deleted via the
686 // Destroy() call below), it is technically still possible to
687 // have implementations where they are in fact not same
688 // multiple-inherited class. Here we use C++ RTTI to check for
689 // this rare case.
690 #ifndef wxNO_RTTI
691 // It is probably better to delete m_popupInterface first, so
692 // that it retains access to its popup control window.
693 if ( dynamic_cast<void*>(this) !=
694 dynamic_cast<void*>(popupCtrl) )
695 delete this;
696 #endif
697 popupCtrl->Destroy();
698 }
699 else
700 {
701 delete this;
702 }
703}
704
a340b80d
VZ
705// ----------------------------------------------------------------------------
706// input handling
707// ----------------------------------------------------------------------------
708
709//
b445b6a7 710// This is pushed to the event handler queue of the child textctrl.
a340b80d
VZ
711//
712class wxComboBoxExtraInputHandler : public wxEvtHandler
713{
714public:
715
a57d600f 716 wxComboBoxExtraInputHandler( wxComboCtrlBase* combo )
a340b80d
VZ
717 : wxEvtHandler()
718 {
719 m_combo = combo;
720 }
d3c7fc99 721 virtual ~wxComboBoxExtraInputHandler() { }
a340b80d
VZ
722 void OnKey(wxKeyEvent& event);
723 void OnFocus(wxFocusEvent& event);
724
725protected:
a57d600f 726 wxComboCtrlBase* m_combo;
a340b80d
VZ
727
728private:
729 DECLARE_EVENT_TABLE()
730};
731
732
733BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler)
734 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey)
df6007a5
JS
735 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey)
736 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey)
a340b80d 737 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
c905c0d6 738 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
a340b80d
VZ
739END_EVENT_TABLE()
740
741
742void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event)
743{
b445b6a7
VZ
744 // Let the wxComboCtrl event handler have a go first.
745 wxComboCtrlBase* combo = m_combo;
a340b80d 746
110d27b3
JS
747 wxKeyEvent redirectedEvent(event);
748 redirectedEvent.SetId(combo->GetId());
749 redirectedEvent.SetEventObject(combo);
a340b80d 750
110d27b3
JS
751 if ( !combo->GetEventHandler()->ProcessEvent(redirectedEvent) )
752 {
753 // Don't let TAB through to the text ctrl - looks ugly
754 if ( event.GetKeyCode() != WXK_TAB )
755 event.Skip();
756 }
a340b80d
VZ
757}
758
a340b80d
VZ
759void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event)
760{
761 // FIXME: This code does run when control is clicked,
762 // yet on Windows it doesn't select all the text.
c905c0d6
VZ
763 if ( event.GetEventType() == wxEVT_SET_FOCUS &&
764 !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) )
a340b80d
VZ
765 {
766 if ( m_combo->GetTextCtrl() )
767 m_combo->GetTextCtrl()->SelectAll();
768 else
769 m_combo->SetSelection(-1,-1);
770 }
771
b445b6a7
VZ
772 // Send focus indication to parent.
773 // NB: This is needed for cases where the textctrl gets focus
774 // instead of its parent. While this may trigger multiple
775 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
776 // from combo's focus event handler), they should be quite
777 // harmless.
81ebc041
VZ
778 wxFocusEvent evt2(event);
779 evt2.SetId(m_combo->GetId());
b445b6a7
VZ
780 evt2.SetEventObject(m_combo);
781 m_combo->GetEventHandler()->ProcessEvent(evt2);
6d0ce565 782
a340b80d
VZ
783 event.Skip();
784}
785
786
787//
788// This is pushed to the event handler queue of the control in popup.
789//
790
06315578 791class wxComboPopupEvtHandler : public wxEvtHandler
a340b80d
VZ
792{
793public:
794
06315578 795 wxComboPopupEvtHandler( wxComboCtrlBase* combo )
a340b80d
VZ
796 : wxEvtHandler()
797 {
798 m_combo = combo;
799 m_beenInside = false;
4794626d
JS
800
801 // Let's make it so that the popup control will not receive mouse
802 // events until mouse left button has been up.
803 m_blockEventsToPopup = true;
a340b80d 804 }
06315578 805 virtual ~wxComboPopupEvtHandler() { }
a340b80d
VZ
806
807 void OnMouseEvent( wxMouseEvent& event );
808
a57d600f 809 // Called from wxComboCtrlBase::OnPopupDismiss
a340b80d
VZ
810 void OnPopupDismiss()
811 {
812 m_beenInside = false;
4794626d 813 m_blockEventsToPopup = true;
a340b80d
VZ
814 }
815
816protected:
a57d600f 817 wxComboCtrlBase* m_combo;
a340b80d 818
4794626d
JS
819 bool m_beenInside;
820 bool m_blockEventsToPopup;
a340b80d
VZ
821
822private:
823 DECLARE_EVENT_TABLE()
824};
825
826
06315578
JS
827BEGIN_EVENT_TABLE(wxComboPopupEvtHandler, wxEvtHandler)
828 EVT_MOUSE_EVENTS(wxComboPopupEvtHandler::OnMouseEvent)
a340b80d
VZ
829END_EVENT_TABLE()
830
831
06315578 832void wxComboPopupEvtHandler::OnMouseEvent( wxMouseEvent& event )
a340b80d
VZ
833{
834 wxPoint pt = event.GetPosition();
6d0ce565 835 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
a340b80d
VZ
836 int evtType = event.GetEventType();
837 bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y;
c905c0d6
VZ
838 bool relayToButton = false;
839
840 event.Skip();
a340b80d 841
4794626d 842 if ( !isInside || !m_combo->IsPopupShown() )
a340b80d 843 {
4794626d
JS
844 // Mouse is outside the popup or popup is not actually shown (yet)
845
846 if ( evtType == wxEVT_MOTION ||
847 evtType == wxEVT_LEFT_DOWN ||
848 evtType == wxEVT_LEFT_UP ||
849 evtType == wxEVT_RIGHT_DOWN )
a340b80d 850 {
4794626d 851 // Block motion and click events outside the popup
974a12f8 852 event.Skip(false);
a340b80d 853 }
4794626d
JS
854 }
855 else
856 {
857 // Mouse is inside the popup, which is fully shown
858
859 m_beenInside = true;
860
861 // Do not let the popup control respond to mouse events until
862 // mouse press used to display the popup has been lifted. This
863 // is important for users with slower mouse fingers or mouse
864 // drivers. Note that we have some redundancy here, just in
865 // case the popup is some native control that does not emit all
866 // mouse event types.
867 if ( evtType == wxEVT_MOTION )
a340b80d 868 {
4794626d 869 if ( m_blockEventsToPopup )
a340b80d 870 {
4794626d
JS
871 if ( event.LeftIsDown() )
872 event.Skip(false);
873 else
874 m_blockEventsToPopup = false;
a340b80d 875 }
4794626d
JS
876 }
877 else if ( evtType == wxEVT_LEFT_DOWN )
878 {
879 if ( m_blockEventsToPopup )
880 m_blockEventsToPopup = false;
881 }
882 else if ( evtType == wxEVT_LEFT_UP )
883 {
884 if ( m_blockEventsToPopup )
a340b80d 885 {
4794626d
JS
886 // On first left up, stop blocking mouse events (but still
887 // block this one)
888 m_blockEventsToPopup = false;
889 event.Skip(false);
db3d7543
JS
890
891 // Also, this button press was (probably) used to display
892 // the popup, so relay it back to the drop-down button
893 // (which supposedly originated it). This is necessary to
894 // refresh it properly.
895 relayToButton = true;
a340b80d 896 }
a340b80d 897 }
4794626d
JS
898 else if ( m_blockEventsToPopup )
899 {
900 event.Skip(false);
901 }
902 }
903
904 //
905 // Some mouse events to popup that happen outside it, before cursor
906 // has been inside the popup, need to be ignored by it but relayed to
907 // the dropbutton.
908 //
909 if ( evtType == wxEVT_LEFT_UP )
910 {
911 if ( !m_combo->IsPopupShown() )
912 {
913 event.Skip(false);
914 relayToButton = true;
915 }
916 else if ( !isInside && !m_beenInside )
917 {
918 // Popup is shown but the cursor is not inside, nor it has been
919 relayToButton = true;
920 }
a340b80d
VZ
921 }
922
c905c0d6
VZ
923 if ( relayToButton )
924 {
c905c0d6
VZ
925 wxWindow* btn = m_combo->GetButton();
926 if ( btn )
db3d7543
JS
927 btn->GetEventHandler()->ProcessEvent(event);
928 else
929 // Bypass the event handling mechanism. Using it would be
930 // confusing for the platform-specific wxComboCtrl
931 // implementations.
932 m_combo->HandleButtonMouseEvent(event, 0);
c905c0d6 933 }
a340b80d
VZ
934}
935
c905c0d6
VZ
936// ----------------------------------------------------------------------------
937// wxComboCtrlTextCtrl
938// ----------------------------------------------------------------------------
939
940class wxComboCtrlTextCtrl : public wxTextCtrl
941{
942public:
943 wxComboCtrlTextCtrl() : wxTextCtrl() { }
944 virtual ~wxComboCtrlTextCtrl() { }
945
946 virtual wxWindow *GetMainWindowOfCompositeControl()
947 {
948 wxComboCtrl* combo = (wxComboCtrl*) GetParent();
949
950 // Returning this instead of just 'parent' lets FindFocus work
951 // correctly even when parent control is a child of a composite
952 // generic control (as is case with wxGenericDatePickerCtrl).
953 return combo->GetMainWindowOfCompositeControl();
954 }
955};
956
a340b80d 957// ----------------------------------------------------------------------------
a57d600f 958// wxComboCtrlBase
a340b80d
VZ
959// ----------------------------------------------------------------------------
960
961
a57d600f 962BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl)
a57d600f
VZ
963 EVT_SIZE(wxComboCtrlBase::OnSizeEvent)
964 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent)
965 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent)
2dfa37d6 966 EVT_IDLE(wxComboCtrlBase::OnIdleEvent)
a57d600f 967 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
b445b6a7 968 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent)
c765e6fb 969 EVT_CHAR(wxComboCtrlBase::OnCharEvent)
a57d600f 970 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged)
a340b80d
VZ
971END_EVENT_TABLE()
972
973
a57d600f 974IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl)
a340b80d 975
a57d600f 976void wxComboCtrlBase::Init()
a340b80d 977{
d3b9f782
VZ
978 m_winPopup = NULL;
979 m_popup = NULL;
974a12f8 980 m_popupWinState = Hidden;
d3b9f782
VZ
981 m_btn = NULL;
982 m_text = NULL;
983 m_popupInterface = NULL;
a340b80d 984
06315578 985 m_popupEvtHandler = NULL;
d3b9f782 986 m_textEvtHandler = NULL;
a340b80d
VZ
987
988#if INSTALL_TOPLEV_HANDLER
d3b9f782 989 m_toplevEvtHandler = NULL;
a340b80d
VZ
990#endif
991
4427c0a3
RR
992 m_mainCtrlWnd = this;
993
a340b80d
VZ
994 m_heightPopup = -1;
995 m_widthMinPopup = -1;
996 m_anchorSide = 0;
997 m_widthCustomPaint = 0;
998 m_widthCustomBorder = 0;
999
1000 m_btnState = 0;
1001 m_btnWidDefault = 0;
1002 m_blankButtonBg = false;
ce968519 1003 m_ignoreEvtText = 0;
06077aaf 1004 m_popupWinType = POPUPWIN_NONE;
7dc234d6 1005 m_btnWid = m_btnHei = -1;
a340b80d
VZ
1006 m_btnSide = wxRIGHT;
1007 m_btnSpacingX = 0;
1008
1009 m_extLeft = 0;
1010 m_extRight = 0;
0847e36e 1011 m_marginLeft = -1;
a340b80d 1012 m_iFlags = 0;
1ac5cfc7 1013 m_textCtrlStyle = 0;
a340b80d 1014 m_timeCanAcceptClick = 0;
2dfa37d6
RD
1015
1016 m_resetFocus = false;
bed867e3 1017 m_hasTcBgCol = false;
a340b80d
VZ
1018}
1019
a57d600f 1020bool wxComboCtrlBase::Create(wxWindow *parent,
b61f4f77
WS
1021 wxWindowID id,
1022 const wxString& value,
1023 const wxPoint& pos,
1024 const wxSize& size,
1025 long style,
1026 const wxValidator& validator,
1027 const wxString& name)
a340b80d
VZ
1028{
1029 if ( !wxControl::Create(parent,
1030 id,
1031 pos,
1032 size,
1033 style | wxWANTS_CHARS,
1034 validator,
1035 name) )
1036 return false;
1037
1038 m_valueString = value;
1039
1040 // Get colours
1041 OnThemeChange();
0847e36e 1042 m_marginLeft = GetNativeTextIndent();
a340b80d 1043
a9e8bf2d
WS
1044 m_iFlags |= wxCC_IFLAG_CREATED;
1045
1046 // If x and y indicate valid size, wxSizeEvent won't be
df97a4ef 1047 // emitted automatically, so we need to add artificial one.
a9e8bf2d
WS
1048 if ( size.x > 0 && size.y > 0 )
1049 {
1050 wxSizeEvent evt(size,GetId());
54580df4 1051 evt.SetEventObject(this);
a9e8bf2d
WS
1052 GetEventHandler()->AddPendingEvent(evt);
1053 }
1054
a340b80d
VZ
1055 return true;
1056}
1057
b445b6a7 1058void wxComboCtrlBase::InstallInputHandlers()
a340b80d 1059{
b445b6a7 1060 if ( m_text )
a340b80d
VZ
1061 {
1062 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
1063 m_text->PushEventHandler(m_textEvtHandler);
1064 }
a340b80d
VZ
1065}
1066
42a3ecf5 1067void
fda62793 1068wxComboCtrlBase::CreateTextCtrl(int style)
a340b80d
VZ
1069{
1070 if ( !(m_windowStyle & wxCB_READONLY) )
1071 {
8e9ec723
RR
1072 if ( m_text )
1073 m_text->Destroy();
1074
42a3ecf5
VZ
1075 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
1076 // not used by the wxPropertyGrid and therefore the tab is processed by
1077 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
1078 // navigation event is then sent to the wrong window.
1ac5cfc7 1079 style |= wxTE_PROCESS_TAB | m_textCtrlStyle;
42a3ecf5 1080
5d95cab8 1081 if ( HasFlag(wxTE_PROCESS_ENTER) )
42a3ecf5
VZ
1082 style |= wxTE_PROCESS_ENTER;
1083
ce968519
RR
1084 // Ignore EVT_TEXT generated by the constructor (but only
1085 // if the event redirector already exists)
1086 // NB: This must be " = 1" instead of "++";
1087 if ( m_textEvtHandler )
1088 m_ignoreEvtText = 1;
1089 else
1090 m_ignoreEvtText = 0;
1091
c905c0d6
VZ
1092 m_text = new wxComboCtrlTextCtrl();
1093 m_text->Create(this, wxID_ANY, m_valueString,
1094 wxDefaultPosition, wxSize(10,-1),
fda62793 1095 style);
25b7a51f
JS
1096
1097 // Connecting the events is currently the most reliable way
1098 wxWindowID id = m_text->GetId();
1099 m_text->Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
1100 wxCommandEventHandler(wxComboCtrlBase::OnTextCtrlEvent),
1101 NULL, this);
1102 m_text->Connect(id, wxEVT_COMMAND_TEXT_ENTER,
1103 wxCommandEventHandler(wxComboCtrlBase::OnTextCtrlEvent),
1104 NULL, this);
1105
107defe3 1106 m_text->SetHint(m_hintText);
a340b80d
VZ
1107 }
1108}
1109
a57d600f 1110void wxComboCtrlBase::OnThemeChange()
a340b80d 1111{
10ba2677
JS
1112 // Because wxComboCtrl has transparent parts on most platforms, we
1113 // don't want to touch the actual background colour. Instead, we just
1114 // usually re-obtain m_tcBgCol here.
1115
1116#if defined(__WXMSW__) || defined(__WXGTK__)
2ec335db 1117 wxVisualAttributes vattrs = wxComboBox::GetClassDefaultAttributes();
10ba2677 1118#else
2ec335db
JS
1119 wxVisualAttributes vattrs;
1120 vattrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1121 vattrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
10ba2677
JS
1122#endif
1123
bed867e3 1124 if ( !m_hasTcBgCol )
10ba2677 1125 m_tcBgCol = vattrs.colBg;
2ec335db 1126
10ba2677 1127#ifndef __WXMAC__
2ec335db
JS
1128 // Only change the colours if application has not specified
1129 // custom ones.
1130 if ( !m_hasFgCol )
1131 {
1132 SetOwnForegroundColour(vattrs.colFg);
2ec335db 1133 }
10ba2677 1134 if ( !HasTransparentBackground() )
3b9ba64c 1135 {
10ba2677 1136 SetOwnBackgroundColour(GetParent()->GetBackgroundColour());
3b9ba64c 1137 }
2ec335db 1138#endif // !__WXMAC__
a340b80d
VZ
1139}
1140
a57d600f 1141wxComboCtrlBase::~wxComboCtrlBase()
a340b80d
VZ
1142{
1143 if ( HasCapture() )
1144 ReleaseMouse();
1145
a340b80d
VZ
1146#if INSTALL_TOPLEV_HANDLER
1147 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
d3b9f782 1148 m_toplevEvtHandler = NULL;
a340b80d
VZ
1149#endif
1150
7ca4ac63 1151 DestroyPopup();
a340b80d 1152
a340b80d
VZ
1153 if ( m_text )
1154 m_text->RemoveEventHandler(m_textEvtHandler);
1155
1156 delete m_textEvtHandler;
a340b80d
VZ
1157}
1158
1159
1160// ----------------------------------------------------------------------------
1161// geometry stuff
1162// ----------------------------------------------------------------------------
1163
1164// Recalculates button and textctrl areas
a57d600f 1165void wxComboCtrlBase::CalculateAreas( int btnWidth )
a340b80d
VZ
1166{
1167 wxSize sz = GetClientSize();
1168 int customBorder = m_widthCustomBorder;
a340b80d
VZ
1169 int btnBorder; // border for button only
1170
87419e97
VZ
1171 // check if button should really be outside the border: we'll do it it if
1172 // its platform default or bitmap+pushbutton background is used, but not if
1173 // there is vertical size adjustment or horizontal spacing.
1174 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
a1b806b9 1175 (m_bmpNormal.IsOk() && m_blankButtonBg) ) &&
87419e97 1176 m_btnSpacingX == 0 &&
7dc234d6 1177 m_btnHei <= 0 )
a340b80d 1178 {
a340b80d
VZ
1179 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
1180 btnBorder = 0;
1181 }
c905c0d6 1182 else if ( (m_iFlags & wxCC_BUTTON_COVERS_BORDER) &&
a1b806b9 1183 m_btnSpacingX == 0 && !m_bmpNormal.IsOk() )
c905c0d6
VZ
1184 {
1185 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1186 btnBorder = 0;
1187 }
a340b80d
VZ
1188 else
1189 {
a340b80d
VZ
1190 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1191 btnBorder = customBorder;
1192 }
1193
1194 // Defaul indentation
0847e36e
JS
1195 if ( m_marginLeft < 0 )
1196 m_marginLeft = GetNativeTextIndent();
a340b80d
VZ
1197
1198 int butWidth = btnWidth;
1199
1200 if ( butWidth <= 0 )
1201 butWidth = m_btnWidDefault;
1202 else
1203 m_btnWidDefault = butWidth;
1204
1205 if ( butWidth <= 0 )
1206 return;
1207
a9e8bf2d
WS
1208 int butHeight = sz.y - btnBorder*2;
1209
a340b80d 1210 // Adjust button width
7dc234d6 1211 if ( m_btnWid > 0 )
a340b80d 1212 butWidth = m_btnWid;
a9e8bf2d
WS
1213 else
1214 {
1215 // Adjust button width to match aspect ratio
1216 // (but only if control is smaller than best size).
1217 int bestHeight = GetBestSize().y;
1218 int height = GetSize().y;
a340b80d 1219
a9e8bf2d
WS
1220 if ( height < bestHeight )
1221 {
1222 // Make very small buttons square, as it makes
1223 // them accommodate arrow image better and still
1224 // looks decent.
1225 if ( height > 18 )
1226 butWidth = (height*butWidth)/bestHeight;
1227 else
1228 butWidth = butHeight;
1229 }
1230 }
a340b80d
VZ
1231
1232 // Adjust button height
7dc234d6 1233 if ( m_btnHei > 0 )
a340b80d
VZ
1234 butHeight = m_btnHei;
1235
1236 // Use size of normal bitmap if...
1237 // It is larger
1238 // OR
1239 // button width is set to default and blank button bg is not drawn
a1b806b9 1240 if ( m_bmpNormal.IsOk() )
a340b80d
VZ
1241 {
1242 int bmpReqWidth = m_bmpNormal.GetWidth();
1243 int bmpReqHeight = m_bmpNormal.GetHeight();
1244
1245 // If drawing blank button background, we need to add some margin.
1246 if ( m_blankButtonBg )
1247 {
1248 bmpReqWidth += BMP_BUTTON_MARGIN*2;
1249 bmpReqHeight += BMP_BUTTON_MARGIN*2;
1250 }
1251
1252 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
1253 butWidth = bmpReqWidth;
1254 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
1255 butHeight = bmpReqHeight;
1256
1257 // Need to fix height?
1258 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
1259 {
1260 int newY = butHeight+(customBorder*2);
a5bbd1cc 1261 SetClientSize(wxDefaultCoord,newY);
a1b806b9 1262 if ( m_bmpNormal.IsOk() || m_btnArea.width != butWidth || m_btnArea.height != butHeight )
640a91ab
PC
1263 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1264 else
1265 m_iFlags &= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
c905c0d6 1266
a340b80d
VZ
1267 sz.y = newY;
1268 }
1269 }
1270
1271 int butAreaWid = butWidth + (m_btnSpacingX*2);
1272
1273 m_btnSize.x = butWidth;
1274 m_btnSize.y = butHeight;
1275
1276 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
2dfa37d6 1277 m_btnArea.y = btnBorder + FOCUS_RING;
a340b80d 1278 m_btnArea.width = butAreaWid;
2dfa37d6 1279 m_btnArea.height = sz.y - ((btnBorder+FOCUS_RING)*2);
a340b80d 1280
2dfa37d6
RD
1281 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder + FOCUS_RING;
1282 m_tcArea.y = customBorder + FOCUS_RING;
1283 m_tcArea.width = sz.x - butAreaWid - (customBorder*2) - (FOCUS_RING*2);
1284 m_tcArea.height = sz.y - ((customBorder+FOCUS_RING)*2);
a340b80d
VZ
1285
1286/*
1287 if ( m_text )
1288 {
1289 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1290 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1291 }
1292*/
1293}
1294
a57d600f 1295void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
a340b80d
VZ
1296{
1297 if ( !m_text )
1298 return;
1299
1300 wxSize sz = GetClientSize();
a340b80d 1301
f85f5ddf 1302 int customBorder = m_widthCustomBorder;
a340b80d
VZ
1303 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
1304 {
4bcf25e9
JS
1305 int x;
1306
1307 if ( !m_widthCustomPaint )
1308 {
1309 // No special custom paint area - we can use 0 left margin
1310 // with wxTextCtrl.
1311 if ( m_text->SetMargins(0) )
1312 textCtrlXAdjust = 0;
1313 x = m_tcArea.x + m_marginLeft + textCtrlXAdjust;
1314 }
1315 else
1316 {
1317 // There is special custom paint area - it is better to
1318 // use some margin with the wxTextCtrl.
1319 m_text->SetMargins(m_marginLeft);
ce00f59b 1320 x = m_tcArea.x + m_widthCustomPaint +
4bcf25e9
JS
1321 m_marginLeft + textCtrlXAdjust;
1322 }
0847e36e 1323
4bcf25e9 1324 // Centre textctrl vertically, if needed
0847e36e 1325#if !TEXTCTRL_TEXT_CENTERED
a340b80d 1326 int tcSizeY = m_text->GetBestSize().y;
0847e36e
JS
1327 int diff0 = sz.y - tcSizeY;
1328 int y = textCtrlYAdjust + (diff0/2);
1329#else
1330 wxUnusedVar(textCtrlYAdjust);
1331 int y = 0;
1332#endif
a340b80d
VZ
1333
1334 if ( y < customBorder )
1335 y = customBorder;
1336
0847e36e
JS
1337 m_text->SetSize(x,
1338 y,
1339 m_tcArea.width - m_tcArea.x - x,
0847e36e 1340 -1 );
a340b80d
VZ
1341
1342 // Make sure textctrl doesn't exceed the bottom custom border
1343 wxSize tsz = m_text->GetSize();
0847e36e
JS
1344 int diff1 = (y + tsz.y) - (sz.y - customBorder);
1345 if ( diff1 >= 0 )
a340b80d 1346 {
0847e36e 1347 tsz.y = tsz.y - diff1 - 1;
a340b80d
VZ
1348 m_text->SetSize(tsz);
1349 }
1350 }
1351 else
1352 {
0847e36e 1353 // If it has border, have textctrl fill the entire text field.
7e4545b8 1354 m_text->SetSize( m_tcArea.x + m_widthCustomPaint,
2dfa37d6
RD
1355 m_tcArea.y,
1356 m_tcArea.width - m_widthCustomPaint,
1357 m_tcArea.height );
a340b80d
VZ
1358 }
1359}
1360
a57d600f 1361wxSize wxComboCtrlBase::DoGetBestSize() const
a340b80d
VZ
1362{
1363 wxSize sizeText(150,0);
1364
1365 if ( m_text )
1366 sizeText = m_text->GetBestSize();
1367
1368 // TODO: Better method to calculate close-to-native control height.
1369
1370 int fhei;
a1b806b9 1371 if ( m_font.IsOk() )
a340b80d 1372 fhei = (m_font.GetPointSize()*2) + 5;
a1b806b9 1373 else if ( wxNORMAL_FONT->IsOk() )
a340b80d
VZ
1374 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
1375 else
1376 fhei = sizeText.y + 4;
1377
1378 // Need to force height to accomodate bitmap?
1379 int btnSizeY = m_btnSize.y;
a1b806b9 1380 if ( m_bmpNormal.IsOk() && fhei < btnSizeY )
a340b80d
VZ
1381 fhei = btnSizeY;
1382
1383 // Control height doesn't depend on border
1384/*
1385 // Add border
1386 int border = m_windowStyle & wxBORDER_MASK;
1387 if ( border == wxSIMPLE_BORDER )
1388 fhei += 2;
1389 else if ( border == wxNO_BORDER )
1390 fhei += (m_widthCustomBorder*2);
1391 else
1392 // Sunken etc.
1393 fhei += 4;
1394*/
1395
1396 // Final adjustments
1397#ifdef __WXGTK__
1398 fhei += 1;
1399#endif
1400
c66ed668
RD
1401#ifdef __WXMAC__
1402 // these are the numbers from the HIG:
1403 switch ( m_windowVariant )
1404 {
1405 case wxWINDOW_VARIANT_NORMAL:
1406 default :
1407 fhei = 22;
1408 break;
1409 case wxWINDOW_VARIANT_SMALL:
1410 fhei = 19;
1411 break;
1412 case wxWINDOW_VARIANT_MINI:
1413 fhei = 15;
1414 break;
1415 }
1416#endif
a340b80d 1417
2dfa37d6
RD
1418 fhei += 2 * FOCUS_RING;
1419 int width = sizeText.x + FOCUS_RING + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH;
1420
1421 wxSize ret(width, fhei);
a340b80d
VZ
1422 CacheBestSize(ret);
1423 return ret;
1424}
1425
a57d600f 1426void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
a340b80d
VZ
1427{
1428 if ( !IsCreated() )
1429 return;
1430
a57d600f 1431 // defined by actual wxComboCtrls
a340b80d
VZ
1432 OnResize();
1433
1434 event.Skip();
1435}
1436
1437// ----------------------------------------------------------------------------
1438// standard operations
1439// ----------------------------------------------------------------------------
1440
a57d600f 1441bool wxComboCtrlBase::Enable(bool enable)
a340b80d
VZ
1442{
1443 if ( !wxControl::Enable(enable) )
1444 return false;
1445
1446 if ( m_btn )
1447 m_btn->Enable(enable);
1448 if ( m_text )
1449 m_text->Enable(enable);
004867db 1450
4df230b8 1451 Refresh();
a340b80d
VZ
1452
1453 return true;
1454}
1455
a57d600f 1456bool wxComboCtrlBase::Show(bool show)
a340b80d
VZ
1457{
1458 if ( !wxControl::Show(show) )
1459 return false;
1460
1461 if (m_btn)
1462 m_btn->Show(show);
1463
1464 if (m_text)
1465 m_text->Show(show);
1466
1467 return true;
1468}
1469
a57d600f 1470bool wxComboCtrlBase::SetFont ( const wxFont& font )
a340b80d
VZ
1471{
1472 if ( !wxControl::SetFont(font) )
1473 return false;
1474
4bcf25e9
JS
1475 if ( m_text )
1476 {
1477 // Without hiding the wxTextCtrl there would be some
1478 // visible 'flicker' (at least on Windows XP).
1479 m_text->Hide();
a340b80d 1480 m_text->SetFont(font);
4bcf25e9
JS
1481 OnResize();
1482 m_text->Show();
1483 }
a340b80d
VZ
1484
1485 return true;
1486}
1487
1488#if wxUSE_TOOLTIPS
a57d600f 1489void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
a340b80d
VZ
1490{
1491 wxControl::DoSetToolTip(tooltip);
1492
1493 // Set tool tip for button and text box
1494 if ( tooltip )
1495 {
1496 const wxString &tip = tooltip->GetTip();
1497 if ( m_text ) m_text->SetToolTip(tip);
1498 if ( m_btn ) m_btn->SetToolTip(tip);
1499 }
1500 else
1501 {
d3b9f782
VZ
1502 if ( m_text ) m_text->SetToolTip( NULL );
1503 if ( m_btn ) m_btn->SetToolTip( NULL );
a340b80d
VZ
1504 }
1505}
1506#endif // wxUSE_TOOLTIPS
1507
5f540750
JS
1508bool wxComboCtrlBase::SetForegroundColour(const wxColour& colour)
1509{
1510 if ( wxControl::SetForegroundColour(colour) )
1511 {
1512 if ( m_text )
1513 m_text->SetForegroundColour(colour);
1514 return true;
1515 }
1516 return false;
1517}
1518
1519bool wxComboCtrlBase::SetBackgroundColour(const wxColour& colour)
1520{
10ba2677
JS
1521 if ( m_text )
1522 m_text->SetBackgroundColour(colour);
1523 m_tcBgCol = colour;
bed867e3 1524 m_hasTcBgCol = true;
10ba2677 1525 return true;
5f540750 1526}
10ba2677
JS
1527
1528wxColour wxComboCtrlBase::GetBackgroundColour() const
1529{
1530 if ( m_text )
1531 return m_text->GetBackgroundColour();
1532 return m_tcBgCol;
1533}
1534
a340b80d
VZ
1535// ----------------------------------------------------------------------------
1536// painting
1537// ----------------------------------------------------------------------------
1538
118f5fbd
RR
1539#if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1540// prepare combo box background on area in a way typical on platform
1541void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
a340b80d
VZ
1542{
1543 wxSize sz = GetClientSize();
1544 bool isEnabled;
c905c0d6 1545 bool doDrawFocusRect; // also selected
a340b80d
VZ
1546
1547 // For smaller size control (and for disabled background) use less spacing
1548 int focusSpacingX;
1549 int focusSpacingY;
1550
1551 if ( !(flags & wxCONTROL_ISSUBMENU) )
1552 {
1553 // Drawing control
1554 isEnabled = IsEnabled();
640a91ab 1555 doDrawFocusRect = ShouldDrawFocus() && !(m_iFlags & wxCC_FULL_BUTTON);
a340b80d
VZ
1556
1557 // Windows-style: for smaller size control (and for disabled background) use less spacing
1558 focusSpacingX = isEnabled ? 2 : 1;
1559 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1560 }
1561 else
1562 {
1563 // Drawing a list item
1564 isEnabled = true; // they are never disabled
640a91ab 1565 doDrawFocusRect = (flags & wxCONTROL_SELECTED) != 0;
a340b80d
VZ
1566
1567 focusSpacingX = 0;
1568 focusSpacingY = 0;
1569 }
1570
1571 // Set the background sub-rectangle for selection, disabled etc
1572 wxRect selRect(rect);
1573 selRect.y += focusSpacingY;
1574 selRect.height -= (focusSpacingY*2);
8e5ec129
WS
1575
1576 int wcp = 0;
1577
1578 if ( !(flags & wxCONTROL_ISSUBMENU) )
1579 wcp += m_widthCustomPaint;
1580
1581 selRect.x += wcp + focusSpacingX;
1582 selRect.width -= wcp + (focusSpacingX*2);
a340b80d
VZ
1583
1584 wxColour bgCol;
3b9ba64c
JS
1585 wxColour fgCol;
1586
c905c0d6 1587 bool doDrawSelRect = true;
a340b80d 1588
3b9ba64c
JS
1589 // Determine foreground colour
1590 if ( isEnabled )
1591 {
1592 if ( doDrawFocusRect )
1593 {
1594 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1595 }
1596 else if ( m_hasFgCol )
1597 {
1598 // Honour the custom foreground colour
1599 fgCol = GetForegroundColour();
1600 }
1601 else
1602 {
1603 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1604 }
1605 }
1606 else
1607 {
1608 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
1609 }
1610
1611 // Determine background colour
a340b80d
VZ
1612 if ( isEnabled )
1613 {
c905c0d6 1614 if ( doDrawFocusRect )
a340b80d 1615 {
a340b80d
VZ
1616 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1617 }
bed867e3 1618 else if ( m_hasTcBgCol )
3b9ba64c
JS
1619 {
1620 // Honour the custom background colour
bed867e3 1621 bgCol = m_tcBgCol;
3b9ba64c 1622 }
a340b80d
VZ
1623 else
1624 {
2dfa37d6 1625#ifndef __WXMAC__ // see note in OnThemeChange
c905c0d6 1626 doDrawSelRect = false;
a340b80d 1627 bgCol = GetBackgroundColour();
2dfa37d6
RD
1628#else
1629 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1630#endif
a340b80d
VZ
1631 }
1632 }
1633 else
1634 {
2dfa37d6 1635#ifndef __WXMAC__ // see note in OnThemeChange
a340b80d 1636 bgCol = GetBackgroundColour();
2dfa37d6
RD
1637#else
1638 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1639#endif
a340b80d
VZ
1640 }
1641
3b9ba64c 1642 dc.SetTextForeground( fgCol );
a340b80d 1643 dc.SetBrush( bgCol );
c905c0d6
VZ
1644 if ( doDrawSelRect )
1645 {
1646 dc.SetPen( bgCol );
1647 dc.DrawRectangle( selRect );
1648 }
118f5fbd
RR
1649
1650 // Don't clip exactly to the selection rectangle so we can draw
1651 // to the non-selected area in front of it.
1652 wxRect clipRect(rect.x,rect.y,
1653 (selRect.x+selRect.width)-rect.x,rect.height);
1654 dc.SetClippingRegion(clipRect);
a340b80d 1655}
118f5fbd
RR
1656#else
1657// Save the library size a bit for platforms that re-implement this.
1658void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1659{
1660}
1661#endif
a340b80d 1662
c905c0d6 1663void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags )
a340b80d
VZ
1664{
1665 int drawState = m_btnState;
1666
c905c0d6
VZ
1667 if ( (m_iFlags & wxCC_BUTTON_STAYS_DOWN) &&
1668 GetPopupWindowState() >= Animating )
a340b80d 1669 drawState |= wxCONTROL_PRESSED;
a340b80d
VZ
1670
1671 wxRect drawRect(rect.x+m_btnSpacingX,
1672 rect.y+((rect.height-m_btnSize.y)/2),
1673 m_btnSize.x,
1674 m_btnSize.y);
1675
1676 // Make sure area is not larger than the control
1677 if ( drawRect.y < rect.y )
1678 drawRect.y = rect.y;
1679 if ( drawRect.height > rect.height )
1680 drawRect.height = rect.height;
1681
1682 bool enabled = IsEnabled();
1683
1684 if ( !enabled )
1685 drawState |= wxCONTROL_DISABLED;
1686
3e4e0908
JS
1687 // Need to clear button background even if m_btn is present
1688 // and also when using custom bitmap for the button
1689 if ( (flags & Button_PaintBackground) &&
1690 (!HasTransparentBackground() ||
1691 !(m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE)) )
a340b80d 1692 {
3e4e0908 1693 wxColour bgCol;
c905c0d6 1694
3e4e0908
JS
1695 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1696 bgCol = GetParent()->GetBackgroundColour();
1697 else
1698 bgCol = GetBackgroundColour();
b61f4f77 1699
3e4e0908
JS
1700 dc.SetBrush(bgCol);
1701 dc.SetPen(bgCol);
1702 dc.DrawRectangle(rect);
1703 }
b61f4f77 1704
a1b806b9 1705 if ( !m_bmpNormal.IsOk() )
3e4e0908
JS
1706 {
1707 if ( flags & Button_BitmapOnly )
1708 return;
a340b80d
VZ
1709
1710 // Draw standard button
1711 wxRendererNative::Get().DrawComboBoxDropButton(this,
1712 dc,
1713 drawRect,
1714 drawState);
1715 }
1716 else
1717 {
1718 // Draw bitmap
1719
1720 wxBitmap* pBmp;
1721
1722 if ( !enabled )
1723 pBmp = &m_bmpDisabled;
1724 else if ( m_btnState & wxCONTROL_PRESSED )
1725 pBmp = &m_bmpPressed;
1726 else if ( m_btnState & wxCONTROL_CURRENT )
1727 pBmp = &m_bmpHover;
1728 else
1729 pBmp = &m_bmpNormal;
1730
1731 if ( m_blankButtonBg )
1732 {
c905c0d6
VZ
1733 if ( !(flags & Button_BitmapOnly) )
1734 {
1735 wxRendererNative::Get().DrawPushButton(this,
1736 dc,
1737 drawRect,
1738 drawState);
1739 }
a340b80d 1740 }
a340b80d
VZ
1741
1742 // Draw bitmap centered in drawRect
1743 dc.DrawBitmap(*pBmp,
1744 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1745 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1746 true);
1747 }
1748}
1749
a57d600f 1750void wxComboCtrlBase::RecalcAndRefresh()
a340b80d
VZ
1751{
1752 if ( IsCreated() )
1753 {
1754 wxSizeEvent evt(GetSize(),GetId());
54580df4 1755 evt.SetEventObject(this);
a340b80d
VZ
1756 GetEventHandler()->ProcessEvent(evt);
1757 Refresh();
1758 }
1759}
1760
a340b80d
VZ
1761// ----------------------------------------------------------------------------
1762// miscellaneous event handlers
1763// ----------------------------------------------------------------------------
1764
a57d600f 1765void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
a340b80d 1766{
8fd38e33
JS
1767 // Avoid infinite recursion
1768 if ( event.GetEventObject() == this )
1769 {
1770 event.Skip();
1771 return;
1772 }
1773
ce968519
RR
1774 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1775 {
1776 if ( m_ignoreEvtText > 0 )
1777 {
1778 m_ignoreEvtText--;
1779 return;
1780 }
1781 }
1782
8fd38e33 1783 // For safety, completely re-create a new wxCommandEvent
63722a74
JS
1784 wxCommandEvent evt2(event);
1785 evt2.SetId(GetId());
8fd38e33 1786 evt2.SetEventObject(this);
8fd38e33 1787 HandleWindowEvent(evt2);
26844be5
JS
1788
1789 event.StopPropagation();
a340b80d
VZ
1790}
1791
1792// call if cursor is on button area or mouse is captured for the button
a57d600f 1793bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
c905c0d6 1794 int flags )
a340b80d
VZ
1795{
1796 int type = event.GetEventType();
1797
1798 if ( type == wxEVT_MOTION )
1799 {
c905c0d6
VZ
1800 if ( (flags & wxCC_MF_ON_BUTTON) &&
1801 IsPopupWindowState(Hidden) )
a340b80d
VZ
1802 {
1803 if ( !(m_btnState & wxCONTROL_CURRENT) )
1804 {
1805 // Mouse hover begins
1806 m_btnState |= wxCONTROL_CURRENT;
1807 if ( HasCapture() ) // Retain pressed state.
1808 m_btnState |= wxCONTROL_PRESSED;
1809 Refresh();
1810 }
1811 }
1812 else if ( (m_btnState & wxCONTROL_CURRENT) )
1813 {
1814 // Mouse hover ends
1815 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1816 Refresh();
1817 }
1818 }
974a12f8 1819 else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK )
a340b80d 1820 {
1efad474 1821 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
a340b80d 1822 {
1efad474
RR
1823 m_btnState |= wxCONTROL_PRESSED;
1824 Refresh();
a340b80d 1825
1efad474
RR
1826 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1827 OnButtonClick();
1828 else
1829 // If showing popup now, do not capture mouse or there will be interference
1830 CaptureMouse();
a340b80d 1831 }
a340b80d
VZ
1832 }
1833 else if ( type == wxEVT_LEFT_UP )
1834 {
1835
1836 // Only accept event if mouse was left-press was previously accepted
1837 if ( HasCapture() )
1838 ReleaseMouse();
1839
1840 if ( m_btnState & wxCONTROL_PRESSED )
1841 {
1842 // If mouse was inside, fire the click event.
1843 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1844 {
1efad474 1845 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
a340b80d
VZ
1846 OnButtonClick();
1847 }
1848
1849 m_btnState &= ~(wxCONTROL_PRESSED);
1850 Refresh();
1851 }
1852 }
1853 else if ( type == wxEVT_LEAVE_WINDOW )
1854 {
1855 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1856 {
1857 m_btnState &= ~(wxCONTROL_CURRENT);
1858
1859 // Mouse hover ends
974a12f8 1860 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
1861 {
1862 m_btnState &= ~(wxCONTROL_PRESSED);
1863 Refresh();
1864 }
1865 }
1866 }
1867 else
1868 return false;
1869
c905c0d6
VZ
1870 // Never have 'hot' state when popup is being shown
1871 // (this is mostly needed because of the animation).
1872 if ( !IsPopupWindowState(Hidden) )
1873 m_btnState &= ~wxCONTROL_CURRENT;
1874
a340b80d
VZ
1875 return true;
1876}
1877
a340b80d 1878// returns true if event was consumed or filtered
a57d600f 1879bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
b104d1f0 1880 int WXUNUSED(flags) )
a340b80d
VZ
1881{
1882 wxLongLong t = ::wxGetLocalTimeMillis();
1883 int evtType = event.GetEventType();
1884
fd5b4315 1885#if USES_WXPOPUPWINDOW || USES_GENERICTLW
06077aaf 1886 if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW )
c667b518 1887 {
974a12f8 1888 if ( IsPopupWindowState(Visible) &&
06077aaf
VZ
1889 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1890 {
a1d5aa93 1891 HidePopup(true);
06077aaf
VZ
1892 return true;
1893 }
c667b518
VZ
1894 }
1895#endif
1896
c905c0d6 1897 // Filter out clicks on button immediately after popup dismiss
a340b80d
VZ
1898 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1899 {
1900 event.SetEventType(0);
1901 return true;
1902 }
1903
1904 return false;
1905}
1906
a57d600f 1907void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
a340b80d
VZ
1908{
1909 int evtType = event.GetEventType();
1910
1911 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1912 (m_windowStyle & wxCB_READONLY) )
1913 {
974a12f8 1914 if ( GetPopupWindowState() >= Animating )
a340b80d 1915 {
06077aaf 1916 #if USES_WXPOPUPWINDOW
a340b80d 1917 // Click here always hides the popup.
06077aaf 1918 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
a1d5aa93 1919 HidePopup(true);
a340b80d
VZ
1920 #endif
1921 }
1922 else
1923 {
1924 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1925 {
1926 // In read-only mode, clicking the text is the
1927 // same as clicking the button.
1928 OnButtonClick();
1929 }
1930 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1931 {
1932 //if ( m_popupInterface->CycleValue() )
1933 // Refresh();
1934 if ( m_popupInterface )
1935 m_popupInterface->OnComboDoubleClick();
1936 }
1937 }
1938 }
f4084513 1939 else if ( evtType == wxEVT_MOUSEWHEEL )
a340b80d 1940 {
f4084513
VS
1941 if ( IsPopupShown() )
1942 {
1943 // relay (some) mouse events to the popup
174b5c87 1944 m_popup->GetEventHandler()->ProcessEvent(event);
f4084513
VS
1945 }
1946 else if ( event.GetWheelAxis() == 0 &&
1947 event.GetWheelRotation() != 0 &&
1948 event.GetModifiers() == 0 )
1949 {
1950 // Translate mousewheel actions into key up/down. This is
1951 // the simplest way of getting native behaviour: scrolling the
1952 // wheel moves selection up/down by one item.
1953 wxKeyEvent kevent(wxEVT_KEY_DOWN);
1954 kevent.m_keyCode = event.GetWheelRotation() > 0
1955 ? WXK_UP
1956 : WXK_DOWN;
174b5c87 1957 GetEventHandler()->ProcessEvent(kevent);
f4084513
VS
1958 }
1959 else
1960 {
1961 event.Skip();
1962 }
a340b80d
VZ
1963 }
1964 else if ( evtType )
f4084513 1965 {
a340b80d 1966 event.Skip();
f4084513 1967 }
a340b80d
VZ
1968}
1969
b445b6a7 1970void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
a340b80d 1971{
b445b6a7
VZ
1972 if ( IsPopupShown() )
1973 {
1974 // pass it to the popped up control
174b5c87 1975 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
b445b6a7
VZ
1976 }
1977 else // no popup
1978 {
bbb86904
JS
1979 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
1980
1981 if ( mainCtrl->GetParent()->HasFlag(wxTAB_TRAVERSAL) )
1982 {
1983 if ( mainCtrl->HandleAsNavigationKey(event) )
1984 return;
1985 }
b445b6a7
VZ
1986
1987 if ( IsKeyPopupToggle(event) )
1988 {
1989 OnButtonClick();
1990 return;
1991 }
1992
1993 int comboStyle = GetWindowStyle();
1994 wxComboPopup* popupInterface = GetPopupControl();
1995
1996 if ( !popupInterface )
1997 {
1998 event.Skip();
1999 return;
2000 }
2001
f029f1d1
VS
2002 int keycode = event.GetKeyCode();
2003
b445b6a7
VZ
2004 if ( (comboStyle & wxCB_READONLY) ||
2005 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
2006 {
2007 popupInterface->OnComboKeyEvent(event);
2008 }
2009 else
2010 event.Skip();
2011 }
2012}
2013
c765e6fb
VS
2014void wxComboCtrlBase::OnCharEvent(wxKeyEvent& event)
2015{
2016 if ( IsPopupShown() )
2017 {
2018 // pass it to the popped up control
2019 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
2020 }
2021 else // no popup
2022 {
2023 wxComboPopup* popupInterface = GetPopupControl();
2024 if ( popupInterface )
2025 {
2026 popupInterface->OnComboCharEvent(event);
2027 }
2028 else
2029 {
2030 event.Skip();
2031 }
2032 }
2033}
2034
b445b6a7
VZ
2035void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
2036{
4f8870e3
RD
2037// On Mac, this leads to infinite recursion and eventually a crash
2038#ifndef __WXMAC__
b445b6a7 2039 if ( event.GetEventType() == wxEVT_SET_FOCUS )
a340b80d 2040 {
9727e8a3
JS
2041 wxWindow* tc = GetTextCtrl();
2042 if ( tc && tc != DoFindFocus() )
c905c0d6 2043 {
9727e8a3 2044 tc->SetFocus();
c905c0d6 2045 }
a340b80d 2046 }
b445b6a7
VZ
2047
2048 Refresh();
4f8870e3 2049#endif
a340b80d
VZ
2050}
2051
2dfa37d6
RD
2052void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) )
2053{
2054 if ( m_resetFocus )
2055 {
2056 m_resetFocus = false;
2057 wxWindow* tc = GetTextCtrl();
2058 if ( tc )
2059 tc->SetFocus();
2060 }
2061}
2062
a57d600f 2063void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
a340b80d
VZ
2064{
2065 OnThemeChange();
0847e36e
JS
2066 // left margin may also have changed
2067 if ( !(m_iFlags & wxCC_IFLAG_LEFT_MARGIN_SET) )
2068 m_marginLeft = GetNativeTextIndent();
a340b80d
VZ
2069 RecalcAndRefresh();
2070}
2071
2072// ----------------------------------------------------------------------------
2073// popup handling
2074// ----------------------------------------------------------------------------
2075
2076// Create popup window and the child control
a57d600f 2077void wxComboCtrlBase::CreatePopup()
a340b80d
VZ
2078{
2079 wxComboPopup* popupInterface = m_popupInterface;
2080 wxWindow* popup;
2081
2082 if ( !m_winPopup )
06077aaf
VZ
2083 {
2084#ifdef wxComboPopupWindowBase2
2085 if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP )
2086 {
fd5b4315 2087 #if !USES_GENERICTLW
06077aaf
VZ
2088 m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER );
2089 #else
fd5b4315
JS
2090 int tlwFlags = wxNO_BORDER;
2091 #ifdef wxCC_GENERIC_TLW_IS_FRAME
2092 tlwFlags |= wxFRAME_NO_TASKBAR;
2093 #endif
2094
2095 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
2096 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY,
2097 wxPoint(-21,-21), wxSize(20, 20),
2098 tlwFlags );
2099 #else
06077aaf
VZ
2100 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString,
2101 wxPoint(-21,-21), wxSize(20, 20),
fd5b4315
JS
2102 tlwFlags );
2103 #endif
06077aaf
VZ
2104 #endif
2105 m_popupWinType = SECONDARY_POPUP_TYPE;
2106 }
2107 else
c905c0d6 2108#endif // wxComboPopupWindowBase2
06077aaf
VZ
2109 {
2110 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
2111 m_popupWinType = PRIMARY_POPUP_TYPE;
2112 }
2113 m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this);
2114 m_winPopup->PushEventHandler(m_popupWinEvtHandler);
2115 }
a340b80d
VZ
2116
2117 popupInterface->Create(m_winPopup);
2118 m_popup = popup = popupInterface->GetControl();
2119
06315578
JS
2120 m_popupEvtHandler = new wxComboPopupEvtHandler(this);
2121 popup->PushEventHandler( m_popupEvtHandler );
a340b80d
VZ
2122
2123 // This may be helpful on some platforms
2124 // (eg. it bypasses a wxGTK popupwindow bug where
2125 // window is not initially hidden when it should be)
2126 m_winPopup->Hide();
2127
2128 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
2129}
2130
7ca4ac63
WS
2131// Destroy popup window and the child control
2132void wxComboCtrlBase::DestroyPopup()
2133{
a1d5aa93 2134 HidePopup(true);
c667b518 2135
7ca4ac63 2136 if ( m_popup )
06315578 2137 m_popup->RemoveEventHandler(m_popupEvtHandler);
7ca4ac63 2138
5276b0a5 2139 wxDELETE(m_popupEvtHandler);
7ca4ac63 2140
a516284f
JS
2141 if ( m_popupInterface )
2142 {
df3c4a42
JS
2143 // NB: DestroyPopup() performs 'delete this'.
2144 m_popupInterface->DestroyPopup();
a516284f
JS
2145 m_popupInterface = NULL;
2146 }
7ca4ac63
WS
2147
2148 if ( m_winPopup )
06077aaf
VZ
2149 {
2150 m_winPopup->RemoveEventHandler(m_popupWinEvtHandler);
5276b0a5 2151 wxDELETE(m_popupWinEvtHandler);
7ca4ac63 2152 m_winPopup->Destroy();
5276b0a5 2153 m_winPopup = NULL;
06077aaf 2154 }
7ca4ac63 2155
d3b9f782 2156 m_popup = NULL;
7ca4ac63
WS
2157}
2158
db53c6ea 2159void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
a340b80d 2160{
a57d600f 2161 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
6d0ce565 2162
7ca4ac63 2163 DestroyPopup();
a340b80d 2164
6d0ce565
VZ
2165 iface->InitBase(this);
2166 iface->Init();
2167
a340b80d
VZ
2168 m_popupInterface = iface;
2169
7ca4ac63 2170 if ( !iface->LazyCreate() )
a340b80d
VZ
2171 {
2172 CreatePopup();
2173 }
2174 else
2175 {
d3b9f782 2176 m_popup = NULL;
a340b80d
VZ
2177 }
2178
6d0ce565 2179 // This must be done after creation
6636ef8d 2180 if ( !m_valueString.empty() )
6d0ce565 2181 {
a340b80d 2182 iface->SetStringValue(m_valueString);
6d0ce565
VZ
2183 //Refresh();
2184 }
2185}
a340b80d 2186
6d0ce565 2187// Ensures there is atleast the default popup
a57d600f 2188void wxComboCtrlBase::EnsurePopupControl()
6d0ce565
VZ
2189{
2190 if ( !m_popupInterface )
2191 SetPopupControl(NULL);
a340b80d
VZ
2192}
2193
a57d600f 2194void wxComboCtrlBase::OnButtonClick()
a340b80d
VZ
2195{
2196 // Derived classes can override this method for totally custom
2197 // popup action
dc2b49b3 2198 switch ( GetPopupWindowState() )
a1d5aa93 2199 {
dc2b49b3
VS
2200 case Hidden:
2201 {
ffb9247a 2202 Popup();
dc2b49b3
VS
2203 break;
2204 }
2205
2206 case Animating:
2207 case Visible:
2208 {
2209 HidePopup(true);
2210 break;
2211 }
a1d5aa93 2212 }
a340b80d
VZ
2213}
2214
ffb9247a
JS
2215void wxComboCtrlBase::Popup()
2216{
2217 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId());
2218 event.SetEventObject(this);
2219 HandleWindowEvent(event);
2220
2221 ShowPopup();
2222}
2223
a57d600f 2224void wxComboCtrlBase::ShowPopup()
a340b80d 2225{
6d0ce565 2226 EnsurePopupControl();
974a12f8
RR
2227 wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") );
2228
2229 if ( IsPopupWindowState(Animating) )
2230 return;
a340b80d
VZ
2231
2232 SetFocus();
2233
2234 // Space above and below
2235 int screenHeight;
2236 wxPoint scrPos;
2237 int spaceAbove;
2238 int spaceBelow;
2239 int maxHeightPopup;
2240 wxSize ctrlSz = GetSize();
2241
2242 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
2243 scrPos = GetParent()->ClientToScreen(GetPosition());
2244
2245 spaceAbove = scrPos.y;
2246 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
2247
2248 maxHeightPopup = spaceBelow;
2249 if ( spaceAbove > spaceBelow )
2250 maxHeightPopup = spaceAbove;
2251
2252 // Width
2253 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
2254
2255 if ( widthPopup < m_widthMinPopup )
2256 widthPopup = m_widthMinPopup;
2257
2258 wxWindow* winPopup = m_winPopup;
2259 wxWindow* popup;
2260
2261 // Need to disable tab traversal of parent
2262 //
2263 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
2264 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2265 // that if transient popup is open, then tab traversal is to be ignored.
2266 // However, I think this code would still be needed for cases where
2267 // transient popup doesn't work yet (wxWinCE?).
bbb86904
JS
2268 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
2269 wxWindow* parent = mainCtrl->GetParent();
a340b80d
VZ
2270 int parentFlags = parent->GetWindowStyle();
2271 if ( parentFlags & wxTAB_TRAVERSAL )
2272 {
2273 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
2274 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
2275 }
2276
2277 if ( !winPopup )
2278 {
2279 CreatePopup();
2280 winPopup = m_winPopup;
2281 popup = m_popup;
2282 }
2283 else
2284 {
2285 popup = m_popup;
2286 }
2287
9727e8a3 2288 winPopup->Enable();
a61c9122 2289
a340b80d
VZ
2290 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
2291
2292 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
2293 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
2294 maxHeightPopup);
2295
2296 popup->SetSize(adjustedSize);
2297 popup->Move(0,0);
2298 m_popupInterface->OnPopup();
2299
2300 //
2301 // Reposition and resize popup window
2302 //
2303
2304 wxSize szp = popup->GetSize();
2305
2306 int popupX;
2307 int popupY = scrPos.y + ctrlSz.y;
2308
b7540dc1 2309 // Default anchor is wxLEFT
a340b80d
VZ
2310 int anchorSide = m_anchorSide;
2311 if ( !anchorSide )
b7540dc1 2312 anchorSide = wxLEFT;
a340b80d 2313
b7540dc1
WS
2314 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
2315 int leftX = scrPos.x - m_extLeft;
c4b37573
VZ
2316
2317 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
2318 leftX -= ctrlSz.x;
2319
b7540dc1
WS
2320 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
2321
2322 // If there is not enough horizontal space, anchor on the other side.
2323 // If there is no space even then, place the popup at x 0.
2324 if ( anchorSide == wxRIGHT )
2325 {
2326 if ( rightX < 0 )
2327 {
2328 if ( (leftX+szp.x) < screenWidth )
2329 anchorSide = wxLEFT;
2330 else
2331 anchorSide = 0;
2332 }
2333 }
2334 else
2335 {
2336 if ( (leftX+szp.x) >= screenWidth )
2337 {
2338 if ( rightX >= 0 )
2339 anchorSide = wxRIGHT;
2340 else
2341 anchorSide = 0;
2342 }
2343 }
2344
2345 // Select x coordinate according to the anchor side
a340b80d 2346 if ( anchorSide == wxRIGHT )
b7540dc1
WS
2347 popupX = rightX;
2348 else if ( anchorSide == wxLEFT )
2349 popupX = leftX;
a340b80d 2350 else
b7540dc1 2351 popupX = 0;
a340b80d 2352
974a12f8
RR
2353 int showFlags = CanDeferShow;
2354
a340b80d
VZ
2355 if ( spaceBelow < szp.y )
2356 {
2357 popupY = scrPos.y - szp.y;
974a12f8 2358 showFlags |= ShowAbove;
a340b80d
VZ
2359 }
2360
974a12f8
RR
2361#if INSTALL_TOPLEV_HANDLER
2362 // Put top level window event handler into place
2363 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
2364 {
2365 if ( !m_toplevEvtHandler )
2366 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
a340b80d 2367
974a12f8
RR
2368 wxWindow* toplev = ::wxGetTopLevelParent( this );
2369 wxASSERT( toplev );
2370 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
2371 toplev->PushEventHandler( m_toplevEvtHandler );
2372 }
2373#endif
a340b80d
VZ
2374
2375 // Set string selection (must be this way instead of SetStringSelection)
2376 if ( m_text )
2377 {
2378 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2379 m_text->SelectAll();
2380
2381 m_popupInterface->SetStringValue( m_text->GetValue() );
2382 }
2383 else
2384 {
2385 // This is neede since focus/selection indication may change when popup is shown
a340b80d
VZ
2386 Refresh();
2387 }
2388
2389 // This must be after SetStringValue
974a12f8 2390 m_popupWinState = Animating;
a340b80d 2391
974a12f8 2392 wxRect popupWinRect( popupX, popupY, szp.x, szp.y );
a340b80d 2393
974a12f8 2394 m_popup = popup;
30be036c
RR
2395 if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) ||
2396 AnimateShow( popupWinRect, showFlags ) )
06077aaf 2397 {
974a12f8
RR
2398 DoShowPopup( popupWinRect, showFlags );
2399 }
2400}
a340b80d 2401
974a12f8
RR
2402bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) )
2403{
2404 return true;
2405}
2406
2407void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) )
2408{
2409 wxWindow* winPopup = m_winPopup;
2410
2411 if ( IsPopupWindowState(Animating) )
2412 {
2413 // Make sure the popup window is shown in the right position.
2414 // Should not matter even if animation already did this.
2415
2416 // Some platforms (GTK) may like SetSize and Move to be separate
2417 // (though the bug was probably fixed).
2418 winPopup->SetSize( rect );
2419
c905c0d6
VZ
2420#if USES_WXPOPUPTRANSIENTWINDOW
2421 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2422 ((wxPopupTransientWindow*)winPopup)->Popup(m_popup);
2423 else
2424#endif
2425 winPopup->Show();
974a12f8
RR
2426
2427 m_popupWinState = Visible;
52999871
JS
2428
2429 // If popup window was a generic top-level window, or the
2430 // wxPopupWindow implemenation on this platform is classified as
2431 // perfect, then we should be able to safely set focus to the popup
2432 // control.
2433 if ( IsPopupWinTypePerfect(m_popupWinType) )
2434 m_popup->SetFocus();
06077aaf 2435 }
974a12f8
RR
2436 else if ( IsPopupWindowState(Hidden) )
2437 {
2438 // Animation was aborted
a340b80d 2439
974a12f8
RR
2440 wxASSERT( !winPopup->IsShown() );
2441
2442 m_popupWinState = Hidden;
2443 }
c905c0d6
VZ
2444
2445 Refresh();
a340b80d
VZ
2446}
2447
a1d5aa93 2448void wxComboCtrlBase::OnPopupDismiss(bool generateEvent)
974a12f8 2449{
a340b80d 2450 // Just in case, avoid double dismiss
974a12f8 2451 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
2452 return;
2453
9727e8a3
JS
2454 // This must be set before focus - otherwise there will be recursive
2455 // OnPopupDismisses.
974a12f8 2456 m_popupWinState = Hidden;
a340b80d 2457
9727e8a3
JS
2458 //SetFocus();
2459 m_winPopup->Disable();
2460
a340b80d
VZ
2461 // Inform popup control itself
2462 m_popupInterface->OnDismiss();
2463
06315578
JS
2464 if ( m_popupEvtHandler )
2465 ((wxComboPopupEvtHandler*)m_popupEvtHandler)->OnPopupDismiss();
a340b80d
VZ
2466
2467#if INSTALL_TOPLEV_HANDLER
2468 // Remove top level window event handler
2469 if ( m_toplevEvtHandler )
2470 {
2471 wxWindow* toplev = ::wxGetTopLevelParent( this );
2472 if ( toplev )
2473 toplev->RemoveEventHandler( m_toplevEvtHandler );
2474 }
2475#endif
2476
974a12f8
RR
2477 m_timeCanAcceptClick = ::wxGetLocalTimeMillis();
2478
2479 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2480 m_timeCanAcceptClick += 150;
a340b80d
VZ
2481
2482 // If cursor not on dropdown button, then clear its state
2483 // (technically not required by all ports, but do it for all just in case)
22a35096 2484 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
a340b80d
VZ
2485 m_btnState = 0;
2486
2487 // Return parent's tab traversal flag.
2488 // See ShowPopup for notes.
2489 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
2490 {
2491 wxWindow* parent = GetParent();
2492 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
2493 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
2494 }
2495
2496 // refresh control (necessary even if m_text)
2497 Refresh();
2498
a340b80d 2499 SetFocus();
a1d5aa93
JS
2500
2501 if ( generateEvent )
2502 {
2503 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId());
2504 event.SetEventObject(this);
65702d2f 2505 HandleWindowEvent(event);
a1d5aa93 2506 }
a340b80d
VZ
2507}
2508
a1d5aa93 2509void wxComboCtrlBase::HidePopup(bool generateEvent)
a340b80d
VZ
2510{
2511 // Should be able to call this without popup interface
974a12f8 2512 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
2513 return;
2514
2515 // transfer value and show it in textctrl, if any
974a12f8 2516 if ( !IsPopupWindowState(Animating) )
0306e73e 2517 SetValueByUser( m_popupInterface->GetStringValue() );
a340b80d 2518
974a12f8 2519 m_winPopup->Hide();
a340b80d 2520
a1d5aa93 2521 OnPopupDismiss(generateEvent);
a340b80d
VZ
2522}
2523
2524// ----------------------------------------------------------------------------
2525// customization methods
2526// ----------------------------------------------------------------------------
2527
a57d600f 2528void wxComboCtrlBase::SetButtonPosition( int width, int height,
7dc234d6 2529 int side, int spacingX )
a340b80d
VZ
2530{
2531 m_btnWid = width;
2532 m_btnHei = height;
2533 m_btnSide = side;
2534 m_btnSpacingX = spacingX;
2535
735a300a
JS
2536 if ( width > 0 || height > 0 || spacingX )
2537 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
2538
a340b80d
VZ
2539 RecalcAndRefresh();
2540}
2541
7dc234d6
WS
2542wxSize wxComboCtrlBase::GetButtonSize()
2543{
2544 if ( m_btnSize.x > 0 )
2545 return m_btnSize;
2546
2547 wxSize retSize(m_btnWid,m_btnHei);
2548
2549 // Need to call CalculateAreas now if button size is
2550 // is not explicitly specified.
2551 if ( retSize.x <= 0 || retSize.y <= 0)
2552 {
2553 OnResize();
2554
2555 retSize = m_btnSize;
2556 }
2557
2558 return retSize;
2559}
2560
a57d600f 2561void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
a340b80d
VZ
2562 bool blankButtonBg,
2563 const wxBitmap& bmpPressed,
2564 const wxBitmap& bmpHover,
2565 const wxBitmap& bmpDisabled )
2566{
2567 m_bmpNormal = bmpNormal;
2568 m_blankButtonBg = blankButtonBg;
2569
a1b806b9 2570 if ( bmpPressed.IsOk() )
a340b80d
VZ
2571 m_bmpPressed = bmpPressed;
2572 else
2573 m_bmpPressed = bmpNormal;
2574
a1b806b9 2575 if ( bmpHover.IsOk() )
a340b80d
VZ
2576 m_bmpHover = bmpHover;
2577 else
2578 m_bmpHover = bmpNormal;
2579
a1b806b9 2580 if ( bmpDisabled.IsOk() )
a340b80d
VZ
2581 m_bmpDisabled = bmpDisabled;
2582 else
2583 m_bmpDisabled = bmpNormal;
2584
2585 RecalcAndRefresh();
2586}
2587
a57d600f 2588void wxComboCtrlBase::SetCustomPaintWidth( int width )
a340b80d
VZ
2589{
2590 if ( m_text )
2591 {
2592 // move textctrl accordingly
2593 wxRect r = m_text->GetRect();
2594 int inc = width - m_widthCustomPaint;
2595 r.x += inc;
2596 r.width -= inc;
2597 m_text->SetSize( r );
2598 }
2599
2600 m_widthCustomPaint = width;
2601
2602 RecalcAndRefresh();
2603}
2604
0847e36e
JS
2605bool wxComboCtrlBase::DoSetMargins(const wxPoint& margins)
2606{
2607 // For general sanity's sake, we ignore top margin. Instead
2608 // we will always try to center the text vertically.
2609 bool res = true;
2610
2611 if ( margins.x != -1 )
2612 {
2613 m_marginLeft = margins.x;
2614 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2615 }
2616 else
2617 {
2618 m_marginLeft = GetNativeTextIndent();
2619 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2620 }
2621
2622 if ( margins.y != -1 )
2623 {
2624 res = false;
2625 }
2626
2627 RecalcAndRefresh();
2628
2629 return res;
2630}
2631
2632wxPoint wxComboCtrlBase::DoGetMargins() const
2633{
2634 return wxPoint(m_marginLeft, -1);
2635}
2636
ddadf560 2637#if WXWIN_COMPATIBILITY_2_8
a57d600f 2638void wxComboCtrlBase::SetTextIndent( int indent )
a340b80d
VZ
2639{
2640 if ( indent < 0 )
2641 {
0847e36e
JS
2642 m_marginLeft = GetNativeTextIndent();
2643 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
a340b80d
VZ
2644 }
2645 else
2646 {
0847e36e
JS
2647 m_marginLeft = indent;
2648 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
a340b80d
VZ
2649 }
2650
2651 RecalcAndRefresh();
2652}
2653
48284cc7 2654wxCoord wxComboCtrlBase::GetTextIndent() const
0847e36e
JS
2655{
2656 return m_marginLeft;
2657}
2658#endif
2659
a57d600f 2660wxCoord wxComboCtrlBase::GetNativeTextIndent() const
a340b80d
VZ
2661{
2662 return DEFAULT_TEXT_INDENT;
2663}
2664
1ac5cfc7
JS
2665void wxComboCtrlBase::SetTextCtrlStyle( int style )
2666{
2667 m_textCtrlStyle = style;
2668
2669 if ( m_text )
2670 m_text->SetWindowStyle(style);
2671}
2672
a340b80d 2673// ----------------------------------------------------------------------------
fda62793 2674// wxTextEntry interface
a340b80d
VZ
2675// ----------------------------------------------------------------------------
2676
fda62793 2677wxString wxComboCtrlBase::DoGetValue() const
a340b80d
VZ
2678{
2679 if ( m_text )
2680 return m_text->GetValue();
2681 return m_valueString;
2682}
2683
0306e73e
JS
2684void wxComboCtrlBase::SetValueWithEvent(const wxString& value,
2685 bool withEvent)
a340b80d 2686{
0306e73e
JS
2687 DoSetValue(value, withEvent ? SetValue_SendEvent : 0);
2688}
22b6de6a 2689
0306e73e
JS
2690void wxComboCtrlBase::OnSetValue(const wxString& value)
2691{
2692 // Note: before wxComboCtrl inherited from wxTextEntry,
2693 // this code used to be in SetValueWithEvent().
a340b80d
VZ
2694
2695 // Since wxComboPopup may want to paint the combo as well, we need
2696 // to set the string value here (as well as sometimes in ShowPopup).
4738d674 2697 if ( m_valueString != value )
a340b80d 2698 {
238b33ab
JS
2699 bool found = true;
2700 wxString trueValue = value;
2701
4c51a665 2702 // Conform to wxComboBox behaviour: read-only control can only accept
238b33ab
JS
2703 // valid list items and empty string
2704 if ( m_popupInterface && HasFlag(wxCB_READONLY) && value.length() )
2705 {
2706 found = m_popupInterface->FindItem(value,
2707 &trueValue);
2708 }
4738d674 2709
238b33ab
JS
2710 if ( found )
2711 {
2712 m_valueString = trueValue;
4738d674 2713
238b33ab
JS
2714 EnsurePopupControl();
2715
2716 if ( m_popupInterface )
2717 m_popupInterface->SetStringValue(trueValue);
2718 }
a340b80d 2719 }
4738d674
VZ
2720
2721 Refresh();
6d0ce565
VZ
2722}
2723
0306e73e 2724void wxComboCtrlBase::SetValueByUser(const wxString& value)
ce968519 2725{
0306e73e
JS
2726 // NB: Order of function calls is important here. Otherwise
2727 // the SelectAll() may not work.
2728
2729 if ( m_text )
2730 {
2731 m_text->SetValue(value);
2732
2733 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2734 m_text->SelectAll();
2735 }
2736
2737 OnSetValue(value);
ce968519
RR
2738}
2739
6d0ce565 2740// In this SetValue variant wxComboPopup::SetStringValue is not called
a57d600f 2741void wxComboCtrlBase::SetText(const wxString& value)
6d0ce565
VZ
2742{
2743 // Unlike in SetValue(), this must be called here or
2744 // the behaviour will no be consistent in readonlys.
2745 EnsurePopupControl();
a340b80d
VZ
2746
2747 m_valueString = value;
2748
ce968519
RR
2749 if ( m_text )
2750 {
2751 m_ignoreEvtText++;
2752 m_text->SetValue( value );
2753 }
2754
a340b80d
VZ
2755 Refresh();
2756}
2757
a57d600f 2758void wxComboCtrlBase::Copy()
a340b80d
VZ
2759{
2760 if ( m_text )
2761 m_text->Copy();
2762}
2763
a57d600f 2764void wxComboCtrlBase::Cut()
a340b80d
VZ
2765{
2766 if ( m_text )
2767 m_text->Cut();
2768}
2769
a57d600f 2770void wxComboCtrlBase::Paste()
a340b80d
VZ
2771{
2772 if ( m_text )
2773 m_text->Paste();
2774}
2775
a57d600f 2776void wxComboCtrlBase::SetInsertionPoint(long pos)
a340b80d
VZ
2777{
2778 if ( m_text )
2779 m_text->SetInsertionPoint(pos);
2780}
2781
a57d600f 2782long wxComboCtrlBase::GetInsertionPoint() const
a340b80d
VZ
2783{
2784 if ( m_text )
2785 return m_text->GetInsertionPoint();
2786
2787 return 0;
2788}
2789
a57d600f 2790long wxComboCtrlBase::GetLastPosition() const
a340b80d
VZ
2791{
2792 if ( m_text )
2793 return m_text->GetLastPosition();
2794
2795 return 0;
2796}
2797
0306e73e 2798void wxComboCtrlBase::WriteText(const wxString& text)
a340b80d
VZ
2799{
2800 if ( m_text )
0306e73e
JS
2801 {
2802 m_text->WriteText(text);
2803 OnSetValue(m_text->GetValue());
2804 }
2805 else
2806 {
2807 OnSetValue(text);
2808 }
a340b80d
VZ
2809}
2810
0306e73e 2811void wxComboCtrlBase::DoSetValue(const wxString& value, int flags)
fda62793
JS
2812{
2813 if ( m_text )
0306e73e
JS
2814 {
2815 if ( flags & SetValue_SendEvent )
2816 m_text->SetValue(value);
2817 else
2818 m_text->ChangeValue(value);
2819 }
2820
2821 OnSetValue(value);
2822}
2823
2824void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2825{
2826 if ( m_text )
2827 {
2828 m_text->Replace(from, to, value);
2829 OnSetValue(m_text->GetValue());
2830 }
fda62793
JS
2831}
2832
a57d600f 2833void wxComboCtrlBase::Remove(long from, long to)
a340b80d
VZ
2834{
2835 if ( m_text )
0306e73e 2836 {
a340b80d 2837 m_text->Remove(from, to);
0306e73e
JS
2838 OnSetValue(m_text->GetValue());
2839 }
a340b80d
VZ
2840}
2841
a57d600f 2842void wxComboCtrlBase::SetSelection(long from, long to)
a340b80d
VZ
2843{
2844 if ( m_text )
2845 m_text->SetSelection(from, to);
2846}
2847
fda62793
JS
2848void wxComboCtrlBase::GetSelection(long *from, long *to) const
2849{
2850 if ( m_text )
2851 {
2852 m_text->GetSelection(from, to);
2853 }
2854 else
2855 {
2856 *from = 0;
2857 *to = 0;
2858 }
2859}
2860
2861bool wxComboCtrlBase::IsEditable() const
2862{
2863 if ( m_text )
2864 return m_text->IsEditable();
2865 return false;
2866}
2867
2868void wxComboCtrlBase::SetEditable(bool editable)
2869{
2870 if ( m_text )
2871 m_text->SetEditable(editable);
2872}
2873
a57d600f 2874void wxComboCtrlBase::Undo()
a340b80d
VZ
2875{
2876 if ( m_text )
2877 m_text->Undo();
2878}
2879
fda62793
JS
2880void wxComboCtrlBase::Redo()
2881{
2882 if ( m_text )
2883 m_text->Redo();
2884}
2885
2886bool wxComboCtrlBase::CanUndo() const
2887{
2888 if ( m_text )
2889 return m_text->CanUndo();
2890
2891 return false;
2892}
2893
2894bool wxComboCtrlBase::CanRedo() const
2895{
2896 if ( m_text )
2897 return m_text->CanRedo();
2898
2899 return false;
2900}
2901
107defe3
JS
2902bool wxComboCtrlBase::SetHint(const wxString& hint)
2903{
2904 m_hintText = hint;
2905 bool res = true;
2906 if ( m_text )
2907 res = m_text->SetHint(hint);
2908 Refresh();
2909 return res;
2910}
2911
2912wxString wxComboCtrlBase::GetHint() const
2913{
2914 return m_hintText;
2915}
2916
a57d600f 2917#endif // wxUSE_COMBOCTRL