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 #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
23 #include "wx/collpane.h"
26 #include "wx/toplevel.h"
27 #include "wx/button.h"
32 #include "wx/statline.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // ============================================================================
40 // ============================================================================
42 const char wxCollapsiblePaneNameStr
[] = "collapsiblePane";
44 //-----------------------------------------------------------------------------
45 // wxGenericCollapsiblePane
46 //-----------------------------------------------------------------------------
48 wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED
, wxCollapsiblePaneEvent
)
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
)
56 WX_EVENT_TABLE_CONTROL_CONTAINER(wxGenericCollapsiblePane
)
59 WX_DELEGATE_TO_CONTROL_CONTAINER(wxGenericCollapsiblePane
, wxControl
)
61 void wxGenericCollapsiblePane::Init()
63 WX_INIT_CONTROL_CONTAINER();
71 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
73 const wxString
& label
,
77 const wxValidator
& val
,
80 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
85 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
86 // on Mac we use the disclosure triangle
87 // we need a light gray line above and below, lets approximate with the frame
89 m_pButton
= new wxDisclosureTriangle( this, wxID_ANY
, GetBtnLabel(),
90 wxDefaultPosition
, wxDefaultSize
, wxSIMPLE_BORDER
);
91 m_pButton
->SetBackgroundColour( wxColour( 221, 226, 239 ) );
92 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
93 // m_sz->Add(4,4); where shall we put it?
94 m_sz
->Add( m_pButton
, 1);
96 // create children and lay them out using a wxBoxSizer
97 // (so that we automatically get RTL features)
98 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
99 wxDefaultSize
, wxBU_EXACTFIT
);
100 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
101 // on other platforms we put the static line and the button horizontally
102 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
103 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
104 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
107 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
108 // this, no idea why...
109 #if defined(__WXWINCE__) || defined(__WXGTK__)
110 SetBackgroundColour(parent
->GetBackgroundColour());
113 // do not set sz as our sizers since we handle the pane window without using sizers
114 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
115 wxTAB_TRAVERSAL
|wxNO_BORDER
);
117 // start as collapsed:
123 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
126 m_pButton
->SetContainingSizer(NULL
);
129 m_pStaticLine
->SetContainingSizer(NULL
);
131 // our sizer is not deleted automatically since we didn't use SetSizer()!
135 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
137 // NB: do not use GetSize() but rather GetMinSize()
138 wxSize sz
= m_sz
->GetMinSize();
140 // when expanded, we need more vertical space
143 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
144 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
150 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
152 // on mac the triangle indicates the state, no string change
156 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
160 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
162 // minimal size has priority over the best size so set here our min size
166 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
168 // the user asked to explicitely handle the resizing itself...
173 wxTopLevelWindow
*top
=
174 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
178 wxSizer
*sizer
= top
->GetSizer();
182 const wxSize newBestSize
= sizer
->ComputeFittingClientSize(top
);
183 top
->SetMinClientSize(newBestSize
);
185 // we shouldn't attempt to resize a maximized window, whatever happens
186 if ( !top
->IsMaximized() )
187 top
->SetClientSize(newBestSize
);
190 void wxGenericCollapsiblePane::Collapse(bool collapse
)
193 if ( IsCollapsed() == collapse
)
197 m_pPane
->Show(!collapse
);
199 // update button label
200 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
201 m_pButton
->SetOpen( !collapse
);
203 // NB: this must be done after updating our "state"
204 m_pButton
->SetLabel(GetBtnLabel());
208 OnStateChange(GetBestSize());
211 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
215 m_pButton
->SetLabel(GetBtnLabel());
217 m_pButton
->SetLabel(GetBtnLabel());
218 m_pButton
->SetInitialSize();
224 bool wxGenericCollapsiblePane::Layout()
227 if (!m_pButton
|| !m_pPane
|| !m_sz
)
228 return false; // we need to complete the creation first!
230 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
231 return false; // we need to complete the creation first!
234 wxSize
oursz(GetSize());
236 // move & resize the button and the static line
237 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
242 // move & resize the container window
243 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
244 m_pPane
->SetSize(0, yoffset
,
245 oursz
.x
, oursz
.y
- yoffset
);
247 // this is very important to make the pane window layout show correctly
254 int wxGenericCollapsiblePane::GetBorder() const
256 #if defined( __WXMAC__ )
258 #elif defined(__WXMSW__)
260 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
268 //-----------------------------------------------------------------------------
269 // wxGenericCollapsiblePane - event handlers
270 //-----------------------------------------------------------------------------
272 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
274 if ( event
.GetEventObject() != m_pButton
)
280 Collapse(!IsCollapsed());
282 // this change was generated by the user - send the event
283 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
284 GetEventHandler()->ProcessEvent(ev
);
287 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
289 #if 0 // for debug only
291 dc
.SetPen(*wxBLACK_PEN
);
292 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
293 dc
.DrawRectangle(wxPoint(0,0), GetSize());
294 dc
.SetPen(*wxRED_PEN
);
295 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
301 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE