]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/collpane.cpp
Copy recently added wxGenericValidator fields in Copy().
[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
ff654490 20#if wxUSE_COLLPANE && !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"
3c1f8cb1 28
ff654490
VZ
29// the lines below duplicate the same definitions in collpaneg.cpp, if we have
30// another implementation of this class we should extract them to a common file
31
23318a53 32const char wxCollapsiblePaneNameStr[] = "collapsiblePane";
ff654490 33
9b11752c 34wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED, wxCollapsiblePaneEvent );
ff654490
VZ
35
36IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
37
3c1f8cb1
VZ
38// ============================================================================
39// implementation
40// ============================================================================
41
3c1f8cb1
VZ
42//-----------------------------------------------------------------------------
43// "notify::expanded" signal
44//-----------------------------------------------------------------------------
45
46extern "C" {
47
e4161a2a
VZ
48static void
49gtk_collapsiblepane_expanded_callback(GObject * WXUNUSED(object),
50 GParamSpec * WXUNUSED(param_spec),
51 wxCollapsiblePane *p)
3c1f8cb1 52{
4f5dc9aa
FM
53 // NB: unlike for the "activate" signal, when this callback is called, if
54 // we try to query the "collapsed" status through p->IsCollapsed(), we
55 // get the right value. I.e. here p->IsCollapsed() will return false if
56 // this callback has been called at the end of a collapsed->expanded
57 // transition and viceversa. Inside the "activate" signal callback
58 // p->IsCollapsed() would return the wrong value!
59
60 wxSize sz;
61 if ( p->IsExpanded() )
3c1f8cb1 62 {
4f5dc9aa
FM
63 // NB: we cannot use the p->GetBestSize() or p->GetMinSize() functions
64 // here as they would return the size for the collapsed expander
65 // even if the collapsed->expanded transition has already been
66 // completed; we solve this problem doing:
67
68 sz = p->m_szCollapsed;
69
70 wxSize panesz = p->GetPane()->GetBestSize();
71 sz.x = wxMax(sz.x, panesz.x);
72 sz.y += gtk_expander_get_spacing(GTK_EXPANDER(p->m_widget)) + panesz.y;
73 }
74 else // collapsed
75 {
76 // same problem described above: using p->Get[Best|Min]Size() here we
77 // would get the size of the control when it is expanded even if the
78 // expanded->collapsed transition should be complete now...
79 // So, we use the size cached at control-creation time...
80 sz = p->m_szCollapsed;
3c1f8cb1
VZ
81 }
82
4f5dc9aa
FM
83 // VERY IMPORTANT:
84 // just calling
85 // p->OnStateChange(sz);
86 // here would work work BUT:
87 // 1) in the expanded->collapsed transition it provokes a lot of flickering
88 // 2) in the collapsed->expanded transition using the "Change status" wxButton
89 // in samples/collpane application some strange warnings would be generated
90 // by the "clearlooks" theme, if that's your theme.
91 //
92 // So we prefer to use some GTK+ native optimized calls, which prevent too many resize
93 // calculations to happen. Note that the following code has been very carefully designed
94 // and tested - be VERY careful when changing it!
95
96 // 1) need to update our size hints
97 // NB: this function call won't actually do any long operation
d13b34d3 98 // (redraw/relayout/resize) so that it's flicker-free
4f5dc9aa
FM
99 p->SetMinSize(sz);
100
912c3932
VZ
101 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
102 {
0514c6a2
RD
103 // fire an event
104 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
937013e0 105 p->HandleWindowEvent(ev);
74ab5f5b 106
4c51a665 107 // the user asked to explicitly handle the resizing itself...
912c3932
VZ
108 return;
109 }
5c234706 110
912c3932
VZ
111 wxTopLevelWindow *
112 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
113 if ( top && top->GetSizer() )
114 {
0ccf0043 115 // 2) recalculate minimal size of the top window
4f5dc9aa 116 sz = top->GetSizer()->CalcMin();
912c3932 117
912c3932
VZ
118 if (top->m_mainWidget)
119 {
2e10110a
VS
120 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program
121 // you know that this magic call is required to make it possible
122 // to shrink the top level window in the expanded->collapsed
123 // transition. This may be sometimes undesired but *is*
124 // necessary and if you look carefully, all GTK+ programs using
125 // GtkExpander perform this trick (e.g. the standard "open file"
d13b34d3 126 // dialog of GTK+>=2.4 is not resizable when the expander is
2e10110a 127 // collapsed!)
0ccf0043
RR
128 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
129
cca410b3 130 // 4) set size hints
2e10110a 131 top->SetMinClientSize(sz);
912c3932 132
cca410b3
PC
133 // 5) set size
134 top->SetClientSize(sz);
912c3932
VZ
135 }
136 }
0ccf0043 137
550d433e 138 if ( p->m_bIgnoreNextChange )
3c1f8cb1
VZ
139 {
140 // change generated programmatically - do not send an event!
141 p->m_bIgnoreNextChange = false;
142 return;
143 }
144
145 // fire an event
146 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
937013e0 147 p->HandleWindowEvent(ev);
3c1f8cb1
VZ
148}
149}
150
48200154 151void wxCollapsiblePane::AddChildGTK(wxWindowGTK* child)
3c1f8cb1 152{
48200154 153 // should be used only once to insert the "pane" into the
550d433e
VZ
154 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
155 // it has been called only once (and in any case we would get a warning
156 // from the following call as GtkExpander is a GtkBin and can contain only
157 // a single child!).
48200154 158 gtk_container_add(GTK_CONTAINER(m_widget), child->m_widget);
3c1f8cb1
VZ
159}
160
3c1f8cb1
VZ
161//-----------------------------------------------------------------------------
162// wxCollapsiblePane
163//-----------------------------------------------------------------------------
164
ff654490 165IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxControl)
3c1f8cb1 166
ff654490 167BEGIN_EVENT_TABLE(wxCollapsiblePane, wxCollapsiblePaneBase)
3c1f8cb1
VZ
168 EVT_SIZE(wxCollapsiblePane::OnSize)
169END_EVENT_TABLE()
170
550d433e
VZ
171bool wxCollapsiblePane::Create(wxWindow *parent,
172 wxWindowID id,
173 const wxString& label,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxValidator& val,
178 const wxString& name)
3c1f8cb1 179{
3c1f8cb1
VZ
180 m_bIgnoreNextChange = false;
181
550d433e
VZ
182 if ( !PreCreation( parent, pos, size ) ||
183 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
3c1f8cb1
VZ
184 {
185 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
186 return false;
187 }
188
ff928a27
VZ
189 m_widget =
190 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
9ff9d30c 191 g_object_ref(m_widget);
3c1f8cb1 192
4f5dc9aa
FM
193 // see the gtk_collapsiblepane_expanded_callback comments to understand why
194 // we connect to the "notify::expanded" signal instead of the more common
195 // "activate" one
196 g_signal_connect(m_widget, "notify::expanded",
3c1f8cb1 197 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
5c234706 198
3c1f8cb1 199 // this the real "pane"
037c7b4c 200 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
4f5dc9aa 201 wxTAB_TRAVERSAL|wxNO_BORDER, wxS("wxCollapsiblePanePane"));
3c1f8cb1 202
10bd1f7d 203 gtk_widget_show(m_widget);
3c1f8cb1 204 m_parent->DoAddChild( this );
5c234706 205
3c1f8cb1 206 PostCreation(size);
ce00f59b 207
5c234706
VZ
208 // we should blend into our parent background
209 const wxColour bg = parent->GetBackgroundColour();
210 SetBackgroundColour(bg);
211 m_pPane->SetBackgroundColour(bg);
212
3c1f8cb1 213 // remember the size of this control when it's collapsed
4f5dc9aa 214 m_szCollapsed = GetBestSize();
3c1f8cb1 215
3c1f8cb1
VZ
216 return true;
217}
218
219wxSize wxCollapsiblePane::DoGetBestSize() const
220{
ff654490 221 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
3c1f8cb1 222
4f5dc9aa
FM
223 GtkRequisition req;
224 req.width = 2;
225 req.height = 2;
226 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
227 (m_widget, &req );
5c234706 228
4f5dc9aa
FM
229 // notice that we do not cache our best size here as it changes
230 // all times the user expands/hide our pane
231 return wxSize(req.width, req.height);
69d32caf
RR
232}
233
3c1f8cb1
VZ
234void wxCollapsiblePane::Collapse(bool collapse)
235{
ff654490
VZ
236 // optimization
237 if (IsCollapsed() == collapse)
238 return;
3c1f8cb1 239
ff654490
VZ
240 // do not send event in next signal handler call
241 m_bIgnoreNextChange = true;
242 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
3c1f8cb1
VZ
243}
244
245bool wxCollapsiblePane::IsCollapsed() const
246{
ff654490 247 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
3c1f8cb1
VZ
248}
249
250void wxCollapsiblePane::SetLabel(const wxString &str)
251{
5a607f8b
VZ
252 gtk_expander_set_label(GTK_EXPANDER(m_widget),
253 wxGTK_CONV(GTKConvertMnemonics(str)));
912c3932 254
ff654490
VZ
255 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
256 // we may get the size of the control with the pane size summed up if we are expanded!
257 //m_szCollapsed.x = GetBestSize().x;
3c1f8cb1
VZ
258}
259
260void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
261{
262#if 0 // for debug only
263 wxClientDC dc(this);
264 dc.SetPen(*wxBLACK_PEN);
265 dc.SetBrush(*wxTRANSPARENT_BRUSH);
266 dc.DrawRectangle(wxPoint(0,0), GetSize());
267 dc.SetPen(*wxRED_PEN);
268 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
269#endif
270
3c1f8cb1 271 // here we need to resize the pane window otherwise, even if the GtkExpander container
5c234706 272 // is expanded or shrunk, the pane window won't be updated!
912c3932 273 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
3c1f8cb1 274
4c51a665 275 // we need to explicitly call m_pPane->Layout() or else it won't correctly relayout
3c1f8cb1
VZ
276 // (even if SetAutoLayout(true) has been called on it!)
277 m_pPane->Layout();
278}
279
4f5dc9aa
FM
280
281GdkWindow *wxCollapsiblePane::GTKGetWindow(wxArrayGdkWindows& windows) const
282{
283 GtkWidget *label = gtk_expander_get_label_widget( GTK_EXPANDER(m_widget) );
385e8575
PC
284 windows.Add(gtk_widget_get_window(label));
285 windows.Add(gtk_widget_get_window(m_widget));
4f5dc9aa
FM
286
287 return NULL;
ce00f59b 288}
4f5dc9aa 289
ff654490 290#endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)
3c1f8cb1 291