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"
20 #include "wx/collpane.h"
23 #include "wx/toplevel.h"
24 #include "wx/button.h"
28 #include "wx/statline.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // the number of pixels to leave between the button and the static line and
35 // between the button and the pane
36 #define wxCP_MARGIN 10
38 // ============================================================================
40 // ============================================================================
42 const wxChar wxGenericCollapsiblePaneNameStr
[] = wxT("genericCollapsiblePane");
44 //-----------------------------------------------------------------------------
45 // wxGenericCollapsiblePane
46 //-----------------------------------------------------------------------------
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
49 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
50 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
52 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
53 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane::OnButton
)
54 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
58 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
60 const wxString
& label
,
64 const wxValidator
& val
,
67 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
72 // create children; their size & position is set in OnSize()
73 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
74 wxDefaultSize
, wxBU_EXACTFIT
);
75 m_pStatLine
= new wxStaticLine(this, wxID_ANY
);
76 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
79 // start as collapsed:
82 //CacheBestSize(GetBestSize());
86 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
88 wxSize sz
= m_pButton
->GetBestSize();
91 sz
.SetWidth(sz
.x
+ wxCP_MARGIN
+ m_pStatLine
->GetBestSize().x
);
92 const wxCoord paneWidth
= m_pPane
->GetBestSize().x
;
93 if ( sz
.x
< paneWidth
)
96 // when expanded, we need more vertical space
98 sz
.SetHeight(sz
.y
+ wxCP_MARGIN
+ m_pPane
->GetBestSize().y
);
103 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
105 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
108 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
110 // minimal size has priority over the best size so set here our min size
115 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
118 // we've changed our size, thus our top level parent needs to relayout
122 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
123 // but maybe there are cases where this is unwanted!
126 // FIXME: the SetSizeHints() call would be required also for GTK+ for
127 // the expanded->collapsed transition. Unfortunately if we
128 // enable this line, then the GTK+ top window won't always be
129 // resized by the SetClientSize() call below! As a side effect
130 // of this dirty fix, the minimal size for the pane window is
131 // not set in GTK+ and the user can hide it shrinking the "top"
135 top
->GetSizer()->SetSizeHints(top
);
138 // we shouldn't attempt to resize a maximized window, whatever happens
139 if ( !top
->IsMaximized() )
143 // use SetClientSize() and not SetSize() otherwise the size for
144 // e.g. a wxFrame with a menubar wouldn't be correctly set
145 top
->SetClientSize(sz
);
149 // force our parent to "fit", i.e. expand so that it can honour
157 void wxGenericCollapsiblePane::Collapse(bool collapse
)
160 if ( IsCollapsed() == collapse
)
164 m_pPane
->Show(!collapse
);
166 // update button label
167 // NB: this must be done after updating our "state"
168 m_pButton
->SetLabel(GetBtnLabel());
170 OnStateChange(GetBestSize());
173 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
176 m_pButton
->SetLabel(GetBtnLabel());
177 m_pButton
->SetBestFittingSize();
182 void wxGenericCollapsiblePane::LayoutChildren()
184 wxSize btnSz
= m_pButton
->GetSize();
186 // the button position & size are always ok...
188 // move & resize the static line
189 m_pStatLine
->SetSize(btnSz
.x
+ wxCP_MARGIN
, btnSz
.y
/2,
190 GetSize().x
- btnSz
.x
- wxCP_MARGIN
, -1,
191 wxSIZE_USE_EXISTING
);
193 // move & resize the container window
194 m_pPane
->SetSize(0, btnSz
.y
+ wxCP_MARGIN
,
195 GetSize().x
, GetSize().y
- btnSz
.y
- wxCP_MARGIN
);
200 //-----------------------------------------------------------------------------
201 // wxGenericCollapsiblePane - event handlers
202 //-----------------------------------------------------------------------------
204 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
206 if ( event
.GetEventObject() != m_pButton
)
212 Collapse(!IsCollapsed());
214 // this change was generated by the user - send the event
215 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
216 GetEventHandler()->ProcessEvent(ev
);
219 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
221 #if 0 // for debug only
223 dc
.SetPen(*wxBLACK_PEN
);
224 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
225 dc
.DrawRectangle(wxPoint(0,0), GetSize());
226 dc
.SetPen(*wxRED_PEN
);
227 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
231 if (!m_pButton
|| !m_pStatLine
|| !m_pPane
)
232 return; // we need to complete the creation first!
236 // this is very important to make the pane window layout show correctly