]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
don't include private headers from wx/unix/evtloop.h
[wxWidgets.git] / src / gtk / collpane.cpp
CommitLineData
3c1f8cb1
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/collpane.cpp
3// Purpose: wxCollapsiblePane
4// Author: Francesco Montorsi
5// Modified By:
6// Created: 8/10/2006
7// Id: $Id$
8// Copyright: (c) Francesco Montorsi
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
ff654490 20#if wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)
3c1f8cb1
VZ
21
22#include "wx/collpane.h"
0ccf0043
RR
23#include "wx/toplevel.h"
24#include "wx/sizer.h"
4c79f9c1 25#include "wx/panel.h"
0ccf0043 26
2cbf7014 27#include "wx/gtk/private.h"
3c1f8cb1 28
ff654490
VZ
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
31
23318a53 32const char wxCollapsiblePaneNameStr[] = "collapsiblePane";
ff654490 33
3c778901 34wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED, wxCollapsiblePaneEvent )
ff654490
VZ
35
36IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
37
3c1f8cb1
VZ
38// ============================================================================
39// implementation
40// ============================================================================
41
3c1f8cb1
VZ
42//-----------------------------------------------------------------------------
43// "notify::expanded" signal
44//-----------------------------------------------------------------------------
45
46extern "C" {
47
e4161a2a
VZ
48static void
49gtk_collapsiblepane_expanded_callback(GObject * WXUNUSED(object),
50 GParamSpec * WXUNUSED(param_spec),
51 wxCollapsiblePane *p)
3c1f8cb1 52{
b53aea81 53 if (!p->IsCollapsed())
3c1f8cb1 54 {
b53aea81
RR
55 if (p->GetPane()->GetSizer())
56 p->GetPane()->GetSizer()->Fit( p->GetPane() );
3c1f8cb1 57 }
b53aea81
RR
58}
59}
3c1f8cb1 60
b53aea81
RR
61extern "C" {
62static void
63gtk_collpane_map_unmap_callback( GtkWidget *WXUNUSED(pane), GdkEvent *WXUNUSED(event), wxCollapsiblePane* p )
64{
912c3932
VZ
65 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
66 {
0514c6a2
RD
67 // fire an event
68 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
937013e0 69 p->HandleWindowEvent(ev);
74ab5f5b 70
912c3932
VZ
71 // the user asked to explicitely handle the resizing itself...
72 return;
73 }
b53aea81 74
912c3932
VZ
75 wxTopLevelWindow *
76 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
77 if ( top && top->GetSizer() )
78 {
0ccf0043 79 // 2) recalculate minimal size of the top window
b53aea81 80 wxSize sz = top->GetSizer()->CalcMin();
912c3932 81
912c3932
VZ
82 if (top->m_mainWidget)
83 {
2e10110a
VS
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
91 // collapsed!)
0ccf0043
RR
92 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
93
cca410b3 94 // 4) set size hints
2e10110a 95 top->SetMinClientSize(sz);
912c3932 96
cca410b3
PC
97 // 5) set size
98 top->SetClientSize(sz);
912c3932
VZ
99 }
100 }
0ccf0043 101
550d433e 102 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
103 {
104 // change generated programmatically - do not send an event!
105 p->m_bIgnoreNextChange = false;
106 return;
107 }
108
109 // fire an event
110 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
937013e0 111 p->HandleWindowEvent(ev);
3c1f8cb1
VZ
112}
113}
114
b53aea81 115
48200154 116void wxCollapsiblePane::AddChildGTK(wxWindowGTK* child)
3c1f8cb1 117{
48200154 118 // should be used only once to insert the "pane" into the
550d433e
VZ
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
122 // a single child!).
48200154 123 gtk_container_add(GTK_CONTAINER(m_widget), child->m_widget);
3c1f8cb1
VZ
124}
125
3c1f8cb1
VZ
126//-----------------------------------------------------------------------------
127// wxCollapsiblePane
128//-----------------------------------------------------------------------------
129
ff654490 130IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxControl)
3c1f8cb1 131
ff654490 132BEGIN_EVENT_TABLE(wxCollapsiblePane, wxCollapsiblePaneBase)
3c1f8cb1
VZ
133 EVT_SIZE(wxCollapsiblePane::OnSize)
134END_EVENT_TABLE()
135
550d433e
VZ
136bool wxCollapsiblePane::Create(wxWindow *parent,
137 wxWindowID id,
138 const wxString& label,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxValidator& val,
143 const wxString& name)
3c1f8cb1 144{
3c1f8cb1
VZ
145 m_bIgnoreNextChange = false;
146
550d433e
VZ
147 if ( !PreCreation( parent, pos, size ) ||
148 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
149 {
150 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
151 return false;
152 }
153
ff928a27
VZ
154 m_widget =
155 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
9ff9d30c 156 g_object_ref(m_widget);
3c1f8cb1 157
b53aea81 158 g_signal_connect_after(m_widget, "notify::expanded",
3c1f8cb1 159 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
b53aea81 160
3c1f8cb1 161 // this the real "pane"
037c7b4c 162 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
04443021 163 wxTAB_TRAVERSAL|wxNO_BORDER, wxT("wxCollapsiblePanePane") );
3c1f8cb1 164
10bd1f7d 165 gtk_widget_show(m_widget);
3c1f8cb1 166 m_parent->DoAddChild( this );
b53aea81 167
3c1f8cb1 168 PostCreation(size);
3c1f8cb1
VZ
169
170 // remember the size of this control when it's collapsed
b53aea81
RR
171 GtkRequisition req;
172 req.width = 2;
173 req.height = 2;
174 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
175 (m_widget, &req );
176
177 m_szCollapsed = wxSize( req.width, req.height );
178
179 g_signal_connect (m_pPane->m_widget, "map_event",
180 G_CALLBACK (gtk_collpane_map_unmap_callback), this);
181 g_signal_connect (m_pPane->m_widget, "unmap_event",
182 G_CALLBACK (gtk_collpane_map_unmap_callback), this);
183
3c1f8cb1 184
3c1f8cb1
VZ
185 return true;
186}
187
188wxSize wxCollapsiblePane::DoGetBestSize() const
189{
ff654490 190 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
3c1f8cb1 191
b53aea81
RR
192 wxSize sz = m_szCollapsed;
193 if ( IsExpanded() )
194 {
195 wxSize panesz = GetPane()->GetBestSize();
196 sz.x = wxMax(sz.x, panesz.x);
197 sz.y += gtk_expander_get_spacing(GTK_EXPANDER(m_widget)) + panesz.y;
198 }
199
200 return sz;
3c1f8cb1
VZ
201}
202
203void wxCollapsiblePane::Collapse(bool collapse)
204{
ff654490
VZ
205 // optimization
206 if (IsCollapsed() == collapse)
207 return;
3c1f8cb1 208
ff654490
VZ
209 // do not send event in next signal handler call
210 m_bIgnoreNextChange = true;
211 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
3c1f8cb1
VZ
212}
213
214bool wxCollapsiblePane::IsCollapsed() const
215{
ff654490 216 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
3c1f8cb1
VZ
217}
218
219void wxCollapsiblePane::SetLabel(const wxString &str)
220{
ff654490 221 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
912c3932 222
ff654490
VZ
223 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
224 // we may get the size of the control with the pane size summed up if we are expanded!
225 //m_szCollapsed.x = GetBestSize().x;
3c1f8cb1
VZ
226}
227
228void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
229{
230#if 0 // for debug only
231 wxClientDC dc(this);
232 dc.SetPen(*wxBLACK_PEN);
233 dc.SetBrush(*wxTRANSPARENT_BRUSH);
234 dc.DrawRectangle(wxPoint(0,0), GetSize());
235 dc.SetPen(*wxRED_PEN);
236 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
237#endif
238
3c1f8cb1
VZ
239 // here we need to resize the pane window otherwise, even if the GtkExpander container
240 // is expanded or shrinked, the pane window won't be updated!
912c3932 241 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1
VZ
242
243 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
244 // (even if SetAutoLayout(true) has been called on it!)
245 m_pPane->Layout();
246}
247
ff654490 248#endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)
3c1f8cb1 249