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