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