1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <hintids.hxx>
22 #include <svl/eitem.hxx>
23 #include <tools/fract.hxx>
24 #include <editeng/lrspitem.hxx>
25 #include <editeng/ulspitem.hxx>
26 #include <editeng/sizeitem.hxx>
27 #include <svx/pageitem.hxx>
28 #include <editeng/brushitem.hxx>
29 #include <editeng/frmdiritem.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/settings.hxx>
32 #include <tgrditem.hxx>
33 #include <viewopt.hxx>
36 #include <svx/unobrushitemhelper.hxx>
38 void SwPageExample::UpdateExample( const SfxItemSet
& rSet
)
40 if (SfxItemState::DEFAULT
<= rSet
.GetItemState(RES_FRAMEDIR
))
42 const SvxFrameDirectionItem
& rDirItem
= rSet
.Get(RES_FRAMEDIR
);
43 m_bVertical
= rDirItem
.GetValue() == SvxFrameDirection::Vertical_RL_TB
||
44 rDirItem
.GetValue() == SvxFrameDirection::Vertical_LR_TB
;
47 SfxItemPool
* pPool
= rSet
.GetPool();
48 sal_uInt16 nWhich
= pPool
->GetWhich( SID_ATTR_PAGE
);
49 if ( rSet
.GetItemState( nWhich
, false ) == SfxItemState::SET
)
52 const SvxPageItem
& rPage
= static_cast<const SvxPageItem
&>(rSet
.Get(nWhich
));
53 SetUsage(rPage
.GetPageUsage());
56 nWhich
= pPool
->GetWhich( SID_ATTR_PAGE_SIZE
);
58 if ( rSet
.GetItemState( nWhich
, false ) == SfxItemState::SET
)
60 // orientation and size from PageItem
61 const SvxSizeItem
& rSize
= static_cast<const SvxSizeItem
&>(rSet
.Get( nWhich
));
62 SetSize( rSize
.GetSize() );
64 nWhich
= RES_LR_SPACE
;
65 if ( rSet
.GetItemState( nWhich
, false ) == SfxItemState::SET
)
67 // set left and right border
68 const SvxLRSpaceItem
& rLRSpace
= static_cast<const SvxLRSpaceItem
&>(rSet
.Get( nWhich
));
70 SetLeft( rLRSpace
.GetLeft() );
71 SetRight( rLRSpace
.GetRight() );
79 nWhich
= RES_UL_SPACE
;
81 if ( rSet
.GetItemState( nWhich
, false ) == SfxItemState::SET
)
83 // set upper and lower border
84 const SvxULSpaceItem
& rULSpace
= static_cast<const SvxULSpaceItem
&>(rSet
.Get( nWhich
));
86 SetTop( rULSpace
.GetUpper() );
87 SetBottom( rULSpace
.GetLower() );
95 // evaluate header-attributes
96 const SfxPoolItem
* pItem
;
97 if( SfxItemState::SET
== rSet
.GetItemState( pPool
->GetWhich( SID_ATTR_PAGE_HEADERSET
),
100 const SfxItemSet
& rHeaderSet
= static_cast<const SvxSetItem
*>(pItem
)->GetItemSet();
101 const SfxBoolItem
& rHeaderOn
=
102 rHeaderSet
.Get( pPool
->GetWhich( SID_ATTR_PAGE_ON
) );
104 if ( rHeaderOn
.GetValue() )
106 const SvxSizeItem
& rSize
=
107 rHeaderSet
.Get(pPool
->GetWhich(SID_ATTR_PAGE_SIZE
));
109 const SvxULSpaceItem
& rUL
= rHeaderSet
.Get(pPool
->GetWhich(SID_ATTR_ULSPACE
));
110 const SvxLRSpaceItem
& rLR
= rHeaderSet
.Get(pPool
->GetWhich(SID_ATTR_LRSPACE
));
112 SetHdHeight( rSize
.GetSize().Height() - rUL
.GetLower());
113 SetHdDist( rUL
.GetLower() );
114 SetHdLeft( rLR
.GetLeft() );
115 SetHdRight( rLR
.GetRight() );
118 if(SfxItemState::SET
== rHeaderSet
.GetItemState(RES_BACKGROUND
))
120 // create FillAttributes from SvxBrushItem //SetHdColor(rItem.GetColor());
121 const SvxBrushItem
& rItem
= rHeaderSet
.Get(RES_BACKGROUND
);
122 SfxItemSetFixed
<XATTR_FILL_FIRST
, XATTR_FILL_LAST
> aTempSet(*rHeaderSet
.GetPool());
124 setSvxBrushItemAsFillAttributesToTargetSet(rItem
, aTempSet
);
125 setHeaderFillAttributes(
126 std::make_shared
<drawinglayer::attribute::SdrAllFillAttributesHelper
>(
134 if( SfxItemState::SET
== rSet
.GetItemState( pPool
->GetWhich( SID_ATTR_PAGE_FOOTERSET
),
137 const SfxItemSet
& rFooterSet
= static_cast<const SvxSetItem
*>(pItem
)->GetItemSet();
138 const SfxBoolItem
& rFooterOn
= rFooterSet
.Get( SID_ATTR_PAGE_ON
);
140 if ( rFooterOn
.GetValue() )
142 const SvxSizeItem
& rSize
=
143 rFooterSet
.Get( pPool
->GetWhich( SID_ATTR_PAGE_SIZE
) );
145 const SvxULSpaceItem
& rUL
= rFooterSet
.Get(pPool
->GetWhich( SID_ATTR_ULSPACE
) );
146 const SvxLRSpaceItem
& rLR
= rFooterSet
.Get(pPool
->GetWhich( SID_ATTR_LRSPACE
) );
148 SetFtHeight( rSize
.GetSize().Height() - rUL
.GetUpper());
149 SetFtDist( rUL
.GetUpper() );
150 SetFtLeft( rLR
.GetLeft() );
151 SetFtRight( rLR
.GetRight() );
154 if( rFooterSet
.GetItemState( RES_BACKGROUND
) == SfxItemState::SET
)
156 // create FillAttributes from SvxBrushItem //SetFtColor(rItem.GetColor());
157 const SvxBrushItem
& rItem
= rFooterSet
.Get(RES_BACKGROUND
);
158 SfxItemSetFixed
<XATTR_FILL_FIRST
, XATTR_FILL_LAST
> aTempSet(*rFooterSet
.GetPool());
160 setSvxBrushItemAsFillAttributesToTargetSet(rItem
, aTempSet
);
161 setFooterFillAttributes(
162 std::make_shared
<drawinglayer::attribute::SdrAllFillAttributesHelper
>(
170 if(const SvxBrushItem
* pBrushItem
= rSet
.GetItemIfSet(RES_BACKGROUND
, false))
172 // create FillAttributes from SvxBrushItem
173 SfxItemSetFixed
<XATTR_FILL_FIRST
, XATTR_FILL_LAST
> aTempSet(*rSet
.GetPool());
175 setSvxBrushItemAsFillAttributesToTargetSet(*pBrushItem
, aTempSet
);
176 setPageFillAttributes(
177 std::make_shared
<drawinglayer::attribute::SdrAllFillAttributesHelper
>(
184 void SwColExample::DrawPage(vcl::RenderContext
& rRenderContext
, const Point
& rOrg
,
185 const bool bSecond
, const bool bEnabled
)
187 SwPageExample::DrawPage(rRenderContext
, rOrg
, bSecond
, bEnabled
);
190 sal_uInt16 nColumnCount
= m_pColMgr
->GetCount();
194 tools::Long nL
= GetLeft();
195 tools::Long nR
= GetRight();
197 if (GetUsage() == SvxPageUsage::Mirror
&& !bSecond
)
204 rRenderContext
.SetFillColor(COL_LIGHTGRAY
);
205 tools::Rectangle aRect
;
206 aRect
.SetRight( rOrg
.X() + GetSize().Width() - nR
);
207 aRect
.SetLeft( rOrg
.X() + nL
);
208 aRect
.SetTop( rOrg
.Y() + GetTop() + GetHdHeight() + GetHdDist() );
209 aRect
.SetBottom( rOrg
.Y() + GetSize().Height() - GetBottom() - GetFtHeight() - GetFtDist() );
210 rRenderContext
.DrawRect(aRect
);
212 const tools::Rectangle
aDefineRect(aRect
);
213 const drawinglayer::attribute::SdrAllFillAttributesHelperPtr
& rFillAttributes
= getPageFillAttributes();
215 if (!rFillAttributes
|| !rFillAttributes
->isUsed())
217 // If there is no fill, use fallback color
218 const StyleSettings
& rStyleSettings
= rRenderContext
.GetSettings().GetStyleSettings();
219 const Color
& rFieldColor
= rStyleSettings
.GetFieldColor();
221 setPageFillAttributes(
222 std::make_shared
<drawinglayer::attribute::SdrAllFillAttributesHelper
>(
226 // #97495# make sure that the automatic column width's are always equal
227 bool bAutoWidth
= m_pColMgr
->IsAutoWidth();
228 sal_Int32 nAutoColWidth
= 0;
231 sal_Int32 nColumnWidthSum
= 0;
232 for (sal_uInt16 i
= 0; i
< nColumnCount
; ++i
)
233 nColumnWidthSum
+= m_pColMgr
->GetColWidth( i
);
234 nAutoColWidth
= nColumnWidthSum
/ nColumnCount
;
237 for (sal_uInt16 i
= 0; i
< nColumnCount
; ++i
)
240 nAutoColWidth
= m_pColMgr
->GetColWidth(i
);
243 aRect
.SetRight( aRect
.Left() + nAutoColWidth
);
245 aRect
.SetBottom( aRect
.Top() + nAutoColWidth
);
247 // use primitive draw command
248 drawFillAttributes(rRenderContext
, getPageFillAttributes(), aRect
, aDefineRect
);
250 if (i
< nColumnCount
- 1)
253 aRect
.SetLeft( aRect
.Right() + m_pColMgr
->GetGutterWidth(i
) );
255 aRect
.SetTop( aRect
.Bottom() + m_pColMgr
->GetGutterWidth(i
) );
258 if (!m_pColMgr
->HasLine())
261 Point
aUp(rOrg
.X() + nL
, rOrg
.Y() + GetTop());
262 Point
aDown(rOrg
.X() + nL
,
263 rOrg
.Y() + GetSize().Height() - GetBottom() - GetFtHeight() - GetFtDist());
265 if (m_pColMgr
->GetLineHeightPercent() != 100)
267 tools::Long nLength
= !m_bVertical
? aDown
.Y() - aUp
.Y() : aDown
.X() - aUp
.X();
268 nLength
-= nLength
* m_pColMgr
->GetLineHeightPercent() / 100;
269 switch (m_pColMgr
->GetAdjust())
273 aUp
.AdjustY(nLength
);
275 aUp
.AdjustX(nLength
);
279 aDown
.AdjustY( -nLength
);
281 aDown
.AdjustX( -nLength
);
286 aUp
.AdjustY(nLength
/ 2 );
287 aDown
.AdjustY( -(nLength
/ 2) );
291 aUp
.AdjustX(nLength
/ 2 );
292 aDown
.AdjustX( -(nLength
/ 2) );
296 break; // prevent warning
300 for (sal_uInt16 i
= 0; i
< nColumnCount
- 1; ++i
)
302 int nGutter
= m_pColMgr
->GetGutterWidth(i
);
303 int nDist
= m_pColMgr
->GetColWidth( i
) + nGutter
;
304 nDist
-= (i
== 0) ? nGutter
/ 2 : 0;
308 aDown
.AdjustX(nDist
);
313 aDown
.AdjustY(nDist
);
316 rRenderContext
.DrawLine(aUp
, aDown
);
320 SwColumnOnlyExample::SwColumnOnlyExample()
321 : m_aFrameSize(SvxPaperInfo::GetPaperSize(PAPER_A4
)) // DIN A4
323 ::FitToActualSize(m_aCols
, o3tl::narrowing
<sal_uInt16
>(m_aFrameSize
.Width()));
326 void SwColumnOnlyExample::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& /*rRect*/)
328 rRenderContext
.Push(vcl::PushFlags::MAPMODE
);
330 Fraction
aScale(m_aWinSize
.Height(), m_aFrameSize
.Height());
331 MapMode
aMapMode(MapUnit::MapTwip
);
332 aMapMode
.SetScaleX(aScale
);
333 aMapMode
.SetScaleY(aScale
);
334 rRenderContext
.SetMapMode(aMapMode
);
336 const StyleSettings
& rStyleSettings
= rRenderContext
.GetSettings().GetStyleSettings();
337 const Color
& rFieldColor
= rStyleSettings
.GetFieldColor();
338 const Color
& rDlgColor
= rStyleSettings
.GetDialogColor();
339 const Color
& rFieldTextColor
= SwViewOption::GetCurrentViewOptions().GetFontColor();
340 Color
aGrayColor(COL_LIGHTGRAY
);
341 if (rFieldColor
== aGrayColor
)
344 Size
aLogSize(rRenderContext
.PixelToLogic(GetOutputSizePixel()));
345 tools::Rectangle
aCompleteRect(Point(0,0), aLogSize
);
346 rRenderContext
.SetLineColor(rDlgColor
);
347 rRenderContext
.SetFillColor(rDlgColor
);
348 rRenderContext
.DrawRect(aCompleteRect
);
350 rRenderContext
.SetLineColor(rFieldTextColor
);
351 Point
aTL((aLogSize
.Width() - m_aFrameSize
.Width()) / 2,
352 (aLogSize
.Height() - m_aFrameSize
.Height()) / 2);
353 tools::Rectangle
aRect(aTL
, m_aFrameSize
);
355 //draw a shadow rectangle
356 rRenderContext
.SetFillColor(COL_GRAY
);
357 tools::Rectangle
aShadowRect(aRect
);
358 aShadowRect
.Move(aTL
.Y(), aTL
.Y());
359 rRenderContext
.DrawRect(aShadowRect
);
361 rRenderContext
.SetFillColor(rFieldColor
);
362 rRenderContext
.DrawRect(aRect
);
364 rRenderContext
.SetFillColor(aGrayColor
);
367 tools::Long nLength
= aLogSize
.Height() - 2 * aTL
.Y();
369 Point
aDown(aTL
.X(), nLength
);
371 if (m_aCols
.GetLineAdj() != COLADJ_NONE
)
375 sal_uInt16 nPercent
= m_aCols
.GetLineHeight();
378 nLength
-= nLength
* nPercent
/ 100;
379 switch(m_aCols
.GetLineAdj())
381 case COLADJ_BOTTOM
: aUp
.AdjustY(nLength
); break;
382 case COLADJ_TOP
: aDown
.AdjustY( -nLength
); break;
384 aUp
.AdjustY(nLength
/ 2 );
385 aDown
.AdjustY( -(nLength
/ 2) );
388 break; //prevent warning
393 const SwColumns
& rCols
= m_aCols
.GetColumns();
394 sal_uInt16 nColCount
= rCols
.size();
397 rRenderContext
.DrawRect(aRect
);
398 rRenderContext
.SetFillColor(rFieldColor
);
399 tools::Rectangle
aFrameRect(aTL
, m_aFrameSize
);
400 tools::Long nSum
= aTL
.X();
401 for (sal_uInt16 i
= 0; i
< nColCount
; i
++)
403 const SwColumn
* pCol
= &rCols
[i
];
404 aFrameRect
.SetLeft( nSum
+ pCol
->GetLeft() ); //nSum + pCol->GetLeft() + aTL.X();
405 nSum
+= pCol
->GetWishWidth();
406 aFrameRect
.SetRight( nSum
- pCol
->GetRight() );
407 rRenderContext
.DrawRect(aFrameRect
);
412 for (sal_uInt16 i
= 0; i
< nColCount
- 1; i
++)
414 nSum
+= rCols
[i
].GetWishWidth();
417 rRenderContext
.DrawLine(aUp
, aDown
);
421 rRenderContext
.Pop();
424 void SwColumnOnlyExample::SetColumns(const SwFormatCol
& rCol
)
427 sal_uInt16 nWishSum
= m_aCols
.GetWishWidth();
428 tools::Long nFrameWidth
= m_aFrameSize
.Width();
429 SwColumns
& rCols
= m_aCols
.GetColumns();
430 sal_uInt16 nColCount
= rCols
.size();
432 for(sal_uInt16 i
= 0; i
< nColCount
; i
++)
434 SwColumn
* pCol
= &rCols
[i
];
435 tools::Long nWish
= pCol
->GetWishWidth();
436 nWish
*= nFrameWidth
;
438 pCol
->SetWishWidth(o3tl::narrowing
<sal_uInt16
>(nWish
));
439 tools::Long nLeft
= pCol
->GetLeft();
440 nLeft
*= nFrameWidth
;
442 pCol
->SetLeft(o3tl::narrowing
<sal_uInt16
>(nLeft
));
443 tools::Long nRight
= pCol
->GetRight();
444 nRight
*= nFrameWidth
;
446 pCol
->SetRight(o3tl::narrowing
<sal_uInt16
>(nRight
));
448 // #97495# make sure that the automatic column width's are always equal
449 if(!(nColCount
&& m_aCols
.IsOrtho()))
452 sal_Int32 nColumnWidthSum
= 0;
454 for(i
= 0; i
< nColCount
; ++i
)
456 SwColumn
* pCol
= &rCols
[i
];
457 nColumnWidthSum
+= pCol
->GetWishWidth();
458 nColumnWidthSum
-= (pCol
->GetRight() + pCol
->GetLeft());
460 nColumnWidthSum
/= nColCount
;
461 for(i
= 0; i
< nColCount
; ++i
)
463 SwColumn
* pCol
= &rCols
[i
];
464 pCol
->SetWishWidth( static_cast< sal_uInt16
>(nColumnWidthSum
+ pCol
->GetRight() + pCol
->GetLeft()));
468 void SwColumnOnlyExample::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
470 weld::CustomWidgetController::SetDrawingArea(pDrawingArea
);
471 OutputDevice
& rRefDevice
= pDrawingArea
->get_ref_device();
472 Size
aPrefSize(rRefDevice
.LogicToPixel(Size(75, 46), MapMode(MapUnit::MapAppFont
)));
473 pDrawingArea
->set_size_request(aPrefSize
.Width(), aPrefSize
.Height());
476 void SwColumnOnlyExample::Resize()
478 OutputDevice
& rRefDevice
= GetDrawingArea()->get_ref_device();
479 rRefDevice
.Push(vcl::PushFlags::MAPMODE
);
480 rRefDevice
.SetMapMode(MapMode(MapUnit::MapTwip
));
481 m_aWinSize
= GetOutputSizePixel();
482 m_aWinSize
.AdjustHeight( -4 );
483 m_aWinSize
.AdjustWidth( -4 );
484 m_aWinSize
= rRefDevice
.PixelToLogic(m_aWinSize
);
489 SwPageGridExample::SwPageGridExample()
493 void SwPageGridExample::DrawPage(vcl::RenderContext
& rRenderContext
, const Point
& rOrg
,
494 const bool bSecond
, const bool bEnabled
)
496 SwPageExample::DrawPage(rRenderContext
, rOrg
, bSecond
, bEnabled
);
498 if (!m_pGridItem
|| !m_pGridItem
->GetGridType())
502 Color aLineColor
= m_pGridItem
->GetColor();
503 if (aLineColor
== COL_AUTO
)
505 aLineColor
= rRenderContext
.GetFillColor();
508 rRenderContext
.SetLineColor(aLineColor
);
509 tools::Long nL
= GetLeft();
510 tools::Long nR
= GetRight();
512 if (GetUsage() == SvxPageUsage::Mirror
&& !bSecond
)
514 // rotate for mirrored
519 tools::Rectangle aRect
;
520 aRect
.SetRight( rOrg
.X() + GetSize().Width() - nR
);
521 aRect
.SetLeft( rOrg
.X() + nL
);
522 aRect
.SetTop( rOrg
.Y() + GetTop() + GetHdHeight() + GetHdDist() );
523 aRect
.SetBottom( rOrg
.Y() + GetSize().Height() - GetBottom() - GetFtHeight() - GetFtDist() );
525 //increase the values to get a 'viewable' preview
526 sal_Int32 nBaseHeight
= m_pGridItem
->GetBaseHeight() * 3;
527 sal_Int32 nRubyHeight
= m_pGridItem
->GetRubyHeight() * 3;
529 //detect height of rectangles
530 tools::Rectangle
aRubyRect(aRect
.TopLeft(),
532 Size(nRubyHeight
, aRect
.GetHeight()) :
533 Size(aRect
.GetWidth(), nRubyHeight
));
534 tools::Rectangle
aCharRect(aRect
.TopLeft(),
536 Size(nBaseHeight
, aRect
.GetHeight()) :
537 Size(aRect
.GetWidth(), nBaseHeight
));
539 sal_Int32 nLineHeight
= nBaseHeight
+ nRubyHeight
;
541 //detect count of rectangles
542 sal_Int32 nLines
= (m_bVertical
? aRect
.GetWidth(): aRect
.GetHeight()) / nLineHeight
;
543 if (nLines
> m_pGridItem
->GetLines())
544 nLines
= m_pGridItem
->GetLines();
546 // determine start position
549 sal_Int16 nXStart
= static_cast<sal_Int16
>(aRect
.GetWidth() / 2 - nLineHeight
* nLines
/2);
550 aRubyRect
.Move(nXStart
, 0);
551 aCharRect
.Move(nXStart
, 0);
555 sal_Int16 nYStart
= static_cast<sal_Int16
>(aRect
.GetHeight() / 2 - nLineHeight
* nLines
/2);
556 aRubyRect
.Move(0, nYStart
);
557 aCharRect
.Move(0, nYStart
);
560 if (m_pGridItem
->IsRubyTextBelow())
561 m_bVertical
? aRubyRect
.Move(nBaseHeight
, 0) : aRubyRect
.Move(0, nBaseHeight
);
563 m_bVertical
? aCharRect
.Move(nRubyHeight
, 0) : aCharRect
.Move(0, nRubyHeight
);
566 bool bBothLines
= m_pGridItem
->GetGridType() == GRID_LINES_CHARS
;
567 rRenderContext
.SetFillColor(COL_TRANSPARENT
);
568 sal_Int32 nXMove
= m_bVertical
? nLineHeight
: 0;
569 sal_Int32 nYMove
= m_bVertical
? 0 : nLineHeight
;
570 for (sal_Int32 nLine
= 0; nLine
< nLines
; nLine
++)
572 rRenderContext
.DrawRect(aRubyRect
);
573 rRenderContext
.DrawRect(aCharRect
);
576 Point aStart
= aCharRect
.TopLeft();
577 Point aEnd
= m_bVertical
? aCharRect
.TopRight() : aCharRect
.BottomLeft();
578 while (m_bVertical
? aStart
.Y() < aRect
.Bottom(): aStart
.X() < aRect
.Right())
580 rRenderContext
.DrawLine(aStart
, aEnd
);
582 aStart
.setY( aEnd
.AdjustY(nBaseHeight
) );
584 aStart
.setX( aEnd
.AdjustX(nBaseHeight
) );
587 aRubyRect
.Move(nXMove
, nYMove
);
588 aCharRect
.Move(nXMove
, nYMove
);
593 void SwPageGridExample::UpdateExample( const SfxItemSet
& rSet
)
596 //get the grid information
597 if (SfxItemState::DEFAULT
<= rSet
.GetItemState(RES_TEXTGRID
))
598 m_pGridItem
.reset(rSet
.Get(RES_TEXTGRID
).Clone());
599 SwPageExample::UpdateExample(rSet
);
602 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */