compilation fix: wxGenericCollapsiblePane needs wxButton and wxStaticLine
[wxWidgets.git] / src / generic / collpaneg.cpp
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"
19
20 #include "wx/collpane.h"
21
22 #if wxUSE_BUTTON && wxUSE_STATLINE
23
24 #ifndef WX_PRECOMP
25 #include "wx/toplevel.h"
26 #include "wx/button.h"
27 #include "wx/sizer.h"
28 #endif // !WX_PRECOMP
29
30 #include "wx/statline.h"
31
32 // ----------------------------------------------------------------------------
33 // constants
34 // ----------------------------------------------------------------------------
35
36 // the number of pixels to leave between the button and the static line and
37 // between the button and the pane
38 #define wxCP_MARGIN 10
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 const wxChar wxGenericCollapsiblePaneNameStr[] = wxT("genericCollapsiblePane");
45
46 //-----------------------------------------------------------------------------
47 // wxGenericCollapsiblePane
48 //-----------------------------------------------------------------------------
49
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
51 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
52 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
53
54 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
55 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
56 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
57 END_EVENT_TABLE()
58
59
60 bool wxGenericCollapsiblePane::Create(wxWindow *parent,
61 wxWindowID id,
62 const wxString& label,
63 const wxPoint& pos,
64 const wxSize& size,
65 long style,
66 const wxValidator& val,
67 const wxString& name)
68 {
69 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
70 return false;
71
72 m_strLabel = label;
73
74 // create children; their size & position is set in OnSize()
75 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
76 wxDefaultSize, wxBU_EXACTFIT);
77 m_pStatLine = new wxStaticLine(this, wxID_ANY);
78 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
79 wxNO_BORDER);
80
81 // start as collapsed:
82 m_pPane->Hide();
83
84 //CacheBestSize(GetBestSize());
85 return true;
86 }
87
88 wxSize wxGenericCollapsiblePane::DoGetBestSize() const
89 {
90 wxSize sz = m_pButton->GetBestSize();
91
92 // set width
93 sz.SetWidth(sz.x + wxCP_MARGIN + m_pStatLine->GetBestSize().x);
94 const wxCoord paneWidth = m_pPane->GetBestSize().x;
95 if ( sz.x < paneWidth )
96 sz.x = paneWidth;
97
98 // when expanded, we need more vertical space
99 if ( IsExpanded() )
100 sz.SetHeight(sz.y + wxCP_MARGIN + m_pPane->GetBestSize().y);
101
102 return sz;
103 }
104
105 wxString wxGenericCollapsiblePane::GetBtnLabel() const
106 {
107 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
108 }
109
110 void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
111 {
112 // minimal size has priority over the best size so set here our min size
113 SetMinSize(sz);
114 SetSize(sz);
115
116 wxTopLevelWindow *
117 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
118 if ( top )
119 {
120 // we've changed our size, thus our top level parent needs to relayout
121 // itself
122 top->Layout();
123
124 // FIXME: this makes wxGenericCollapsiblePane behave as the user expect
125 // but maybe there are cases where this is unwanted!
126 if (top->GetSizer())
127 #ifdef __WXGTK__
128 // FIXME: the SetSizeHints() call would be required also for GTK+ for
129 // the expanded->collapsed transition. Unfortunately if we
130 // enable this line, then the GTK+ top window won't always be
131 // resized by the SetClientSize() call below! As a side effect
132 // of this dirty fix, the minimal size for the pane window is
133 // not set in GTK+ and the user can hide it shrinking the "top"
134 // window...
135 if (IsCollapsed())
136 #endif
137 top->GetSizer()->SetSizeHints(top);
138
139
140 // we shouldn't attempt to resize a maximized window, whatever happens
141 if ( !top->IsMaximized() )
142 {
143 if ( IsCollapsed() )
144 {
145 // use SetClientSize() and not SetSize() otherwise the size for
146 // e.g. a wxFrame with a menubar wouldn't be correctly set
147 top->SetClientSize(sz);
148 }
149 else
150 {
151 // force our parent to "fit", i.e. expand so that it can honour
152 // our minimal size
153 top->Fit();
154 }
155 }
156 }
157 }
158
159 void wxGenericCollapsiblePane::Collapse(bool collapse)
160 {
161 // optimization
162 if ( IsCollapsed() == collapse )
163 return;
164
165 // update our state
166 m_pPane->Show(!collapse);
167
168 // update button label
169 // NB: this must be done after updating our "state"
170 m_pButton->SetLabel(GetBtnLabel());
171
172 OnStateChange(GetBestSize());
173 }
174
175 void wxGenericCollapsiblePane::SetLabel(const wxString &label)
176 {
177 m_strLabel = label;
178 m_pButton->SetLabel(GetBtnLabel());
179 m_pButton->SetBestFittingSize();
180
181 LayoutChildren();
182 }
183
184 void wxGenericCollapsiblePane::LayoutChildren()
185 {
186 wxSize btnSz = m_pButton->GetSize();
187
188 // the button position & size are always ok...
189
190 // move & resize the static line
191 m_pStatLine->SetSize(btnSz.x + wxCP_MARGIN, btnSz.y/2,
192 GetSize().x - btnSz.x - wxCP_MARGIN, -1,
193 wxSIZE_USE_EXISTING);
194
195 // move & resize the container window
196 m_pPane->SetSize(0, btnSz.y + wxCP_MARGIN,
197 GetSize().x, GetSize().y - btnSz.y - wxCP_MARGIN);
198 }
199
200
201
202 //-----------------------------------------------------------------------------
203 // wxGenericCollapsiblePane - event handlers
204 //-----------------------------------------------------------------------------
205
206 void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
207 {
208 if ( event.GetEventObject() != m_pButton )
209 {
210 event.Skip();
211 return;
212 }
213
214 Collapse(!IsCollapsed());
215
216 // this change was generated by the user - send the event
217 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
218 GetEventHandler()->ProcessEvent(ev);
219 }
220
221 void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
222 {
223 #if 0 // for debug only
224 wxClientDC dc(this);
225 dc.SetPen(*wxBLACK_PEN);
226 dc.SetBrush(*wxTRANSPARENT_BRUSH);
227 dc.DrawRectangle(wxPoint(0,0), GetSize());
228 dc.SetPen(*wxRED_PEN);
229 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
230 #endif
231
232
233 if (!m_pButton || !m_pStatLine || !m_pPane)
234 return; // we need to complete the creation first!
235
236 LayoutChildren();
237
238 // this is very important to make the pane window layout show correctly
239 m_pPane->Layout();
240 }
241
242 #endif // wxUSE_BUTTON && wxUSE_STATLINE