bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / func / fucopy.cxx
blob5f6ee2ce0511dfbfc0231ec0722a0638b49b8d67
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 <svx/svxids.hrc>
24 #include "sdresid.hxx"
25 #include "sdattr.hxx"
26 #include "strings.hrc"
27 #include "ViewShell.hxx"
28 #include "View.hxx"
29 #include "drawdoc.hxx"
30 #include "DrawDocShell.hxx"
31 #include <vcl/wrkwin.hxx>
32 #include <svx/svdobj.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <sfx2/app.hxx>
35 #include <svx/xcolit.hxx>
36 #include <svx/xflclit.hxx>
37 #include <svx/xdef.hxx>
38 #include <svx/xfillit0.hxx>
39 #include <sfx2/request.hxx>
40 #include "sdabstdlg.hxx"
41 #include <boost/scoped_ptr.hpp>
43 using namespace com::sun::star;
45 namespace sd {
47 TYPEINIT1( FuCopy, FuPoor );
49 FuCopy::FuCopy (
50 ViewShell* pViewSh,
51 ::sd::Window* pWin,
52 ::sd::View* pView,
53 SdDrawDocument* pDoc,
54 SfxRequest& rReq)
55 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
59 rtl::Reference<FuPoor> FuCopy::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
61 rtl::Reference<FuPoor> xFunc( new FuCopy( pViewSh, pWin, pView, pDoc, rReq ) );
62 xFunc->DoExecute(rReq);
63 return xFunc;
66 void FuCopy::DoExecute( SfxRequest& rReq )
68 if( mpView->AreObjectsMarked() )
70 // Undo
71 OUString aString( mpView->GetDescriptionOfMarkedObjects() );
72 aString += " " + SD_RESSTR( STR_UNDO_COPYOBJECTS );
73 mpView->BegUndo( aString );
75 const SfxItemSet* pArgs = rReq.GetArgs();
77 if( !pArgs )
79 SfxItemSet aSet( mpViewShell->GetPool(),
80 ATTR_COPY_START, ATTR_COPY_END, 0 );
82 // indicate color attribute
83 SfxItemSet aAttr( mpDoc->GetPool() );
84 mpView->GetAttributes( aAttr );
85 const SfxPoolItem* pPoolItem = NULL;
87 if( SfxItemState::SET == aAttr.GetItemState( XATTR_FILLSTYLE, true, &pPoolItem ) )
89 drawing::FillStyle eStyle = static_cast<const XFillStyleItem*>(pPoolItem)->GetValue();
91 if( eStyle == drawing::FillStyle_SOLID &&
92 SfxItemState::SET == aAttr.GetItemState( XATTR_FILLCOLOR, true, &pPoolItem ) )
94 const XFillColorItem* pItem = static_cast<const XFillColorItem*>(pPoolItem);
95 XColorItem aXColorItem( ATTR_COPY_START_COLOR, pItem->GetName(),
96 pItem->GetColorValue() );
97 aSet.Put( aXColorItem );
102 SdAbstractDialogFactory* pFact = SdAbstractDialogFactory::Create();
103 if( pFact )
105 boost::scoped_ptr<AbstractCopyDlg> pDlg(pFact->CreateCopyDlg(NULL, aSet, mpDoc->GetColorList(), mpView ));
106 if (!pDlg)
107 return;
109 sal_uInt16 nResult = pDlg->Execute();
111 switch( nResult )
113 case RET_OK:
114 pDlg->GetAttr( aSet );
115 rReq.Done( aSet );
116 pArgs = rReq.GetArgs();
117 break;
119 default:
121 pDlg.reset();
122 mpView->EndUndo();
124 return; // Cancel
129 Rectangle aRect;
130 sal_Int32 lWidth = 0, lHeight = 0, lSizeX = 0L, lSizeY = 0L, lAngle = 0L;
131 sal_uInt16 nNumber = 0;
132 Color aStartColor, aEndColor;
133 bool bColor = false;
134 const SfxPoolItem* pPoolItem = NULL;
136 // Count
137 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_NUMBER, true, &pPoolItem ) )
138 nNumber = static_cast<const SfxUInt16Item*>( pPoolItem )->GetValue();
140 // translation
141 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_MOVE_X, true, &pPoolItem ) )
142 lSizeX = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
143 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_MOVE_Y, true, &pPoolItem ) )
144 lSizeY = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
145 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_ANGLE, true, &pPoolItem ) )
146 lAngle = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
148 // scale
149 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_WIDTH, true, &pPoolItem ) )
150 lWidth = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
151 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_HEIGHT, true, &pPoolItem ) )
152 lHeight = static_cast<const SfxInt32Item*>( pPoolItem )->GetValue();
154 // start/end color
155 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_START_COLOR, true, &pPoolItem ) )
157 aStartColor = static_cast<const XColorItem*>( pPoolItem )->GetColorValue();
158 bColor = true;
160 if( pArgs && SfxItemState::SET == pArgs->GetItemState( ATTR_COPY_END_COLOR, true, &pPoolItem ) )
162 aEndColor = static_cast<const XColorItem*>( pPoolItem )->GetColorValue();
163 if( aStartColor == aEndColor )
164 bColor = false;
166 else
167 bColor = false;
169 // remove handles
170 //HMHmpView->HideMarkHdl();
172 boost::scoped_ptr<SfxProgress> pProgress;
173 bool bWaiting = false;
175 if( nNumber > 1 )
177 OUString aStr( SD_RESSTR( STR_OBJECTS ) );
178 aStr += " " + SD_RESSTR( STR_UNDO_COPYOBJECTS );
180 pProgress.reset(new SfxProgress( mpDocSh, aStr, nNumber ));
181 mpDocSh->SetWaitCursor( true );
182 bWaiting = true;
185 const SdrMarkList aMarkList( mpView->GetMarkedObjectList() );
186 const size_t nMarkCount = aMarkList.GetMarkCount();
187 SdrObject* pObj = NULL;
189 // calculate number of possible copies
190 aRect = mpView->GetAllMarkedRect();
192 if( lWidth < 0L )
194 long nTmp = ( aRect.Right() - aRect.Left() ) / -lWidth;
195 nNumber = (sal_uInt16) std::min( nTmp, (long)nNumber );
198 if( lHeight < 0L )
200 long nTmp = ( aRect.Bottom() - aRect.Top() ) / -lHeight;
201 nNumber = (sal_uInt16) std::min( nTmp, (long)nNumber );
204 for( sal_uInt16 i = 1; i <= nNumber; i++ )
206 if( pProgress )
207 pProgress->SetState( i );
209 aRect = mpView->GetAllMarkedRect();
211 if( ( 1 == i ) && bColor )
213 SfxItemSet aNewSet( mpViewShell->GetPool(), XATTR_FILLSTYLE, XATTR_FILLCOLOR, 0L );
214 aNewSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
215 aNewSet.Put( XFillColorItem( OUString(), aStartColor ) );
216 mpView->SetAttributes( aNewSet );
219 // make a copy of selected objects
220 mpView->CopyMarked();
222 // get newly selected objects
223 SdrMarkList aCopyMarkList( mpView->GetMarkedObjectList() );
224 const size_t nCopyMarkCount = aMarkList.GetMarkCount();
226 // set protection flags at marked copies to null
227 for( size_t j = 0; j < nCopyMarkCount; ++j )
229 pObj = aCopyMarkList.GetMark( j )->GetMarkedSdrObj();
231 if( pObj )
233 pObj->SetMoveProtect( false );
234 pObj->SetResizeProtect( false );
238 Fraction aWidth( aRect.Right() - aRect.Left() + lWidth, aRect.Right() - aRect.Left() );
239 Fraction aHeight( aRect.Bottom() - aRect.Top() + lHeight, aRect.Bottom() - aRect.Top() );
241 if( mpView->IsResizeAllowed() )
242 mpView->ResizeAllMarked( aRect.TopLeft(), aWidth, aHeight );
244 if( mpView->IsRotateAllowed() )
245 mpView->RotateAllMarked( aRect.Center(), lAngle * 100 );
247 if( mpView->IsMoveAllowed() )
248 mpView->MoveAllMarked( Size( lSizeX, lSizeY ) );
250 // set protection flags at marked copies to original values
251 if( nMarkCount == nCopyMarkCount )
253 for( size_t j = 0; j < nMarkCount; ++j )
255 SdrObject* pSrcObj = aMarkList.GetMark( j )->GetMarkedSdrObj();
256 SdrObject* pDstObj = aCopyMarkList.GetMark( j )->GetMarkedSdrObj();
258 if( pSrcObj && pDstObj &&
259 ( pSrcObj->GetObjInventor() == pDstObj->GetObjInventor() ) &&
260 ( pSrcObj->GetObjIdentifier() == pDstObj->GetObjIdentifier() ) )
262 pDstObj->SetMoveProtect( pSrcObj->IsMoveProtect() );
263 pDstObj->SetResizeProtect( pSrcObj->IsResizeProtect() );
268 if( bColor )
270 // probably room for optimizations, but may can lead to rounding errors
271 sal_uInt8 nRed = aStartColor.GetRed() + (sal_uInt8) ( ( (long) aEndColor.GetRed() - (long) aStartColor.GetRed() ) * (long) i / (long) nNumber );
272 sal_uInt8 nGreen = aStartColor.GetGreen() + (sal_uInt8) ( ( (long) aEndColor.GetGreen() - (long) aStartColor.GetGreen() ) * (long) i / (long) nNumber );
273 sal_uInt8 nBlue = aStartColor.GetBlue() + (sal_uInt8) ( ( (long) aEndColor.GetBlue() - (long) aStartColor.GetBlue() ) * (long) i / (long) nNumber );
274 Color aNewColor( nRed, nGreen, nBlue );
275 SfxItemSet aNewSet( mpViewShell->GetPool(), XATTR_FILLSTYLE, XATTR_FILLCOLOR, 0L );
276 aNewSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
277 aNewSet.Put( XFillColorItem( OUString(), aNewColor ) );
278 mpView->SetAttributes( aNewSet );
282 pProgress.reset();
284 if ( bWaiting )
285 mpDocSh->SetWaitCursor( false );
287 // show handles
288 mpView->AdjustMarkHdl(); //HMH sal_True );
289 //HMHpView->ShowMarkHdl();
291 mpView->EndUndo();
295 } // end of namespace
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */