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::OnStateChange(const wxSize
& sz
)
107 // minimal size has priority over the best size so set here our min size
111 wxWindow
*top
= wxGetTopLevelParent(this);
114 // we've changed our size, thus our top level parent needs to relayout
118 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
119 // but maybe there are cases where this is unwanted!
122 // FIXME: the SetSizeHints() call would be required also for GTK+ for
123 // the expanded->collapsed transition. Unfortunately if we
124 // enable this line, then the GTK+ top window won't always be
125 // resized by the SetClientSize() call below! As a side effect
126 // of this dirty fix, the minimal size for the pane window is
127 // not set in GTK+ and the user can hide it shrinking the "top"
131 top
->GetSizer()->SetSizeHints(top
);
135 // use SetClientSize() and not SetSize() otherwise the size for
136 // e.g. a wxFrame with a menubar wouldn't be correctly set
137 top
->SetClientSize(sz
);
141 // force our parent to "fit", i.e. expand so that it can honour
148 void wxGenericCollapsiblePane::Collapse(bool collapse
)
151 if ( IsCollapsed() == collapse
)
155 m_pPane
->Show(!collapse
);
157 // update button label
158 // NB: this must be done after updating our "state"
159 m_pButton
->SetLabel(GetBtnLabel());
161 OnStateChange(GetBestSize());
164 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
167 m_pButton
->SetLabel(GetBtnLabel());
168 m_pButton
->SetBestFittingSize();
173 void wxGenericCollapsiblePane::LayoutChildren()
175 wxSize btnSz
= m_pButton
->GetSize();
177 // the button position & size are always ok...
179 // move & resize the static line
180 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
181 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
182 wxSIZE_USE_EXISTING
);
184 // move & resize the container window
185 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
186 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
191 //-----------------------------------------------------------------------------
192 // wxGenericCollapsiblePane - event handlers
193 //-----------------------------------------------------------------------------
195 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
197 if ( event
.GetEventObject() != m_pButton
)
203 Collapse(!IsCollapsed());
205 // this change was generated by the user - send the event
206 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
207 GetEventHandler()->ProcessEvent(ev
);
210 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
212 #if 0 // for debug only
214 dc
.SetPen(*wxBLACK_PEN
);
215 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
216 dc
.DrawRectangle(wxPoint(0,0), GetSize());
217 dc
.SetPen(*wxRED_PEN
);
218 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
222 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
223 return; // we need to complete the creation first!
227 // this is very important to make the pane window layout show correctly