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
))
96 // the user asked to explicitely handle the resizing itself...
101 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
102 if ( top
&& top
->GetSizer() )
104 // 2) recalculate minimal size of the top window
105 wxSize sz
= top
->GetSizer()->CalcMin();
107 if (top
->m_mainWidget
)
109 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
110 // that this magic call is required to make it possible to shrink the
111 // top level window in the expanded->collapsed transition.
112 // This may be sometimes undesired but *is* necessary and if you look
113 // carefully, all GTK+ programs using GtkExpander perform this trick
114 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
115 // when the expander is collapsed!)
116 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
118 // 4) set size hints: note that this code has been taken and adapted
119 // from src/gtk/toplevel.cpp
122 geom
.min_width
= sz
.x
;
123 geom
.min_height
= sz
.y
;
125 gtk_window_set_geometry_hints( GTK_WINDOW(top
->m_widget
),
130 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
131 // to do the size changes immediately and not delaying them in the idle
134 top
->m_height
= sz
.y
;
136 int client_x
= top
->m_miniEdge
;
137 int client_y
= top
->m_miniEdge
+ top
->m_miniTitle
;
138 int client_w
= top
->m_width
- 2*top
->m_miniEdge
;
139 int client_h
= top
->m_height
- 2*top
->m_miniEdge
- top
->m_miniTitle
;
145 gtk_pizza_set_size( GTK_PIZZA(top
->m_mainWidget
),
147 client_x
, client_y
, client_w
, client_h
);
149 gtk_widget_set_size_request( top
->m_wxwindow
, sz
.x
, sz
.y
);
154 if ( p
->m_bIgnoreNextChange
)
156 // change generated programmatically - do not send an event!
157 p
->m_bIgnoreNextChange
= false;
162 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
163 p
->GetEventHandler()->ProcessEvent(ev
);
168 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
170 // this callback should be used only once to insert the "pane" into the
171 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
172 // it has been called only once (and in any case we would get a warning
173 // from the following call as GtkExpander is a GtkBin and can contain only
175 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
182 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
184 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
185 EVT_SIZE(wxCollapsiblePane::OnSize
)
188 bool wxCollapsiblePane::Create(wxWindow
*parent
,
190 const wxString
& label
,
194 const wxValidator
& val
,
195 const wxString
& name
)
197 if (gtk_check_version(2,4,0))
198 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
199 pos
, size
, style
, val
, name
);
202 m_acceptsFocus
= true;
203 m_bIgnoreNextChange
= false;
205 if ( !PreCreation( parent
, pos
, size
) ||
206 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
208 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
213 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
215 // see the gtk_collapsiblepane_expanded_callback comments to understand why
216 // we connect to the "notify::expanded" signal instead of the more common
218 g_signal_connect(m_widget
, "notify::expanded",
219 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
221 // before creating m_pPane, we need to makesure our own insert callback
223 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
225 // this the real "pane"
226 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
227 wxTAB_TRAVERSAL
|wxNO_BORDER
);
229 gtk_widget_show( GTK_WIDGET(m_widget
) );
230 m_parent
->DoAddChild( this );
234 // remember the size of this control when it's collapsed
235 m_szCollapsed
= GetBestSize();
240 wxSize
wxCollapsiblePane::DoGetBestSize() const
242 if (!gtk_check_version(2,4,0))
244 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
249 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
252 // notice that we do not cache our best size here as it changes
253 // all times the user expands/hide our pane
254 return wxSize(req
.width
, req
.height
);
257 return wxGenericCollapsiblePane::DoGetBestSize();
260 void wxCollapsiblePane::Collapse(bool collapse
)
262 if (!gtk_check_version(2,4,0))
265 if (IsCollapsed() == collapse
)
268 // do not send event in next signal handler call
269 m_bIgnoreNextChange
= true;
270 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
273 wxGenericCollapsiblePane::Collapse(collapse
);
276 bool wxCollapsiblePane::IsCollapsed() const
278 if (!gtk_check_version(2,4,0))
279 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
281 return wxGenericCollapsiblePane::IsCollapsed();
284 void wxCollapsiblePane::SetLabel(const wxString
&str
)
286 if (!gtk_check_version(2,4,0))
288 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
290 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
291 // we may get the size of the control with the pane size summed up if we are expanded!
292 //m_szCollapsed.x = GetBestSize().x;
295 wxGenericCollapsiblePane::SetLabel(str
);
298 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
300 #if 0 // for debug only
302 dc
.SetPen(*wxBLACK_PEN
);
303 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
304 dc
.DrawRectangle(wxPoint(0,0), GetSize());
305 dc
.SetPen(*wxRED_PEN
);
306 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
309 // here we need to resize the pane window otherwise, even if the GtkExpander container
310 // is expanded or shrinked, the pane window won't be updated!
311 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
313 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
314 // (even if SetAutoLayout(true) has been called on it!)
318 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)