bump product version to 6.4.0.3
[LibreOffice.git] / sd / source / ui / func / fucopy.cxx
blobd0461367ca5627c5b7814944b49db60c2b1e4099
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 <fucopy.hxx>
21 #include <sfx2/progress.hxx>
22 #include <svl/intitem.hxx>
24 #include <sdattr.hrc>
25 #include <sdresid.hxx>
26 #include <strings.hrc>
27 #include <ViewShell.hxx>
28 #include <View.hxx>
29 #include <drawdoc.hxx>
30 #include <DrawDocShell.hxx>
31 #include <svx/svdobj.hxx>
32 #include <svx/xcolit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <svx/xdef.hxx>
35 #include <svx/xfillit0.hxx>
36 #include <sfx2/request.hxx>
37 #include <sdabstdlg.hxx>
38 #include <memory>
40 using namespace com::sun::star;
42 namespace sd {
45 FuCopy::FuCopy (
46 ViewShell* pViewSh,
47 ::sd::Window* pWin,
48 ::sd::View* pView,
49 SdDrawDocument* pDoc,
50 SfxRequest& rReq)
51 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
55 rtl::Reference<FuPoor> FuCopy::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
57 rtl::Reference<FuPoor> xFunc( new FuCopy( pViewSh, pWin, pView, pDoc, rReq ) );
58 xFunc->DoExecute(rReq);
59 return xFunc;
62 void FuCopy::DoExecute( SfxRequest& rReq )
64 if( !mpView->AreObjectsMarked() )
65 return;
67 // Undo
68 OUString aString = mpView->GetDescriptionOfMarkedObjects() +
69 " " + SdResId( STR_UNDO_COPYOBJECTS );
70 mpView->BegUndo( aString );
72 const SfxItemSet* pArgs = rReq.GetArgs();
74 if( !pArgs )
76 SfxItemSet aSet( mpViewShell->GetPool(),
77 svl::Items<ATTR_COPY_START, ATTR_COPY_END>{} );
79 // indicate color attribute
80 SfxItemSet aAttr( mpDoc->GetPool() );
81 mpView->GetAttributes( aAttr );
82 const SfxPoolItem* pPoolItem = nullptr;
84 if( SfxItemState::SET == aAttr.GetItemState( XATTR_FILLSTYLE, true, &pPoolItem ) )
86 drawing::FillStyle eStyle = static_cast<const XFillStyleItem*>(pPoolItem)->GetValue();
88 if( eStyle == drawing::FillStyle_SOLID &&
89 SfxItemState::SET == aAttr.GetItemState( XATTR_FILLCOLOR, true, &pPoolItem ) )
91 const XFillColorItem* pItem = static_cast<const XFillColorItem*>(pPoolItem);
92 XColorItem aXColorItem( ATTR_COPY_START_COLOR, pItem->GetName(),
93 pItem->GetColorValue() );
94 aSet.Put( aXColorItem );
99 SdAbstractDialogFactory* pFact = SdAbstractDialogFactory::Create();
100 ScopedVclPtr<AbstractCopyDlg> pDlg(pFact->CreateCopyDlg(mpViewShell->GetFrameWeld(), aSet, mpView ));
102 sal_uInt16 nResult = pDlg->Execute();
104 switch( nResult )
106 case RET_OK:
107 pDlg->GetAttr( aSet );
108 rReq.Done( aSet );
109 pArgs = rReq.GetArgs();
110 break;
112 default:
114 pDlg.disposeAndClear();
115 mpView->EndUndo();
116 return; // Cancel
121 ::tools::Rectangle aRect;
122 sal_Int32 lWidth = 0, lHeight = 0, lSizeX = 0, lSizeY = 0, lAngle = 0;
123 sal_uInt16 nNumber = 0;
124 Color aStartColor, aEndColor;
125 bool bColor = false;
126 const SfxPoolItem* pPoolItem = nullptr;
128 // Count
129 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_NUMBER, true, &pPoolItem ) )
130 nNumber = static_cast<const SfxUInt16Item*>( pPoolItem )->GetValue();
132 // translation
133 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_MOVE_X, true, &pPoolItem ) )
134 lSizeX = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
135 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_MOVE_Y, true, &pPoolItem ) )
136 lSizeY = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
137 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_ANGLE, true, &pPoolItem ) )
138 lAngle = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
140 // scale
141 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_WIDTH, true, &pPoolItem ) )
142 lWidth = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
143 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_HEIGHT, true, &pPoolItem ) )
144 lHeight = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
146 // start/end color
147 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_START_COLOR, true, &pPoolItem ) )
149 aStartColor = static_cast<const XColorItem*>( pPoolItem )->GetColorValue();
150 bColor = true;
152 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_END_COLOR, true, &pPoolItem ) )
154 aEndColor = static_cast<const XColorItem*>( pPoolItem )->GetColorValue();
155 if( aStartColor == aEndColor )
156 bColor = false;
158 else
159 bColor = false;
161 // remove handles
162 //HMHmpView->HideMarkHdl();
164 std::unique_ptr<SfxProgress> pProgress;
165 bool bWaiting = false;
167 if( nNumber > 1 )
169 OUString aStr = SdResId( STR_OBJECTS ) +
170 " " + SdResId( STR_UNDO_COPYOBJECTS );
172 pProgress.reset(new SfxProgress( mpDocSh, aStr, nNumber ));
173 mpDocSh->SetWaitCursor( true );
174 bWaiting = true;
177 const SdrMarkList aMarkList( mpView->GetMarkedObjectList() );
178 const size_t nMarkCount = aMarkList.GetMarkCount();
179 SdrObject* pObj = nullptr;
181 // calculate number of possible copies
182 aRect = mpView->GetAllMarkedRect();
184 if( lWidth < 0 )
186 long nTmp = ( aRect.Right() - aRect.Left() ) / -lWidth;
187 nNumber = static_cast<sal_uInt16>(std::min( nTmp, static_cast<long>(nNumber) ));
190 if( lHeight < 0 )
192 long nTmp = ( aRect.Bottom() - aRect.Top() ) / -lHeight;
193 nNumber = static_cast<sal_uInt16>(std::min( nTmp, static_cast<long>(nNumber) ));
196 for( sal_uInt16 i = 1; i <= nNumber; i++ )
198 if( pProgress )
199 pProgress->SetState( i );
201 aRect = mpView->GetAllMarkedRect();
203 if( ( 1 == i ) && bColor )
205 SfxItemSet aNewSet( mpViewShell->GetPool(), svl::Items<XATTR_FILLSTYLE, XATTR_FILLCOLOR>{} );
206 aNewSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
207 aNewSet.Put( XFillColorItem( OUString(), aStartColor ) );
208 mpView->SetAttributes( aNewSet );
211 // make a copy of selected objects
212 mpView->CopyMarked();
214 // get newly selected objects
215 SdrMarkList aCopyMarkList( mpView->GetMarkedObjectList() );
216 const size_t nCopyMarkCount = aMarkList.GetMarkCount();
218 // set protection flags at marked copies to null
219 for( size_t j = 0; j < nCopyMarkCount; ++j )
221 pObj = aCopyMarkList.GetMark( j )->GetMarkedSdrObj();
223 if( pObj )
225 pObj->SetMoveProtect( false );
226 pObj->SetResizeProtect( false );
230 Fraction aWidth( aRect.Right() - aRect.Left() + lWidth, aRect.Right() - aRect.Left() );
231 Fraction aHeight( aRect.Bottom() - aRect.Top() + lHeight, aRect.Bottom() - aRect.Top() );
233 if( mpView->IsResizeAllowed() )
234 mpView->ResizeAllMarked( aRect.TopLeft(), aWidth, aHeight );
236 if( mpView->IsRotateAllowed() )
237 mpView->RotateAllMarked( aRect.Center(), lAngle );
239 if( mpView->IsMoveAllowed() )
240 mpView->MoveAllMarked( Size( lSizeX, lSizeY ) );
242 // set protection flags at marked copies to original values
243 if( nMarkCount == nCopyMarkCount )
245 for( size_t j = 0; j < nMarkCount; ++j )
247 SdrObject* pSrcObj = aMarkList.GetMark( j )->GetMarkedSdrObj();
248 SdrObject* pDstObj = aCopyMarkList.GetMark( j )->GetMarkedSdrObj();
250 if( pSrcObj && pDstObj &&
251 ( pSrcObj->GetObjInventor() == pDstObj->GetObjInventor() ) &&
252 ( pSrcObj->GetObjIdentifier() == pDstObj->GetObjIdentifier() ) )
254 pDstObj->SetMoveProtect( pSrcObj->IsMoveProtect() );
255 pDstObj->SetResizeProtect( pSrcObj->IsResizeProtect() );
260 if( bColor )
262 // probably room for optimizations, but may can lead to rounding errors
263 sal_uInt8 nRed = aStartColor.GetRed() + static_cast<sal_uInt8>( ( static_cast<long>(aEndColor.GetRed()) - static_cast<long>(aStartColor.GetRed()) ) * static_cast<long>(i) / static_cast<long>(nNumber) );
264 sal_uInt8 nGreen = aStartColor.GetGreen() + static_cast<sal_uInt8>( ( static_cast<long>(aEndColor.GetGreen()) - static_cast<long>(aStartColor.GetGreen()) ) * static_cast<long>(i) / static_cast<long>(nNumber) );
265 sal_uInt8 nBlue = aStartColor.GetBlue() + static_cast<sal_uInt8>( ( static_cast<long>(aEndColor.GetBlue()) - static_cast<long>(aStartColor.GetBlue()) ) * static_cast<long>(i) / static_cast<long>(nNumber) );
266 Color aNewColor( nRed, nGreen, nBlue );
267 SfxItemSet aNewSet( mpViewShell->GetPool(), svl::Items<XATTR_FILLSTYLE, XATTR_FILLCOLOR>{} );
268 aNewSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
269 aNewSet.Put( XFillColorItem( OUString(), aNewColor ) );
270 mpView->SetAttributes( aNewSet );
274 pProgress.reset();
276 if ( bWaiting )
277 mpDocSh->SetWaitCursor( false );
279 // show handles
280 mpView->AdjustMarkHdl(); //HMH sal_True );
281 //HMHpView->ShowMarkHdl();
283 mpView->EndUndo();
286 } // end of namespace
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */