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