]>
Commit | Line | Data |
---|---|---|
ec376c8f | 1 | /////////////////////////////////////////////////////////////////////////////// |
fec9cc08 | 2 | // Name: src/common/pickerbase.cpp |
ec376c8f VZ |
3 | // Purpose: wxPickerBase class implementation |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 15/04/2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Francesco Montorsi | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
9a6384ca WS |
27 | #if wxUSE_COLOURPICKERCTRL || \ |
28 | wxUSE_DIRPICKERCTRL || \ | |
29 | wxUSE_FILEPICKERCTRL || \ | |
30 | wxUSE_FONTPICKERCTRL | |
31 | ||
ec376c8f | 32 | #include "wx/pickerbase.h" |
a2dc658b | 33 | #include "wx/tooltip.h" |
ec376c8f | 34 | |
fec9cc08 WS |
35 | #ifndef WX_PRECOMP |
36 | #include "wx/textctrl.h" | |
37 | #endif | |
ec376c8f | 38 | |
a2dc658b | 39 | |
ec376c8f VZ |
40 | // ============================================================================ |
41 | // implementation | |
42 | // ============================================================================ | |
43 | ||
3c3b3558 | 44 | IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl) |
ec376c8f | 45 | |
a65ffcb2 VZ |
46 | BEGIN_EVENT_TABLE(wxPickerBase, wxControl) |
47 | EVT_SIZE(wxPickerBase::OnSize) | |
48 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxPickerBase) | |
49 | END_EVENT_TABLE() | |
6c20e8f8 | 50 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxPickerBase, wxControl) |
a65ffcb2 VZ |
51 | |
52 | ||
ec376c8f VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxPickerBase | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
ec376c8f VZ |
57 | bool wxPickerBase::CreateBase(wxWindow *parent, |
58 | wxWindowID id, | |
59 | const wxString &text, | |
60 | const wxPoint& pos, | |
61 | const wxSize& size, | |
62 | long style, | |
63 | const wxValidator& validator, | |
55b43eaa | 64 | const wxString& name) |
ec376c8f VZ |
65 | { |
66 | // remove any border style from our style as wxPickerBase's window must be | |
67 | // invisible (user styles must be set on the textctrl or the platform-dependent picker) | |
68 | style &= ~wxBORDER_MASK; | |
03647350 | 69 | |
fec9cc08 | 70 | if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL, |
a65ffcb2 | 71 | validator, name)) |
ec376c8f | 72 | return false; |
03647350 | 73 | |
4dfa1721 | 74 | SetMinSize( size ); |
03647350 | 75 | |
a65ffcb2 VZ |
76 | m_sizer = new wxBoxSizer(wxHORIZONTAL); |
77 | ||
ec376c8f VZ |
78 | if (HasFlag(wxPB_USE_TEXTCTRL)) |
79 | { | |
80 | // NOTE: the style of this class (wxPickerBase) and the style of the | |
81 | // attached text control are different: GetTextCtrlStyle() extracts | |
82 | // the styles related to the textctrl from the styles passed here | |
5f6475c1 VZ |
83 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
84 | wxDefaultPosition, wxDefaultSize, | |
55b43eaa | 85 | GetTextCtrlStyle(style)); |
ec376c8f VZ |
86 | if (!m_text) |
87 | { | |
88 | wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") ); | |
89 | return false; | |
90 | } | |
91 | ||
92 | // set the maximum lenght allowed for this textctrl. | |
93 | // This is very important since any change to it will trigger an update in | |
94 | // the m_picker; for very long strings, this real-time synchronization could | |
95 | // become a CPU-blocker and thus should be avoided. | |
96 | // 32 characters will be more than enough for all common uses. | |
60433f3f | 97 | m_text->SetMaxLength(32); |
ec376c8f VZ |
98 | |
99 | // set the initial contents of the textctrl | |
100 | m_text->SetValue(text); | |
101 | ||
e70abc2d | 102 | m_text->Connect(m_text->GetId(), wxEVT_COMMAND_TEXT_UPDATED, |
ec376c8f VZ |
103 | wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate), |
104 | NULL, this); | |
e70abc2d | 105 | m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS, |
ec376c8f VZ |
106 | wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus), |
107 | NULL, this); | |
108 | ||
e70abc2d | 109 | m_text->Connect(m_text->GetId(), wxEVT_DESTROY, |
ec376c8f VZ |
110 | wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete), |
111 | NULL, this); | |
a65ffcb2 | 112 | |
ecd87e5b | 113 | // the text control's proportion values defaults to 2 |
a65ffcb2 | 114 | m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5); |
ec376c8f | 115 | } |
03647350 | 116 | |
ec376c8f VZ |
117 | return true; |
118 | } | |
119 | ||
a65ffcb2 VZ |
120 | void wxPickerBase::PostCreation() |
121 | { | |
ecd87e5b VZ |
122 | // the picker's proportion value defaults to 1 when there's no text control |
123 | // associated with it - in that case it defaults to 0 | |
124 | m_sizer->Add(m_picker, HasTextCtrl() ? 0 : 1, GetDefaultPickerCtrlFlag(), 5); | |
a65ffcb2 | 125 | |
df5f11fe VZ |
126 | // For aesthetic reasons, make sure the picker is at least as high as the |
127 | // associated text control and is always at least square. | |
128 | const wxSize pickerBestSize(m_picker->GetBestSize()); | |
0f27b663 | 129 | const wxSize textBestSize( HasTextCtrl() ? m_text->GetBestSize() : wxSize()); |
df5f11fe VZ |
130 | wxSize pickerMinSize; |
131 | pickerMinSize.y = wxMax(pickerBestSize.y, textBestSize.y); | |
6dfd1add | 132 | pickerMinSize.x = wxMax(pickerBestSize.x, pickerMinSize.y); |
df5f11fe VZ |
133 | if ( pickerMinSize != pickerBestSize ) |
134 | m_picker->SetMinSize(pickerMinSize); | |
135 | ||
a65ffcb2 | 136 | SetSizer(m_sizer); |
03647350 | 137 | |
4dfa1721 | 138 | SetInitialSize( GetMinSize() ); |
a65ffcb2 VZ |
139 | } |
140 | ||
8ef74b15 VZ |
141 | #if wxUSE_TOOLTIPS |
142 | ||
143 | void wxPickerBase::DoSetToolTip(wxToolTip *tip) | |
a2dc658b VZ |
144 | { |
145 | // don't set the tooltip on us but rather on our two child windows | |
146 | // as otherwise it would appear only when the cursor is placed on the | |
147 | // small area around the child windows which belong to wxPickerBase | |
148 | m_picker->SetToolTip(tip); | |
149 | ||
150 | // do a copy as wxWindow will own the pointer we pass | |
4f690a1d VZ |
151 | if ( m_text ) |
152 | m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL); | |
a2dc658b VZ |
153 | } |
154 | ||
8ef74b15 | 155 | #endif // wxUSE_TOOLTIPS |
a2dc658b VZ |
156 | |
157 | // ---------------------------------------------------------------------------- | |
158 | // wxPickerBase - event handlers | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
a7b15169 | 161 | void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event) |
ec376c8f | 162 | { |
a7b15169 | 163 | event.Skip(); |
ec376c8f VZ |
164 | |
165 | // don't leave the textctrl empty | |
bb4b11a2 | 166 | if (m_text && m_text->GetValue().empty()) |
ec376c8f VZ |
167 | UpdateTextCtrlFromPicker(); |
168 | } | |
169 | ||
170 | void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &) | |
171 | { | |
172 | // the textctrl has been deleted; our pointer is invalid! | |
173 | m_text = NULL; | |
174 | } | |
175 | ||
176 | void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &) | |
177 | { | |
178 | // for each text-change, update the picker | |
179 | UpdatePickerFromTextCtrl(); | |
180 | } | |
181 | ||
a65ffcb2 | 182 | void wxPickerBase::OnSize(wxSizeEvent &event) |
06f23ff9 | 183 | { |
a65ffcb2 VZ |
184 | if (GetAutoLayout()) |
185 | Layout(); | |
186 | event.Skip(); | |
06f23ff9 | 187 | } |
9a6384ca WS |
188 | |
189 | #endif // Any picker in use |