1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/collpaneg.cpp
3 // Purpose: wxGenericCollapsiblePane
4 // Author: Francesco Montorsi
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
27 #include "wx/collpane.h"
30 #include "wx/toplevel.h"
31 #include "wx/button.h"
36 #include "wx/statline.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // ============================================================================
44 // ============================================================================
46 const char wxCollapsiblePaneNameStr
[] = "collapsiblePane";
48 //-----------------------------------------------------------------------------
49 // wxGenericCollapsiblePane
50 //-----------------------------------------------------------------------------
52 wxDEFINE_EVENT( wxEVT_COLLAPSIBLEPANE_CHANGED
, wxCollapsiblePaneEvent
);
53 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
56 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
57 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane::OnButton
)
58 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
61 void wxGenericCollapsiblePane::Init()
69 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
71 const wxString
& label
,
75 const wxValidator
& val
,
78 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
83 // sizer containing the expand button and possibly a static line
84 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
86 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
87 // on Mac we use the special disclosure triangle button
89 m_pButton
= new wxDisclosureTriangle(this, wxID_ANY
, GetBtnLabel());
92 // create children and lay them out using a wxBoxSizer
93 // (so that we automatically get RTL features)
94 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
95 wxDefaultSize
, wxBU_EXACTFIT
);
96 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
98 // on other platforms we put the static line and the button horizontally
99 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
100 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
103 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
104 // this, no idea why...
105 #if defined(__WXWINCE__) || defined(__WXGTK__)
106 SetBackgroundColour(parent
->GetBackgroundColour());
109 // do not set sz as our sizers since we handle the pane window without using sizers
110 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
111 wxTAB_TRAVERSAL
|wxNO_BORDER
, wxT("wxCollapsiblePanePane") );
113 // start as collapsed:
119 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
122 m_pButton
->SetContainingSizer(NULL
);
125 m_pStaticLine
->SetContainingSizer(NULL
);
127 // our sizer is not deleted automatically since we didn't use SetSizer()!
131 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
133 // NB: do not use GetSize() but rather GetMinSize()
134 wxSize sz
= m_sz
->GetMinSize();
136 // when expanded, we need more vertical space
139 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
140 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
146 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
148 // on mac the triangle indicates the state, no string change
152 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
156 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
158 // minimal size has priority over the best size so set here our min size
162 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
164 // the user asked to explicitly handle the resizing itself...
169 wxTopLevelWindow
*top
=
170 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
174 wxSizer
*sizer
= top
->GetSizer();
178 const wxSize newBestSize
= sizer
->ComputeFittingClientSize(top
);
179 top
->SetMinClientSize(newBestSize
);
181 // we shouldn't attempt to resize a maximized window, whatever happens
182 if ( !top
->IsMaximized() )
183 top
->SetClientSize(newBestSize
);
186 void wxGenericCollapsiblePane::Collapse(bool collapse
)
189 if ( IsCollapsed() == collapse
)
192 InvalidateBestSize();
195 m_pPane
->Show(!collapse
);
197 // update button label
198 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
199 m_pButton
->SetOpen( !collapse
);
201 // NB: this must be done after updating our "state"
202 m_pButton
->SetLabel(GetBtnLabel());
206 OnStateChange(GetBestSize());
209 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
213 m_pButton
->SetLabel(GetBtnLabel());
215 m_pButton
->SetLabel(GetBtnLabel());
216 m_pButton
->SetInitialSize();
222 bool wxGenericCollapsiblePane::Layout()
225 if (!m_pButton
|| !m_pPane
|| !m_sz
)
226 return false; // we need to complete the creation first!
228 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
229 return false; // we need to complete the creation first!
232 wxSize
oursz(GetSize());
234 // move & resize the button and the static line
235 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
240 // move & resize the container window
241 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
242 m_pPane
->SetSize(0, yoffset
,
243 oursz
.x
, oursz
.y
- yoffset
);
245 // this is very important to make the pane window layout show correctly
252 int wxGenericCollapsiblePane::GetBorder() const
254 #if defined( __WXMAC__ )
256 #elif defined(__WXMSW__)
258 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
266 //-----------------------------------------------------------------------------
267 // wxGenericCollapsiblePane - event handlers
268 //-----------------------------------------------------------------------------
270 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
272 if ( event
.GetEventObject() != m_pButton
)
278 Collapse(!IsCollapsed());
280 // this change was generated by the user - send the event
281 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
282 GetEventHandler()->ProcessEvent(ev
);
285 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
287 #if 0 // for debug only
289 dc
.SetPen(*wxBLACK_PEN
);
290 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
291 dc
.DrawRectangle(wxPoint(0,0), GetSize());
292 dc
.SetPen(*wxRED_PEN
);
293 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
299 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE