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