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());
88 // do not set sz as our sizers since we handle the pane window without using sizers
89 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
92 // start as collapsed:
98 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
100 if (m_pButton
&& m_pStaticLine
&& m_sz
)
102 m_pButton
->SetContainingSizer(NULL
);
103 m_pStaticLine
->SetContainingSizer(NULL
);
105 // our sizer is not deleted automatically since we didn't use SetSizer()!
110 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
112 // NB: do not use GetSize() but rather GetMinSize()
113 wxSize sz
= m_sz
->GetMinSize();
115 // when expanded, we need more vertical space
118 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
119 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
125 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
127 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
130 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
132 // minimal size has priority over the best size so set here our min size
136 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
138 // the user asked to explicitely handle the resizing itself...
144 // NB: the following block of code has been accurately designed to
145 // as much flicker-free as possible; be careful when modifying it!
149 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
152 // NB: don't Layout() the 'top' window as its size has not been correctly
153 // updated yet and we don't want to do an initial Layout() with the old
154 // size immediately followed by a SetClientSize/Fit call for the new
155 // size; that would provoke flickering!
159 // FIXME: the SetSizeHints() call would be required also for GTK+ for
160 // the expanded->collapsed transition. Unfortunately if we
161 // enable this line, then the GTK+ top window won't always be
162 // resized by the SetClientSize() call below! As a side effect
163 // of this dirty fix, the minimal size for the pane window is
164 // not set in GTK+ and the user can hide it shrinking the "top"
168 top
->GetSizer()->SetSizeHints(top
);
171 // we shouldn't attempt to resize a maximized window, whatever happens
172 if ( !top
->IsMaximized() )
176 // expanded -> collapsed transition
179 // we have just set the size hints...
180 wxSize sz
= top
->GetSizer()->CalcMin();
182 // use SetClientSize() and not SetSize() otherwise the size for
183 // e.g. a wxFrame with a menubar wouldn't be correctly set
184 top
->SetClientSize(sz
);
191 // collapsed -> expanded transition
193 // force our parent to "fit", i.e. expand so that it can honour
201 void wxGenericCollapsiblePane::Collapse(bool collapse
)
204 if ( IsCollapsed() == collapse
)
208 m_pPane
->Show(!collapse
);
210 // update button label
211 // NB: this must be done after updating our "state"
212 m_pButton
->SetLabel(GetBtnLabel());
214 OnStateChange(GetBestSize());
217 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
220 m_pButton
->SetLabel(GetBtnLabel());
221 m_pButton
->SetInitialSize();
226 bool wxGenericCollapsiblePane::Layout()
228 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
229 return false; // we need to complete the creation first!
231 wxSize
oursz(GetSize());
233 // move & resize the button and the static line
234 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
239 // move & resize the container window
240 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
241 m_pPane
->SetSize(0, yoffset
,
242 oursz
.x
, oursz
.y
- yoffset
);
244 // this is very important to make the pane window layout show correctly
251 int wxGenericCollapsiblePane::GetBorder() const
253 #if defined( __WXMAC__ )
255 #elif defined(__WXGTK20__)
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