Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_SW_INC_HTMLTBL_HXX
21 : #define INCLUDED_SW_INC_HTMLTBL_HXX
22 :
23 : #include <vcl/timer.hxx>
24 : #include <editeng/svxenum.hxx>
25 :
26 : #include "swtypes.hxx"
27 : #include "node.hxx"
28 :
29 : class SwTableBox;
30 : class SwTable;
31 : class SwHTMLTableLayout;
32 : class SwDoc;
33 : class SwFrmFmt;
34 :
35 : #define HTMLTABLE_RESIZE_NOW (ULONG_MAX)
36 :
37 : class SwHTMLTableLayoutCnts
38 : {
39 : SwHTMLTableLayoutCnts *pNext; ///< The next content.
40 :
41 : /// Only one of the following two pointers may be set!
42 : SwTableBox *pBox; ///< A Box.
43 : SwHTMLTableLayout *pTable; ///< A "table within a table".
44 :
45 : /** During first run there are still no boxes. In this case
46 : pStartNode is used instead of pBox. */
47 : const SwStartNode *pStartNode;
48 :
49 : /** The following counters indicate how often a pass has been
50 : done for this content. Therefore they are compared against
51 : a reference value. If 255 is reached the continue with 0.
52 : This avoids reinitialization on every resize. */
53 : sal_uInt8 nPass1Done; ///< How many times has Pass 1 been called?
54 : sal_uInt8 nWidthSet; ///< How many times has the width been set?
55 :
56 : bool bNoBreakTag; ///< <NOBR>-Tag over complete content.
57 :
58 : public:
59 :
60 : SwHTMLTableLayoutCnts( const SwStartNode* pSttNd, SwHTMLTableLayout* pTab,
61 : bool bNoBreakTag, SwHTMLTableLayoutCnts* pNxt );
62 :
63 : ~SwHTMLTableLayoutCnts();
64 :
65 0 : void SetTableBox( SwTableBox *pBx ) { pBox = pBx; }
66 0 : SwTableBox *GetTableBox() const { return pBox; }
67 :
68 0 : SwHTMLTableLayout *GetTable() const { return pTable; }
69 :
70 : const SwStartNode *GetStartNode() const;
71 :
72 : /// Calculation of next node.
73 0 : SwHTMLTableLayoutCnts *GetNext() const { return pNext; }
74 :
75 0 : void SetWidthSet( sal_uInt8 nRef ) { nWidthSet = nRef; }
76 0 : sal_Bool IsWidthSet( sal_uInt8 nRef ) const { return nRef==nWidthSet; }
77 :
78 0 : void SetPass1Done( sal_uInt8 nRef ) { nPass1Done = nRef; }
79 0 : sal_Bool IsPass1Done( sal_uInt8 nRef ) const { return nRef==nPass1Done; }
80 :
81 0 : bool HasNoBreakTag() const { return bNoBreakTag; }
82 : };
83 :
84 : class SwHTMLTableLayoutCell
85 : {
86 : SwHTMLTableLayoutCnts *pContents; ///< Content of cell.
87 :
88 : sal_uInt16 nRowSpan; ///< ROWSPAN of cell.
89 : sal_uInt16 nColSpan; ///< COLSPAN of cell.
90 : sal_uInt16 nWidthOption; ///< Given width of cell in Twip or %.
91 :
92 : bool bPrcWidthOption : 1; ///< nWidth is %-value.
93 : bool bNoWrapOption : 1; ///< NOWRAP-option.
94 :
95 : public:
96 :
97 : SwHTMLTableLayoutCell( SwHTMLTableLayoutCnts *pCnts,
98 : sal_uInt16 nRSpan, sal_uInt16 nCSpan,
99 : sal_uInt16 nWidthOpt, bool bPrcWdthOpt,
100 : bool nNWrapOpt );
101 :
102 : ~SwHTMLTableLayoutCell();
103 :
104 : /// Set or get content of a cell.
105 0 : void SetContents( SwHTMLTableLayoutCnts *pCnts ) { pContents = pCnts; }
106 0 : SwHTMLTableLayoutCnts *GetContents() const { return pContents; }
107 :
108 : inline void SetProtected();
109 :
110 : /// Set or get ROWSPAN/COLSPAN of cell.
111 0 : void SetRowSpan( sal_uInt16 nRSpan ) { nRowSpan = nRSpan; }
112 0 : sal_uInt16 GetRowSpan() const { return nRowSpan; }
113 0 : sal_uInt16 GetColSpan() const { return nColSpan; }
114 :
115 0 : sal_uInt16 GetWidthOption() const { return nWidthOption; }
116 0 : bool IsPrcWidthOption() const { return bPrcWidthOption; }
117 :
118 0 : bool HasNoWrapOption() const { return bNoWrapOption; }
119 : };
120 :
121 : class SwHTMLTableLayoutColumn
122 : {
123 :
124 : /// Interim values of AutoLayoutPass1,
125 : sal_uLong nMinNoAlign, nMaxNoAlign, nAbsMinNoAlign;
126 :
127 : /// Results of AutoLayoutPass1
128 : sal_uLong nMin, nMax;
129 :
130 : /// Results of Pass 2.
131 : sal_uInt16 nAbsColWidth; ///< In Twips.
132 : sal_uInt16 nRelColWidth; ///< In Twips or relative to USHRT_MAX.
133 :
134 : sal_uInt16 nWidthOption; ///< Options of <COL> or <TD>/<TH>.
135 :
136 : bool bRelWidthOption : 1;
137 : bool bLeftBorder : 1;
138 :
139 : public:
140 :
141 : SwHTMLTableLayoutColumn( sal_uInt16 nColWidthOpt, bool bRelColWidthOpt,
142 : bool bLBorder );
143 :
144 0 : ~SwHTMLTableLayoutColumn() {}
145 :
146 : inline void MergeCellWidthOption( sal_uInt16 nWidth, bool bPrc );
147 : inline void SetWidthOption( sal_uInt16 nWidth, bool bRelWidth, bool bTest );
148 :
149 0 : sal_uInt16 GetWidthOption() const { return nWidthOption; }
150 0 : bool IsRelWidthOption() const { return bRelWidthOption; }
151 :
152 : inline void MergeMinMaxNoAlign( sal_uLong nMin, sal_uLong nMax, sal_uLong nAbsMin );
153 0 : sal_uLong GetMinNoAlign() const { return nMinNoAlign; }
154 0 : sal_uLong GetMaxNoAlign() const { return nMaxNoAlign; }
155 0 : sal_uLong GetAbsMinNoAlign() const { return nAbsMinNoAlign; }
156 : inline void ClearPass1Info( bool bWidthOpt );
157 :
158 : inline void SetMinMax( sal_uLong nMin, sal_uLong nMax );
159 0 : void SetMax( sal_uLong nVal ) { nMax = nVal; }
160 0 : void AddToMin( sal_uLong nVal ) { nMin += nVal; }
161 0 : void AddToMax( sal_uLong nVal ) { nMax += nVal; }
162 0 : sal_uLong GetMin() const { return nMin; }
163 0 : sal_uLong GetMax() const { return nMax; }
164 :
165 0 : void SetAbsColWidth( sal_uInt16 nWidth ) { nAbsColWidth = nWidth; }
166 0 : sal_uInt16 GetAbsColWidth() const { return nAbsColWidth; }
167 :
168 0 : void SetRelColWidth( sal_uInt16 nWidth ) { nRelColWidth = nWidth; }
169 0 : sal_uInt16 GetRelColWidth() const { return nRelColWidth; }
170 :
171 0 : bool HasLeftBorder() const { return bLeftBorder; }
172 : };
173 :
174 : class SwHTMLTableLayout
175 : {
176 : Timer aResizeTimer; ///< Timer for DelayedResize.
177 :
178 : SwHTMLTableLayoutColumn **aColumns;
179 : SwHTMLTableLayoutCell **aCells;
180 :
181 : const SwTable *pSwTable; ///< SwTable (Top-Table only).
182 : SwTableBox *pLeftFillerBox; ///< Left filler-box (table in table only).
183 : SwTableBox *pRightFillerBox; ///< Right filler-box (table in Table only).
184 :
185 : sal_uLong nMin; ///< Minimal width of table (Twips).
186 : sal_uLong nMax; ///< Maximal width of table (Twips).
187 :
188 : sal_uInt16 nRows; ///< Row count.
189 : sal_uInt16 nCols; ///< Column count.
190 :
191 : sal_uInt16 nLeftMargin; ///< Space to left margin (from paragraph).
192 : sal_uInt16 nRightMargin; ///< Space to left margin (from paragraph).
193 :
194 : sal_uInt16 nInhAbsLeftSpace; ///< Space inherited from surrounding box
195 : sal_uInt16 nInhAbsRightSpace; ///< that was added to boxes.
196 :
197 : sal_uInt16 nRelLeftFill; ///< Width of boxes relative to alignment
198 : sal_uInt16 nRelRightFill; ///< of tables in tables.
199 :
200 : sal_uInt16 nRelTabWidth; ///< Relative width of table.
201 :
202 : sal_uInt16 nWidthOption; ///< Width of table (in Twips oder %).
203 : sal_uInt16 nCellPadding; ///< Space to contents (in Twips).
204 : sal_uInt16 nCellSpacing; ///< Cell spacing (in Twips).
205 : sal_uInt16 nBorder; /** Line strength of outer border, or rather the
206 : space needed for it as calculated by Netscape. */
207 :
208 : sal_uInt16 nLeftBorderWidth;
209 : sal_uInt16 nRightBorderWidth;
210 : sal_uInt16 nInhLeftBorderWidth;
211 : sal_uInt16 nInhRightBorderWidth;
212 : sal_uInt16 nBorderWidth;
213 :
214 : sal_uInt16 nDelayedResizeAbsAvail; ///< Param for delayed Resize.
215 : sal_uInt16 nLastResizeAbsAvail;
216 :
217 : sal_uInt8 nPass1Done; ///< Reference-values for
218 : sal_uInt8 nWidthSet; ///< the runs through loop.
219 :
220 : SvxAdjust eTableAdjust; ///< Alignment of table.
221 :
222 : bool bColsOption : 1; ///< Table has a COLS-option.
223 : bool bColTags : 1; ///< Tabelle has COL/COLGRP-tags.
224 : bool bPrcWidthOption : 1; ///< Width is given in percent.
225 : bool bUseRelWidth : 1; ///< SwTable gets relative width.
226 :
227 : sal_Bool bMustResize : 1; ///< Table width must be defined.
228 : sal_Bool bExportable : 1; ///< Layout may be used for export.
229 : sal_Bool bBordersChanged : 1; ///< Borders have been changed.
230 : sal_Bool bMayBeInFlyFrame : 1; ///< Table could be within frame.
231 :
232 : sal_Bool bDelayedResizeRecalc : 1; ///< Param for delayed Resize.
233 : sal_Bool bMustNotResize : 1; ///< Table may not be resized.
234 : sal_Bool bMustNotRecalc : 1; ///< Table may not be adapted to its contents.
235 :
236 : void AddBorderWidth( sal_uLong &rMin, sal_uLong &rMax, sal_uLong& rAbsMin,
237 : sal_uInt16 nCol, sal_uInt16 nColSpan,
238 : sal_Bool bSwBorders=sal_True ) const;
239 : void SetBoxWidth( SwTableBox *pBox, sal_uInt16 nCol, sal_uInt16 nColSpan ) const;
240 :
241 : const SwStartNode *GetAnyBoxStartNode() const;
242 : SwFrmFmt *FindFlyFrmFmt() const;
243 0 : const SwDoc *GetDoc() const { return GetAnyBoxStartNode()->GetDoc(); }
244 :
245 0 : void ClearPass1Info() { nMin = nMax = 0; }
246 :
247 : void _Resize( sal_uInt16 nAbsAvail, sal_Bool bRecalc=sal_False );
248 :
249 : DECL_STATIC_LINK( SwHTMLTableLayout, DelayedResize_Impl, void* );
250 :
251 : static sal_uInt16 GetBrowseWidthByVisArea( const SwDoc& rDoc );
252 : public:
253 :
254 : SwHTMLTableLayout( const SwTable *pSwTbl,
255 : sal_uInt16 nRows, sal_uInt16 nCols, bool bColsOpt, bool ColTgs,
256 : sal_uInt16 nWidth, bool bPrcWidth, sal_uInt16 nBorderOpt,
257 : sal_uInt16 nCellPad, sal_uInt16 nCellSp, SvxAdjust eAdjust,
258 : sal_uInt16 nLMargin, sal_uInt16 nRMargin, sal_uInt16 nBWidth,
259 : sal_uInt16 nLeftBWidth, sal_uInt16 nRightBWidth,
260 : sal_uInt16 nInhLeftBWidth, sal_uInt16 nInhRightBWidth );
261 :
262 : ~SwHTMLTableLayout();
263 :
264 : sal_uInt16 GetLeftCellSpace( sal_uInt16 nCol, sal_uInt16 nColSpan,
265 : sal_Bool bSwBorders=sal_True ) const;
266 : sal_uInt16 GetRightCellSpace( sal_uInt16 nCol, sal_uInt16 nColSpan,
267 : sal_Bool bSwBorders=sal_True ) const;
268 : inline sal_uInt16 GetInhCellSpace( sal_uInt16 nCol, sal_uInt16 nColSpan ) const;
269 :
270 : inline void SetInhBorderWidths( sal_uInt16 nLeft, sal_uInt16 nRight );
271 :
272 : void GetAvail( sal_uInt16 nCol, sal_uInt16 nColSpan, sal_uInt16& rAbsAvail,
273 : sal_uInt16& rRelAvail ) const;
274 :
275 : void AutoLayoutPass1();
276 : void AutoLayoutPass2( sal_uInt16 nAbsAvail, sal_uInt16 nRelAvail,
277 : sal_uInt16 nAbsLeftSpace, sal_uInt16 nAbsRightSpace,
278 : sal_uInt16 nParentInhSpace );
279 : void SetWidths( sal_Bool bCallPass2=sal_False, sal_uInt16 nAbsAvail=0,
280 : sal_uInt16 nRelAvail=0, sal_uInt16 nAbsLeftSpace=0,
281 : sal_uInt16 nAbsRightSpace=0,
282 : sal_uInt16 nParentInhSpace=0 );
283 :
284 : inline SwHTMLTableLayoutColumn *GetColumn( sal_uInt16 nCol ) const;
285 : inline void SetColumn( SwHTMLTableLayoutColumn *pCol, sal_uInt16 nCol );
286 :
287 : inline SwHTMLTableLayoutCell *GetCell( sal_uInt16 nRow, sal_uInt16 nCol ) const;
288 : inline void SetCell( SwHTMLTableLayoutCell *pCell, sal_uInt16 nRow, sal_uInt16 nCol );
289 :
290 0 : void SetLeftFillerBox( SwTableBox *pBox ) { pLeftFillerBox = pBox; }
291 0 : void SetRightFillerBox( SwTableBox *pBox ) { pRightFillerBox = pBox; }
292 :
293 0 : sal_uLong GetMin() const { return nMin; }
294 0 : sal_uLong GetMax() const { return nMax; }
295 0 : sal_uInt16 GetRelLeftFill() const { return nRelLeftFill; }
296 0 : sal_uInt16 GetRelRightFill() const { return nRelRightFill; }
297 :
298 : inline long GetBrowseWidthMin() const;
299 :
300 0 : bool HasColsOption() const { return bColsOption; }
301 0 : bool HasColTags() const { return bColTags; }
302 :
303 0 : bool IsTopTable() const { return pSwTable != 0; }
304 :
305 0 : void SetMustResize( sal_Bool bSet ) { bMustResize = bSet; }
306 0 : void SetMustNotResize( sal_Bool bSet ) { bMustNotResize = bSet; }
307 0 : void SetMustNotRecalc( sal_Bool bSet ) { bMustNotRecalc = bSet; }
308 :
309 : /** Recalculation of table widths for available width that has been passed.
310 : - If bRecalc is set, contents of boxes are included into calculation.
311 : - If bForce is set, table will be recalculated even if this was
312 : disallowed by SetMustNotResize.
313 : - If nDelay > 0 the calculation is delayed accordingly. Resizing calls
314 : occuring during delay-time are ignored, but the delay may be counted
315 : under certain circumstances.
316 : - If nDelay == HTMLTABLE_RESIZE_NOW, resize immediately and do not
317 : consider any resize-calls that might possibly be in order.
318 : - The return value indicates whether the table has been changed. */
319 : sal_Bool Resize( sal_uInt16 nAbsAvail, sal_Bool bRecalc=sal_False, sal_Bool bForce=sal_False,
320 : sal_uLong nDelay=0 );
321 :
322 : void BordersChanged( sal_uInt16 nAbsAvail, sal_Bool bRecalc=sal_False );
323 :
324 : /** Calculate available width. This works only if a layout or a
325 : SwViewShell exists. Otherwise returns 0.
326 : This is needed by HTML-filter because it doesn't have access to the layout.) */
327 : static sal_uInt16 GetBrowseWidth( const SwDoc& rDoc );
328 :
329 : /// Calculates available width by table-frame.
330 : sal_uInt16 GetBrowseWidthByTabFrm( const SwTabFrm& rTabFrm ) const;
331 :
332 : /** Calculates available width by the table-frame or
333 : static GetBrowseWidth if no layout exists. */
334 : sal_uInt16 GetBrowseWidthByTable( const SwDoc& rDoc ) const;
335 :
336 : /// For Export.
337 0 : sal_uInt16 GetWidthOption() const { return nWidthOption; }
338 0 : bool HasPrcWidthOption() const { return bPrcWidthOption; }
339 :
340 0 : sal_uInt16 GetCellPadding() const { return nCellPadding; }
341 0 : sal_uInt16 GetCellSpacing() const { return nCellSpacing; }
342 0 : sal_uInt16 GetBorder() const { return nBorder; }
343 :
344 0 : sal_uInt16 GetRowCount() const { return nRows; }
345 0 : sal_uInt16 GetColCount() const { return nCols; }
346 :
347 0 : void SetExportable( sal_Bool bSet ) { bExportable = bSet; }
348 0 : sal_Bool IsExportable() const { return bExportable; }
349 :
350 0 : sal_Bool HaveBordersChanged() const { return bBordersChanged; }
351 :
352 0 : void SetMayBeInFlyFrame( sal_Bool bSet ) { bMayBeInFlyFrame = bSet; }
353 0 : sal_Bool MayBeInFlyFrame() const { return bMayBeInFlyFrame; }
354 : };
355 :
356 0 : inline void SwHTMLTableLayoutCell::SetProtected()
357 : {
358 0 : nRowSpan = 1;
359 0 : nColSpan = 1;
360 :
361 0 : pContents = 0;
362 0 : }
363 :
364 0 : inline void SwHTMLTableLayoutColumn::MergeMinMaxNoAlign( sal_uLong nCMin,
365 : sal_uLong nCMax, sal_uLong nAbsMin )
366 : {
367 0 : if( nCMin > nMinNoAlign )
368 0 : nMinNoAlign = nCMin;
369 0 : if( nCMax > nMaxNoAlign )
370 0 : nMaxNoAlign = nCMax;
371 0 : if( nAbsMin > nAbsMinNoAlign )
372 0 : nAbsMinNoAlign = nAbsMin;
373 0 : }
374 :
375 0 : inline void SwHTMLTableLayoutColumn::ClearPass1Info( bool bWidthOpt )
376 : {
377 0 : nMinNoAlign = nMaxNoAlign = nAbsMinNoAlign = MINLAY;
378 0 : nMin = nMax = 0;
379 0 : if( bWidthOpt )
380 : {
381 0 : nWidthOption = 0;
382 0 : bRelWidthOption = false;
383 : }
384 0 : }
385 :
386 0 : inline void SwHTMLTableLayoutColumn::MergeCellWidthOption(
387 : sal_uInt16 nWidth, bool bRel )
388 : {
389 0 : if( !nWidthOption ||
390 0 : (bRel==bRelWidthOption && nWidthOption < nWidth) )
391 : {
392 0 : nWidthOption = nWidth;
393 0 : bRelWidthOption = bRel;
394 : }
395 0 : }
396 :
397 0 : inline void SwHTMLTableLayoutColumn::SetMinMax( sal_uLong nMn, sal_uLong nMx )
398 : {
399 0 : nMin = nMn;
400 0 : nMax = nMx;
401 0 : }
402 :
403 0 : inline sal_uInt16 SwHTMLTableLayout::GetInhCellSpace( sal_uInt16 nCol,
404 : sal_uInt16 nColSpan ) const
405 : {
406 0 : sal_uInt16 nSpace = 0;
407 0 : if( nCol==0 )
408 0 : nSpace = nSpace + sal::static_int_cast< sal_uInt16 >(nInhAbsLeftSpace);
409 0 : if( nCol+nColSpan==nCols )
410 0 : nSpace = nSpace + sal::static_int_cast< sal_uInt16 >(nInhAbsRightSpace);
411 :
412 0 : return nSpace;
413 : }
414 :
415 0 : inline SwHTMLTableLayoutColumn *SwHTMLTableLayout::GetColumn( sal_uInt16 nCol ) const
416 : {
417 0 : return aColumns[nCol];
418 : }
419 :
420 0 : inline void SwHTMLTableLayoutColumn::SetWidthOption(
421 : sal_uInt16 nWidth, bool bRelWidth, bool bTest )
422 : {
423 0 : if( bTest && bRelWidthOption==bRelWidth )
424 : {
425 0 : if( nWidth > nWidthOption )
426 0 : nWidthOption = nWidth;
427 : }
428 : else
429 0 : nWidthOption = nWidth;
430 0 : bRelWidthOption = bRelWidth;
431 0 : }
432 :
433 0 : inline void SwHTMLTableLayout::SetColumn( SwHTMLTableLayoutColumn *pCol, sal_uInt16 nCol )
434 : {
435 0 : aColumns[nCol] = pCol;
436 0 : }
437 :
438 0 : inline SwHTMLTableLayoutCell *SwHTMLTableLayout::GetCell( sal_uInt16 nRow, sal_uInt16 nCol ) const
439 : {
440 0 : return aCells[nRow*nCols+nCol];
441 : }
442 :
443 0 : inline void SwHTMLTableLayout::SetCell( SwHTMLTableLayoutCell *pCell,
444 : sal_uInt16 nRow, sal_uInt16 nCol )
445 : {
446 0 : aCells[nRow*nCols+nCol] = pCell;
447 0 : }
448 :
449 0 : inline long SwHTMLTableLayout::GetBrowseWidthMin() const
450 : {
451 0 : return (long)( (!nWidthOption || bPrcWidthOption) ? nMin : nRelTabWidth );
452 : }
453 :
454 0 : void SwHTMLTableLayout::SetInhBorderWidths( sal_uInt16 nLeft, sal_uInt16 nRight )
455 : {
456 0 : nInhLeftBorderWidth = nLeft;
457 0 : nInhRightBorderWidth = nRight;
458 0 : }
459 :
460 : #endif
461 :
462 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|