]>
Commit | Line | Data |
---|---|---|
3c1f8cb1 VZ |
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" | |
2cbf7014 | 19 | |
360c6a85 PC |
20 | #include "wx/collpane.h" |
21 | ||
2cbf7014 | 22 | #ifndef WX_PRECOMP |
360c6a85 | 23 | #include "wx/toplevel.h" |
2cbf7014 | 24 | #include "wx/button.h" |
4ec3100a | 25 | #include "wx/sizer.h" |
2cbf7014 VZ |
26 | #endif // !WX_PRECOMP |
27 | ||
3c1f8cb1 VZ |
28 | #include "wx/statline.h" |
29 | ||
2cbf7014 VZ |
30 | // ---------------------------------------------------------------------------- |
31 | // constants | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | // the number of pixels to leave between the button and the static line and | |
35 | // between the button and the pane | |
36 | #define wxCP_MARGIN 10 | |
37 | ||
3c1f8cb1 VZ |
38 | // ============================================================================ |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
42 | const wxChar wxGenericCollapsiblePaneNameStr[] = wxT("genericCollapsiblePane"); | |
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) | |
2cbf7014 | 53 | EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton) |
3c1f8cb1 VZ |
54 | EVT_SIZE(wxGenericCollapsiblePane::OnSize) |
55 | END_EVENT_TABLE() | |
56 | ||
57 | ||
2cbf7014 VZ |
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) | |
3c1f8cb1 VZ |
66 | { |
67 | if ( !wxControl::Create(parent, id, pos, size, style, val, name) ) | |
68 | return false; | |
69 | ||
70 | m_strLabel = label; | |
71 | ||
72 | // create children; their size & position is set in OnSize() | |
2cbf7014 | 73 | m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0), |
3c1f8cb1 VZ |
74 | wxDefaultSize, wxBU_EXACTFIT); |
75 | m_pStatLine = new wxStaticLine(this, wxID_ANY); | |
550d433e VZ |
76 | m_pPane = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
77 | wxNO_BORDER); | |
3c1f8cb1 VZ |
78 | |
79 | // start as collapsed: | |
80 | m_pPane->Hide(); | |
81 | ||
82 | //CacheBestSize(GetBestSize()); | |
83 | return true; | |
84 | } | |
85 | ||
86 | wxSize wxGenericCollapsiblePane::DoGetBestSize() const | |
87 | { | |
88 | wxSize sz = m_pButton->GetBestSize(); | |
89 | ||
90 | // set width | |
550d433e VZ |
91 | sz.SetWidth(sz.x + wxCP_MARGIN + m_pStatLine->GetBestSize().x); |
92 | const wxCoord paneWidth = m_pPane->GetBestSize().x; | |
93 | if ( sz.x < paneWidth ) | |
94 | sz.x = paneWidth; | |
3c1f8cb1 VZ |
95 | |
96 | // when expanded, we need more vertical space | |
550d433e VZ |
97 | if ( IsExpanded() ) |
98 | sz.SetHeight(sz.y + wxCP_MARGIN + m_pPane->GetBestSize().y); | |
3c1f8cb1 VZ |
99 | |
100 | return sz; | |
101 | } | |
102 | ||
103 | wxString wxGenericCollapsiblePane::GetBtnLabel() const | |
104 | { | |
550d433e | 105 | return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<")); |
3c1f8cb1 VZ |
106 | } |
107 | ||
4223cec5 | 108 | void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz) |
3c1f8cb1 | 109 | { |
3c1f8cb1 | 110 | // minimal size has priority over the best size so set here our min size |
3c1f8cb1 VZ |
111 | SetMinSize(sz); |
112 | SetSize(sz); | |
113 | ||
069a9976 VZ |
114 | wxTopLevelWindow * |
115 | top = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
116 | if ( top ) | |
3c1f8cb1 | 117 | { |
550d433e VZ |
118 | // we've changed our size, thus our top level parent needs to relayout |
119 | // itself | |
3c1f8cb1 VZ |
120 | top->Layout(); |
121 | ||
550d433e VZ |
122 | // FIXME: this makes wxGenericCollapsiblePane behave as the user expect |
123 | // but maybe there are cases where this is unwanted! | |
3c1f8cb1 VZ |
124 | if (top->GetSizer()) |
125 | #ifdef __WXGTK__ | |
550d433e VZ |
126 | // FIXME: the SetSizeHints() call would be required also for GTK+ for |
127 | // the expanded->collapsed transition. Unfortunately if we | |
128 | // enable this line, then the GTK+ top window won't always be | |
129 | // resized by the SetClientSize() call below! As a side effect | |
130 | // of this dirty fix, the minimal size for the pane window is | |
131 | // not set in GTK+ and the user can hide it shrinking the "top" | |
132 | // window... | |
3c1f8cb1 VZ |
133 | if (IsCollapsed()) |
134 | #endif | |
135 | top->GetSizer()->SetSizeHints(top); | |
136 | ||
069a9976 VZ |
137 | |
138 | // we shouldn't attempt to resize a maximized window, whatever happens | |
139 | if ( !top->IsMaximized() ) | |
3c1f8cb1 | 140 | { |
069a9976 VZ |
141 | if ( IsCollapsed() ) |
142 | { | |
143 | // use SetClientSize() and not SetSize() otherwise the size for | |
144 | // e.g. a wxFrame with a menubar wouldn't be correctly set | |
145 | top->SetClientSize(sz); | |
146 | } | |
147 | else | |
148 | { | |
149 | // force our parent to "fit", i.e. expand so that it can honour | |
150 | // our minimal size | |
151 | top->Fit(); | |
152 | } | |
3c1f8cb1 VZ |
153 | } |
154 | } | |
155 | } | |
156 | ||
4223cec5 VZ |
157 | void wxGenericCollapsiblePane::Collapse(bool collapse) |
158 | { | |
159 | // optimization | |
160 | if ( IsCollapsed() == collapse ) | |
161 | return; | |
162 | ||
163 | // update our state | |
164 | m_pPane->Show(!collapse); | |
165 | ||
166 | // update button label | |
167 | // NB: this must be done after updating our "state" | |
168 | m_pButton->SetLabel(GetBtnLabel()); | |
169 | ||
170 | OnStateChange(GetBestSize()); | |
171 | } | |
172 | ||
3c1f8cb1 VZ |
173 | void wxGenericCollapsiblePane::SetLabel(const wxString &label) |
174 | { | |
175 | m_strLabel = label; | |
176 | m_pButton->SetLabel(GetBtnLabel()); | |
177 | m_pButton->SetBestFittingSize(); | |
178 | ||
179 | LayoutChildren(); | |
180 | } | |
181 | ||
182 | void wxGenericCollapsiblePane::LayoutChildren() | |
183 | { | |
184 | wxSize btnSz = m_pButton->GetSize(); | |
185 | ||
186 | // the button position & size are always ok... | |
187 | ||
188 | // move & resize the static line | |
550d433e VZ |
189 | m_pStatLine->SetSize(btnSz.x + wxCP_MARGIN, btnSz.y/2, |
190 | GetSize().x - btnSz.x - wxCP_MARGIN, -1, | |
3c1f8cb1 VZ |
191 | wxSIZE_USE_EXISTING); |
192 | ||
193 | // move & resize the container window | |
550d433e VZ |
194 | m_pPane->SetSize(0, btnSz.y + wxCP_MARGIN, |
195 | GetSize().x, GetSize().y - btnSz.y - wxCP_MARGIN); | |
3c1f8cb1 VZ |
196 | } |
197 | ||
198 | ||
199 | ||
200 | //----------------------------------------------------------------------------- | |
201 | // wxGenericCollapsiblePane - event handlers | |
202 | //----------------------------------------------------------------------------- | |
203 | ||
2cbf7014 | 204 | void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event) |
3c1f8cb1 | 205 | { |
2cbf7014 VZ |
206 | if ( event.GetEventObject() != m_pButton ) |
207 | { | |
208 | event.Skip(); | |
209 | return; | |
210 | } | |
211 | ||
3c1f8cb1 VZ |
212 | Collapse(!IsCollapsed()); |
213 | ||
214 | // this change was generated by the user - send the event | |
215 | wxCollapsiblePaneEvent ev(this, GetId(), IsCollapsed()); | |
216 | GetEventHandler()->ProcessEvent(ev); | |
217 | } | |
218 | ||
219 | void wxGenericCollapsiblePane::OnSize(wxSizeEvent& WXUNUSED(event)) | |
220 | { | |
221 | #if 0 // for debug only | |
222 | wxClientDC dc(this); | |
223 | dc.SetPen(*wxBLACK_PEN); | |
224 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
225 | dc.DrawRectangle(wxPoint(0,0), GetSize()); | |
226 | dc.SetPen(*wxRED_PEN); | |
227 | dc.DrawRectangle(wxPoint(0,0), GetBestSize()); | |
228 | #endif | |
229 | ||
230 | ||
231 | if (!m_pButton || !m_pStatLine || !m_pPane) | |
232 | return; // we need to complete the creation first! | |
233 | ||
234 | LayoutChildren(); | |
235 | ||
236 | // this is very important to make the pane window layout show correctly | |
237 | m_pPane->Layout(); | |
238 | } |