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"
29 // ============================================================================
31 // ============================================================================
33 //-----------------------------------------------------------------------------
34 // "notify::expanded" signal
35 //-----------------------------------------------------------------------------
40 gtk_collapsiblepane_expanded_callback(GObject
* WXUNUSED(object
),
41 GParamSpec
* WXUNUSED(param_spec
),
44 // NB: unlike for the "activate" signal, when this callback is called, if
45 // we try to query the "collapsed" status through p->IsCollapsed(), we
46 // get the right value. I.e. here p->IsCollapsed() will return false if
47 // this callback has been called at the end of a collapsed->expanded
48 // transition and viceversa. Inside the "activate" signal callback
49 // p->IsCollapsed() would return the wrong value!
52 if ( p
->IsExpanded() )
54 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
55 // here as they would return the size for the collapsed expander
56 // even if the collapsed->expanded transition has already been
57 // completed; we solve this problem doing:
59 sz
= p
->m_szCollapsed
;
61 wxSize panesz
= p
->GetPane()->GetBestSize();
62 sz
.x
= wxMax(sz
.x
, panesz
.x
);
63 sz
.y
+= gtk_expander_get_spacing(GTK_EXPANDER(p
->m_widget
)) + panesz
.y
;
67 // same problem described above: using p->Get[Best|Min]Size() here we
68 // would get the size of the control when it is expanded even if the
69 // expanded->collapsed transition should be complete now...
70 // So, we use the size cached at control-creation time...
71 sz
= p
->m_szCollapsed
;
76 // p->OnStateChange(sz);
77 // here would work work BUT:
78 // 1) in the expanded->collapsed transition it provokes a lot of flickering
79 // 2) in the collapsed->expanded transition using the "Change status" wxButton
80 // in samples/collpane application some strange warnings would be generated
81 // by the "clearlooks" theme, if that's your theme.
83 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
84 // calculations to happen. Note that the following code has been very carefully designed
85 // and tested - be VERY careful when changing it!
87 // 1) need to update our size hints
88 // NB: this function call won't actually do any long operation
89 // (redraw/relayouting/resizing) so that it's flicker-free
92 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
95 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
96 p
->GetEventHandler()->ProcessEvent(ev
);
98 // the user asked to explicitely handle the resizing itself...
103 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
104 if ( top
&& top
->GetSizer() )
106 // 2) recalculate minimal size of the top window
107 sz
= top
->GetSizer()->CalcMin();
109 if (top
->m_mainWidget
)
111 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
112 // that this magic call is required to make it possible to shrink the
113 // top level window in the expanded->collapsed transition.
114 // This may be sometimes undesired but *is* necessary and if you look
115 // carefully, all GTK+ programs using GtkExpander perform this trick
116 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
117 // when the expander is collapsed!)
118 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
121 top
->SetSizeHints(sz
.x
, sz
.y
);
124 top
->SetClientSize(sz
);
128 if ( p
->m_bIgnoreNextChange
)
130 // change generated programmatically - do not send an event!
131 p
->m_bIgnoreNextChange
= false;
136 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
137 p
->GetEventHandler()->ProcessEvent(ev
);
142 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
144 // this callback should be used only once to insert the "pane" into the
145 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
146 // it has been called only once (and in any case we would get a warning
147 // from the following call as GtkExpander is a GtkBin and can contain only
149 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
156 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
158 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
159 EVT_SIZE(wxCollapsiblePane::OnSize
)
162 bool wxCollapsiblePane::Create(wxWindow
*parent
,
164 const wxString
& label
,
168 const wxValidator
& val
,
169 const wxString
& name
)
171 if (gtk_check_version(2,4,0))
172 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
173 pos
, size
, style
, val
, name
);
175 m_bIgnoreNextChange
= false;
177 if ( !PreCreation( parent
, pos
, size
) ||
178 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
180 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
185 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
187 // see the gtk_collapsiblepane_expanded_callback comments to understand why
188 // we connect to the "notify::expanded" signal instead of the more common
190 g_signal_connect(m_widget
, "notify::expanded",
191 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
193 // before creating m_pPane, we need to makesure our own insert callback
195 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
197 // this the real "pane"
198 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
199 wxTAB_TRAVERSAL
|wxNO_BORDER
);
201 gtk_widget_show(m_widget
);
202 m_parent
->DoAddChild( this );
206 // remember the size of this control when it's collapsed
207 m_szCollapsed
= GetBestSize();
212 wxSize
wxCollapsiblePane::DoGetBestSize() const
214 if (!gtk_check_version(2,4,0))
216 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
221 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
224 // notice that we do not cache our best size here as it changes
225 // all times the user expands/hide our pane
226 return wxSize(req
.width
, req
.height
);
229 return wxGenericCollapsiblePane::DoGetBestSize();
232 void wxCollapsiblePane::Collapse(bool collapse
)
234 if (!gtk_check_version(2,4,0))
237 if (IsCollapsed() == collapse
)
240 // do not send event in next signal handler call
241 m_bIgnoreNextChange
= true;
242 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
245 wxGenericCollapsiblePane::Collapse(collapse
);
248 bool wxCollapsiblePane::IsCollapsed() const
250 if (!gtk_check_version(2,4,0))
251 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
253 return wxGenericCollapsiblePane::IsCollapsed();
256 void wxCollapsiblePane::SetLabel(const wxString
&str
)
258 if (!gtk_check_version(2,4,0))
260 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
262 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
263 // we may get the size of the control with the pane size summed up if we are expanded!
264 //m_szCollapsed.x = GetBestSize().x;
267 wxGenericCollapsiblePane::SetLabel(str
);
270 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
272 #if 0 // for debug only
274 dc
.SetPen(*wxBLACK_PEN
);
275 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
276 dc
.DrawRectangle(wxPoint(0,0), GetSize());
277 dc
.SetPen(*wxRED_PEN
);
278 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
281 // here we need to resize the pane window otherwise, even if the GtkExpander container
282 // is expanded or shrinked, the pane window won't be updated!
283 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
285 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
286 // (even if SetAutoLayout(true) has been called on it!)
290 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)