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
) )
73 // on Mac we use the disclosure triangle
75 m_pButton
= new wxDisclosureTriangle( this, wxID_ANY
, GetBtnLabel() );
76 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
77 // m_sz->Add(4,4); where shall we put it?
78 m_sz
->Add( m_pButton
);
80 // create children and lay them out using a wxBoxSizer
81 // (so that we automatically get RTL features)
82 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
83 wxDefaultSize
, wxBU_EXACTFIT
);
84 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
85 // on other platforms we put the static line and the button horizontally
86 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
87 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
88 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
91 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
92 // this, no idea why...
93 #if defined(__WXWINCE__) || defined(__WXGTK__)
94 SetBackgroundColour(parent
->GetBackgroundColour());
97 // do not set sz as our sizers since we handle the pane window without using sizers
98 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
99 wxTAB_TRAVERSAL
|wxNO_BORDER
);
101 // start as collapsed:
107 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
110 m_pButton
->SetContainingSizer(NULL
);
113 m_pStaticLine
->SetContainingSizer(NULL
);
115 // our sizer is not deleted automatically since we didn't use SetSizer()!
119 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
121 // NB: do not use GetSize() but rather GetMinSize()
122 wxSize sz
= m_sz
->GetMinSize();
124 // when expanded, we need more vertical space
127 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
128 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
134 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
136 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
139 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
141 // minimal size has priority over the best size so set here our min size
145 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
147 // the user asked to explicitely handle the resizing itself...
153 // NB: the following block of code has been accurately designed to
154 // as much flicker-free as possible; be careful when modifying it!
158 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
161 // NB: don't Layout() the 'top' window as its size has not been correctly
162 // updated yet and we don't want to do an initial Layout() with the old
163 // size immediately followed by a SetClientSize/Fit call for the new
164 // size; that would provoke flickering!
168 // FIXME: the SetSizeHints() call would be required also for GTK+ for
169 // the expanded->collapsed transition. Unfortunately if we
170 // enable this line, then the GTK+ top window won't always be
171 // resized by the SetClientSize() call below! As a side effect
172 // of this dirty fix, the minimal size for the pane window is
173 // not set in GTK+ and the user can hide it shrinking the "top"
177 top
->GetSizer()->SetSizeHints(top
);
180 // we shouldn't attempt to resize a maximized window, whatever happens
181 if ( !top
->IsMaximized() )
185 // expanded -> collapsed transition
188 // we have just set the size hints...
189 wxSize szClient
= top
->GetSizer()->CalcMin();
191 // use SetClientSize() and not SetSize() otherwise the size for
192 // e.g. a wxFrame with a menubar wouldn't be correctly set
193 top
->SetClientSize(szClient
);
200 // collapsed -> expanded transition
202 // force our parent to "fit", i.e. expand so that it can honour
210 void wxGenericCollapsiblePane::Collapse(bool collapse
)
213 if ( IsCollapsed() == collapse
)
217 m_pPane
->Show(!collapse
);
219 // update button label
221 m_pButton
->SetLabel(GetBtnLabel());
223 // NB: this must be done after updating our "state"
224 m_pButton
->SetLabel(GetBtnLabel());
228 OnStateChange(GetBestSize());
231 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
235 m_pButton
->SetLabel(GetBtnLabel());
237 m_pButton
->SetLabel(GetBtnLabel());
238 m_pButton
->SetInitialSize();
244 bool wxGenericCollapsiblePane::Layout()
247 if (!m_pButton
|| !m_pPane
|| !m_sz
)
248 return false; // we need to complete the creation first!
250 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
251 return false; // we need to complete the creation first!
254 wxSize
oursz(GetSize());
256 // move & resize the button and the static line
257 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
262 // move & resize the container window
263 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
264 m_pPane
->SetSize(0, yoffset
,
265 oursz
.x
, oursz
.y
- yoffset
);
267 // this is very important to make the pane window layout show correctly
274 int wxGenericCollapsiblePane::GetBorder() const
276 #if defined( __WXMAC__ )
278 #elif defined(__WXMSW__)
280 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
288 //-----------------------------------------------------------------------------
289 // wxGenericCollapsiblePane - event handlers
290 //-----------------------------------------------------------------------------
292 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
294 if ( event
.GetEventObject() != m_pButton
)
300 Collapse(!IsCollapsed());
302 // this change was generated by the user - send the event
303 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
304 GetEventHandler()->ProcessEvent(ev
);
307 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
309 #if 0 // for debug only
311 dc
.SetPen(*wxBLACK_PEN
);
312 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
313 dc
.DrawRectangle(wxPoint(0,0), GetSize());
314 dc
.SetPen(*wxRED_PEN
);
315 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
321 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE