mention wxTreeCtrl::SelectItem() fix of r60921
[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
9b11752c 53wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_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 59 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
68b1c878
VZ
60
61 WX_EVENT_TABLE_CONTROL_CONTAINER(wxGenericCollapsiblePane)
3c1f8cb1
VZ
62END_EVENT_TABLE()
63
68b1c878
VZ
64WX_DELEGATE_TO_CONTROL_CONTAINER(wxGenericCollapsiblePane, wxControl)
65
66void wxGenericCollapsiblePane::Init()
67{
68 WX_INIT_CONTROL_CONTAINER();
69
70 m_pButton = NULL;
71 m_pPane = NULL;
72 m_pStaticLine = NULL;
73 m_sz = NULL;
74}
3c1f8cb1 75
2cbf7014
VZ
76bool wxGenericCollapsiblePane::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxString& label,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxValidator& val,
83 const wxString& name)
3c1f8cb1
VZ
84{
85 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
86 return false;
87
88 m_strLabel = label;
89
96f04e1d 90#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
47922888 91 // on Mac we use the disclosure triangle
f2412e6a 92 // we need a light gray line above and below, lets approximate with the frame
47922888 93 m_pStaticLine = NULL;
23318a53 94 m_pButton = new wxDisclosureTriangle( this, wxID_ANY, GetBtnLabel(),
f2412e6a
SC
95 wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER );
96 m_pButton->SetBackgroundColour( wxColour( 221, 226, 239 ) );
47922888
RR
97 m_sz = new wxBoxSizer(wxHORIZONTAL);
98 // m_sz->Add(4,4); where shall we put it?
f2412e6a 99 m_sz->Add( m_pButton, 1);
47922888 100#else
912c3932
VZ
101 // create children and lay them out using a wxBoxSizer
102 // (so that we automatically get RTL features)
2cbf7014 103 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1 104 wxDefaultSize, wxBU_EXACTFIT);
912c3932 105 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
912c3932
VZ
106 // on other platforms we put the static line and the button horizontally
107 m_sz = new wxBoxSizer(wxHORIZONTAL);
108 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
109 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
110#endif
111
56eebcda
VZ
112 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
113 // this, no idea why...
ff654490 114#if defined(__WXWINCE__) || defined(__WXGTK__)
948f4c37
WS
115 SetBackgroundColour(parent->GetBackgroundColour());
116#endif
117
912c3932 118 // do not set sz as our sizers since we handle the pane window without using sizers
037c7b4c 119 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
04443021 120 wxTAB_TRAVERSAL|wxNO_BORDER, wxT("wxCollapsiblePanePane") );
3c1f8cb1
VZ
121
122 // start as collapsed:
123 m_pPane->Hide();
124
3c1f8cb1
VZ
125 return true;
126}
127
912c3932 128wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
3c1f8cb1 129{
47922888 130 if (m_pButton)
912c3932 131 m_pButton->SetContainingSizer(NULL);
23318a53 132
47922888 133 if (m_pStaticLine)
912c3932 134 m_pStaticLine->SetContainingSizer(NULL);
23318a53 135
47922888
RR
136 // our sizer is not deleted automatically since we didn't use SetSizer()!
137 wxDELETE(m_sz);
912c3932
VZ
138}
139
140wxSize wxGenericCollapsiblePane::DoGetBestSize() const
141{
142 // NB: do not use GetSize() but rather GetMinSize()
143 wxSize sz = m_sz->GetMinSize();
3c1f8cb1
VZ
144
145 // when expanded, we need more vertical space
550d433e 146 if ( IsExpanded() )
912c3932
VZ
147 {
148 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
149 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
150 }
3c1f8cb1
VZ
151
152 return sz;
153}
154
155wxString wxGenericCollapsiblePane::GetBtnLabel() const
156{
f2412e6a
SC
157 // on mac the triangle indicates the state, no string change
158#ifdef __WXMAC__
159 return m_strLabel;
160#else
550d433e 161 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
f2412e6a 162#endif
3c1f8cb1
VZ
163}
164
4223cec5 165void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 166{
3c1f8cb1 167 // minimal size has priority over the best size so set here our min size
fcdb9540 168// SetMinSize(sz);
3c1f8cb1
VZ
169 SetSize(sz);
170
912c3932
VZ
171 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
172 {
173 // the user asked to explicitely handle the resizing itself...
174 return;
175 }
176
177
ed6008f4
VS
178 wxTopLevelWindow *top =
179 wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
180 if ( !top )
181 return;
182
183 wxSizer *sizer = top->GetSizer();
184 if ( !sizer )
185 return;
186
187 const wxSize newBestSize = sizer->ComputeFittingClientSize(top);
188 top->SetMinClientSize(newBestSize);
189
190 // we shouldn't attempt to resize a maximized window, whatever happens
191 if ( !top->IsMaximized() )
192 top->SetClientSize(newBestSize);
3c1f8cb1
VZ
193}
194
4223cec5
VZ
195void wxGenericCollapsiblePane::Collapse(bool collapse)
196{
197 // optimization
198 if ( IsCollapsed() == collapse )
199 return;
200
201 // update our state
202 m_pPane->Show(!collapse);
203
204 // update button label
96f04e1d 205#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
f2412e6a 206 m_pButton->SetOpen( !collapse );
47922888 207#else
4223cec5
VZ
208 // NB: this must be done after updating our "state"
209 m_pButton->SetLabel(GetBtnLabel());
47922888
RR
210#endif
211
4223cec5
VZ
212
213 OnStateChange(GetBestSize());
214}
215
3c1f8cb1
VZ
216void wxGenericCollapsiblePane::SetLabel(const wxString &label)
217{
218 m_strLabel = label;
47922888
RR
219#ifdef __WXMAC__
220 m_pButton->SetLabel(GetBtnLabel());
221#else
3c1f8cb1 222 m_pButton->SetLabel(GetBtnLabel());
170acdc9 223 m_pButton->SetInitialSize();
47922888 224#endif
3c1f8cb1 225
912c3932 226 Layout();
3c1f8cb1
VZ
227}
228
912c3932 229bool wxGenericCollapsiblePane::Layout()
3c1f8cb1 230{
47922888
RR
231#ifdef __WXMAC__
232 if (!m_pButton || !m_pPane || !m_sz)
233 return false; // we need to complete the creation first!
234#else
912c3932
VZ
235 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
236 return false; // we need to complete the creation first!
47922888 237#endif
912c3932
VZ
238
239 wxSize oursz(GetSize());
3c1f8cb1 240
912c3932
VZ
241 // move & resize the button and the static line
242 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
243 m_sz->Layout();
3c1f8cb1 244
912c3932
VZ
245 if ( IsExpanded() )
246 {
247 // move & resize the container window
248 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
249 m_pPane->SetSize(0, yoffset,
250 oursz.x, oursz.y - yoffset);
251
252 // this is very important to make the pane window layout show correctly
253 m_pPane->Layout();
254 }
3c1f8cb1 255
912c3932
VZ
256 return true;
257}
258
259int wxGenericCollapsiblePane::GetBorder() const
260{
261#if defined( __WXMAC__ )
262 return 6;
912c3932
VZ
263#elif defined(__WXMSW__)
264 wxASSERT(m_pButton);
265 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
266#else
267 return 5;
268#endif
3c1f8cb1
VZ
269}
270
271
272
273//-----------------------------------------------------------------------------
274// wxGenericCollapsiblePane - event handlers
275//-----------------------------------------------------------------------------
276
2cbf7014 277void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 278{
2cbf7014
VZ
279 if ( event.GetEventObject() != m_pButton )
280 {
281 event.Skip();
282 return;
283 }
284
3c1f8cb1
VZ
285 Collapse(!IsCollapsed());
286
287 // this change was generated by the user - send the event
288 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
289 GetEventHandler()->ProcessEvent(ev);
290}
291
292void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
293{
294#if 0 // for debug only
295 wxClientDC dc(this);
296 dc.SetPen(*wxBLACK_PEN);
297 dc.SetBrush(*wxTRANSPARENT_BRUSH);
298 dc.DrawRectangle(wxPoint(0,0), GetSize());
299 dc.SetPen(*wxRED_PEN);
300 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
301#endif
302
912c3932 303 Layout();
3c1f8cb1 304}
687b91d2 305
912c3932 306#endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE