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