cid#1607171 Data race condition
[LibreOffice.git] / sd / source / ui / slidesorter / view / SlsFramePainter.cxx
blob31c301868a9365dc6c46372313ab088a37e7a72c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "SlsFramePainter.hxx"
21 #include <vcl/outdev.hxx>
22 #include <osl/diagnose.h>
24 namespace sd::slidesorter::view {
26 FramePainter::FramePainter (const BitmapEx& rShadowBitmap)
27 : maTopLeft(rShadowBitmap,-1,-1),
28 maTop(rShadowBitmap,0,-1),
29 maTopRight(rShadowBitmap,+1,-1),
30 maLeft(rShadowBitmap,-1,0),
31 maRight(rShadowBitmap,+1,0),
32 maBottomLeft(rShadowBitmap,-1,+1),
33 maBottom(rShadowBitmap,0,+1),
34 maBottomRight(rShadowBitmap,+1,+1),
35 maCenter(rShadowBitmap,0,0),
36 mbIsValid(false)
38 if (rShadowBitmap.GetSizePixel().Width() == rShadowBitmap.GetSizePixel().Height()
39 && (rShadowBitmap.GetSizePixel().Width()-1)%2 == 0
40 && ((rShadowBitmap.GetSizePixel().Width()-1)/2)%2 == 1)
42 mbIsValid = true;
44 else
46 OSL_ASSERT(rShadowBitmap.GetSizePixel().Width() == rShadowBitmap.GetSizePixel().Height());
47 OSL_ASSERT((rShadowBitmap.GetSizePixel().Width()-1)%2 == 0);
48 OSL_ASSERT(((rShadowBitmap.GetSizePixel().Width()-1)/2)%2 == 1);
52 FramePainter::~FramePainter()
56 void FramePainter::PaintFrame (
57 OutputDevice& rDevice,
58 const ::tools::Rectangle& rBox) const
60 if ( ! mbIsValid)
61 return;
63 // Paint the shadow.
64 maTopLeft.PaintCorner(rDevice, rBox.TopLeft());
65 maTopRight.PaintCorner(rDevice, rBox.TopRight());
66 maBottomLeft.PaintCorner(rDevice, rBox.BottomLeft());
67 maBottomRight.PaintCorner(rDevice, rBox.BottomRight());
68 maLeft.PaintSide(rDevice, rBox.TopLeft(), rBox.BottomLeft(), maTopLeft, maBottomLeft);
69 maRight.PaintSide(rDevice, rBox.TopRight(), rBox.BottomRight(), maTopRight, maBottomRight);
70 maTop.PaintSide(rDevice, rBox.TopLeft(), rBox.TopRight(), maTopLeft, maTopRight);
71 maBottom.PaintSide(rDevice, rBox.BottomLeft(), rBox.BottomRight(), maBottomLeft, maBottomRight);
72 maCenter.PaintCenter(rDevice,rBox);
75 void FramePainter::AdaptColor (
76 const Color aNewColor)
78 // Get the source color.
79 if (maCenter.maBitmap.IsEmpty())
80 return;
81 const Color aSourceColor = maCenter.maBitmap.GetPixelColor(0,0);
83 // Erase the center bitmap.
84 maCenter.maBitmap.SetEmpty();
86 // Replace the color in all bitmaps.
87 maTopLeft.maBitmap.Replace(aSourceColor, aNewColor);
88 maTop.maBitmap.Replace(aSourceColor, aNewColor);
89 maTopRight.maBitmap.Replace(aSourceColor, aNewColor);
90 maLeft.maBitmap.Replace(aSourceColor, aNewColor);
91 maCenter.maBitmap.Replace(aSourceColor, aNewColor);
92 maRight.maBitmap.Replace(aSourceColor, aNewColor);
93 maBottomLeft.maBitmap.Replace(aSourceColor, aNewColor);
94 maBottom.maBitmap.Replace(aSourceColor, aNewColor);
95 maBottomRight.maBitmap.Replace(aSourceColor, aNewColor);
98 //===== FramePainter::OffsetBitmap ============================================
100 FramePainter::OffsetBitmap::OffsetBitmap (
101 const BitmapEx& rBitmap,
102 const sal_Int32 nHorizontalPosition,
103 const sal_Int32 nVerticalPosition)
105 OSL_ASSERT(nHorizontalPosition>=-1 && nHorizontalPosition<=+1);
106 OSL_ASSERT(nVerticalPosition>=-1 && nVerticalPosition<=+1);
108 const sal_Int32 nS (1);
109 const sal_Int32 nC (::std::max<sal_Int32>(0,(rBitmap.GetSizePixel().Width()-nS)/2));
110 const sal_Int32 nO (nC/2);
112 const Point aOrigin(
113 nHorizontalPosition<0 ? 0 : (nHorizontalPosition == 0 ? nC : nC+nS),
114 nVerticalPosition<0 ? 0 : (nVerticalPosition == 0 ? nC : nC+nS));
115 const Size aSize(
116 nHorizontalPosition==0 ? nS : nC,
117 nVerticalPosition==0 ? nS : nC);
118 maBitmap = BitmapEx(rBitmap, aOrigin, aSize);
119 if (maBitmap.IsEmpty())
120 return;
121 maOffset = Point(
122 nHorizontalPosition<0 ? -nO : nHorizontalPosition>0 ? -nO : 0,
123 nVerticalPosition<0 ? -nO : nVerticalPosition>0 ? -nO : 0);
125 // Enlarge the side bitmaps so that painting the frame requires less
126 // paint calls.
127 const sal_Int32 nSideBitmapSize (64);
128 if (nHorizontalPosition == 0 && nVerticalPosition == 0)
130 maBitmap.Scale(Size(nSideBitmapSize,nSideBitmapSize));
132 else if (nHorizontalPosition == 0)
134 maBitmap.Scale(Size(nSideBitmapSize,aSize.Height()));
136 else if (nVerticalPosition == 0)
138 maBitmap.Scale(Size(maBitmap.GetSizePixel().Width(), nSideBitmapSize));
142 void FramePainter::OffsetBitmap::PaintCorner (
143 OutputDevice& rDevice,
144 const Point& rAnchor) const
146 if ( ! maBitmap.IsEmpty())
147 rDevice.DrawBitmapEx(rAnchor+maOffset, maBitmap);
150 void FramePainter::OffsetBitmap::PaintSide (
151 OutputDevice& rDevice,
152 const Point& rAnchor1,
153 const Point& rAnchor2,
154 const OffsetBitmap& rCornerBitmap1,
155 const OffsetBitmap& rCornerBitmap2) const
157 if (maBitmap.IsEmpty())
158 return;
160 const Size aBitmapSize (maBitmap.GetSizePixel());
161 if (rAnchor1.Y() == rAnchor2.Y())
163 // Side is horizontal.
164 const sal_Int32 nY (rAnchor1.Y() + maOffset.Y());
165 const sal_Int32 nLeft (
166 rAnchor1.X()
167 + rCornerBitmap1.maBitmap.GetSizePixel().Width()
168 + rCornerBitmap1.maOffset.X());
169 const sal_Int32 nRight (
170 rAnchor2.X()
171 + rCornerBitmap2.maOffset.X()
172 - 1);
173 for (sal_Int32 nX=nLeft; nX<=nRight; nX+=aBitmapSize.Width())
175 rDevice.DrawBitmapEx(
176 Point(nX,nY),
177 Size(std::min(aBitmapSize.Width(),static_cast<::tools::Long>(nRight-nX+1)),aBitmapSize.Height()),
178 maBitmap);
181 else if (rAnchor1.X() == rAnchor2.X())
183 // Side is vertical.
184 const sal_Int32 nX (rAnchor1.X() + maOffset.X());
185 const sal_Int32 nTop (
186 rAnchor1.Y()
187 + rCornerBitmap1.maBitmap.GetSizePixel().Height()
188 + rCornerBitmap1.maOffset.Y());
189 const sal_Int32 nBottom (
190 rAnchor2.Y()
191 + rCornerBitmap2.maOffset.Y()
192 - 1);
193 for (sal_Int32 nY=nTop; nY<=nBottom; nY+=aBitmapSize.Height())
195 rDevice.DrawBitmapEx(
196 Point(nX,nY),
197 Size(aBitmapSize.Width(), std::min(aBitmapSize.Height(), static_cast<::tools::Long>(nBottom-nY+1))),
198 maBitmap);
201 else
203 // Diagonal sides indicates an error.
204 OSL_ASSERT(false);
208 void FramePainter::OffsetBitmap::PaintCenter (
209 OutputDevice& rDevice,
210 const ::tools::Rectangle& rBox) const
212 const Size aBitmapSize (maBitmap.GetSizePixel());
213 for (::tools::Long nY=rBox.Top(); nY<=rBox.Bottom(); nY+=aBitmapSize.Height())
214 for (::tools::Long nX=rBox.Left(); nX<=rBox.Right(); nX+=aBitmapSize.Width())
215 rDevice.DrawBitmapEx(
216 Point(nX,nY),
217 Size(
218 ::std::min(aBitmapSize.Width(), rBox.Right()-nX+1),
219 std::min(aBitmapSize.Height(), rBox.Bottom()-nY+1)),
220 maBitmap);
223 } // end of namespace sd::slidesorter::view
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */