]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/foldbar/foldpanelitem.cpp
use default position, not 0, 0
[wxWidgets.git] / contrib / src / foldbar / foldpanelitem.cpp
CommitLineData
957f5ab7 1/////////////////////////////////////////////////////////////////////////////
f857e441
WS
2// Name: foldpanelitem.cpp
3// Purpose:
4// Author: Jorgen Bodde
7a8d9418
WS
5// Modified by: ABX - 19/12/2004 : possibility of horizontal orientation
6// : wxWidgets coding standards
f857e441
WS
7// Created: 22/06/2004
8// RCS-ID: $Id$
9// Copyright: (c) Jorgen Bodde
10// Licence: wxWindows licence
957f5ab7
VZ
11/////////////////////////////////////////////////////////////////////////////
12
13// For compilers that support precompilation, includes "wx/wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
7a8d9418
WS
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif
23
24#include "wx/foldbar/foldpanelbar.h"
957f5ab7
VZ
25
26#include <wx/arrimpl.cpp>
27WX_DEFINE_OBJARRAY(wxFoldWindowItemArray);
28
29//----------------------------------------------------------------------------
30// wxFoldPanelItem
31//----------------------------------------------------------------------------
32
33IMPLEMENT_CLASS( wxFoldPanelItem, wxPanel )
34
35BEGIN_EVENT_TABLE(wxFoldPanelItem,wxPanel)
f857e441
WS
36 EVT_CAPTIONBAR(wxID_ANY, wxFoldPanelItem::OnPressCaption)
37 EVT_PAINT(wxFoldPanelItem::OnPaint)
38 //EVT_SIZE(wxFoldPanelItem::OnSize)
957f5ab7
VZ
39END_EVENT_TABLE()
40
f857e441
WS
41wxFoldPanelItem::wxFoldPanelItem( wxWindow *parent, const wxString &caption, wxImageList *icons, bool collapsedInitially,
42 const wxCaptionBarStyle &style )
7a8d9418
WS
43 : m_controlCreated(false)
44 , m_userSize(0)
45 , m_panelSize(0)
46 , m_lastInsertPos(0)
47 , m_itemPos(0)
48 , m_userSized(false)
957f5ab7
VZ
49{
50 wxCHECK2(parent, return);
f857e441
WS
51
52 wxPanel::Create(parent, wxID_ANY);
53
54 // create the caption bar, in collapsed or expanded state
55
7a8d9418
WS
56 m_captionBar = new wxCaptionBar(this, caption, icons, wxID_ANY, style, wxPoint(0,0));
57 //m_captionBar->SetBoldFont();
f857e441
WS
58
59 if(collapsedInitially)
7a8d9418 60 m_captionBar->Collapse();
f857e441 61
7a8d9418 62 m_controlCreated = true;
f857e441
WS
63
64 // make initial size for component, if collapsed, the
65 // size is determined on the panel height and won't change
66
7a8d9418
WS
67 wxSize size = m_captionBar->GetSize();
68 m_panelSize = IsVertical()?size.GetHeight():size.GetWidth();
69 m_lastInsertPos = m_panelSize;
957f5ab7 70}
f857e441 71
957f5ab7
VZ
72void wxFoldPanelItem::AddWindow(wxWindow *window, int flags, int ySpacing, int leftSpacing, int rightSpacing)
73{
f857e441
WS
74 wxASSERT(window);
75
76 wxFoldWindowItem *wi = new wxFoldWindowItem(window, flags, ySpacing, leftSpacing, rightSpacing);
7a8d9418
WS
77 m_items.Add(wi);
78
79 bool vertical = this->IsVertical();
80
81 window->SetSize( vertical ? leftSpacing : m_lastInsertPos + ySpacing,
82 vertical ? m_lastInsertPos + ySpacing : leftSpacing,
83 wxDefaultCoord,
84 wxDefaultCoord,
85 wxSIZE_USE_EXISTING);
f857e441 86
7a8d9418 87 m_lastInsertPos += wi->GetWindowLength( vertical );
f857e441
WS
88
89 ResizePanel();
957f5ab7
VZ
90}
91
92void wxFoldPanelItem::AddSeparator(const wxColour &color, int ySpacing, int leftSpacing, int rightSpacing)
f857e441 93{
7a8d9418
WS
94 wxFoldWindowItem *wi = new wxFoldWindowItem(m_lastInsertPos, color, ySpacing, leftSpacing, rightSpacing);
95 m_items.Add(wi);
f857e441 96
7a8d9418 97 m_lastInsertPos += wi->GetWindowLength( this->IsVertical() );
957f5ab7 98
f857e441 99 ResizePanel();
957f5ab7
VZ
100}
101
102
103wxFoldPanelItem::~wxFoldPanelItem()
104{
7a8d9418 105 m_items.Clear();
957f5ab7
VZ
106}
107
108void wxFoldPanelItem::OnPressCaption(wxCaptionBarEvent &event)
109{
f857e441
WS
110 // tell the upper container we are responsible
111 // for this event, so it can fold the panel item
112 // and do a refresh
957f5ab7 113
f857e441
WS
114 event.SetTag((void *)this);
115 event.Skip();
957f5ab7
VZ
116}
117
118/* Inactive */
119void wxFoldPanelItem::OnSize(wxSizeEvent &event)
120{
f857e441 121 // deny access to pointers (yet)
957f5ab7 122
7a8d9418 123 if(!m_controlCreated)
f857e441
WS
124 {
125 event.Skip();
126 return;
127 }
957f5ab7 128
f857e441
WS
129 // calculate the size needed for this window, so
130 // we get the parent size, and determine the size for the caption and the panel
957f5ab7 131
f857e441
WS
132 //wxRect rect = GetRect();
133
134 //wxSize size(0,wxDefaultCoord);
135 //size.SetWidth(rect.GetWidth());
7a8d9418 136 //m_captionBar->SetSize(size);
957f5ab7
VZ
137
138}
139
7a8d9418 140int wxFoldPanelItem::Reposition(int pos)
957f5ab7 141{
f857e441
WS
142 // NOTE: Call Resize before Reposition when an item is added, because the new
143 // size needed will be calculated by Resize. Ofcourse the relative position
144 // of the controls have to be correct in respect to the caption bar
145
146 Freeze();
147
7a8d9418
WS
148 bool vertical = this->IsVertical();
149
150 SetSize( vertical ? wxDefaultCoord : pos,
151 vertical ? pos : wxDefaultCoord,
152 wxDefaultCoord,
153 wxDefaultCoord,
154 wxSIZE_USE_EXISTING);
155
156 m_itemPos = pos;
f857e441
WS
157
158 Thaw();
159
7a8d9418 160 return GetPanelLength();
957f5ab7
VZ
161}
162
163void wxFoldPanelItem::ResizePanel()
164{
7a8d9418
WS
165 bool vertical = IsVertical();
166
f857e441
WS
167 // prevent unnecessary updates by blocking repaints for a sec
168
169 Freeze();
957f5ab7 170
f857e441
WS
171 // force this panel to take the width of the parent panel and the y of the
172 // user or calulated width (which will be recalculated by the contents here
957f5ab7 173
f857e441 174 wxSize size;
7a8d9418 175 if(m_captionBar->IsCollapsed())
f857e441 176 {
7a8d9418
WS
177 size = m_captionBar->GetSize();
178 m_panelSize = vertical ? size.GetHeight() : size.GetWidth();
f857e441
WS
179 }
180 else
181 {
182 size = GetBestSize();
7a8d9418 183 m_panelSize = vertical ? size.GetHeight() : size.GetWidth();
957f5ab7 184
7a8d9418
WS
185 if(m_userSized)
186 {
187 if ( vertical )
188 size.SetHeight(m_userSize);
189 else
190 size.SetWidth(m_userSize);
191 }
f857e441 192 }
957f5ab7 193
f857e441 194 wxSize pnlsize = GetParent()->GetSize();
7a8d9418
WS
195 if ( vertical )
196 size.SetWidth(pnlsize.GetWidth());
197 else
198 size.SetHeight(pnlsize.GetHeight());
957f5ab7 199
f857e441 200 // resize caption bar
7a8d9418
WS
201 m_captionBar->SetSize( vertical ? size.GetWidth() : wxDefaultCoord,
202 vertical ? wxDefaultCoord : size.GetHeight());
957f5ab7 203
f857e441
WS
204 // resize the panel
205 SetSize(size);
957f5ab7 206
f857e441 207 // go by all the controls and call Layout
957f5ab7 208
7a8d9418
WS
209 for(size_t i = 0; i < m_items.GetCount(); i++)
210 m_items.Item(i).ResizeItem( vertical ? size.GetWidth() : size.GetHeight() , vertical );
957f5ab7 211
f857e441 212 // and draw all
957f5ab7 213
f857e441 214 Thaw();
957f5ab7
VZ
215}
216
217void wxFoldPanelItem::OnPaint(wxPaintEvent& WXUNUSED(event))
218{
f857e441 219 // draw all the items that are lines
f857e441 220 wxPaintDC dc(this);
7a8d9418 221 bool vertical = IsVertical();
f857e441 222
7a8d9418 223 for(size_t i = 0; i < m_items.GetCount(); i++)
f857e441 224 {
7a8d9418 225 wxFoldWindowItem &item = m_items.Item(i);
f857e441
WS
226 wxPen pen(item.GetLineColour(), 1, wxSOLID);
227 if(item.GetType() == wxFoldWindowItem::SEPARATOR)
228 {
229 dc.SetPen(pen);
7a8d9418
WS
230 int a = item.GetLeftSpacing();
231 int b = item.GetLineY() + item.GetSpacing();
232 int c = item.GetLineLength();
233 int d = a + c;
234 if (vertical)
235 dc.DrawLine(a, b, d, b);
236 else
237 dc.DrawLine(b, a, b, d);
f857e441
WS
238 }
239 }
957f5ab7 240}
7a8d9418
WS
241
242bool wxFoldPanelItem::IsVertical() const
243{
244 // grandparent of wxFoldPanelItem is wxFoldPanelBar
245 // default is vertical
246 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
247 wxCHECK_MSG( panel, true, _T("wrong parent") );
248 wxFoldPanelBar *bar = wxDynamicCast(panel->GetParent(), wxFoldPanelBar);
249 wxCHECK_MSG( bar, true, _T("wrong parent") );
250 return bar->IsVertical();
251}