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 <com/sun/star/drawing/ColorMode.hpp>
21 #include <o3tl/any.hxx>
22 #include <vcl/GraphicAttributes.hxx>
23 #include <grfatr.hxx>
24 #include <swunohelper.hxx>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 
28 #include <unomid.h>
29 
30 using namespace ::com::sun::star;
31 
Clone(SfxItemPool *) const32 SwMirrorGrf* SwMirrorGrf::Clone( SfxItemPool* ) const
33 {
34     return new SwMirrorGrf( *this );
35 }
36 
GetValueCount() const37 sal_uInt16 SwMirrorGrf::GetValueCount() const
38 {
39     return 4;
40 }
41 
operator ==(const SfxPoolItem & rItem) const42 bool SwMirrorGrf::operator==( const SfxPoolItem& rItem) const
43 {
44     return SfxEnumItem::operator==(rItem) &&
45             static_cast<const SwMirrorGrf&>(rItem).IsGrfToggle() == IsGrfToggle();
46 }
47 
lcl_IsHoriOnEvenPages(MirrorGraph nEnum,bool bToggle)48 static bool lcl_IsHoriOnEvenPages(MirrorGraph nEnum, bool bToggle)
49 {
50     bool bEnum = nEnum == MirrorGraph::Vertical ||
51                    nEnum == MirrorGraph::Both;
52     return bEnum != bToggle;
53 }
54 
lcl_IsHoriOnOddPages(MirrorGraph nEnum)55 static bool lcl_IsHoriOnOddPages(MirrorGraph nEnum)
56 {
57     bool bEnum = nEnum == MirrorGraph::Vertical ||
58                    nEnum == MirrorGraph::Both;
59     return bEnum;
60 }
61 
QueryValue(uno::Any & rVal,sal_uInt8 nMemberId) const62 bool SwMirrorGrf::QueryValue( uno::Any& rVal, sal_uInt8 nMemberId ) const
63 {
64     bool bRet = true;
65     bool bVal = false;
66     // vertical and horizontal were swapped at some point
67     nMemberId &= ~CONVERT_TWIPS;
68     switch ( nMemberId )
69     {
70         case MID_MIRROR_HORZ_EVEN_PAGES:
71             bVal = lcl_IsHoriOnEvenPages(GetValue(), IsGrfToggle());
72         break;
73         case MID_MIRROR_HORZ_ODD_PAGES:
74             bVal = lcl_IsHoriOnOddPages(GetValue());
75         break;
76         case MID_MIRROR_VERT:
77             bVal = GetValue() == MirrorGraph::Horizontal ||
78                    GetValue() == MirrorGraph::Both;
79             break;
80         default:
81             OSL_ENSURE( false, "unknown MemberId" );
82             bRet = false;
83     }
84     rVal <<= bVal;
85     return bRet;
86 }
87 
PutValue(const uno::Any & rVal,sal_uInt8 nMemberId)88 bool SwMirrorGrf::PutValue( const uno::Any& rVal, sal_uInt8 nMemberId )
89 {
90     bool bRet = true;
91     bool bVal = *o3tl::doAccess<bool>(rVal);
92     // vertical and horizontal were swapped at some point
93     nMemberId &= ~CONVERT_TWIPS;
94     switch ( nMemberId )
95     {
96         case MID_MIRROR_HORZ_EVEN_PAGES:
97         case MID_MIRROR_HORZ_ODD_PAGES:
98         {
99             bool bIsVert = GetValue() == MirrorGraph::Horizontal ||
100                                 GetValue() == MirrorGraph::Both;
101             bool bOnOddPages = nMemberId == MID_MIRROR_HORZ_EVEN_PAGES ?
102                                     lcl_IsHoriOnOddPages(GetValue()) : bVal;
103             bool bOnEvenPages = nMemberId == MID_MIRROR_HORZ_ODD_PAGES ?
104                                        lcl_IsHoriOnEvenPages(GetValue(), IsGrfToggle()) : bVal;
105             MirrorGraph nEnum = bOnOddPages ?
106                     bIsVert ? MirrorGraph::Both : MirrorGraph::Vertical :
107                         bIsVert ? MirrorGraph::Horizontal : MirrorGraph::Dont;
108             bool bToggle = bOnOddPages != bOnEvenPages;
109             SetValue(nEnum);
110             SetGrfToggle( bToggle );
111         }
112         break;
113         case MID_MIRROR_VERT:
114             if ( bVal )
115             {
116                 if ( GetValue() == MirrorGraph::Vertical )
117                     SetValue( MirrorGraph::Both );
118                 else if ( GetValue() != MirrorGraph::Both )
119                     SetValue( MirrorGraph::Horizontal );
120             }
121             else
122             {
123                 if ( GetValue() == MirrorGraph::Both )
124                     SetValue( MirrorGraph::Vertical );
125                 else if ( GetValue() == MirrorGraph::Horizontal )
126                     SetValue( MirrorGraph::Dont );
127             }
128             break;
129         default:
130             OSL_ENSURE( false, "unknown MemberId" );
131             bRet = false;
132     }
133     return bRet;
134 }
135 
SwCropGrf()136 SwCropGrf::SwCropGrf()
137     : SvxGrfCrop( RES_GRFATR_CROPGRF )
138 {}
139 
SwCropGrf(sal_Int32 nL,sal_Int32 nR,sal_Int32 nT,sal_Int32 nB)140 SwCropGrf::SwCropGrf(sal_Int32 nL, sal_Int32 nR, sal_Int32 nT, sal_Int32 nB )
141     : SvxGrfCrop( nL, nR, nT, nB, RES_GRFATR_CROPGRF )
142 {}
143 
Clone(SfxItemPool *) const144 SwCropGrf* SwCropGrf::Clone( SfxItemPool* ) const
145 {
146     return new SwCropGrf( *this );
147 }
148 
checkAndCorrectValue(Degree10 nValue)149 Degree10 SwRotationGrf::checkAndCorrectValue(Degree10 nValue)
150 {
151     if(nValue.get() < 0)
152     {
153         // smaller zero, modulo (will keep negative) and add one range
154         DBG_ASSERT(false, "SwRotationGrf: Value is in 10th degree and *has* to be in [0 .. 3600[ (!)");
155         return Degree10(3600 + (nValue.get() % 3600));
156     }
157     else if (nValue.get() >= 3600)
158     {
159         // bigger range, use modulo
160         DBG_ASSERT(false, "SwRotationGrf: Value is in 10th degree and *has* to be in [0 .. 3600[ (!)");
161         return Degree10(nValue.get() % 3600);
162     }
163 
164     return nValue;
165 }
166 
SwRotationGrf(Degree10 nVal,const Size & rSz)167 SwRotationGrf::SwRotationGrf( Degree10 nVal, const Size& rSz )
168     // tdf#115529 check and evtl. correct value
169 :   SfxUInt16Item( RES_GRFATR_ROTATION, checkAndCorrectValue(nVal).get() ),
170     m_aUnrotatedSize( rSz )
171 {
172 }
173 
Clone(SfxItemPool *) const174 SwRotationGrf* SwRotationGrf::Clone( SfxItemPool * ) const
175 {
176     return new SwRotationGrf( *this );
177 }
178 
operator ==(const SfxPoolItem & rCmp) const179 bool SwRotationGrf::operator==( const SfxPoolItem& rCmp ) const
180 {
181     return SfxUInt16Item::operator==( rCmp ) &&
182         GetUnrotatedSize() == static_cast<const SwRotationGrf&>(rCmp).GetUnrotatedSize();
183 }
184 
QueryValue(uno::Any & rVal,sal_uInt8) const185 bool SwRotationGrf::QueryValue( uno::Any& rVal, sal_uInt8 ) const
186 {
187     // SfxUInt16Item::QueryValue returns sal_Int32 in Any now... (srx642w)
188     // where we still want this to be a sal_Int16
189     rVal <<= static_cast<sal_Int16>(GetValue());
190     return true;
191 }
192 
PutValue(const uno::Any & rVal,sal_uInt8)193 bool SwRotationGrf::PutValue( const uno::Any& rVal, sal_uInt8 )
194 {
195     // SfxUInt16Item::QueryValue returns sal_Int32 in Any now... (srx642w)
196     // where we still want this to be a sal_Int16
197     sal_Int16 nValue = 0;
198     if (rVal >>= nValue)
199     {
200         // sal_uInt16 argument needed
201         // tdf#115529 check and evtl. correct value
202         SetValue(checkAndCorrectValue(Degree10(nValue)));
203         return true;
204     }
205 
206     OSL_FAIL( "SwRotationGrf::PutValue - Wrong type!" );
207     return false;
208 }
209 
210 // Sw___Grf::Clone(..)
211 
Clone(SfxItemPool *) const212 SwLuminanceGrf* SwLuminanceGrf::Clone( SfxItemPool * ) const
213 {
214     return new SwLuminanceGrf( *this );
215 }
216 
Clone(SfxItemPool *) const217 SwContrastGrf* SwContrastGrf::Clone( SfxItemPool * ) const
218 {
219     return new SwContrastGrf( *this );
220 }
221 
Clone(SfxItemPool *) const222 SwChannelRGrf* SwChannelRGrf::Clone( SfxItemPool * ) const
223 {
224     return new SwChannelRGrf( *this );
225 }
226 
Clone(SfxItemPool *) const227 SwChannelGGrf* SwChannelGGrf::Clone( SfxItemPool * ) const
228 {
229     return new SwChannelGGrf( *this );
230 }
231 
Clone(SfxItemPool *) const232 SwChannelBGrf* SwChannelBGrf::Clone( SfxItemPool * ) const
233 {
234     return new SwChannelBGrf( *this );
235 }
236 
Clone(SfxItemPool *) const237 SwGammaGrf* SwGammaGrf::Clone( SfxItemPool * ) const
238 {
239     return new SwGammaGrf( *this );
240 }
241 
242 // SwGammaGrf
243 
operator ==(const SfxPoolItem & rCmp) const244 bool SwGammaGrf::operator==( const SfxPoolItem& rCmp ) const
245 {
246     return SfxPoolItem::operator==( rCmp ) &&
247         m_nValue == static_cast<const SwGammaGrf&>(rCmp).GetValue();
248 }
249 
QueryValue(uno::Any & rVal,sal_uInt8) const250 bool SwGammaGrf::QueryValue( uno::Any& rVal, sal_uInt8 ) const
251 {
252     rVal <<= m_nValue;
253     return true;
254 }
255 
PutValue(const uno::Any & rVal,sal_uInt8)256 bool SwGammaGrf::PutValue( const uno::Any& rVal, sal_uInt8 )
257 {
258     return rVal >>= m_nValue;
259 }
260 
261 // Sw___Grf::Clone(..) cont'd
262 
Clone(SfxItemPool *) const263 SwInvertGrf* SwInvertGrf::Clone( SfxItemPool * ) const
264 {
265     return new SwInvertGrf( *this );
266 }
267 
Clone(SfxItemPool *) const268 SwTransparencyGrf* SwTransparencyGrf::Clone( SfxItemPool * ) const
269 {
270     return new SwTransparencyGrf( *this );
271 }
272 
273 // SwTransparencyGrf
274 
QueryValue(uno::Any & rVal,sal_uInt8) const275 bool SwTransparencyGrf::QueryValue( uno::Any& rVal,
276                                         sal_uInt8 ) const
277 {
278     //OSL_ENSURE(dynamic_cast<const SfxByteItem*>( this ) !=  nullptr,"Put/QueryValue should be removed!");
279     SAL_WARN("sw", "Put/QueryValue should be removed!");
280     sal_Int16 nRet = GetValue();
281     OSL_ENSURE( 0 <= nRet && nRet <= 100, "value out of range" );
282     rVal <<= nRet;
283     return true;
284 }
285 
PutValue(const uno::Any & rVal,sal_uInt8)286 bool SwTransparencyGrf::PutValue( const uno::Any& rVal,
287                                         sal_uInt8 )
288 {
289     //temporary conversion until this is a SfxInt16Item!
290     //OSL_ENSURE(dynamic_cast<const SfxByteItem*>( this ) !=  nullptr,"Put/QueryValue should be removed!");
291     SAL_WARN("sw", "Put/QueryValue should be removed!");
292     sal_Int16 nVal = 0;
293     if(!(rVal >>= nVal) || nVal < -100 || nVal > 100)
294         return false;
295     if(nVal < 0)
296     {
297         // for compatibility with old documents
298         // introduce rounding as for SO 6.0 PP2
299         nVal = ( ( nVal * 128 ) - (99/2) ) / 100;
300         nVal += 128;
301     }
302     OSL_ENSURE( 0 <= nVal && nVal <= 100, "value out of range" );
303     SetValue(static_cast<sal_uInt8>(nVal));
304     return true;
305 }
306 
307 // Sw___Grf::Clone(..) cont'd
308 
Clone(SfxItemPool *) const309 SwDrawModeGrf* SwDrawModeGrf::Clone( SfxItemPool * ) const
310 {
311     return new SwDrawModeGrf( *this );
312 }
313 
314 // SwDrawModeGrf
315 
GetValueCount() const316 sal_uInt16 SwDrawModeGrf::GetValueCount() const
317 {
318     return sal_uInt16(GraphicDrawMode::Watermark) + 1;
319 }
320 
QueryValue(uno::Any & rVal,sal_uInt8) const321 bool SwDrawModeGrf::QueryValue( uno::Any& rVal,
322                                 sal_uInt8 ) const
323 {
324     drawing::ColorMode eRet = static_cast<drawing::ColorMode>(GetEnumValue());
325     rVal <<= eRet;
326     return true;
327 }
328 
PutValue(const uno::Any & rVal,sal_uInt8)329 bool SwDrawModeGrf::PutValue( const uno::Any& rVal,
330                                 sal_uInt8 )
331 {
332     sal_Int32 eVal = SWUnoHelper::GetEnumAsInt32( rVal );
333     if(eVal >= 0 && eVal <= sal_uInt16(GraphicDrawMode::Watermark))
334     {
335         SetEnumValue(o3tl::narrowing<sal_uInt16>(eVal));
336         return true;
337     }
338     return false;
339 }
340 
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
342