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