]> git.saurik.com Git - wxWidgets.git/blame - src/generic/collpaneg.cpp
implement wxListBox::EnsureVisible() in wxGTK; add a test for it to the widgets sample
[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"
912c3932 19#include "wx/defs.h"
2cbf7014 20
912c3932 21#if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
360c6a85 22
912c3932 23#include "wx/collpane.h"
687b91d2 24
2cbf7014 25#ifndef WX_PRECOMP
360c6a85 26 #include "wx/toplevel.h"
2cbf7014 27 #include "wx/button.h"
4ec3100a 28 #include "wx/sizer.h"
42a2ba5e 29 #include "wx/panel.h"
2cbf7014
VZ
30#endif // !WX_PRECOMP
31
3c1f8cb1
VZ
32#include "wx/statline.h"
33
2cbf7014
VZ
34// ----------------------------------------------------------------------------
35// constants
36// ----------------------------------------------------------------------------
37
3c1f8cb1
VZ
38// ============================================================================
39// implementation
40// ============================================================================
41
037c7b4c 42const wxChar wxCollapsiblePaneNameStr[] = wxT("collapsiblePane");
3c1f8cb1
VZ
43
44//-----------------------------------------------------------------------------
45// wxGenericCollapsiblePane
46//-----------------------------------------------------------------------------
47
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
49IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
50IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
51
52BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
2cbf7014 53 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
3c1f8cb1 54 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
68b1c878
VZ
55
56 WX_EVENT_TABLE_CONTROL_CONTAINER(wxGenericCollapsiblePane)
3c1f8cb1
VZ
57END_EVENT_TABLE()
58
68b1c878
VZ
59WX_DELEGATE_TO_CONTROL_CONTAINER(wxGenericCollapsiblePane, wxControl)
60
61void wxGenericCollapsiblePane::Init()
62{
63 WX_INIT_CONTROL_CONTAINER();
64
65 m_pButton = NULL;
66 m_pPane = NULL;
67 m_pStaticLine = NULL;
68 m_sz = NULL;
69}
3c1f8cb1 70
2cbf7014
VZ
71bool wxGenericCollapsiblePane::Create(wxWindow *parent,
72 wxWindowID id,
73 const wxString& label,
74 const wxPoint& pos,
75 const wxSize& size,
76 long style,
77 const wxValidator& val,
78 const wxString& name)
3c1f8cb1
VZ
79{
80 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
81 return false;
82
83 m_strLabel = label;
84
47922888
RR
85#ifdef __WXMAC__
86 // on Mac we use the disclosure triangle
f2412e6a 87 // we need a light gray line above and below, lets approximate with the frame
47922888 88 m_pStaticLine = NULL;
f2412e6a
SC
89 m_pButton = new wxDisclosureTriangle( this, wxID_ANY, GetBtnLabel(),
90 wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER );
91 m_pButton->SetBackgroundColour( wxColour( 221, 226, 239 ) );
47922888
RR
92 m_sz = new wxBoxSizer(wxHORIZONTAL);
93 // m_sz->Add(4,4); where shall we put it?
f2412e6a 94 m_sz->Add( m_pButton, 1);
47922888 95#else
912c3932
VZ
96 // create children and lay them out using a wxBoxSizer
97 // (so that we automatically get RTL features)
2cbf7014 98 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1 99 wxDefaultSize, wxBU_EXACTFIT);
912c3932 100 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
912c3932
VZ
101 // on other platforms we put the static line and the button horizontally
102 m_sz = new wxBoxSizer(wxHORIZONTAL);
103 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
104 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
105#endif
106
56eebcda
VZ
107 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
108 // this, no idea why...
ff654490 109#if defined(__WXWINCE__) || defined(__WXGTK__)
948f4c37
WS
110 SetBackgroundColour(parent->GetBackgroundColour());
111#endif
112
912c3932 113 // do not set sz as our sizers since we handle the pane window without using sizers
037c7b4c
RD
114 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
115 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1
VZ
116
117 // start as collapsed:
118 m_pPane->Hide();
119
3c1f8cb1
VZ
120 return true;
121}
122
912c3932 123wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
3c1f8cb1 124{
47922888 125 if (m_pButton)
912c3932 126 m_pButton->SetContainingSizer(NULL);
47922888
RR
127
128 if (m_pStaticLine)
912c3932 129 m_pStaticLine->SetContainingSizer(NULL);
47922888
RR
130
131 // our sizer is not deleted automatically since we didn't use SetSizer()!
132 wxDELETE(m_sz);
912c3932
VZ
133}
134
135wxSize wxGenericCollapsiblePane::DoGetBestSize() const
136{
137 // NB: do not use GetSize() but rather GetMinSize()
138 wxSize sz = m_sz->GetMinSize();
3c1f8cb1
VZ
139
140 // when expanded, we need more vertical space
550d433e 141 if ( IsExpanded() )
912c3932
VZ
142 {
143 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
144 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
145 }
3c1f8cb1
VZ
146
147 return sz;
148}
149
150wxString wxGenericCollapsiblePane::GetBtnLabel() const
151{
f2412e6a
SC
152 // on mac the triangle indicates the state, no string change
153#ifdef __WXMAC__
154 return m_strLabel;
155#else
550d433e 156 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
f2412e6a 157#endif
3c1f8cb1
VZ
158}
159
4223cec5 160void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 161{
3c1f8cb1 162 // minimal size has priority over the best size so set here our min size
3c1f8cb1
VZ
163 SetMinSize(sz);
164 SetSize(sz);
165
912c3932
VZ
166 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
167 {
168 // the user asked to explicitely handle the resizing itself...
169 return;
170 }
171
172
173 //
174 // NB: the following block of code has been accurately designed to
175 // as much flicker-free as possible; be careful when modifying it!
176 //
177
069a9976
VZ
178 wxTopLevelWindow *
179 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
180 if ( top )
3c1f8cb1 181 {
912c3932
VZ
182 // NB: don't Layout() the 'top' window as its size has not been correctly
183 // updated yet and we don't want to do an initial Layout() with the old
184 // size immediately followed by a SetClientSize/Fit call for the new
185 // size; that would provoke flickering!
3c1f8cb1 186
3c1f8cb1
VZ
187 if (top->GetSizer())
188#ifdef __WXGTK__
550d433e
VZ
189 // FIXME: the SetSizeHints() call would be required also for GTK+ for
190 // the expanded->collapsed transition. Unfortunately if we
191 // enable this line, then the GTK+ top window won't always be
192 // resized by the SetClientSize() call below! As a side effect
193 // of this dirty fix, the minimal size for the pane window is
194 // not set in GTK+ and the user can hide it shrinking the "top"
195 // window...
3c1f8cb1
VZ
196 if (IsCollapsed())
197#endif
198 top->GetSizer()->SetSizeHints(top);
199
069a9976
VZ
200
201 // we shouldn't attempt to resize a maximized window, whatever happens
202 if ( !top->IsMaximized() )
3c1f8cb1 203 {
069a9976
VZ
204 if ( IsCollapsed() )
205 {
912c3932
VZ
206 // expanded -> collapsed transition
207 if (top->GetSizer())
208 {
209 // we have just set the size hints...
74ab5f5b 210 wxSize szClient = top->GetSizer()->CalcMin();
912c3932
VZ
211
212 // use SetClientSize() and not SetSize() otherwise the size for
213 // e.g. a wxFrame with a menubar wouldn't be correctly set
74ab5f5b 214 top->SetClientSize(szClient);
912c3932
VZ
215 }
216 else
217 top->Layout();
069a9976
VZ
218 }
219 else
220 {
912c3932
VZ
221 // collapsed -> expanded transition
222
069a9976
VZ
223 // force our parent to "fit", i.e. expand so that it can honour
224 // our minimal size
225 top->Fit();
226 }
3c1f8cb1
VZ
227 }
228 }
229}
230
4223cec5
VZ
231void wxGenericCollapsiblePane::Collapse(bool collapse)
232{
233 // optimization
234 if ( IsCollapsed() == collapse )
235 return;
236
237 // update our state
238 m_pPane->Show(!collapse);
239
240 // update button label
47922888 241#ifdef __WXMAC__
f2412e6a 242 m_pButton->SetOpen( !collapse );
47922888 243#else
4223cec5
VZ
244 // NB: this must be done after updating our "state"
245 m_pButton->SetLabel(GetBtnLabel());
47922888
RR
246#endif
247
4223cec5
VZ
248
249 OnStateChange(GetBestSize());
250}
251
3c1f8cb1
VZ
252void wxGenericCollapsiblePane::SetLabel(const wxString &label)
253{
254 m_strLabel = label;
47922888
RR
255#ifdef __WXMAC__
256 m_pButton->SetLabel(GetBtnLabel());
257#else
3c1f8cb1 258 m_pButton->SetLabel(GetBtnLabel());
170acdc9 259 m_pButton->SetInitialSize();
47922888 260#endif
3c1f8cb1 261
912c3932 262 Layout();
3c1f8cb1
VZ
263}
264
912c3932 265bool wxGenericCollapsiblePane::Layout()
3c1f8cb1 266{
47922888
RR
267#ifdef __WXMAC__
268 if (!m_pButton || !m_pPane || !m_sz)
269 return false; // we need to complete the creation first!
270#else
912c3932
VZ
271 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
272 return false; // we need to complete the creation first!
47922888 273#endif
912c3932
VZ
274
275 wxSize oursz(GetSize());
3c1f8cb1 276
912c3932
VZ
277 // move & resize the button and the static line
278 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
279 m_sz->Layout();
3c1f8cb1 280
912c3932
VZ
281 if ( IsExpanded() )
282 {
283 // move & resize the container window
284 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
285 m_pPane->SetSize(0, yoffset,
286 oursz.x, oursz.y - yoffset);
287
288 // this is very important to make the pane window layout show correctly
289 m_pPane->Layout();
290 }
3c1f8cb1 291
912c3932
VZ
292 return true;
293}
294
295int wxGenericCollapsiblePane::GetBorder() const
296{
297#if defined( __WXMAC__ )
298 return 6;
912c3932
VZ
299#elif defined(__WXMSW__)
300 wxASSERT(m_pButton);
301 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
302#else
303 return 5;
304#endif
3c1f8cb1
VZ
305}
306
307
308
309//-----------------------------------------------------------------------------
310// wxGenericCollapsiblePane - event handlers
311//-----------------------------------------------------------------------------
312
2cbf7014 313void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 314{
2cbf7014
VZ
315 if ( event.GetEventObject() != m_pButton )
316 {
317 event.Skip();
318 return;
319 }
320
3c1f8cb1
VZ
321 Collapse(!IsCollapsed());
322
323 // this change was generated by the user - send the event
324 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
325 GetEventHandler()->ProcessEvent(ev);
326}
327
328void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
329{
330#if 0 // for debug only
331 wxClientDC dc(this);
332 dc.SetPen(*wxBLACK_PEN);
333 dc.SetBrush(*wxTRANSPARENT_BRUSH);
334 dc.DrawRectangle(wxPoint(0,0), GetSize());
335 dc.SetPen(*wxRED_PEN);
336 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
337#endif
338
912c3932 339 Layout();
3c1f8cb1 340}
687b91d2 341
912c3932 342#endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE