]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_list.cpp
wxVariant is in wxBase so should be forward declared using WXDLLIMPEXP_BASE and not...
[wxWidgets.git] / src / html / m_list.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: m_list.cpp
3 // Purpose: wxHtml module for lists
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
10 #pragma implementation
11 #endif
12
13 #include "wx/wxprec.h"
14
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include "wx/brush.h"
25 #include "wx/dc.h"
26 #endif
27
28 #include "wx/html/forcelnk.h"
29 #include "wx/html/m_templ.h"
30
31 #include "wx/html/htmlcell.h"
32
33 FORCE_LINK_ME(m_list)
34
35
36 //-----------------------------------------------------------------------------
37 // wxHtmlListmarkCell
38 //-----------------------------------------------------------------------------
39
40 class wxHtmlListmarkCell : public wxHtmlCell
41 {
42 private:
43 wxBrush m_Brush;
44 public:
45 wxHtmlListmarkCell(wxDC *dc, const wxColour& clr);
46 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
47 wxHtmlRenderingInfo& info);
48
49 DECLARE_NO_COPY_CLASS(wxHtmlListmarkCell)
50 };
51
52 wxHtmlListmarkCell::wxHtmlListmarkCell(wxDC* dc, const wxColour& clr) : wxHtmlCell(), m_Brush(clr, wxSOLID)
53 {
54 m_Width = dc->GetCharHeight();
55 m_Height = dc->GetCharHeight();
56 // bottom of list mark is lined with bottom of letters in next cell
57 m_Descent = m_Height / 3;
58 }
59
60
61
62 void wxHtmlListmarkCell::Draw(wxDC& dc, int x, int y,
63 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
64 wxHtmlRenderingInfo& WXUNUSED(info))
65 {
66 dc.SetBrush(m_Brush);
67 dc.DrawEllipse(x + m_PosX + m_Width / 3, y + m_PosY + m_Height / 3,
68 (m_Width / 3), (m_Width / 3));
69 }
70
71 //-----------------------------------------------------------------------------
72 // wxHtmlListCell
73 //-----------------------------------------------------------------------------
74
75 struct wxHtmlListItemStruct
76 {
77 wxHtmlContainerCell *mark;
78 wxHtmlContainerCell *cont;
79 int minWidth;
80 int maxWidth;
81 };
82
83 class wxHtmlListCell : public wxHtmlContainerCell
84 {
85 private:
86 wxBrush m_Brush;
87
88 int m_NumRows;
89 wxHtmlListItemStruct *m_RowInfo;
90 void ReallocRows(int rows);
91 void ComputeMinMaxWidths();
92 int ComputeMaxBase(wxHtmlCell *cell);
93 int m_ListmarkWidth;
94
95 public:
96 wxHtmlListCell(wxHtmlContainerCell *parent);
97 virtual ~wxHtmlListCell();
98 void AddRow(wxHtmlContainerCell *mark, wxHtmlContainerCell *cont);
99 virtual void Layout(int w);
100
101 DECLARE_NO_COPY_CLASS(wxHtmlListCell)
102 };
103
104 wxHtmlListCell::wxHtmlListCell(wxHtmlContainerCell *parent) : wxHtmlContainerCell(parent)
105 {
106 m_NumRows = 0;
107 m_RowInfo = 0;
108 m_ListmarkWidth = 0;
109 }
110
111 wxHtmlListCell::~wxHtmlListCell()
112 {
113 if (m_RowInfo) free(m_RowInfo);
114 }
115
116 int wxHtmlListCell::ComputeMaxBase(wxHtmlCell *cell)
117 {
118 if(!cell)
119 return 0;
120
121 wxHtmlCell *child = cell->GetFirstChild();
122
123 while(child)
124 {
125 int base = ComputeMaxBase( child );
126 if ( base > 0 ) return base + child->GetPosY();
127 child = child->GetNext();
128 }
129
130 return cell->GetHeight() - cell->GetDescent();
131 }
132
133 void wxHtmlListCell::Layout(int w)
134 {
135 wxHtmlCell::Layout(w);
136
137 ComputeMinMaxWidths();
138 m_Width = wxMax(m_Width, wxMin(w, GetMaxTotalWidth()));
139
140 int s_width = m_Width - m_IndentLeft;
141
142 int vpos = 0;
143 for (int r = 0; r < m_NumRows; r++)
144 {
145 // do layout first time to layout contents and adjust pos
146 m_RowInfo[r].mark->Layout(m_ListmarkWidth);
147 m_RowInfo[r].cont->Layout(s_width - m_ListmarkWidth);
148
149 const int base_mark = ComputeMaxBase( m_RowInfo[r].mark );
150 const int base_cont = ComputeMaxBase( m_RowInfo[r].cont );
151 const int adjust_mark = vpos + wxMax(base_cont-base_mark,0);
152 const int adjust_cont = vpos + wxMax(base_mark-base_cont,0);
153
154 m_RowInfo[r].mark->SetPos(m_IndentLeft, adjust_mark);
155 m_RowInfo[r].cont->SetPos(m_IndentLeft + m_ListmarkWidth, adjust_cont);
156
157 vpos = wxMax(adjust_mark + m_RowInfo[r].mark->GetHeight(),
158 adjust_cont + m_RowInfo[r].cont->GetHeight());
159 }
160 m_Height = vpos;
161 }
162
163 void wxHtmlListCell::AddRow(wxHtmlContainerCell *mark, wxHtmlContainerCell *cont)
164 {
165 ReallocRows(++m_NumRows);
166 m_RowInfo[m_NumRows - 1].mark = mark;
167 m_RowInfo[m_NumRows - 1].cont = cont;
168 }
169
170 void wxHtmlListCell::ReallocRows(int rows)
171 {
172 m_RowInfo = (wxHtmlListItemStruct*) realloc(m_RowInfo, sizeof(wxHtmlListItemStruct) * rows);
173 m_RowInfo[rows - 1].mark = NULL;
174 m_RowInfo[rows - 1].cont = NULL;
175 m_RowInfo[rows - 1].minWidth = 0;
176 m_RowInfo[rows - 1].maxWidth = 0;
177
178 m_NumRows = rows;
179 }
180
181 void wxHtmlListCell::ComputeMinMaxWidths()
182 {
183 if (m_NumRows == 0) return;
184
185 m_MaxTotalWidth = 0;
186 m_Width = 0;
187
188 for (int r = 0; r < m_NumRows; r++)
189 {
190 wxHtmlListItemStruct& row = m_RowInfo[r];
191 row.mark->Layout(1);
192 row.cont->Layout(1);
193 int maxWidth = row.cont->GetMaxTotalWidth();
194 int width = row.cont->GetWidth();
195 if (row.mark->GetWidth() > m_ListmarkWidth)
196 m_ListmarkWidth = row.mark->GetWidth();
197 if (maxWidth > m_MaxTotalWidth)
198 m_MaxTotalWidth = maxWidth;
199 if (width > m_Width)
200 m_Width = width;
201 }
202 m_Width += m_ListmarkWidth + m_IndentLeft;
203 m_MaxTotalWidth += m_ListmarkWidth + m_IndentLeft;
204 }
205
206 //-----------------------------------------------------------------------------
207 // wxHtmlListcontentCell
208 //-----------------------------------------------------------------------------
209
210 class wxHtmlListcontentCell : public wxHtmlContainerCell
211 {
212 public:
213 wxHtmlListcontentCell(wxHtmlContainerCell *p) : wxHtmlContainerCell(p) {}
214 virtual void Layout(int w) {
215 // Reset top indentation, fixes <li><p>
216 SetIndent(0, wxHTML_INDENT_TOP);
217 wxHtmlContainerCell::Layout(w);
218 }
219 };
220
221 //-----------------------------------------------------------------------------
222 // The list handler:
223 //-----------------------------------------------------------------------------
224
225
226 TAG_HANDLER_BEGIN(OLULLI, "OL,UL,LI")
227
228 TAG_HANDLER_VARS
229 wxHtmlListCell *m_List;
230 int m_Numbering;
231 // this is number of actual item of list or 0 for dots
232
233 TAG_HANDLER_CONSTR(OLULLI)
234 {
235 m_List = NULL;
236 m_Numbering = 0;
237 }
238
239 TAG_HANDLER_PROC(tag)
240 {
241 wxHtmlContainerCell *c;
242
243 // List Item:
244 if (m_List && tag.GetName() == wxT("LI"))
245 {
246 c = m_WParser->SetContainer(new wxHtmlContainerCell(m_List));
247 c->SetAlignVer(wxHTML_ALIGN_TOP);
248
249 wxHtmlContainerCell *mark = c;
250 c->SetWidthFloat(2 * m_WParser->GetCharWidth(), wxHTML_UNITS_PIXELS);
251 if (m_Numbering == 0)
252 {
253 // Centering gives more space after the bullet
254 c->SetAlignHor(wxHTML_ALIGN_CENTER);
255 c->InsertCell(new wxHtmlListmarkCell(m_WParser->GetDC(), m_WParser->GetActualColor()));
256 }
257 else
258 {
259 c->SetAlignHor(wxHTML_ALIGN_RIGHT);
260 wxString mark;
261 mark.Printf(wxT("%i."), m_Numbering);
262 c->InsertCell(new wxHtmlWordCell(mark, *(m_WParser->GetDC())));
263 }
264 m_WParser->CloseContainer();
265
266 c = m_WParser->OpenContainer();
267
268 m_List->AddRow(mark, c);
269 c = m_WParser->OpenContainer();
270 m_WParser->SetContainer(new wxHtmlListcontentCell(c));
271
272 if (m_Numbering != 0) m_Numbering++;
273 }
274
275 // Begin of List (not-numbered): "UL", "OL"
276 else if (tag.GetName() == wxT("UL") || tag.GetName() == wxT("OL"))
277 {
278 int oldnum = m_Numbering;
279
280 if (tag.GetName() == wxT("UL")) m_Numbering = 0;
281 else m_Numbering = 1;
282
283 wxHtmlContainerCell *oldcont;
284 oldcont = c = m_WParser->OpenContainer();
285
286 wxHtmlListCell *oldList = m_List;
287 m_List = new wxHtmlListCell(c);
288 m_List->SetIndent(2 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
289
290 ParseInner(tag);
291
292 m_WParser->SetContainer(oldcont);
293 m_WParser->CloseContainer();
294
295 m_Numbering = oldnum;
296 m_List = oldList;
297 return true;
298 }
299 return false;
300
301 }
302
303 TAG_HANDLER_END(OLULLI)
304
305
306 TAGS_MODULE_BEGIN(List)
307
308 TAGS_MODULE_ADD(OLULLI)
309
310 TAGS_MODULE_END(List)
311
312 #endif