Remove all lines containing cvs/svn "$Id$" keyword.
[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 // 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
26 #if wxUSE_COLOURPICKERCTRL || \
27 wxUSE_DIRPICKERCTRL || \
28 wxUSE_FILEPICKERCTRL || \
29 wxUSE_FONTPICKERCTRL
30
31 #include "wx/pickerbase.h"
32 #include "wx/tooltip.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/textctrl.h"
36 #endif
37
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 IMPLEMENT_ABSTRACT_CLASS(wxPickerBase, wxControl)
44
45 // ----------------------------------------------------------------------------
46 // wxPickerBase
47 // ----------------------------------------------------------------------------
48
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,
56 const wxString& name)
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;
61
62 if (!wxControl::Create(parent, id, pos, size, style | wxNO_BORDER | wxTAB_TRAVERSAL,
63 validator, name))
64 return false;
65
66 SetMinSize( size );
67
68 m_sizer = new wxBoxSizer(wxHORIZONTAL);
69
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
75 m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
76 wxDefaultPosition, wxDefaultSize,
77 GetTextCtrlStyle(style));
78 if (!m_text)
79 {
80 wxFAIL_MSG( wxT("wxPickerBase's textctrl creation failed") );
81 return false;
82 }
83
84 // set the maximum length allowed for this textctrl.
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.
89 m_text->SetMaxLength(32);
90
91 // set the initial contents of the textctrl
92 m_text->SetValue(text);
93
94 m_text->Connect(m_text->GetId(), wxEVT_TEXT,
95 wxCommandEventHandler(wxPickerBase::OnTextCtrlUpdate),
96 NULL, this);
97 m_text->Connect(m_text->GetId(), wxEVT_KILL_FOCUS,
98 wxFocusEventHandler(wxPickerBase::OnTextCtrlKillFocus),
99 NULL, this);
100
101 m_text->Connect(m_text->GetId(), wxEVT_DESTROY,
102 wxWindowDestroyEventHandler(wxPickerBase::OnTextCtrlDelete),
103 NULL, this);
104
105 // the text control's proportion values defaults to 2
106 m_sizer->Add(m_text, 2, GetDefaultTextCtrlFlag(), 5);
107 }
108
109 return true;
110 }
111
112 void wxPickerBase::PostCreation()
113 {
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);
117
118 // For aesthetic reasons, make sure the picker is at least as high as the
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 }
132
133 SetSizer(m_sizer);
134
135 SetInitialSize( GetMinSize() );
136 }
137
138 #if wxUSE_TOOLTIPS
139
140 void wxPickerBase::DoSetToolTip(wxToolTip *tip)
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
148 if ( m_text )
149 m_text->SetToolTip(tip ? new wxToolTip(tip->GetTip()) : NULL);
150 }
151
152 #endif // wxUSE_TOOLTIPS
153
154 // ----------------------------------------------------------------------------
155 // wxPickerBase - event handlers
156 // ----------------------------------------------------------------------------
157
158 void wxPickerBase::OnTextCtrlKillFocus(wxFocusEvent& event)
159 {
160 event.Skip();
161
162 // don't leave the textctrl empty
163 if (m_text && m_text->GetValue().empty())
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
179 #endif // Any picker in use