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