]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
implemented wxMBConv::IsUTF8() helper for more classes so that all uses of UTF-8...
[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 {
96 // the user asked to explicitely handle the resizing itself...
97 return;
98 }
99
100 wxTopLevelWindow *
101 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
102 if ( top && top->GetSizer() )
103 {
0ccf0043 104 // 2) recalculate minimal size of the top window
912c3932
VZ
105 wxSize sz = top->GetSizer()->CalcMin();
106
912c3932
VZ
107 if (top->m_mainWidget)
108 {
0ccf0043
RR
109 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program you know
110 // that this magic call is required to make it possible to shrink the
111 // top level window in the expanded->collapsed transition.
112 // This may be sometimes undesired but *is* necessary and if you look
113 // carefully, all GTK+ programs using GtkExpander perform this trick
114 // (e.g. the standard "open file" dialog of GTK+>=2.4 is not resizeable
115 // when the expander is collapsed!)
116 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
117
118 // 4) set size hints: note that this code has been taken and adapted
119 // from src/gtk/toplevel.cpp
120 GdkGeometry geom;
912c3932
VZ
121
122 geom.min_width = sz.x;
123 geom.min_height = sz.y;
124
125 gtk_window_set_geometry_hints( GTK_WINDOW(top->m_widget),
0ccf0043
RR
126 (GtkWidget*) NULL,
127 &geom,
128 GDK_HINT_MIN_SIZE );
912c3932 129
0ccf0043
RR
130 // 5) set size: also this code has been adapted from src/gtk/toplevel.cpp
131 // to do the size changes immediately and not delaying them in the idle
132 // time
912c3932
VZ
133 top->m_width = sz.x;
134 top->m_height = sz.y;
135
136 int client_x = top->m_miniEdge;
137 int client_y = top->m_miniEdge + top->m_miniTitle;
138 int client_w = top->m_width - 2*top->m_miniEdge;
139 int client_h = top->m_height - 2*top->m_miniEdge - top->m_miniTitle;
140 if (client_w < 0)
141 client_w = 0;
142 if (client_h < 0)
143 client_h = 0;
144
912c3932
VZ
145 gtk_pizza_set_size( GTK_PIZZA(top->m_mainWidget),
146 top->m_wxwindow,
147 client_x, client_y, client_w, client_h );
148
149 gtk_widget_set_size_request( top->m_wxwindow, sz.x, sz.y );
150
151 }
152 }
0ccf0043 153
550d433e 154 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
155 {
156 // change generated programmatically - do not send an event!
157 p->m_bIgnoreNextChange = false;
158 return;
159 }
160
161 // fire an event
162 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
163 p->GetEventHandler()->ProcessEvent(ev);
164}
165}
166
550d433e
VZ
167static void
168gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
3c1f8cb1 169{
550d433e
VZ
170 // this callback should be used only once to insert the "pane" into the
171 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
172 // it has been called only once (and in any case we would get a warning
173 // from the following call as GtkExpander is a GtkBin and can contain only
174 // a single child!).
3c1f8cb1
VZ
175 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
176}
177
3c1f8cb1
VZ
178//-----------------------------------------------------------------------------
179// wxCollapsiblePane
180//-----------------------------------------------------------------------------
181
182IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
183
184BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
185 EVT_SIZE(wxCollapsiblePane::OnSize)
186END_EVENT_TABLE()
187
550d433e
VZ
188bool wxCollapsiblePane::Create(wxWindow *parent,
189 wxWindowID id,
190 const wxString& label,
191 const wxPoint& pos,
192 const wxSize& size,
193 long style,
194 const wxValidator& val,
195 const wxString& name)
3c1f8cb1 196{
550d433e
VZ
197 if (gtk_check_version(2,4,0))
198 return wxGenericCollapsiblePane::Create(parent, id, label,
199 pos, size, style, val, name);
3c1f8cb1 200
3c1f8cb1
VZ
201 m_bIgnoreNextChange = false;
202
550d433e
VZ
203 if ( !PreCreation( parent, pos, size ) ||
204 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
205 {
206 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
207 return false;
208 }
209
ff928a27
VZ
210 m_widget =
211 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
3c1f8cb1 212
550d433e
VZ
213 // see the gtk_collapsiblepane_expanded_callback comments to understand why
214 // we connect to the "notify::expanded" signal instead of the more common
215 // "activate" one
3c1f8cb1
VZ
216 g_signal_connect(m_widget, "notify::expanded",
217 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
218
550d433e
VZ
219 // before creating m_pPane, we need to makesure our own insert callback
220 // will be used
3c1f8cb1
VZ
221 m_insertCallback = gtk_collapsiblepane_insert_callback;
222
223 // this the real "pane"
037c7b4c
RD
224 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
225 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1 226
10bd1f7d 227 gtk_widget_show(m_widget);
3c1f8cb1
VZ
228 m_parent->DoAddChild( this );
229
230 PostCreation(size);
3c1f8cb1
VZ
231
232 // remember the size of this control when it's collapsed
233 m_szCollapsed = GetBestSize();
234
3c1f8cb1
VZ
235 return true;
236}
237
238wxSize wxCollapsiblePane::DoGetBestSize() const
239{
240 if (!gtk_check_version(2,4,0))
241 {
3c1f8cb1
VZ
242 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
243
244 GtkRequisition req;
245 req.width = 2;
246 req.height = 2;
247 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
248 (m_widget, &req );
249
550d433e 250 // notice that we do not cache our best size here as it changes
912c3932 251 // all times the user expands/hide our pane
550d433e 252 return wxSize(req.width, req.height);
3c1f8cb1
VZ
253 }
254
255 return wxGenericCollapsiblePane::DoGetBestSize();
256}
257
258void wxCollapsiblePane::Collapse(bool collapse)
259{
260 if (!gtk_check_version(2,4,0))
261 {
262 // optimization
263 if (IsCollapsed() == collapse)
264 return;
265
266 // do not send event in next signal handler call
267 m_bIgnoreNextChange = true;
268 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
269 }
270 else
271 wxGenericCollapsiblePane::Collapse(collapse);
272}
273
274bool wxCollapsiblePane::IsCollapsed() const
275{
276 if (!gtk_check_version(2,4,0))
277 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
278
279 return wxGenericCollapsiblePane::IsCollapsed();
280}
281
282void wxCollapsiblePane::SetLabel(const wxString &str)
283{
284 if (!gtk_check_version(2,4,0))
912c3932 285 {
9489e62f 286 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
912c3932
VZ
287
288 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
289 // we may get the size of the control with the pane size summed up if we are expanded!
290 //m_szCollapsed.x = GetBestSize().x;
291 }
3c1f8cb1
VZ
292 else
293 wxGenericCollapsiblePane::SetLabel(str);
294}
295
296void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
297{
298#if 0 // for debug only
299 wxClientDC dc(this);
300 dc.SetPen(*wxBLACK_PEN);
301 dc.SetBrush(*wxTRANSPARENT_BRUSH);
302 dc.DrawRectangle(wxPoint(0,0), GetSize());
303 dc.SetPen(*wxRED_PEN);
304 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
305#endif
306
3c1f8cb1
VZ
307 // here we need to resize the pane window otherwise, even if the GtkExpander container
308 // is expanded or shrinked, the pane window won't be updated!
912c3932 309 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1
VZ
310
311 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
312 // (even if SetAutoLayout(true) has been called on it!)
313 m_pPane->Layout();
314}
315
99d625fc 316#endif // wxUSE_COLLPANE && defined(__WXGTK24__) && !defined(__WXUNIVERSAL__)
3c1f8cb1 317