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