Use a GtkVBox to do TLW layout. Rework some of the remaining sizing code.
[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
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 //-----------------------------------------------------------------------------
34 // "notify::expanded" signal
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38
39 static void
40 gtk_collapsiblepane_expanded_callback(GObject * WXUNUSED(object),
41 GParamSpec * WXUNUSED(param_spec),
42 wxCollapsiblePane *p)
43 {
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!
50
51 wxSize sz;
52 if ( p->IsExpanded() )
53 {
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;
64 }
65 else // collapsed
66 {
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...
70 // So, we use the size cached at control-creation time...
71 sz = p->m_szCollapsed;
72 }
73
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.
82 //
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!
86
87 // 1) need to update our size hints
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 {
94 // fire an event
95 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
96 p->GetEventHandler()->ProcessEvent(ev);
97
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 {
106 // 2) recalculate minimal size of the top window
107 sz = top->GetSizer()->CalcMin();
108
109 if (top->m_mainWidget)
110 {
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
120 // 4) set size hints
121 top->SetSizeHints(sz.x, sz.y);
122
123 // 5) set size
124 top->SetClientSize(sz);
125 }
126 }
127
128 if ( p->m_bIgnoreNextChange )
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
141 static void
142 gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
143 {
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!).
149 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
150 }
151
152 //-----------------------------------------------------------------------------
153 // wxCollapsiblePane
154 //-----------------------------------------------------------------------------
155
156 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
157
158 BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
159 EVT_SIZE(wxCollapsiblePane::OnSize)
160 END_EVENT_TABLE()
161
162 bool 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)
170 {
171 if (gtk_check_version(2,4,0))
172 return wxGenericCollapsiblePane::Create(parent, id, label,
173 pos, size, style, val, name);
174
175 m_bIgnoreNextChange = false;
176
177 if ( !PreCreation( parent, pos, size ) ||
178 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
179 {
180 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
181 return false;
182 }
183
184 m_widget =
185 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
186
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
190 g_signal_connect(m_widget, "notify::expanded",
191 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
192
193 // before creating m_pPane, we need to makesure our own insert callback
194 // will be used
195 m_insertCallback = gtk_collapsiblepane_insert_callback;
196
197 // this the real "pane"
198 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
199 wxTAB_TRAVERSAL|wxNO_BORDER);
200
201 gtk_widget_show(m_widget);
202 m_parent->DoAddChild( this );
203
204 PostCreation(size);
205
206 // remember the size of this control when it's collapsed
207 m_szCollapsed = GetBestSize();
208
209 return true;
210 }
211
212 wxSize wxCollapsiblePane::DoGetBestSize() const
213 {
214 if (!gtk_check_version(2,4,0))
215 {
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
224 // notice that we do not cache our best size here as it changes
225 // all times the user expands/hide our pane
226 return wxSize(req.width, req.height);
227 }
228
229 return wxGenericCollapsiblePane::DoGetBestSize();
230 }
231
232 void 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
248 bool 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
256 void wxCollapsiblePane::SetLabel(const wxString &str)
257 {
258 if (!gtk_check_version(2,4,0))
259 {
260 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
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 }
266 else
267 wxGenericCollapsiblePane::SetLabel(str);
268 }
269
270 void 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
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!
283 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
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
290 #endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
291