Also report events from wxCollapsiblePane's label
[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 "wx/gtk/private.h"
28
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
32 const char wxCollapsiblePaneNameStr[] = "collapsiblePane";
33
34 wxDEFINE_EVENT( wxEVT_COMMAND_COLLPANE_CHANGED, wxCollapsiblePaneEvent )
35
36 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 //-----------------------------------------------------------------------------
43 // "notify::expanded" signal
44 //-----------------------------------------------------------------------------
45
46 extern "C" {
47
48 static void
49 gtk_collapsiblepane_expanded_callback(GObject * WXUNUSED(object),
50 GParamSpec * WXUNUSED(param_spec),
51 wxCollapsiblePane *p)
52 {
53 if (!p->IsCollapsed())
54 {
55 if (p->GetPane()->GetSizer())
56 p->GetPane()->GetSizer()->Fit( p->GetPane() );
57 }
58 }
59 }
60
61 extern "C" {
62 static void
63 gtk_collpane_map_unmap_callback( GtkWidget *WXUNUSED(pane), GdkEvent *WXUNUSED(event), wxCollapsiblePane* p )
64 {
65 if (p->HasFlag(wxCP_NO_TLW_RESIZE))
66 {
67 // fire an event
68 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
69 p->HandleWindowEvent(ev);
70
71 // the user asked to explicitely handle the resizing itself...
72 return;
73 }
74
75 wxTopLevelWindow *
76 top = wxDynamicCast(wxGetTopLevelParent(p), wxTopLevelWindow);
77 if ( top && top->GetSizer() )
78 {
79 // 2) recalculate minimal size of the top window
80 wxSize sz = top->GetSizer()->CalcMin();
81
82 if (top->m_mainWidget)
83 {
84 // 3) MAGIC HACK: if you ever used GtkExpander in a GTK+ program
85 // you know that this magic call is required to make it possible
86 // to shrink the top level window in the expanded->collapsed
87 // transition. This may be sometimes undesired but *is*
88 // necessary and if you look carefully, all GTK+ programs using
89 // GtkExpander perform this trick (e.g. the standard "open file"
90 // dialog of GTK+>=2.4 is not resizeable when the expander is
91 // collapsed!)
92 gtk_window_set_resizable (GTK_WINDOW (top->m_widget), p->IsExpanded());
93
94 // 4) set size hints
95 top->SetMinClientSize(sz);
96
97 // 5) set size
98 top->SetClientSize(sz);
99 }
100 }
101
102 if ( p->m_bIgnoreNextChange )
103 {
104 // change generated programmatically - do not send an event!
105 p->m_bIgnoreNextChange = false;
106 return;
107 }
108
109 // fire an event
110 wxCollapsiblePaneEvent ev(p, p->GetId(), p->IsCollapsed());
111 p->HandleWindowEvent(ev);
112 }
113 }
114
115
116 void wxCollapsiblePane::AddChildGTK(wxWindowGTK* child)
117 {
118 // should be used only once to insert the "pane" into the
119 // GtkExpander widget. wxGenericCollapsiblePane::DoAddChild() will check if
120 // it has been called only once (and in any case we would get a warning
121 // from the following call as GtkExpander is a GtkBin and can contain only
122 // a single child!).
123 gtk_container_add(GTK_CONTAINER(m_widget), child->m_widget);
124 }
125
126 //-----------------------------------------------------------------------------
127 // wxCollapsiblePane
128 //-----------------------------------------------------------------------------
129
130 IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePane, wxControl)
131
132 BEGIN_EVENT_TABLE(wxCollapsiblePane, wxCollapsiblePaneBase)
133 EVT_SIZE(wxCollapsiblePane::OnSize)
134 END_EVENT_TABLE()
135
136 bool wxCollapsiblePane::Create(wxWindow *parent,
137 wxWindowID id,
138 const wxString& label,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxValidator& val,
143 const wxString& name)
144 {
145 m_bIgnoreNextChange = false;
146
147 if ( !PreCreation( parent, pos, size ) ||
148 !wxControl::CreateBase(parent, id, pos, size, style, val, name) )
149 {
150 wxFAIL_MSG( wxT("wxCollapsiblePane creation failed") );
151 return false;
152 }
153
154 m_widget =
155 gtk_expander_new_with_mnemonic(wxGTK_CONV(GTKConvertMnemonics(label)));
156 g_object_ref(m_widget);
157
158 g_signal_connect_after(m_widget, "notify::expanded",
159 G_CALLBACK(gtk_collapsiblepane_expanded_callback), this);
160
161 // this the real "pane"
162 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
163 wxTAB_TRAVERSAL|wxNO_BORDER, wxT("wxCollapsiblePanePane") );
164
165 gtk_widget_show(m_widget);
166 m_parent->DoAddChild( this );
167
168 PostCreation(size);
169
170 // remember the size of this control when it's collapsed
171 GtkRequisition req;
172 req.width = 2;
173 req.height = 2;
174 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
175 (m_widget, &req );
176
177 m_szCollapsed = wxSize( req.width, req.height );
178
179 g_signal_connect (m_pPane->m_widget, "map_event",
180 G_CALLBACK (gtk_collpane_map_unmap_callback), this);
181 g_signal_connect (m_pPane->m_widget, "unmap_event",
182 G_CALLBACK (gtk_collpane_map_unmap_callback), this);
183
184
185 return true;
186 }
187
188 wxSize wxCollapsiblePane::DoGetBestSize() const
189 {
190 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
191
192 wxSize sz = m_szCollapsed;
193 if ( IsExpanded() )
194 {
195 wxSize panesz = GetPane()->GetBestSize();
196 sz.x = wxMax(sz.x, panesz.x);
197 sz.y += gtk_expander_get_spacing(GTK_EXPANDER(m_widget)) + panesz.y;
198 }
199
200 return sz;
201 }
202
203 GdkWindow *wxCollapsiblePane::GTKGetWindow(wxArrayGdkWindows& windows) const
204 {
205 GtkWidget *label = gtk_expander_get_label_widget( GTK_EXPANDER(m_widget) );
206 windows.Add( label->window );
207 windows.Add( m_widget->window );
208
209 return NULL;
210 }
211
212 void wxCollapsiblePane::Collapse(bool collapse)
213 {
214 // optimization
215 if (IsCollapsed() == collapse)
216 return;
217
218 // do not send event in next signal handler call
219 m_bIgnoreNextChange = true;
220 gtk_expander_set_expanded(GTK_EXPANDER(m_widget), !collapse);
221 }
222
223 bool wxCollapsiblePane::IsCollapsed() const
224 {
225 return !gtk_expander_get_expanded(GTK_EXPANDER(m_widget));
226 }
227
228 void wxCollapsiblePane::SetLabel(const wxString &str)
229 {
230 gtk_expander_set_label(GTK_EXPANDER(m_widget), wxGTK_CONV(str));
231
232 // FIXME: we need to update our collapsed width in some way but using GetBestSize()
233 // we may get the size of the control with the pane size summed up if we are expanded!
234 //m_szCollapsed.x = GetBestSize().x;
235 }
236
237 void wxCollapsiblePane::OnSize(wxSizeEvent &ev)
238 {
239 #if 0 // for debug only
240 wxClientDC dc(this);
241 dc.SetPen(*wxBLACK_PEN);
242 dc.SetBrush(*wxTRANSPARENT_BRUSH);
243 dc.DrawRectangle(wxPoint(0,0), GetSize());
244 dc.SetPen(*wxRED_PEN);
245 dc.DrawRectangle(wxPoint(0,0), GetBestSize());
246 #endif
247
248 // here we need to resize the pane window otherwise, even if the GtkExpander container
249 // is expanded or shrinked, the pane window won't be updated!
250 m_pPane->SetSize(ev.GetSize().x, ev.GetSize().y - m_szCollapsed.y);
251
252 // we need to explicitely call m_pPane->Layout() or else it won't correctly relayout
253 // (even if SetAutoLayout(true) has been called on it!)
254 m_pPane->Layout();
255 }
256
257 #endif // wxUSE_COLLPANE && !defined(__WXUNIVERSAL__)
258