Added missing include
[wxWidgets.git] / src / generic / collpaneg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/collpaneg.cpp
3 // Purpose: wxGenericCollapsiblePane
4 // Author: Francesco Montorsi
5 // Modified By:
6 // Created: 8/10/2006
7 // Id: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19 #include "wx/defs.h"
20
21 #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
22
23 #include "wx/collpane.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/toplevel.h"
27 #include "wx/button.h"
28 #include "wx/sizer.h"
29 #include "wx/panel.h"
30 #endif // !WX_PRECOMP
31
32 #include "wx/statline.h"
33
34 // ----------------------------------------------------------------------------
35 // constants
36 // ----------------------------------------------------------------------------
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 const wxChar wxCollapsiblePaneNameStr[] = wxT("collapsiblePane");
43
44 //-----------------------------------------------------------------------------
45 // wxGenericCollapsiblePane
46 //-----------------------------------------------------------------------------
47
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
49 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
50 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
51
52 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
53 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
54 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
55 END_EVENT_TABLE()
56
57
58 bool wxGenericCollapsiblePane::Create(wxWindow *parent,
59 wxWindowID id,
60 const wxString& label,
61 const wxPoint& pos,
62 const wxSize& size,
63 long style,
64 const wxValidator& val,
65 const wxString& name)
66 {
67 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
68 return false;
69
70 m_strLabel = label;
71
72 // create children and lay them out using a wxBoxSizer
73 // (so that we automatically get RTL features)
74 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
75 wxDefaultSize, wxBU_EXACTFIT);
76 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
77 #ifdef __WXMAC__
78 // on Mac we put the static libe above the button
79 m_sz = new wxBoxSizer(wxVERTICAL);
80 m_sz->Add(m_pStaticLine, 0, wxALL|wxGROW, GetBorder());
81 m_sz->Add(m_pButton, 0, wxLEFT|wxRIGHT|wxBOTTOM, GetBorder());
82 #else
83 // on other platforms we put the static line and the button horizontally
84 m_sz = new wxBoxSizer(wxHORIZONTAL);
85 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
86 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
87 #endif
88
89 #ifdef __WXWINCE__
90 SetBackgroundColour(parent->GetBackgroundColour());
91 #endif
92
93 // do not set sz as our sizers since we handle the pane window without using sizers
94 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
95 wxTAB_TRAVERSAL|wxNO_BORDER);
96
97 // start as collapsed:
98 m_pPane->Hide();
99
100 return true;
101 }
102
103 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
104 {
105 if (m_pButton && m_pStaticLine && m_sz)
106 {
107 m_pButton->SetContainingSizer(NULL);
108 m_pStaticLine->SetContainingSizer(NULL);
109
110 // our sizer is not deleted automatically since we didn't use SetSizer()!
111 wxDELETE(m_sz);
112 }
113 }
114
115 wxSize wxGenericCollapsiblePane::DoGetBestSize() const
116 {
117 // NB: do not use GetSize() but rather GetMinSize()
118 wxSize sz = m_sz->GetMinSize();
119
120 // when expanded, we need more vertical space
121 if ( IsExpanded() )
122 {
123 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
124 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
125 }
126
127 return sz;
128 }
129
130 wxString wxGenericCollapsiblePane::GetBtnLabel() const
131 {
132 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
133 }
134
135 void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
136 {
137 // minimal size has priority over the best size so set here our min size
138 SetMinSize(sz);
139 SetSize(sz);
140
141 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
142 {
143 // the user asked to explicitely handle the resizing itself...
144 return;
145 }
146
147
148 //
149 // NB: the following block of code has been accurately designed to
150 // as much flicker-free as possible; be careful when modifying it!
151 //
152
153 wxTopLevelWindow *
154 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
155 if ( top )
156 {
157 // NB: don't Layout() the 'top' window as its size has not been correctly
158 // updated yet and we don't want to do an initial Layout() with the old
159 // size immediately followed by a SetClientSize/Fit call for the new
160 // size; that would provoke flickering!
161
162 if (top->GetSizer())
163 #ifdef __WXGTK__
164 // FIXME: the SetSizeHints() call would be required also for GTK+ for
165 // the expanded->collapsed transition. Unfortunately if we
166 // enable this line, then the GTK+ top window won't always be
167 // resized by the SetClientSize() call below! As a side effect
168 // of this dirty fix, the minimal size for the pane window is
169 // not set in GTK+ and the user can hide it shrinking the "top"
170 // window...
171 if (IsCollapsed())
172 #endif
173 top->GetSizer()->SetSizeHints(top);
174
175
176 // we shouldn't attempt to resize a maximized window, whatever happens
177 if ( !top->IsMaximized() )
178 {
179 if ( IsCollapsed() )
180 {
181 // expanded -> collapsed transition
182 if (top->GetSizer())
183 {
184 // we have just set the size hints...
185 wxSize sz = top->GetSizer()->CalcMin();
186
187 // use SetClientSize() and not SetSize() otherwise the size for
188 // e.g. a wxFrame with a menubar wouldn't be correctly set
189 top->SetClientSize(sz);
190 }
191 else
192 top->Layout();
193 }
194 else
195 {
196 // collapsed -> expanded transition
197
198 // force our parent to "fit", i.e. expand so that it can honour
199 // our minimal size
200 top->Fit();
201 }
202 }
203 }
204 }
205
206 void wxGenericCollapsiblePane::Collapse(bool collapse)
207 {
208 // optimization
209 if ( IsCollapsed() == collapse )
210 return;
211
212 // update our state
213 m_pPane->Show(!collapse);
214
215 // update button label
216 // NB: this must be done after updating our "state"
217 m_pButton->SetLabel(GetBtnLabel());
218
219 OnStateChange(GetBestSize());
220 }
221
222 void wxGenericCollapsiblePane::SetLabel(const wxString &label)
223 {
224 m_strLabel = label;
225 m_pButton->SetLabel(GetBtnLabel());
226 m_pButton->SetInitialSize();
227
228 Layout();
229 }
230
231 bool wxGenericCollapsiblePane::Layout()
232 {
233 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
234 return false; // we need to complete the creation first!
235
236 wxSize oursz(GetSize());
237
238 // move & resize the button and the static line
239 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
240 m_sz->Layout();
241
242 if ( IsExpanded() )
243 {
244 // move & resize the container window
245 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
246 m_pPane->SetSize(0, yoffset,
247 oursz.x, oursz.y - yoffset);
248
249 // this is very important to make the pane window layout show correctly
250 m_pPane->Layout();
251 }
252
253 return true;
254 }
255
256 int wxGenericCollapsiblePane::GetBorder() const
257 {
258 #if defined( __WXMAC__ )
259 return 6;
260 #elif defined(__WXGTK20__)
261 return 3;
262 #elif defined(__WXMSW__)
263 wxASSERT(m_pButton);
264 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
265 #else
266 return 5;
267 #endif
268 }
269
270
271
272 //-----------------------------------------------------------------------------
273 // wxGenericCollapsiblePane - event handlers
274 //-----------------------------------------------------------------------------
275
276 void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
277 {
278 if ( event.GetEventObject() != m_pButton )
279 {
280 event.Skip();
281 return;
282 }
283
284 Collapse(!IsCollapsed());
285
286 // this change was generated by the user - send the event
287 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
288 GetEventHandler()->ProcessEvent(ev);
289 }
290
291 void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
292 {
293 #if 0 // for debug only
294 wxClientDC dc(this);
295 dc.SetPen(*wxBLACK_PEN);
296 dc.SetBrush(*wxTRANSPARENT_BRUSH);
297 dc.DrawRectangle(wxPoint(0,0), GetSize());
298 dc.SetPen(*wxRED_PEN);
299 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
300 #endif
301
302 Layout();
303 }
304
305 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE