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
112 wxWindow
*top
= wxGetTopLevelParent(this);
115 // we've changed our size, thus our top level parent needs to relayout
119 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
120 // but maybe there are cases where this is unwanted!
123 // FIXME: the SetSizeHints() call would be required also for GTK+ for
124 // the expanded->collapsed transition. Unfortunately if we
125 // enable this line, then the GTK+ top window won't always be
126 // resized by the SetClientSize() call below! As a side effect
127 // of this dirty fix, the minimal size for the pane window is
128 // not set in GTK+ and the user can hide it shrinking the "top"
132 top
->GetSizer()->SetSizeHints(top
);
136 // use SetClientSize() and not SetSize() otherwise the size for
137 // e.g. a wxFrame with a menubar wouldn't be correctly set
138 top
->SetClientSize(sz
);
142 // force our parent to "fit", i.e. expand so that it can honour
149 void wxGenericCollapsiblePane::Collapse(bool collapse
)
152 if ( IsCollapsed() == collapse
)
156 m_pPane
->Show(!collapse
);
158 // update button label
159 // NB: this must be done after updating our "state"
160 m_pButton
->SetLabel(GetBtnLabel());
162 OnStateChange(GetBestSize());
165 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
168 m_pButton
->SetLabel(GetBtnLabel());
169 m_pButton
->SetBestFittingSize();
174 void wxGenericCollapsiblePane::LayoutChildren()
176 wxSize btnSz
= m_pButton
->GetSize();
178 // the button position & size are always ok...
180 // move & resize the static line
181 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
182 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
183 wxSIZE_USE_EXISTING
);
185 // move & resize the container window
186 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
187 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
192 //-----------------------------------------------------------------------------
193 // wxGenericCollapsiblePane - event handlers
194 //-----------------------------------------------------------------------------
196 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
198 if ( event
.GetEventObject() != m_pButton
)
204 Collapse(!IsCollapsed());
206 // this change was generated by the user - send the event
207 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
208 GetEventHandler()->ProcessEvent(ev
);
211 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
213 #if 0 // for debug only
215 dc
.SetPen(*wxBLACK_PEN
);
216 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
217 dc
.DrawRectangle(wxPoint(0,0), GetSize());
218 dc
.SetPen(*wxRED_PEN
);
219 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
223 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
224 return; // we need to complete the creation first!
228 // this is very important to make the pane window layout show correctly