]> git.saurik.com Git - wxWidgets.git/blame - src/common/combocmn.cpp
remove some extraneous semicolons
[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
JS
162// NB: Let's not be afraid to use wxGTK's wxPopupTransientWindow as a
163// 'perfect' popup, as it can succesfully host child controls even in
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
677// ----------------------------------------------------------------------------
678// input handling
679// ----------------------------------------------------------------------------
680
681//
b445b6a7 682// This is pushed to the event handler queue of the child textctrl.
a340b80d
VZ
683//
684class wxComboBoxExtraInputHandler : public wxEvtHandler
685{
686public:
687
a57d600f 688 wxComboBoxExtraInputHandler( wxComboCtrlBase* combo )
a340b80d
VZ
689 : wxEvtHandler()
690 {
691 m_combo = combo;
692 }
d3c7fc99 693 virtual ~wxComboBoxExtraInputHandler() { }
a340b80d
VZ
694 void OnKey(wxKeyEvent& event);
695 void OnFocus(wxFocusEvent& event);
696
697protected:
a57d600f 698 wxComboCtrlBase* m_combo;
a340b80d
VZ
699
700private:
701 DECLARE_EVENT_TABLE()
702};
703
704
705BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler)
706 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey)
df6007a5
JS
707 EVT_KEY_UP(wxComboBoxExtraInputHandler::OnKey)
708 EVT_CHAR(wxComboBoxExtraInputHandler::OnKey)
a340b80d 709 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
c905c0d6 710 EVT_KILL_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
a340b80d
VZ
711END_EVENT_TABLE()
712
713
714void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event)
715{
b445b6a7
VZ
716 // Let the wxComboCtrl event handler have a go first.
717 wxComboCtrlBase* combo = m_combo;
a340b80d 718
110d27b3
JS
719 wxKeyEvent redirectedEvent(event);
720 redirectedEvent.SetId(combo->GetId());
721 redirectedEvent.SetEventObject(combo);
a340b80d 722
110d27b3
JS
723 if ( !combo->GetEventHandler()->ProcessEvent(redirectedEvent) )
724 {
725 // Don't let TAB through to the text ctrl - looks ugly
726 if ( event.GetKeyCode() != WXK_TAB )
727 event.Skip();
728 }
a340b80d
VZ
729}
730
a340b80d
VZ
731void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event)
732{
733 // FIXME: This code does run when control is clicked,
734 // yet on Windows it doesn't select all the text.
c905c0d6
VZ
735 if ( event.GetEventType() == wxEVT_SET_FOCUS &&
736 !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) )
a340b80d
VZ
737 {
738 if ( m_combo->GetTextCtrl() )
739 m_combo->GetTextCtrl()->SelectAll();
740 else
741 m_combo->SetSelection(-1,-1);
742 }
743
b445b6a7
VZ
744 // Send focus indication to parent.
745 // NB: This is needed for cases where the textctrl gets focus
746 // instead of its parent. While this may trigger multiple
747 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
748 // from combo's focus event handler), they should be quite
749 // harmless.
81ebc041
VZ
750 wxFocusEvent evt2(event);
751 evt2.SetId(m_combo->GetId());
b445b6a7
VZ
752 evt2.SetEventObject(m_combo);
753 m_combo->GetEventHandler()->ProcessEvent(evt2);
6d0ce565 754
a340b80d
VZ
755 event.Skip();
756}
757
758
759//
760// This is pushed to the event handler queue of the control in popup.
761//
762
06315578 763class wxComboPopupEvtHandler : public wxEvtHandler
a340b80d
VZ
764{
765public:
766
06315578 767 wxComboPopupEvtHandler( wxComboCtrlBase* combo )
a340b80d
VZ
768 : wxEvtHandler()
769 {
770 m_combo = combo;
771 m_beenInside = false;
4794626d
JS
772
773 // Let's make it so that the popup control will not receive mouse
774 // events until mouse left button has been up.
775 m_blockEventsToPopup = true;
a340b80d 776 }
06315578 777 virtual ~wxComboPopupEvtHandler() { }
a340b80d
VZ
778
779 void OnMouseEvent( wxMouseEvent& event );
780
a57d600f 781 // Called from wxComboCtrlBase::OnPopupDismiss
a340b80d
VZ
782 void OnPopupDismiss()
783 {
784 m_beenInside = false;
4794626d 785 m_blockEventsToPopup = true;
a340b80d
VZ
786 }
787
788protected:
a57d600f 789 wxComboCtrlBase* m_combo;
a340b80d 790
4794626d
JS
791 bool m_beenInside;
792 bool m_blockEventsToPopup;
a340b80d
VZ
793
794private:
795 DECLARE_EVENT_TABLE()
796};
797
798
06315578
JS
799BEGIN_EVENT_TABLE(wxComboPopupEvtHandler, wxEvtHandler)
800 EVT_MOUSE_EVENTS(wxComboPopupEvtHandler::OnMouseEvent)
a340b80d
VZ
801END_EVENT_TABLE()
802
803
06315578 804void wxComboPopupEvtHandler::OnMouseEvent( wxMouseEvent& event )
a340b80d
VZ
805{
806 wxPoint pt = event.GetPosition();
6d0ce565 807 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
a340b80d
VZ
808 int evtType = event.GetEventType();
809 bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y;
c905c0d6
VZ
810 bool relayToButton = false;
811
812 event.Skip();
a340b80d 813
4794626d 814 if ( !isInside || !m_combo->IsPopupShown() )
a340b80d 815 {
4794626d
JS
816 // Mouse is outside the popup or popup is not actually shown (yet)
817
818 if ( evtType == wxEVT_MOTION ||
819 evtType == wxEVT_LEFT_DOWN ||
820 evtType == wxEVT_LEFT_UP ||
821 evtType == wxEVT_RIGHT_DOWN )
a340b80d 822 {
4794626d 823 // Block motion and click events outside the popup
974a12f8 824 event.Skip(false);
a340b80d 825 }
4794626d
JS
826 }
827 else
828 {
829 // Mouse is inside the popup, which is fully shown
830
831 m_beenInside = true;
832
833 // Do not let the popup control respond to mouse events until
834 // mouse press used to display the popup has been lifted. This
835 // is important for users with slower mouse fingers or mouse
836 // drivers. Note that we have some redundancy here, just in
837 // case the popup is some native control that does not emit all
838 // mouse event types.
839 if ( evtType == wxEVT_MOTION )
a340b80d 840 {
4794626d 841 if ( m_blockEventsToPopup )
a340b80d 842 {
4794626d
JS
843 if ( event.LeftIsDown() )
844 event.Skip(false);
845 else
846 m_blockEventsToPopup = false;
a340b80d 847 }
4794626d
JS
848 }
849 else if ( evtType == wxEVT_LEFT_DOWN )
850 {
851 if ( m_blockEventsToPopup )
852 m_blockEventsToPopup = false;
853 }
854 else if ( evtType == wxEVT_LEFT_UP )
855 {
856 if ( m_blockEventsToPopup )
a340b80d 857 {
4794626d
JS
858 // On first left up, stop blocking mouse events (but still
859 // block this one)
860 m_blockEventsToPopup = false;
861 event.Skip(false);
db3d7543
JS
862
863 // Also, this button press was (probably) used to display
864 // the popup, so relay it back to the drop-down button
865 // (which supposedly originated it). This is necessary to
866 // refresh it properly.
867 relayToButton = true;
a340b80d 868 }
a340b80d 869 }
4794626d
JS
870 else if ( m_blockEventsToPopup )
871 {
872 event.Skip(false);
873 }
874 }
875
876 //
877 // Some mouse events to popup that happen outside it, before cursor
878 // has been inside the popup, need to be ignored by it but relayed to
879 // the dropbutton.
880 //
881 if ( evtType == wxEVT_LEFT_UP )
882 {
883 if ( !m_combo->IsPopupShown() )
884 {
885 event.Skip(false);
886 relayToButton = true;
887 }
888 else if ( !isInside && !m_beenInside )
889 {
890 // Popup is shown but the cursor is not inside, nor it has been
891 relayToButton = true;
892 }
a340b80d
VZ
893 }
894
c905c0d6
VZ
895 if ( relayToButton )
896 {
c905c0d6
VZ
897 wxWindow* btn = m_combo->GetButton();
898 if ( btn )
db3d7543
JS
899 btn->GetEventHandler()->ProcessEvent(event);
900 else
901 // Bypass the event handling mechanism. Using it would be
902 // confusing for the platform-specific wxComboCtrl
903 // implementations.
904 m_combo->HandleButtonMouseEvent(event, 0);
c905c0d6 905 }
a340b80d
VZ
906}
907
c905c0d6
VZ
908// ----------------------------------------------------------------------------
909// wxComboCtrlTextCtrl
910// ----------------------------------------------------------------------------
911
912class wxComboCtrlTextCtrl : public wxTextCtrl
913{
914public:
915 wxComboCtrlTextCtrl() : wxTextCtrl() { }
916 virtual ~wxComboCtrlTextCtrl() { }
917
918 virtual wxWindow *GetMainWindowOfCompositeControl()
919 {
920 wxComboCtrl* combo = (wxComboCtrl*) GetParent();
921
922 // Returning this instead of just 'parent' lets FindFocus work
923 // correctly even when parent control is a child of a composite
924 // generic control (as is case with wxGenericDatePickerCtrl).
925 return combo->GetMainWindowOfCompositeControl();
926 }
927};
928
a340b80d 929// ----------------------------------------------------------------------------
a57d600f 930// wxComboCtrlBase
a340b80d
VZ
931// ----------------------------------------------------------------------------
932
933
a57d600f
VZ
934BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl)
935 EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
936 EVT_SIZE(wxComboCtrlBase::OnSizeEvent)
937 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent)
938 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent)
2dfa37d6 939 EVT_IDLE(wxComboCtrlBase::OnIdleEvent)
a57d600f 940 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
b445b6a7 941 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent)
c765e6fb 942 EVT_CHAR(wxComboCtrlBase::OnCharEvent)
a57d600f
VZ
943 EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
944 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged)
a340b80d
VZ
945END_EVENT_TABLE()
946
947
a57d600f 948IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl)
a340b80d 949
a57d600f 950void wxComboCtrlBase::Init()
a340b80d 951{
d3b9f782
VZ
952 m_winPopup = NULL;
953 m_popup = NULL;
974a12f8 954 m_popupWinState = Hidden;
d3b9f782
VZ
955 m_btn = NULL;
956 m_text = NULL;
957 m_popupInterface = NULL;
a340b80d 958
06315578 959 m_popupEvtHandler = NULL;
d3b9f782 960 m_textEvtHandler = NULL;
a340b80d
VZ
961
962#if INSTALL_TOPLEV_HANDLER
d3b9f782 963 m_toplevEvtHandler = NULL;
a340b80d
VZ
964#endif
965
4427c0a3
RR
966 m_mainCtrlWnd = this;
967
a340b80d
VZ
968 m_heightPopup = -1;
969 m_widthMinPopup = -1;
970 m_anchorSide = 0;
971 m_widthCustomPaint = 0;
972 m_widthCustomBorder = 0;
973
974 m_btnState = 0;
975 m_btnWidDefault = 0;
976 m_blankButtonBg = false;
ce968519 977 m_ignoreEvtText = 0;
06077aaf 978 m_popupWinType = POPUPWIN_NONE;
7dc234d6 979 m_btnWid = m_btnHei = -1;
a340b80d
VZ
980 m_btnSide = wxRIGHT;
981 m_btnSpacingX = 0;
982
983 m_extLeft = 0;
984 m_extRight = 0;
0847e36e 985 m_marginLeft = -1;
a340b80d 986 m_iFlags = 0;
1ac5cfc7 987 m_textCtrlStyle = 0;
a340b80d 988 m_timeCanAcceptClick = 0;
2dfa37d6
RD
989
990 m_resetFocus = false;
a340b80d
VZ
991}
992
a57d600f 993bool wxComboCtrlBase::Create(wxWindow *parent,
b61f4f77
WS
994 wxWindowID id,
995 const wxString& value,
996 const wxPoint& pos,
997 const wxSize& size,
998 long style,
999 const wxValidator& validator,
1000 const wxString& name)
a340b80d
VZ
1001{
1002 if ( !wxControl::Create(parent,
1003 id,
1004 pos,
1005 size,
1006 style | wxWANTS_CHARS,
1007 validator,
1008 name) )
1009 return false;
1010
1011 m_valueString = value;
1012
1013 // Get colours
1014 OnThemeChange();
0847e36e 1015 m_marginLeft = GetNativeTextIndent();
a340b80d 1016
a9e8bf2d
WS
1017 m_iFlags |= wxCC_IFLAG_CREATED;
1018
1019 // If x and y indicate valid size, wxSizeEvent won't be
1020 // emitted automatically, so we need to add artifical one.
1021 if ( size.x > 0 && size.y > 0 )
1022 {
1023 wxSizeEvent evt(size,GetId());
1024 GetEventHandler()->AddPendingEvent(evt);
1025 }
1026
a340b80d
VZ
1027 return true;
1028}
1029
b445b6a7 1030void wxComboCtrlBase::InstallInputHandlers()
a340b80d 1031{
b445b6a7 1032 if ( m_text )
a340b80d
VZ
1033 {
1034 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
1035 m_text->PushEventHandler(m_textEvtHandler);
1036 }
a340b80d
VZ
1037}
1038
42a3ecf5 1039void
fda62793 1040wxComboCtrlBase::CreateTextCtrl(int style)
a340b80d
VZ
1041{
1042 if ( !(m_windowStyle & wxCB_READONLY) )
1043 {
8e9ec723
RR
1044 if ( m_text )
1045 m_text->Destroy();
1046
42a3ecf5
VZ
1047 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
1048 // not used by the wxPropertyGrid and therefore the tab is processed by
1049 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
1050 // navigation event is then sent to the wrong window.
1ac5cfc7 1051 style |= wxTE_PROCESS_TAB | m_textCtrlStyle;
42a3ecf5 1052
5d95cab8 1053 if ( HasFlag(wxTE_PROCESS_ENTER) )
42a3ecf5
VZ
1054 style |= wxTE_PROCESS_ENTER;
1055
ce968519
RR
1056 // Ignore EVT_TEXT generated by the constructor (but only
1057 // if the event redirector already exists)
1058 // NB: This must be " = 1" instead of "++";
1059 if ( m_textEvtHandler )
1060 m_ignoreEvtText = 1;
1061 else
1062 m_ignoreEvtText = 0;
1063
c905c0d6
VZ
1064 m_text = new wxComboCtrlTextCtrl();
1065 m_text->Create(this, wxID_ANY, m_valueString,
1066 wxDefaultPosition, wxSize(10,-1),
fda62793 1067 style);
107defe3 1068 m_text->SetHint(m_hintText);
a340b80d
VZ
1069 }
1070}
1071
a57d600f 1072void wxComboCtrlBase::OnThemeChange()
a340b80d 1073{
2dfa37d6
RD
1074 // Leave the default bg on the Mac so the area used by the focus ring will
1075 // be the correct colour and themed brush. Instead we'll use
1076 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
1077#ifndef __WXMAC__
2ec335db
JS
1078 #if defined(__WXMSW__) || defined(__WXGTK__)
1079 wxVisualAttributes vattrs = wxComboBox::GetClassDefaultAttributes();
1080 #else
1081 wxVisualAttributes vattrs;
1082 vattrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1083 vattrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1084 #endif
1085
1086 // Only change the colours if application has not specified
1087 // custom ones.
1088 if ( !m_hasFgCol )
1089 {
1090 SetOwnForegroundColour(vattrs.colFg);
1091 m_hasFgCol = false;
1092 }
3b9ba64c
JS
1093 if ( !m_hasBgCol )
1094 {
2ec335db 1095 SetOwnBackgroundColour(vattrs.colBg);
3b9ba64c
JS
1096 m_hasBgCol = false;
1097 }
2ec335db 1098#endif // !__WXMAC__
a340b80d
VZ
1099}
1100
a57d600f 1101wxComboCtrlBase::~wxComboCtrlBase()
a340b80d
VZ
1102{
1103 if ( HasCapture() )
1104 ReleaseMouse();
1105
a340b80d
VZ
1106#if INSTALL_TOPLEV_HANDLER
1107 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
d3b9f782 1108 m_toplevEvtHandler = NULL;
a340b80d
VZ
1109#endif
1110
7ca4ac63 1111 DestroyPopup();
a340b80d 1112
a340b80d
VZ
1113 if ( m_text )
1114 m_text->RemoveEventHandler(m_textEvtHandler);
1115
1116 delete m_textEvtHandler;
a340b80d
VZ
1117}
1118
1119
1120// ----------------------------------------------------------------------------
1121// geometry stuff
1122// ----------------------------------------------------------------------------
1123
1124// Recalculates button and textctrl areas
a57d600f 1125void wxComboCtrlBase::CalculateAreas( int btnWidth )
a340b80d
VZ
1126{
1127 wxSize sz = GetClientSize();
1128 int customBorder = m_widthCustomBorder;
a340b80d
VZ
1129 int btnBorder; // border for button only
1130
87419e97
VZ
1131 // check if button should really be outside the border: we'll do it it if
1132 // its platform default or bitmap+pushbutton background is used, but not if
1133 // there is vertical size adjustment or horizontal spacing.
1134 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
1135 (m_bmpNormal.Ok() && m_blankButtonBg) ) &&
1136 m_btnSpacingX == 0 &&
7dc234d6 1137 m_btnHei <= 0 )
a340b80d 1138 {
a340b80d
VZ
1139 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
1140 btnBorder = 0;
1141 }
c905c0d6
VZ
1142 else if ( (m_iFlags & wxCC_BUTTON_COVERS_BORDER) &&
1143 m_btnSpacingX == 0 && !m_bmpNormal.Ok() )
1144 {
1145 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1146 btnBorder = 0;
1147 }
a340b80d
VZ
1148 else
1149 {
a340b80d
VZ
1150 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
1151 btnBorder = customBorder;
1152 }
1153
1154 // Defaul indentation
0847e36e
JS
1155 if ( m_marginLeft < 0 )
1156 m_marginLeft = GetNativeTextIndent();
a340b80d
VZ
1157
1158 int butWidth = btnWidth;
1159
1160 if ( butWidth <= 0 )
1161 butWidth = m_btnWidDefault;
1162 else
1163 m_btnWidDefault = butWidth;
1164
1165 if ( butWidth <= 0 )
1166 return;
1167
a9e8bf2d
WS
1168 int butHeight = sz.y - btnBorder*2;
1169
a340b80d 1170 // Adjust button width
7dc234d6 1171 if ( m_btnWid > 0 )
a340b80d 1172 butWidth = m_btnWid;
a9e8bf2d
WS
1173 else
1174 {
1175 // Adjust button width to match aspect ratio
1176 // (but only if control is smaller than best size).
1177 int bestHeight = GetBestSize().y;
1178 int height = GetSize().y;
a340b80d 1179
a9e8bf2d
WS
1180 if ( height < bestHeight )
1181 {
1182 // Make very small buttons square, as it makes
1183 // them accommodate arrow image better and still
1184 // looks decent.
1185 if ( height > 18 )
1186 butWidth = (height*butWidth)/bestHeight;
1187 else
1188 butWidth = butHeight;
1189 }
1190 }
a340b80d
VZ
1191
1192 // Adjust button height
7dc234d6 1193 if ( m_btnHei > 0 )
a340b80d
VZ
1194 butHeight = m_btnHei;
1195
1196 // Use size of normal bitmap if...
1197 // It is larger
1198 // OR
1199 // button width is set to default and blank button bg is not drawn
1200 if ( m_bmpNormal.Ok() )
1201 {
1202 int bmpReqWidth = m_bmpNormal.GetWidth();
1203 int bmpReqHeight = m_bmpNormal.GetHeight();
1204
1205 // If drawing blank button background, we need to add some margin.
1206 if ( m_blankButtonBg )
1207 {
1208 bmpReqWidth += BMP_BUTTON_MARGIN*2;
1209 bmpReqHeight += BMP_BUTTON_MARGIN*2;
1210 }
1211
1212 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
1213 butWidth = bmpReqWidth;
1214 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
1215 butHeight = bmpReqHeight;
1216
1217 // Need to fix height?
1218 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
1219 {
1220 int newY = butHeight+(customBorder*2);
a5bbd1cc 1221 SetClientSize(wxDefaultCoord,newY);
640a91ab
PC
1222 if ( m_bmpNormal.Ok() || m_btnArea.width != butWidth || m_btnArea.height != butHeight )
1223 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1224 else
1225 m_iFlags &= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
c905c0d6 1226
a340b80d
VZ
1227 sz.y = newY;
1228 }
1229 }
1230
1231 int butAreaWid = butWidth + (m_btnSpacingX*2);
1232
1233 m_btnSize.x = butWidth;
1234 m_btnSize.y = butHeight;
1235
1236 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
2dfa37d6 1237 m_btnArea.y = btnBorder + FOCUS_RING;
a340b80d 1238 m_btnArea.width = butAreaWid;
2dfa37d6 1239 m_btnArea.height = sz.y - ((btnBorder+FOCUS_RING)*2);
a340b80d 1240
2dfa37d6
RD
1241 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder + FOCUS_RING;
1242 m_tcArea.y = customBorder + FOCUS_RING;
1243 m_tcArea.width = sz.x - butAreaWid - (customBorder*2) - (FOCUS_RING*2);
1244 m_tcArea.height = sz.y - ((customBorder+FOCUS_RING)*2);
a340b80d
VZ
1245
1246/*
1247 if ( m_text )
1248 {
1249 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1250 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1251 }
1252*/
1253}
1254
a57d600f 1255void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
a340b80d
VZ
1256{
1257 if ( !m_text )
1258 return;
1259
1260 wxSize sz = GetClientSize();
a340b80d 1261
f85f5ddf 1262 int customBorder = m_widthCustomBorder;
a340b80d
VZ
1263 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
1264 {
4bcf25e9
JS
1265 int x;
1266
1267 if ( !m_widthCustomPaint )
1268 {
1269 // No special custom paint area - we can use 0 left margin
1270 // with wxTextCtrl.
1271 if ( m_text->SetMargins(0) )
1272 textCtrlXAdjust = 0;
1273 x = m_tcArea.x + m_marginLeft + textCtrlXAdjust;
1274 }
1275 else
1276 {
1277 // There is special custom paint area - it is better to
1278 // use some margin with the wxTextCtrl.
1279 m_text->SetMargins(m_marginLeft);
ce00f59b 1280 x = m_tcArea.x + m_widthCustomPaint +
4bcf25e9
JS
1281 m_marginLeft + textCtrlXAdjust;
1282 }
0847e36e 1283
4bcf25e9 1284 // Centre textctrl vertically, if needed
0847e36e 1285#if !TEXTCTRL_TEXT_CENTERED
a340b80d 1286 int tcSizeY = m_text->GetBestSize().y;
0847e36e
JS
1287 int diff0 = sz.y - tcSizeY;
1288 int y = textCtrlYAdjust + (diff0/2);
1289#else
1290 wxUnusedVar(textCtrlYAdjust);
1291 int y = 0;
1292#endif
a340b80d
VZ
1293
1294 if ( y < customBorder )
1295 y = customBorder;
1296
0847e36e
JS
1297 m_text->SetSize(x,
1298 y,
1299 m_tcArea.width - m_tcArea.x - x,
0847e36e 1300 -1 );
a340b80d
VZ
1301
1302 // Make sure textctrl doesn't exceed the bottom custom border
1303 wxSize tsz = m_text->GetSize();
0847e36e
JS
1304 int diff1 = (y + tsz.y) - (sz.y - customBorder);
1305 if ( diff1 >= 0 )
a340b80d 1306 {
0847e36e 1307 tsz.y = tsz.y - diff1 - 1;
a340b80d
VZ
1308 m_text->SetSize(tsz);
1309 }
1310 }
1311 else
1312 {
0847e36e 1313 // If it has border, have textctrl fill the entire text field.
7e4545b8 1314 m_text->SetSize( m_tcArea.x + m_widthCustomPaint,
2dfa37d6
RD
1315 m_tcArea.y,
1316 m_tcArea.width - m_widthCustomPaint,
1317 m_tcArea.height );
a340b80d
VZ
1318 }
1319}
1320
a57d600f 1321wxSize wxComboCtrlBase::DoGetBestSize() const
a340b80d
VZ
1322{
1323 wxSize sizeText(150,0);
1324
1325 if ( m_text )
1326 sizeText = m_text->GetBestSize();
1327
1328 // TODO: Better method to calculate close-to-native control height.
1329
1330 int fhei;
1331 if ( m_font.Ok() )
1332 fhei = (m_font.GetPointSize()*2) + 5;
1333 else if ( wxNORMAL_FONT->Ok() )
1334 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
1335 else
1336 fhei = sizeText.y + 4;
1337
1338 // Need to force height to accomodate bitmap?
1339 int btnSizeY = m_btnSize.y;
1340 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
1341 fhei = btnSizeY;
1342
1343 // Control height doesn't depend on border
1344/*
1345 // Add border
1346 int border = m_windowStyle & wxBORDER_MASK;
1347 if ( border == wxSIMPLE_BORDER )
1348 fhei += 2;
1349 else if ( border == wxNO_BORDER )
1350 fhei += (m_widthCustomBorder*2);
1351 else
1352 // Sunken etc.
1353 fhei += 4;
1354*/
1355
1356 // Final adjustments
1357#ifdef __WXGTK__
1358 fhei += 1;
1359#endif
1360
c66ed668
RD
1361#ifdef __WXMAC__
1362 // these are the numbers from the HIG:
1363 switch ( m_windowVariant )
1364 {
1365 case wxWINDOW_VARIANT_NORMAL:
1366 default :
1367 fhei = 22;
1368 break;
1369 case wxWINDOW_VARIANT_SMALL:
1370 fhei = 19;
1371 break;
1372 case wxWINDOW_VARIANT_MINI:
1373 fhei = 15;
1374 break;
1375 }
1376#endif
a340b80d 1377
2dfa37d6
RD
1378 fhei += 2 * FOCUS_RING;
1379 int width = sizeText.x + FOCUS_RING + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH;
1380
1381 wxSize ret(width, fhei);
a340b80d
VZ
1382 CacheBestSize(ret);
1383 return ret;
1384}
1385
a57d600f 1386void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
a340b80d
VZ
1387{
1388 if ( !IsCreated() )
1389 return;
1390
a57d600f 1391 // defined by actual wxComboCtrls
a340b80d
VZ
1392 OnResize();
1393
1394 event.Skip();
1395}
1396
1397// ----------------------------------------------------------------------------
1398// standard operations
1399// ----------------------------------------------------------------------------
1400
a57d600f 1401bool wxComboCtrlBase::Enable(bool enable)
a340b80d
VZ
1402{
1403 if ( !wxControl::Enable(enable) )
1404 return false;
1405
1406 if ( m_btn )
1407 m_btn->Enable(enable);
1408 if ( m_text )
1409 m_text->Enable(enable);
004867db 1410
4df230b8 1411 Refresh();
a340b80d
VZ
1412
1413 return true;
1414}
1415
a57d600f 1416bool wxComboCtrlBase::Show(bool show)
a340b80d
VZ
1417{
1418 if ( !wxControl::Show(show) )
1419 return false;
1420
1421 if (m_btn)
1422 m_btn->Show(show);
1423
1424 if (m_text)
1425 m_text->Show(show);
1426
1427 return true;
1428}
1429
a57d600f 1430bool wxComboCtrlBase::SetFont ( const wxFont& font )
a340b80d
VZ
1431{
1432 if ( !wxControl::SetFont(font) )
1433 return false;
1434
4bcf25e9
JS
1435 if ( m_text )
1436 {
1437 // Without hiding the wxTextCtrl there would be some
1438 // visible 'flicker' (at least on Windows XP).
1439 m_text->Hide();
a340b80d 1440 m_text->SetFont(font);
4bcf25e9
JS
1441 OnResize();
1442 m_text->Show();
1443 }
a340b80d
VZ
1444
1445 return true;
1446}
1447
1448#if wxUSE_TOOLTIPS
a57d600f 1449void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
a340b80d
VZ
1450{
1451 wxControl::DoSetToolTip(tooltip);
1452
1453 // Set tool tip for button and text box
1454 if ( tooltip )
1455 {
1456 const wxString &tip = tooltip->GetTip();
1457 if ( m_text ) m_text->SetToolTip(tip);
1458 if ( m_btn ) m_btn->SetToolTip(tip);
1459 }
1460 else
1461 {
d3b9f782
VZ
1462 if ( m_text ) m_text->SetToolTip( NULL );
1463 if ( m_btn ) m_btn->SetToolTip( NULL );
a340b80d
VZ
1464 }
1465}
1466#endif // wxUSE_TOOLTIPS
1467
5f540750
JS
1468bool wxComboCtrlBase::SetForegroundColour(const wxColour& colour)
1469{
1470 if ( wxControl::SetForegroundColour(colour) )
1471 {
1472 if ( m_text )
1473 m_text->SetForegroundColour(colour);
1474 return true;
1475 }
1476 return false;
1477}
1478
1479bool wxComboCtrlBase::SetBackgroundColour(const wxColour& colour)
1480{
1481 if ( wxControl::SetBackgroundColour(colour) )
1482 {
1483 if ( m_text )
1484 m_text->SetBackgroundColour(colour);
1485 return true;
1486 }
1487 return false;
1488}
a340b80d
VZ
1489// ----------------------------------------------------------------------------
1490// painting
1491// ----------------------------------------------------------------------------
1492
118f5fbd
RR
1493#if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1494// prepare combo box background on area in a way typical on platform
1495void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
a340b80d
VZ
1496{
1497 wxSize sz = GetClientSize();
1498 bool isEnabled;
c905c0d6 1499 bool doDrawFocusRect; // also selected
a340b80d
VZ
1500
1501 // For smaller size control (and for disabled background) use less spacing
1502 int focusSpacingX;
1503 int focusSpacingY;
1504
1505 if ( !(flags & wxCONTROL_ISSUBMENU) )
1506 {
1507 // Drawing control
1508 isEnabled = IsEnabled();
640a91ab 1509 doDrawFocusRect = ShouldDrawFocus() && !(m_iFlags & wxCC_FULL_BUTTON);
a340b80d
VZ
1510
1511 // Windows-style: for smaller size control (and for disabled background) use less spacing
1512 focusSpacingX = isEnabled ? 2 : 1;
1513 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1514 }
1515 else
1516 {
1517 // Drawing a list item
1518 isEnabled = true; // they are never disabled
640a91ab 1519 doDrawFocusRect = (flags & wxCONTROL_SELECTED) != 0;
a340b80d
VZ
1520
1521 focusSpacingX = 0;
1522 focusSpacingY = 0;
1523 }
1524
1525 // Set the background sub-rectangle for selection, disabled etc
1526 wxRect selRect(rect);
1527 selRect.y += focusSpacingY;
1528 selRect.height -= (focusSpacingY*2);
8e5ec129
WS
1529
1530 int wcp = 0;
1531
1532 if ( !(flags & wxCONTROL_ISSUBMENU) )
1533 wcp += m_widthCustomPaint;
1534
1535 selRect.x += wcp + focusSpacingX;
1536 selRect.width -= wcp + (focusSpacingX*2);
a340b80d
VZ
1537
1538 wxColour bgCol;
3b9ba64c
JS
1539 wxColour fgCol;
1540
c905c0d6 1541 bool doDrawSelRect = true;
a340b80d 1542
3b9ba64c
JS
1543 // Determine foreground colour
1544 if ( isEnabled )
1545 {
1546 if ( doDrawFocusRect )
1547 {
1548 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1549 }
1550 else if ( m_hasFgCol )
1551 {
1552 // Honour the custom foreground colour
1553 fgCol = GetForegroundColour();
1554 }
1555 else
1556 {
1557 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1558 }
1559 }
1560 else
1561 {
1562 fgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
1563 }
1564
1565 // Determine background colour
a340b80d
VZ
1566 if ( isEnabled )
1567 {
c905c0d6 1568 if ( doDrawFocusRect )
a340b80d 1569 {
a340b80d
VZ
1570 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1571 }
3b9ba64c
JS
1572 else if ( m_hasBgCol )
1573 {
1574 // Honour the custom background colour
1575 bgCol = GetBackgroundColour();
1576 }
a340b80d
VZ
1577 else
1578 {
2dfa37d6 1579#ifndef __WXMAC__ // see note in OnThemeChange
c905c0d6 1580 doDrawSelRect = false;
a340b80d 1581 bgCol = GetBackgroundColour();
2dfa37d6
RD
1582#else
1583 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1584#endif
a340b80d
VZ
1585 }
1586 }
1587 else
1588 {
2dfa37d6 1589#ifndef __WXMAC__ // see note in OnThemeChange
a340b80d 1590 bgCol = GetBackgroundColour();
2dfa37d6
RD
1591#else
1592 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1593#endif
a340b80d
VZ
1594 }
1595
3b9ba64c 1596 dc.SetTextForeground( fgCol );
a340b80d 1597 dc.SetBrush( bgCol );
c905c0d6
VZ
1598 if ( doDrawSelRect )
1599 {
1600 dc.SetPen( bgCol );
1601 dc.DrawRectangle( selRect );
1602 }
118f5fbd
RR
1603
1604 // Don't clip exactly to the selection rectangle so we can draw
1605 // to the non-selected area in front of it.
1606 wxRect clipRect(rect.x,rect.y,
1607 (selRect.x+selRect.width)-rect.x,rect.height);
1608 dc.SetClippingRegion(clipRect);
a340b80d 1609}
118f5fbd
RR
1610#else
1611// Save the library size a bit for platforms that re-implement this.
1612void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1613{
1614}
1615#endif
a340b80d 1616
c905c0d6 1617void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags )
a340b80d
VZ
1618{
1619 int drawState = m_btnState;
1620
c905c0d6
VZ
1621 if ( (m_iFlags & wxCC_BUTTON_STAYS_DOWN) &&
1622 GetPopupWindowState() >= Animating )
a340b80d 1623 drawState |= wxCONTROL_PRESSED;
a340b80d
VZ
1624
1625 wxRect drawRect(rect.x+m_btnSpacingX,
1626 rect.y+((rect.height-m_btnSize.y)/2),
1627 m_btnSize.x,
1628 m_btnSize.y);
1629
1630 // Make sure area is not larger than the control
1631 if ( drawRect.y < rect.y )
1632 drawRect.y = rect.y;
1633 if ( drawRect.height > rect.height )
1634 drawRect.height = rect.height;
1635
1636 bool enabled = IsEnabled();
1637
1638 if ( !enabled )
1639 drawState |= wxCONTROL_DISABLED;
1640
1641 if ( !m_bmpNormal.Ok() )
1642 {
c905c0d6
VZ
1643 if ( flags & Button_BitmapOnly )
1644 return;
1645
a340b80d 1646 // Need to clear button background even if m_btn is present
c905c0d6 1647 if ( flags & Button_PaintBackground )
b61f4f77
WS
1648 {
1649 wxColour bgCol;
1650
1651 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1652 bgCol = GetParent()->GetBackgroundColour();
1653 else
1654 bgCol = GetBackgroundColour();
1655
1656 dc.SetBrush(bgCol);
1657 dc.SetPen(bgCol);
a340b80d 1658 dc.DrawRectangle(rect);
b61f4f77 1659 }
a340b80d
VZ
1660
1661 // Draw standard button
1662 wxRendererNative::Get().DrawComboBoxDropButton(this,
1663 dc,
1664 drawRect,
1665 drawState);
1666 }
1667 else
1668 {
1669 // Draw bitmap
1670
1671 wxBitmap* pBmp;
1672
1673 if ( !enabled )
1674 pBmp = &m_bmpDisabled;
1675 else if ( m_btnState & wxCONTROL_PRESSED )
1676 pBmp = &m_bmpPressed;
1677 else if ( m_btnState & wxCONTROL_CURRENT )
1678 pBmp = &m_bmpHover;
1679 else
1680 pBmp = &m_bmpNormal;
1681
1682 if ( m_blankButtonBg )
1683 {
1684 // If using blank button background, we need to clear its background
1685 // with button face colour instead of colour for rest of the control.
c905c0d6 1686 if ( flags & Button_PaintBackground )
a340b80d
VZ
1687 {
1688 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1689 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1690 dc.SetPen(bgCol);
1691 dc.SetBrush(bgCol);
1692 dc.DrawRectangle(rect);
1693 }
1694
c905c0d6
VZ
1695 if ( !(flags & Button_BitmapOnly) )
1696 {
1697 wxRendererNative::Get().DrawPushButton(this,
1698 dc,
1699 drawRect,
1700 drawState);
1701 }
a340b80d
VZ
1702 }
1703 else
1704
1705 {
1706 // Need to clear button background even if m_btn is present
1707 // (assume non-button background was cleared just before this call so brushes are good)
c905c0d6 1708 if ( flags & Button_PaintBackground )
a340b80d
VZ
1709 dc.DrawRectangle(rect);
1710 }
1711
1712 // Draw bitmap centered in drawRect
1713 dc.DrawBitmap(*pBmp,
1714 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1715 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1716 true);
1717 }
1718}
1719
a57d600f 1720void wxComboCtrlBase::RecalcAndRefresh()
a340b80d
VZ
1721{
1722 if ( IsCreated() )
1723 {
1724 wxSizeEvent evt(GetSize(),GetId());
1725 GetEventHandler()->ProcessEvent(evt);
1726 Refresh();
1727 }
1728}
1729
a340b80d
VZ
1730// ----------------------------------------------------------------------------
1731// miscellaneous event handlers
1732// ----------------------------------------------------------------------------
1733
a57d600f 1734void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
a340b80d 1735{
ce968519
RR
1736 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1737 {
1738 if ( m_ignoreEvtText > 0 )
1739 {
1740 m_ignoreEvtText--;
1741 return;
1742 }
1743 }
1744
98d0cd96 1745 // Change event id, object and string before relaying it forward
a340b80d 1746 event.SetId(GetId());
98d0cd96
WS
1747 wxString s = event.GetString();
1748 event.SetEventObject(this);
1749 event.SetString(s);
a340b80d
VZ
1750 event.Skip();
1751}
1752
1753// call if cursor is on button area or mouse is captured for the button
a57d600f 1754bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
c905c0d6 1755 int flags )
a340b80d
VZ
1756{
1757 int type = event.GetEventType();
1758
1759 if ( type == wxEVT_MOTION )
1760 {
c905c0d6
VZ
1761 if ( (flags & wxCC_MF_ON_BUTTON) &&
1762 IsPopupWindowState(Hidden) )
a340b80d
VZ
1763 {
1764 if ( !(m_btnState & wxCONTROL_CURRENT) )
1765 {
1766 // Mouse hover begins
1767 m_btnState |= wxCONTROL_CURRENT;
1768 if ( HasCapture() ) // Retain pressed state.
1769 m_btnState |= wxCONTROL_PRESSED;
1770 Refresh();
1771 }
1772 }
1773 else if ( (m_btnState & wxCONTROL_CURRENT) )
1774 {
1775 // Mouse hover ends
1776 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1777 Refresh();
1778 }
1779 }
974a12f8 1780 else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK )
a340b80d 1781 {
1efad474 1782 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
a340b80d 1783 {
1efad474
RR
1784 m_btnState |= wxCONTROL_PRESSED;
1785 Refresh();
a340b80d 1786
1efad474
RR
1787 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1788 OnButtonClick();
1789 else
1790 // If showing popup now, do not capture mouse or there will be interference
1791 CaptureMouse();
a340b80d 1792 }
a340b80d
VZ
1793 }
1794 else if ( type == wxEVT_LEFT_UP )
1795 {
1796
1797 // Only accept event if mouse was left-press was previously accepted
1798 if ( HasCapture() )
1799 ReleaseMouse();
1800
1801 if ( m_btnState & wxCONTROL_PRESSED )
1802 {
1803 // If mouse was inside, fire the click event.
1804 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1805 {
1efad474 1806 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
a340b80d
VZ
1807 OnButtonClick();
1808 }
1809
1810 m_btnState &= ~(wxCONTROL_PRESSED);
1811 Refresh();
1812 }
1813 }
1814 else if ( type == wxEVT_LEAVE_WINDOW )
1815 {
1816 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1817 {
1818 m_btnState &= ~(wxCONTROL_CURRENT);
1819
1820 // Mouse hover ends
974a12f8 1821 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
1822 {
1823 m_btnState &= ~(wxCONTROL_PRESSED);
1824 Refresh();
1825 }
1826 }
1827 }
1828 else
1829 return false;
1830
c905c0d6
VZ
1831 // Never have 'hot' state when popup is being shown
1832 // (this is mostly needed because of the animation).
1833 if ( !IsPopupWindowState(Hidden) )
1834 m_btnState &= ~wxCONTROL_CURRENT;
1835
a340b80d
VZ
1836 return true;
1837}
1838
a340b80d 1839// returns true if event was consumed or filtered
a57d600f 1840bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
b104d1f0 1841 int WXUNUSED(flags) )
a340b80d
VZ
1842{
1843 wxLongLong t = ::wxGetLocalTimeMillis();
1844 int evtType = event.GetEventType();
1845
fd5b4315 1846#if USES_WXPOPUPWINDOW || USES_GENERICTLW
06077aaf 1847 if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW )
c667b518 1848 {
974a12f8 1849 if ( IsPopupWindowState(Visible) &&
06077aaf
VZ
1850 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1851 {
a1d5aa93 1852 HidePopup(true);
06077aaf
VZ
1853 return true;
1854 }
c667b518
VZ
1855 }
1856#endif
1857
c905c0d6 1858 // Filter out clicks on button immediately after popup dismiss
a340b80d
VZ
1859 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1860 {
1861 event.SetEventType(0);
1862 return true;
1863 }
1864
1865 return false;
1866}
1867
a57d600f 1868void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
a340b80d
VZ
1869{
1870 int evtType = event.GetEventType();
1871
1872 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1873 (m_windowStyle & wxCB_READONLY) )
1874 {
974a12f8 1875 if ( GetPopupWindowState() >= Animating )
a340b80d 1876 {
06077aaf 1877 #if USES_WXPOPUPWINDOW
a340b80d 1878 // Click here always hides the popup.
06077aaf 1879 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
a1d5aa93 1880 HidePopup(true);
a340b80d
VZ
1881 #endif
1882 }
1883 else
1884 {
1885 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1886 {
1887 // In read-only mode, clicking the text is the
1888 // same as clicking the button.
1889 OnButtonClick();
1890 }
1891 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1892 {
1893 //if ( m_popupInterface->CycleValue() )
1894 // Refresh();
1895 if ( m_popupInterface )
1896 m_popupInterface->OnComboDoubleClick();
1897 }
1898 }
1899 }
f4084513 1900 else if ( evtType == wxEVT_MOUSEWHEEL )
a340b80d 1901 {
f4084513
VS
1902 if ( IsPopupShown() )
1903 {
1904 // relay (some) mouse events to the popup
174b5c87 1905 m_popup->GetEventHandler()->ProcessEvent(event);
f4084513
VS
1906 }
1907 else if ( event.GetWheelAxis() == 0 &&
1908 event.GetWheelRotation() != 0 &&
1909 event.GetModifiers() == 0 )
1910 {
1911 // Translate mousewheel actions into key up/down. This is
1912 // the simplest way of getting native behaviour: scrolling the
1913 // wheel moves selection up/down by one item.
1914 wxKeyEvent kevent(wxEVT_KEY_DOWN);
1915 kevent.m_keyCode = event.GetWheelRotation() > 0
1916 ? WXK_UP
1917 : WXK_DOWN;
174b5c87 1918 GetEventHandler()->ProcessEvent(kevent);
f4084513
VS
1919 }
1920 else
1921 {
1922 event.Skip();
1923 }
a340b80d
VZ
1924 }
1925 else if ( evtType )
f4084513 1926 {
a340b80d 1927 event.Skip();
f4084513 1928 }
a340b80d
VZ
1929}
1930
b445b6a7 1931void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
a340b80d 1932{
b445b6a7
VZ
1933 if ( IsPopupShown() )
1934 {
1935 // pass it to the popped up control
174b5c87 1936 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
b445b6a7
VZ
1937 }
1938 else // no popup
1939 {
bbb86904
JS
1940 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
1941
1942 if ( mainCtrl->GetParent()->HasFlag(wxTAB_TRAVERSAL) )
1943 {
1944 if ( mainCtrl->HandleAsNavigationKey(event) )
1945 return;
1946 }
b445b6a7
VZ
1947
1948 if ( IsKeyPopupToggle(event) )
1949 {
1950 OnButtonClick();
1951 return;
1952 }
1953
1954 int comboStyle = GetWindowStyle();
1955 wxComboPopup* popupInterface = GetPopupControl();
1956
1957 if ( !popupInterface )
1958 {
1959 event.Skip();
1960 return;
1961 }
1962
f029f1d1
VS
1963 int keycode = event.GetKeyCode();
1964
b445b6a7
VZ
1965 if ( (comboStyle & wxCB_READONLY) ||
1966 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
1967 {
1968 popupInterface->OnComboKeyEvent(event);
1969 }
1970 else
1971 event.Skip();
1972 }
1973}
1974
c765e6fb
VS
1975void wxComboCtrlBase::OnCharEvent(wxKeyEvent& event)
1976{
1977 if ( IsPopupShown() )
1978 {
1979 // pass it to the popped up control
1980 GetPopupControl()->GetControl()->GetEventHandler()->ProcessEvent(event);
1981 }
1982 else // no popup
1983 {
1984 wxComboPopup* popupInterface = GetPopupControl();
1985 if ( popupInterface )
1986 {
1987 popupInterface->OnComboCharEvent(event);
1988 }
1989 else
1990 {
1991 event.Skip();
1992 }
1993 }
1994}
1995
b445b6a7
VZ
1996void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1997{
1998 if ( event.GetEventType() == wxEVT_SET_FOCUS )
a340b80d 1999 {
9727e8a3
JS
2000 wxWindow* tc = GetTextCtrl();
2001 if ( tc && tc != DoFindFocus() )
c905c0d6 2002 {
9727e8a3 2003 tc->SetFocus();
c905c0d6 2004 }
a340b80d 2005 }
b445b6a7
VZ
2006
2007 Refresh();
a340b80d
VZ
2008}
2009
2dfa37d6
RD
2010void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) )
2011{
2012 if ( m_resetFocus )
2013 {
2014 m_resetFocus = false;
2015 wxWindow* tc = GetTextCtrl();
2016 if ( tc )
2017 tc->SetFocus();
2018 }
2019}
2020
a57d600f 2021void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
a340b80d
VZ
2022{
2023 OnThemeChange();
0847e36e
JS
2024 // left margin may also have changed
2025 if ( !(m_iFlags & wxCC_IFLAG_LEFT_MARGIN_SET) )
2026 m_marginLeft = GetNativeTextIndent();
a340b80d
VZ
2027 RecalcAndRefresh();
2028}
2029
2030// ----------------------------------------------------------------------------
2031// popup handling
2032// ----------------------------------------------------------------------------
2033
2034// Create popup window and the child control
a57d600f 2035void wxComboCtrlBase::CreatePopup()
a340b80d
VZ
2036{
2037 wxComboPopup* popupInterface = m_popupInterface;
2038 wxWindow* popup;
2039
2040 if ( !m_winPopup )
06077aaf
VZ
2041 {
2042#ifdef wxComboPopupWindowBase2
2043 if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP )
2044 {
fd5b4315 2045 #if !USES_GENERICTLW
06077aaf
VZ
2046 m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER );
2047 #else
fd5b4315
JS
2048 int tlwFlags = wxNO_BORDER;
2049 #ifdef wxCC_GENERIC_TLW_IS_FRAME
2050 tlwFlags |= wxFRAME_NO_TASKBAR;
2051 #endif
2052
2053 #ifdef wxCC_GENERIC_TLW_IS_NONOWNEDWINDOW
2054 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY,
2055 wxPoint(-21,-21), wxSize(20, 20),
2056 tlwFlags );
2057 #else
06077aaf
VZ
2058 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString,
2059 wxPoint(-21,-21), wxSize(20, 20),
fd5b4315
JS
2060 tlwFlags );
2061 #endif
06077aaf
VZ
2062 #endif
2063 m_popupWinType = SECONDARY_POPUP_TYPE;
2064 }
2065 else
c905c0d6 2066#endif // wxComboPopupWindowBase2
06077aaf
VZ
2067 {
2068 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
2069 m_popupWinType = PRIMARY_POPUP_TYPE;
2070 }
2071 m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this);
2072 m_winPopup->PushEventHandler(m_popupWinEvtHandler);
2073 }
a340b80d
VZ
2074
2075 popupInterface->Create(m_winPopup);
2076 m_popup = popup = popupInterface->GetControl();
2077
06315578
JS
2078 m_popupEvtHandler = new wxComboPopupEvtHandler(this);
2079 popup->PushEventHandler( m_popupEvtHandler );
a340b80d
VZ
2080
2081 // This may be helpful on some platforms
2082 // (eg. it bypasses a wxGTK popupwindow bug where
2083 // window is not initially hidden when it should be)
2084 m_winPopup->Hide();
2085
2086 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
2087}
2088
7ca4ac63
WS
2089// Destroy popup window and the child control
2090void wxComboCtrlBase::DestroyPopup()
2091{
a1d5aa93 2092 HidePopup(true);
c667b518 2093
7ca4ac63 2094 if ( m_popup )
06315578 2095 m_popup->RemoveEventHandler(m_popupEvtHandler);
7ca4ac63 2096
5276b0a5 2097 wxDELETE(m_popupEvtHandler);
7ca4ac63 2098
5276b0a5 2099 wxDELETE(m_popupInterface);
7ca4ac63
WS
2100
2101 if ( m_winPopup )
06077aaf
VZ
2102 {
2103 m_winPopup->RemoveEventHandler(m_popupWinEvtHandler);
5276b0a5 2104 wxDELETE(m_popupWinEvtHandler);
7ca4ac63 2105 m_winPopup->Destroy();
5276b0a5 2106 m_winPopup = NULL;
06077aaf 2107 }
7ca4ac63 2108
d3b9f782 2109 m_popup = NULL;
7ca4ac63
WS
2110}
2111
db53c6ea 2112void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
a340b80d 2113{
a57d600f 2114 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
6d0ce565 2115
7ca4ac63 2116 DestroyPopup();
a340b80d 2117
6d0ce565
VZ
2118 iface->InitBase(this);
2119 iface->Init();
2120
a340b80d
VZ
2121 m_popupInterface = iface;
2122
7ca4ac63 2123 if ( !iface->LazyCreate() )
a340b80d
VZ
2124 {
2125 CreatePopup();
2126 }
2127 else
2128 {
d3b9f782 2129 m_popup = NULL;
a340b80d
VZ
2130 }
2131
6d0ce565
VZ
2132 // This must be done after creation
2133 if ( m_valueString.length() )
2134 {
a340b80d 2135 iface->SetStringValue(m_valueString);
6d0ce565
VZ
2136 //Refresh();
2137 }
2138}
a340b80d 2139
6d0ce565 2140// Ensures there is atleast the default popup
a57d600f 2141void wxComboCtrlBase::EnsurePopupControl()
6d0ce565
VZ
2142{
2143 if ( !m_popupInterface )
2144 SetPopupControl(NULL);
a340b80d
VZ
2145}
2146
a57d600f 2147void wxComboCtrlBase::OnButtonClick()
a340b80d
VZ
2148{
2149 // Derived classes can override this method for totally custom
2150 // popup action
dc2b49b3 2151 switch ( GetPopupWindowState() )
a1d5aa93 2152 {
dc2b49b3
VS
2153 case Hidden:
2154 {
ffb9247a 2155 Popup();
dc2b49b3
VS
2156 break;
2157 }
2158
2159 case Animating:
2160 case Visible:
2161 {
2162 HidePopup(true);
2163 break;
2164 }
a1d5aa93 2165 }
a340b80d
VZ
2166}
2167
ffb9247a
JS
2168void wxComboCtrlBase::Popup()
2169{
2170 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId());
2171 event.SetEventObject(this);
2172 HandleWindowEvent(event);
2173
2174 ShowPopup();
2175}
2176
a57d600f 2177void wxComboCtrlBase::ShowPopup()
a340b80d 2178{
6d0ce565 2179 EnsurePopupControl();
974a12f8
RR
2180 wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") );
2181
2182 if ( IsPopupWindowState(Animating) )
2183 return;
a340b80d
VZ
2184
2185 SetFocus();
2186
2187 // Space above and below
2188 int screenHeight;
2189 wxPoint scrPos;
2190 int spaceAbove;
2191 int spaceBelow;
2192 int maxHeightPopup;
2193 wxSize ctrlSz = GetSize();
2194
2195 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
2196 scrPos = GetParent()->ClientToScreen(GetPosition());
2197
2198 spaceAbove = scrPos.y;
2199 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
2200
2201 maxHeightPopup = spaceBelow;
2202 if ( spaceAbove > spaceBelow )
2203 maxHeightPopup = spaceAbove;
2204
2205 // Width
2206 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
2207
2208 if ( widthPopup < m_widthMinPopup )
2209 widthPopup = m_widthMinPopup;
2210
2211 wxWindow* winPopup = m_winPopup;
2212 wxWindow* popup;
2213
2214 // Need to disable tab traversal of parent
2215 //
2216 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
2217 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
2218 // that if transient popup is open, then tab traversal is to be ignored.
2219 // However, I think this code would still be needed for cases where
2220 // transient popup doesn't work yet (wxWinCE?).
bbb86904
JS
2221 wxWindow* mainCtrl = GetMainWindowOfCompositeControl();
2222 wxWindow* parent = mainCtrl->GetParent();
a340b80d
VZ
2223 int parentFlags = parent->GetWindowStyle();
2224 if ( parentFlags & wxTAB_TRAVERSAL )
2225 {
2226 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
2227 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
2228 }
2229
2230 if ( !winPopup )
2231 {
2232 CreatePopup();
2233 winPopup = m_winPopup;
2234 popup = m_popup;
2235 }
2236 else
2237 {
2238 popup = m_popup;
2239 }
2240
9727e8a3 2241 winPopup->Enable();
a61c9122 2242
a340b80d
VZ
2243 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
2244
2245 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
2246 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
2247 maxHeightPopup);
2248
2249 popup->SetSize(adjustedSize);
2250 popup->Move(0,0);
2251 m_popupInterface->OnPopup();
2252
2253 //
2254 // Reposition and resize popup window
2255 //
2256
2257 wxSize szp = popup->GetSize();
2258
2259 int popupX;
2260 int popupY = scrPos.y + ctrlSz.y;
2261
b7540dc1 2262 // Default anchor is wxLEFT
a340b80d
VZ
2263 int anchorSide = m_anchorSide;
2264 if ( !anchorSide )
b7540dc1 2265 anchorSide = wxLEFT;
a340b80d 2266
b7540dc1
WS
2267 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
2268 int leftX = scrPos.x - m_extLeft;
c4b37573
VZ
2269
2270 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
2271 leftX -= ctrlSz.x;
2272
b7540dc1
WS
2273 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
2274
2275 // If there is not enough horizontal space, anchor on the other side.
2276 // If there is no space even then, place the popup at x 0.
2277 if ( anchorSide == wxRIGHT )
2278 {
2279 if ( rightX < 0 )
2280 {
2281 if ( (leftX+szp.x) < screenWidth )
2282 anchorSide = wxLEFT;
2283 else
2284 anchorSide = 0;
2285 }
2286 }
2287 else
2288 {
2289 if ( (leftX+szp.x) >= screenWidth )
2290 {
2291 if ( rightX >= 0 )
2292 anchorSide = wxRIGHT;
2293 else
2294 anchorSide = 0;
2295 }
2296 }
2297
2298 // Select x coordinate according to the anchor side
a340b80d 2299 if ( anchorSide == wxRIGHT )
b7540dc1
WS
2300 popupX = rightX;
2301 else if ( anchorSide == wxLEFT )
2302 popupX = leftX;
a340b80d 2303 else
b7540dc1 2304 popupX = 0;
a340b80d 2305
974a12f8
RR
2306 int showFlags = CanDeferShow;
2307
a340b80d
VZ
2308 if ( spaceBelow < szp.y )
2309 {
2310 popupY = scrPos.y - szp.y;
974a12f8 2311 showFlags |= ShowAbove;
a340b80d
VZ
2312 }
2313
974a12f8
RR
2314#if INSTALL_TOPLEV_HANDLER
2315 // Put top level window event handler into place
2316 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
2317 {
2318 if ( !m_toplevEvtHandler )
2319 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
a340b80d 2320
974a12f8
RR
2321 wxWindow* toplev = ::wxGetTopLevelParent( this );
2322 wxASSERT( toplev );
2323 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
2324 toplev->PushEventHandler( m_toplevEvtHandler );
2325 }
2326#endif
a340b80d
VZ
2327
2328 // Set string selection (must be this way instead of SetStringSelection)
2329 if ( m_text )
2330 {
2331 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2332 m_text->SelectAll();
2333
2334 m_popupInterface->SetStringValue( m_text->GetValue() );
2335 }
2336 else
2337 {
2338 // This is neede since focus/selection indication may change when popup is shown
a340b80d
VZ
2339 Refresh();
2340 }
2341
2342 // This must be after SetStringValue
974a12f8 2343 m_popupWinState = Animating;
a340b80d 2344
974a12f8 2345 wxRect popupWinRect( popupX, popupY, szp.x, szp.y );
a340b80d 2346
974a12f8 2347 m_popup = popup;
30be036c
RR
2348 if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) ||
2349 AnimateShow( popupWinRect, showFlags ) )
06077aaf 2350 {
974a12f8
RR
2351 DoShowPopup( popupWinRect, showFlags );
2352 }
2353}
a340b80d 2354
974a12f8
RR
2355bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) )
2356{
2357 return true;
2358}
2359
2360void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) )
2361{
2362 wxWindow* winPopup = m_winPopup;
2363
2364 if ( IsPopupWindowState(Animating) )
2365 {
2366 // Make sure the popup window is shown in the right position.
2367 // Should not matter even if animation already did this.
2368
2369 // Some platforms (GTK) may like SetSize and Move to be separate
2370 // (though the bug was probably fixed).
2371 winPopup->SetSize( rect );
2372
c905c0d6
VZ
2373#if USES_WXPOPUPTRANSIENTWINDOW
2374 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2375 ((wxPopupTransientWindow*)winPopup)->Popup(m_popup);
2376 else
2377#endif
2378 winPopup->Show();
974a12f8
RR
2379
2380 m_popupWinState = Visible;
52999871
JS
2381
2382 // If popup window was a generic top-level window, or the
2383 // wxPopupWindow implemenation on this platform is classified as
2384 // perfect, then we should be able to safely set focus to the popup
2385 // control.
2386 if ( IsPopupWinTypePerfect(m_popupWinType) )
2387 m_popup->SetFocus();
06077aaf 2388 }
974a12f8
RR
2389 else if ( IsPopupWindowState(Hidden) )
2390 {
2391 // Animation was aborted
a340b80d 2392
974a12f8
RR
2393 wxASSERT( !winPopup->IsShown() );
2394
2395 m_popupWinState = Hidden;
2396 }
c905c0d6
VZ
2397
2398 Refresh();
a340b80d
VZ
2399}
2400
a1d5aa93 2401void wxComboCtrlBase::OnPopupDismiss(bool generateEvent)
974a12f8 2402{
a340b80d 2403 // Just in case, avoid double dismiss
974a12f8 2404 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
2405 return;
2406
9727e8a3
JS
2407 // This must be set before focus - otherwise there will be recursive
2408 // OnPopupDismisses.
974a12f8 2409 m_popupWinState = Hidden;
a340b80d 2410
9727e8a3
JS
2411 //SetFocus();
2412 m_winPopup->Disable();
2413
a340b80d
VZ
2414 // Inform popup control itself
2415 m_popupInterface->OnDismiss();
2416
06315578
JS
2417 if ( m_popupEvtHandler )
2418 ((wxComboPopupEvtHandler*)m_popupEvtHandler)->OnPopupDismiss();
a340b80d
VZ
2419
2420#if INSTALL_TOPLEV_HANDLER
2421 // Remove top level window event handler
2422 if ( m_toplevEvtHandler )
2423 {
2424 wxWindow* toplev = ::wxGetTopLevelParent( this );
2425 if ( toplev )
2426 toplev->RemoveEventHandler( m_toplevEvtHandler );
2427 }
2428#endif
2429
974a12f8
RR
2430 m_timeCanAcceptClick = ::wxGetLocalTimeMillis();
2431
2432 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2433 m_timeCanAcceptClick += 150;
a340b80d
VZ
2434
2435 // If cursor not on dropdown button, then clear its state
2436 // (technically not required by all ports, but do it for all just in case)
22a35096 2437 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
a340b80d
VZ
2438 m_btnState = 0;
2439
2440 // Return parent's tab traversal flag.
2441 // See ShowPopup for notes.
2442 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
2443 {
2444 wxWindow* parent = GetParent();
2445 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
2446 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
2447 }
2448
2449 // refresh control (necessary even if m_text)
2450 Refresh();
2451
a340b80d 2452 SetFocus();
a1d5aa93
JS
2453
2454 if ( generateEvent )
2455 {
2456 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId());
2457 event.SetEventObject(this);
65702d2f 2458 HandleWindowEvent(event);
a1d5aa93 2459 }
a340b80d
VZ
2460}
2461
a1d5aa93 2462void wxComboCtrlBase::HidePopup(bool generateEvent)
a340b80d
VZ
2463{
2464 // Should be able to call this without popup interface
974a12f8 2465 if ( IsPopupWindowState(Hidden) )
a340b80d
VZ
2466 return;
2467
2468 // transfer value and show it in textctrl, if any
974a12f8 2469 if ( !IsPopupWindowState(Animating) )
0306e73e 2470 SetValueByUser( m_popupInterface->GetStringValue() );
a340b80d 2471
974a12f8 2472 m_winPopup->Hide();
a340b80d 2473
a1d5aa93 2474 OnPopupDismiss(generateEvent);
a340b80d
VZ
2475}
2476
2477// ----------------------------------------------------------------------------
2478// customization methods
2479// ----------------------------------------------------------------------------
2480
a57d600f 2481void wxComboCtrlBase::SetButtonPosition( int width, int height,
7dc234d6 2482 int side, int spacingX )
a340b80d
VZ
2483{
2484 m_btnWid = width;
2485 m_btnHei = height;
2486 m_btnSide = side;
2487 m_btnSpacingX = spacingX;
2488
735a300a
JS
2489 if ( width > 0 || height > 0 || spacingX )
2490 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
2491
a340b80d
VZ
2492 RecalcAndRefresh();
2493}
2494
7dc234d6
WS
2495wxSize wxComboCtrlBase::GetButtonSize()
2496{
2497 if ( m_btnSize.x > 0 )
2498 return m_btnSize;
2499
2500 wxSize retSize(m_btnWid,m_btnHei);
2501
2502 // Need to call CalculateAreas now if button size is
2503 // is not explicitly specified.
2504 if ( retSize.x <= 0 || retSize.y <= 0)
2505 {
2506 OnResize();
2507
2508 retSize = m_btnSize;
2509 }
2510
2511 return retSize;
2512}
2513
a57d600f 2514void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
a340b80d
VZ
2515 bool blankButtonBg,
2516 const wxBitmap& bmpPressed,
2517 const wxBitmap& bmpHover,
2518 const wxBitmap& bmpDisabled )
2519{
2520 m_bmpNormal = bmpNormal;
2521 m_blankButtonBg = blankButtonBg;
2522
2523 if ( bmpPressed.Ok() )
2524 m_bmpPressed = bmpPressed;
2525 else
2526 m_bmpPressed = bmpNormal;
2527
2528 if ( bmpHover.Ok() )
2529 m_bmpHover = bmpHover;
2530 else
2531 m_bmpHover = bmpNormal;
2532
2533 if ( bmpDisabled.Ok() )
2534 m_bmpDisabled = bmpDisabled;
2535 else
2536 m_bmpDisabled = bmpNormal;
2537
2538 RecalcAndRefresh();
2539}
2540
a57d600f 2541void wxComboCtrlBase::SetCustomPaintWidth( int width )
a340b80d
VZ
2542{
2543 if ( m_text )
2544 {
2545 // move textctrl accordingly
2546 wxRect r = m_text->GetRect();
2547 int inc = width - m_widthCustomPaint;
2548 r.x += inc;
2549 r.width -= inc;
2550 m_text->SetSize( r );
2551 }
2552
2553 m_widthCustomPaint = width;
2554
2555 RecalcAndRefresh();
2556}
2557
0847e36e
JS
2558bool wxComboCtrlBase::DoSetMargins(const wxPoint& margins)
2559{
2560 // For general sanity's sake, we ignore top margin. Instead
2561 // we will always try to center the text vertically.
2562 bool res = true;
2563
2564 if ( margins.x != -1 )
2565 {
2566 m_marginLeft = margins.x;
2567 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2568 }
2569 else
2570 {
2571 m_marginLeft = GetNativeTextIndent();
2572 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2573 }
2574
2575 if ( margins.y != -1 )
2576 {
2577 res = false;
2578 }
2579
2580 RecalcAndRefresh();
2581
2582 return res;
2583}
2584
2585wxPoint wxComboCtrlBase::DoGetMargins() const
2586{
2587 return wxPoint(m_marginLeft, -1);
2588}
2589
ddadf560 2590#if WXWIN_COMPATIBILITY_2_8
a57d600f 2591void wxComboCtrlBase::SetTextIndent( int indent )
a340b80d
VZ
2592{
2593 if ( indent < 0 )
2594 {
0847e36e
JS
2595 m_marginLeft = GetNativeTextIndent();
2596 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
a340b80d
VZ
2597 }
2598 else
2599 {
0847e36e
JS
2600 m_marginLeft = indent;
2601 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
a340b80d
VZ
2602 }
2603
2604 RecalcAndRefresh();
2605}
2606
48284cc7 2607wxCoord wxComboCtrlBase::GetTextIndent() const
0847e36e
JS
2608{
2609 return m_marginLeft;
2610}
2611#endif
2612
a57d600f 2613wxCoord wxComboCtrlBase::GetNativeTextIndent() const
a340b80d
VZ
2614{
2615 return DEFAULT_TEXT_INDENT;
2616}
2617
1ac5cfc7
JS
2618void wxComboCtrlBase::SetTextCtrlStyle( int style )
2619{
2620 m_textCtrlStyle = style;
2621
2622 if ( m_text )
2623 m_text->SetWindowStyle(style);
2624}
2625
a340b80d 2626// ----------------------------------------------------------------------------
fda62793 2627// wxTextEntry interface
a340b80d
VZ
2628// ----------------------------------------------------------------------------
2629
fda62793 2630wxString wxComboCtrlBase::DoGetValue() const
a340b80d
VZ
2631{
2632 if ( m_text )
2633 return m_text->GetValue();
2634 return m_valueString;
2635}
2636
0306e73e
JS
2637void wxComboCtrlBase::SetValueWithEvent(const wxString& value,
2638 bool withEvent)
a340b80d 2639{
0306e73e
JS
2640 DoSetValue(value, withEvent ? SetValue_SendEvent : 0);
2641}
22b6de6a 2642
0306e73e
JS
2643void wxComboCtrlBase::OnSetValue(const wxString& value)
2644{
2645 // Note: before wxComboCtrl inherited from wxTextEntry,
2646 // this code used to be in SetValueWithEvent().
a340b80d
VZ
2647
2648 // Since wxComboPopup may want to paint the combo as well, we need
2649 // to set the string value here (as well as sometimes in ShowPopup).
4738d674 2650 if ( m_valueString != value )
a340b80d 2651 {
238b33ab
JS
2652 bool found = true;
2653 wxString trueValue = value;
2654
2655 // Conform to wxComboBox behavior: read-only control can only accept
2656 // valid list items and empty string
2657 if ( m_popupInterface && HasFlag(wxCB_READONLY) && value.length() )
2658 {
2659 found = m_popupInterface->FindItem(value,
2660 &trueValue);
2661 }
4738d674 2662
238b33ab
JS
2663 if ( found )
2664 {
2665 m_valueString = trueValue;
4738d674 2666
238b33ab
JS
2667 EnsurePopupControl();
2668
2669 if ( m_popupInterface )
2670 m_popupInterface->SetStringValue(trueValue);
2671 }
a340b80d 2672 }
4738d674
VZ
2673
2674 Refresh();
6d0ce565
VZ
2675}
2676
0306e73e 2677void wxComboCtrlBase::SetValueByUser(const wxString& value)
ce968519 2678{
0306e73e
JS
2679 // NB: Order of function calls is important here. Otherwise
2680 // the SelectAll() may not work.
2681
2682 if ( m_text )
2683 {
2684 m_text->SetValue(value);
2685
2686 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2687 m_text->SelectAll();
2688 }
2689
2690 OnSetValue(value);
ce968519
RR
2691}
2692
6d0ce565 2693// In this SetValue variant wxComboPopup::SetStringValue is not called
a57d600f 2694void wxComboCtrlBase::SetText(const wxString& value)
6d0ce565
VZ
2695{
2696 // Unlike in SetValue(), this must be called here or
2697 // the behaviour will no be consistent in readonlys.
2698 EnsurePopupControl();
a340b80d
VZ
2699
2700 m_valueString = value;
2701
ce968519
RR
2702 if ( m_text )
2703 {
2704 m_ignoreEvtText++;
2705 m_text->SetValue( value );
2706 }
2707
a340b80d
VZ
2708 Refresh();
2709}
2710
a57d600f 2711void wxComboCtrlBase::Copy()
a340b80d
VZ
2712{
2713 if ( m_text )
2714 m_text->Copy();
2715}
2716
a57d600f 2717void wxComboCtrlBase::Cut()
a340b80d
VZ
2718{
2719 if ( m_text )
2720 m_text->Cut();
2721}
2722
a57d600f 2723void wxComboCtrlBase::Paste()
a340b80d
VZ
2724{
2725 if ( m_text )
2726 m_text->Paste();
2727}
2728
a57d600f 2729void wxComboCtrlBase::SetInsertionPoint(long pos)
a340b80d
VZ
2730{
2731 if ( m_text )
2732 m_text->SetInsertionPoint(pos);
2733}
2734
a57d600f 2735long wxComboCtrlBase::GetInsertionPoint() const
a340b80d
VZ
2736{
2737 if ( m_text )
2738 return m_text->GetInsertionPoint();
2739
2740 return 0;
2741}
2742
a57d600f 2743long wxComboCtrlBase::GetLastPosition() const
a340b80d
VZ
2744{
2745 if ( m_text )
2746 return m_text->GetLastPosition();
2747
2748 return 0;
2749}
2750
0306e73e 2751void wxComboCtrlBase::WriteText(const wxString& text)
a340b80d
VZ
2752{
2753 if ( m_text )
0306e73e
JS
2754 {
2755 m_text->WriteText(text);
2756 OnSetValue(m_text->GetValue());
2757 }
2758 else
2759 {
2760 OnSetValue(text);
2761 }
a340b80d
VZ
2762}
2763
0306e73e 2764void wxComboCtrlBase::DoSetValue(const wxString& value, int flags)
fda62793
JS
2765{
2766 if ( m_text )
0306e73e
JS
2767 {
2768 if ( flags & SetValue_SendEvent )
2769 m_text->SetValue(value);
2770 else
2771 m_text->ChangeValue(value);
2772 }
2773
2774 OnSetValue(value);
2775}
2776
2777void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2778{
2779 if ( m_text )
2780 {
2781 m_text->Replace(from, to, value);
2782 OnSetValue(m_text->GetValue());
2783 }
fda62793
JS
2784}
2785
a57d600f 2786void wxComboCtrlBase::Remove(long from, long to)
a340b80d
VZ
2787{
2788 if ( m_text )
0306e73e 2789 {
a340b80d 2790 m_text->Remove(from, to);
0306e73e
JS
2791 OnSetValue(m_text->GetValue());
2792 }
a340b80d
VZ
2793}
2794
a57d600f 2795void wxComboCtrlBase::SetSelection(long from, long to)
a340b80d
VZ
2796{
2797 if ( m_text )
2798 m_text->SetSelection(from, to);
2799}
2800
fda62793
JS
2801void wxComboCtrlBase::GetSelection(long *from, long *to) const
2802{
2803 if ( m_text )
2804 {
2805 m_text->GetSelection(from, to);
2806 }
2807 else
2808 {
2809 *from = 0;
2810 *to = 0;
2811 }
2812}
2813
2814bool wxComboCtrlBase::IsEditable() const
2815{
2816 if ( m_text )
2817 return m_text->IsEditable();
2818 return false;
2819}
2820
2821void wxComboCtrlBase::SetEditable(bool editable)
2822{
2823 if ( m_text )
2824 m_text->SetEditable(editable);
2825}
2826
a57d600f 2827void wxComboCtrlBase::Undo()
a340b80d
VZ
2828{
2829 if ( m_text )
2830 m_text->Undo();
2831}
2832
fda62793
JS
2833void wxComboCtrlBase::Redo()
2834{
2835 if ( m_text )
2836 m_text->Redo();
2837}
2838
2839bool wxComboCtrlBase::CanUndo() const
2840{
2841 if ( m_text )
2842 return m_text->CanUndo();
2843
2844 return false;
2845}
2846
2847bool wxComboCtrlBase::CanRedo() const
2848{
2849 if ( m_text )
2850 return m_text->CanRedo();
2851
2852 return false;
2853}
2854
107defe3
JS
2855bool wxComboCtrlBase::SetHint(const wxString& hint)
2856{
2857 m_hintText = hint;
2858 bool res = true;
2859 if ( m_text )
2860 res = m_text->SetHint(hint);
2861 Refresh();
2862 return res;
2863}
2864
2865wxString wxComboCtrlBase::GetHint() const
2866{
2867 return m_hintText;
2868}
2869
a57d600f 2870#endif // wxUSE_COMBOCTRL