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"
21 #include "wx/button.h"
25 #include "wx/collpane.h"
26 #include "wx/statline.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 // the number of pixels to leave between the button and the static line and
33 // between the button and the pane
34 #define wxCP_MARGIN 10
36 // ============================================================================
38 // ============================================================================
40 const wxChar wxGenericCollapsiblePaneNameStr
[] = wxT("genericCollapsiblePane");
42 //-----------------------------------------------------------------------------
43 // wxGenericCollapsiblePane
44 //-----------------------------------------------------------------------------
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
47 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
48 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
50 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
51 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane::OnButton
)
52 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
56 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
58 const wxString
& label
,
62 const wxValidator
& val
,
65 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
70 // create children; their size & position is set in OnSize()
71 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
72 wxDefaultSize
, wxBU_EXACTFIT
);
73 m_pStatLine
= new wxStaticLine(this, wxID_ANY
);
74 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
77 // start as collapsed:
80 //CacheBestSize(GetBestSize());
84 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
86 wxSize sz
= m_pButton
->GetBestSize();
89 sz
.SetWidth(sz
.x
+ wxCP_MARGIN
+ m_pStatLine
->GetBestSize().x
);
90 const wxCoord paneWidth
= m_pPane
->GetBestSize().x
;
91 if ( sz
.x
< paneWidth
)
94 // when expanded, we need more vertical space
96 sz
.SetHeight(sz
.y
+ wxCP_MARGIN
+ m_pPane
->GetBestSize().y
);
101 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
103 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
106 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
108 // minimal size has priority over the best size so set here our min size
113 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
116 // we've changed our size, thus our top level parent needs to relayout
120 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
121 // but maybe there are cases where this is unwanted!
124 // FIXME: the SetSizeHints() call would be required also for GTK+ for
125 // the expanded->collapsed transition. Unfortunately if we
126 // enable this line, then the GTK+ top window won't always be
127 // resized by the SetClientSize() call below! As a side effect
128 // of this dirty fix, the minimal size for the pane window is
129 // not set in GTK+ and the user can hide it shrinking the "top"
133 top
->GetSizer()->SetSizeHints(top
);
136 // we shouldn't attempt to resize a maximized window, whatever happens
137 if ( !top
->IsMaximized() )
141 // use SetClientSize() and not SetSize() otherwise the size for
142 // e.g. a wxFrame with a menubar wouldn't be correctly set
143 top
->SetClientSize(sz
);
147 // force our parent to "fit", i.e. expand so that it can honour
155 void wxGenericCollapsiblePane::Collapse(bool collapse
)
158 if ( IsCollapsed() == collapse
)
162 m_pPane
->Show(!collapse
);
164 // update button label
165 // NB: this must be done after updating our "state"
166 m_pButton
->SetLabel(GetBtnLabel());
168 OnStateChange(GetBestSize());
171 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
174 m_pButton
->SetLabel(GetBtnLabel());
175 m_pButton
->SetBestFittingSize();
180 void wxGenericCollapsiblePane::LayoutChildren()
182 wxSize btnSz
= m_pButton
->GetSize();
184 // the button position & size are always ok...
186 // move & resize the static line
187 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
188 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
189 wxSIZE_USE_EXISTING
);
191 // move & resize the container window
192 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
193 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
198 //-----------------------------------------------------------------------------
199 // wxGenericCollapsiblePane - event handlers
200 //-----------------------------------------------------------------------------
202 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
204 if ( event
.GetEventObject() != m_pButton
)
210 Collapse(!IsCollapsed());
212 // this change was generated by the user - send the event
213 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
214 GetEventHandler()->ProcessEvent(ev
);
217 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
219 #if 0 // for debug only
221 dc
.SetPen(*wxBLACK_PEN
);
222 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
223 dc
.DrawRectangle(wxPoint(0,0), GetSize());
224 dc
.SetPen(*wxRED_PEN
);
225 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
229 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
230 return; // we need to complete the creation first!
234 // this is very important to make the pane window layout show correctly