Commit [ 1592301 ] [wxgtk] Fix wxCollapsiblePane to be flicker-free
[wxWidgets.git] / src / gtk / collpane.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/collpane.cpp
3 // Purpose: wxCollapsiblePane
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 #if wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
21
22 #include "wx/collpane.h"
23 #include "wx/toplevel.h"
24 #include "wx/sizer.h"
25
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/win_gtk.h"
28
29 #include <gtk/gtkexpander.h>
30
31 const wxChar wxCollapsiblePaneNameStr[] = wxT("CollapsiblePane");
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 //-----------------------------------------------------------------------------
38 // "notify::expanded" signal
39 //-----------------------------------------------------------------------------
40
41 extern "C" {
42
43 static void gtk_collapsiblepane_expanded_callback (GObject *object,
44 GParamSpec *param_spec,
45 wxCollapsiblePane *p)
46 {
47 // NB: unlike for the "activate" signal, when this callback is called, if
48 // we try to query the "collapsed" status through p->IsCollapsed(), we
49 // get the right value. I.e. here p->IsCollapsed() will return false if
50 // this callback has been called at the end of a collapsed->expanded
51 // transition and viceversa. Inside the "activate" signal callback
52 // p->IsCollapsed() would return the wrong value!
53
54 wxSize sz;
55 if ( p->IsExpanded() )
56 {
57 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
58 // here as they would return the size for the collapsed expander
59 // even if the collapsed->expanded transition has already been
60 // completed; we solve this problem doing:
61
62 sz = p->m_szCollapsed;
63
64 wxSize panesz = p->GetPane()->GetBestSize();
65 sz.x = wxMax(sz.x, panesz.x);
66 sz.y += gtk_expander_get_spacing(GTK_EXPANDER(p->m_widget)) + panesz.y;
67 }
68 else // collapsed
69 {
70 // same problem described above: using p->Get[Best|Min]Size() here we
71 // would get the size of the control when it is expanded even if the
72 // expanded->collapsed transition should be complete now...
73 // So, we use the size cached at control-creation time...
74 sz = p->m_szCollapsed;
75 }
76
77 // VERY IMPORTANT:
78 // just calling
79 // p->OnStateChange(sz);
80 // here would work work BUT:
81 // 1) in the expanded->collapsed transition it provokes a lot of flickering
82 // 2) in the collapsed->expanded transition using the "Change status" wxButton
83 // in samples/collpane application some strange warnings would be generated
84 // by the "clearlooks" theme, if that's your theme.
85 //
86 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
87 // calculations to happen. Note that the following code has been very carefully designed
88 // and tested - be VERY careful when changing it!
89
90 // 1) need to update our size hints
91 // NB: this function call won't actually do any long operation
92 // (redraw/relayouting/resizing) so that it's flicker-free
93 p->SetMinSize(sz);
94
95 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
96 {
97 // the user asked to explicitely handle the resizing itself...
98 return;
99 }
100
101 wxTopLevelWindow *
102 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
103 if ( top && top->GetSizer() )
104 {
105 // 2) recalculate minimal size of the top window
106 wxSize sz = top->GetSizer()->CalcMin();
107
108 if (top->m_mainWidget)
109 {
110 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
111 // that this magic call is required to make it possible to shrink the
112 // top level window in the expanded->collapsed transition.
113 // This may be sometimes undesired but *is* necessary and if you look
114 // carefully, all GTK+ programs using GtkExpander perform this trick
115 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
116 // when the expander is collapsed!)
117 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
118
119 // 4) set size hints: note that this code has been taken and adapted
120 // from src/gtk/toplevel.cpp
121 GdkGeometry geom;
122
123 geom.min_width = sz.x;
124 geom.min_height = sz.y;
125
126 gtk_window_set_geometry_hints( GTK_WINDOW(top->m_widget),
127 (GtkWidget*) NULL,
128 &geom,
129 GDK_HINT_MIN_SIZE );
130
131 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
132 // to do the size changes immediately and not delaying them in the idle
133 // time
134 top->m_width = sz.x;
135 top->m_height = sz.y;
136
137 int client_x = top->m_miniEdge;
138 int client_y = top->m_miniEdge + top->m_miniTitle;
139 int client_w = top->m_width - 2*top->m_miniEdge;
140 int client_h = top->m_height - 2*top->m_miniEdge - top->m_miniTitle;
141 if (client_w < 0)
142 client_w = 0;
143 if (client_h < 0)
144 client_h = 0;
145
146 gtk_pizza_set_size( GTK_PIZZA(top->m_mainWidget),
147 top->m_wxwindow,
148 client_x, client_y, client_w, client_h );
149
150 gtk_widget_set_size_request( top->m_wxwindow, sz.x, sz.y );
151
152 }
153 }
154
155 if ( p->m_bIgnoreNextChange )
156 {
157 // change generated programmatically - do not send an event!
158 p->m_bIgnoreNextChange = false;
159 return;
160 }
161
162 // fire an event
163 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
164 p->GetEventHandler()->ProcessEvent(ev);
165 }
166 }
167
168 static void
169 gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
170 {
171 // this callback should be used only once to insert the "pane" into the
172 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
173 // it has been called only once (and in any case we would get a warning
174 // from the following call as GtkExpander is a GtkBin and can contain only
175 // a single child!).
176 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
177 }
178
179 //-----------------------------------------------------------------------------
180 // wxCollapsiblePane
181 //-----------------------------------------------------------------------------
182
183 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
184
185 BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
186 EVT_SIZE(wxCollapsiblePane::OnSize)
187 END_EVENT_TABLE()
188
189 bool wxCollapsiblePane::Create(wxWindow *parent,
190 wxWindowID id,
191 const wxString& label,
192 const wxPoint& pos,
193 const wxSize& size,
194 long style,
195 const wxValidator& val,
196 const wxString& name)
197 {
198 if (gtk_check_version(2,4,0))
199 return wxGenericCollapsiblePane::Create(parent, id, label,
200 pos, size, style, val, name);
201
202 m_needParent = true;
203 m_acceptsFocus = true;
204 m_bIgnoreNextChange = false;
205
206 if ( !PreCreation( parent, pos, size ) ||
207 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
208 {
209 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
210 return false;
211 }
212
213 m_widget =
214 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
215
216 // see the gtk_collapsiblepane_expanded_callback comments to understand why
217 // we connect to the "notify::expanded" signal instead of the more common
218 // "activate" one
219 g_signal_connect(m_widget, "notify::expanded",
220 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
221
222 // before creating m_pPane, we need to makesure our own insert callback
223 // will be used
224 m_insertCallback = gtk_collapsiblepane_insert_callback;
225
226 // this the real "pane"
227 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
228 wxNO_BORDER);
229
230 gtk_widget_show( GTK_WIDGET(m_widget) );
231 m_parent->DoAddChild( this );
232
233 PostCreation(size);
234
235 // remember the size of this control when it's collapsed
236 m_szCollapsed = GetBestSize();
237
238 return true;
239 }
240
241 wxSize wxCollapsiblePane::DoGetBestSize() const
242 {
243 if (!gtk_check_version(2,4,0))
244 {
245 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
246
247 GtkRequisition req;
248 req.width = 2;
249 req.height = 2;
250 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
251 (m_widget, &req );
252
253 // notice that we do not cache our best size here as it changes
254 // all times the user expands/hide our pane
255 return wxSize(req.width, req.height);
256 }
257
258 return wxGenericCollapsiblePane::DoGetBestSize();
259 }
260
261 void wxCollapsiblePane::Collapse(bool collapse)
262 {
263 if (!gtk_check_version(2,4,0))
264 {
265 // optimization
266 if (IsCollapsed() == collapse)
267 return;
268
269 // do not send event in next signal handler call
270 m_bIgnoreNextChange = true;
271 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
272 }
273 else
274 wxGenericCollapsiblePane::Collapse(collapse);
275 }
276
277 bool wxCollapsiblePane::IsCollapsed() const
278 {
279 if (!gtk_check_version(2,4,0))
280 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
281
282 return wxGenericCollapsiblePane::IsCollapsed();
283 }
284
285 void wxCollapsiblePane::SetLabel(const wxString &str)
286 {
287 if (!gtk_check_version(2,4,0))
288 {
289 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
290
291 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
292 // we may get the size of the control with the pane size summed up if we are expanded!
293 //m_szCollapsed.x = GetBestSize().x;
294 }
295 else
296 wxGenericCollapsiblePane::SetLabel(str);
297 }
298
299 void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
300 {
301 #if 0 // for debug only
302 wxClientDC dc(this);
303 dc.SetPen(*wxBLACK_PEN);
304 dc.SetBrush(*wxTRANSPARENT_BRUSH);
305 dc.DrawRectangle(wxPoint(0,0), GetSize());
306 dc.SetPen(*wxRED_PEN);
307 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
308 #endif
309
310 // here we need to resize the pane window otherwise, even if the GtkExpander container
311 // is expanded or shrinked, the pane window won't be updated!
312 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
313
314 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
315 // (even if SetAutoLayout(true) has been called on it!)
316 m_pPane->Layout();
317 }
318
319 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
320