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