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