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"
26 #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
28 #include "wx/collpane.h"
31 #include "wx/toplevel.h"
32 #include "wx/button.h"
37 #include "wx/statline.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // ============================================================================
45 // ============================================================================
47 const char wxCollapsiblePaneNameStr
[] = "collapsiblePane";
49 //-----------------------------------------------------------------------------
50 // wxGenericCollapsiblePane
51 //-----------------------------------------------------------------------------
53 wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED
, wxCollapsiblePaneEvent
);
54 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
55 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
57 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
58 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane::OnButton
)
59 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
62 void wxGenericCollapsiblePane::Init()
70 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
72 const wxString
& label
,
76 const wxValidator
& val
,
79 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
84 // sizer containing the expand button and possibly a static line
85 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
87 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
88 // on Mac we use the special disclosure triangle button
90 m_pButton
= new wxDisclosureTriangle(this, wxID_ANY
, GetBtnLabel());
93 // create children and lay them out using a wxBoxSizer
94 // (so that we automatically get RTL features)
95 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
96 wxDefaultSize
, wxBU_EXACTFIT
);
97 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
99 // on other platforms we put the static line and the button horizontally
100 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
101 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
104 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
105 // this, no idea why...
106 #if defined(__WXWINCE__) || defined(__WXGTK__)
107 SetBackgroundColour(parent
->GetBackgroundColour());
110 // do not set sz as our sizers since we handle the pane window without using sizers
111 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
112 wxTAB_TRAVERSAL
|wxNO_BORDER
, wxT("wxCollapsiblePanePane") );
114 // start as collapsed:
120 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
123 m_pButton
->SetContainingSizer(NULL
);
126 m_pStaticLine
->SetContainingSizer(NULL
);
128 // our sizer is not deleted automatically since we didn't use SetSizer()!
132 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
134 // NB: do not use GetSize() but rather GetMinSize()
135 wxSize sz
= m_sz
->GetMinSize();
137 // when expanded, we need more vertical space
140 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
141 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
147 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
149 // on mac the triangle indicates the state, no string change
153 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
157 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
159 // minimal size has priority over the best size so set here our min size
163 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
165 // the user asked to explicitly handle the resizing itself...
170 wxTopLevelWindow
*top
=
171 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
175 wxSizer
*sizer
= top
->GetSizer();
179 const wxSize newBestSize
= sizer
->ComputeFittingClientSize(top
);
180 top
->SetMinClientSize(newBestSize
);
182 // we shouldn't attempt to resize a maximized window, whatever happens
183 if ( !top
->IsMaximized() )
184 top
->SetClientSize(newBestSize
);
187 void wxGenericCollapsiblePane::Collapse(bool collapse
)
190 if ( IsCollapsed() == collapse
)
193 InvalidateBestSize();
196 m_pPane
->Show(!collapse
);
198 // update button label
199 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
200 m_pButton
->SetOpen( !collapse
);
202 // NB: this must be done after updating our "state"
203 m_pButton
->SetLabel(GetBtnLabel());
207 OnStateChange(GetBestSize());
210 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
214 m_pButton
->SetLabel(GetBtnLabel());
216 m_pButton
->SetLabel(GetBtnLabel());
217 m_pButton
->SetInitialSize();
223 bool wxGenericCollapsiblePane::Layout()
226 if (!m_pButton
|| !m_pPane
|| !m_sz
)
227 return false; // we need to complete the creation first!
229 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
230 return false; // we need to complete the creation first!
233 wxSize
oursz(GetSize());
235 // move & resize the button and the static line
236 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
241 // move & resize the container window
242 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
243 m_pPane
->SetSize(0, yoffset
,
244 oursz
.x
, oursz
.y
- yoffset
);
246 // this is very important to make the pane window layout show correctly
253 int wxGenericCollapsiblePane::GetBorder() const
255 #if defined( __WXMAC__ )
257 #elif defined(__WXMSW__)
259 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
267 //-----------------------------------------------------------------------------
268 // wxGenericCollapsiblePane - event handlers
269 //-----------------------------------------------------------------------------
271 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
273 if ( event
.GetEventObject() != m_pButton
)
279 Collapse(!IsCollapsed());
281 // this change was generated by the user - send the event
282 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
283 GetEventHandler()->ProcessEvent(ev
);
286 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
288 #if 0 // for debug only
290 dc
.SetPen(*wxBLACK_PEN
);
291 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
292 dc
.DrawRectangle(wxPoint(0,0), GetSize());
293 dc
.SetPen(*wxRED_PEN
);
294 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
300 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE