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"
27 #include "wx/gtk/private.h"
29 // the lines below duplicate the same definitions in collpaneg.cpp, if we have
30 // another implementation of this class we should extract them to a common file
32 const wxChar wxCollapsiblePaneNameStr
[] = wxT("collapsiblePane");
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED
)
36 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent
, wxCommandEvent
)
38 // ============================================================================
40 // ============================================================================
42 //-----------------------------------------------------------------------------
43 // "notify::expanded" signal
44 //-----------------------------------------------------------------------------
49 gtk_collapsiblepane_expanded_callback(GObject
* WXUNUSED(object
),
50 GParamSpec
* WXUNUSED(param_spec
),
53 // NB: unlike for the "activate" signal, when this callback is called, if
54 // we try to query the "collapsed" status through p->IsCollapsed(), we
55 // get the right value. I.e. here p->IsCollapsed() will return false if
56 // this callback has been called at the end of a collapsed->expanded
57 // transition and viceversa. Inside the "activate" signal callback
58 // p->IsCollapsed() would return the wrong value!
61 if ( p
->IsExpanded() )
63 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
64 // here as they would return the size for the collapsed expander
65 // even if the collapsed->expanded transition has already been
66 // completed; we solve this problem doing:
68 sz
= p
->m_szCollapsed
;
70 wxSize panesz
= p
->GetPane()->GetBestSize();
71 sz
.x
= wxMax(sz
.x
, panesz
.x
);
72 sz
.y
+= gtk_expander_get_spacing(GTK_EXPANDER(p
->m_widget
)) + panesz
.y
;
76 // same problem described above: using p->Get[Best|Min]Size() here we
77 // would get the size of the control when it is expanded even if the
78 // expanded->collapsed transition should be complete now...
79 // So, we use the size cached at control-creation time...
80 sz
= p
->m_szCollapsed
;
85 // p->OnStateChange(sz);
86 // here would work work BUT:
87 // 1) in the expanded->collapsed transition it provokes a lot of flickering
88 // 2) in the collapsed->expanded transition using the "Change status" wxButton
89 // in samples/collpane application some strange warnings would be generated
90 // by the "clearlooks" theme, if that's your theme.
92 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
93 // calculations to happen. Note that the following code has been very carefully designed
94 // and tested - be VERY careful when changing it!
96 // 1) need to update our size hints
97 // NB: this function call won't actually do any long operation
98 // (redraw/relayouting/resizing) so that it's flicker-free
101 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
104 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
105 p
->HandleWindowEvent(ev
);
107 // the user asked to explicitely handle the resizing itself...
112 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
113 if ( top
&& top
->GetSizer() )
115 // 2) recalculate minimal size of the top window
116 sz
= top
->GetSizer()->CalcMin();
118 if (top
->m_mainWidget
)
120 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program
121 // you know that this magic call is required to make it possible
122 // to shrink the top level window in the expanded->collapsed
123 // transition. This may be sometimes undesired but *is*
124 // necessary and if you look carefully, all GTK+ programs using
125 // GtkExpander perform this trick (e.g. the standard "open file"
126 // dialog of GTK+>=2.4 is not resizeable when the expander is
128 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
131 top
->SetMinClientSize(sz
);
134 top
->SetClientSize(sz
);
138 if ( p
->m_bIgnoreNextChange
)
140 // change generated programmatically - do not send an event!
141 p
->m_bIgnoreNextChange
= false;
146 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
147 p
->HandleWindowEvent(ev
);
152 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
154 // this callback should be used only once to insert the "pane" into the
155 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
156 // it has been called only once (and in any case we would get a warning
157 // from the following call as GtkExpander is a GtkBin and can contain only
159 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxControl
)
168 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxCollapsiblePaneBase
)
169 EVT_SIZE(wxCollapsiblePane::OnSize
)
172 bool wxCollapsiblePane::Create(wxWindow
*parent
,
174 const wxString
& label
,
178 const wxValidator
& val
,
179 const wxString
& name
)
181 m_bIgnoreNextChange
= false;
183 if ( !PreCreation( parent
, pos
, size
) ||
184 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
186 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
191 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
193 // see the gtk_collapsiblepane_expanded_callback comments to understand why
194 // we connect to the "notify::expanded" signal instead of the more common
196 g_signal_connect(m_widget
, "notify::expanded",
197 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
199 // before creating m_pPane, we need to makesure our own insert callback
201 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
203 // this the real "pane"
204 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
205 wxTAB_TRAVERSAL
|wxNO_BORDER
);
207 gtk_widget_show(m_widget
);
208 m_parent
->DoAddChild( this );
212 // remember the size of this control when it's collapsed
213 m_szCollapsed
= GetBestSize();
218 wxSize
wxCollapsiblePane::DoGetBestSize() const
220 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
225 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
228 // notice that we do not cache our best size here as it changes
229 // all times the user expands/hide our pane
230 return wxSize(req
.width
, req
.height
);
233 void wxCollapsiblePane::Collapse(bool collapse
)
236 if (IsCollapsed() == collapse
)
239 // do not send event in next signal handler call
240 m_bIgnoreNextChange
= true;
241 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
244 bool wxCollapsiblePane::IsCollapsed() const
246 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
249 void wxCollapsiblePane::SetLabel(const wxString
&str
)
251 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
253 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
254 // we may get the size of the control with the pane size summed up if we are expanded!
255 //m_szCollapsed.x = GetBestSize().x;
258 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
260 #if 0 // for debug only
262 dc
.SetPen(*wxBLACK_PEN
);
263 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
264 dc
.DrawRectangle(wxPoint(0,0), GetSize());
265 dc
.SetPen(*wxRED_PEN
);
266 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
269 // here we need to resize the pane window otherwise, even if the GtkExpander container
270 // is expanded or shrinked, the pane window won't be updated!
271 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
273 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
274 // (even if SetAutoLayout(true) has been called on it!)
278 #endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)