]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
Compile fix for Universal
[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
912c3932 20#if wxUSE_COLLPANE && defined( __WXGTK24__ )
3c1f8cb1
VZ
21
22#include "wx/collpane.h"
2cbf7014 23#include "wx/gtk/private.h"
912c3932 24#include "wx/gtk/win_gtk.h"
550d433e 25
3c1f8cb1 26#include <gtk/gtkexpander.h>
3c1f8cb1 27
550d433e
VZ
28const wxChar wxCollapsiblePaneNameStr[] = wxT("CollapsiblePane");
29
3c1f8cb1
VZ
30// ============================================================================
31// implementation
32// ============================================================================
33
3c1f8cb1
VZ
34//-----------------------------------------------------------------------------
35// "notify::expanded" signal
36//-----------------------------------------------------------------------------
37
38extern "C" {
39
40static void gtk_collapsiblepane_expanded_callback (GObject *object,
41 GParamSpec *param_spec,
42 wxCollapsiblePane *p)
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
912c3932
VZ
74#if 1
75 // this does work but in the expanded->collapsed transition it provokes
76 // a lot of flicker!!!
77 //
78 // It also has the problem that in the collapsed->expanded transition with the
79 // "clearlooks" GTK theme I get:
80 //
81 // ** (collpane:18928): CRITICAL **: clearlooks_style_draw_focus: assertion `height >= -1' failed
82 // ** (collpane:18928): CRITICAL **: clearlooks_style_draw_focus: assertion `height >= -1' failed
83 //
84 // Not sure however if this is a ClearLooks bug or rather my bug.
85 // Note that those warnings only appear:
86 // 1) if you're using clearlooks theme
87 // 2) if you use the "Change status" wxButton in samples/collpane application
4223cec5 88 p->OnStateChange(sz);
3c1f8cb1 89
912c3932
VZ
90#else // flicker-free code
91
92
93 // need to update our size hints
94 // NB: this function call won't actually do any long operation
95 // (redraw/relayouting/resizing) so that it's flicker-free
96 p->SetMinSize(sz);
97
98 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
99 {
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 {
108 // recalculate minimal size of the top window
109 wxSize sz = top->GetSizer()->CalcMin();
110
111 // FIXME:
112 // THE PROBLEM WITH THIS CODE IS THAT IN THE EXPANDED->COLLAPSED TRANSITION
113 // IT DOES *NOT* SHRINK THE TOP WINDOW.
114 // However it's flicker-free, native code and it also does not have the
115 // ** (collpane:18928): CRITICAL **: clearlooks_style_draw_focus: assertion `height >= -1' failed
116 // problem
117
118 if (top->m_mainWidget)
119 {
120 wxLogDebug(wxT("setting min size to %d;%d"), sz.x, sz.y);
121
122 // set size hints
123 GdkGeometry geom;
124
125 geom.min_width = sz.x;
126 geom.min_height = sz.y;
127
128 gtk_window_set_geometry_hints( GTK_WINDOW(top->m_widget),
129 (GtkWidget*) NULL,
130 &geom,
131 GDK_HINT_MIN_SIZE );
132 //gtk_window_set_default_size( GTK_WINDOW(top->m_widget), sz.x, sz.y );
133
134
135 /* I revert back to wxGTK's original behaviour. m_mainWidget holds the
136 * menubar, the toolbar and the client area, which is represented by
137 * m_wxwindow.
138 * this hurts in the eye, but I don't want to call SetSize()
139 * because I don't want to call any non-native functions here. */
140
141 top->m_width = sz.x;
142 top->m_height = sz.y;
143
144 int client_x = top->m_miniEdge;
145 int client_y = top->m_miniEdge + top->m_miniTitle;
146 int client_w = top->m_width - 2*top->m_miniEdge;
147 int client_h = top->m_height - 2*top->m_miniEdge - top->m_miniTitle;
148 if (client_w < 0)
149 client_w = 0;
150 if (client_h < 0)
151 client_h = 0;
152
153 // Let the parent perform the resize
154 gtk_pizza_set_size( GTK_PIZZA(top->m_mainWidget),
155 top->m_wxwindow,
156 client_x, client_y, client_w, client_h );
157
158 gtk_widget_set_size_request( top->m_wxwindow, sz.x, sz.y );
159
160 }
161 }
162#endif
550d433e 163 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
164 {
165 // change generated programmatically - do not send an event!
166 p->m_bIgnoreNextChange = false;
167 return;
168 }
169
170 // fire an event
171 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
172 p->GetEventHandler()->ProcessEvent(ev);
173}
174}
175
550d433e
VZ
176static void
177gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
3c1f8cb1 178{
550d433e
VZ
179 // this callback should be used only once to insert the "pane" into the
180 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
181 // it has been called only once (and in any case we would get a warning
182 // from the following call as GtkExpander is a GtkBin and can contain only
183 // a single child!).
3c1f8cb1
VZ
184 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
185}
186
3c1f8cb1
VZ
187//-----------------------------------------------------------------------------
188// wxCollapsiblePane
189//-----------------------------------------------------------------------------
190
191IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
192
193BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
194 EVT_SIZE(wxCollapsiblePane::OnSize)
195END_EVENT_TABLE()
196
550d433e
VZ
197bool wxCollapsiblePane::Create(wxWindow *parent,
198 wxWindowID id,
199 const wxString& label,
200 const wxPoint& pos,
201 const wxSize& size,
202 long style,
203 const wxValidator& val,
204 const wxString& name)
3c1f8cb1 205{
550d433e
VZ
206 if (gtk_check_version(2,4,0))
207 return wxGenericCollapsiblePane::Create(parent, id, label,
208 pos, size, style, val, name);
3c1f8cb1
VZ
209
210 m_needParent = true;
211 m_acceptsFocus = true;
212 m_bIgnoreNextChange = false;
213
550d433e
VZ
214 if ( !PreCreation( parent, pos, size ) ||
215 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
216 {
217 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
218 return false;
219 }
220
ff928a27
VZ
221 m_widget =
222 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
3c1f8cb1 223
550d433e
VZ
224 // see the gtk_collapsiblepane_expanded_callback comments to understand why
225 // we connect to the "notify::expanded" signal instead of the more common
226 // "activate" one
3c1f8cb1
VZ
227 g_signal_connect(m_widget, "notify::expanded",
228 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
229
550d433e
VZ
230 // before creating m_pPane, we need to makesure our own insert callback
231 // will be used
3c1f8cb1
VZ
232 m_insertCallback = gtk_collapsiblepane_insert_callback;
233
234 // this the real "pane"
550d433e
VZ
235 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
236 wxNO_BORDER);
3c1f8cb1
VZ
237
238 gtk_widget_show( GTK_WIDGET(m_widget) );
239 m_parent->DoAddChild( this );
240
241 PostCreation(size);
3c1f8cb1
VZ
242
243 // remember the size of this control when it's collapsed
244 m_szCollapsed = GetBestSize();
245
3c1f8cb1
VZ
246 return true;
247}
248
249wxSize wxCollapsiblePane::DoGetBestSize() const
250{
251 if (!gtk_check_version(2,4,0))
252 {
3c1f8cb1
VZ
253 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
254
255 GtkRequisition req;
256 req.width = 2;
257 req.height = 2;
258 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
259 (m_widget, &req );
260
550d433e 261 // notice that we do not cache our best size here as it changes
912c3932 262 // all times the user expands/hide our pane
550d433e 263 return wxSize(req.width, req.height);
3c1f8cb1
VZ
264 }
265
266 return wxGenericCollapsiblePane::DoGetBestSize();
267}
268
269void wxCollapsiblePane::Collapse(bool collapse)
270{
271 if (!gtk_check_version(2,4,0))
272 {
273 // optimization
274 if (IsCollapsed() == collapse)
275 return;
276
277 // do not send event in next signal handler call
278 m_bIgnoreNextChange = true;
279 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
280 }
281 else
282 wxGenericCollapsiblePane::Collapse(collapse);
283}
284
285bool wxCollapsiblePane::IsCollapsed() const
286{
287 if (!gtk_check_version(2,4,0))
288 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
289
290 return wxGenericCollapsiblePane::IsCollapsed();
291}
292
293void wxCollapsiblePane::SetLabel(const wxString &str)
294{
295 if (!gtk_check_version(2,4,0))
912c3932 296 {
9489e62f 297 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
912c3932
VZ
298
299 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
300 // we may get the size of the control with the pane size summed up if we are expanded!
301 //m_szCollapsed.x = GetBestSize().x;
302 }
3c1f8cb1
VZ
303 else
304 wxGenericCollapsiblePane::SetLabel(str);
305}
306
307void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
308{
309#if 0 // for debug only
310 wxClientDC dc(this);
311 dc.SetPen(*wxBLACK_PEN);
312 dc.SetBrush(*wxTRANSPARENT_BRUSH);
313 dc.DrawRectangle(wxPoint(0,0), GetSize());
314 dc.SetPen(*wxRED_PEN);
315 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
316#endif
317
3c1f8cb1
VZ
318 // here we need to resize the pane window otherwise, even if the GtkExpander container
319 // is expanded or shrinked, the pane window won't be updated!
912c3932 320 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1
VZ
321
322 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
323 // (even if SetAutoLayout(true) has been called on it!)
324 m_pPane->Layout();
325}
326
912c3932 327#endif // wxUSE_COLLPANE && defined( __WXGTK24__ )
3c1f8cb1 328