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