]> git.saurik.com Git - wxWidgets.git/blame - src/generic/collpaneg.cpp
move Ellipsize() to wxControl so it can be easily used by other controls
[wxWidgets.git] / src / generic / collpaneg.cpp
CommitLineData
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"
912c3932 19#include "wx/defs.h"
2cbf7014 20
912c3932 21#if wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE
360c6a85 22
912c3932 23#include "wx/collpane.h"
687b91d2 24
2cbf7014 25#ifndef WX_PRECOMP
360c6a85 26 #include "wx/toplevel.h"
2cbf7014 27 #include "wx/button.h"
4ec3100a 28 #include "wx/sizer.h"
42a2ba5e 29 #include "wx/panel.h"
2cbf7014
VZ
30#endif // !WX_PRECOMP
31
3c1f8cb1
VZ
32#include "wx/statline.h"
33
2cbf7014
VZ
34// ----------------------------------------------------------------------------
35// constants
36// ----------------------------------------------------------------------------
37
3c1f8cb1
VZ
38// ============================================================================
39// implementation
40// ============================================================================
41
23318a53 42const char wxCollapsiblePaneNameStr[] = "collapsiblePane";
3c1f8cb1
VZ
43
44//-----------------------------------------------------------------------------
45// wxGenericCollapsiblePane
46//-----------------------------------------------------------------------------
47
48DEFINE_EVENT_TYPE(wxEVT_COMMAND_COLLPANE_CHANGED)
49IMPLEMENT_DYNAMIC_CLASS(wxGenericCollapsiblePane, wxControl)
50IMPLEMENT_DYNAMIC_CLASS(wxCollapsiblePaneEvent, wxCommandEvent)
51
52BEGIN_EVENT_TABLE(wxGenericCollapsiblePane, wxControl)
2cbf7014 53 EVT_BUTTON(wxID_ANY, wxGenericCollapsiblePane::OnButton)
3c1f8cb1 54 EVT_SIZE(wxGenericCollapsiblePane::OnSize)
68b1c878
VZ
55
56 WX_EVENT_TABLE_CONTROL_CONTAINER(wxGenericCollapsiblePane)
3c1f8cb1
VZ
57END_EVENT_TABLE()
58
68b1c878
VZ
59WX_DELEGATE_TO_CONTROL_CONTAINER(wxGenericCollapsiblePane, wxControl)
60
61void 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}
3c1f8cb1 70
2cbf7014
VZ
71bool 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)
3c1f8cb1
VZ
79{
80 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
81 return false;
82
83 m_strLabel = label;
84
96f04e1d 85#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
47922888 86 // on Mac we use the disclosure triangle
f2412e6a 87 // we need a light gray line above and below, lets approximate with the frame
47922888 88 m_pStaticLine = NULL;
23318a53 89 m_pButton = new wxDisclosureTriangle( this, wxID_ANY, GetBtnLabel(),
f2412e6a
SC
90 wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER );
91 m_pButton->SetBackgroundColour( wxColour( 221, 226, 239 ) );
47922888
RR
92 m_sz = new wxBoxSizer(wxHORIZONTAL);
93 // m_sz->Add(4,4); where shall we put it?
f2412e6a 94 m_sz->Add( m_pButton, 1);
47922888 95#else
912c3932
VZ
96 // create children and lay them out using a wxBoxSizer
97 // (so that we automatically get RTL features)
2cbf7014 98 m_pButton = new wxButton(this, wxID_ANY, GetBtnLabel(), wxPoint(0, 0),
3c1f8cb1 99 wxDefaultSize, wxBU_EXACTFIT);
912c3932 100 m_pStaticLine = new wxStaticLine(this, wxID_ANY);
912c3932
VZ
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
56eebcda
VZ
107 // FIXME: at least under wxCE and wxGTK1 the background is black if we don't do
108 // this, no idea why...
ff654490 109#if defined(__WXWINCE__) || defined(__WXGTK__)
948f4c37
WS
110 SetBackgroundColour(parent->GetBackgroundColour());
111#endif
112
912c3932 113 // do not set sz as our sizers since we handle the pane window without using sizers
037c7b4c
RD
114 m_pPane = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
115 wxTAB_TRAVERSAL|wxNO_BORDER);
3c1f8cb1
VZ
116
117 // start as collapsed:
118 m_pPane->Hide();
119
3c1f8cb1
VZ
120 return true;
121}
122
912c3932 123wxGenericCollapsiblePane::~wxGenericCollapsiblePane()
3c1f8cb1 124{
47922888 125 if (m_pButton)
912c3932 126 m_pButton->SetContainingSizer(NULL);
23318a53 127
47922888 128 if (m_pStaticLine)
912c3932 129 m_pStaticLine->SetContainingSizer(NULL);
23318a53 130
47922888
RR
131 // our sizer is not deleted automatically since we didn't use SetSizer()!
132 wxDELETE(m_sz);
912c3932
VZ
133}
134
135wxSize wxGenericCollapsiblePane::DoGetBestSize() const
136{
137 // NB: do not use GetSize() but rather GetMinSize()
138 wxSize sz = m_sz->GetMinSize();
3c1f8cb1
VZ
139
140 // when expanded, we need more vertical space
550d433e 141 if ( IsExpanded() )
912c3932
VZ
142 {
143 sz.SetWidth(wxMax( sz.GetWidth(), m_pPane->GetBestSize().x ));
144 sz.SetHeight(sz.y + GetBorder() + m_pPane->GetBestSize().y);
145 }
3c1f8cb1
VZ
146
147 return sz;
148}
149
150wxString wxGenericCollapsiblePane::GetBtnLabel() const
151{
f2412e6a
SC
152 // on mac the triangle indicates the state, no string change
153#ifdef __WXMAC__
154 return m_strLabel;
155#else
550d433e 156 return m_strLabel + (IsCollapsed() ? wxT(" >>") : wxT(" <<"));
f2412e6a 157#endif
3c1f8cb1
VZ
158}
159
4223cec5 160void wxGenericCollapsiblePane::OnStateChange(const wxSize& sz)
3c1f8cb1 161{
3c1f8cb1 162 // minimal size has priority over the best size so set here our min size
3c1f8cb1
VZ
163 SetMinSize(sz);
164 SetSize(sz);
165
912c3932
VZ
166 if (this->HasFlag(wxCP_NO_TLW_RESIZE))
167 {
168 // the user asked to explicitely handle the resizing itself...
169 return;
170 }
171
172
ed6008f4
VS
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);
3c1f8cb1
VZ
188}
189
4223cec5
VZ
190void 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
96f04e1d 200#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
f2412e6a 201 m_pButton->SetOpen( !collapse );
47922888 202#else
4223cec5
VZ
203 // NB: this must be done after updating our "state"
204 m_pButton->SetLabel(GetBtnLabel());
47922888
RR
205#endif
206
4223cec5
VZ
207
208 OnStateChange(GetBestSize());
209}
210
3c1f8cb1
VZ
211void wxGenericCollapsiblePane::SetLabel(const wxString &label)
212{
213 m_strLabel = label;
47922888
RR
214#ifdef __WXMAC__
215 m_pButton->SetLabel(GetBtnLabel());
216#else
3c1f8cb1 217 m_pButton->SetLabel(GetBtnLabel());
170acdc9 218 m_pButton->SetInitialSize();
47922888 219#endif
3c1f8cb1 220
912c3932 221 Layout();
3c1f8cb1
VZ
222}
223
912c3932 224bool wxGenericCollapsiblePane::Layout()
3c1f8cb1 225{
47922888
RR
226#ifdef __WXMAC__
227 if (!m_pButton || !m_pPane || !m_sz)
228 return false; // we need to complete the creation first!
229#else
912c3932
VZ
230 if (!m_pButton || !m_pStaticLine || !m_pPane || !m_sz)
231 return false; // we need to complete the creation first!
47922888 232#endif
912c3932
VZ
233
234 wxSize oursz(GetSize());
3c1f8cb1 235
912c3932
VZ
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();
3c1f8cb1 239
912c3932
VZ
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 }
3c1f8cb1 250
912c3932
VZ
251 return true;
252}
253
254int wxGenericCollapsiblePane::GetBorder() const
255{
256#if defined( __WXMAC__ )
257 return 6;
912c3932
VZ
258#elif defined(__WXMSW__)
259 wxASSERT(m_pButton);
260 return m_pButton->ConvertDialogToPixels(wxSize(2, 0)).x;
261#else
262 return 5;
263#endif
3c1f8cb1
VZ
264}
265
266
267
268//-----------------------------------------------------------------------------
269// wxGenericCollapsiblePane - event handlers
270//-----------------------------------------------------------------------------
271
2cbf7014 272void wxGenericCollapsiblePane::OnButton(wxCommandEvent& event)
3c1f8cb1 273{
2cbf7014
VZ
274 if ( event.GetEventObject() != m_pButton )
275 {
276 event.Skip();
277 return;
278 }
279
3c1f8cb1
VZ
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
287void 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
912c3932 298 Layout();
3c1f8cb1 299}
687b91d2 300
912c3932 301#endif // wxUSE_COLLPANE && wxUSE_BUTTON && wxUSE_STATLINE