]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/foldbar/foldpanelitem.cpp
added foldbar contrib
[wxWidgets.git] / contrib / src / foldbar / foldpanelitem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxFoldPanelItem.cpp
3 // Author: XX
4 // Created: Tuesday, June 22, 2004 21:01:02
5 // Copyright: XX
6 /////////////////////////////////////////////////////////////////////////////
7
8 // For compilers that support precompilation, includes "wx/wx.h".
9 #include "wx/wxprec.h"
10
11 #ifdef __BORLANDC__
12 #pragma hdrstop
13 #endif
14
15 #include "wx/foldbar/foldpanelitem.h"
16
17 #include <wx/arrimpl.cpp>
18 WX_DEFINE_OBJARRAY(wxFoldWindowItemArray);
19
20 //----------------------------------------------------------------------------
21 // wxFoldPanelItem
22 //----------------------------------------------------------------------------
23
24 IMPLEMENT_CLASS( wxFoldPanelItem, wxPanel )
25
26 BEGIN_EVENT_TABLE(wxFoldPanelItem,wxPanel)
27 EVT_CAPTIONBAR(-1, wxFoldPanelItem::OnPressCaption)
28 EVT_PAINT(wxFoldPanelItem::OnPaint)
29 //EVT_SIZE(wxFoldPanelItem::OnSize)
30 END_EVENT_TABLE()
31
32 wxFoldPanelItem::wxFoldPanelItem( wxWindow *parent, const wxString &caption, wxImageList *icons, bool collapsedInitially,
33 const wxCaptionBarStyle &style )
34 : _controlCreated(false)
35 , _yUserSize(0)
36 , _yPanelSize(0)
37 , _yPos(0)
38 , _userSized(false)
39 , _yLastInsertPos(0)
40 {
41 wxCHECK2(parent, return);
42
43 wxPanel::Create(parent, -1);
44
45 // create the caption bar, in collapsed or expanded state
46
47 _captionBar = new wxCaptionBar(this, caption, icons, -1, style, wxPoint(0,0));
48 //_captionBar->SetBoldFont();
49
50 if(collapsedInitially)
51 _captionBar->Collapse();
52
53 _controlCreated = true;
54
55 // make initial size for component, if collapsed, the
56 // size is determined on the panel height and won't change
57
58 wxSize size = _captionBar->GetSize();
59 _yPanelSize = size.GetHeight();
60 _yLastInsertPos = _yPanelSize;
61 }
62
63 void wxFoldPanelItem::AddWindow(wxWindow *window, int flags, int ySpacing, int leftSpacing, int rightSpacing)
64 {
65 wxASSERT(window);
66
67 wxFoldWindowItem *wi = new wxFoldWindowItem(window, flags, ySpacing, leftSpacing, rightSpacing);
68 _items.Add(wi);
69
70 window->SetSize(leftSpacing, _yLastInsertPos + ySpacing, -1, -1, wxSIZE_USE_EXISTING);
71 _yLastInsertPos += wi->GetWindowHeight();
72
73 ResizePanel();
74 }
75
76 void wxFoldPanelItem::AddSeparator(const wxColour &color, int ySpacing, int leftSpacing, int rightSpacing)
77 {
78 wxFoldWindowItem *wi = new wxFoldWindowItem(_yLastInsertPos, color, ySpacing, leftSpacing, rightSpacing);
79 _items.Add(wi);
80
81 _yLastInsertPos += wi->GetWindowHeight();
82
83 ResizePanel();
84 }
85
86
87 wxFoldPanelItem::~wxFoldPanelItem()
88 {
89 _items.Clear();
90 }
91
92 void wxFoldPanelItem::OnPressCaption(wxCaptionBarEvent &event)
93 {
94 // tell the upper container we are responsible
95 // for this event, so it can fold the panel item
96 // and do a refresh
97
98 event.SetTag((void *)this);
99 event.Skip();
100 }
101
102 /* Inactive */
103 void wxFoldPanelItem::OnSize(wxSizeEvent &event)
104 {
105 // deny access to pointers (yet)
106
107 if(!_controlCreated)
108 {
109 event.Skip();
110 return;
111 }
112
113 // calculate the size needed for this window, so
114 // we get the parent size, and determine the size for the caption and the panel
115
116 //wxRect rect = GetRect();
117
118 //wxSize size(0,-1);
119 //size.SetWidth(rect.GetWidth());
120 //_captionBar->SetSize(size);
121
122 }
123
124 int wxFoldPanelItem::Reposition(int y)
125 {
126 // NOTE: Call Resize before Reposition when an item is added, because the new
127 // size needed will be calculated by Resize. Ofcourse the relative position
128 // of the controls have to be correct in respect to the caption bar
129
130 Freeze();
131
132 SetSize(-1, y, -1, -1, wxSIZE_USE_EXISTING);
133 _yPos = y;
134
135 Thaw();
136
137 return GetPanelHeight();
138 }
139
140 void wxFoldPanelItem::ResizePanel()
141 {
142 // prevent unnecessary updates by blocking repaints for a sec
143
144 Freeze();
145
146 // force this panel to take the width of the parent panel and the y of the
147 // user or calulated width (which will be recalculated by the contents here
148
149 wxSize size;
150 if(_captionBar->IsCollapsed())
151 {
152 size = _captionBar->GetSize();
153 _yPanelSize = size.GetHeight();
154 }
155 else
156 {
157 size = GetBestSize();
158 _yPanelSize = size.GetHeight();
159
160 if(_userSized)
161 size.SetHeight(_yUserSize);
162 }
163
164 wxSize pnlsize = GetParent()->GetSize();
165 size.SetWidth(pnlsize.GetWidth());
166
167 // resize caption bar
168 _captionBar->SetSize(wxSize(size.GetWidth(), -1));
169
170 // resize the panel
171 SetSize(size);
172
173 // go by all the controls and call Layout
174
175 for(size_t i = 0; i < _items.GetCount(); i++)
176 _items.Item(i).ResizeItem(size.GetWidth());
177
178 // and draw all
179
180 Thaw();
181 }
182
183 void wxFoldPanelItem::OnPaint(wxPaintEvent& WXUNUSED(event))
184 {
185 // draw all the items that are lines
186
187 wxPaintDC dc(this);
188
189 for(size_t i = 0; i < _items.GetCount(); i++)
190 {
191 wxFoldWindowItem &item = _items.Item(i);
192 wxPen pen(item.GetLineColour(), 1, wxSOLID);
193 if(item.GetType() == wxFoldWindowItem::SEPARATOR)
194 {
195 dc.SetPen(pen);
196 dc.DrawLine(item.GetLeftSpacing(), item.GetLineY() + item.GetYSpacing(),
197 item.GetLineWidth() + item.GetLeftSpacing(), item.GetLineY() + item.GetYSpacing());
198 }
199 }
200 }