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