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