]>
Commit | Line | Data |
---|---|---|
ec376c8f VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/pickerbase.cpp | |
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 | ||
27 | #include "wx/pickerbase.h" | |
28 | ||
29 | ||
30 | // ============================================================================ | |
31 | // implementation | |
32 | // ============================================================================ | |
33 | ||
34 | IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxWindow) | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // wxPickerBase | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | wxPickerBase::~wxPickerBase() | |
41 | { | |
42 | // destroy the windows we are managing: these are not automatically | |
43 | // destroyed by wxWindow because they are not built as our children | |
44 | // but rather as children of the parent of the wxPickerBase class | |
45 | // (since wxPickerBase does not represent a real window) | |
46 | if (m_text) m_text->Destroy(); | |
47 | if (m_picker) m_picker->Destroy(); | |
48 | } | |
49 | ||
50 | bool wxPickerBase::CreateBase(wxWindow *parent, | |
51 | wxWindowID id, | |
52 | const wxString &text, | |
53 | const wxPoint& pos, | |
54 | const wxSize& size, | |
55 | long style, | |
56 | const wxValidator& validator, | |
57 | const wxString& name) | |
58 | { | |
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; | |
62 | if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL, | |
63 | validator, name)) | |
64 | return false; | |
65 | ||
66 | if (HasFlag(wxPB_USE_TEXTCTRL)) | |
67 | { | |
68 | // NOTE: the style of this class (wxPickerBase) and the style of the | |
69 | // attached text control are different: GetTextCtrlStyle() extracts | |
70 | // the styles related to the textctrl from the styles passed here | |
71 | m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxPoint(0, 0), | |
72 | wxSize(40, size.GetHeight()), GetTextCtrlStyle(style)); | |
73 | if (!m_text) | |
74 | { | |
75 | wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") ); | |
76 | return false; | |
77 | } | |
78 | ||
79 | // set the maximum lenght allowed for this textctrl. | |
80 | // This is very important since any change to it will trigger an update in | |
81 | // the m_picker; for very long strings, this real-time synchronization could | |
82 | // become a CPU-blocker and thus should be avoided. | |
83 | // 32 characters will be more than enough for all common uses. | |
84 | m_text->SetMaxLength(32); | |
85 | ||
86 | // set the initial contents of the textctrl | |
87 | m_text->SetValue(text); | |
88 | ||
89 | m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED, | |
90 | wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate), | |
91 | NULL, this); | |
92 | m_text->Connect(wxEVT_KILL_FOCUS, | |
93 | wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus), | |
94 | NULL, this); | |
95 | ||
96 | m_text->Connect(wxEVT_DESTROY, | |
97 | wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete), | |
98 | NULL, this); | |
99 | } | |
100 | ||
101 | return true; | |
102 | } | |
103 | ||
104 | void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent &) | |
105 | { | |
106 | wxASSERT(m_text); | |
107 | ||
108 | // don't leave the textctrl empty | |
109 | if (m_text->GetValue().IsEmpty()) | |
110 | UpdateTextCtrlFromPicker(); | |
111 | } | |
112 | ||
113 | void wxPickerBase::OnTextCtrlDelete(wxWindowDestroyEvent &) | |
114 | { | |
115 | // the textctrl has been deleted; our pointer is invalid! | |
116 | m_text = NULL; | |
117 | } | |
118 | ||
119 | void wxPickerBase::OnTextCtrlUpdate(wxCommandEvent &) | |
120 | { | |
121 | // for each text-change, update the picker | |
122 | UpdatePickerFromTextCtrl(); | |
123 | } | |
124 | ||
125 | int wxPickerBase::GetTextCtrlWidth(int given) | |
126 | { | |
127 | // compute the width of m_text like a wxBoxSizer(wxHORIZONTAL) would do | |
128 | // NOTE: the proportion of m_picker is fixed to 1 | |
129 | return ((given - m_margin) / (m_textProportion + 1)) * m_textProportion; | |
130 | } | |
131 | ||
132 | void wxPickerBase::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH) | |
133 | { | |
134 | wxControl::DoSetSizeHints(minW, minH, maxW, maxH, incW, incH); | |
135 | ||
136 | if (m_text) | |
137 | { | |
138 | // compute minWidth and maxWidth of the ausiliary textctrl | |
139 | int textCtrlMinW = -1, textCtrlMaxW = -1; | |
140 | if (minW != -1) | |
141 | { | |
142 | textCtrlMinW = GetTextCtrlWidth(minW); | |
143 | minW -= textCtrlMinW + m_margin; | |
144 | } | |
145 | ||
146 | if (maxW != -1) | |
147 | { | |
148 | textCtrlMaxW = GetTextCtrlWidth(maxW); | |
149 | maxW -= textCtrlMaxW + m_margin; | |
150 | } | |
151 | ||
152 | m_text->SetSizeHints(textCtrlMinW, minH, textCtrlMaxW, maxH, incW, incH); | |
153 | } | |
154 | ||
155 | if (m_picker) | |
156 | m_picker->SetSizeHints(minW, minH, maxW, maxH, incW, incH); | |
157 | } | |
158 | ||
159 | void wxPickerBase::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
160 | { | |
161 | wxControl::DoSetSize(x, y, width, height, sizeFlags); | |
162 | ||
163 | int pickerx = 0; | |
164 | if (m_text) | |
165 | { | |
166 | // compute width of the ausiliary textctrl | |
167 | int textCtrlW = GetTextCtrlWidth(width); | |
168 | ||
169 | // set the m_text's position relatively to this window | |
170 | m_text->SetSize(0, 0, textCtrlW, height, sizeFlags); | |
171 | ||
172 | // change position of the real picker | |
173 | pickerx += textCtrlW + m_margin; | |
174 | width -= textCtrlW + m_margin; | |
175 | } | |
176 | ||
177 | if (m_picker) | |
178 | m_picker->SetSize(pickerx, 0, width, height, sizeFlags); | |
179 | } | |
180 | ||
181 | wxSize wxPickerBase::DoGetBestSize() const | |
182 | { | |
183 | wxSize ret = m_picker->GetBestSize(); | |
184 | ||
185 | if (m_text) | |
186 | { | |
187 | wxSize sz = m_text->GetBestSize(); | |
188 | ||
189 | ret.SetWidth( ret.GetWidth() + sz.GetWidth() + m_margin ); | |
190 | ret.SetHeight( wxMax(ret.GetHeight(), sz.GetHeight()) ); | |
191 | } | |
192 | ||
193 | return ret; | |
194 | } |