]> git.saurik.com Git - wxWidgets.git/blob - src/generic/collpaneg.cpp
Committing in .
[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 #include "wx/defs.h"
20
21 #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
22
23 #include "wx/collpane.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/toplevel.h"
27 #include "wx/button.h"
28 #include "wx/sizer.h"
29 #endif // !WX_PRECOMP
30
31 #include "wx/statline.h"
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 const wxChar wxGenericCollapsiblePaneNameStr[] = wxT("genericCollapsiblePane");
42
43 //-----------------------------------------------------------------------------
44 // wxGenericCollapsiblePane
45 //-----------------------------------------------------------------------------
46
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
48 IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
49 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
50
51 BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
52 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
53 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
54 END_EVENT_TABLE()
55
56
57 bool wxGenericCollapsiblePane::Create(wxWindow *parent,
58 wxWindowID id,
59 const wxString& label,
60 const wxPoint& pos,
61 const wxSize& size,
62 long style,
63 const wxValidator& val,
64 const wxString& name)
65 {
66 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
67 return false;
68
69 m_strLabel = label;
70
71 // create children and lay them out using a wxBoxSizer
72 // (so that we automatically get RTL features)
73 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
74 wxDefaultSize, wxBU_EXACTFIT);
75 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
76 #ifdef __WXMAC__
77 // on Mac we put the static libe above the button
78 m_sz = new wxBoxSizer(wxVERTICAL);
79 m_sz->Add(m_pStaticLine, 0, wxALL|wxGROW, GetBorder());
80 m_sz->Add(m_pButton, 0, wxLEFT|wxRIGHT|wxBOTTOM, GetBorder());
81 #else
82 // on other platforms we put the static line and the button horizontally
83 m_sz = new wxBoxSizer(wxHORIZONTAL);
84 m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder());
85 m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder());
86 #endif
87
88 // do not set sz as our sizers since we handle the pane window without using sizers
89 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
90 wxNO_BORDER);
91
92 // start as collapsed:
93 m_pPane->Hide();
94
95 return true;
96 }
97
98 wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
99 {
100 if (m_pButton && m_pStaticLine && m_sz)
101 {
102 m_pButton->SetContainingSizer(NULL);
103 m_pStaticLine->SetContainingSizer(NULL);
104
105 // our sizer is not deleted automatically since we didn't use SetSizer()!
106 wxDELETE(m_sz);
107 }
108 }
109
110 wxSize wxGenericCollapsiblePane::DoGetBestSize() const
111 {
112 // NB: do not use GetSize() but rather GetMinSize()
113 wxSize sz = m_sz->GetMinSize();
114
115 // when expanded, we need more vertical space
116 if ( IsExpanded() )
117 {
118 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
119 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
120 }
121
122 return sz;
123 }
124
125 wxString wxGenericCollapsiblePane::GetBtnLabel() const
126 {
127 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
128 }
129
130 void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
131 {
132 // minimal size has priority over the best size so set here our min size
133 SetMinSize(sz);
134 SetSize(sz);
135
136 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
137 {
138 // the user asked to explicitely handle the resizing itself...
139 return;
140 }
141
142
143 //
144 // NB: the following block of code has been accurately designed to
145 // as much flicker-free as possible; be careful when modifying it!
146 //
147
148 wxTopLevelWindow *
149 top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
150 if ( top )
151 {
152 // NB: don't Layout() the 'top' window as its size has not been correctly
153 // updated yet and we don't want to do an initial Layout() with the old
154 // size immediately followed by a SetClientSize/Fit call for the new
155 // size; that would provoke flickering!
156
157 if (top->GetSizer())
158 #ifdef __WXGTK__
159 // FIXME: the SetSizeHints() call would be required also for GTK+ for
160 // the expanded->collapsed transition. Unfortunately if we
161 // enable this line, then the GTK+ top window won't always be
162 // resized by the SetClientSize() call below! As a side effect
163 // of this dirty fix, the minimal size for the pane window is
164 // not set in GTK+ and the user can hide it shrinking the "top"
165 // window...
166 if (IsCollapsed())
167 #endif
168 top->GetSizer()->SetSizeHints(top);
169
170
171 // we shouldn't attempt to resize a maximized window, whatever happens
172 if ( !top->IsMaximized() )
173 {
174 if ( IsCollapsed() )
175 {
176 // expanded -> collapsed transition
177 if (top->GetSizer())
178 {
179 // we have just set the size hints...
180 wxSize sz = top->GetSizer()->CalcMin();
181
182 // use SetClientSize() and not SetSize() otherwise the size for
183 // e.g. a wxFrame with a menubar wouldn't be correctly set
184 top->SetClientSize(sz);
185 }
186 else
187 top->Layout();
188 }
189 else
190 {
191 // collapsed -> expanded transition
192
193 // force our parent to "fit", i.e. expand so that it can honour
194 // our minimal size
195 top->Fit();
196 }
197 }
198 }
199 }
200
201 void wxGenericCollapsiblePane::Collapse(bool collapse)
202 {
203 // optimization
204 if ( IsCollapsed() == collapse )
205 return;
206
207 // update our state
208 m_pPane->Show(!collapse);
209
210 // update button label
211 // NB: this must be done after updating our "state"
212 m_pButton->SetLabel(GetBtnLabel());
213
214 OnStateChange(GetBestSize());
215 }
216
217 void wxGenericCollapsiblePane::SetLabel(const wxString &label)
218 {
219 m_strLabel = label;
220 m_pButton->SetLabel(GetBtnLabel());
221 m_pButton->SetInitialSize();
222
223 Layout();
224 }
225
226 bool wxGenericCollapsiblePane::Layout()
227 {
228 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
229 return false; // we need to complete the creation first!
230
231 wxSize oursz(GetSize());
232
233 // move & resize the button and the static line
234 m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight());
235 m_sz->Layout();
236
237 if ( IsExpanded() )
238 {
239 // move & resize the container window
240 int yoffset = m_sz->GetSize().GetHeight() + GetBorder();
241 m_pPane->SetSize(0, yoffset,
242 oursz.x, oursz.y - yoffset);
243
244 // this is very important to make the pane window layout show correctly
245 m_pPane->Layout();
246 }
247
248 return true;
249 }
250
251 int wxGenericCollapsiblePane::GetBorder() const
252 {
253 #if defined( __WXMAC__ )
254 return 6;
255 #elif defined(__WXGTK20__)
256 return 3;
257 #elif defined(__WXMSW__)
258 wxASSERT(m_pButton);
259 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
260 #else
261 return 5;
262 #endif
263 }
264
265
266
267 //-----------------------------------------------------------------------------
268 // wxGenericCollapsiblePane - event handlers
269 //-----------------------------------------------------------------------------
270
271 void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
272 {
273 if ( event.GetEventObject() != m_pButton )
274 {
275 event.Skip();
276 return;
277 }
278
279 Collapse(!IsCollapsed());
280
281 // this change was generated by the user - send the event
282 wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed());
283 GetEventHandler()->ProcessEvent(ev);
284 }
285
286 void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event))
287 {
288 #if 0 // for debug only
289 wxClientDC dc(this);
290 dc.SetPen(*wxBLACK_PEN);
291 dc.SetBrush(*wxTRANSPARENT_BRUSH);
292 dc.DrawRectangle(wxPoint(0,0), GetSize());
293 dc.SetPen(*wxRED_PEN);
294 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
295 #endif
296
297 Layout();
298 }
299
300 #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE