]> git.saurik.com Git - wxWidgets.git/blame - src/generic/collpaneg.cpp
Corrected wxRTTI for wxNotebook so dynamic casting to wxBookCtrlBase works
[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
47922888
RR
72#ifdef __WXMAC__
73 // on Mac we use the disclosure triangle
f2412e6a 74 // we need a light gray line above and below, lets approximate with the frame
47922888 75 m_pStaticLine = NULL;
f2412e6a
SC
76 m_pButton = new wxDisclosureTriangle( this, wxID_ANY, GetBtnLabel(),
77 wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER );
78 m_pButton->SetBackgroundColour( wxColour( 221, 226, 239 ) );
47922888
RR
79 m_sz = new wxBoxSizer(wxHORIZONTAL);
80 // m_sz->Add(4,4); where shall we put it?
f2412e6a 81 m_sz->Add( m_pButton, 1);
47922888 82#else
912c3932
VZ
83 // create children and lay them out using a wxBoxSizer
84 // (so that we automatically get RTL features)
2cbf7014 85 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1 86 wxDefaultSize, wxBU_EXACTFIT);
912c3932 87 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
912c3932
VZ
88 // on other platforms we put the static line and the button horizontally
89 m_sz = new wxBoxSizer(wxHORIZONTAL);
90 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
91 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
92#endif
93
56eebcda
VZ
94 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
95 // this, no idea why...
ff654490 96#if defined(__WXWINCE__) || defined(__WXGTK__)
948f4c37
WS
97 SetBackgroundColour(parent->GetBackgroundColour());
98#endif
99
912c3932 100 // do not set sz as our sizers since we handle the pane window without using sizers
037c7b4c
RD
101 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
102 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1
VZ
103
104 // start as collapsed:
105 m_pPane->Hide();
106
3c1f8cb1
VZ
107 return true;
108}
109
912c3932 110wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
3c1f8cb1 111{
47922888 112 if (m_pButton)
912c3932 113 m_pButton->SetContainingSizer(NULL);
47922888
RR
114
115 if (m_pStaticLine)
912c3932 116 m_pStaticLine->SetContainingSizer(NULL);
47922888
RR
117
118 // our sizer is not deleted automatically since we didn't use SetSizer()!
119 wxDELETE(m_sz);
912c3932
VZ
120}
121
122wxSize wxGenericCollapsiblePane::DoGetBestSize() const
123{
124 // NB: do not use GetSize() but rather GetMinSize()
125 wxSize sz = m_sz->GetMinSize();
3c1f8cb1
VZ
126
127 // when expanded, we need more vertical space
550d433e 128 if ( IsExpanded() )
912c3932
VZ
129 {
130 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
131 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
132 }
3c1f8cb1
VZ
133
134 return sz;
135}
136
137wxString wxGenericCollapsiblePane::GetBtnLabel() const
138{
f2412e6a
SC
139 // on mac the triangle indicates the state, no string change
140#ifdef __WXMAC__
141 return m_strLabel;
142#else
550d433e 143 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
f2412e6a 144#endif
3c1f8cb1
VZ
145}
146
4223cec5 147void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 148{
3c1f8cb1 149 // minimal size has priority over the best size so set here our min size
3c1f8cb1
VZ
150 SetMinSize(sz);
151 SetSize(sz);
152
912c3932
VZ
153 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
154 {
155 // the user asked to explicitely handle the resizing itself...
156 return;
157 }
158
159
160 //
161 // NB: the following block of code has been accurately designed to
162 // as much flicker-free as possible; be careful when modifying it!
163 //
164
069a9976
VZ
165 wxTopLevelWindow *
166 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
167 if ( top )
3c1f8cb1 168 {
912c3932
VZ
169 // NB: don't Layout() the 'top' window as its size has not been correctly
170 // updated yet and we don't want to do an initial Layout() with the old
171 // size immediately followed by a SetClientSize/Fit call for the new
172 // size; that would provoke flickering!
3c1f8cb1 173
3c1f8cb1
VZ
174 if (top->GetSizer())
175#ifdef __WXGTK__
550d433e
VZ
176 // FIXME: the SetSizeHints() call would be required also for GTK+ for
177 // the expanded->collapsed transition. Unfortunately if we
178 // enable this line, then the GTK+ top window won't always be
179 // resized by the SetClientSize() call below! As a side effect
180 // of this dirty fix, the minimal size for the pane window is
181 // not set in GTK+ and the user can hide it shrinking the "top"
182 // window...
3c1f8cb1
VZ
183 if (IsCollapsed())
184#endif
185 top->GetSizer()->SetSizeHints(top);
186
069a9976
VZ
187
188 // we shouldn't attempt to resize a maximized window, whatever happens
189 if ( !top->IsMaximized() )
3c1f8cb1 190 {
069a9976
VZ
191 if ( IsCollapsed() )
192 {
912c3932
VZ
193 // expanded -> collapsed transition
194 if (top->GetSizer())
195 {
196 // we have just set the size hints...
74ab5f5b 197 wxSize szClient = top->GetSizer()->CalcMin();
912c3932
VZ
198
199 // use SetClientSize() and not SetSize() otherwise the size for
200 // e.g. a wxFrame with a menubar wouldn't be correctly set
74ab5f5b 201 top->SetClientSize(szClient);
912c3932
VZ
202 }
203 else
204 top->Layout();
069a9976
VZ
205 }
206 else
207 {
912c3932
VZ
208 // collapsed -> expanded transition
209
069a9976
VZ
210 // force our parent to "fit", i.e. expand so that it can honour
211 // our minimal size
212 top->Fit();
213 }
3c1f8cb1
VZ
214 }
215 }
216}
217
4223cec5
VZ
218void wxGenericCollapsiblePane::Collapse(bool collapse)
219{
220 // optimization
221 if ( IsCollapsed() == collapse )
222 return;
223
224 // update our state
225 m_pPane->Show(!collapse);
226
227 // update button label
47922888 228#ifdef __WXMAC__
f2412e6a 229 m_pButton->SetOpen( !collapse );
47922888 230#else
4223cec5
VZ
231 // NB: this must be done after updating our "state"
232 m_pButton->SetLabel(GetBtnLabel());
47922888
RR
233#endif
234
4223cec5
VZ
235
236 OnStateChange(GetBestSize());
237}
238
3c1f8cb1
VZ
239void wxGenericCollapsiblePane::SetLabel(const wxString &label)
240{
241 m_strLabel = label;
47922888
RR
242#ifdef __WXMAC__
243 m_pButton->SetLabel(GetBtnLabel());
244#else
3c1f8cb1 245 m_pButton->SetLabel(GetBtnLabel());
170acdc9 246 m_pButton->SetInitialSize();
47922888 247#endif
3c1f8cb1 248
912c3932 249 Layout();
3c1f8cb1
VZ
250}
251
912c3932 252bool wxGenericCollapsiblePane::Layout()
3c1f8cb1 253{
47922888
RR
254#ifdef __WXMAC__
255 if (!m_pButton || !m_pPane || !m_sz)
256 return false; // we need to complete the creation first!
257#else
912c3932
VZ
258 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
259 return false; // we need to complete the creation first!
47922888 260#endif
912c3932
VZ
261
262 wxSize oursz(GetSize());
3c1f8cb1 263
912c3932
VZ
264 // move & resize the button and the static line
265 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
266 m_sz->Layout();
3c1f8cb1 267
912c3932
VZ
268 if ( IsExpanded() )
269 {
270 // move & resize the container window
271 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
272 m_pPane->SetSize(0, yoffset,
273 oursz.x, oursz.y - yoffset);
274
275 // this is very important to make the pane window layout show correctly
276 m_pPane->Layout();
277 }
3c1f8cb1 278
912c3932
VZ
279 return true;
280}
281
282int wxGenericCollapsiblePane::GetBorder() const
283{
284#if defined( __WXMAC__ )
285 return 6;
912c3932
VZ
286#elif defined(__WXMSW__)
287 wxASSERT(m_pButton);
288 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
289#else
290 return 5;
291#endif
3c1f8cb1
VZ
292}
293
294
295
296//-----------------------------------------------------------------------------
297// wxGenericCollapsiblePane - event handlers
298//-----------------------------------------------------------------------------
299
2cbf7014 300void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 301{
2cbf7014
VZ
302 if ( event.GetEventObject() != m_pButton )
303 {
304 event.Skip();
305 return;
306 }
307
3c1f8cb1
VZ
308 Collapse(!IsCollapsed());
309
310 // this change was generated by the user - send the event
311 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
312 GetEventHandler()->ProcessEvent(ev);
313}
314
315void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
316{
317#if 0 // for debug only
318 wxClientDC dc(this);
319 dc.SetPen(*wxBLACK_PEN);
320 dc.SetBrush(*wxTRANSPARENT_BRUSH);
321 dc.DrawRectangle(wxPoint(0,0), GetSize());
322 dc.SetPen(*wxRED_PEN);
323 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
324#endif
325
912c3932 326 Layout();
3c1f8cb1 327}
687b91d2 328
912c3932 329#endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE