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"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/win_gtk.h"
29 #include <gtk/gtkexpander.h>
31 const wxChar wxCollapsiblePaneNameStr
[] = wxT("CollapsiblePane");
33 // ============================================================================
35 // ============================================================================
37 //-----------------------------------------------------------------------------
38 // "notify::expanded" signal
39 //-----------------------------------------------------------------------------
43 static void gtk_collapsiblepane_expanded_callback (GObject
*object
,
44 GParamSpec
*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
))
97 // the user asked to explicitely handle the resizing itself...
102 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
103 if ( top
&& top
->GetSizer() )
105 // 2) recalculate minimal size of the top window
106 wxSize sz
= top
->GetSizer()->CalcMin();
108 if (top
->m_mainWidget
)
110 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
111 // that this magic call is required to make it possible to shrink the
112 // top level window in the expanded->collapsed transition.
113 // This may be sometimes undesired but *is* necessary and if you look
114 // carefully, all GTK+ programs using GtkExpander perform this trick
115 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
116 // when the expander is collapsed!)
117 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
119 // 4) set size hints: note that this code has been taken and adapted
120 // from src/gtk/toplevel.cpp
123 geom
.min_width
= sz
.x
;
124 geom
.min_height
= sz
.y
;
126 gtk_window_set_geometry_hints( GTK_WINDOW(top
->m_widget
),
131 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
132 // to do the size changes immediately and not delaying them in the idle
135 top
->m_height
= sz
.y
;
137 int client_x
= top
->m_miniEdge
;
138 int client_y
= top
->m_miniEdge
+ top
->m_miniTitle
;
139 int client_w
= top
->m_width
- 2*top
->m_miniEdge
;
140 int client_h
= top
->m_height
- 2*top
->m_miniEdge
- top
->m_miniTitle
;
146 gtk_pizza_set_size( GTK_PIZZA(top
->m_mainWidget
),
148 client_x
, client_y
, client_w
, client_h
);
150 gtk_widget_set_size_request( top
->m_wxwindow
, sz
.x
, sz
.y
);
155 if ( p
->m_bIgnoreNextChange
)
157 // change generated programmatically - do not send an event!
158 p
->m_bIgnoreNextChange
= false;
163 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
164 p
->GetEventHandler()->ProcessEvent(ev
);
169 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
171 // this callback should be used only once to insert the "pane" into the
172 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
173 // it has been called only once (and in any case we would get a warning
174 // from the following call as GtkExpander is a GtkBin and can contain only
176 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
185 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
186 EVT_SIZE(wxCollapsiblePane::OnSize
)
189 bool wxCollapsiblePane::Create(wxWindow
*parent
,
191 const wxString
& label
,
195 const wxValidator
& val
,
196 const wxString
& name
)
198 if (gtk_check_version(2,4,0))
199 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
200 pos
, size
, style
, val
, name
);
203 m_acceptsFocus
= true;
204 m_bIgnoreNextChange
= false;
206 if ( !PreCreation( parent
, pos
, size
) ||
207 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
209 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
214 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
216 // see the gtk_collapsiblepane_expanded_callback comments to understand why
217 // we connect to the "notify::expanded" signal instead of the more common
219 g_signal_connect(m_widget
, "notify::expanded",
220 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
222 // before creating m_pPane, we need to makesure our own insert callback
224 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
226 // this the real "pane"
227 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
230 gtk_widget_show( GTK_WIDGET(m_widget
) );
231 m_parent
->DoAddChild( this );
235 // remember the size of this control when it's collapsed
236 m_szCollapsed
= GetBestSize();
241 wxSize
wxCollapsiblePane::DoGetBestSize() const
243 if (!gtk_check_version(2,4,0))
245 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
250 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
253 // notice that we do not cache our best size here as it changes
254 // all times the user expands/hide our pane
255 return wxSize(req
.width
, req
.height
);
258 return wxGenericCollapsiblePane::DoGetBestSize();
261 void wxCollapsiblePane::Collapse(bool collapse
)
263 if (!gtk_check_version(2,4,0))
266 if (IsCollapsed() == collapse
)
269 // do not send event in next signal handler call
270 m_bIgnoreNextChange
= true;
271 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
274 wxGenericCollapsiblePane::Collapse(collapse
);
277 bool wxCollapsiblePane::IsCollapsed() const
279 if (!gtk_check_version(2,4,0))
280 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
282 return wxGenericCollapsiblePane::IsCollapsed();
285 void wxCollapsiblePane::SetLabel(const wxString
&str
)
287 if (!gtk_check_version(2,4,0))
289 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
291 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
292 // we may get the size of the control with the pane size summed up if we are expanded!
293 //m_szCollapsed.x = GetBestSize().x;
296 wxGenericCollapsiblePane::SetLabel(str
);
299 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
301 #if 0 // for debug only
303 dc
.SetPen(*wxBLACK_PEN
);
304 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
305 dc
.DrawRectangle(wxPoint(0,0), GetSize());
306 dc
.SetPen(*wxRED_PEN
);
307 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
310 // here we need to resize the pane window otherwise, even if the GtkExpander container
311 // is expanded or shrinked, the pane window won't be updated!
312 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
314 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
315 // (even if SetAutoLayout(true) has been called on it!)
319 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)