]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
Made wxDataViewCtrl sample use a real, expandabl data tree
[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"
912c3932 28#include "wx/gtk/win_gtk.h"
550d433e 29
3c1f8cb1 30#include <gtk/gtkexpander.h>
3c1f8cb1
VZ
31
32// ============================================================================
33// implementation
34// ============================================================================
35
3c1f8cb1
VZ
36//-----------------------------------------------------------------------------
37// "notify::expanded" signal
38//-----------------------------------------------------------------------------
39
40extern "C" {
41
42static void gtk_collapsiblepane_expanded_callback (GObject *object,
43 GParamSpec *param_spec,
44 wxCollapsiblePane *p)
45{
550d433e
VZ
46 // NB: unlike for the "activate" signal, when this callback is called, if
47 // we try to query the "collapsed" status through p->IsCollapsed(), we
48 // get the right value. I.e. here p->IsCollapsed() will return false if
49 // this callback has been called at the end of a collapsed->expanded
50 // transition and viceversa. Inside the "activate" signal callback
51 // p->IsCollapsed() would return the wrong value!
3c1f8cb1
VZ
52
53 wxSize sz;
550d433e 54 if ( p->IsExpanded() )
3c1f8cb1 55 {
912c3932
VZ
56 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
57 // here as they would return the size for the collapsed expander
58 // even if the collapsed->expanded transition has already been
59 // completed; we solve this problem doing:
60
61 sz = p->m_szCollapsed;
62
63 wxSize panesz = p->GetPane()->GetBestSize();
64 sz.x = wxMax(sz.x, panesz.x);
65 sz.y += gtk_expander_get_spacing(GTK_EXPANDER(p->m_widget)) + panesz.y;
3c1f8cb1 66 }
550d433e 67 else // collapsed
3c1f8cb1 68 {
550d433e
VZ
69 // same problem described above: using p->Get[Best|Min]Size() here we
70 // would get the size of the control when it is expanded even if the
71 // expanded->collapsed transition should be complete now...
3c1f8cb1
VZ
72 // So, we use the size cached at control-creation time...
73 sz = p->m_szCollapsed;
74 }
75
0ccf0043
RR
76 // VERY IMPORTANT:
77 // just calling
78 // p->OnStateChange(sz);
79 // here would work work BUT:
80 // 1) in the expanded->collapsed transition it provokes a lot of flickering
81 // 2) in the collapsed->expanded transition using the "Change status" wxButton
82 // in samples/collpane application some strange warnings would be generated
83 // by the "clearlooks" theme, if that's your theme.
912c3932 84 //
0ccf0043
RR
85 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
86 // calculations to happen. Note that the following code has been very carefully designed
87 // and tested - be VERY careful when changing it!
912c3932 88
0ccf0043 89 // 1) need to update our size hints
912c3932
VZ
90 // NB: this function call won't actually do any long operation
91 // (redraw/relayouting/resizing) so that it's flicker-free
92 p->SetMinSize(sz);
93
94 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
95 {
0514c6a2
RD
96 // fire an event
97 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
98 p->GetEventHandler()->ProcessEvent(ev);
99
912c3932
VZ
100 // the user asked to explicitely handle the resizing itself...
101 return;
102 }
103
104 wxTopLevelWindow *
105 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
106 if ( top && top->GetSizer() )
107 {
0ccf0043 108 // 2) recalculate minimal size of the top window
912c3932
VZ
109 wxSize sz = top->GetSizer()->CalcMin();
110
912c3932
VZ
111 if (top->m_mainWidget)
112 {
0ccf0043
RR
113 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
114 // that this magic call is required to make it possible to shrink the
115 // top level window in the expanded->collapsed transition.
116 // This may be sometimes undesired but *is* necessary and if you look
117 // carefully, all GTK+ programs using GtkExpander perform this trick
118 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
119 // when the expander is collapsed!)
120 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
121
122 // 4) set size hints: note that this code has been taken and adapted
123 // from src/gtk/toplevel.cpp
124 GdkGeometry geom;
912c3932
VZ
125
126 geom.min_width = sz.x;
127 geom.min_height = sz.y;
128
129 gtk_window_set_geometry_hints( GTK_WINDOW(top->m_widget),
0ccf0043
RR
130 (GtkWidget*) NULL,
131 &geom,
132 GDK_HINT_MIN_SIZE );
912c3932 133
0ccf0043
RR
134 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
135 // to do the size changes immediately and not delaying them in the idle
136 // time
912c3932
VZ
137 top->m_width = sz.x;
138 top->m_height = sz.y;
139
140 int client_x = top->m_miniEdge;
141 int client_y = top->m_miniEdge + top->m_miniTitle;
142 int client_w = top->m_width - 2*top->m_miniEdge;
143 int client_h = top->m_height - 2*top->m_miniEdge - top->m_miniTitle;
144 if (client_w < 0)
145 client_w = 0;
146 if (client_h < 0)
147 client_h = 0;
148
912c3932
VZ
149 gtk_pizza_set_size( GTK_PIZZA(top->m_mainWidget),
150 top->m_wxwindow,
151 client_x, client_y, client_w, client_h );
152
153 gtk_widget_set_size_request( top->m_wxwindow, sz.x, sz.y );
154
155 }
156 }
0ccf0043 157
550d433e 158 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
159 {
160 // change generated programmatically - do not send an event!
161 p->m_bIgnoreNextChange = false;
162 return;
163 }
164
165 // fire an event
166 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
167 p->GetEventHandler()->ProcessEvent(ev);
168}
169}
170
550d433e
VZ
171static void
172gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
3c1f8cb1 173{
550d433e
VZ
174 // this callback should be used only once to insert the "pane" into the
175 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
176 // it has been called only once (and in any case we would get a warning
177 // from the following call as GtkExpander is a GtkBin and can contain only
178 // a single child!).
3c1f8cb1
VZ
179 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
180}
181
3c1f8cb1
VZ
182//-----------------------------------------------------------------------------
183// wxCollapsiblePane
184//-----------------------------------------------------------------------------
185
186IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
187
188BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
189 EVT_SIZE(wxCollapsiblePane::OnSize)
190END_EVENT_TABLE()
191
550d433e
VZ
192bool wxCollapsiblePane::Create(wxWindow *parent,
193 wxWindowID id,
194 const wxString& label,
195 const wxPoint& pos,
196 const wxSize& size,
197 long style,
198 const wxValidator& val,
199 const wxString& name)
3c1f8cb1 200{
550d433e
VZ
201 if (gtk_check_version(2,4,0))
202 return wxGenericCollapsiblePane::Create(parent, id, label,
203 pos, size, style, val, name);
3c1f8cb1 204
3c1f8cb1
VZ
205 m_bIgnoreNextChange = false;
206
550d433e
VZ
207 if ( !PreCreation( parent, pos, size ) ||
208 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
209 {
210 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
211 return false;
212 }
213
ff928a27
VZ
214 m_widget =
215 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
3c1f8cb1 216
550d433e
VZ
217 // see the gtk_collapsiblepane_expanded_callback comments to understand why
218 // we connect to the "notify::expanded" signal instead of the more common
219 // "activate" one
3c1f8cb1
VZ
220 g_signal_connect(m_widget, "notify::expanded",
221 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
222
550d433e
VZ
223 // before creating m_pPane, we need to makesure our own insert callback
224 // will be used
3c1f8cb1
VZ
225 m_insertCallback = gtk_collapsiblepane_insert_callback;
226
227 // this the real "pane"
037c7b4c
RD
228 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
229 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1 230
10bd1f7d 231 gtk_widget_show(m_widget);
3c1f8cb1
VZ
232 m_parent->DoAddChild( this );
233
234 PostCreation(size);
3c1f8cb1
VZ
235
236 // remember the size of this control when it's collapsed
237 m_szCollapsed = GetBestSize();
238
3c1f8cb1
VZ
239 return true;
240}
241
242wxSize wxCollapsiblePane::DoGetBestSize() const
243{
244 if (!gtk_check_version(2,4,0))
245 {
3c1f8cb1
VZ
246 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
247
248 GtkRequisition req;
249 req.width = 2;
250 req.height = 2;
251 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
252 (m_widget, &req );
253
550d433e 254 // notice that we do not cache our best size here as it changes
912c3932 255 // all times the user expands/hide our pane
550d433e 256 return wxSize(req.width, req.height);
3c1f8cb1
VZ
257 }
258
259 return wxGenericCollapsiblePane::DoGetBestSize();
260}
261
262void wxCollapsiblePane::Collapse(bool collapse)
263{
264 if (!gtk_check_version(2,4,0))
265 {
266 // optimization
267 if (IsCollapsed() == collapse)
268 return;
269
270 // do not send event in next signal handler call
271 m_bIgnoreNextChange = true;
272 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
273 }
274 else
275 wxGenericCollapsiblePane::Collapse(collapse);
276}
277
278bool wxCollapsiblePane::IsCollapsed() const
279{
280 if (!gtk_check_version(2,4,0))
281 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
282
283 return wxGenericCollapsiblePane::IsCollapsed();
284}
285
286void wxCollapsiblePane::SetLabel(const wxString &str)
287{
288 if (!gtk_check_version(2,4,0))
912c3932 289 {
9489e62f 290 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
912c3932
VZ
291
292 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
293 // we may get the size of the control with the pane size summed up if we are expanded!
294 //m_szCollapsed.x = GetBestSize().x;
295 }
3c1f8cb1
VZ
296 else
297 wxGenericCollapsiblePane::SetLabel(str);
298}
299
300void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
301{
302#if 0 // for debug only
303 wxClientDC dc(this);
304 dc.SetPen(*wxBLACK_PEN);
305 dc.SetBrush(*wxTRANSPARENT_BRUSH);
306 dc.DrawRectangle(wxPoint(0,0), GetSize());
307 dc.SetPen(*wxRED_PEN);
308 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
309#endif
310
3c1f8cb1
VZ
311 // here we need to resize the pane window otherwise, even if the GtkExpander container
312 // is expanded or shrinked, the pane window won't be updated!
912c3932 313 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1
VZ
314
315 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
316 // (even if SetAutoLayout(true) has been called on it!)
317 m_pPane->Layout();
318}
319
99d625fc 320#endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
3c1f8cb1 321