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