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 <fuconcs.hxx>
21 #include <rtl/ustring.hxx>
22 
23 #include <svx/svxids.hrc>
24 
25 #include <sfx2/viewfrm.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/request.hxx>
28 #include <editeng/adjustitem.hxx>
29 #include <editeng/eeitem.hxx>
30 #include <svx/svdoashp.hxx>
31 #include <svx/sdtagitm.hxx>
32 
33 #include <ViewShell.hxx>
34 #include <ViewShellBase.hxx>
35 #include <ToolBarManager.hxx>
36 #include <svx/gallery.hxx>
37 #include <svx/sdooitm.hxx>
38 #include <svl/itempool.hxx>
39 #include <svl/stritem.hxx>
40 
41 #include <View.hxx>
42 #include <Window.hxx>
43 #include <drawdoc.hxx>
44 
45 namespace sd {
46 
47 
FuConstructCustomShape(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDoc,SfxRequest & rReq)48 FuConstructCustomShape::FuConstructCustomShape (
49         ViewShell*          pViewSh,
50         ::sd::Window*       pWin,
51         ::sd::View*         pView,
52         SdDrawDocument*     pDoc,
53         SfxRequest&         rReq ) :
54     FuConstruct(pViewSh, pWin, pView, pDoc, rReq)
55 {
56 }
57 
Create(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDoc,SfxRequest & rReq,bool bPermanent)58 rtl::Reference<FuPoor> FuConstructCustomShape::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq, bool bPermanent )
59 {
60     FuConstructCustomShape* pFunc;
61     rtl::Reference<FuPoor> xFunc( pFunc = new FuConstructCustomShape( pViewSh, pWin, pView, pDoc, rReq ) );
62     xFunc->DoExecute(rReq);
63     pFunc->SetPermanent( bPermanent );
64     return xFunc;
65 }
66 
DoExecute(SfxRequest & rReq)67 void FuConstructCustomShape::DoExecute( SfxRequest& rReq )
68 {
69     FuConstruct::DoExecute( rReq );
70 
71     const SfxItemSet* pArgs = rReq.GetArgs();
72     if ( pArgs )
73     {
74         const SfxStringItem& rItm = static_cast<const SfxStringItem&>(pArgs->Get( rReq.GetSlot() ));
75         aCustomShape = rItm.GetValue();
76     }
77 
78     mpViewShell->GetViewShellBase().GetToolBarManager()->SetToolBar(
79         ToolBarManager::ToolBarGroup::Function,
80         ToolBarManager::msDrawingObjectToolBar);
81 }
82 
MouseButtonDown(const MouseEvent & rMEvt)83 bool FuConstructCustomShape::MouseButtonDown(const MouseEvent& rMEvt)
84 {
85     bool bReturn = FuConstruct::MouseButtonDown(rMEvt);
86 
87     if ( rMEvt.IsLeft() && !mpView->IsAction() )
88     {
89         Point aPnt( mpWindow->PixelToLogic( rMEvt.GetPosPixel() ) );
90 
91         mpWindow->CaptureMouse();
92         sal_uInt16 nDrgLog = sal_uInt16 ( mpWindow->PixelToLogic(Size(DRGPIX,0)).Width() );
93 
94         mpView->BegCreateObj(aPnt, nullptr, nDrgLog);
95 
96         SdrObject* pObj = mpView->GetCreateObj();
97         if ( pObj )
98         {
99             SetAttributes( pObj );
100             bool bForceFillStyle = true;
101             bool bForceNoFillStyle = false;
102             if ( static_cast<SdrObjCustomShape*>(pObj)->UseNoFillStyle() )
103             {
104                 bForceFillStyle = false;
105                 bForceNoFillStyle = true;
106             }
107             SfxItemSet aAttr(mpDoc->GetPool());
108             SetStyleSheet( aAttr, pObj, bForceFillStyle, bForceNoFillStyle );
109             pObj->SetMergedItemSet(aAttr);
110         }
111     }
112 
113     return bReturn;
114 }
115 
MouseButtonUp(const MouseEvent & rMEvt)116 bool FuConstructCustomShape::MouseButtonUp(const MouseEvent& rMEvt)
117 {
118     bool bReturn(false);
119 
120     if(mpView->IsCreateObj() && rMEvt.IsLeft())
121     {
122         SdrObject* pObj = mpView->GetCreateObj();
123         if( pObj && mpView->EndCreateObj( SdrCreateCmd::ForceEnd ) )
124         {
125             bReturn = true;
126         }
127     }
128     bReturn = FuConstruct::MouseButtonUp (rMEvt) || bReturn;
129 
130     if (!bPermanent)
131         mpViewShell->GetViewFrame()->GetDispatcher()->Execute(SID_OBJECT_SELECT, SfxCallMode::ASYNCHRON);
132 
133     return bReturn;
134 }
135 
Activate()136 void FuConstructCustomShape::Activate()
137 {
138     mpView->SetCurrentObj( OBJ_CUSTOMSHAPE );
139     FuConstruct::Activate();
140 }
141 
142 /**
143  * set attribute for the object to be created
144  */
SetAttributes(SdrObject * pObj)145 void FuConstructCustomShape::SetAttributes( SdrObject* pObj )
146 {
147     bool bAttributesAppliedFromGallery = false;
148 
149     if ( GalleryExplorer::GetSdrObjCount( GALLERY_THEME_POWERPOINT ) )
150     {
151         std::vector< OUString > aObjList;
152         if ( GalleryExplorer::FillObjListTitle( GALLERY_THEME_POWERPOINT, aObjList ) )
153         {
154             for ( std::vector<OUString>::size_type i = 0; i < aObjList.size(); i++ )
155             {
156                 if ( aObjList[ i ].equalsIgnoreAsciiCase( aCustomShape ) )
157                 {
158                     FmFormModel aFormModel;
159                     SfxItemPool& rPool(aFormModel.GetItemPool());
160                     rPool.FreezeIdRanges();
161 
162                     if ( GalleryExplorer::GetSdrObj( GALLERY_THEME_POWERPOINT, i, &aFormModel ) )
163                     {
164                         const SdrPage* pPage = aFormModel.GetPage( 0 );
165                         if ( pPage )
166                         {
167                             const SdrObject* pSourceObj = pPage->GetObj( 0 );
168                             if( pSourceObj )
169                             {
170                                 const SfxItemSet& rSource = pSourceObj->GetMergedItemSet();
171                                 SfxItemSet aDest(
172                                     pObj->getSdrModelFromSdrObject().GetItemPool(),
173                                     svl::Items<
174                                         // Ranges from SdrAttrObj:
175                                         SDRATTR_START, SDRATTR_SHADOW_LAST,
176                                         SDRATTR_MISC_FIRST, SDRATTR_MISC_LAST,
177                                         SDRATTR_TEXTDIRECTION,
178                                             SDRATTR_TEXTDIRECTION,
179                                         // Graphic attributes, 3D properties,
180                                         // CustomShape properties:
181                                         SDRATTR_GRAF_FIRST,
182                                             SDRATTR_CUSTOMSHAPE_LAST,
183                                         // Range from SdrTextObj:
184                                         EE_ITEMS_START, EE_ITEMS_END>{});
185                                 aDest.Set( rSource );
186                                 pObj->SetMergedItemSet( aDest );
187                                 // Enables Word-wrap by default (tdf#134369)
188                                 pObj->SetMergedItem( SdrOnOffItem( SDRATTR_TEXT_WORDWRAP, true ) );
189                                 Degree100 nAngle = pSourceObj->GetRotateAngle();
190                                 if ( nAngle )
191                                     pObj->NbcRotate( pObj->GetSnapRect().Center(), nAngle );
192                                 bAttributesAppliedFromGallery = true;
193                             }
194                         }
195                     }
196                     break;
197                 }
198             }
199         }
200     }
201     if ( !bAttributesAppliedFromGallery )
202     {
203         pObj->SetMergedItem( SvxAdjustItem( SvxAdjust::Center, EE_PARA_JUST ) );
204         pObj->SetMergedItem( SdrTextVertAdjustItem( SDRTEXTVERTADJUST_CENTER ) );
205         pObj->SetMergedItem( SdrTextHorzAdjustItem( SDRTEXTHORZADJUST_BLOCK ) );
206         pObj->SetMergedItem( makeSdrTextAutoGrowHeightItem( false ) );
207         static_cast<SdrObjCustomShape*>(pObj)->MergeDefaultAttributes( &aCustomShape );
208     }
209 }
210 
GetShapeType() const211 const OUString& FuConstructCustomShape::GetShapeType() const
212 {
213     return aCustomShape;
214 }
215 
CreateDefaultObject(const sal_uInt16,const::tools::Rectangle & rRectangle)216 SdrObjectUniquePtr FuConstructCustomShape::CreateDefaultObject(const sal_uInt16, const ::tools::Rectangle& rRectangle)
217 {
218     SdrObjectUniquePtr pObj(SdrObjFactory::MakeNewObject(
219         mpView->getSdrModelFromSdrView(),
220         mpView->GetCurrentObjInventor(),
221         mpView->GetCurrentObjIdentifier()));
222 
223     if( pObj )
224     {
225         ::tools::Rectangle aRect( rRectangle );
226         if ( doConstructOrthogonal() )
227             ImpForceQuadratic( aRect );
228         pObj->SetLogicRect( aRect );
229         SetAttributes( pObj.get() );
230         SfxItemSet aAttr(mpDoc->GetPool());
231         SetStyleSheet(aAttr, pObj.get());
232         pObj->SetMergedItemSet(aAttr);
233     }
234     return pObj;
235 }
236 
237 // #i33136#
doConstructOrthogonal() const238 bool FuConstructCustomShape::doConstructOrthogonal() const
239 {
240     return SdrObjCustomShape::doConstructOrthogonal(aCustomShape);
241 }
242 
243 } // end of namespace sd
244 
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
246