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