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"
31 #include "wx/statline.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // ============================================================================
39 // ============================================================================
41 const wxChar wxGenericCollapsiblePaneNameStr
[] = wxT("genericCollapsiblePane");
43 //-----------------------------------------------------------------------------
44 // wxGenericCollapsiblePane
45 //-----------------------------------------------------------------------------
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
48 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane
, wxControl
)
49 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
51 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane
, wxControl
)
52 EVT_BUTTON(wxID_ANY
, wxGenericCollapsiblePane::OnButton
)
53 EVT_SIZE(wxGenericCollapsiblePane::OnSize
)
57 bool wxGenericCollapsiblePane::Create(wxWindow
*parent
,
59 const wxString
& label
,
63 const wxValidator
& val
,
66 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
71 // create children and lay them out using a wxBoxSizer
72 // (so that we automatically get RTL features)
73 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
74 wxDefaultSize
, wxBU_EXACTFIT
);
75 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
77 // on Mac we put the static libe above the button
78 m_sz
= new wxBoxSizer(wxVERTICAL
);
79 m_sz
->Add(m_pStaticLine
, 0, wxALL
|wxGROW
, GetBorder());
80 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxRIGHT
|wxBOTTOM
, GetBorder());
82 // on other platforms we put the static line and the button horizontally
83 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
84 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
85 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
89 SetBackgroundColour(parent
->GetBackgroundColour());
92 // do not set sz as our sizers since we handle the pane window without using sizers
93 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
96 // start as collapsed:
102 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
104 if (m_pButton
&& m_pStaticLine
&& m_sz
)
106 m_pButton
->SetContainingSizer(NULL
);
107 m_pStaticLine
->SetContainingSizer(NULL
);
109 // our sizer is not deleted automatically since we didn't use SetSizer()!
114 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
116 // NB: do not use GetSize() but rather GetMinSize()
117 wxSize sz
= m_sz
->GetMinSize();
119 // when expanded, we need more vertical space
122 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
123 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
129 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
131 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
134 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
136 // minimal size has priority over the best size so set here our min size
140 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
142 // the user asked to explicitely handle the resizing itself...
148 // NB: the following block of code has been accurately designed to
149 // as much flicker-free as possible; be careful when modifying it!
153 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
156 // NB: don't Layout() the 'top' window as its size has not been correctly
157 // updated yet and we don't want to do an initial Layout() with the old
158 // size immediately followed by a SetClientSize/Fit call for the new
159 // size; that would provoke flickering!
163 // FIXME: the SetSizeHints() call would be required also for GTK+ for
164 // the expanded->collapsed transition. Unfortunately if we
165 // enable this line, then the GTK+ top window won't always be
166 // resized by the SetClientSize() call below! As a side effect
167 // of this dirty fix, the minimal size for the pane window is
168 // not set in GTK+ and the user can hide it shrinking the "top"
172 top
->GetSizer()->SetSizeHints(top
);
175 // we shouldn't attempt to resize a maximized window, whatever happens
176 if ( !top
->IsMaximized() )
180 // expanded -> collapsed transition
183 // we have just set the size hints...
184 wxSize sz
= top
->GetSizer()->CalcMin();
186 // use SetClientSize() and not SetSize() otherwise the size for
187 // e.g. a wxFrame with a menubar wouldn't be correctly set
188 top
->SetClientSize(sz
);
195 // collapsed -> expanded transition
197 // force our parent to "fit", i.e. expand so that it can honour
205 void wxGenericCollapsiblePane::Collapse(bool collapse
)
208 if ( IsCollapsed() == collapse
)
212 m_pPane
->Show(!collapse
);
214 // update button label
215 // NB: this must be done after updating our "state"
216 m_pButton
->SetLabel(GetBtnLabel());
218 OnStateChange(GetBestSize());
221 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
224 m_pButton
->SetLabel(GetBtnLabel());
225 m_pButton
->SetInitialSize();
230 bool wxGenericCollapsiblePane::Layout()
232 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
233 return false; // we need to complete the creation first!
235 wxSize
oursz(GetSize());
237 // move & resize the button and the static line
238 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
243 // move & resize the container window
244 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
245 m_pPane
->SetSize(0, yoffset
,
246 oursz
.x
, oursz
.y
- yoffset
);
248 // this is very important to make the pane window layout show correctly
255 int wxGenericCollapsiblePane::GetBorder() const
257 #if defined( __WXMAC__ )
259 #elif defined(__WXGTK20__)
261 #elif defined(__WXMSW__)
263 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
271 //-----------------------------------------------------------------------------
272 // wxGenericCollapsiblePane - event handlers
273 //-----------------------------------------------------------------------------
275 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
277 if ( event
.GetEventObject() != m_pButton
)
283 Collapse(!IsCollapsed());
285 // this change was generated by the user - send the event
286 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
287 GetEventHandler()->ProcessEvent(ev
);
290 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
292 #if 0 // for debug only
294 dc
.SetPen(*wxBLACK_PEN
);
295 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
296 dc
.DrawRectangle(wxPoint(0,0), GetSize());
297 dc
.SetPen(*wxRED_PEN
);
298 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
304 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE