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 : #include "SlsFramePainter.hxx"
21 : #include <vcl/outdev.hxx>
22 : #include <vcl/bmpacc.hxx>
23 :
24 : namespace sd { namespace slidesorter { namespace view {
25 :
26 344 : 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 344 : mbIsValid(false)
37 : {
38 688 : if (rShadowBitmap.GetSizePixel().Width() == rShadowBitmap.GetSizePixel().Height()
39 344 : && (rShadowBitmap.GetSizePixel().Width()-1)%2 == 0
40 688 : && ((rShadowBitmap.GetSizePixel().Width()-1)/2)%2 == 1)
41 : {
42 344 : mbIsValid = true;
43 : }
44 : else
45 : {
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);
49 : }
50 344 : }
51 :
52 344 : FramePainter::~FramePainter (void)
53 : {
54 344 : }
55 :
56 474 : void FramePainter::PaintFrame (
57 : OutputDevice& rDevice,
58 : const Rectangle& rBox) const
59 : {
60 474 : if ( ! mbIsValid)
61 474 : return;
62 :
63 : // Paint the shadow.
64 474 : maTopLeft.PaintCorner(rDevice, rBox.TopLeft());
65 474 : maTopRight.PaintCorner(rDevice, rBox.TopRight());
66 474 : maBottomLeft.PaintCorner(rDevice, rBox.BottomLeft());
67 474 : maBottomRight.PaintCorner(rDevice, rBox.BottomRight());
68 474 : maLeft.PaintSide(rDevice, rBox.TopLeft(), rBox.BottomLeft(), maTopLeft, maBottomLeft);
69 474 : maRight.PaintSide(rDevice, rBox.TopRight(), rBox.BottomRight(), maTopRight, maBottomRight);
70 474 : maTop.PaintSide(rDevice, rBox.TopLeft(), rBox.TopRight(), maTopLeft, maTopRight);
71 474 : maBottom.PaintSide(rDevice, rBox.BottomLeft(), rBox.BottomRight(), maBottomLeft, maBottomRight);
72 474 : maCenter.PaintCenter(rDevice,rBox);
73 : }
74 :
75 109 : void FramePainter::AdaptColor (
76 : const Color aNewColor,
77 : const bool bEraseCenter)
78 : {
79 : // Get the source color.
80 109 : if (maCenter.maBitmap.IsEmpty())
81 0 : return;
82 109 : BitmapReadAccess* pReadAccess = maCenter.maBitmap.GetBitmap().AcquireReadAccess();
83 109 : if (pReadAccess == NULL)
84 0 : return;
85 109 : const Color aSourceColor = pReadAccess->GetColor(0,0);
86 109 : maCenter.maBitmap.GetBitmap().ReleaseAccess(pReadAccess);
87 :
88 : // Erase the center bitmap.
89 109 : if (bEraseCenter)
90 109 : maCenter.maBitmap.SetEmpty();
91 :
92 : // Replace the color in all bitmaps.
93 109 : maTopLeft.maBitmap.Replace(aSourceColor, aNewColor, 0);
94 109 : maTop.maBitmap.Replace(aSourceColor, aNewColor, 0);
95 109 : maTopRight.maBitmap.Replace(aSourceColor, aNewColor, 0);
96 109 : maLeft.maBitmap.Replace(aSourceColor, aNewColor, 0);
97 109 : maCenter.maBitmap.Replace(aSourceColor, aNewColor, 0);
98 109 : maRight.maBitmap.Replace(aSourceColor, aNewColor, 0);
99 109 : maBottomLeft.maBitmap.Replace(aSourceColor, aNewColor, 0);
100 109 : maBottom.maBitmap.Replace(aSourceColor, aNewColor, 0);
101 109 : maBottomRight.maBitmap.Replace(aSourceColor, aNewColor, 0);
102 : }
103 :
104 : //===== FramePainter::OffsetBitmap ============================================
105 :
106 3096 : FramePainter::OffsetBitmap::OffsetBitmap (
107 : const BitmapEx& rBitmap,
108 : const sal_Int32 nHorizontalPosition,
109 : const sal_Int32 nVerticalPosition)
110 : : maBitmap(),
111 3096 : maOffset()
112 : {
113 : OSL_ASSERT(nHorizontalPosition>=-1 && nHorizontalPosition<=+1);
114 : OSL_ASSERT(nVerticalPosition>=-1 && nVerticalPosition<=+1);
115 :
116 3096 : const sal_Int32 nS (1);
117 3096 : const sal_Int32 nC (::std::max<sal_Int32>(0,(rBitmap.GetSizePixel().Width()-nS)/2));
118 3096 : const sal_Int32 nO (nC/2);
119 :
120 : const Point aOrigin(
121 1032 : nHorizontalPosition<0 ? 0 : (nHorizontalPosition == 0 ? nC : nC+nS),
122 4128 : nVerticalPosition<0 ? 0 : (nVerticalPosition == 0 ? nC : nC+nS));
123 : const Size aSize(
124 : nHorizontalPosition==0 ? nS : nC,
125 3096 : nVerticalPosition==0 ? nS : nC);
126 3096 : maBitmap = BitmapEx(rBitmap, aOrigin, aSize);
127 3096 : if (maBitmap.IsEmpty())
128 3096 : return;
129 : maOffset = Point(
130 : nHorizontalPosition<0 ? -nO : nHorizontalPosition>0 ? -nO : 0,
131 3096 : nVerticalPosition<0 ? -nO : nVerticalPosition>0 ? -nO : 0);
132 :
133 : // Enlarge the side bitmaps so that painting the frame requires less
134 : // paint calls.
135 3096 : const sal_Int32 nSideBitmapSize (64);
136 3096 : if (nHorizontalPosition == 0 && nVerticalPosition == 0)
137 : {
138 344 : maBitmap.Scale(Size(nSideBitmapSize,nSideBitmapSize));
139 : }
140 2752 : else if (nHorizontalPosition == 0)
141 : {
142 688 : maBitmap.Scale(Size(nSideBitmapSize,aSize.Height()));
143 : }
144 2064 : else if (nVerticalPosition == 0)
145 : {
146 688 : maBitmap.Scale(Size(maBitmap.GetSizePixel().Width(), nSideBitmapSize));
147 : }
148 : }
149 :
150 1896 : void FramePainter::OffsetBitmap::PaintCorner (
151 : OutputDevice& rDevice,
152 : const Point& rAnchor) const
153 : {
154 1896 : if ( ! maBitmap.IsEmpty())
155 1896 : rDevice.DrawBitmapEx(rAnchor+maOffset, maBitmap);
156 1896 : }
157 :
158 1896 : void FramePainter::OffsetBitmap::PaintSide (
159 : OutputDevice& rDevice,
160 : const Point& rAnchor1,
161 : const Point& rAnchor2,
162 : const OffsetBitmap& rCornerBitmap1,
163 : const OffsetBitmap& rCornerBitmap2) const
164 : {
165 1896 : if (maBitmap.IsEmpty())
166 1896 : return;
167 :
168 1896 : const Size aBitmapSize (maBitmap.GetSizePixel());
169 1896 : if (rAnchor1.Y() == rAnchor2.Y())
170 : {
171 : // Side is horizontal.
172 948 : const sal_Int32 nY (rAnchor1.Y() + maOffset.Y());
173 : const sal_Int32 nLeft (
174 948 : rAnchor1.X()
175 948 : + rCornerBitmap1.maBitmap.GetSizePixel().Width()
176 1896 : + rCornerBitmap1.maOffset.X());
177 : const sal_Int32 nRight (
178 948 : rAnchor2.X()
179 948 : + rCornerBitmap2.maOffset.X()\
180 1896 : - 1);
181 3792 : for (sal_Int32 nX=nLeft; nX<=nRight; nX+=aBitmapSize.Width())
182 : {
183 : rDevice.DrawBitmapEx(
184 : Point(nX,nY),
185 5688 : Size(std::min(aBitmapSize.Width(),static_cast<long>(nRight-nX+1)),aBitmapSize.Height()),
186 5688 : maBitmap);
187 : }
188 : }
189 948 : else if (rAnchor1.X() == rAnchor2.X())
190 : {
191 : // Side is vertical.
192 948 : const sal_Int32 nX (rAnchor1.X() + maOffset.X());
193 : const sal_Int32 nTop (
194 948 : rAnchor1.Y()
195 948 : + rCornerBitmap1.maBitmap.GetSizePixel().Height()
196 1896 : + rCornerBitmap1.maOffset.Y());
197 : const sal_Int32 nBottom (
198 948 : rAnchor2.Y()
199 948 : + rCornerBitmap2.maOffset.Y()
200 1896 : - 1);
201 4346 : for (sal_Int32 nY=nTop; nY<=nBottom; nY+=aBitmapSize.Height())
202 : {
203 : rDevice.DrawBitmapEx(
204 : Point(nX,nY),
205 6796 : Size(aBitmapSize.Width(), std::min(aBitmapSize.Height(), static_cast<long>(nBottom-nY+1))),
206 6796 : maBitmap);
207 : }
208 : }
209 : else
210 : {
211 : // Diagonal sides indicatee an error.
212 : OSL_ASSERT(false);
213 : }
214 : }
215 :
216 474 : void FramePainter::OffsetBitmap::PaintCenter (
217 : OutputDevice& rDevice,
218 : const Rectangle& rBox) const
219 : {
220 474 : const Size aBitmapSize (maBitmap.GetSizePixel());
221 2230 : for (sal_Int32 nY=rBox.Top(); nY<=rBox.Bottom(); nY+=aBitmapSize.Height())
222 7024 : for (sal_Int32 nX=rBox.Left(); nX<=rBox.Right(); nX+=aBitmapSize.Width())
223 : rDevice.DrawBitmapEx(
224 : Point(nX,nY),
225 : Size(
226 10536 : ::std::min(aBitmapSize.Width(), rBox.Right()-nX+1),
227 10536 : std::min(aBitmapSize.Height(), rBox.Bottom()-nY+1)),
228 15804 : maBitmap);
229 474 : }
230 :
231 114 : } } } // end of namespace sd::slidesorter::view
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|