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