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