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
74 // we need a light gray line above and below, lets approximate with the frame
76 m_pButton
= new wxDisclosureTriangle( this, wxID_ANY
, GetBtnLabel(),
77 wxDefaultPosition
, wxDefaultSize
, wxSIMPLE_BORDER
);
78 m_pButton
->SetBackgroundColour( wxColour( 221, 226, 239 ) );
79 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
80 // m_sz->Add(4,4); where shall we put it?
81 m_sz
->Add( m_pButton
, 1);
83 // create children and lay them out using a wxBoxSizer
84 // (so that we automatically get RTL features)
85 m_pButton
= new wxButton(this, wxID_ANY
, GetBtnLabel(), wxPoint(0, 0),
86 wxDefaultSize
, wxBU_EXACTFIT
);
87 m_pStaticLine
= new wxStaticLine(this, wxID_ANY
);
88 // on other platforms we put the static line and the button horizontally
89 m_sz
= new wxBoxSizer(wxHORIZONTAL
);
90 m_sz
->Add(m_pButton
, 0, wxLEFT
|wxTOP
|wxBOTTOM
, GetBorder());
91 m_sz
->Add(m_pStaticLine
, 1, wxALIGN_CENTER
|wxLEFT
|wxRIGHT
, GetBorder());
94 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
95 // this, no idea why...
96 #if defined(__WXWINCE__) || defined(__WXGTK__)
97 SetBackgroundColour(parent
->GetBackgroundColour());
100 // do not set sz as our sizers since we handle the pane window without using sizers
101 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
102 wxTAB_TRAVERSAL
|wxNO_BORDER
);
104 // start as collapsed:
110 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
113 m_pButton
->SetContainingSizer(NULL
);
116 m_pStaticLine
->SetContainingSizer(NULL
);
118 // our sizer is not deleted automatically since we didn't use SetSizer()!
122 wxSize
wxGenericCollapsiblePane::DoGetBestSize() const
124 // NB: do not use GetSize() but rather GetMinSize()
125 wxSize sz
= m_sz
->GetMinSize();
127 // when expanded, we need more vertical space
130 sz
.SetWidth(wxMax( sz
.GetWidth(), m_pPane
->GetBestSize().x
));
131 sz
.SetHeight(sz
.y
+ GetBorder() + m_pPane
->GetBestSize().y
);
137 wxString
wxGenericCollapsiblePane::GetBtnLabel() const
139 // on mac the triangle indicates the state, no string change
143 return m_strLabel
+ (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
147 void wxGenericCollapsiblePane::OnStateChange(const wxSize
& sz
)
149 // minimal size has priority over the best size so set here our min size
153 if (this->HasFlag(wxCP_NO_TLW_RESIZE
))
155 // the user asked to explicitely handle the resizing itself...
161 // NB: the following block of code has been accurately designed to
162 // as much flicker-free as possible; be careful when modifying it!
166 top
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
169 // NB: don't Layout() the 'top' window as its size has not been correctly
170 // updated yet and we don't want to do an initial Layout() with the old
171 // size immediately followed by a SetClientSize/Fit call for the new
172 // size; that would provoke flickering!
176 // FIXME: the SetSizeHints() call would be required also for GTK+ for
177 // the expanded->collapsed transition. Unfortunately if we
178 // enable this line, then the GTK+ top window won't always be
179 // resized by the SetClientSize() call below! As a side effect
180 // of this dirty fix, the minimal size for the pane window is
181 // not set in GTK+ and the user can hide it shrinking the "top"
185 top
->GetSizer()->SetSizeHints(top
);
188 // we shouldn't attempt to resize a maximized window, whatever happens
189 if ( !top
->IsMaximized() )
193 // expanded -> collapsed transition
196 // we have just set the size hints...
197 wxSize szClient
= top
->GetSizer()->CalcMin();
199 // use SetClientSize() and not SetSize() otherwise the size for
200 // e.g. a wxFrame with a menubar wouldn't be correctly set
201 top
->SetClientSize(szClient
);
208 // collapsed -> expanded transition
210 // force our parent to "fit", i.e. expand so that it can honour
218 void wxGenericCollapsiblePane::Collapse(bool collapse
)
221 if ( IsCollapsed() == collapse
)
225 m_pPane
->Show(!collapse
);
227 // update button label
229 m_pButton
->SetOpen( !collapse
);
231 // NB: this must be done after updating our "state"
232 m_pButton
->SetLabel(GetBtnLabel());
236 OnStateChange(GetBestSize());
239 void wxGenericCollapsiblePane::SetLabel(const wxString
&label
)
243 m_pButton
->SetLabel(GetBtnLabel());
245 m_pButton
->SetLabel(GetBtnLabel());
246 m_pButton
->SetInitialSize();
252 bool wxGenericCollapsiblePane::Layout()
255 if (!m_pButton
|| !m_pPane
|| !m_sz
)
256 return false; // we need to complete the creation first!
258 if (!m_pButton
|| !m_pStaticLine
|| !m_pPane
|| !m_sz
)
259 return false; // we need to complete the creation first!
262 wxSize
oursz(GetSize());
264 // move & resize the button and the static line
265 m_sz
->SetDimension(0, 0, oursz
.GetWidth(), m_sz
->GetMinSize().GetHeight());
270 // move & resize the container window
271 int yoffset
= m_sz
->GetSize().GetHeight() + GetBorder();
272 m_pPane
->SetSize(0, yoffset
,
273 oursz
.x
, oursz
.y
- yoffset
);
275 // this is very important to make the pane window layout show correctly
282 int wxGenericCollapsiblePane::GetBorder() const
284 #if defined( __WXMAC__ )
286 #elif defined(__WXMSW__)
288 return m_pButton
->ConvertDialogToPixels(wxSize(2, 0)).x
;
296 //-----------------------------------------------------------------------------
297 // wxGenericCollapsiblePane - event handlers
298 //-----------------------------------------------------------------------------
300 void wxGenericCollapsiblePane::OnButton(wxCommandEvent
& event
)
302 if ( event
.GetEventObject() != m_pButton
)
308 Collapse(!IsCollapsed());
310 // this change was generated by the user - send the event
311 wxCollapsiblePaneEvent
ev(this, GetId(), IsCollapsed());
312 GetEventHandler()->ProcessEvent(ev
);
315 void wxGenericCollapsiblePane::OnSize(wxSizeEvent
& WXUNUSED(event
))
317 #if 0 // for debug only
319 dc
.SetPen(*wxBLACK_PEN
);
320 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
321 dc
.DrawRectangle(wxPoint(0,0), GetSize());
322 dc
.SetPen(*wxRED_PEN
);
323 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
329 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE