]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
fix a few hundreds of harmless unused parameters warnings and a couple of real bugs...
[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
99d625fc 20#if wxUSE_COLLPANE && defined(__WXGTK24__) && !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
VZ
28
29// ============================================================================
30// implementation
31// ============================================================================
32
3c1f8cb1
VZ
33//-----------------------------------------------------------------------------
34// "notify::expanded" signal
35//-----------------------------------------------------------------------------
36
37extern "C" {
38
e4161a2a
VZ
39static void
40gtk_collapsiblepane_expanded_callback(GObject * WXUNUSED(object),
41 GParamSpec * WXUNUSED(param_spec),
42 wxCollapsiblePane *p)
3c1f8cb1 43{
550d433e
VZ
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!
3c1f8cb1
VZ
50
51 wxSize sz;
550d433e 52 if ( p->IsExpanded() )
3c1f8cb1 53 {
912c3932
VZ
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:
58
59 sz = p->m_szCollapsed;
60
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;
3c1f8cb1 64 }
550d433e 65 else // collapsed
3c1f8cb1 66 {
550d433e
VZ
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...
3c1f8cb1
VZ
70 // So, we use the size cached at control-creation time...
71 sz = p->m_szCollapsed;
72 }
73
0ccf0043
RR
74 // VERY IMPORTANT:
75 // just calling
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.
912c3932 82 //
0ccf0043
RR
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!
912c3932 86
0ccf0043 87 // 1) need to update our size hints
912c3932
VZ
88 // NB: this function call won't actually do any long operation
89 // (redraw/relayouting/resizing) so that it's flicker-free
90 p->SetMinSize(sz);
91
92 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
93 {
0514c6a2
RD
94 // fire an event
95 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
96 p->GetEventHandler()->ProcessEvent(ev);
74ab5f5b 97
912c3932
VZ
98 // the user asked to explicitely handle the resizing itself...
99 return;
100 }
101
102 wxTopLevelWindow *
103 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
104 if ( top && top->GetSizer() )
105 {
0ccf0043 106 // 2) recalculate minimal size of the top window
74ab5f5b 107 sz = top->GetSizer()->CalcMin();
912c3932 108
912c3932
VZ
109 if (top->m_mainWidget)
110 {
0ccf0043
RR
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());
119
cca410b3
PC
120 // 4) set size hints
121 top->SetSizeHints(sz.x, sz.y);
912c3932 122
cca410b3
PC
123 // 5) set size
124 top->SetClientSize(sz);
912c3932
VZ
125 }
126 }
0ccf0043 127
550d433e 128 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
129 {
130 // change generated programmatically - do not send an event!
131 p->m_bIgnoreNextChange = false;
132 return;
133 }
134
135 // fire an event
136 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
137 p->GetEventHandler()->ProcessEvent(ev);
138}
139}
140
550d433e
VZ
141static void
142gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
3c1f8cb1 143{
550d433e
VZ
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
148 // a single child!).
3c1f8cb1
VZ
149 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
150}
151
3c1f8cb1
VZ
152//-----------------------------------------------------------------------------
153// wxCollapsiblePane
154//-----------------------------------------------------------------------------
155
156IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
157
158BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
159 EVT_SIZE(wxCollapsiblePane::OnSize)
160END_EVENT_TABLE()
161
550d433e
VZ
162bool wxCollapsiblePane::Create(wxWindow *parent,
163 wxWindowID id,
164 const wxString& label,
165 const wxPoint& pos,
166 const wxSize& size,
167 long style,
168 const wxValidator& val,
169 const wxString& name)
3c1f8cb1 170{
550d433e
VZ
171 if (gtk_check_version(2,4,0))
172 return wxGenericCollapsiblePane::Create(parent, id, label,
173 pos, size, style, val, name);
3c1f8cb1 174
3c1f8cb1
VZ
175 m_bIgnoreNextChange = false;
176
550d433e
VZ
177 if ( !PreCreation( parent, pos, size ) ||
178 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
179 {
180 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
181 return false;
182 }
183
ff928a27
VZ
184 m_widget =
185 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
3c1f8cb1 186
550d433e
VZ
187 // see the gtk_collapsiblepane_expanded_callback comments to understand why
188 // we connect to the "notify::expanded" signal instead of the more common
189 // "activate" one
3c1f8cb1
VZ
190 g_signal_connect(m_widget, "notify::expanded",
191 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
192
550d433e
VZ
193 // before creating m_pPane, we need to makesure our own insert callback
194 // will be used
3c1f8cb1
VZ
195 m_insertCallback = gtk_collapsiblepane_insert_callback;
196
197 // this the real "pane"
037c7b4c
RD
198 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
199 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1 200
10bd1f7d 201 gtk_widget_show(m_widget);
3c1f8cb1
VZ
202 m_parent->DoAddChild( this );
203
204 PostCreation(size);
3c1f8cb1
VZ
205
206 // remember the size of this control when it's collapsed
207 m_szCollapsed = GetBestSize();
208
3c1f8cb1
VZ
209 return true;
210}
211
212wxSize wxCollapsiblePane::DoGetBestSize() const
213{
214 if (!gtk_check_version(2,4,0))
215 {
3c1f8cb1
VZ
216 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
217
218 GtkRequisition req;
219 req.width = 2;
220 req.height = 2;
221 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
222 (m_widget, &req );
223
550d433e 224 // notice that we do not cache our best size here as it changes
912c3932 225 // all times the user expands/hide our pane
550d433e 226 return wxSize(req.width, req.height);
3c1f8cb1
VZ
227 }
228
229 return wxGenericCollapsiblePane::DoGetBestSize();
230}
231
232void wxCollapsiblePane::Collapse(bool collapse)
233{
234 if (!gtk_check_version(2,4,0))
235 {
236 // optimization
237 if (IsCollapsed() == collapse)
238 return;
239
240 // do not send event in next signal handler call
241 m_bIgnoreNextChange = true;
242 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
243 }
244 else
245 wxGenericCollapsiblePane::Collapse(collapse);
246}
247
248bool wxCollapsiblePane::IsCollapsed() const
249{
250 if (!gtk_check_version(2,4,0))
251 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
252
253 return wxGenericCollapsiblePane::IsCollapsed();
254}
255
256void wxCollapsiblePane::SetLabel(const wxString &str)
257{
258 if (!gtk_check_version(2,4,0))
912c3932 259 {
9489e62f 260 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
912c3932
VZ
261
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;
265 }
3c1f8cb1
VZ
266 else
267 wxGenericCollapsiblePane::SetLabel(str);
268}
269
270void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
271{
272#if 0 // for debug only
273 wxClientDC dc(this);
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());
279#endif
280
3c1f8cb1
VZ
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!
912c3932 283 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1
VZ
284
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!)
287 m_pPane->Layout();
288}
289
99d625fc 290#endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
3c1f8cb1 291