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