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