1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/pickerbase.cpp
3 // Purpose: wxPickerBase class implementation
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_COLOURPICKERCTRL || \
28 wxUSE_DIRPICKERCTRL || \
29 wxUSE_FILEPICKERCTRL || \
32 #include "wx/pickerbase.h"
33 #include "wx/tooltip.h"
36 #include "wx/textctrl.h"
40 // ============================================================================
42 // ============================================================================
44 IMPLEMENT_ABSTRACT_CLASS(wxPickerBase
, wxControl
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 bool wxPickerBase::CreateBase(wxWindow
*parent
,
56 const wxValidator
& validator
,
59 // remove any border style from our style as wxPickerBase's window must be
60 // invisible (user styles must be set on the textctrl or the platform-dependent picker)
61 style
&= ~wxBORDER_MASK
;
63 if (!wxControl::Create(parent
, id
, pos
, size
, style
| wxNO_BORDER
| wxTAB_TRAVERSAL
,
69 m_sizer
= new wxBoxSizer(wxHORIZONTAL
);
71 if (HasFlag(wxPB_USE_TEXTCTRL
))
73 // NOTE: the style of this class (wxPickerBase) and the style of the
74 // attached text control are different: GetTextCtrlStyle() extracts
75 // the styles related to the textctrl from the styles passed here
76 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
77 wxDefaultPosition
, wxDefaultSize
,
78 GetTextCtrlStyle(style
));
81 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
85 // set the maximum length allowed for this textctrl.
86 // This is very important since any change to it will trigger an update in
87 // the m_picker; for very long strings, this real-time synchronization could
88 // become a CPU-blocker and thus should be avoided.
89 // 32 characters will be more than enough for all common uses.
90 m_text
->SetMaxLength(32);
92 // set the initial contents of the textctrl
93 m_text
->SetValue(text
);
95 m_text
->Connect(m_text
->GetId(), wxEVT_COMMAND_TEXT_UPDATED
,
96 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate
),
98 m_text
->Connect(m_text
->GetId(), wxEVT_KILL_FOCUS
,
99 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus
),
102 m_text
->Connect(m_text
->GetId(), wxEVT_DESTROY
,
103 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete
),
106 // the text control's proportion values defaults to 2
107 m_sizer
->Add(m_text
, 2, GetDefaultTextCtrlFlag(), 5);
113 void wxPickerBase::PostCreation()
115 // the picker's proportion value defaults to 1 when there's no text control
116 // associated with it - in that case it defaults to 0
117 m_sizer
->Add(m_picker
, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5);
119 // For aesthetic reasons, make sure the picker is at least as high as the
120 // associated text control and is always at least square, unless we are
121 // explicitly using wxPB_SMALL style to force it to take as little space as
123 if ( !HasFlag(wxPB_SMALL
) )
125 const wxSize
pickerBestSize(m_picker
->GetBestSize());
126 const wxSize
textBestSize( HasTextCtrl() ? m_text
->GetBestSize() : wxSize());
127 wxSize pickerMinSize
;
128 pickerMinSize
.y
= wxMax(pickerBestSize
.y
, textBestSize
.y
);
129 pickerMinSize
.x
= wxMax(pickerBestSize
.x
, pickerMinSize
.y
);
130 if ( pickerMinSize
!= pickerBestSize
)
131 m_picker
->SetMinSize(pickerMinSize
);
136 SetInitialSize( GetMinSize() );
141 void wxPickerBase::DoSetToolTip(wxToolTip
*tip
)
143 // don't set the tooltip on us but rather on our two child windows
144 // as otherwise it would appear only when the cursor is placed on the
145 // small area around the child windows which belong to wxPickerBase
146 m_picker
->SetToolTip(tip
);
148 // do a copy as wxWindow will own the pointer we pass
150 m_text
->SetToolTip(tip
? new wxToolTip(tip
->GetTip()) : NULL
);
153 #endif // wxUSE_TOOLTIPS
155 // ----------------------------------------------------------------------------
156 // wxPickerBase - event handlers
157 // ----------------------------------------------------------------------------
159 void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent
& event
)
163 // don't leave the textctrl empty
164 if (m_text
&& m_text
->GetValue().empty())
165 UpdateTextCtrlFromPicker();
168 void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent
&)
170 // the textctrl has been deleted; our pointer is invalid!
174 void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent
&)
176 // for each text-change, update the picker
177 UpdatePickerFromTextCtrl();
180 #endif // Any picker in use