]>
git.saurik.com Git - wxWidgets.git/blob - src/common/pickerbase.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/pickerbase.cpp
3 // Purpose: wxPickerBase class implementation
4 // Author: Francesco Montorsi
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_COLOURPICKERCTRL || \
27 wxUSE_DIRPICKERCTRL || \
28 wxUSE_FILEPICKERCTRL || \
31 #include "wx/pickerbase.h"
32 #include "wx/tooltip.h"
35 #include "wx/textctrl.h"
39 // ============================================================================
41 // ============================================================================
43 IMPLEMENT_ABSTRACT_CLASS(wxPickerBase
, wxControl
)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 bool wxPickerBase::CreateBase(wxWindow
*parent
,
55 const wxValidator
& validator
,
58 // remove any border style from our style as wxPickerBase's window must be
59 // invisible (user styles must be set on the textctrl or the platform-dependent picker)
60 style
&= ~wxBORDER_MASK
;
62 if (!wxControl::Create(parent
, id
, pos
, size
, style
| wxNO_BORDER
| wxTAB_TRAVERSAL
,
68 m_sizer
= new wxBoxSizer(wxHORIZONTAL
);
70 if (HasFlag(wxPB_USE_TEXTCTRL
))
72 // NOTE: the style of this class (wxPickerBase) and the style of the
73 // attached text control are different: GetTextCtrlStyle() extracts
74 // the styles related to the textctrl from the styles passed here
75 m_text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
76 wxDefaultPosition
, wxDefaultSize
,
77 GetTextCtrlStyle(style
));
80 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
84 // set the maximum length allowed for this textctrl.
85 // This is very important since any change to it will trigger an update in
86 // the m_picker; for very long strings, this real-time synchronization could
87 // become a CPU-blocker and thus should be avoided.
88 // 32 characters will be more than enough for all common uses.
89 m_text
->SetMaxLength(32);
91 // set the initial contents of the textctrl
92 m_text
->SetValue(text
);
94 m_text
->Connect(m_text
->GetId(), wxEVT_TEXT
,
95 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate
),
97 m_text
->Connect(m_text
->GetId(), wxEVT_KILL_FOCUS
,
98 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus
),
101 m_text
->Connect(m_text
->GetId(), wxEVT_DESTROY
,
102 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete
),
105 // the text control's proportion values defaults to 2
106 m_sizer
->Add(m_text
, 2, GetDefaultTextCtrlFlag(), 5);
112 void wxPickerBase::PostCreation()
114 // the picker's proportion value defaults to 1 when there's no text control
115 // associated with it - in that case it defaults to 0
116 m_sizer
->Add(m_picker
, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5);
118 // For aesthetic reasons, make sure the picker is at least as high as the
119 // associated text control and is always at least square, unless we are
120 // explicitly using wxPB_SMALL style to force it to take as little space as
122 if ( !HasFlag(wxPB_SMALL
) )
124 const wxSize
pickerBestSize(m_picker
->GetBestSize());
125 const wxSize
textBestSize( HasTextCtrl() ? m_text
->GetBestSize() : wxSize());
126 wxSize pickerMinSize
;
127 pickerMinSize
.y
= wxMax(pickerBestSize
.y
, textBestSize
.y
);
128 pickerMinSize
.x
= wxMax(pickerBestSize
.x
, pickerMinSize
.y
);
129 if ( pickerMinSize
!= pickerBestSize
)
130 m_picker
->SetMinSize(pickerMinSize
);
135 SetInitialSize( GetMinSize() );
140 void wxPickerBase::DoSetToolTip(wxToolTip
*tip
)
142 // don't set the tooltip on us but rather on our two child windows
143 // as otherwise it would appear only when the cursor is placed on the
144 // small area around the child windows which belong to wxPickerBase
145 m_picker
->SetToolTip(tip
);
147 // do a copy as wxWindow will own the pointer we pass
149 m_text
->SetToolTip(tip
? new wxToolTip(tip
->GetTip()) : NULL
);
152 #endif // wxUSE_TOOLTIPS
154 // ----------------------------------------------------------------------------
155 // wxPickerBase - event handlers
156 // ----------------------------------------------------------------------------
158 void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent
& event
)
162 // don't leave the textctrl empty
163 if (m_text
&& m_text
->GetValue().empty())
164 UpdateTextCtrlFromPicker();
167 void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent
&)
169 // the textctrl has been deleted; our pointer is invalid!
173 void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent
&)
175 // for each text-change, update the picker
176 UpdatePickerFromTextCtrl();
179 #endif // Any picker in use