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