]> git.saurik.com Git - wxWidgets.git/blob - src/common/combocmn.cpp
[ 1574264 ] Fix height of wxComboCtrl's wxTextCtrl
[wxWidgets.git] / src / common / combocmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/combocmn.cpp
3 // Purpose: wxComboCtrlBase
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
26 #if wxUSE_COMBOCTRL
27
28 #include "wx/combobox.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/dialog.h"
35 #include "wx/timer.h"
36 #endif
37
38 #include "wx/tooltip.h"
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
55 #define DEFAULT_POPUP_HEIGHT 400
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)
65 #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered
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)
73 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
74
75 #elif defined(__WXMAC__)
76
77 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
78 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
79
80 #else
81
82 #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform)
83 #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered
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
142 class wxComboFrameEventHandler : public wxEvtHandler
143 {
144 public:
145 wxComboFrameEventHandler( wxComboCtrlBase* pCb );
146 virtual ~wxComboFrameEventHandler();
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
158 protected:
159 wxWindow* m_focusStart;
160 wxComboCtrlBase* m_combo;
161
162 private:
163 DECLARE_EVENT_TABLE()
164 };
165
166 BEGIN_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)
176 END_EVENT_TABLE()
177
178 wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo )
179 : wxEvtHandler()
180 {
181 m_combo = combo;
182 }
183
184 wxComboFrameEventHandler::~wxComboFrameEventHandler()
185 {
186 }
187
188 void wxComboFrameEventHandler::OnPopup()
189 {
190 m_focusStart = ::wxWindow::FindFocus();
191 }
192
193 void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event )
194 {
195 wxWindow* winFocused = ::wxWindow::FindFocus();
196
197 wxWindow* popup = m_combo->GetPopupControl()->GetControl();
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
216 void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event )
217 {
218 m_combo->HidePopup();
219 event.Skip();
220 }
221
222 void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event )
223 {
224 m_combo->HidePopup();
225 event.Skip();
226 }
227
228 void wxComboFrameEventHandler::OnClose( wxCloseEvent& event )
229 {
230 m_combo->HidePopup();
231 event.Skip();
232 }
233
234 void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event )
235 {
236 m_combo->HidePopup();
237 event.Skip();
238 }
239
240 void wxComboFrameEventHandler::OnResize( wxSizeEvent& event )
241 {
242 m_combo->HidePopup();
243 event.Skip();
244 }
245
246 void 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
256 // wxComboCtrl.
257 // ----------------------------------------------------------------------------
258
259 class wxComboPopupWindow : public wxComboPopupWindowBase
260 {
261 public:
262
263 wxComboPopupWindow( wxComboCtrlBase *parent, int style = wxBORDER_NONE );
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
276 protected:
277
278 #if USE_TRANSIENT_POPUP
279 virtual void OnDismiss();
280 #endif
281
282 private:
283 DECLARE_EVENT_TABLE()
284 };
285
286
287 BEGIN_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)
294 END_EVENT_TABLE()
295
296
297 wxComboPopupWindow::wxComboPopupWindow( wxComboCtrlBase *parent,
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
312 void 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
322 void wxComboPopupWindow::OnMouseEvent( wxMouseEvent& event )
323 {
324 event.Skip();
325 }
326
327 #if !wxUSE_POPUPWIN
328 void wxComboPopupWindow::OnActivate( wxActivateEvent& event )
329 {
330 if ( !event.GetActive() )
331 {
332 // Tell combo control that we are dismissed.
333 wxComboCtrl* combo = (wxComboCtrl*) GetParent();
334 wxASSERT( combo );
335 wxASSERT( combo->IsKindOf(CLASSINFO(wxComboCtrl)) );
336
337 combo->HidePopup();
338
339 event.Skip();
340 }
341 }
342 #endif
343
344 #if USE_TRANSIENT_POPUP
345 bool 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.
353 void wxComboPopupWindow::OnDismiss()
354 {
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") );
358
359 combo->OnPopupDismiss();
360 }
361 #endif
362
363 // ----------------------------------------------------------------------------
364 // wxComboPopup
365 //
366 // ----------------------------------------------------------------------------
367
368 wxComboPopup::~wxComboPopup()
369 {
370 }
371
372 void wxComboPopup::OnPopup()
373 {
374 }
375
376 void wxComboPopup::OnDismiss()
377 {
378 }
379
380 wxSize wxComboPopup::GetAdjustedSize( int minWidth,
381 int prefHeight,
382 int WXUNUSED(maxHeight) )
383 {
384 return wxSize(minWidth,prefHeight);
385 }
386
387 void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo,
388 wxDC& dc, const wxRect& rect )
389 {
390 if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl
391 {
392 combo->PrepareBackground(dc,rect,0);
393
394 dc.DrawText( combo->GetValue(),
395 rect.x + combo->GetTextIndent(),
396 (rect.height-dc.GetCharHeight())/2 + rect.y );
397 }
398 }
399
400 void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
401 {
402 DefaultPaintComboControl(m_combo,dc,rect);
403 }
404
405 void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
406 {
407 event.Skip();
408 }
409
410 void wxComboPopup::OnComboDoubleClick()
411 {
412 }
413
414 void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) )
415 {
416 }
417
418 bool wxComboPopup::LazyCreate()
419 {
420 return false;
421 }
422
423 void wxComboPopup::Dismiss()
424 {
425 m_combo->HidePopup();
426 }
427
428 // ----------------------------------------------------------------------------
429 // input handling
430 // ----------------------------------------------------------------------------
431
432 //
433 // This is pushed to the event handler queue of the child textctrl.
434 //
435 class wxComboBoxExtraInputHandler : public wxEvtHandler
436 {
437 public:
438
439 wxComboBoxExtraInputHandler( wxComboCtrlBase* combo )
440 : wxEvtHandler()
441 {
442 m_combo = combo;
443 }
444 virtual ~wxComboBoxExtraInputHandler() { }
445 void OnKey(wxKeyEvent& event);
446 void OnFocus(wxFocusEvent& event);
447
448 protected:
449 wxComboCtrlBase* m_combo;
450
451 private:
452 DECLARE_EVENT_TABLE()
453 };
454
455
456 BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler)
457 EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey)
458 EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus)
459 END_EVENT_TABLE()
460
461
462 void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event)
463 {
464 // Let the wxComboCtrl event handler have a go first.
465 wxComboCtrlBase* combo = m_combo;
466 wxObject* prevObj = event.GetEventObject();
467
468 event.SetId(combo->GetId());
469 event.SetEventObject(combo);
470 combo->GetEventHandler()->ProcessEvent(event);
471
472 event.SetId(((wxWindow*)prevObj)->GetId());
473 event.SetEventObject(prevObj);
474 }
475
476 void 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
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);
497
498 event.Skip();
499 }
500
501
502 //
503 // This is pushed to the event handler queue of the control in popup.
504 //
505
506 class wxComboPopupExtraEventHandler : public wxEvtHandler
507 {
508 public:
509
510 wxComboPopupExtraEventHandler( wxComboCtrlBase* combo )
511 : wxEvtHandler()
512 {
513 m_combo = combo;
514 m_beenInside = false;
515 }
516 virtual ~wxComboPopupExtraEventHandler() { }
517
518 void OnMouseEvent( wxMouseEvent& event );
519
520 // Called from wxComboCtrlBase::OnPopupDismiss
521 void OnPopupDismiss()
522 {
523 m_beenInside = false;
524 }
525
526 protected:
527 wxComboCtrlBase* m_combo;
528
529 bool m_beenInside;
530
531 private:
532 DECLARE_EVENT_TABLE()
533 };
534
535
536 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler)
537 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent)
538 END_EVENT_TABLE()
539
540
541 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event )
542 {
543 wxPoint pt = event.GetPosition();
544 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
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 // ----------------------------------------------------------------------------
598 // wxComboCtrlBase
599 // ----------------------------------------------------------------------------
600
601
602 BEGIN_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)
608 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent)
609 EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
610 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged)
611 END_EVENT_TABLE()
612
613
614 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl)
615
616 void wxComboCtrlBase::Init()
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
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;
641 m_ignoreEvtText = 0;
642 m_btnWid = m_btnHei = -1;
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;
650 m_timeCanAcceptClick = 0;
651 }
652
653 bool wxComboCtrlBase::Create(wxWindow *parent,
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)
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
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
687 return true;
688 }
689
690 void wxComboCtrlBase::InstallInputHandlers()
691 {
692 if ( m_text )
693 {
694 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
695 m_text->PushEventHandler(m_textEvtHandler);
696 }
697 }
698
699 void
700 wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator)
701 {
702 if ( !(m_windowStyle & wxCB_READONLY) )
703 {
704 if ( m_text )
705 m_text->Destroy();
706
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
713 if ( HasFlag(wxTE_PROCESS_ENTER) )
714 style |= wxTE_PROCESS_ENTER;
715
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
724 m_text = new wxTextCtrl(this, wxID_ANY, m_valueString,
725 wxDefaultPosition, wxDefaultSize,
726 style, validator);
727
728 // This is required for some platforms (GTK+ atleast)
729 m_text->SetSizeHints(2,4);
730 }
731 }
732
733 void wxComboCtrlBase::OnThemeChange()
734 {
735 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
736 }
737
738 wxComboCtrlBase::~wxComboCtrlBase()
739 {
740 if ( HasCapture() )
741 ReleaseMouse();
742
743 #if INSTALL_TOPLEV_HANDLER
744 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
745 m_toplevEvtHandler = (wxEvtHandler*) NULL;
746 #endif
747
748 DestroyPopup();
749
750 if ( m_text )
751 m_text->RemoveEventHandler(m_textEvtHandler);
752
753 delete m_textEvtHandler;
754 }
755
756
757 // ----------------------------------------------------------------------------
758 // geometry stuff
759 // ----------------------------------------------------------------------------
760
761 // Recalculates button and textctrl areas
762 void wxComboCtrlBase::CalculateAreas( int btnWidth )
763 {
764 wxSize sz = GetClientSize();
765 int customBorder = m_widthCustomBorder;
766 int btnBorder; // border for button only
767
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 &&
774 m_btnHei <= 0 )
775 {
776 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
777 btnBorder = 0;
778 }
779 else
780 {
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
799 int butHeight = sz.y - btnBorder*2;
800
801 // Adjust button width
802 if ( m_btnWid > 0 )
803 butWidth = m_btnWid;
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;
810
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 }
822
823 // Adjust button height
824 if ( m_btnHei > 0 )
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);
852 SetClientSize(wxDefaultCoord,newY);
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
881 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
882 {
883 if ( !m_text )
884 return;
885
886 wxSize sz = GetClientSize();
887 int customBorder = m_widthCustomBorder;
888
889 #if !TEXTCTRL_TEXT_CENTERED
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
916 #endif
917 {
918 // If it has border, have textctrl will the entire text field.
919 m_text->SetSize( m_tcArea.x + m_widthCustomPaint,
920 customBorder,
921 sz.x - m_btnArea.width - m_widthCustomPaint - customBorder,
922 sz.y-(customBorder*2) );
923 }
924 }
925
926 wxSize wxComboCtrlBase::DoGetBestSize() const
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
973 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
974 {
975 if ( !IsCreated() )
976 return;
977
978 // defined by actual wxComboCtrls
979 OnResize();
980
981 event.Skip();
982 }
983
984 // ----------------------------------------------------------------------------
985 // standard operations
986 // ----------------------------------------------------------------------------
987
988 bool wxComboCtrlBase::Enable(bool enable)
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
1001 bool wxComboCtrlBase::Show(bool show)
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
1015 bool wxComboCtrlBase::SetFont ( const wxFont& font )
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
1027 void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
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
1050 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1051 // prepare combo box background on area in a way typical on platform
1052 void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
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);
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);
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 );
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);
1127 }
1128 #else
1129 // Save the library size a bit for platforms that re-implement this.
1130 void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1131 {
1132 }
1133 #endif
1134
1135 void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, bool paintBg )
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
1163 if ( paintBg )
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);
1174 dc.DrawRectangle(rect);
1175 }
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
1234 void wxComboCtrlBase::RecalcAndRefresh()
1235 {
1236 if ( IsCreated() )
1237 {
1238 wxSizeEvent evt(GetSize(),GetId());
1239 GetEventHandler()->ProcessEvent(evt);
1240 Refresh();
1241 }
1242 }
1243
1244 // ----------------------------------------------------------------------------
1245 // miscellaneous event handlers
1246 // ----------------------------------------------------------------------------
1247
1248 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
1249 {
1250 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1251 {
1252 if ( m_ignoreEvtText > 0 )
1253 {
1254 m_ignoreEvtText--;
1255 return;
1256 }
1257 }
1258
1259 // Change event id, object and string before relaying it forward
1260 event.SetId(GetId());
1261 wxString s = event.GetString();
1262 event.SetEventObject(this);
1263 event.SetString(s);
1264 event.Skip();
1265 }
1266
1267 // call if cursor is on button area or mouse is captured for the button
1268 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
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 {
1295 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1296 {
1297 m_btnState |= wxCONTROL_PRESSED;
1298 Refresh();
1299
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();
1305 }
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 {
1319 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
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
1347 // returns true if event was consumed or filtered
1348 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
1349 int WXUNUSED(flags) )
1350 {
1351 wxLongLong t = ::wxGetLocalTimeMillis();
1352 int evtType = event.GetEventType();
1353
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
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
1373 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
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
1417 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
1418 {
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();
1427
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
1464 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1465 {
1466 if ( event.GetEventType() == wxEVT_SET_FOCUS )
1467 {
1468 if ( m_text && m_text != ::wxWindow::FindFocus() )
1469 m_text->SetFocus();
1470 }
1471
1472 Refresh();
1473 }
1474
1475 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
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
1489 void wxComboCtrlBase::CreatePopup()
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
1511 // Destroy popup window and the child control
1512 void wxComboCtrlBase::DestroyPopup()
1513 {
1514 HidePopup();
1515
1516 if ( m_popup )
1517 m_popup->RemoveEventHandler(m_popupExtraHandler);
1518
1519 delete m_popupExtraHandler;
1520
1521 delete m_popupInterface;
1522
1523 if ( m_winPopup )
1524 m_winPopup->Destroy();
1525
1526 m_popupExtraHandler = (wxEvtHandler*) NULL;
1527 m_popupInterface = (wxComboPopup*) NULL;
1528 m_winPopup = (wxWindow*) NULL;
1529 m_popup = (wxWindow*) NULL;
1530 }
1531
1532 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
1533 {
1534 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
1535
1536 DestroyPopup();
1537
1538 iface->InitBase(this);
1539 iface->Init();
1540
1541 m_popupInterface = iface;
1542
1543 if ( !iface->LazyCreate() )
1544 {
1545 CreatePopup();
1546 }
1547 else
1548 {
1549 m_popup = (wxWindow*) NULL;
1550 }
1551
1552 // This must be done after creation
1553 if ( m_valueString.length() )
1554 {
1555 iface->SetStringValue(m_valueString);
1556 //Refresh();
1557 }
1558 }
1559
1560 // Ensures there is atleast the default popup
1561 void wxComboCtrlBase::EnsurePopupControl()
1562 {
1563 if ( !m_popupInterface )
1564 SetPopupControl(NULL);
1565 }
1566
1567 void wxComboCtrlBase::OnButtonClick()
1568 {
1569 // Derived classes can override this method for totally custom
1570 // popup action
1571 ShowPopup();
1572 }
1573
1574 void wxComboCtrlBase::ShowPopup()
1575 {
1576 EnsurePopupControl();
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
1653 // Default anchor is wxLEFT
1654 int anchorSide = m_anchorSide;
1655 if ( !anchorSide )
1656 anchorSide = wxLEFT;
1657
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
1686 if ( anchorSide == wxRIGHT )
1687 popupX = rightX;
1688 else if ( anchorSide == wxLEFT )
1689 popupX = leftX;
1690 else
1691 popupX = 0;
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
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
1747 void wxComboCtrlBase::OnPopupDismiss()
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)
1776 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
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
1797 void wxComboCtrlBase::HidePopup()
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
1820 void wxComboCtrlBase::SetButtonPosition( int width, int height,
1821 int side, int spacingX )
1822 {
1823 m_btnWid = width;
1824 m_btnHei = height;
1825 m_btnSide = side;
1826 m_btnSpacingX = spacingX;
1827
1828 RecalcAndRefresh();
1829 }
1830
1831 wxSize 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
1850 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
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
1877 void wxComboCtrlBase::SetCustomPaintWidth( int width )
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
1894 void wxComboCtrlBase::SetTextIndent( int indent )
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
1910 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
1911 {
1912 return DEFAULT_TEXT_INDENT;
1913 }
1914
1915 // ----------------------------------------------------------------------------
1916 // methods forwarded to wxTextCtrl
1917 // ----------------------------------------------------------------------------
1918
1919 wxString wxComboCtrlBase::GetValue() const
1920 {
1921 if ( m_text )
1922 return m_text->GetValue();
1923 return m_valueString;
1924 }
1925
1926 void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent)
1927 {
1928 if ( m_text )
1929 {
1930 if ( !withEvent )
1931 m_ignoreEvtText++;
1932
1933 m_text->SetValue(value);
1934 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1935 m_text->SelectAll();
1936 }
1937
1938 m_valueString = value;
1939
1940 Refresh();
1941
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 }
1948 }
1949
1950 void wxComboCtrlBase::SetValue(const wxString& value)
1951 {
1952 SetValueWithEvent(value, false);
1953 }
1954
1955 // In this SetValue variant wxComboPopup::SetStringValue is not called
1956 void wxComboCtrlBase::SetText(const wxString& value)
1957 {
1958 // Unlike in SetValue(), this must be called here or
1959 // the behaviour will no be consistent in readonlys.
1960 EnsurePopupControl();
1961
1962 m_valueString = value;
1963
1964 if ( m_text )
1965 {
1966 m_ignoreEvtText++;
1967 m_text->SetValue( value );
1968 }
1969
1970 Refresh();
1971 }
1972
1973 void wxComboCtrlBase::Copy()
1974 {
1975 if ( m_text )
1976 m_text->Copy();
1977 }
1978
1979 void wxComboCtrlBase::Cut()
1980 {
1981 if ( m_text )
1982 m_text->Cut();
1983 }
1984
1985 void wxComboCtrlBase::Paste()
1986 {
1987 if ( m_text )
1988 m_text->Paste();
1989 }
1990
1991 void wxComboCtrlBase::SetInsertionPoint(long pos)
1992 {
1993 if ( m_text )
1994 m_text->SetInsertionPoint(pos);
1995 }
1996
1997 void wxComboCtrlBase::SetInsertionPointEnd()
1998 {
1999 if ( m_text )
2000 m_text->SetInsertionPointEnd();
2001 }
2002
2003 long wxComboCtrlBase::GetInsertionPoint() const
2004 {
2005 if ( m_text )
2006 return m_text->GetInsertionPoint();
2007
2008 return 0;
2009 }
2010
2011 long wxComboCtrlBase::GetLastPosition() const
2012 {
2013 if ( m_text )
2014 return m_text->GetLastPosition();
2015
2016 return 0;
2017 }
2018
2019 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2020 {
2021 if ( m_text )
2022 m_text->Replace(from, to, value);
2023 }
2024
2025 void wxComboCtrlBase::Remove(long from, long to)
2026 {
2027 if ( m_text )
2028 m_text->Remove(from, to);
2029 }
2030
2031 void wxComboCtrlBase::SetSelection(long from, long to)
2032 {
2033 if ( m_text )
2034 m_text->SetSelection(from, to);
2035 }
2036
2037 void wxComboCtrlBase::Undo()
2038 {
2039 if ( m_text )
2040 m_text->Undo();
2041 }
2042
2043 #endif // wxUSE_COMBOCTRL