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