tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / unoidl / SdUnoDrawView.cxx
blobedab1fabb0e464f4cfc6d571ea4f89177fa89445
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 <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>
28 #include <Window.hxx>
29 #include <pres.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>
43 #include <vector>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::drawing;
49 namespace sd {
51 SdUnoDrawView::SdUnoDrawView(
52 DrawViewShell& rViewShell,
53 View& rView) noexcept
54 : mrDrawViewShell(rViewShell),
55 mrView(rView)
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(),
89 bLayerMode);
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)
102 break;
104 SdDrawDocument* pSdModel = pModel->GetDoc();
105 if (pSdModel == nullptr)
106 break;
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)
112 break;
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);
121 while (false);
123 return xCurrentLayer;
126 void SdUnoDrawView::setActiveLayer (const Reference<drawing::XLayer>& rxLayer)
128 // Get the SdrLayer object corresponding to the given reference.
129 if ( ! rxLayer.is())
130 return;
132 SdLayer* pLayer = dynamic_cast<SdLayer*> (rxLayer.get());
133 if (pLayer == nullptr)
134 return;
136 SdrLayer* pSdrLayer = pLayer->GetSdrLayer();
137 if (pSdrLayer == nullptr)
138 return;
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 )
149 bool bOk = true;
151 ::std::vector<SdrObject*> aObjects;
153 SdrPage* pSdrPage = nullptr;
155 Reference< drawing::XShape > xShape;
156 aSelection >>= xShape;
158 if(xShape.is())
160 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xShape );
161 if( pObj )
163 pSdrPage = pObj->getSdrPageFromSdrObject();
164 aObjects.push_back( pObj );
166 else
168 bOk = false;
171 else
173 Reference< drawing::XShapes > xShapes;
174 aSelection >>= xShapes;
175 if( xShapes.is() )
177 const sal_uInt32 nCount = xShapes->getCount();
178 for( sal_uInt32 i = 0; i < nCount; i++ )
180 xShapes->getByIndex(i) >>= xShape;
181 if( xShape.is() )
183 SdrObject* pObj = SdrObject::getSdrObjectFromXShape(xShape);
184 if( !pObj )
186 bOk = false;
187 break;
190 if( pSdrPage == nullptr )
192 pSdrPage = pObj->getSdrPageFromSdrObject();
194 else if( pSdrPage != pObj->getSdrPageFromSdrObject() )
196 bOk = false;
197 break;
200 aObjects.push_back( pObj );
206 if( bOk )
208 if( pSdrPage )
210 setMasterPageMode( pSdrPage->IsMasterPage() );
211 mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
212 mrDrawViewShell.WriteFrameViewData();
215 SdrPageView *pPV = mrView.GetSdrPageView();
217 if(pPV)
219 // first deselect all
220 mrView.UnmarkAllObj( pPV );
222 for( SdrObject* pObj : aObjects )
224 mrView.MarkObj( pObj, pPV );
227 else
229 bOk = false;
233 return bOk;
236 Any SAL_CALL SdUnoDrawView::getSelection()
238 Any aAny;
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();
247 if( nCount )
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);
254 if(pMark==nullptr)
255 continue;
257 SdrObject *pObj = pMark->GetMarkedSdrObj();
258 if(pObj==nullptr || pObj->getSdrPageFromSdrObject() == nullptr)
259 continue;
261 Reference< drawing::XDrawPage > xPage( pObj->getSdrPageFromSdrObject()->getUnoPage(), UNO_QUERY);
263 if(!xPage.is())
264 continue;
266 SvxDrawPage* pDrawPage = comphelper::getFromUnoTunnel<SvxDrawPage>( xPage );
268 if(pDrawPage==nullptr)
269 continue;
271 Reference< drawing::XShape > xShape( pObj->getUnoShape(), UNO_QUERY );
273 if(xShape.is())
274 xShapes->add(xShape);
276 aAny <<= xShapes;
280 return aAny;
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 (
292 sal_Int32 nHandle,
293 const Any& rValue)
295 switch( nHandle )
297 case DrawController::PROPERTY_CURRENTPAGE:
299 Reference< drawing::XDrawPage > xPage;
300 rValue >>= xPage;
301 setCurrentPage( xPage );
303 break;
305 case DrawController::PROPERTY_MASTERPAGEMODE:
307 bool bValue = false;
308 rValue >>= bValue;
309 setMasterPageMode( bValue );
311 break;
313 case DrawController::PROPERTY_LAYERMODE:
315 bool bValue = false;
316 rValue >>= bValue;
317 setLayerMode( bValue );
319 break;
320 case DrawController::PROPERTY_ACTIVE_LAYER:
322 Reference<drawing::XLayer> xLayer;
323 rValue >>= xLayer;
324 setActiveLayer (xLayer);
326 break;
327 case DrawController::PROPERTY_ZOOMVALUE:
329 sal_Int16 nZoom = 0;
330 rValue >>= nZoom;
331 SetZoom( nZoom );
333 break;
334 case DrawController::PROPERTY_ZOOMTYPE:
336 sal_Int16 nType = 0;
337 rValue >>= nType;
338 SetZoomType( nType );
340 break;
341 case DrawController::PROPERTY_VIEWOFFSET:
343 awt::Point aOffset;
344 rValue >>= aOffset;
345 SetViewOffset( aOffset );
347 break;
348 default:
349 throw beans::UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
353 Any SAL_CALL SdUnoDrawView::getFastPropertyValue (
354 sal_Int32 nHandle)
356 Any aValue;
357 switch( nHandle )
359 case DrawController::PROPERTY_CURRENTPAGE:
360 aValue <<= getCurrentPage();
361 break;
363 case DrawController::PROPERTY_MASTERPAGEMODE:
364 aValue <<= getMasterPageMode();
365 break;
367 case DrawController::PROPERTY_LAYERMODE:
368 aValue <<= getLayerMode();
369 break;
371 case DrawController::PROPERTY_ACTIVE_LAYER:
372 aValue <<= getActiveLayer();
373 break;
375 case DrawController::PROPERTY_ZOOMVALUE:
376 aValue <<= GetZoom();
377 break;
378 case DrawController::PROPERTY_ZOOMTYPE:
379 aValue <<= sal_Int16(css::view::DocumentZoomType::BY_VALUE);
380 break;
381 case DrawController::PROPERTY_VIEWOFFSET:
382 aValue <<= GetViewOffset();
383 break;
385 case DrawController::PROPERTY_DRAWVIEWMODE:
386 aValue = getDrawViewMode();
387 break;
389 default:
390 throw beans::UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
393 return aValue;
396 // XDrawView
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;
404 if(pSdrPage)
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;
423 if(pPage)
424 xPage.set( pPage->getUnoPage(), UNO_QUERY );
426 return xPage;
429 sal_Int16 SdUnoDrawView::GetZoom() const
431 if (mrDrawViewShell.GetActiveWindow() )
433 return static_cast<sal_Int16>(mrDrawViewShell.GetActiveWindow()->GetZoom());
435 else
437 return 0;
441 void SdUnoDrawView::SetZoom( sal_Int16 nZoom )
443 SvxZoomItem aZoomItem( SvxZoomType::PERCENT, nZoom );
445 SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
446 if( pViewFrame )
448 SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
449 if( pDispatcher )
451 pDispatcher->ExecuteList(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON,
452 { &aZoomItem });
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();
475 if( !pViewFrame )
476 return;
478 SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
479 if( !pDispatcher )
480 return;
482 SvxZoomType eZoomType;
483 switch( nType )
485 case css::view::DocumentZoomType::OPTIMAL:
486 eZoomType = SvxZoomType::OPTIMAL;
487 break;
489 case css::view::DocumentZoomType::PAGE_WIDTH:
490 case css::view::DocumentZoomType::PAGE_WIDTH_EXACT:
491 eZoomType = SvxZoomType::PAGEWIDTH;
492 break;
494 case css::view::DocumentZoomType::ENTIRE_PAGE:
495 eZoomType = SvxZoomType::WHOLEPAGE;
496 break;
498 default:
499 return;
501 SvxZoomItem aZoomItem( eZoomType );
502 pDispatcher->ExecuteList(SID_ATTR_ZOOM, SfxCallMode::SYNCHRON,
503 { &aZoomItem });
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);
513 else
514 return nullptr;
517 Any SdUnoDrawView::getDrawViewMode() const
519 Any aRet;
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;
526 return aRet;
529 // XServiceInfo
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: */