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