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