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