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