]> git.saurik.com Git - wxWidgets.git/blob - src/common/combocmn.cpp
Add missing header files so they'll get installed
[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 if ( m_text )
701 m_text->Destroy();
702
703 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
704 // not used by the wxPropertyGrid and therefore the tab is processed by
705 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
706 // navigation event is then sent to the wrong window.
707 style |= wxTE_PROCESS_TAB;
708
709 if ( HasFlag(wxTE_PROCESS_ENTER) )
710 style |= wxTE_PROCESS_ENTER;
711
712 // Ignore EVT_TEXT generated by the constructor (but only
713 // if the event redirector already exists)
714 // NB: This must be " = 1" instead of "++";
715 if ( m_textEvtHandler )
716 m_ignoreEvtText = 1;
717 else
718 m_ignoreEvtText = 0;
719
720 m_text = new wxTextCtrl(this, wxID_ANY, m_valueString,
721 wxDefaultPosition, wxDefaultSize,
722 style, validator);
723
724 // This is required for some platforms (GTK+ atleast)
725 m_text->SetSizeHints(2,4);
726 }
727 }
728
729 void wxComboCtrlBase::OnThemeChange()
730 {
731 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
732 }
733
734 wxComboCtrlBase::~wxComboCtrlBase()
735 {
736 if ( HasCapture() )
737 ReleaseMouse();
738
739 #if INSTALL_TOPLEV_HANDLER
740 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
741 m_toplevEvtHandler = (wxEvtHandler*) NULL;
742 #endif
743
744 DestroyPopup();
745
746 if ( m_text )
747 m_text->RemoveEventHandler(m_textEvtHandler);
748
749 delete m_textEvtHandler;
750 }
751
752
753 // ----------------------------------------------------------------------------
754 // geometry stuff
755 // ----------------------------------------------------------------------------
756
757 // Recalculates button and textctrl areas
758 void wxComboCtrlBase::CalculateAreas( int btnWidth )
759 {
760 wxSize sz = GetClientSize();
761 int customBorder = m_widthCustomBorder;
762 int btnBorder; // border for button only
763
764 // check if button should really be outside the border: we'll do it it if
765 // its platform default or bitmap+pushbutton background is used, but not if
766 // there is vertical size adjustment or horizontal spacing.
767 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
768 (m_bmpNormal.Ok() && m_blankButtonBg) ) &&
769 m_btnSpacingX == 0 &&
770 m_btnHei <= 0 )
771 {
772 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
773 btnBorder = 0;
774 }
775 else
776 {
777 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
778 btnBorder = customBorder;
779 }
780
781 // Defaul indentation
782 if ( m_absIndent < 0 )
783 m_absIndent = GetNativeTextIndent();
784
785 int butWidth = btnWidth;
786
787 if ( butWidth <= 0 )
788 butWidth = m_btnWidDefault;
789 else
790 m_btnWidDefault = butWidth;
791
792 if ( butWidth <= 0 )
793 return;
794
795 int butHeight = sz.y - btnBorder*2;
796
797 // Adjust button width
798 if ( m_btnWid > 0 )
799 butWidth = m_btnWid;
800 else
801 {
802 // Adjust button width to match aspect ratio
803 // (but only if control is smaller than best size).
804 int bestHeight = GetBestSize().y;
805 int height = GetSize().y;
806
807 if ( height < bestHeight )
808 {
809 // Make very small buttons square, as it makes
810 // them accommodate arrow image better and still
811 // looks decent.
812 if ( height > 18 )
813 butWidth = (height*butWidth)/bestHeight;
814 else
815 butWidth = butHeight;
816 }
817 }
818
819 // Adjust button height
820 if ( m_btnHei > 0 )
821 butHeight = m_btnHei;
822
823 // Use size of normal bitmap if...
824 // It is larger
825 // OR
826 // button width is set to default and blank button bg is not drawn
827 if ( m_bmpNormal.Ok() )
828 {
829 int bmpReqWidth = m_bmpNormal.GetWidth();
830 int bmpReqHeight = m_bmpNormal.GetHeight();
831
832 // If drawing blank button background, we need to add some margin.
833 if ( m_blankButtonBg )
834 {
835 bmpReqWidth += BMP_BUTTON_MARGIN*2;
836 bmpReqHeight += BMP_BUTTON_MARGIN*2;
837 }
838
839 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
840 butWidth = bmpReqWidth;
841 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
842 butHeight = bmpReqHeight;
843
844 // Need to fix height?
845 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
846 {
847 int newY = butHeight+(customBorder*2);
848 SetClientSize(wxDefaultCoord,newY);
849 sz.y = newY;
850 }
851 }
852
853 int butAreaWid = butWidth + (m_btnSpacingX*2);
854
855 m_btnSize.x = butWidth;
856 m_btnSize.y = butHeight;
857
858 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
859 m_btnArea.y = btnBorder;
860 m_btnArea.width = butAreaWid;
861 m_btnArea.height = sz.y - (btnBorder*2);
862
863 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder;
864 m_tcArea.y = customBorder;
865 m_tcArea.width = sz.x - butAreaWid - (customBorder*2);
866 m_tcArea.height = sz.y - (customBorder*2);
867
868 /*
869 if ( m_text )
870 {
871 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
872 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
873 }
874 */
875 }
876
877 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
878 {
879 if ( !m_text )
880 return;
881
882 wxSize sz = GetClientSize();
883 int customBorder = m_widthCustomBorder;
884
885 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
886 {
887 // Centre textctrl
888 int tcSizeY = m_text->GetBestSize().y;
889 int diff = sz.y - tcSizeY;
890 int y = textCtrlYAdjust + (diff/2);
891
892 if ( y < customBorder )
893 y = customBorder;
894
895 m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust,
896 y,
897 m_tcArea.width - COMBO_MARGIN -
898 (textCtrlXAdjust + m_widthCustomPaint + m_absIndent),
899 -1 );
900
901 // Make sure textctrl doesn't exceed the bottom custom border
902 wxSize tsz = m_text->GetSize();
903 diff = (y + tsz.y) - (sz.y - customBorder);
904 if ( diff >= 0 )
905 {
906 tsz.y = tsz.y - diff - 1;
907 m_text->SetSize(tsz);
908 }
909 }
910 else
911 {
912 // If it has border, have textctrl will the entire text field.
913 m_text->SetSize( m_tcArea.x,
914 0,
915 sz.x - m_btnArea.width - m_widthCustomPaint - customBorder,
916 sz.y );
917 }
918 }
919
920 wxSize wxComboCtrlBase::DoGetBestSize() const
921 {
922 wxSize sizeText(150,0);
923
924 if ( m_text )
925 sizeText = m_text->GetBestSize();
926
927 // TODO: Better method to calculate close-to-native control height.
928
929 int fhei;
930 if ( m_font.Ok() )
931 fhei = (m_font.GetPointSize()*2) + 5;
932 else if ( wxNORMAL_FONT->Ok() )
933 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
934 else
935 fhei = sizeText.y + 4;
936
937 // Need to force height to accomodate bitmap?
938 int btnSizeY = m_btnSize.y;
939 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
940 fhei = btnSizeY;
941
942 // Control height doesn't depend on border
943 /*
944 // Add border
945 int border = m_windowStyle & wxBORDER_MASK;
946 if ( border == wxSIMPLE_BORDER )
947 fhei += 2;
948 else if ( border == wxNO_BORDER )
949 fhei += (m_widthCustomBorder*2);
950 else
951 // Sunken etc.
952 fhei += 4;
953 */
954
955 // Final adjustments
956 #ifdef __WXGTK__
957 fhei += 1;
958 #endif
959
960 wxSize ret(sizeText.x + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH,
961 fhei);
962
963 CacheBestSize(ret);
964 return ret;
965 }
966
967 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
968 {
969 if ( !IsCreated() )
970 return;
971
972 // defined by actual wxComboCtrls
973 OnResize();
974
975 event.Skip();
976 }
977
978 // ----------------------------------------------------------------------------
979 // standard operations
980 // ----------------------------------------------------------------------------
981
982 bool wxComboCtrlBase::Enable(bool enable)
983 {
984 if ( !wxControl::Enable(enable) )
985 return false;
986
987 if ( m_btn )
988 m_btn->Enable(enable);
989 if ( m_text )
990 m_text->Enable(enable);
991
992 return true;
993 }
994
995 bool wxComboCtrlBase::Show(bool show)
996 {
997 if ( !wxControl::Show(show) )
998 return false;
999
1000 if (m_btn)
1001 m_btn->Show(show);
1002
1003 if (m_text)
1004 m_text->Show(show);
1005
1006 return true;
1007 }
1008
1009 bool wxComboCtrlBase::SetFont ( const wxFont& font )
1010 {
1011 if ( !wxControl::SetFont(font) )
1012 return false;
1013
1014 if (m_text)
1015 m_text->SetFont(font);
1016
1017 return true;
1018 }
1019
1020 #if wxUSE_TOOLTIPS
1021 void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
1022 {
1023 wxControl::DoSetToolTip(tooltip);
1024
1025 // Set tool tip for button and text box
1026 if ( tooltip )
1027 {
1028 const wxString &tip = tooltip->GetTip();
1029 if ( m_text ) m_text->SetToolTip(tip);
1030 if ( m_btn ) m_btn->SetToolTip(tip);
1031 }
1032 else
1033 {
1034 if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL );
1035 if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL );
1036 }
1037 }
1038 #endif // wxUSE_TOOLTIPS
1039
1040 // ----------------------------------------------------------------------------
1041 // painting
1042 // ----------------------------------------------------------------------------
1043
1044 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1045 // prepare combo box background on area in a way typical on platform
1046 void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
1047 {
1048 wxSize sz = GetClientSize();
1049 bool isEnabled;
1050 bool isFocused; // also selected
1051
1052 // For smaller size control (and for disabled background) use less spacing
1053 int focusSpacingX;
1054 int focusSpacingY;
1055
1056 if ( !(flags & wxCONTROL_ISSUBMENU) )
1057 {
1058 // Drawing control
1059 isEnabled = IsEnabled();
1060 isFocused = ShouldDrawFocus();
1061
1062 // Windows-style: for smaller size control (and for disabled background) use less spacing
1063 focusSpacingX = isEnabled ? 2 : 1;
1064 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1065 }
1066 else
1067 {
1068 // Drawing a list item
1069 isEnabled = true; // they are never disabled
1070 isFocused = flags & wxCONTROL_SELECTED ? true : false;
1071
1072 focusSpacingX = 0;
1073 focusSpacingY = 0;
1074 }
1075
1076 // Set the background sub-rectangle for selection, disabled etc
1077 wxRect selRect(rect);
1078 selRect.y += focusSpacingY;
1079 selRect.height -= (focusSpacingY*2);
1080
1081 int wcp = 0;
1082
1083 if ( !(flags & wxCONTROL_ISSUBMENU) )
1084 wcp += m_widthCustomPaint;
1085
1086 selRect.x += wcp + focusSpacingX;
1087 selRect.width -= wcp + (focusSpacingX*2);
1088
1089 wxColour bgCol;
1090
1091 if ( isEnabled )
1092 {
1093 // If popup is hidden and this control is focused,
1094 // then draw the focus-indicator (selbgcolor background etc.).
1095 if ( isFocused )
1096 {
1097 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
1098 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1099 }
1100 else
1101 {
1102 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
1103 bgCol = GetBackgroundColour();
1104 }
1105 }
1106 else
1107 {
1108 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) );
1109 bgCol = GetBackgroundColour();
1110 }
1111
1112 dc.SetBrush( bgCol );
1113 dc.SetPen( bgCol );
1114 dc.DrawRectangle( selRect );
1115
1116 // Don't clip exactly to the selection rectangle so we can draw
1117 // to the non-selected area in front of it.
1118 wxRect clipRect(rect.x,rect.y,
1119 (selRect.x+selRect.width)-rect.x,rect.height);
1120 dc.SetClippingRegion(clipRect);
1121 }
1122 #else
1123 // Save the library size a bit for platforms that re-implement this.
1124 void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1125 {
1126 }
1127 #endif
1128
1129 void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, bool paintBg )
1130 {
1131 int drawState = m_btnState;
1132
1133 #ifdef __WXGTK__
1134 if ( m_isPopupShown )
1135 drawState |= wxCONTROL_PRESSED;
1136 #endif
1137
1138 wxRect drawRect(rect.x+m_btnSpacingX,
1139 rect.y+((rect.height-m_btnSize.y)/2),
1140 m_btnSize.x,
1141 m_btnSize.y);
1142
1143 // Make sure area is not larger than the control
1144 if ( drawRect.y < rect.y )
1145 drawRect.y = rect.y;
1146 if ( drawRect.height > rect.height )
1147 drawRect.height = rect.height;
1148
1149 bool enabled = IsEnabled();
1150
1151 if ( !enabled )
1152 drawState |= wxCONTROL_DISABLED;
1153
1154 if ( !m_bmpNormal.Ok() )
1155 {
1156 // Need to clear button background even if m_btn is present
1157 if ( paintBg )
1158 {
1159 wxColour bgCol;
1160
1161 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1162 bgCol = GetParent()->GetBackgroundColour();
1163 else
1164 bgCol = GetBackgroundColour();
1165
1166 dc.SetBrush(bgCol);
1167 dc.SetPen(bgCol);
1168 dc.DrawRectangle(rect);
1169 }
1170
1171 // Draw standard button
1172 wxRendererNative::Get().DrawComboBoxDropButton(this,
1173 dc,
1174 drawRect,
1175 drawState);
1176 }
1177 else
1178 {
1179 // Draw bitmap
1180
1181 wxBitmap* pBmp;
1182
1183 if ( !enabled )
1184 pBmp = &m_bmpDisabled;
1185 else if ( m_btnState & wxCONTROL_PRESSED )
1186 pBmp = &m_bmpPressed;
1187 else if ( m_btnState & wxCONTROL_CURRENT )
1188 pBmp = &m_bmpHover;
1189 else
1190 pBmp = &m_bmpNormal;
1191
1192 if ( m_blankButtonBg )
1193 {
1194 // If using blank button background, we need to clear its background
1195 // with button face colour instead of colour for rest of the control.
1196 if ( paintBg )
1197 {
1198 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1199 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1200 dc.SetPen(bgCol);
1201 dc.SetBrush(bgCol);
1202 dc.DrawRectangle(rect);
1203 }
1204
1205 wxRendererNative::Get().DrawPushButton(this,
1206 dc,
1207 drawRect,
1208 drawState);
1209
1210 }
1211 else
1212
1213 {
1214 // Need to clear button background even if m_btn is present
1215 // (assume non-button background was cleared just before this call so brushes are good)
1216 if ( paintBg )
1217 dc.DrawRectangle(rect);
1218 }
1219
1220 // Draw bitmap centered in drawRect
1221 dc.DrawBitmap(*pBmp,
1222 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1223 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1224 true);
1225 }
1226 }
1227
1228 void wxComboCtrlBase::RecalcAndRefresh()
1229 {
1230 if ( IsCreated() )
1231 {
1232 wxSizeEvent evt(GetSize(),GetId());
1233 GetEventHandler()->ProcessEvent(evt);
1234 Refresh();
1235 }
1236 }
1237
1238 // ----------------------------------------------------------------------------
1239 // miscellaneous event handlers
1240 // ----------------------------------------------------------------------------
1241
1242 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
1243 {
1244 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1245 {
1246 if ( m_ignoreEvtText > 0 )
1247 {
1248 m_ignoreEvtText--;
1249 return;
1250 }
1251 }
1252
1253 // Change event id, object and string before relaying it forward
1254 event.SetId(GetId());
1255 wxString s = event.GetString();
1256 event.SetEventObject(this);
1257 event.SetString(s);
1258 event.Skip();
1259 }
1260
1261 // call if cursor is on button area or mouse is captured for the button
1262 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
1263 int flags )
1264 {
1265 int type = event.GetEventType();
1266
1267 if ( type == wxEVT_MOTION )
1268 {
1269 if ( flags & wxCC_MF_ON_BUTTON )
1270 {
1271 if ( !(m_btnState & wxCONTROL_CURRENT) )
1272 {
1273 // Mouse hover begins
1274 m_btnState |= wxCONTROL_CURRENT;
1275 if ( HasCapture() ) // Retain pressed state.
1276 m_btnState |= wxCONTROL_PRESSED;
1277 Refresh();
1278 }
1279 }
1280 else if ( (m_btnState & wxCONTROL_CURRENT) )
1281 {
1282 // Mouse hover ends
1283 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1284 Refresh();
1285 }
1286 }
1287 else if ( type == wxEVT_LEFT_DOWN )
1288 {
1289 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1290 {
1291 m_btnState |= wxCONTROL_PRESSED;
1292 Refresh();
1293
1294 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1295 OnButtonClick();
1296 else
1297 // If showing popup now, do not capture mouse or there will be interference
1298 CaptureMouse();
1299 }
1300 }
1301 else if ( type == wxEVT_LEFT_UP )
1302 {
1303
1304 // Only accept event if mouse was left-press was previously accepted
1305 if ( HasCapture() )
1306 ReleaseMouse();
1307
1308 if ( m_btnState & wxCONTROL_PRESSED )
1309 {
1310 // If mouse was inside, fire the click event.
1311 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1312 {
1313 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1314 OnButtonClick();
1315 }
1316
1317 m_btnState &= ~(wxCONTROL_PRESSED);
1318 Refresh();
1319 }
1320 }
1321 else if ( type == wxEVT_LEAVE_WINDOW )
1322 {
1323 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1324 {
1325 m_btnState &= ~(wxCONTROL_CURRENT);
1326
1327 // Mouse hover ends
1328 if ( !m_isPopupShown )
1329 {
1330 m_btnState &= ~(wxCONTROL_PRESSED);
1331 Refresh();
1332 }
1333 }
1334 }
1335 else
1336 return false;
1337
1338 return true;
1339 }
1340
1341 // returns true if event was consumed or filtered
1342 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
1343 int WXUNUSED(flags) )
1344 {
1345 wxLongLong t = ::wxGetLocalTimeMillis();
1346 int evtType = event.GetEventType();
1347
1348 #if !USE_TRANSIENT_POPUP
1349 if ( m_isPopupShown &&
1350 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1351 {
1352 HidePopup();
1353 return true;
1354 }
1355 #endif
1356
1357 // Filter out clicks on button immediately after popup dismiss (Windows like behaviour)
1358 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1359 {
1360 event.SetEventType(0);
1361 return true;
1362 }
1363
1364 return false;
1365 }
1366
1367 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
1368 {
1369 int evtType = event.GetEventType();
1370
1371 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1372 (m_windowStyle & wxCB_READONLY) )
1373 {
1374 if ( m_isPopupShown )
1375 {
1376 #if !wxUSE_POPUPWIN
1377 // Normally do nothing - evt handler should close it for us
1378 #elif !USE_TRANSIENT_POPUP
1379 // Click here always hides the popup.
1380 HidePopup();
1381 #endif
1382 }
1383 else
1384 {
1385 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1386 {
1387 // In read-only mode, clicking the text is the
1388 // same as clicking the button.
1389 OnButtonClick();
1390 }
1391 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1392 {
1393 //if ( m_popupInterface->CycleValue() )
1394 // Refresh();
1395 if ( m_popupInterface )
1396 m_popupInterface->OnComboDoubleClick();
1397 }
1398 }
1399 }
1400 else
1401 if ( m_isPopupShown )
1402 {
1403 // relay (some) mouse events to the popup
1404 if ( evtType == wxEVT_MOUSEWHEEL )
1405 m_popup->AddPendingEvent(event);
1406 }
1407 else if ( evtType )
1408 event.Skip();
1409 }
1410
1411 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
1412 {
1413 if ( IsPopupShown() )
1414 {
1415 // pass it to the popped up control
1416 GetPopupControl()->GetControl()->AddPendingEvent(event);
1417 }
1418 else // no popup
1419 {
1420 int keycode = event.GetKeyCode();
1421
1422 if ( keycode == WXK_TAB )
1423 {
1424 wxNavigationKeyEvent evt;
1425 evt.SetFlags(wxNavigationKeyEvent::FromTab|
1426 (!event.ShiftDown() ? wxNavigationKeyEvent::IsForward
1427 : wxNavigationKeyEvent::IsBackward));
1428 evt.SetEventObject(this);
1429 GetParent()->GetEventHandler()->AddPendingEvent(evt);
1430 return;
1431 }
1432
1433 if ( IsKeyPopupToggle(event) )
1434 {
1435 OnButtonClick();
1436 return;
1437 }
1438
1439 int comboStyle = GetWindowStyle();
1440 wxComboPopup* popupInterface = GetPopupControl();
1441
1442 if ( !popupInterface )
1443 {
1444 event.Skip();
1445 return;
1446 }
1447
1448 if ( (comboStyle & wxCB_READONLY) ||
1449 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
1450 {
1451 popupInterface->OnComboKeyEvent(event);
1452 }
1453 else
1454 event.Skip();
1455 }
1456 }
1457
1458 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1459 {
1460 if ( event.GetEventType() == wxEVT_SET_FOCUS )
1461 {
1462 if ( m_text && m_text != ::wxWindow::FindFocus() )
1463 m_text->SetFocus();
1464 }
1465
1466 Refresh();
1467 }
1468
1469 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
1470 {
1471 OnThemeChange();
1472 // indentation may also have changed
1473 if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) )
1474 m_absIndent = GetNativeTextIndent();
1475 RecalcAndRefresh();
1476 }
1477
1478 // ----------------------------------------------------------------------------
1479 // popup handling
1480 // ----------------------------------------------------------------------------
1481
1482 // Create popup window and the child control
1483 void wxComboCtrlBase::CreatePopup()
1484 {
1485 wxComboPopup* popupInterface = m_popupInterface;
1486 wxWindow* popup;
1487
1488 if ( !m_winPopup )
1489 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
1490
1491 popupInterface->Create(m_winPopup);
1492 m_popup = popup = popupInterface->GetControl();
1493
1494 m_popupExtraHandler = new wxComboPopupExtraEventHandler(this);
1495 popup->PushEventHandler( m_popupExtraHandler );
1496
1497 // This may be helpful on some platforms
1498 // (eg. it bypasses a wxGTK popupwindow bug where
1499 // window is not initially hidden when it should be)
1500 m_winPopup->Hide();
1501
1502 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
1503 }
1504
1505 // Destroy popup window and the child control
1506 void wxComboCtrlBase::DestroyPopup()
1507 {
1508 HidePopup();
1509
1510 if ( m_popup )
1511 m_popup->RemoveEventHandler(m_popupExtraHandler);
1512
1513 delete m_popupExtraHandler;
1514
1515 delete m_popupInterface;
1516
1517 if ( m_winPopup )
1518 m_winPopup->Destroy();
1519
1520 m_popupExtraHandler = (wxEvtHandler*) NULL;
1521 m_popupInterface = (wxComboPopup*) NULL;
1522 m_winPopup = (wxWindow*) NULL;
1523 m_popup = (wxWindow*) NULL;
1524 }
1525
1526 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
1527 {
1528 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
1529
1530 DestroyPopup();
1531
1532 iface->InitBase(this);
1533 iface->Init();
1534
1535 m_popupInterface = iface;
1536
1537 if ( !iface->LazyCreate() )
1538 {
1539 CreatePopup();
1540 }
1541 else
1542 {
1543 m_popup = (wxWindow*) NULL;
1544 }
1545
1546 // This must be done after creation
1547 if ( m_valueString.length() )
1548 {
1549 iface->SetStringValue(m_valueString);
1550 //Refresh();
1551 }
1552 }
1553
1554 // Ensures there is atleast the default popup
1555 void wxComboCtrlBase::EnsurePopupControl()
1556 {
1557 if ( !m_popupInterface )
1558 SetPopupControl(NULL);
1559 }
1560
1561 void wxComboCtrlBase::OnButtonClick()
1562 {
1563 // Derived classes can override this method for totally custom
1564 // popup action
1565 ShowPopup();
1566 }
1567
1568 void wxComboCtrlBase::ShowPopup()
1569 {
1570 EnsurePopupControl();
1571 wxCHECK_RET( !IsPopupShown(), wxT("popup window already shown") );
1572
1573 SetFocus();
1574
1575 // Space above and below
1576 int screenHeight;
1577 wxPoint scrPos;
1578 int spaceAbove;
1579 int spaceBelow;
1580 int maxHeightPopup;
1581 wxSize ctrlSz = GetSize();
1582
1583 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
1584 scrPos = GetParent()->ClientToScreen(GetPosition());
1585
1586 spaceAbove = scrPos.y;
1587 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
1588
1589 maxHeightPopup = spaceBelow;
1590 if ( spaceAbove > spaceBelow )
1591 maxHeightPopup = spaceAbove;
1592
1593 // Width
1594 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
1595
1596 if ( widthPopup < m_widthMinPopup )
1597 widthPopup = m_widthMinPopup;
1598
1599 wxWindow* winPopup = m_winPopup;
1600 wxWindow* popup;
1601
1602 // Need to disable tab traversal of parent
1603 //
1604 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1605 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1606 // that if transient popup is open, then tab traversal is to be ignored.
1607 // However, I think this code would still be needed for cases where
1608 // transient popup doesn't work yet (wxWinCE?).
1609 wxWindow* parent = GetParent();
1610 int parentFlags = parent->GetWindowStyle();
1611 if ( parentFlags & wxTAB_TRAVERSAL )
1612 {
1613 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
1614 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
1615 }
1616
1617 if ( !winPopup )
1618 {
1619 CreatePopup();
1620 winPopup = m_winPopup;
1621 popup = m_popup;
1622 }
1623 else
1624 {
1625 popup = m_popup;
1626 }
1627
1628 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
1629
1630 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
1631 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
1632 maxHeightPopup);
1633
1634 popup->SetSize(adjustedSize);
1635 popup->Move(0,0);
1636 m_popupInterface->OnPopup();
1637
1638 //
1639 // Reposition and resize popup window
1640 //
1641
1642 wxSize szp = popup->GetSize();
1643
1644 int popupX;
1645 int popupY = scrPos.y + ctrlSz.y;
1646
1647 // Default anchor is wxLEFT
1648 int anchorSide = m_anchorSide;
1649 if ( !anchorSide )
1650 anchorSide = wxLEFT;
1651
1652 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
1653 int leftX = scrPos.x - m_extLeft;
1654 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
1655
1656 // If there is not enough horizontal space, anchor on the other side.
1657 // If there is no space even then, place the popup at x 0.
1658 if ( anchorSide == wxRIGHT )
1659 {
1660 if ( rightX < 0 )
1661 {
1662 if ( (leftX+szp.x) < screenWidth )
1663 anchorSide = wxLEFT;
1664 else
1665 anchorSide = 0;
1666 }
1667 }
1668 else
1669 {
1670 if ( (leftX+szp.x) >= screenWidth )
1671 {
1672 if ( rightX >= 0 )
1673 anchorSide = wxRIGHT;
1674 else
1675 anchorSide = 0;
1676 }
1677 }
1678
1679 // Select x coordinate according to the anchor side
1680 if ( anchorSide == wxRIGHT )
1681 popupX = rightX;
1682 else if ( anchorSide == wxLEFT )
1683 popupX = leftX;
1684 else
1685 popupX = 0;
1686
1687 if ( spaceBelow < szp.y )
1688 {
1689 popupY = scrPos.y - szp.y;
1690 }
1691
1692 // Move to position
1693 //wxLogDebug(wxT("popup scheduled position1: %i,%i"),ptp.x,ptp.y);
1694 //wxLogDebug(wxT("popup position1: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1695
1696 // Some platforms (GTK) may need these two to be separate
1697 winPopup->SetSize( szp.x, szp.y );
1698 winPopup->Move( popupX, popupY );
1699
1700 //wxLogDebug(wxT("popup position2: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y);
1701
1702 m_popup = popup;
1703
1704 // Set string selection (must be this way instead of SetStringSelection)
1705 if ( m_text )
1706 {
1707 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1708 m_text->SelectAll();
1709
1710 m_popupInterface->SetStringValue( m_text->GetValue() );
1711 }
1712 else
1713 {
1714 // This is neede since focus/selection indication may change when popup is shown
1715 Refresh();
1716 }
1717
1718 // This must be after SetStringValue
1719 m_isPopupShown = true;
1720
1721 // Show it
1722 #if USE_TRANSIENT_POPUP
1723 ((wxPopupTransientWindow*)winPopup)->Popup(popup);
1724 #else
1725 winPopup->Show();
1726 #endif
1727
1728 #if INSTALL_TOPLEV_HANDLER
1729 // Put top level window event handler into place
1730 if ( !m_toplevEvtHandler )
1731 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
1732
1733 wxWindow* toplev = ::wxGetTopLevelParent( this );
1734 wxASSERT( toplev );
1735 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
1736 toplev->PushEventHandler( m_toplevEvtHandler );
1737 #endif
1738
1739 }
1740
1741 void wxComboCtrlBase::OnPopupDismiss()
1742 {
1743 // Just in case, avoid double dismiss
1744 if ( !m_isPopupShown )
1745 return;
1746
1747 // *Must* set this before focus etc.
1748 m_isPopupShown = false;
1749
1750 // Inform popup control itself
1751 m_popupInterface->OnDismiss();
1752
1753 if ( m_popupExtraHandler )
1754 ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss();
1755
1756 #if INSTALL_TOPLEV_HANDLER
1757 // Remove top level window event handler
1758 if ( m_toplevEvtHandler )
1759 {
1760 wxWindow* toplev = ::wxGetTopLevelParent( this );
1761 if ( toplev )
1762 toplev->RemoveEventHandler( m_toplevEvtHandler );
1763 }
1764 #endif
1765
1766 m_timeCanAcceptClick = ::wxGetLocalTimeMillis() + 150;
1767
1768 // If cursor not on dropdown button, then clear its state
1769 // (technically not required by all ports, but do it for all just in case)
1770 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
1771 m_btnState = 0;
1772
1773 // Return parent's tab traversal flag.
1774 // See ShowPopup for notes.
1775 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
1776 {
1777 wxWindow* parent = GetParent();
1778 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
1779 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
1780 }
1781
1782 // refresh control (necessary even if m_text)
1783 Refresh();
1784
1785 #if !wxUSE_POPUPWIN
1786 SetFocus();
1787 #endif
1788
1789 }
1790
1791 void wxComboCtrlBase::HidePopup()
1792 {
1793 // Should be able to call this without popup interface
1794 //wxCHECK_RET( m_popupInterface, _T("no popup interface") );
1795 if ( !m_isPopupShown )
1796 return;
1797
1798 // transfer value and show it in textctrl, if any
1799 SetValue( m_popupInterface->GetStringValue() );
1800
1801 #if USE_TRANSIENT_POPUP
1802 ((wxPopupTransientWindow*)m_winPopup)->Dismiss();
1803 #else
1804 m_winPopup->Hide();
1805 #endif
1806
1807 OnPopupDismiss();
1808 }
1809
1810 // ----------------------------------------------------------------------------
1811 // customization methods
1812 // ----------------------------------------------------------------------------
1813
1814 void wxComboCtrlBase::SetButtonPosition( int width, int height,
1815 int side, int spacingX )
1816 {
1817 m_btnWid = width;
1818 m_btnHei = height;
1819 m_btnSide = side;
1820 m_btnSpacingX = spacingX;
1821
1822 RecalcAndRefresh();
1823 }
1824
1825 wxSize wxComboCtrlBase::GetButtonSize()
1826 {
1827 if ( m_btnSize.x > 0 )
1828 return m_btnSize;
1829
1830 wxSize retSize(m_btnWid,m_btnHei);
1831
1832 // Need to call CalculateAreas now if button size is
1833 // is not explicitly specified.
1834 if ( retSize.x <= 0 || retSize.y <= 0)
1835 {
1836 OnResize();
1837
1838 retSize = m_btnSize;
1839 }
1840
1841 return retSize;
1842 }
1843
1844 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
1845 bool blankButtonBg,
1846 const wxBitmap& bmpPressed,
1847 const wxBitmap& bmpHover,
1848 const wxBitmap& bmpDisabled )
1849 {
1850 m_bmpNormal = bmpNormal;
1851 m_blankButtonBg = blankButtonBg;
1852
1853 if ( bmpPressed.Ok() )
1854 m_bmpPressed = bmpPressed;
1855 else
1856 m_bmpPressed = bmpNormal;
1857
1858 if ( bmpHover.Ok() )
1859 m_bmpHover = bmpHover;
1860 else
1861 m_bmpHover = bmpNormal;
1862
1863 if ( bmpDisabled.Ok() )
1864 m_bmpDisabled = bmpDisabled;
1865 else
1866 m_bmpDisabled = bmpNormal;
1867
1868 RecalcAndRefresh();
1869 }
1870
1871 void wxComboCtrlBase::SetCustomPaintWidth( int width )
1872 {
1873 if ( m_text )
1874 {
1875 // move textctrl accordingly
1876 wxRect r = m_text->GetRect();
1877 int inc = width - m_widthCustomPaint;
1878 r.x += inc;
1879 r.width -= inc;
1880 m_text->SetSize( r );
1881 }
1882
1883 m_widthCustomPaint = width;
1884
1885 RecalcAndRefresh();
1886 }
1887
1888 void wxComboCtrlBase::SetTextIndent( int indent )
1889 {
1890 if ( indent < 0 )
1891 {
1892 m_absIndent = GetNativeTextIndent();
1893 m_iFlags &= ~(wxCC_IFLAG_INDENT_SET);
1894 }
1895 else
1896 {
1897 m_absIndent = indent;
1898 m_iFlags |= wxCC_IFLAG_INDENT_SET;
1899 }
1900
1901 RecalcAndRefresh();
1902 }
1903
1904 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
1905 {
1906 return DEFAULT_TEXT_INDENT;
1907 }
1908
1909 // ----------------------------------------------------------------------------
1910 // methods forwarded to wxTextCtrl
1911 // ----------------------------------------------------------------------------
1912
1913 wxString wxComboCtrlBase::GetValue() const
1914 {
1915 if ( m_text )
1916 return m_text->GetValue();
1917 return m_valueString;
1918 }
1919
1920 void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent)
1921 {
1922 if ( m_text )
1923 {
1924 if ( !withEvent )
1925 m_ignoreEvtText++;
1926
1927 m_text->SetValue(value);
1928 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1929 m_text->SelectAll();
1930 }
1931
1932 m_valueString = value;
1933
1934 Refresh();
1935
1936 // Since wxComboPopup may want to paint the combo as well, we need
1937 // to set the string value here (as well as sometimes in ShowPopup).
1938 if ( m_valueString != value && m_popupInterface )
1939 {
1940 m_popupInterface->SetStringValue(value);
1941 }
1942 }
1943
1944 void wxComboCtrlBase::SetValue(const wxString& value)
1945 {
1946 SetValueWithEvent(value, false);
1947 }
1948
1949 // In this SetValue variant wxComboPopup::SetStringValue is not called
1950 void wxComboCtrlBase::SetText(const wxString& value)
1951 {
1952 // Unlike in SetValue(), this must be called here or
1953 // the behaviour will no be consistent in readonlys.
1954 EnsurePopupControl();
1955
1956 m_valueString = value;
1957
1958 if ( m_text )
1959 {
1960 m_ignoreEvtText++;
1961 m_text->SetValue( value );
1962 }
1963
1964 Refresh();
1965 }
1966
1967 void wxComboCtrlBase::Copy()
1968 {
1969 if ( m_text )
1970 m_text->Copy();
1971 }
1972
1973 void wxComboCtrlBase::Cut()
1974 {
1975 if ( m_text )
1976 m_text->Cut();
1977 }
1978
1979 void wxComboCtrlBase::Paste()
1980 {
1981 if ( m_text )
1982 m_text->Paste();
1983 }
1984
1985 void wxComboCtrlBase::SetInsertionPoint(long pos)
1986 {
1987 if ( m_text )
1988 m_text->SetInsertionPoint(pos);
1989 }
1990
1991 void wxComboCtrlBase::SetInsertionPointEnd()
1992 {
1993 if ( m_text )
1994 m_text->SetInsertionPointEnd();
1995 }
1996
1997 long wxComboCtrlBase::GetInsertionPoint() const
1998 {
1999 if ( m_text )
2000 return m_text->GetInsertionPoint();
2001
2002 return 0;
2003 }
2004
2005 long wxComboCtrlBase::GetLastPosition() const
2006 {
2007 if ( m_text )
2008 return m_text->GetLastPosition();
2009
2010 return 0;
2011 }
2012
2013 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2014 {
2015 if ( m_text )
2016 m_text->Replace(from, to, value);
2017 }
2018
2019 void wxComboCtrlBase::Remove(long from, long to)
2020 {
2021 if ( m_text )
2022 m_text->Remove(from, to);
2023 }
2024
2025 void wxComboCtrlBase::SetSelection(long from, long to)
2026 {
2027 if ( m_text )
2028 m_text->SetSelection(from, to);
2029 }
2030
2031 void wxComboCtrlBase::Undo()
2032 {
2033 if ( m_text )
2034 m_text->Undo();
2035 }
2036
2037 #endif // wxUSE_COMBOCTRL