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