]> git.saurik.com Git - wxWidgets.git/blame - src/generic/collpaneg.cpp
Disable wxUSE_ENH_METAFILE for wxGTK builds.
[wxWidgets.git] / src / generic / collpaneg.cpp
CommitLineData
3c1f8cb1
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/collpaneg.cpp
3// Purpose: wxGenericCollapsiblePane
4// Author: Francesco Montorsi
5// Modified By:
6// Created: 8/10/2006
7// Id: $Id$
8// Copyright: (c) Francesco Montorsi
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
b4715d08
VZ
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
912c3932 24#include "wx/defs.h"
2cbf7014 25
912c3932 26#if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
360c6a85 27
912c3932 28#include "wx/collpane.h"
687b91d2 29
2cbf7014 30#ifndef WX_PRECOMP
360c6a85 31 #include "wx/toplevel.h"
2cbf7014 32 #include "wx/button.h"
4ec3100a 33 #include "wx/sizer.h"
42a2ba5e 34 #include "wx/panel.h"
2cbf7014
VZ
35#endif // !WX_PRECOMP
36
3c1f8cb1
VZ
37#include "wx/statline.h"
38
2cbf7014
VZ
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
3c1f8cb1
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
23318a53 47const char wxCollapsiblePaneNameStr[] = "collapsiblePane";
3c1f8cb1
VZ
48
49//-----------------------------------------------------------------------------
50// wxGenericCollapsiblePane
51//-----------------------------------------------------------------------------
52
ce7fe42e 53wxDEFINE_EVENT( wxEVT_COLLAPSIBLEPANE_CHANGED, wxCollapsiblePaneEvent );
3c1f8cb1
VZ
54IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
55IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
56
57BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
2cbf7014 58 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
3c1f8cb1
VZ
59 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
60END_EVENT_TABLE()
61
68b1c878
VZ
62void wxGenericCollapsiblePane::Init()
63{
68b1c878
VZ
64 m_pButton = NULL;
65 m_pPane = NULL;
66 m_pStaticLine = NULL;
67 m_sz = NULL;
68}
3c1f8cb1 69
2cbf7014
VZ
70bool wxGenericCollapsiblePane::Create(wxWindow *parent,
71 wxWindowID id,
72 const wxString& label,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxValidator& val,
77 const wxString& name)
3c1f8cb1
VZ
78{
79 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
80 return false;
81
82 m_strLabel = label;
83
73b1b996
VZ
84 // sizer containing the expand button and possibly a static line
85 m_sz = new wxBoxSizer(wxHORIZONTAL);
86
96f04e1d 87#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
73b1b996 88 // on Mac we use the special disclosure triangle button
47922888 89 m_pStaticLine = NULL;
73b1b996
VZ
90 m_pButton = new wxDisclosureTriangle(this, wxID_ANY, GetBtnLabel());
91 m_sz->Add(m_pButton);
47922888 92#else
912c3932
VZ
93 // create children and lay them out using a wxBoxSizer
94 // (so that we automatically get RTL features)
2cbf7014 95 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1 96 wxDefaultSize, wxBU_EXACTFIT);
912c3932 97 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
73b1b996 98
912c3932 99 // on other platforms we put the static line and the button horizontally
912c3932
VZ
100 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
101 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
102#endif
103
56eebcda
VZ
104 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
105 // this, no idea why...
ff654490 106#if defined(__WXWINCE__) || defined(__WXGTK__)
948f4c37
WS
107 SetBackgroundColour(parent->GetBackgroundColour());
108#endif
109
912c3932 110 // do not set sz as our sizers since we handle the pane window without using sizers
037c7b4c 111 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
04443021 112 wxTAB_TRAVERSAL|wxNO_BORDER, wxT("wxCollapsiblePanePane") );
3c1f8cb1
VZ
113
114 // start as collapsed:
115 m_pPane->Hide();
116
3c1f8cb1
VZ
117 return true;
118}
119
912c3932 120wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
3c1f8cb1 121{
47922888 122 if (m_pButton)
912c3932 123 m_pButton->SetContainingSizer(NULL);
23318a53 124
47922888 125 if (m_pStaticLine)
912c3932 126 m_pStaticLine->SetContainingSizer(NULL);
23318a53 127
47922888
RR
128 // our sizer is not deleted automatically since we didn't use SetSizer()!
129 wxDELETE(m_sz);
912c3932
VZ
130}
131
132wxSize wxGenericCollapsiblePane::DoGetBestSize() const
133{
134 // NB: do not use GetSize() but rather GetMinSize()
135 wxSize sz = m_sz->GetMinSize();
3c1f8cb1
VZ
136
137 // when expanded, we need more vertical space
550d433e 138 if ( IsExpanded() )
912c3932
VZ
139 {
140 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
141 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
142 }
3c1f8cb1
VZ
143
144 return sz;
145}
146
147wxString wxGenericCollapsiblePane::GetBtnLabel() const
148{
f2412e6a
SC
149 // on mac the triangle indicates the state, no string change
150#ifdef __WXMAC__
151 return m_strLabel;
152#else
550d433e 153 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
f2412e6a 154#endif
3c1f8cb1
VZ
155}
156
4223cec5 157void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 158{
3c1f8cb1 159 // minimal size has priority over the best size so set here our min size
fcdb9540 160// SetMinSize(sz);
3c1f8cb1
VZ
161 SetSize(sz);
162
912c3932
VZ
163 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
164 {
4c51a665 165 // the user asked to explicitly handle the resizing itself...
912c3932
VZ
166 return;
167 }
168
169
ed6008f4
VS
170 wxTopLevelWindow *top =
171 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
172 if ( !top )
173 return;
174
175 wxSizer *sizer = top->GetSizer();
176 if ( !sizer )
177 return;
178
179 const wxSize newBestSize = sizer->ComputeFittingClientSize(top);
180 top->SetMinClientSize(newBestSize);
181
182 // we shouldn't attempt to resize a maximized window, whatever happens
183 if ( !top->IsMaximized() )
184 top->SetClientSize(newBestSize);
3c1f8cb1
VZ
185}
186
4223cec5
VZ
187void wxGenericCollapsiblePane::Collapse(bool collapse)
188{
189 // optimization
190 if ( IsCollapsed() == collapse )
191 return;
192
47c82b4b
VS
193 InvalidateBestSize();
194
4223cec5
VZ
195 // update our state
196 m_pPane->Show(!collapse);
197
198 // update button label
96f04e1d 199#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
f2412e6a 200 m_pButton->SetOpen( !collapse );
47922888 201#else
4223cec5
VZ
202 // NB: this must be done after updating our "state"
203 m_pButton->SetLabel(GetBtnLabel());
47922888
RR
204#endif
205
4223cec5
VZ
206
207 OnStateChange(GetBestSize());
208}
209
3c1f8cb1
VZ
210void wxGenericCollapsiblePane::SetLabel(const wxString &label)
211{
212 m_strLabel = label;
47922888
RR
213#ifdef __WXMAC__
214 m_pButton->SetLabel(GetBtnLabel());
215#else
3c1f8cb1 216 m_pButton->SetLabel(GetBtnLabel());
170acdc9 217 m_pButton->SetInitialSize();
47922888 218#endif
3c1f8cb1 219
912c3932 220 Layout();
3c1f8cb1
VZ
221}
222
912c3932 223bool wxGenericCollapsiblePane::Layout()
3c1f8cb1 224{
47922888
RR
225#ifdef __WXMAC__
226 if (!m_pButton || !m_pPane || !m_sz)
227 return false; // we need to complete the creation first!
228#else
912c3932
VZ
229 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
230 return false; // we need to complete the creation first!
47922888 231#endif
912c3932
VZ
232
233 wxSize oursz(GetSize());
3c1f8cb1 234
912c3932
VZ
235 // move & resize the button and the static line
236 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
237 m_sz->Layout();
3c1f8cb1 238
912c3932
VZ
239 if ( IsExpanded() )
240 {
241 // move & resize the container window
242 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
243 m_pPane->SetSize(0, yoffset,
244 oursz.x, oursz.y - yoffset);
245
246 // this is very important to make the pane window layout show correctly
247 m_pPane->Layout();
248 }
3c1f8cb1 249
912c3932
VZ
250 return true;
251}
252
253int wxGenericCollapsiblePane::GetBorder() const
254{
255#if defined( __WXMAC__ )
256 return 6;
912c3932
VZ
257#elif defined(__WXMSW__)
258 wxASSERT(m_pButton);
259 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
260#else
261 return 5;
262#endif
3c1f8cb1
VZ
263}
264
265
266
267//-----------------------------------------------------------------------------
268// wxGenericCollapsiblePane - event handlers
269//-----------------------------------------------------------------------------
270
2cbf7014 271void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 272{
2cbf7014
VZ
273 if ( event.GetEventObject() != m_pButton )
274 {
275 event.Skip();
276 return;
277 }
278
3c1f8cb1
VZ
279 Collapse(!IsCollapsed());
280
281 // this change was generated by the user - send the event
282 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
283 GetEventHandler()->ProcessEvent(ev);
284}
285
286void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
287{
288#if 0 // for debug only
289 wxClientDC dc(this);
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());
295#endif
296
912c3932 297 Layout();
3c1f8cb1 298}
687b91d2 299
912c3932 300#endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE