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