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/toplevel.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/win_gtk.h"
30 #include <gtk/gtkexpander.h>
32 // ============================================================================
34 // ============================================================================
36 //-----------------------------------------------------------------------------
37 // "notify::expanded" signal
38 //-----------------------------------------------------------------------------
42 static void gtk_collapsiblepane_expanded_callback (GObject
*object
,
43 GParamSpec
*param_spec
,
46 // NB: unlike for the "activate" signal, when this callback is called, if
47 // we try to query the "collapsed" status through p->IsCollapsed(), we
48 // get the right value. I.e. here p->IsCollapsed() will return false if
49 // this callback has been called at the end of a collapsed->expanded
50 // transition and viceversa. Inside the "activate" signal callback
51 // p->IsCollapsed() would return the wrong value!
54 if ( p
->IsExpanded() )
56 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
57 // here as they would return the size for the collapsed expander
58 // even if the collapsed->expanded transition has already been
59 // completed; we solve this problem doing:
61 sz
= p
->m_szCollapsed
;
63 wxSize panesz
= p
->GetPane()->GetBestSize();
64 sz
.x
= wxMax(sz
.x
, panesz
.x
);
65 sz
.y
+= gtk_expander_get_spacing(GTK_EXPANDER(p
->m_widget
)) + panesz
.y
;
69 // same problem described above: using p->Get[Best|Min]Size() here we
70 // would get the size of the control when it is expanded even if the
71 // expanded->collapsed transition should be complete now...
72 // So, we use the size cached at control-creation time...
73 sz
= p
->m_szCollapsed
;
78 // p->OnStateChange(sz);
79 // here would work work BUT:
80 // 1) in the expanded->collapsed transition it provokes a lot of flickering
81 // 2) in the collapsed->expanded transition using the "Change status" wxButton
82 // in samples/collpane application some strange warnings would be generated
83 // by the "clearlooks" theme, if that's your theme.
85 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
86 // calculations to happen. Note that the following code has been very carefully designed
87 // and tested - be VERY careful when changing it!
89 // 1) need to update our size hints
90 // NB: this function call won't actually do any long operation
91 // (redraw/relayouting/resizing) so that it's flicker-free
94 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
97 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
98 p
->GetEventHandler()->ProcessEvent(ev
);
100 // the user asked to explicitely handle the resizing itself...
105 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
106 if ( top
&& top
->GetSizer() )
108 // 2) recalculate minimal size of the top window
109 wxSize sz
= top
->GetSizer()->CalcMin();
111 if (top
->m_mainWidget
)
113 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
114 // that this magic call is required to make it possible to shrink the
115 // top level window in the expanded->collapsed transition.
116 // This may be sometimes undesired but *is* necessary and if you look
117 // carefully, all GTK+ programs using GtkExpander perform this trick
118 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
119 // when the expander is collapsed!)
120 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
122 // 4) set size hints: note that this code has been taken and adapted
123 // from src/gtk/toplevel.cpp
126 geom
.min_width
= sz
.x
;
127 geom
.min_height
= sz
.y
;
129 gtk_window_set_geometry_hints( GTK_WINDOW(top
->m_widget
),
134 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
135 // to do the size changes immediately and not delaying them in the idle
138 top
->m_height
= sz
.y
;
140 int client_x
= top
->m_miniEdge
;
141 int client_y
= top
->m_miniEdge
+ top
->m_miniTitle
;
142 int client_w
= top
->m_width
- 2*top
->m_miniEdge
;
143 int client_h
= top
->m_height
- 2*top
->m_miniEdge
- top
->m_miniTitle
;
149 gtk_pizza_set_size( GTK_PIZZA(top
->m_mainWidget
),
151 client_x
, client_y
, client_w
, client_h
);
153 gtk_widget_set_size_request( top
->m_wxwindow
, sz
.x
, sz
.y
);
158 if ( p
->m_bIgnoreNextChange
)
160 // change generated programmatically - do not send an event!
161 p
->m_bIgnoreNextChange
= false;
166 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
167 p
->GetEventHandler()->ProcessEvent(ev
);
172 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
174 // this callback should be used only once to insert the "pane" into the
175 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
176 // it has been called only once (and in any case we would get a warning
177 // from the following call as GtkExpander is a GtkBin and can contain only
179 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
186 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
188 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
189 EVT_SIZE(wxCollapsiblePane::OnSize
)
192 bool wxCollapsiblePane::Create(wxWindow
*parent
,
194 const wxString
& label
,
198 const wxValidator
& val
,
199 const wxString
& name
)
201 if (gtk_check_version(2,4,0))
202 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
203 pos
, size
, style
, val
, name
);
205 m_bIgnoreNextChange
= false;
207 if ( !PreCreation( parent
, pos
, size
) ||
208 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
210 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
215 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
217 // see the gtk_collapsiblepane_expanded_callback comments to understand why
218 // we connect to the "notify::expanded" signal instead of the more common
220 g_signal_connect(m_widget
, "notify::expanded",
221 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
223 // before creating m_pPane, we need to makesure our own insert callback
225 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
227 // this the real "pane"
228 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
229 wxTAB_TRAVERSAL
|wxNO_BORDER
);
231 gtk_widget_show(m_widget
);
232 m_parent
->DoAddChild( this );
236 // remember the size of this control when it's collapsed
237 m_szCollapsed
= GetBestSize();
242 wxSize
wxCollapsiblePane::DoGetBestSize() const
244 if (!gtk_check_version(2,4,0))
246 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
251 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
254 // notice that we do not cache our best size here as it changes
255 // all times the user expands/hide our pane
256 return wxSize(req
.width
, req
.height
);
259 return wxGenericCollapsiblePane::DoGetBestSize();
262 void wxCollapsiblePane::Collapse(bool collapse
)
264 if (!gtk_check_version(2,4,0))
267 if (IsCollapsed() == collapse
)
270 // do not send event in next signal handler call
271 m_bIgnoreNextChange
= true;
272 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
275 wxGenericCollapsiblePane::Collapse(collapse
);
278 bool wxCollapsiblePane::IsCollapsed() const
280 if (!gtk_check_version(2,4,0))
281 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
283 return wxGenericCollapsiblePane::IsCollapsed();
286 void wxCollapsiblePane::SetLabel(const wxString
&str
)
288 if (!gtk_check_version(2,4,0))
290 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
292 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
293 // we may get the size of the control with the pane size summed up if we are expanded!
294 //m_szCollapsed.x = GetBestSize().x;
297 wxGenericCollapsiblePane::SetLabel(str
);
300 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
302 #if 0 // for debug only
304 dc
.SetPen(*wxBLACK_PEN
);
305 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
306 dc
.DrawRectangle(wxPoint(0,0), GetSize());
307 dc
.SetPen(*wxRED_PEN
);
308 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
311 // here we need to resize the pane window otherwise, even if the GtkExpander container
312 // is expanded or shrinked, the pane window won't be updated!
313 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
315 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
316 // (even if SetAutoLayout(true) has been called on it!)
320 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)