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 <DrawViewWrapper.hxx>
21 #include <chartview/DrawModelWrapper.hxx>
22 #include <ConfigurationAccess.hxx>
23 
24 #include <unotools/lingucfg.hxx>
25 #include <editeng/langitem.hxx>
26 #include <svl/intitem.hxx>
27 #include <svl/itempool.hxx>
28 #include <svx/obj3d.hxx>
29 #include <svx/svdpagv.hxx>
30 #include <svx/svdmodel.hxx>
31 #include <svx/svdetc.hxx>
32 #include <svx/svdoutl.hxx>
33 #include <svx/svxids.hrc>
34 #include <svx/unoshape.hxx>
35 #include <editeng/fhgtitem.hxx>
36 
37 #include <com/sun/star/frame/XModel.hpp>
38 
39 #include <sfx2/objsh.hxx>
40 #include <svx/helperhittest3d.hxx>
41 
42 using namespace ::com::sun::star;
43 
44 namespace chart
45 {
46 
47 namespace
48 {
lcl_getHitTolerance(OutputDevice const * pOutDev)49     short lcl_getHitTolerance( OutputDevice const * pOutDev )
50     {
51         const short HITPIX=2; //hit-tolerance in pixel
52         short nHitTolerance = 50;
53         if(pOutDev)
54             nHitTolerance = static_cast<short>(pOutDev->PixelToLogic(Size(HITPIX,0)).Width());
55         return nHitTolerance;
56     }
57 
58 // this code is copied from sfx2/source/doc/objembed.cxx. It is a workaround to
59 // get the reference device (e.g. printer) from the parent document
lcl_GetParentRefDevice(const uno::Reference<frame::XModel> & xModel)60 OutputDevice * lcl_GetParentRefDevice( const uno::Reference< frame::XModel > & xModel )
61 {
62     SfxObjectShell* pParent = SfxObjectShell::GetParentShell(xModel);
63     if ( pParent )
64         return pParent->GetDocumentRefDev();
65     return nullptr;
66 }
67 
68 }
69 
DrawViewWrapper(SdrModel & rSdrModel,OutputDevice * pOut)70 DrawViewWrapper::DrawViewWrapper(
71     SdrModel& rSdrModel,
72     OutputDevice* pOut)
73 :   E3dView(rSdrModel, pOut)
74     ,m_pMarkHandleProvider(nullptr)
75     ,m_apOutliner(SdrMakeOutliner(OutlinerMode::TextObject, rSdrModel))
76     ,m_bRestoreMapMode( false )
77 {
78     SetBufferedOutputAllowed(true);
79     SetBufferedOverlayAllowed(true);
80     SetPagePaintingAllowed(true);
81 
82     // #i12587# support for shapes in chart
83     SdrOutliner* pOutliner = getOutliner();
84     SfxItemPool* pOutlinerPool = ( pOutliner ? pOutliner->GetEditTextObjectPool() : nullptr );
85     if ( pOutlinerPool )
86     {
87         SvtLinguConfig aLinguConfig;
88         SvtLinguOptions aLinguOptions;
89         aLinguConfig.GetOptions( aLinguOptions );
90         pOutlinerPool->SetPoolDefaultItem( SvxLanguageItem( aLinguOptions.nDefaultLanguage, EE_CHAR_LANGUAGE ) );
91         pOutlinerPool->SetPoolDefaultItem( SvxLanguageItem( aLinguOptions.nDefaultLanguage_CJK, EE_CHAR_LANGUAGE_CJK ) );
92         pOutlinerPool->SetPoolDefaultItem( SvxLanguageItem( aLinguOptions.nDefaultLanguage_CTL, EE_CHAR_LANGUAGE_CTL ) );
93 
94         // set font height without changing SdrEngineDefaults
95         pOutlinerPool->SetPoolDefaultItem( SvxFontHeightItem( 423, 100, EE_CHAR_FONTHEIGHT ) );  // 12pt
96     }
97 
98     // #i121463# Use big handles by default
99     SetMarkHdlSizePixel(9);
100 
101     ReInit();
102 }
103 
ReInit()104 void DrawViewWrapper::ReInit()
105 {
106     OutputDevice* pOutDev = GetFirstOutputDevice();
107     Size aOutputSize(100,100);
108     if(pOutDev)
109         aOutputSize = pOutDev->GetOutputSize();
110 
111     mbPageVisible = false;
112     mbPageBorderVisible = false;
113     mbBordVisible = false;
114     mbGridVisible = false;
115     mbHlplVisible = false;
116 
117     SetNoDragXorPolys(true);//for interactive 3D resize-dragging: paint only a single rectangle (not a simulated 3D object)
118 
119     //a correct work area is at least necessary for correct values in the position and  size dialog
120     tools::Rectangle aRect(Point(0,0), aOutputSize);
121     SetWorkArea(aRect);
122 
123     ShowSdrPage(GetModel()->GetPage(0));
124 }
125 
~DrawViewWrapper()126 DrawViewWrapper::~DrawViewWrapper()
127 {
128     maComeBackIdle.Stop();//@todo this should be done in destructor of base class
129     UnmarkAllObj();//necessary to avoid a paint call during the destructor hierarchy
130 }
131 
GetPageView() const132 SdrPageView* DrawViewWrapper::GetPageView() const
133 {
134     SdrPageView* pSdrPageView = GetSdrPageView();
135     return pSdrPageView;
136 };
137 
SetMarkHandles(SfxViewShell * pOtherShell)138 void DrawViewWrapper::SetMarkHandles(SfxViewShell* pOtherShell)
139 {
140     if( m_pMarkHandleProvider && m_pMarkHandleProvider->getMarkHandles( maHdlList ) )
141         return;
142     else
143         SdrView::SetMarkHandles(pOtherShell);
144 }
145 
getHitObject(const Point & rPnt) const146 SdrObject* DrawViewWrapper::getHitObject( const Point& rPnt ) const
147 {
148     SdrPageView* pSdrPageView = GetPageView();
149     SdrObject* pRet = SdrView::PickObj(rPnt, lcl_getHitTolerance( GetFirstOutputDevice() ), pSdrPageView,
150                                        SdrSearchOptions::DEEP | SdrSearchOptions::TESTMARKABLE);
151 
152     if( pRet )
153     {
154         // ignore some special shapes
155         OUString aShapeName = pRet->GetName();
156 
157         // return right away if it is a field button
158         if (aShapeName.startsWith("FieldButton"))
159             return pRet;
160 
161         if( aShapeName.match("PlotAreaIncludingAxes") || aShapeName.match("PlotAreaExcludingAxes") )
162         {
163             pRet->SetMarkProtect( true );
164             return getHitObject( rPnt );
165         }
166 
167         //3d objects need a special treatment
168         //because the simple PickObj method is not accurate in this case for performance reasons
169         E3dObject* pE3d = dynamic_cast< E3dObject* >(pRet);
170         if( pE3d )
171         {
172             E3dScene* pScene(pE3d->getRootE3dSceneFromE3dObject());
173 
174             if(nullptr != pScene)
175             {
176                 // prepare result vector and call helper
177                 std::vector< const E3dCompoundObject* > aHitList;
178                 const basegfx::B2DPoint aHitPoint(rPnt.X(), rPnt.Y());
179                 getAllHit3DObjectsSortedFrontToBack(aHitPoint, *pScene, aHitList);
180 
181                 if(!aHitList.empty())
182                 {
183                     // choose the frontmost hit 3D object of the scene
184                     pRet = const_cast< E3dCompoundObject* >(aHitList[0]);
185                 }
186             }
187         }
188     }
189     return pRet;
190 }
191 
MarkObject(SdrObject * pObj)192 void DrawViewWrapper::MarkObject( SdrObject* pObj )
193 {
194     bool bFrameDragSingles = true;//true == green == surrounding handles
195     if(pObj)
196         pObj->SetMarkProtect(false);
197     if( m_pMarkHandleProvider )
198         bFrameDragSingles = m_pMarkHandleProvider->getFrameDragSingles();
199 
200     SetFrameDragSingles(bFrameDragSingles);//decide whether each single object should get handles
201     SdrView::MarkObj( pObj, GetPageView() );
202     showMarkHandles();
203 }
204 
setMarkHandleProvider(MarkHandleProvider * pMarkHandleProvider)205 void DrawViewWrapper::setMarkHandleProvider( MarkHandleProvider* pMarkHandleProvider )
206 {
207     m_pMarkHandleProvider = pMarkHandleProvider;
208 }
209 
CompleteRedraw(OutputDevice * pOut,const vcl::Region & rReg,sdr::contact::ViewObjectContactRedirector *)210 void DrawViewWrapper::CompleteRedraw(OutputDevice* pOut, const vcl::Region& rReg, sdr::contact::ViewObjectContactRedirector* /* pRedirector */)
211 {
212     svtools::ColorConfig aColorConfig;
213     Color aFillColor( aColorConfig.GetColorValue( svtools::DOCCOLOR ).nColor );
214     SetApplicationBackgroundColor(aFillColor);
215     E3dView::CompleteRedraw( pOut, rReg );
216 }
217 
getSelectedObject() const218 SdrObject* DrawViewWrapper::getSelectedObject() const
219 {
220     SdrObject* pObj(nullptr);
221     const SdrMarkList& rMarkList = GetMarkedObjectList();
222     if(rMarkList.GetMarkCount() == 1)
223     {
224         SdrMark* pMark = rMarkList.GetMark(0);
225         pObj = pMark->GetMarkedSdrObj();
226     }
227     return pObj;
228 }
229 
getTextEditObject() const230 SdrObject* DrawViewWrapper::getTextEditObject() const
231 {
232     SdrObject* pObj = getSelectedObject();
233     SdrObject* pTextObj = nullptr;
234     if( pObj && pObj->HasTextEdit())
235         pTextObj = pObj;
236     return pTextObj;
237 }
238 
attachParentReferenceDevice(const uno::Reference<frame::XModel> & xChartModel)239 void DrawViewWrapper::attachParentReferenceDevice( const uno::Reference< frame::XModel > & xChartModel )
240 {
241     OutputDevice * pParentRefDev( lcl_GetParentRefDevice( xChartModel ));
242     SdrOutliner * pOutliner( getOutliner());
243     if( pParentRefDev && pOutliner )
244     {
245         pOutliner->SetRefDevice( pParentRefDev );
246     }
247 }
248 
getOutliner() const249 SdrOutliner* DrawViewWrapper::getOutliner() const
250 {
251     return m_apOutliner.get();
252 }
253 
getPositionAndSizeItemSetFromMarkedObject() const254 SfxItemSet DrawViewWrapper::getPositionAndSizeItemSetFromMarkedObject() const
255 {
256     SfxItemSet aFullSet(
257         GetModel()->GetItemPool(),
258         svl::Items<
259             SDRATTR_CORNER_RADIUS, SDRATTR_CORNER_RADIUS,
260             SID_ATTR_TRANSFORM_POS_X, SID_ATTR_TRANSFORM_ANGLE,
261             SID_ATTR_TRANSFORM_PROTECT_POS, SID_ATTR_TRANSFORM_AUTOHEIGHT,
262             SID_ATTR_METRIC, SID_ATTR_METRIC>{});
263     SfxItemSet aGeoSet( E3dView::GetGeoAttrFromMarked() );
264     aFullSet.Put( aGeoSet );
265     aFullSet.Put( SfxUInt16Item(SID_ATTR_METRIC,static_cast< sal_uInt16 >( ConfigurationAccess::getFieldUnit())));
266     return aFullSet;
267 }
268 
getNamedSdrObject(const OUString & rName) const269 SdrObject* DrawViewWrapper::getNamedSdrObject( const OUString& rName ) const
270 {
271     if(rName.isEmpty())
272         return nullptr;
273     SdrPageView* pSdrPageView = GetPageView();
274     if( pSdrPageView )
275     {
276         return DrawModelWrapper::getNamedSdrObject( rName, pSdrPageView->GetObjList() );
277     }
278     return nullptr;
279 }
280 
IsObjectHit(SdrObject const * pObj,const Point & rPnt)281 bool DrawViewWrapper::IsObjectHit( SdrObject const * pObj, const Point& rPnt )
282 {
283     if(pObj)
284     {
285         tools::Rectangle aRect(pObj->GetCurrentBoundRect());
286         return aRect.IsInside(rPnt);
287     }
288     return false;
289 }
290 
Notify(SfxBroadcaster & rBC,const SfxHint & rHint)291 void DrawViewWrapper::Notify(SfxBroadcaster& rBC, const SfxHint& rHint)
292 {
293     //prevent wrong reselection of objects
294     SdrModel* pSdrModel( GetModel() );
295     if( pSdrModel && pSdrModel->isLocked() )
296         return;
297 
298     const SdrHint* pSdrHint = ( rHint.GetId() == SfxHintId::ThisIsAnSdrHint ? static_cast<const SdrHint*>(&rHint) : nullptr );
299 
300     //#i76053# do nothing when only changes on the hidden draw page were made ( e.g. when the symbols for the dialogs are created )
301     SdrPageView* pSdrPageView = GetPageView();
302     if( pSdrHint && pSdrPageView )
303     {
304         if( pSdrPageView->GetPage() != pSdrHint->GetPage() )
305             return;
306     }
307 
308     E3dView::Notify(rBC, rHint);
309 
310     if( pSdrHint == nullptr )
311         return;
312 
313     SdrHintKind eKind = pSdrHint->GetKind();
314     if( eKind == SdrHintKind::BeginEdit )
315     {
316         // #i79965# remember map mode
317         OSL_ASSERT( ! m_bRestoreMapMode );
318         OutputDevice* pOutDev = GetFirstOutputDevice();
319         if( pOutDev )
320         {
321             m_aMapModeToRestore = pOutDev->GetMapMode();
322             m_bRestoreMapMode = true;
323         }
324     }
325     else if( eKind == SdrHintKind::EndEdit )
326     {
327         // #i79965# scroll back view when ending text edit
328         OSL_ASSERT( m_bRestoreMapMode );
329         if( m_bRestoreMapMode )
330         {
331             OutputDevice* pOutDev = GetFirstOutputDevice();
332             if( pOutDev )
333             {
334                 pOutDev->SetMapMode( m_aMapModeToRestore );
335                 m_bRestoreMapMode = false;
336             }
337         }
338     }
339 }
340 
getSdrObject(const uno::Reference<drawing::XShape> & xShape)341 SdrObject* DrawViewWrapper::getSdrObject( const uno::Reference<
342                     drawing::XShape >& xShape )
343 {
344     SdrObject* pRet = nullptr;
345     uno::Reference< lang::XTypeProvider > xTypeProvider( xShape, uno::UNO_QUERY );
346     if(xTypeProvider.is())
347     {
348         pRet = SdrObject::getSdrObjectFromXShape(xShape);
349     }
350     return pRet;
351 }
352 
353 } //namespace chart
354 
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
356