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