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(__WXUNIVERSAL__)
22 #include "wx/collpane.h"
23 #include "wx/toplevel.h"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/gtk2-compat.h"
31 // the lines below duplicate the same definitions in collpaneg.cpp, if we have
32 // another implementation of this class we should extract them to a common file
34 const char wxCollapsiblePaneNameStr
[] = "collapsiblePane";
36 wxDEFINE_EVENT( wxEVT_COLLAPSIBLEPANE_CHANGED
, wxCollapsiblePaneEvent
);
38 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
40 // ============================================================================
42 // ============================================================================
44 //-----------------------------------------------------------------------------
45 // "notify::expanded" signal
46 //-----------------------------------------------------------------------------
51 gtk_collapsiblepane_expanded_callback(GObject
* WXUNUSED(object
),
52 GParamSpec
* WXUNUSED(param_spec
),
55 // NB: unlike for the "activate" signal, when this callback is called, if
56 // we try to query the "collapsed" status through p->IsCollapsed(), we
57 // get the right value. I.e. here p->IsCollapsed() will return false if
58 // this callback has been called at the end of a collapsed->expanded
59 // transition and viceversa. Inside the "activate" signal callback
60 // p->IsCollapsed() would return the wrong value!
63 if ( p
->IsExpanded() )
65 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
66 // here as they would return the size for the collapsed expander
67 // even if the collapsed->expanded transition has already been
68 // completed; we solve this problem doing:
70 sz
= p
->m_szCollapsed
;
72 wxSize panesz
= p
->GetPane()->GetBestSize();
73 sz
.x
= wxMax(sz
.x
, panesz
.x
);
74 sz
.y
+= gtk_expander_get_spacing(GTK_EXPANDER(p
->m_widget
)) + panesz
.y
;
78 // same problem described above: using p->Get[Best|Min]Size() here we
79 // would get the size of the control when it is expanded even if the
80 // expanded->collapsed transition should be complete now...
81 // So, we use the size cached at control-creation time...
82 sz
= p
->m_szCollapsed
;
87 // p->OnStateChange(sz);
88 // here would work work BUT:
89 // 1) in the expanded->collapsed transition it provokes a lot of flickering
90 // 2) in the collapsed->expanded transition using the "Change status" wxButton
91 // in samples/collpane application some strange warnings would be generated
92 // by the "clearlooks" theme, if that's your theme.
94 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
95 // calculations to happen. Note that the following code has been very carefully designed
96 // and tested - be VERY careful when changing it!
98 // 1) need to update our size hints
99 // NB: this function call won't actually do any long operation
100 // (redraw/relayout/resize) so that it's flicker-free
103 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
106 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
107 p
->HandleWindowEvent(ev
);
109 // the user asked to explicitly handle the resizing itself...
114 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
115 if ( top
&& top
->GetSizer() )
117 // 2) recalculate minimal size of the top window
118 sz
= top
->GetSizer()->CalcMin();
120 if (top
->m_mainWidget
)
122 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program
123 // you know that this magic call is required to make it possible
124 // to shrink the top level window in the expanded->collapsed
125 // transition. This may be sometimes undesired but *is*
126 // necessary and if you look carefully, all GTK+ programs using
127 // GtkExpander perform this trick (e.g. the standard "open file"
128 // dialog of GTK+>=2.4 is not resizable when the expander is
130 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
133 top
->SetMinClientSize(sz
);
136 top
->SetClientSize(sz
);
140 if ( p
->m_bIgnoreNextChange
)
142 // change generated programmatically - do not send an event!
143 p
->m_bIgnoreNextChange
= false;
148 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
149 p
->HandleWindowEvent(ev
);
153 void wxCollapsiblePane::AddChildGTK(wxWindowGTK
* child
)
155 // should be used only once to insert the "pane" into the
156 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
157 // it has been called only once (and in any case we would get a warning
158 // from the following call as GtkExpander is a GtkBin and can contain only
160 gtk_container_add(GTK_CONTAINER(m_widget
), child
->m_widget
);
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxControl
)
169 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxCollapsiblePaneBase
)
170 EVT_SIZE(wxCollapsiblePane::OnSize
)
173 bool wxCollapsiblePane::Create(wxWindow
*parent
,
175 const wxString
& label
,
179 const wxValidator
& val
,
180 const wxString
& name
)
182 m_bIgnoreNextChange
= false;
184 if ( !PreCreation( parent
, pos
, size
) ||
185 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
187 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
192 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
193 g_object_ref(m_widget
);
195 // see the gtk_collapsiblepane_expanded_callback comments to understand why
196 // we connect to the "notify::expanded" signal instead of the more common
198 g_signal_connect(m_widget
, "notify::expanded",
199 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
201 // this the real "pane"
202 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
203 wxTAB_TRAVERSAL
|wxNO_BORDER
, wxS("wxCollapsiblePanePane"));
205 m_parent
->DoAddChild( this );
209 // we should blend into our parent background
210 const wxColour bg
= parent
->GetBackgroundColour();
211 SetBackgroundColour(bg
);
212 m_pPane
->SetBackgroundColour(bg
);
214 // remember the size of this control when it's collapsed
215 m_szCollapsed
= GetBestSize();
220 wxSize
wxCollapsiblePane::DoGetBestSize() const
222 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
226 gtk_widget_get_preferred_size(m_widget
, NULL
, &req
);
228 GTK_WIDGET_GET_CLASS(m_widget
)->size_request(m_widget
, &req
);
231 // notice that we do not cache our best size here as it changes
232 // all times the user expands/hide our pane
233 return wxSize(req
.width
, req
.height
);
236 void wxCollapsiblePane::Collapse(bool collapse
)
239 if (IsCollapsed() == collapse
)
242 // do not send event in next signal handler call
243 m_bIgnoreNextChange
= true;
244 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
247 bool wxCollapsiblePane::IsCollapsed() const
249 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
252 void wxCollapsiblePane::SetLabel(const wxString
&str
)
254 gtk_expander_set_label(GTK_EXPANDER(m_widget
),
255 wxGTK_CONV(GTKConvertMnemonics(str
)));
257 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
258 // we may get the size of the control with the pane size summed up if we are expanded!
259 //m_szCollapsed.x = GetBestSize().x;
262 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
264 #if 0 // for debug only
266 dc
.SetPen(*wxBLACK_PEN
);
267 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
268 dc
.DrawRectangle(wxPoint(0,0), GetSize());
269 dc
.SetPen(*wxRED_PEN
);
270 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
273 // here we need to resize the pane window otherwise, even if the GtkExpander container
274 // is expanded or shrunk, the pane window won't be updated!
275 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
277 // we need to explicitly call m_pPane->Layout() or else it won't correctly relayout
278 // (even if SetAutoLayout(true) has been called on it!)
283 GdkWindow
*wxCollapsiblePane::GTKGetWindow(wxArrayGdkWindows
& windows
) const
285 GtkWidget
*label
= gtk_expander_get_label_widget( GTK_EXPANDER(m_widget
) );
286 windows
.Add(gtk_widget_get_window(label
));
287 windows
.Add(gtk_widget_get_window(m_widget
));
292 #endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)