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 //-----------------------------------------------------------------------------
43 gtk_collapsiblepane_expanded_callback(GObject
* WXUNUSED(object
),
44 GParamSpec
* WXUNUSED(param_spec
),
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!
55 if ( p
->IsExpanded() )
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:
62 sz
= p
->m_szCollapsed
;
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
;
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
;
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.
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!
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
95 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
98 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
99 p
->GetEventHandler()->ProcessEvent(ev
);
101 // the user asked to explicitely handle the resizing itself...
106 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
107 if ( top
&& top
->GetSizer() )
109 // 2) recalculate minimal size of the top window
110 sz
= top
->GetSizer()->CalcMin();
112 if (top
->m_mainWidget
)
114 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
115 // that this magic call is required to make it possible to shrink the
116 // top level window in the expanded->collapsed transition.
117 // This may be sometimes undesired but *is* necessary and if you look
118 // carefully, all GTK+ programs using GtkExpander perform this trick
119 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
120 // when the expander is collapsed!)
121 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
123 // 4) set size hints: note that this code has been taken and adapted
124 // from src/gtk/toplevel.cpp
127 geom
.min_width
= sz
.x
;
128 geom
.min_height
= sz
.y
;
130 gtk_window_set_geometry_hints( GTK_WINDOW(top
->m_widget
),
135 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
136 // to do the size changes immediately and not delaying them in the idle
139 top
->m_height
= sz
.y
;
141 int client_x
= top
->m_miniEdge
;
142 int client_y
= top
->m_miniEdge
+ top
->m_miniTitle
;
143 int client_w
= top
->m_width
- 2*top
->m_miniEdge
;
144 int client_h
= top
->m_height
- 2*top
->m_miniEdge
- top
->m_miniTitle
;
150 gtk_pizza_set_size( GTK_PIZZA(top
->m_mainWidget
),
152 client_x
, client_y
, client_w
, client_h
);
154 gtk_widget_set_size_request( top
->m_wxwindow
, sz
.x
, sz
.y
);
159 if ( p
->m_bIgnoreNextChange
)
161 // change generated programmatically - do not send an event!
162 p
->m_bIgnoreNextChange
= false;
167 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
168 p
->GetEventHandler()->ProcessEvent(ev
);
173 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
175 // this callback should be used only once to insert the "pane" into the
176 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
177 // it has been called only once (and in any case we would get a warning
178 // from the following call as GtkExpander is a GtkBin and can contain only
180 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
189 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
190 EVT_SIZE(wxCollapsiblePane::OnSize
)
193 bool wxCollapsiblePane::Create(wxWindow
*parent
,
195 const wxString
& label
,
199 const wxValidator
& val
,
200 const wxString
& name
)
202 if (gtk_check_version(2,4,0))
203 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
204 pos
, size
, style
, val
, name
);
206 m_bIgnoreNextChange
= false;
208 if ( !PreCreation( parent
, pos
, size
) ||
209 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
211 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
216 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
218 // see the gtk_collapsiblepane_expanded_callback comments to understand why
219 // we connect to the "notify::expanded" signal instead of the more common
221 g_signal_connect(m_widget
, "notify::expanded",
222 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
224 // before creating m_pPane, we need to makesure our own insert callback
226 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
228 // this the real "pane"
229 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
230 wxTAB_TRAVERSAL
|wxNO_BORDER
);
232 gtk_widget_show(m_widget
);
233 m_parent
->DoAddChild( this );
237 // remember the size of this control when it's collapsed
238 m_szCollapsed
= GetBestSize();
243 wxSize
wxCollapsiblePane::DoGetBestSize() const
245 if (!gtk_check_version(2,4,0))
247 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
252 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
255 // notice that we do not cache our best size here as it changes
256 // all times the user expands/hide our pane
257 return wxSize(req
.width
, req
.height
);
260 return wxGenericCollapsiblePane::DoGetBestSize();
263 void wxCollapsiblePane::Collapse(bool collapse
)
265 if (!gtk_check_version(2,4,0))
268 if (IsCollapsed() == collapse
)
271 // do not send event in next signal handler call
272 m_bIgnoreNextChange
= true;
273 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
276 wxGenericCollapsiblePane::Collapse(collapse
);
279 bool wxCollapsiblePane::IsCollapsed() const
281 if (!gtk_check_version(2,4,0))
282 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
284 return wxGenericCollapsiblePane::IsCollapsed();
287 void wxCollapsiblePane::SetLabel(const wxString
&str
)
289 if (!gtk_check_version(2,4,0))
291 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
293 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
294 // we may get the size of the control with the pane size summed up if we are expanded!
295 //m_szCollapsed.x = GetBestSize().x;
298 wxGenericCollapsiblePane::SetLabel(str
);
301 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
303 #if 0 // for debug only
305 dc
.SetPen(*wxBLACK_PEN
);
306 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
307 dc
.DrawRectangle(wxPoint(0,0), GetSize());
308 dc
.SetPen(*wxRED_PEN
);
309 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
312 // here we need to resize the pane window otherwise, even if the GtkExpander container
313 // is expanded or shrinked, the pane window won't be updated!
314 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
316 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
317 // (even if SetAutoLayout(true) has been called on it!)
321 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)