1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/collpane.cpp
3 // Purpose: wxCollapsiblePane
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
20 #if wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
22 #include "wx/collpane.h"
23 #include "wx/gtk/private.h"
24 #include "wx/gtk/win_gtk.h"
26 #include <gtk/gtkexpander.h>
28 const wxChar wxCollapsiblePaneNameStr
[] = wxT("CollapsiblePane");
30 // ============================================================================
32 // ============================================================================
34 //-----------------------------------------------------------------------------
35 // "notify::expanded" signal
36 //-----------------------------------------------------------------------------
40 static void gtk_collapsiblepane_expanded_callback (GObject
*object
,
41 GParamSpec
*param_spec
,
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!
52 if ( p
->IsExpanded() )
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:
59 sz
= p
->m_szCollapsed
;
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
;
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
;
75 // this does work but in the expanded->collapsed transition it provokes
76 // a lot of flicker!!!
78 // It also has the problem that in the collapsed->expanded transition with the
79 // "clearlooks" GTK theme I get:
81 // ** (collpane:18928): CRITICAL **: clearlooks_style_draw_focus: assertion `height >= -1' failed
82 // ** (collpane:18928): CRITICAL **: clearlooks_style_draw_focus: assertion `height >= -1' failed
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
90 #else // flicker-free code
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
98 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
100 // the user asked to explicitely handle the resizing itself...
105 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
106 if ( top
&& top
->GetSizer() )
108 // recalculate minimal size of the top window
109 wxSize sz
= top
->GetSizer()->CalcMin();
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
118 if (top
->m_mainWidget
)
120 wxLogDebug(wxT("setting min size to %d;%d"), sz
.x
, sz
.y
);
125 geom
.min_width
= sz
.x
;
126 geom
.min_height
= sz
.y
;
128 gtk_window_set_geometry_hints( GTK_WINDOW(top
->m_widget
),
132 //gtk_window_set_default_size( GTK_WINDOW(top->m_widget), sz.x, sz.y );
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
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. */
142 top
->m_height
= sz
.y
;
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
;
153 // Let the parent perform the resize
154 gtk_pizza_set_size( GTK_PIZZA(top
->m_mainWidget
),
156 client_x
, client_y
, client_w
, client_h
);
158 gtk_widget_set_size_request( top
->m_wxwindow
, sz
.x
, sz
.y
);
163 if ( p
->m_bIgnoreNextChange
)
165 // change generated programmatically - do not send an event!
166 p
->m_bIgnoreNextChange
= false;
171 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
172 p
->GetEventHandler()->ProcessEvent(ev
);
177 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
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
184 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
193 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
194 EVT_SIZE(wxCollapsiblePane::OnSize
)
197 bool wxCollapsiblePane::Create(wxWindow
*parent
,
199 const wxString
& label
,
203 const wxValidator
& val
,
204 const wxString
& name
)
206 if (gtk_check_version(2,4,0))
207 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
208 pos
, size
, style
, val
, name
);
211 m_acceptsFocus
= true;
212 m_bIgnoreNextChange
= false;
214 if ( !PreCreation( parent
, pos
, size
) ||
215 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
217 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
222 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
224 // see the gtk_collapsiblepane_expanded_callback comments to understand why
225 // we connect to the "notify::expanded" signal instead of the more common
227 g_signal_connect(m_widget
, "notify::expanded",
228 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
230 // before creating m_pPane, we need to makesure our own insert callback
232 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
234 // this the real "pane"
235 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
238 gtk_widget_show( GTK_WIDGET(m_widget
) );
239 m_parent
->DoAddChild( this );
243 // remember the size of this control when it's collapsed
244 m_szCollapsed
= GetBestSize();
249 wxSize
wxCollapsiblePane::DoGetBestSize() const
251 if (!gtk_check_version(2,4,0))
253 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
258 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
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
);
266 return wxGenericCollapsiblePane::DoGetBestSize();
269 void wxCollapsiblePane::Collapse(bool collapse
)
271 if (!gtk_check_version(2,4,0))
274 if (IsCollapsed() == collapse
)
277 // do not send event in next signal handler call
278 m_bIgnoreNextChange
= true;
279 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
282 wxGenericCollapsiblePane::Collapse(collapse
);
285 bool wxCollapsiblePane::IsCollapsed() const
287 if (!gtk_check_version(2,4,0))
288 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
290 return wxGenericCollapsiblePane::IsCollapsed();
293 void wxCollapsiblePane::SetLabel(const wxString
&str
)
295 if (!gtk_check_version(2,4,0))
297 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
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;
304 wxGenericCollapsiblePane::SetLabel(str
);
307 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
309 #if 0 // for debug only
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());
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
);
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!)
327 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)