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