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 char wxCollapsiblePaneNameStr
[] = "collapsiblePane";
34 wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED
, wxCollapsiblePaneEvent
);
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 if (!p
->IsCollapsed())
55 if (p
->GetPane()->GetSizer())
56 p
->GetPane()->GetSizer()->Fit( p
->GetPane() );
63 gtk_collpane_map_unmap_callback( GtkWidget
*WXUNUSED(pane
), GdkEvent
*WXUNUSED(event
), wxCollapsiblePane
* p
)
65 if (p
->HasFlag(wxCP_NO_TLW_RESIZE
))
68 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
69 p
->HandleWindowEvent(ev
);
71 // the user asked to explicitly handle the resizing itself...
76 top
= wxDynamicCast(wxGetTopLevelParent(p
), wxTopLevelWindow
);
77 if ( top
&& top
->GetSizer() )
79 // 2) recalculate minimal size of the top window
80 wxSize sz
= top
->GetSizer()->CalcMin();
82 if (top
->m_mainWidget
)
84 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program
85 // you know that this magic call is required to make it possible
86 // to shrink the top level window in the expanded->collapsed
87 // transition. This may be sometimes undesired but *is*
88 // necessary and if you look carefully, all GTK+ programs using
89 // GtkExpander perform this trick (e.g. the standard "open file"
90 // dialog of GTK+>=2.4 is not resizeable when the expander is
92 gtk_window_set_resizable (GTK_WINDOW (top
->m_widget
), p
->IsExpanded());
95 top
->SetMinClientSize(sz
);
98 top
->SetClientSize(sz
);
102 if ( p
->m_bIgnoreNextChange
)
104 // change generated programmatically - do not send an event!
105 p
->m_bIgnoreNextChange
= false;
110 wxCollapsiblePaneEvent
ev(p
, p
->GetId(), p
->IsCollapsed());
111 p
->HandleWindowEvent(ev
);
116 void wxCollapsiblePane::AddChildGTK(wxWindowGTK
* child
)
118 // should be used only once to insert the "pane" into the
119 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
120 // it has been called only once (and in any case we would get a warning
121 // from the following call as GtkExpander is a GtkBin and can contain only
123 gtk_container_add(GTK_CONTAINER(m_widget
), child
->m_widget
);
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane
, wxControl
)
132 BEGIN_EVENT_TABLE(wxCollapsiblePane
, wxCollapsiblePaneBase
)
133 EVT_SIZE(wxCollapsiblePane::OnSize
)
136 bool wxCollapsiblePane::Create(wxWindow
*parent
,
138 const wxString
& label
,
142 const wxValidator
& val
,
143 const wxString
& name
)
145 m_bIgnoreNextChange
= false;
147 if ( !PreCreation( parent
, pos
, size
) ||
148 !wxControl::CreateBase(parent
, id
, pos
, size
, style
, val
, name
) )
150 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
155 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label
)));
156 g_object_ref(m_widget
);
158 g_signal_connect_after(m_widget
, "notify::expanded",
159 G_CALLBACK(gtk_collapsiblepane_expanded_callback
), this);
161 // this the real "pane"
162 m_pPane
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
163 wxTAB_TRAVERSAL
|wxNO_BORDER
, wxT("wxCollapsiblePanePane") );
165 gtk_widget_show(m_widget
);
166 m_parent
->DoAddChild( this );
170 // we should blend into our parent background
171 const wxColour bg
= parent
->GetBackgroundColour();
172 SetBackgroundColour(bg
);
173 m_pPane
->SetBackgroundColour(bg
);
175 // remember the size of this control when it's collapsed
179 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
182 m_szCollapsed
= wxSize( req
.width
, req
.height
);
184 g_signal_connect (m_pPane
->m_widget
, "map_event",
185 G_CALLBACK (gtk_collpane_map_unmap_callback
), this);
186 g_signal_connect (m_pPane
->m_widget
, "unmap_event",
187 G_CALLBACK (gtk_collpane_map_unmap_callback
), this);
193 wxSize
wxCollapsiblePane::DoGetBestSize() const
195 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
197 wxSize sz
= m_szCollapsed
;
200 wxSize panesz
= GetPane()->GetBestSize();
201 sz
.x
= wxMax(sz
.x
, panesz
.x
);
202 sz
.y
+= gtk_expander_get_spacing(GTK_EXPANDER(m_widget
)) + panesz
.y
;
208 GdkWindow
*wxCollapsiblePane::GTKGetWindow(wxArrayGdkWindows
& windows
) const
210 GtkWidget
*label
= gtk_expander_get_label_widget( GTK_EXPANDER(m_widget
) );
211 windows
.Add( label
->window
);
212 windows
.Add( m_widget
->window
);
217 void wxCollapsiblePane::Collapse(bool collapse
)
220 if (IsCollapsed() == collapse
)
223 // do not send event in next signal handler call
224 m_bIgnoreNextChange
= true;
225 gtk_expander_set_expanded(GTK_EXPANDER(m_widget
), !collapse
);
228 bool wxCollapsiblePane::IsCollapsed() const
230 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget
));
233 void wxCollapsiblePane::SetLabel(const wxString
&str
)
235 gtk_expander_set_label(GTK_EXPANDER(m_widget
), wxGTK_CONV(str
));
237 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
238 // we may get the size of the control with the pane size summed up if we are expanded!
239 //m_szCollapsed.x = GetBestSize().x;
242 void wxCollapsiblePane::OnSize(wxSizeEvent
&ev
)
244 #if 0 // for debug only
246 dc
.SetPen(*wxBLACK_PEN
);
247 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
248 dc
.DrawRectangle(wxPoint(0,0), GetSize());
249 dc
.SetPen(*wxRED_PEN
);
250 dc
.DrawRectangle(wxPoint(0,0), GetBestSize());
253 // here we need to resize the pane window otherwise, even if the GtkExpander container
254 // is expanded or shrunk, the pane window won't be updated!
255 m_pPane
->SetSize(ev
.GetSize().x
, ev
.GetSize().y
- m_szCollapsed
.y
);
257 // we need to explicitly call m_pPane->Layout() or else it won't correctly relayout
258 // (even if SetAutoLayout(true) has been called on it!)
262 #endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)