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