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