]>
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 | |
ec376c8f VZ |
7 | // Copyright: (c) Francesco Montorsi |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
9a6384ca WS |
26 | #if wxUSE_COLOURPICKERCTRL || \ |
27 | wxUSE_DIRPICKERCTRL || \ | |
28 | wxUSE_FILEPICKERCTRL || \ | |
29 | wxUSE_FONTPICKERCTRL | |
30 | ||
ec376c8f | 31 | #include "wx/pickerbase.h" |
a2dc658b | 32 | #include "wx/tooltip.h" |
ec376c8f | 33 | |
fec9cc08 WS |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/textctrl.h" | |
36 | #endif | |
ec376c8f | 37 | |
a2dc658b | 38 | |
ec376c8f VZ |
39 | // ============================================================================ |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
3c3b3558 | 43 | IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl) |
ec376c8f VZ |
44 | |
45 | // ---------------------------------------------------------------------------- | |
46 | // wxPickerBase | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
ec376c8f VZ |
49 | bool wxPickerBase::CreateBase(wxWindow *parent, |
50 | wxWindowID id, | |
51 | const wxString &text, | |
52 | const wxPoint& pos, | |
53 | const wxSize& size, | |
54 | long style, | |
55 | const wxValidator& validator, | |
55b43eaa | 56 | const wxString& name) |
ec376c8f VZ |
57 | { |
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; | |
03647350 | 61 | |
fec9cc08 | 62 | if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL, |
a65ffcb2 | 63 | validator, name)) |
ec376c8f | 64 | return false; |
03647350 | 65 | |
4dfa1721 | 66 | SetMinSize( size ); |
03647350 | 67 | |
a65ffcb2 VZ |
68 | m_sizer = new wxBoxSizer(wxHORIZONTAL); |
69 | ||
ec376c8f VZ |
70 | if (HasFlag(wxPB_USE_TEXTCTRL)) |
71 | { | |
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 | |
5f6475c1 VZ |
75 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
76 | wxDefaultPosition, wxDefaultSize, | |
55b43eaa | 77 | GetTextCtrlStyle(style)); |
ec376c8f VZ |
78 | if (!m_text) |
79 | { | |
80 | wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") ); | |
81 | return false; | |
82 | } | |
83 | ||
25e3f0c6 | 84 | // set the maximum length allowed for this textctrl. |
ec376c8f VZ |
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. | |
60433f3f | 89 | m_text->SetMaxLength(32); |
ec376c8f VZ |
90 | |
91 | // set the initial contents of the textctrl | |
92 | m_text->SetValue(text); | |
93 | ||
ce7fe42e | 94 | m_text->Connect(m_text->GetId(), wxEVT_TEXT, |
ec376c8f VZ |
95 | wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate), |
96 | NULL, this); | |
e70abc2d | 97 | m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS, |
ec376c8f VZ |
98 | wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus), |
99 | NULL, this); | |
100 | ||
e70abc2d | 101 | m_text->Connect(m_text->GetId(), wxEVT_DESTROY, |
ec376c8f VZ |
102 | wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete), |
103 | NULL, this); | |
a65ffcb2 | 104 | |
ecd87e5b | 105 | // the text control's proportion values defaults to 2 |
a65ffcb2 | 106 | m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5); |
ec376c8f | 107 | } |
03647350 | 108 | |
ec376c8f VZ |
109 | return true; |
110 | } | |
111 | ||
a65ffcb2 VZ |
112 | void wxPickerBase::PostCreation() |
113 | { | |
ecd87e5b VZ |
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); | |
a65ffcb2 | 117 | |
df5f11fe | 118 | // For aesthetic reasons, make sure the picker is at least as high as the |
75bc8b34 VZ |
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 | |
121 | // possible. | |
122 | if ( !HasFlag(wxPB_SMALL) ) | |
123 | { | |
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); | |
131 | } | |
df5f11fe | 132 | |
a65ffcb2 | 133 | SetSizer(m_sizer); |
03647350 | 134 | |
4dfa1721 | 135 | SetInitialSize( GetMinSize() ); |
a65ffcb2 VZ |
136 | } |
137 | ||
8ef74b15 VZ |
138 | #if wxUSE_TOOLTIPS |
139 | ||
140 | void wxPickerBase::DoSetToolTip(wxToolTip *tip) | |
a2dc658b VZ |
141 | { |
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); | |
146 | ||
147 | // do a copy as wxWindow will own the pointer we pass | |
4f690a1d VZ |
148 | if ( m_text ) |
149 | m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL); | |
a2dc658b VZ |
150 | } |
151 | ||
8ef74b15 | 152 | #endif // wxUSE_TOOLTIPS |
a2dc658b VZ |
153 | |
154 | // ---------------------------------------------------------------------------- | |
155 | // wxPickerBase - event handlers | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
a7b15169 | 158 | void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event) |
ec376c8f | 159 | { |
a7b15169 | 160 | event.Skip(); |
ec376c8f VZ |
161 | |
162 | // don't leave the textctrl empty | |
bb4b11a2 | 163 | if (m_text && m_text->GetValue().empty()) |
ec376c8f VZ |
164 | UpdateTextCtrlFromPicker(); |
165 | } | |
166 | ||
167 | void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &) | |
168 | { | |
169 | // the textctrl has been deleted; our pointer is invalid! | |
170 | m_text = NULL; | |
171 | } | |
172 | ||
173 | void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &) | |
174 | { | |
175 | // for each text-change, update the picker | |
176 | UpdatePickerFromTextCtrl(); | |
177 | } | |
178 | ||
9a6384ca | 179 | #endif // Any picker in use |