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 wxChar wxCollapsiblePaneNameStr
[] = wxT("collapsiblePane");
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 and lay them out using a wxBoxSizer
73 // (so that we automatically get RTL features)
74 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
75 wxDefaultSize
, wxBU_EXACTFIT
);
76 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
78 // on Mac we put the static libe above the button
79 m_sz
= new wxBoxSizer(wxVERTICAL
);
80 m_sz
->Add(m_pStaticLine
, 0, wxALL
|wxGROW
, GetBorder());
81 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxRIGHT
|wxBOTTOM
, GetBorder());
83 // on other platforms we put the static line and the button horizontally
84 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
85 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
86 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
89 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
90 // this, no idea why...
91 #if defined(__WXWINCE__) || (defined(__WXGTK__) && !defined(__WXGTK20__))
92 SetBackgroundColour(parent
->GetBackgroundColour());
95 // do not set sz as our sizers since we handle the pane window without using sizers
96 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
97 wxTAB_TRAVERSAL
|wxNO_BORDER
);
99 // start as collapsed:
105 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
107 if (m_pButton
&& m_pStaticLine
&& m_sz
)
109 m_pButton
->SetContainingSizer(NULL
);
110 m_pStaticLine
->SetContainingSizer(NULL
);
112 // our sizer is not deleted automatically since we didn't use SetSizer()!
117 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
119 // NB: do not use GetSize() but rather GetMinSize()
120 wxSize sz
= m_sz
->GetMinSize();
122 // when expanded, we need more vertical space
125 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
126 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
132 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
134 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
137 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
139 // minimal size has priority over the best size so set here our min size
143 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
145 // the user asked to explicitely handle the resizing itself...
151 // NB: the following block of code has been accurately designed to
152 // as much flicker-free as possible; be careful when modifying it!
156 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
159 // NB: don't Layout() the 'top' window as its size has not been correctly
160 // updated yet and we don't want to do an initial Layout() with the old
161 // size immediately followed by a SetClientSize/Fit call for the new
162 // size; that would provoke flickering!
166 // FIXME: the SetSizeHints() call would be required also for GTK+ for
167 // the expanded->collapsed transition. Unfortunately if we
168 // enable this line, then the GTK+ top window won't always be
169 // resized by the SetClientSize() call below! As a side effect
170 // of this dirty fix, the minimal size for the pane window is
171 // not set in GTK+ and the user can hide it shrinking the "top"
175 top
->GetSizer()->SetSizeHints(top
);
178 // we shouldn't attempt to resize a maximized window, whatever happens
179 if ( !top
->IsMaximized() )
183 // expanded -> collapsed transition
186 // we have just set the size hints...
187 wxSize sz
= top
->GetSizer()->CalcMin();
189 // use SetClientSize() and not SetSize() otherwise the size for
190 // e.g. a wxFrame with a menubar wouldn't be correctly set
191 top
->SetClientSize(sz
);
198 // collapsed -> expanded transition
200 // force our parent to "fit", i.e. expand so that it can honour
208 void wxGenericCollapsiblePane::Collapse(bool collapse
)
211 if ( IsCollapsed() == collapse
)
215 m_pPane
->Show(!collapse
);
217 // update button label
218 // NB: this must be done after updating our "state"
219 m_pButton
->SetLabel(GetBtnLabel());
221 OnStateChange(GetBestSize());
224 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
227 m_pButton
->SetLabel(GetBtnLabel());
228 m_pButton
->SetInitialSize();
233 bool wxGenericCollapsiblePane::Layout()
235 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
236 return false; // we need to complete the creation first!
238 wxSize
oursz(GetSize());
240 // move & resize the button and the static line
241 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
246 // move & resize the container window
247 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
248 m_pPane
->SetSize(0, yoffset
,
249 oursz
.x
, oursz
.y
- yoffset
);
251 // this is very important to make the pane window layout show correctly
258 int wxGenericCollapsiblePane::GetBorder() const
260 #if defined( __WXMAC__ )
262 #elif defined(__WXGTK20__)
264 #elif defined(__WXMSW__)
266 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
274 //-----------------------------------------------------------------------------
275 // wxGenericCollapsiblePane - event handlers
276 //-----------------------------------------------------------------------------
278 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
280 if ( event
.GetEventObject() != m_pButton
)
286 Collapse(!IsCollapsed());
288 // this change was generated by the user - send the event
289 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
290 GetEventHandler()->ProcessEvent(ev
);
293 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
295 #if 0 // for debug only
297 dc
.SetPen(*wxBLACK_PEN
);
298 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
299 dc
.DrawRectangle(wxPoint(0,0), GetSize());
300 dc
.SetPen(*wxRED_PEN
);
301 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
307 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE