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"
24 #include "wx/collpane.h"
25 #include "wx/statline.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // the number of pixels to leave between the button and the static line and
32 // between the button and the pane
33 #define wxCP_MARGIN 10
35 // ============================================================================
37 // ============================================================================
39 const wxChar wxGenericCollapsiblePaneNameStr
[] = wxT("genericCollapsiblePane");
41 //-----------------------------------------------------------------------------
42 // wxGenericCollapsiblePane
43 //-----------------------------------------------------------------------------
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
46 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
47 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
49 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
50 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane
::OnButton
)
51 EVT_SIZE(wxGenericCollapsiblePane
::OnSize
)
55 bool wxGenericCollapsiblePane
::Create(wxWindow
*parent
,
57 const wxString
& label
,
61 const wxValidator
& val
,
64 if ( !wxControl
::Create(parent
, id
, pos
, size
, style
, val
, name
) )
69 // create children; their size & position is set in OnSize()
70 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
71 wxDefaultSize
, wxBU_EXACTFIT
);
72 m_pStatLine
= new wxStaticLine(this, wxID_ANY
);
73 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
76 // start as collapsed:
79 //CacheBestSize(GetBestSize());
83 wxSize wxGenericCollapsiblePane
::DoGetBestSize() const
85 wxSize sz
= m_pButton
->GetBestSize();
88 sz
.SetWidth(sz
.x
+ wxCP_MARGIN
+ m_pStatLine
->GetBestSize().x
);
89 const wxCoord paneWidth
= m_pPane
->GetBestSize().x
;
90 if ( sz
.x
< paneWidth
)
93 // when expanded, we need more vertical space
95 sz
.SetHeight(sz
.y
+ wxCP_MARGIN
+ m_pPane
->GetBestSize().y
);
100 wxString wxGenericCollapsiblePane
::GetBtnLabel() const
102 return m_strLabel
+ (IsCollapsed() ?
wxT(" >>") : wxT(" <<"));
105 void wxGenericCollapsiblePane
::Collapse(bool collapse
)
108 if ( IsCollapsed() == collapse
)
112 m_pPane
->Show(!collapse
);
114 // update button label
115 // NB: this must be done after updating our "state"
116 m_pButton
->SetLabel(GetBtnLabel());
118 // minimal size has priority over the best size so set here our min size
119 wxSize sz
= GetBestSize();
123 wxWindow
*top
= wxGetTopLevelParent(this);
126 // we've changed our size, thus our top level parent needs to relayout
130 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
131 // but maybe there are cases where this is unwanted!
134 // FIXME: the SetSizeHints() call would be required also for GTK+ for
135 // the expanded->collapsed transition. Unfortunately if we
136 // enable this line, then the GTK+ top window won't always be
137 // resized by the SetClientSize() call below! As a side effect
138 // of this dirty fix, the minimal size for the pane window is
139 // not set in GTK+ and the user can hide it shrinking the "top"
143 top
->GetSizer()->SetSizeHints(top
);
147 // use SetClientSize() and not SetSize() otherwise the size for
148 // e.g. a wxFrame with a menubar wouldn't be correctly set
149 top
->SetClientSize(sz
);
153 // force our parent to "fit", i.e. expand so that it can honour
160 void wxGenericCollapsiblePane
::SetLabel(const wxString
&label
)
163 m_pButton
->SetLabel(GetBtnLabel());
164 m_pButton
->SetBestFittingSize();
169 void wxGenericCollapsiblePane
::LayoutChildren()
171 wxSize btnSz
= m_pButton
->GetSize();
173 // the button position & size are always ok...
175 // move & resize the static line
176 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
177 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
178 wxSIZE_USE_EXISTING
);
180 // move & resize the container window
181 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
182 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
187 //-----------------------------------------------------------------------------
188 // wxGenericCollapsiblePane - event handlers
189 //-----------------------------------------------------------------------------
191 void wxGenericCollapsiblePane
::OnButton(wxCommandEvent
& event
)
193 if ( event
.GetEventObject() != m_pButton
)
199 Collapse(!IsCollapsed());
201 // this change was generated by the user - send the event
202 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
203 GetEventHandler()->ProcessEvent(ev
);
206 void wxGenericCollapsiblePane
::OnSize(wxSizeEvent
& WXUNUSED(event
))
208 #if 0 // for debug only
210 dc
.SetPen(*wxBLACK_PEN
);
211 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
212 dc
.DrawRectangle(wxPoint(0,0), GetSize());
213 dc
.SetPen(*wxRED_PEN
);
214 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
218 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
219 return; // we need to complete the creation first!
223 // this is very important to make the pane window layout show correctly