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
,
62 // start as collapsed:
65 //CacheBestSize(GetBestSize());
69 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
71 wxSize sz
= m_pButton
->GetBestSize();
74 sz
.SetWidth(sz
.x
+ wxCP_MARGIN
+ m_pStatLine
->GetBestSize().x
);
75 const wxCoord paneWidth
= m_pPane
->GetBestSize().x
;
76 if ( sz
.x
< paneWidth
)
79 // when expanded, we need more vertical space
81 sz
.SetHeight(sz
.y
+ wxCP_MARGIN
+ m_pPane
->GetBestSize().y
);
86 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
88 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
91 void wxGenericCollapsiblePane::Collapse(bool collapse
)
94 if ( IsCollapsed() == collapse
)
98 m_pPane
->Show(!collapse
);
100 // update button label
101 // NB: this must be done after updating our "state"
102 m_pButton
->SetLabel(GetBtnLabel());
104 // minimal size has priority over the best size so set here our min size
105 wxSize sz
= GetBestSize();
109 wxWindow
*top
= GetTopLevelParent();
112 // we've changed our size, thus our top level parent needs to relayout
116 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
117 // but maybe there are cases where this is unwanted!
120 // FIXME: the SetSizeHints() call would be required also for GTK+ for
121 // the expanded->collapsed transition. Unfortunately if we
122 // enable this line, then the GTK+ top window won't always be
123 // resized by the SetClientSize() call below! As a side effect
124 // of this dirty fix, the minimal size for the pane window is
125 // not set in GTK+ and the user can hide it shrinking the "top"
129 top
->GetSizer()->SetSizeHints(top
);
133 // use SetClientSize() and not SetSize() otherwise the size for
134 // e.g. a wxFrame with a menubar wouldn't be correctly set
135 top
->SetClientSize(sz
);
139 // force our parent to "fit", i.e. expand so that it can honour
146 wxWindow
*wxGenericCollapsiblePane::GetTopLevelParent()
148 wxWindow
*parent
= GetParent();
149 while (parent
&& !parent
->IsTopLevel())
150 parent
= parent
->GetParent();
155 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
158 m_pButton
->SetLabel(GetBtnLabel());
159 m_pButton
->SetBestFittingSize();
164 void wxGenericCollapsiblePane::LayoutChildren()
166 wxSize btnSz
= m_pButton
->GetSize();
168 // the button position & size are always ok...
170 // move & resize the static line
171 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
172 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
173 wxSIZE_USE_EXISTING
);
175 // move & resize the container window
176 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
177 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
182 //-----------------------------------------------------------------------------
183 // wxGenericCollapsiblePane - event handlers
184 //-----------------------------------------------------------------------------
186 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
&WXUNUSED(event
))
188 Collapse(!IsCollapsed());
190 // this change was generated by the user - send the event
191 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
192 GetEventHandler()->ProcessEvent(ev
);
195 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
197 #if 0 // for debug only
199 dc
.SetPen(*wxBLACK_PEN
);
200 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
201 dc
.DrawRectangle(wxPoint(0,0), GetSize());
202 dc
.SetPen(*wxRED_PEN
);
203 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
207 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
208 return; // we need to complete the creation first!
212 // this is very important to make the pane window layout show correctly