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