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_COMMAND_COLLPANE_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_width(m_widget
, NULL
, &req
.width
);
227 gtk_widget_get_preferred_height_for_width(m_widget
, req
.width
, NULL
, &req
.height
);
229 GTK_WIDGET_GET_CLASS(m_widget
)->size_request(m_widget
, &req
);
232 // notice that we do not cache our best size here as it changes
233 // all times the user expands/hide our pane
234 return wxSize(req
.width
, req
.height
);
237 void wxCollapsiblePane::Collapse(bool collapse
)
240 if (IsCollapsed() == collapse
)
243 // do not send event in next signal handler call
244 m_bIgnoreNextChange
= true;
245 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
248 bool wxCollapsiblePane::IsCollapsed() const
250 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
253 void wxCollapsiblePane::SetLabel(const wxString
&str
)
255 gtk_expander_set_label(GTK_EXPANDER(m_widget
),
256 wxGTK_CONV(GTKConvertMnemonics(str
)));
258 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
259 // we may get the size of the control with the pane size summed up if we are expanded!
260 //m_szCollapsed.x = GetBestSize().x;
263 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
265 #if 0 // for debug only
267 dc
.SetPen(*wxBLACK_PEN
);
268 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
269 dc
.DrawRectangle(wxPoint(0,0), GetSize());
270 dc
.SetPen(*wxRED_PEN
);
271 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
274 // here we need to resize the pane window otherwise, even if the GtkExpander container
275 // is expanded or shrunk, the pane window won't be updated!
276 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
278 // we need to explicitly call m_pPane->Layout() or else it won't correctly relayout
279 // (even if SetAutoLayout(true) has been called on it!)
284 GdkWindow
*wxCollapsiblePane::GTKGetWindow(wxArrayGdkWindows
& windows
) const
286 GtkWidget
*label
= gtk_expander_get_label_widget( GTK_EXPANDER(m_widget
) );
287 windows
.Add(gtk_widget_get_window(label
));
288 windows
.Add(gtk_widget_get_window(m_widget
));
293 #endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)