1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <SdUnoDrawView.hxx>
22 #include <DrawController.hxx>
23 #include <DrawDocShell.hxx>
24 #include <DrawViewShell.hxx>
25 #include <drawdoc.hxx>
26 #include "unolayer.hxx"
27 #include <unomodel.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <sfx2/dispatch.hxx>
34 #include <sfx2/viewfrm.hxx>
35 #include <svx/svdpagv.hxx>
36 #include <svx/unopage.hxx>
37 #include <sfx2/zoomitem.hxx>
38 #include <com/sun/star/drawing/DrawViewMode.hpp>
39 #include <com/sun/star/drawing/ShapeCollection.hpp>
40 #include <com/sun/star/drawing/XLayerManager.hpp>
41 #include <com/sun/star/view/DocumentZoomType.hpp>
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::drawing
;
51 SdUnoDrawView::SdUnoDrawView(
52 DrawViewShell
& rViewShell
,
54 : mrDrawViewShell(rViewShell
),
59 SdUnoDrawView::~SdUnoDrawView() noexcept
63 bool SdUnoDrawView::getMasterPageMode() const noexcept
65 return (mrDrawViewShell
.GetEditMode() == EditMode::MasterPage
);
68 void SdUnoDrawView::setMasterPageMode (bool bMasterPageMode
) noexcept
70 if ((mrDrawViewShell
.GetEditMode() == EditMode::MasterPage
) != bMasterPageMode
)
72 mrDrawViewShell
.ChangeEditMode (
73 bMasterPageMode
? EditMode::MasterPage
: EditMode::Page
,
74 mrDrawViewShell
.IsLayerModeActive());
78 bool SdUnoDrawView::getLayerMode() const noexcept
80 return mrDrawViewShell
.IsLayerModeActive();
83 void SdUnoDrawView::setLayerMode (bool bLayerMode
) noexcept
85 if (mrDrawViewShell
.IsLayerModeActive() != bLayerMode
)
87 mrDrawViewShell
.ChangeEditMode (
88 mrDrawViewShell
.GetEditMode(),
93 Reference
<drawing::XLayer
> SdUnoDrawView::getActiveLayer() const
95 Reference
<drawing::XLayer
> xCurrentLayer
;
99 // Retrieve the layer manager from the model.
100 SdXImpressDocument
* pModel
= GetModel();
101 if (pModel
== nullptr)
104 SdDrawDocument
* pSdModel
= pModel
->GetDoc();
105 if (pSdModel
== nullptr)
108 // From the model get the current SdrLayer object via the layer admin.
109 SdrLayerAdmin
& rLayerAdmin
= pSdModel
->GetLayerAdmin ();
110 SdrLayer
* pLayer
= rLayerAdmin
.GetLayer (mrView
.GetActiveLayer());
111 if (pLayer
== nullptr)
114 // Get the corresponding XLayer object from the implementation
115 // object of the layer manager.
116 Reference
<drawing::XLayerManager
> xManager (pModel
->getLayerManager(), uno::UNO_QUERY
);
117 SdLayerManager
* pManager
= dynamic_cast<SdLayerManager
*> (xManager
.get());
118 if (pManager
!= nullptr)
119 xCurrentLayer
= pManager
->GetLayer (pLayer
);
123 return xCurrentLayer
;
126 void SdUnoDrawView::setActiveLayer (const Reference
<drawing::XLayer
>& rxLayer
)
128 // Get the SdrLayer object corresponding to the given reference.
132 SdLayer
* pLayer
= dynamic_cast<SdLayer
*> (rxLayer
.get());
133 if (pLayer
== nullptr)
136 SdrLayer
* pSdrLayer
= pLayer
->GetSdrLayer();
137 if (pSdrLayer
== nullptr)
140 // Set the new active layer and make the change visible.
141 mrView
.SetActiveLayer (pSdrLayer
->GetName());
142 mrDrawViewShell
.ResetActualLayer ();
145 // XSelectionSupplier
147 sal_Bool SAL_CALL
SdUnoDrawView::select( const Any
& aSelection
)
151 ::std::vector
<SdrObject
*> aObjects
;
153 SdrPage
* pSdrPage
= nullptr;
155 Reference
< drawing::XShape
> xShape
;
156 aSelection
>>= xShape
;
160 SdrObject
* pObj
= SdrObject::getSdrObjectFromXShape( xShape
);
163 pSdrPage
= pObj
->getSdrPageFromSdrObject();
164 aObjects
.push_back( pObj
);
173 Reference
< drawing::XShapes
> xShapes
;
174 aSelection
>>= xShapes
;
177 const sal_uInt32 nCount
= xShapes
->getCount();
178 for( sal_uInt32 i
= 0; i
< nCount
; i
++ )
180 xShapes
->getByIndex(i
) >>= xShape
;
183 SdrObject
* pObj
= SdrObject::getSdrObjectFromXShape(xShape
);
190 if( pSdrPage
== nullptr )
192 pSdrPage
= pObj
->getSdrPageFromSdrObject();
194 else if( pSdrPage
!= pObj
->getSdrPageFromSdrObject() )
200 aObjects
.push_back( pObj
);
210 setMasterPageMode( pSdrPage
->IsMasterPage() );
211 mrDrawViewShell
.SwitchPage( (pSdrPage
->GetPageNum() - 1) >> 1 );
212 mrDrawViewShell
.WriteFrameViewData();
215 SdrPageView
*pPV
= mrView
.GetSdrPageView();
219 // first deselect all
220 mrView
.UnmarkAllObj( pPV
);
222 for( SdrObject
* pObj
: aObjects
)
224 mrView
.MarkObj( pObj
, pPV
);
236 Any SAL_CALL
SdUnoDrawView::getSelection()
240 if( mrView
.IsTextEdit() )
241 mrView
.getTextSelection( aAny
);
243 if( !aAny
.hasValue() )
245 const SdrMarkList
& rMarkList
= mrView
.GetMarkedObjectList();
246 const size_t nCount
= rMarkList
.GetMarkCount();
249 Reference
< drawing::XShapes
> xShapes
= drawing::ShapeCollection::create(
250 comphelper::getProcessComponentContext());
251 for( size_t nNum
= 0; nNum
< nCount
; ++nNum
)
253 SdrMark
*pMark
= rMarkList
.GetMark(nNum
);
257 SdrObject
*pObj
= pMark
->GetMarkedSdrObj();
258 if(pObj
==nullptr || pObj
->getSdrPageFromSdrObject() == nullptr)
261 Reference
< drawing::XDrawPage
> xPage( pObj
->getSdrPageFromSdrObject()->getUnoPage(), UNO_QUERY
);
266 SvxDrawPage
* pDrawPage
= comphelper::getFromUnoTunnel
<SvxDrawPage
>( xPage
);
268 if(pDrawPage
==nullptr)
271 Reference
< drawing::XShape
> xShape( pObj
->getUnoShape(), UNO_QUERY
);
274 xShapes
->add(xShape
);
283 void SAL_CALL
SdUnoDrawView::addSelectionChangeListener (
284 const css::uno::Reference
<css::view::XSelectionChangeListener
>&)
287 void SAL_CALL
SdUnoDrawView::removeSelectionChangeListener (
288 const css::uno::Reference
<css::view::XSelectionChangeListener
>&)
291 void SdUnoDrawView::setFastPropertyValue (
297 case DrawController::PROPERTY_CURRENTPAGE
:
299 Reference
< drawing::XDrawPage
> xPage
;
301 setCurrentPage( xPage
);
305 case DrawController::PROPERTY_MASTERPAGEMODE
:
309 setMasterPageMode( bValue
);
313 case DrawController::PROPERTY_LAYERMODE
:
317 setLayerMode( bValue
);
320 case DrawController::PROPERTY_ACTIVE_LAYER
:
322 Reference
<drawing::XLayer
> xLayer
;
324 setActiveLayer (xLayer
);
327 case DrawController::PROPERTY_ZOOMVALUE
:
334 case DrawController::PROPERTY_ZOOMTYPE
:
338 SetZoomType( nType
);
341 case DrawController::PROPERTY_VIEWOFFSET
:
345 SetViewOffset( aOffset
);
349 throw beans::UnknownPropertyException( OUString::number(nHandle
), static_cast<cppu::OWeakObject
*>(this));
353 Any SAL_CALL
SdUnoDrawView::getFastPropertyValue (
359 case DrawController::PROPERTY_CURRENTPAGE
:
360 aValue
<<= getCurrentPage();
363 case DrawController::PROPERTY_MASTERPAGEMODE
:
364 aValue
<<= getMasterPageMode();
367 case DrawController::PROPERTY_LAYERMODE
:
368 aValue
<<= getLayerMode();
371 case DrawController::PROPERTY_ACTIVE_LAYER
:
372 aValue
<<= getActiveLayer();
375 case DrawController::PROPERTY_ZOOMVALUE
:
376 aValue
<<= GetZoom();
378 case DrawController::PROPERTY_ZOOMTYPE
:
379 aValue
<<= sal_Int16(css::view::DocumentZoomType::BY_VALUE
);
381 case DrawController::PROPERTY_VIEWOFFSET
:
382 aValue
<<= GetViewOffset();
385 case DrawController::PROPERTY_DRAWVIEWMODE
:
386 aValue
= getDrawViewMode();
390 throw beans::UnknownPropertyException( OUString::number(nHandle
), static_cast<cppu::OWeakObject
*>(this));
398 void SAL_CALL
SdUnoDrawView::setCurrentPage (
399 const Reference
< drawing::XDrawPage
>& xPage
)
401 SvxDrawPage
* pDrawPage
= comphelper::getFromUnoTunnel
<SvxDrawPage
>( xPage
);
402 SdrPage
*pSdrPage
= pDrawPage
? pDrawPage
->GetSdrPage() : nullptr;
406 // End editing of text. Otherwise the edited text object would
407 // still be visible on the new page.
408 mrDrawViewShell
.GetView()->SdrEndTextEdit();
410 setMasterPageMode( pSdrPage
->IsMasterPage() );
411 mrDrawViewShell
.SwitchPage( (pSdrPage
->GetPageNum() - 1) >> 1 );
412 mrDrawViewShell
.WriteFrameViewData();
416 Reference
< drawing::XDrawPage
> SAL_CALL
SdUnoDrawView::getCurrentPage()
418 Reference
< drawing::XDrawPage
> xPage
;
420 SdrPageView
*pPV
= mrView
.GetSdrPageView();
421 SdrPage
* pPage
= pPV
? pPV
->GetPage() : nullptr;
424 xPage
.set( pPage
->getUnoPage(), UNO_QUERY
);
429 sal_Int16
SdUnoDrawView::GetZoom() const
431 if (mrDrawViewShell
.GetActiveWindow() )
433 return static_cast<sal_Int16
>(mrDrawViewShell
.GetActiveWindow()->GetZoom());
441 void SdUnoDrawView::SetZoom( sal_Int16 nZoom
)
443 SvxZoomItem
aZoomItem( SvxZoomType::PERCENT
, nZoom
);
445 SfxViewFrame
* pViewFrame
= mrDrawViewShell
.GetViewFrame();
448 SfxDispatcher
* pDispatcher
= pViewFrame
->GetDispatcher();
451 pDispatcher
->ExecuteList(SID_ATTR_ZOOM
, SfxCallMode::SYNCHRON
,
457 void SdUnoDrawView::SetViewOffset(const awt::Point
& rWinPos
)
459 Point
aWinPos( rWinPos
.X
, rWinPos
.Y
);
460 aWinPos
+= mrDrawViewShell
.GetViewOrigin();
461 mrDrawViewShell
.SetWinViewPos( aWinPos
);
464 awt::Point
SdUnoDrawView::GetViewOffset() const
466 Point aRet
= mrDrawViewShell
.GetWinViewPos();
467 aRet
-= mrDrawViewShell
.GetViewOrigin();
469 return awt::Point( aRet
.X(), aRet
.Y() );
472 void SdUnoDrawView::SetZoomType ( sal_Int16 nType
)
474 SfxViewFrame
* pViewFrame
= mrDrawViewShell
.GetViewFrame();
478 SfxDispatcher
* pDispatcher
= pViewFrame
->GetDispatcher();
482 SvxZoomType eZoomType
;
485 case css::view::DocumentZoomType::OPTIMAL
:
486 eZoomType
= SvxZoomType::OPTIMAL
;
489 case css::view::DocumentZoomType::PAGE_WIDTH
:
490 case css::view::DocumentZoomType::PAGE_WIDTH_EXACT
:
491 eZoomType
= SvxZoomType::PAGEWIDTH
;
494 case css::view::DocumentZoomType::ENTIRE_PAGE
:
495 eZoomType
= SvxZoomType::WHOLEPAGE
;
501 SvxZoomItem
aZoomItem( eZoomType
);
502 pDispatcher
->ExecuteList(SID_ATTR_ZOOM
, SfxCallMode::SYNCHRON
,
506 SdXImpressDocument
* SdUnoDrawView::GetModel() const noexcept
508 if (mrView
.GetDocSh()!=nullptr)
510 Reference
<frame::XModel
> xModel (mrView
.GetDocSh()->GetModel());
511 return comphelper::getFromUnoTunnel
<SdXImpressDocument
>(xModel
);
517 Any
SdUnoDrawView::getDrawViewMode() const
520 switch( mrDrawViewShell
.GetPageKind() )
522 case PageKind::Notes
: aRet
<<= DrawViewMode_NOTES
; break;
523 case PageKind::Handout
: aRet
<<= DrawViewMode_HANDOUT
; break;
524 case PageKind::Standard
: aRet
<<= DrawViewMode_DRAW
; break;
530 OUString SAL_CALL
SdUnoDrawView::getImplementationName( )
532 return u
"com.sun.star.comp.sd.SdUnoDrawView"_ustr
;
535 sal_Bool SAL_CALL
SdUnoDrawView::supportsService( const OUString
& ServiceName
)
537 return cppu::supportsService( this, ServiceName
);
540 Sequence
< OUString
> SAL_CALL
SdUnoDrawView::getSupportedServiceNames( )
542 return { u
"com.sun.star.drawing.DrawingDocumentDrawView"_ustr
};
545 } // end of namespace sd
547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */