cleanup (mainly wrapping lines to be < 80 chars); added IsExpanded()
[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 #ifdef __WXGTK24__
21
22 #include "wx/collpane.h"
23
24 #include <gtk/gtkexpander.h>
25 #include <gtk/gtk.h>
26
27 const wxChar wxCollapsiblePaneNameStr[] = wxT("CollapsiblePane");
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 //-----------------------------------------------------------------------------
34 // "notify::expanded" signal
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38
39 static void gtk_collapsiblepane_expanded_callback (GObject *object,
40 GParamSpec *param_spec,
41 wxCollapsiblePane *p)
42 {
43 // NB: unlike for the "activate" signal, when this callback is called, if
44 // we try to query the "collapsed" status through p->IsCollapsed(), we
45 // get the right value. I.e. here p->IsCollapsed() will return false if
46 // this callback has been called at the end of a collapsed->expanded
47 // transition and viceversa. Inside the "activate" signal callback
48 // p->IsCollapsed() would return the wrong value!
49
50 wxSize sz;
51 if ( p->IsExpanded() )
52 {
53 // unfortunately there's no clean way to retrieve the minimal size of
54 // the expanded pane in this handler or in other handlers for the
55 // signals generated by user clicks on the GtkExpander button:
56 // p->GetBestSize() or p->GetMinSize() would still return the size for
57 // the collapsed expander even if the collapsed->expanded transition
58 // has already been completed (this because GTK+ queues some resize
59 // calls which still must be processed). So, the only solution to
60 // correctly set the size hints for this window is to calculate the
61 // expanded size ourselves, without relying on p->Get[Best|Min]Size:
62 sz = p->GetMinSize();
63 sz.SetWidth(wxMax(sz.x, p->GetPane()->GetMinSize().x));
64 sz.SetHeight(sz.y + p->GetPane()->GetMinSize().y + 10);
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 // minimal size has priority over the best size so set here our min size
76 p->SetMinSize(sz);
77 p->SetSize(sz);
78
79 wxWindow *top = p->GetTopLevelParent();
80 if (top)
81 {
82 // we've changed our size, thus our top level parent needs to relayout
83 // itself
84 top->Layout();
85
86 if (p->IsExpanded())
87 {
88 // force our parent to "fit", i.e. expand so that it can honour
89 // our minimal size
90 top->Fit();
91 }
92 else // correctly
93 {
94 if (top->GetSizer())
95 top->GetSizer()->SetSizeHints(top);
96
97 // use SetClientSize() and not SetSize() otherwise the size for
98 // e.g. a wxFrame with a menubar wouldn't be correctly set
99 top->SetClientSize(sz);
100 }
101 }
102
103 if ( p->m_bIgnoreNextChange )
104 {
105 // change generated programmatically - do not send an event!
106 p->m_bIgnoreNextChange = false;
107 return;
108 }
109
110 // fire an event
111 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
112 p->GetEventHandler()->ProcessEvent(ev);
113 }
114 }
115
116 static void
117 gtk_collapsiblepane_insert_callback(wxWindowGTK* parent, wxWindowGTK* child)
118 {
119 // this callback should be used only once to insert the "pane" into the
120 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
121 // it has been called only once (and in any case we would get a warning
122 // from the following call as GtkExpander is a GtkBin and can contain only
123 // a single child!).
124 gtk_container_add (GTK_CONTAINER (parent->m_widget), child->m_widget);
125 }
126
127 //-----------------------------------------------------------------------------
128 // wxCollapsiblePane
129 //-----------------------------------------------------------------------------
130
131 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxGenericCollapsiblePane)
132
133 BEGIN_EVENT_TABLE(wxCollapsiblePane, wxGenericCollapsiblePane)
134 EVT_SIZE(wxCollapsiblePane::OnSize)
135 END_EVENT_TABLE()
136
137 bool wxCollapsiblePane::Create(wxWindow *parent,
138 wxWindowID id,
139 const wxString& label,
140 const wxPoint& pos,
141 const wxSize& size,
142 long style,
143 const wxValidator& val,
144 const wxString& name)
145 {
146 if (gtk_check_version(2,4,0))
147 return wxGenericCollapsiblePane::Create(parent, id, label,
148 pos, size, style, val, name);
149
150 m_needParent = true;
151 m_acceptsFocus = true;
152 m_bIgnoreNextChange = false;
153
154 if ( !PreCreation( parent, pos, size ) ||
155 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
156 {
157 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
158 return false;
159 }
160
161 m_widget = gtk_expander_new(label.c_str());
162
163 // see the gtk_collapsiblepane_expanded_callback comments to understand why
164 // we connect to the "notify::expanded" signal instead of the more common
165 // "activate" one
166 g_signal_connect(m_widget, "notify::expanded",
167 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
168
169 // before creating m_pPane, we need to makesure our own insert callback
170 // will be used
171 m_insertCallback = gtk_collapsiblepane_insert_callback;
172
173 // this the real "pane"
174 m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
175 wxNO_BORDER);
176
177 gtk_widget_show( GTK_WIDGET(m_widget) );
178 m_parent->DoAddChild( this );
179
180 PostCreation(size);
181
182 // remember the size of this control when it's collapsed
183 m_szCollapsed = GetBestSize();
184
185 return true;
186 }
187
188 wxSize wxCollapsiblePane::DoGetBestSize() const
189 {
190 if (!gtk_check_version(2,4,0))
191 {
192 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
193
194 GtkRequisition req;
195 req.width = 2;
196 req.height = 2;
197 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
198 (m_widget, &req );
199
200 // notice that we do not cache our best size here as it changes
201 return wxSize(req.width, req.height);
202 }
203
204 return wxGenericCollapsiblePane::DoGetBestSize();
205 }
206
207 void wxCollapsiblePane::Collapse(bool collapse)
208 {
209 if (!gtk_check_version(2,4,0))
210 {
211 // optimization
212 if (IsCollapsed() == collapse)
213 return;
214
215 // do not send event in next signal handler call
216 m_bIgnoreNextChange = true;
217 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
218 }
219 else
220 wxGenericCollapsiblePane::Collapse(collapse);
221 }
222
223 bool wxCollapsiblePane::IsCollapsed() const
224 {
225 if (!gtk_check_version(2,4,0))
226 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
227
228 return wxGenericCollapsiblePane::IsCollapsed();
229 }
230
231 void wxCollapsiblePane::SetLabel(const wxString &str)
232 {
233 if (!gtk_check_version(2,4,0))
234 gtk_expander_set_label(GTK_EXPANDER(m_widget), str.c_str());
235 else
236 wxGenericCollapsiblePane::SetLabel(str);
237 }
238
239 void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
240 {
241 #if 0 // for debug only
242 wxClientDC dc(this);
243 dc.SetPen(*wxBLACK_PEN);
244 dc.SetBrush(*wxTRANSPARENT_BRUSH);
245 dc.DrawRectangle(wxPoint(0,0), GetSize());
246 dc.SetPen(*wxRED_PEN);
247 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
248 #endif
249
250 // here we need to resize the pane window otherwise, even if the GtkExpander container
251 // is expanded or shrinked, the pane window won't be updated!
252 m_pPane->SetSize(ev.GetSize());
253
254 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
255 // (even if SetAutoLayout(true) has been called on it!)
256 m_pPane->Layout();
257 }
258
259 #endif // __WXGTK24__
260