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