]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/collpaneg.cpp | |
3 | // Purpose: wxGenericCollapsiblePane | |
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 | #include "wx/defs.h" | |
20 | ||
21 | #if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE | |
22 | ||
23 | #include "wx/collpane.h" | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/toplevel.h" | |
27 | #include "wx/button.h" | |
28 | #include "wx/sizer.h" | |
29 | #include "wx/panel.h" | |
30 | #endif // !WX_PRECOMP | |
31 | ||
32 | #include "wx/statline.h" | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // constants | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | // ============================================================================ | |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
42 | const wxChar wxCollapsiblePaneNameStr[] = wxT("collapsiblePane"); | |
43 | ||
44 | //----------------------------------------------------------------------------- | |
45 | // wxGenericCollapsiblePane | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED) | |
49 | IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl) | |
50 | IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent) | |
51 | ||
52 | BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl) | |
53 | EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton) | |
54 | EVT_SIZE(wxGenericCollapsiblePane::OnSize) | |
55 | END_EVENT_TABLE() | |
56 | ||
57 | ||
58 | bool wxGenericCollapsiblePane::Create(wxWindow *parent, | |
59 | wxWindowID id, | |
60 | const wxString& label, | |
61 | const wxPoint& pos, | |
62 | const wxSize& size, | |
63 | long style, | |
64 | const wxValidator& val, | |
65 | const wxString& name) | |
66 | { | |
67 | if ( !wxControl::Create(parent, id, pos, size, style, val, name) ) | |
68 | return false; | |
69 | ||
70 | m_strLabel = label; | |
71 | ||
72 | #ifdef __WXMAC__ | |
73 | // on Mac we use the disclosure triangle | |
74 | // we need a light gray line above and below, lets approximate with the frame | |
75 | m_pStaticLine = NULL; | |
76 | m_pButton = new wxDisclosureTriangle( this, wxID_ANY, GetBtnLabel(), | |
77 | wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER ); | |
78 | m_pButton->SetBackgroundColour( wxColour( 221, 226, 239 ) ); | |
79 | m_sz = new wxBoxSizer(wxHORIZONTAL); | |
80 | // m_sz->Add(4,4); where shall we put it? | |
81 | m_sz->Add( m_pButton, 1); | |
82 | #else | |
83 | // create children and lay them out using a wxBoxSizer | |
84 | // (so that we automatically get RTL features) | |
85 | m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0), | |
86 | wxDefaultSize, wxBU_EXACTFIT); | |
87 | m_pStaticLine = new wxStaticLine(this, wxID_ANY); | |
88 | // on other platforms we put the static line and the button horizontally | |
89 | m_sz = new wxBoxSizer(wxHORIZONTAL); | |
90 | m_sz->Add(m_pButton, 0, wxLEFT|wxTOP|wxBOTTOM, GetBorder()); | |
91 | m_sz->Add(m_pStaticLine, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, GetBorder()); | |
92 | #endif | |
93 | ||
94 | // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do | |
95 | // this, no idea why... | |
96 | #if defined(__WXWINCE__) || defined(__WXGTK__) | |
97 | SetBackgroundColour(parent->GetBackgroundColour()); | |
98 | #endif | |
99 | ||
100 | // do not set sz as our sizers since we handle the pane window without using sizers | |
101 | m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, | |
102 | wxTAB_TRAVERSAL|wxNO_BORDER); | |
103 | ||
104 | // start as collapsed: | |
105 | m_pPane->Hide(); | |
106 | ||
107 | return true; | |
108 | } | |
109 | ||
110 | wxGenericCollapsiblePane::~wxGenericCollapsiblePane() | |
111 | { | |
112 | if (m_pButton) | |
113 | m_pButton->SetContainingSizer(NULL); | |
114 | ||
115 | if (m_pStaticLine) | |
116 | m_pStaticLine->SetContainingSizer(NULL); | |
117 | ||
118 | // our sizer is not deleted automatically since we didn't use SetSizer()! | |
119 | wxDELETE(m_sz); | |
120 | } | |
121 | ||
122 | wxSize wxGenericCollapsiblePane::DoGetBestSize() const | |
123 | { | |
124 | // NB: do not use GetSize() but rather GetMinSize() | |
125 | wxSize sz = m_sz->GetMinSize(); | |
126 | ||
127 | // when expanded, we need more vertical space | |
128 | if ( IsExpanded() ) | |
129 | { | |
130 | sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x )); | |
131 | sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y); | |
132 | } | |
133 | ||
134 | return sz; | |
135 | } | |
136 | ||
137 | wxString wxGenericCollapsiblePane::GetBtnLabel() const | |
138 | { | |
139 | // on mac the triangle indicates the state, no string change | |
140 | #ifdef __WXMAC__ | |
141 | return m_strLabel; | |
142 | #else | |
143 | return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<")); | |
144 | #endif | |
145 | } | |
146 | ||
147 | void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz) | |
148 | { | |
149 | // minimal size has priority over the best size so set here our min size | |
150 | SetMinSize(sz); | |
151 | SetSize(sz); | |
152 | ||
153 | if (this->HasFlag(wxCP_NO_TLW_RESIZE)) | |
154 | { | |
155 | // the user asked to explicitely handle the resizing itself... | |
156 | return; | |
157 | } | |
158 | ||
159 | ||
160 | // | |
161 | // NB: the following block of code has been accurately designed to | |
162 | // as much flicker-free as possible; be careful when modifying it! | |
163 | // | |
164 | ||
165 | wxTopLevelWindow * | |
166 | top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
167 | if ( top ) | |
168 | { | |
169 | // NB: don't Layout() the 'top' window as its size has not been correctly | |
170 | // updated yet and we don't want to do an initial Layout() with the old | |
171 | // size immediately followed by a SetClientSize/Fit call for the new | |
172 | // size; that would provoke flickering! | |
173 | ||
174 | if (top->GetSizer()) | |
175 | #ifdef __WXGTK__ | |
176 | // FIXME: the SetSizeHints() call would be required also for GTK+ for | |
177 | // the expanded->collapsed transition. Unfortunately if we | |
178 | // enable this line, then the GTK+ top window won't always be | |
179 | // resized by the SetClientSize() call below! As a side effect | |
180 | // of this dirty fix, the minimal size for the pane window is | |
181 | // not set in GTK+ and the user can hide it shrinking the "top" | |
182 | // window... | |
183 | if (IsCollapsed()) | |
184 | #endif | |
185 | top->GetSizer()->SetSizeHints(top); | |
186 | ||
187 | ||
188 | // we shouldn't attempt to resize a maximized window, whatever happens | |
189 | if ( !top->IsMaximized() ) | |
190 | { | |
191 | if ( IsCollapsed() ) | |
192 | { | |
193 | // expanded -> collapsed transition | |
194 | if (top->GetSizer()) | |
195 | { | |
196 | // we have just set the size hints... | |
197 | wxSize szClient = top->GetSizer()->CalcMin(); | |
198 | ||
199 | // use SetClientSize() and not SetSize() otherwise the size for | |
200 | // e.g. a wxFrame with a menubar wouldn't be correctly set | |
201 | top->SetClientSize(szClient); | |
202 | } | |
203 | else | |
204 | top->Layout(); | |
205 | } | |
206 | else | |
207 | { | |
208 | // collapsed -> expanded transition | |
209 | ||
210 | // force our parent to "fit", i.e. expand so that it can honour | |
211 | // our minimal size | |
212 | top->Fit(); | |
213 | } | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | void wxGenericCollapsiblePane::Collapse(bool collapse) | |
219 | { | |
220 | // optimization | |
221 | if ( IsCollapsed() == collapse ) | |
222 | return; | |
223 | ||
224 | // update our state | |
225 | m_pPane->Show(!collapse); | |
226 | ||
227 | // update button label | |
228 | #ifdef __WXMAC__ | |
229 | m_pButton->SetOpen( !collapse ); | |
230 | #else | |
231 | // NB: this must be done after updating our "state" | |
232 | m_pButton->SetLabel(GetBtnLabel()); | |
233 | #endif | |
234 | ||
235 | ||
236 | OnStateChange(GetBestSize()); | |
237 | } | |
238 | ||
239 | void wxGenericCollapsiblePane::SetLabel(const wxString &label) | |
240 | { | |
241 | m_strLabel = label; | |
242 | #ifdef __WXMAC__ | |
243 | m_pButton->SetLabel(GetBtnLabel()); | |
244 | #else | |
245 | m_pButton->SetLabel(GetBtnLabel()); | |
246 | m_pButton->SetInitialSize(); | |
247 | #endif | |
248 | ||
249 | Layout(); | |
250 | } | |
251 | ||
252 | bool wxGenericCollapsiblePane::Layout() | |
253 | { | |
254 | #ifdef __WXMAC__ | |
255 | if (!m_pButton || !m_pPane || !m_sz) | |
256 | return false; // we need to complete the creation first! | |
257 | #else | |
258 | if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz) | |
259 | return false; // we need to complete the creation first! | |
260 | #endif | |
261 | ||
262 | wxSize oursz(GetSize()); | |
263 | ||
264 | // move & resize the button and the static line | |
265 | m_sz->SetDimension(0, 0, oursz.GetWidth(), m_sz->GetMinSize().GetHeight()); | |
266 | m_sz->Layout(); | |
267 | ||
268 | if ( IsExpanded() ) | |
269 | { | |
270 | // move & resize the container window | |
271 | int yoffset = m_sz->GetSize().GetHeight() + GetBorder(); | |
272 | m_pPane->SetSize(0, yoffset, | |
273 | oursz.x, oursz.y - yoffset); | |
274 | ||
275 | // this is very important to make the pane window layout show correctly | |
276 | m_pPane->Layout(); | |
277 | } | |
278 | ||
279 | return true; | |
280 | } | |
281 | ||
282 | int wxGenericCollapsiblePane::GetBorder() const | |
283 | { | |
284 | #if defined( __WXMAC__ ) | |
285 | return 6; | |
286 | #elif defined(__WXMSW__) | |
287 | wxASSERT(m_pButton); | |
288 | return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x; | |
289 | #else | |
290 | return 5; | |
291 | #endif | |
292 | } | |
293 | ||
294 | ||
295 | ||
296 | //----------------------------------------------------------------------------- | |
297 | // wxGenericCollapsiblePane - event handlers | |
298 | //----------------------------------------------------------------------------- | |
299 | ||
300 | void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event) | |
301 | { | |
302 | if ( event.GetEventObject() != m_pButton ) | |
303 | { | |
304 | event.Skip(); | |
305 | return; | |
306 | } | |
307 | ||
308 | Collapse(!IsCollapsed()); | |
309 | ||
310 | // this change was generated by the user - send the event | |
311 | wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed()); | |
312 | GetEventHandler()->ProcessEvent(ev); | |
313 | } | |
314 | ||
315 | void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event)) | |
316 | { | |
317 | #if 0 // for debug only | |
318 | wxClientDC dc(this); | |
319 | dc.SetPen(*wxBLACK_PEN); | |
320 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
321 | dc.DrawRectangle(wxPoint(0,0), GetSize()); | |
322 | dc.SetPen(*wxRED_PEN); | |
323 | dc.DrawRectangle(wxPoint(0,0), GetBestSize()); | |
324 | #endif | |
325 | ||
326 | Layout(); | |
327 | } | |
328 | ||
329 | #endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE |