1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/collpaneg.cpp
3 // Purpose: wxGenericCollapsiblePane
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19 #include "wx/collpane.h"
20 #include "wx/statline.h"
22 // ============================================================================
24 // ============================================================================
26 const wxChar wxGenericCollapsiblePaneNameStr
[] = wxT("genericCollapsiblePane");
28 //-----------------------------------------------------------------------------
29 // wxGenericCollapsiblePane
30 //-----------------------------------------------------------------------------
32 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
33 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
34 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
36 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
37 EVT_BUTTON(wxCP_BUTTON_ID
, wxGenericCollapsiblePane::OnButton
)
38 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
42 bool wxGenericCollapsiblePane::Create( wxWindow
*parent
, wxWindowID id
,
43 const wxString
& label
,
47 const wxValidator
& val
,
50 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
55 // create children; their size & position is set in OnSize()
56 m_pButton
= new wxButton(this, wxCP_BUTTON_ID
, GetBtnLabel(), wxPoint(0, 0),
57 wxDefaultSize
, wxBU_EXACTFIT
);
58 m_pStatLine
= new wxStaticLine(this, wxID_ANY
);
59 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxNO_BORDER
);
61 // start as collapsed:
64 //CacheBestSize(GetBestSize());
68 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
70 wxSize sz
= m_pButton
->GetBestSize();
73 sz
.SetWidth( sz
.GetWidth() + wxCP_MARGIN
+ m_pStatLine
->GetBestSize().GetWidth() );
74 sz
.SetWidth( wxMax(sz
.GetWidth(), m_pPane
->GetBestSize().GetWidth()) );
76 // when expanded, we need more vertical space
78 sz
.SetHeight( sz
.GetHeight() + wxCP_MARGIN
+ m_pPane
->GetBestSize().GetHeight() );
83 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
86 return m_strLabel
+ wxT(" >>");
87 return m_strLabel
+ wxT(" <<");
90 void wxGenericCollapsiblePane::Collapse(bool collapse
)
93 if (IsCollapsed() == collapse
)
97 m_pPane
->Show(!collapse
);
99 // update button label
100 // NB: this must be done after updating our "state"
101 m_pButton
->SetLabel(GetBtnLabel());
103 // minimal size has priority over the best size so set here our min size
104 wxSize sz
= GetBestSize();
108 wxWindow
*top
= GetTopLevelParent();
111 // we've changed our size, thus our top level parent needs to relayout itself
114 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect but
115 // maybe there are cases where this is unwanted!
118 // FIXME: the SetSizeHints() call would be required also for GTK+ for the
119 // expanded->collapsed transition.
120 // Unfortunately if we enable this line, then the GTK+ top window
121 // won't always be resized by the SetClientSize() call below!
122 // As a side effect of this dirty fix, the minimal size for the
123 // pane window is not set in GTK+ and the user can hide it shrinking
124 // the "top" window...
127 top
->GetSizer()->SetSizeHints(top
);
131 // NB: we need to use SetClientSize() and not SetSize() otherwise the size for
132 // windows like e.g. wxFrames with wxMenubars won't be correctly set
133 top
->SetClientSize(sz
);
137 // force our parent to "fit", i.e. expand so that it can honour
144 wxWindow
*wxGenericCollapsiblePane::GetTopLevelParent()
146 wxWindow
*parent
= GetParent();
147 while (parent
&& !parent
->IsTopLevel())
148 parent
= parent
->GetParent();
153 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
156 m_pButton
->SetLabel(GetBtnLabel());
157 m_pButton
->SetBestFittingSize();
162 void wxGenericCollapsiblePane::LayoutChildren()
164 wxSize btnSz
= m_pButton
->GetSize();
166 // the button position & size are always ok...
168 // move & resize the static line
169 m_pStatLine
->SetSize(btnSz
.GetWidth() + wxCP_MARGIN
, btnSz
.GetHeight()/2,
170 GetSize().GetWidth() - btnSz
.GetWidth() - wxCP_MARGIN
, -1,
171 wxSIZE_USE_EXISTING
);
173 // move & resize the container window
174 m_pPane
->SetSize(0, btnSz
.GetHeight() + wxCP_MARGIN
,
175 GetSize().GetWidth(), GetSize().GetHeight() - btnSz
.GetHeight() - wxCP_MARGIN
);
180 //-----------------------------------------------------------------------------
181 // wxGenericCollapsiblePane - event handlers
182 //-----------------------------------------------------------------------------
184 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
&WXUNUSED(event
))
186 Collapse(!IsCollapsed());
188 // this change was generated by the user - send the event
189 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
190 GetEventHandler()->ProcessEvent(ev
);
193 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
195 #if 0 // for debug only
197 dc
.SetPen(*wxBLACK_PEN
);
198 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
199 dc
.DrawRectangle(wxPoint(0,0), GetSize());
200 dc
.SetPen(*wxRED_PEN
);
201 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
205 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
206 return; // we need to complete the creation first!
210 // this is very important to make the pane window layout show correctly