]> git.saurik.com Git - wxWidgets.git/blame - src/generic/collpaneg.cpp
avoid infinite recursion by not using GetMaxWidth/Height() from GetMaxSize() implemen...
[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"
2cbf7014 19
360c6a85
PC
20#include "wx/collpane.h"
21
687b91d2
VS
22#if wxUSE_BUTTON && wxUSE_STATLINE
23
2cbf7014 24#ifndef WX_PRECOMP
360c6a85 25 #include "wx/toplevel.h"
2cbf7014 26 #include "wx/button.h"
4ec3100a 27 #include "wx/sizer.h"
2cbf7014
VZ
28#endif // !WX_PRECOMP
29
3c1f8cb1
VZ
30#include "wx/statline.h"
31
2cbf7014
VZ
32// ----------------------------------------------------------------------------
33// constants
34// ----------------------------------------------------------------------------
35
36// the number of pixels to leave between the button and the static line and
37// between the button and the pane
38#define wxCP_MARGIN 10
39
3c1f8cb1
VZ
40// ============================================================================
41// implementation
42// ============================================================================
43
44const wxChar wxGenericCollapsiblePaneNameStr[] = wxT("genericCollapsiblePane");
45
46//-----------------------------------------------------------------------------
47// wxGenericCollapsiblePane
48//-----------------------------------------------------------------------------
49
50DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
51IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
52IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
53
54BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
2cbf7014 55 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
3c1f8cb1
VZ
56 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
57END_EVENT_TABLE()
58
59
2cbf7014
VZ
60bool wxGenericCollapsiblePane::Create(wxWindow *parent,
61 wxWindowID id,
62 const wxString& label,
63 const wxPoint& pos,
64 const wxSize& size,
65 long style,
66 const wxValidator& val,
67 const wxString& name)
3c1f8cb1
VZ
68{
69 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
70 return false;
71
72 m_strLabel = label;
73
74 // create children; their size & position is set in OnSize()
2cbf7014 75 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1
VZ
76 wxDefaultSize, wxBU_EXACTFIT);
77 m_pStatLine = new wxStaticLine(this, wxID_ANY);
550d433e
VZ
78 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
79 wxNO_BORDER);
3c1f8cb1
VZ
80
81 // start as collapsed:
82 m_pPane->Hide();
83
84 //CacheBestSize(GetBestSize());
85 return true;
86}
87
88wxSize wxGenericCollapsiblePane::DoGetBestSize() const
89{
90 wxSize sz = m_pButton->GetBestSize();
91
92 // set width
550d433e
VZ
93 sz.SetWidth(sz.x + wxCP_MARGIN + m_pStatLine->GetBestSize().x);
94 const wxCoord paneWidth = m_pPane->GetBestSize().x;
95 if ( sz.x < paneWidth )
96 sz.x = paneWidth;
3c1f8cb1
VZ
97
98 // when expanded, we need more vertical space
550d433e
VZ
99 if ( IsExpanded() )
100 sz.SetHeight(sz.y + wxCP_MARGIN + m_pPane->GetBestSize().y);
3c1f8cb1
VZ
101
102 return sz;
103}
104
105wxString wxGenericCollapsiblePane::GetBtnLabel() const
106{
550d433e 107 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
3c1f8cb1
VZ
108}
109
4223cec5 110void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 111{
3c1f8cb1 112 // minimal size has priority over the best size so set here our min size
3c1f8cb1
VZ
113 SetMinSize(sz);
114 SetSize(sz);
115
069a9976
VZ
116 wxTopLevelWindow *
117 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
118 if ( top )
3c1f8cb1 119 {
550d433e
VZ
120 // we've changed our size, thus our top level parent needs to relayout
121 // itself
3c1f8cb1
VZ
122 top->Layout();
123
550d433e
VZ
124 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
125 // but maybe there are cases where this is unwanted!
3c1f8cb1
VZ
126 if (top->GetSizer())
127#ifdef __WXGTK__
550d433e
VZ
128 // FIXME: the SetSizeHints() call would be required also for GTK+ for
129 // the expanded->collapsed transition. Unfortunately if we
130 // enable this line, then the GTK+ top window won't always be
131 // resized by the SetClientSize() call below! As a side effect
132 // of this dirty fix, the minimal size for the pane window is
133 // not set in GTK+ and the user can hide it shrinking the "top"
134 // window...
3c1f8cb1
VZ
135 if (IsCollapsed())
136#endif
137 top->GetSizer()->SetSizeHints(top);
138
069a9976
VZ
139
140 // we shouldn't attempt to resize a maximized window, whatever happens
141 if ( !top->IsMaximized() )
3c1f8cb1 142 {
069a9976
VZ
143 if ( IsCollapsed() )
144 {
145 // use SetClientSize() and not SetSize() otherwise the size for
146 // e.g. a wxFrame with a menubar wouldn't be correctly set
147 top->SetClientSize(sz);
148 }
149 else
150 {
151 // force our parent to "fit", i.e. expand so that it can honour
152 // our minimal size
153 top->Fit();
154 }
3c1f8cb1
VZ
155 }
156 }
157}
158
4223cec5
VZ
159void wxGenericCollapsiblePane::Collapse(bool collapse)
160{
161 // optimization
162 if ( IsCollapsed() == collapse )
163 return;
164
165 // update our state
166 m_pPane->Show(!collapse);
167
168 // update button label
169 // NB: this must be done after updating our "state"
170 m_pButton->SetLabel(GetBtnLabel());
171
172 OnStateChange(GetBestSize());
173}
174
3c1f8cb1
VZ
175void wxGenericCollapsiblePane::SetLabel(const wxString &label)
176{
177 m_strLabel = label;
178 m_pButton->SetLabel(GetBtnLabel());
179 m_pButton->SetBestFittingSize();
180
181 LayoutChildren();
182}
183
184void wxGenericCollapsiblePane::LayoutChildren()
185{
186 wxSize btnSz = m_pButton->GetSize();
187
188 // the button position & size are always ok...
189
190 // move & resize the static line
550d433e
VZ
191 m_pStatLine->SetSize(btnSz.x + wxCP_MARGIN, btnSz.y/2,
192 GetSize().x - btnSz.x - wxCP_MARGIN, -1,
3c1f8cb1
VZ
193 wxSIZE_USE_EXISTING);
194
195 // move & resize the container window
550d433e
VZ
196 m_pPane->SetSize(0, btnSz.y + wxCP_MARGIN,
197 GetSize().x, GetSize().y - btnSz.y - wxCP_MARGIN);
3c1f8cb1
VZ
198}
199
200
201
202//-----------------------------------------------------------------------------
203// wxGenericCollapsiblePane - event handlers
204//-----------------------------------------------------------------------------
205
2cbf7014 206void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 207{
2cbf7014
VZ
208 if ( event.GetEventObject() != m_pButton )
209 {
210 event.Skip();
211 return;
212 }
213
3c1f8cb1
VZ
214 Collapse(!IsCollapsed());
215
216 // this change was generated by the user - send the event
217 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
218 GetEventHandler()->ProcessEvent(ev);
219}
220
221void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
222{
223#if 0 // for debug only
224 wxClientDC dc(this);
225 dc.SetPen(*wxBLACK_PEN);
226 dc.SetBrush(*wxTRANSPARENT_BRUSH);
227 dc.DrawRectangle(wxPoint(0,0), GetSize());
228 dc.SetPen(*wxRED_PEN);
229 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
230#endif
231
232
233 if (!m_pButton || !m_pStatLine || !m_pPane)
234 return; // we need to complete the creation first!
235
236 LayoutChildren();
237
238 // this is very important to make the pane window layout show correctly
239 m_pPane->Layout();
240}
687b91d2
VS
241
242#endif // wxUSE_BUTTON && wxUSE_STATLINE