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
)
61 WX_EVENT_TABLE_CONTROL_CONTAINER(wxGenericCollapsiblePane
)
64 WX_DELEGATE_TO_CONTROL_CONTAINER(wxGenericCollapsiblePane
, wxControl
)
66 void wxGenericCollapsiblePane::Init()
68 WX_INIT_CONTROL_CONTAINER();
76 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
78 const wxString
& label
,
82 const wxValidator
& val
,
85 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
90 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
91 // on Mac we use the disclosure triangle
92 // we need a light gray line above and below, lets approximate with the frame
94 m_pButton
= new wxDisclosureTriangle( this, wxID_ANY
, GetBtnLabel(),
95 wxDefaultPosition
, wxDefaultSize
, wxSIMPLE_BORDER
);
96 m_pButton
->SetBackgroundColour( wxColour( 221, 226, 239 ) );
97 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
98 // m_sz->Add(4,4); where shall we put it?
99 m_sz
->Add( m_pButton
, 1);
101 // create children and lay them out using a wxBoxSizer
102 // (so that we automatically get RTL features)
103 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
104 wxDefaultSize
, wxBU_EXACTFIT
);
105 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
106 // on other platforms we put the static line and the button horizontally
107 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
108 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
109 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
112 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
113 // this, no idea why...
114 #if defined(__WXWINCE__) || defined(__WXGTK__)
115 SetBackgroundColour(parent
->GetBackgroundColour());
118 // do not set sz as our sizers since we handle the pane window without using sizers
119 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
120 wxTAB_TRAVERSAL
|wxNO_BORDER
, wxT("wxCollapsiblePanePane") );
122 // start as collapsed:
128 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
131 m_pButton
->SetContainingSizer(NULL
);
134 m_pStaticLine
->SetContainingSizer(NULL
);
136 // our sizer is not deleted automatically since we didn't use SetSizer()!
140 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
142 // NB: do not use GetSize() but rather GetMinSize()
143 wxSize sz
= m_sz
->GetMinSize();
145 // when expanded, we need more vertical space
148 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
149 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
155 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
157 // on mac the triangle indicates the state, no string change
161 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
165 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
167 // minimal size has priority over the best size so set here our min size
171 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
173 // the user asked to explicitely handle the resizing itself...
178 wxTopLevelWindow
*top
=
179 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
183 wxSizer
*sizer
= top
->GetSizer();
187 const wxSize newBestSize
= sizer
->ComputeFittingClientSize(top
);
188 top
->SetMinClientSize(newBestSize
);
190 // we shouldn't attempt to resize a maximized window, whatever happens
191 if ( !top
->IsMaximized() )
192 top
->SetClientSize(newBestSize
);
195 void wxGenericCollapsiblePane::Collapse(bool collapse
)
198 if ( IsCollapsed() == collapse
)
202 m_pPane
->Show(!collapse
);
204 // update button label
205 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
206 m_pButton
->SetOpen( !collapse
);
208 // NB: this must be done after updating our "state"
209 m_pButton
->SetLabel(GetBtnLabel());
213 OnStateChange(GetBestSize());
216 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
220 m_pButton
->SetLabel(GetBtnLabel());
222 m_pButton
->SetLabel(GetBtnLabel());
223 m_pButton
->SetInitialSize();
229 bool wxGenericCollapsiblePane::Layout()
232 if (!m_pButton
|| !m_pPane
|| !m_sz
)
233 return false; // we need to complete the creation first!
235 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
236 return false; // we need to complete the creation first!
239 wxSize
oursz(GetSize());
241 // move & resize the button and the static line
242 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
247 // move & resize the container window
248 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
249 m_pPane
->SetSize(0, yoffset
,
250 oursz
.x
, oursz
.y
- yoffset
);
252 // this is very important to make the pane window layout show correctly
259 int wxGenericCollapsiblePane::GetBorder() const
261 #if defined( __WXMAC__ )
263 #elif defined(__WXMSW__)
265 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
273 //-----------------------------------------------------------------------------
274 // wxGenericCollapsiblePane - event handlers
275 //-----------------------------------------------------------------------------
277 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
279 if ( event
.GetEventObject() != m_pButton
)
285 Collapse(!IsCollapsed());
287 // this change was generated by the user - send the event
288 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
289 GetEventHandler()->ProcessEvent(ev
);
292 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
294 #if 0 // for debug only
296 dc
.SetPen(*wxBLACK_PEN
);
297 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
298 dc
.DrawRectangle(wxPoint(0,0), GetSize());
299 dc
.SetPen(*wxRED_PEN
);
300 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
306 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE