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"
22 #include "wx/collpane.h"
23 #include "wx/gtk/private.h"
25 #include <gtk/gtkexpander.h>
27 const wxChar wxCollapsiblePaneNameStr
[] = wxT("CollapsiblePane");
29 // ============================================================================
31 // ============================================================================
33 //-----------------------------------------------------------------------------
34 // "notify::expanded" signal
35 //-----------------------------------------------------------------------------
39 static void gtk_collapsiblepane_expanded_callback (GObject
*object
,
40 GParamSpec
*param_spec
,
43 // NB: unlike for the "activate" signal, when this callback is called, if
44 // we try to query the "collapsed" status through p->IsCollapsed(), we
45 // get the right value. I.e. here p->IsCollapsed() will return false if
46 // this callback has been called at the end of a collapsed->expanded
47 // transition and viceversa. Inside the "activate" signal callback
48 // p->IsCollapsed() would return the wrong value!
51 if ( p
->IsExpanded() )
53 // unfortunately there's no clean way to retrieve the minimal size of
54 // the expanded pane in this handler or in other handlers for the
55 // signals generated by user clicks on the GtkExpander button:
56 // p->GetBestSize() or p->GetMinSize() would still return the size for
57 // the collapsed expander even if the collapsed->expanded transition
58 // has already been completed (this because GTK+ queues some resize
59 // calls which still must be processed). So, the only solution to
60 // correctly set the size hints for this window is to calculate the
61 // expanded size ourselves, without relying on p->Get[Best|Min]Size:
63 sz
.SetWidth(wxMax(sz
.x
, p
->GetPane()->GetMinSize().x
));
64 sz
.SetHeight(sz
.y
+ p
->GetPane()->GetMinSize().y
+ 10);
68 // same problem described above: using p->Get[Best|Min]Size() here we
69 // would get the size of the control when it is expanded even if the
70 // expanded->collapsed transition should be complete now...
71 // So, we use the size cached at control-creation time...
72 sz
= p
->m_szCollapsed
;
77 if ( p
->m_bIgnoreNextChange
)
79 // change generated programmatically - do not send an event!
80 p
->m_bIgnoreNextChange
= false;
85 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
86 p
->GetEventHandler()->ProcessEvent(ev
);
91 gtk_collapsiblepane_insert_callback(wxWindowGTK
* parent
, wxWindowGTK
* child
)
93 // this callback should be used only once to insert the "pane" into the
94 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
95 // it has been called only once (and in any case we would get a warning
96 // from the following call as GtkExpander is a GtkBin and can contain only
98 gtk_container_add (GTK_CONTAINER (parent
->m_widget
), child
->m_widget
);
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxGenericCollapsiblePane
)
107 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxGenericCollapsiblePane
)
108 EVT_SIZE(wxCollapsiblePane::OnSize
)
111 bool wxCollapsiblePane::Create(wxWindow
*parent
,
113 const wxString
& label
,
117 const wxValidator
& val
,
118 const wxString
& name
)
120 if (gtk_check_version(2,4,0))
121 return wxGenericCollapsiblePane::Create(parent
, id
, label
,
122 pos
, size
, style
, val
, name
);
125 m_acceptsFocus
= true;
126 m_bIgnoreNextChange
= false;
128 if ( !PreCreation( parent
, pos
, size
) ||
129 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
131 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
136 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
138 // see the gtk_collapsiblepane_expanded_callback comments to understand why
139 // we connect to the "notify::expanded" signal instead of the more common
141 g_signal_connect(m_widget
, "notify::expanded",
142 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
144 // before creating m_pPane, we need to makesure our own insert callback
146 m_insertCallback
= gtk_collapsiblepane_insert_callback
;
148 // this the real "pane"
149 m_pPane
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
152 gtk_widget_show( GTK_WIDGET(m_widget
) );
153 m_parent
->DoAddChild( this );
157 // remember the size of this control when it's collapsed
158 m_szCollapsed
= GetBestSize();
163 wxSize
wxCollapsiblePane::DoGetBestSize() const
165 if (!gtk_check_version(2,4,0))
167 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
172 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
175 // notice that we do not cache our best size here as it changes
176 return wxSize(req
.width
, req
.height
);
179 return wxGenericCollapsiblePane::DoGetBestSize();
182 void wxCollapsiblePane::Collapse(bool collapse
)
184 if (!gtk_check_version(2,4,0))
187 if (IsCollapsed() == collapse
)
190 // do not send event in next signal handler call
191 m_bIgnoreNextChange
= true;
192 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
195 wxGenericCollapsiblePane::Collapse(collapse
);
198 bool wxCollapsiblePane::IsCollapsed() const
200 if (!gtk_check_version(2,4,0))
201 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
203 return wxGenericCollapsiblePane::IsCollapsed();
206 void wxCollapsiblePane::SetLabel(const wxString
&str
)
208 if (!gtk_check_version(2,4,0))
209 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
211 wxGenericCollapsiblePane::SetLabel(str
);
214 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
216 #if 0 // for debug only
218 dc
.SetPen(*wxBLACK_PEN
);
219 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
220 dc
.DrawRectangle(wxPoint(0,0), GetSize());
221 dc
.SetPen(*wxRED_PEN
);
222 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
225 // here we need to resize the pane window otherwise, even if the GtkExpander container
226 // is expanded or shrinked, the pane window won't be updated!
227 m_pPane
->SetSize(ev
.GetSize());
229 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
230 // (even if SetAutoLayout(true) has been called on it!)
234 #endif // __WXGTK24__