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