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