1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 
27 #include "SdUnoDrawView.hxx"
28 
29 #include "DrawController.hxx"
30 #include "DrawDocShell.hxx"
31 #include "DrawViewShell.hxx"
32 #include "drawdoc.hxx"
33 #include "unolayer.hxx"
34 #include "unomodel.hxx"
35 #include "unopage.hxx"
36 #include "Window.hxx"
37 #include "pres.hxx"
38 
39 #include <cppuhelper/proptypehlp.hxx>
40 #include <comphelper/serviceinfohelper.hxx>
41 #include <sfx2/dispatch.hxx>
42 #include <sfx2/viewfrm.hxx>
43 #include <svx/svdpagv.hxx>
44 #include <svx/unoshape.hxx>
45 #include <svx/unoshcol.hxx>
46 #include <svx/zoomitem.hxx>
47 #include <com/sun/star/drawing/DrawViewMode.hpp>
48 #include <com/sun/star/drawing/XLayerManager.hpp>
49 #include <com/sun/star/view/DocumentZoomType.hpp>
50 
51 #include <vector>
52 
53 using rtl::OUString;
54 
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::drawing;
58 
59 
60 namespace sd {
61 
SdUnoDrawView(DrawController & rController,DrawViewShell & rViewShell,View & rView)62 SdUnoDrawView::SdUnoDrawView(
63     DrawController& rController,
64     DrawViewShell& rViewShell,
65     View& rView) throw()
66     :	DrawSubControllerInterfaceBase(m_aMutex),
67         mrController(rController),
68         mrDrawViewShell(rViewShell),
69         mrView(rView)
70 {
71 }
72 
73 
74 
75 
~SdUnoDrawView()76 SdUnoDrawView::~SdUnoDrawView() throw()
77 {
78 }
79 
80 
81 
82 
getMasterPageMode(void) const83 sal_Bool SdUnoDrawView::getMasterPageMode(void) const throw()
84 {
85     return (mrDrawViewShell.GetEditMode() == EM_MASTERPAGE);
86 }
87 
88 
89 
90 
setMasterPageMode(sal_Bool bMasterPageMode)91 void SdUnoDrawView::setMasterPageMode (sal_Bool bMasterPageMode) throw()
92 {
93 	if ((mrDrawViewShell.GetEditMode() == EM_MASTERPAGE) != bMasterPageMode)
94     {
95         mrDrawViewShell.ChangeEditMode (
96             bMasterPageMode ? EM_MASTERPAGE : EM_PAGE,
97             mrDrawViewShell.IsLayerModeActive());
98     }
99 }
100 
101 
102 
103 
getLayerMode(void) const104 sal_Bool SdUnoDrawView::getLayerMode(void) const throw()
105 {
106     return mrDrawViewShell.IsLayerModeActive();
107 }
108 
109 
110 
111 
setLayerMode(sal_Bool bLayerMode)112 void SdUnoDrawView::setLayerMode (sal_Bool bLayerMode) throw()
113 {
114 	if (mrDrawViewShell.IsLayerModeActive() != (bLayerMode==sal_True))
115     {
116         mrDrawViewShell.ChangeEditMode (
117             mrDrawViewShell.GetEditMode(),
118             bLayerMode);
119     }
120 }
121 
122 
123 
124 
getActiveLayer(void)125 Reference<drawing::XLayer> SdUnoDrawView::getActiveLayer (void) throw ()
126 {
127     Reference<drawing::XLayer> xCurrentLayer;
128 
129     do
130     {
131         // Retrieve the layer manager from the model.
132         SdXImpressDocument* pModel = GetModel();
133         if (pModel == NULL)
134             break;
135 
136         SdDrawDocument* pSdModel = pModel->GetDoc();
137         if (pSdModel == NULL)
138             break;
139 
140         // From the model get the current SdrLayer object via the layer admin.
141         SdrLayerAdmin& rLayerAdmin = pSdModel->GetLayerAdmin ();
142         SdrLayer* pLayer = rLayerAdmin.GetLayer (mrView.GetActiveLayer(), sal_True);
143         if (pLayer == NULL)
144             break;
145 
146         // Get the corresponding XLayer object from the implementation
147         // object of the layer manager.
148         Reference<drawing::XLayerManager> xManager (pModel->getLayerManager(), uno::UNO_QUERY);
149         SdLayerManager* pManager = SdLayerManager::getImplementation (xManager);
150         if (pManager != NULL)
151             xCurrentLayer = pManager->GetLayer (pLayer);
152     }
153     while (false);
154 	mrController.mpCurrentLayer = &(xCurrentLayer) ;
155 
156     return xCurrentLayer;
157 }
158 
159 
160 
161 
setActiveLayer(const Reference<drawing::XLayer> & rxLayer)162 void SdUnoDrawView::setActiveLayer (const Reference<drawing::XLayer>& rxLayer) throw ()
163 {
164     do
165     {
166         // Get the SdrLayer object corresponding to the given reference.
167         if ( ! rxLayer.is())
168             break;
169 
170         SdLayer* pLayer = SdLayer::getImplementation (rxLayer);
171         if (pLayer == NULL)
172             break;
173 
174         SdrLayer* pSdrLayer = pLayer->GetSdrLayer();
175         if (pSdrLayer == NULL)
176             break;
177 
178         // Set the new active layer and make the change visible.
179         mrView.SetActiveLayer (pSdrLayer->GetName());
180         mrDrawViewShell.ResetActualLayer ();
181     }
182     while (false);
183 }
184 
185 
186 
187 
188 // XSelectionSupplier
189 
190 
select(const Any & aSelection)191 sal_Bool SAL_CALL SdUnoDrawView::select( const Any& aSelection )
192 	throw(lang::IllegalArgumentException, RuntimeException)
193 {
194     bool bOk = true;
195 
196     ::std::vector<SdrObject*> aObjects;
197 
198     SdrPage* pSdrPage = NULL;
199 
200     Reference< drawing::XShape > xShape;
201     aSelection >>= xShape;
202 
203     if(xShape.is())
204     {
205         SvxShape* pShape = SvxShape::getImplementation( xShape );
206         if( pShape && (pShape->GetSdrObject() != NULL) )
207         {
208             SdrObject* pObj = pShape->GetSdrObject();
209             pSdrPage = pObj->GetPage();
210             aObjects.push_back( pObj );
211         }
212         else
213         {
214             bOk = false;
215         }
216     }
217     else
218     {
219         Reference< drawing::XShapes > xShapes;
220         aSelection >>= xShapes;
221         if( xShapes.is() )
222         {
223             const sal_uInt32 nCount = xShapes->getCount();
224             for( sal_uInt32 i = 0; i < nCount; i++ )
225             {
226                 xShapes->getByIndex(i) >>= xShape;
227                 if( xShape.is() )
228                 {
229                     SvxShape* pShape = SvxShape::getImplementation(xShape);
230                     if( (pShape == NULL) || (pShape->GetSdrObject() == NULL) )
231                     {
232                         bOk = false;
233                         break;
234                     }
235 
236                     SdrObject* pObj = pShape->GetSdrObject();
237 
238                     if( pSdrPage == NULL )
239                     {
240                         pSdrPage = pObj->GetPage();
241                     }
242                     else if( pSdrPage != pObj->GetPage() )
243                     {
244                         bOk = false;
245                         break;
246                     }
247 
248                     aObjects.push_back( pObj );
249                 }
250             }
251         }
252     }
253 
254     if( bOk )
255     {
256         if( pSdrPage )
257         {
258             setMasterPageMode( pSdrPage->IsMasterPage() );
259             mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
260             mrDrawViewShell.WriteFrameViewData();
261         }
262 
263         SdrPageView *pPV = mrView.GetSdrPageView();
264 
265         if(pPV)
266         {
267             // first deselect all
268             mrView.UnmarkAllObj( pPV );
269 
270             ::std::vector<SdrObject*>::iterator aIter( aObjects.begin() );
271             const ::std::vector<SdrObject*>::iterator aEnd( aObjects.end() );
272             while( aIter != aEnd )
273             {
274                 SdrObject* pObj = (*aIter++);
275                 mrView.MarkObj( pObj, pPV );
276             }
277         }
278         else
279         {
280             bOk = false;
281         }
282     }
283 
284 	return bOk;
285 }
286 
287 //----------------------------------------------------------------------
288 
getSelection()289 Any SAL_CALL SdUnoDrawView::getSelection()
290 	throw(RuntimeException)
291 {
292 	Any aAny;
293 
294     if( mrView.IsTextEdit() )
295         mrView.getTextSelection( aAny );
296 
297 
298     if( !aAny.hasValue() )
299     {
300         const SdrMarkList& rMarkList = mrView.GetMarkedObjectList();
301         sal_uInt32 nCount = rMarkList.GetMarkCount();
302         if( nCount )
303         {
304             Reference< drawing::XShapes > xShapes( SvxShapeCollection_NewInstance(), UNO_QUERY );
305             for( sal_uInt32 nNum = 0; nNum < nCount; nNum++)
306             {
307                 SdrMark *pMark = rMarkList.GetMark(nNum);
308                 if(pMark==NULL)
309                     continue;
310 
311                 SdrObject *pObj = pMark->GetMarkedSdrObj();
312                 if(pObj==NULL || pObj->GetPage() == NULL)
313                     continue;
314 
315                 Reference< drawing::XDrawPage > xPage( pObj->GetPage()->getUnoPage(), UNO_QUERY);
316 
317                 if(!xPage.is())
318                     continue;
319 
320                 SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
321 
322                 if(pDrawPage==NULL)
323                     continue;
324 
325                 Reference< drawing::XShape > xShape( pObj->getUnoShape(), UNO_QUERY );
326 
327                 if(xShape.is())
328                     xShapes->add(xShape);
329             }
330             aAny <<= xShapes;
331         }
332     }
333 
334 	return aAny;
335 }
336 
337 
338 
339 
addSelectionChangeListener(const css::uno::Reference<css::view::XSelectionChangeListener> & rxListener)340 void SAL_CALL SdUnoDrawView::addSelectionChangeListener (
341     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
342     throw(css::uno::RuntimeException)
343 {
344     (void)rxListener;
345 }
346 
347 
348 
349 
removeSelectionChangeListener(const css::uno::Reference<css::view::XSelectionChangeListener> & rxListener)350 void SAL_CALL SdUnoDrawView::removeSelectionChangeListener (
351     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
352     throw(css::uno::RuntimeException)
353 {
354     (void)rxListener;
355 }
356 
357 
358 
359 
setFastPropertyValue(sal_Int32 nHandle,const Any & rValue)360 void SdUnoDrawView::setFastPropertyValue (
361 	sal_Int32 nHandle,
362         const Any& rValue)
363     throw(css::beans::UnknownPropertyException,
364         css::beans::PropertyVetoException,
365         css::lang::IllegalArgumentException,
366         css::lang::WrappedTargetException,
367         css::uno::RuntimeException)
368 {
369 	switch( nHandle )
370 	{
371 		case DrawController::PROPERTY_CURRENTPAGE:
372 			{
373 				Reference< drawing::XDrawPage > xPage;
374 				rValue >>= xPage;
375 				setCurrentPage( xPage );
376 			}
377 			break;
378 
379 		case DrawController::PROPERTY_MASTERPAGEMODE:
380 			{
381 				sal_Bool bValue = sal_False;
382 				rValue >>= bValue;
383 				setMasterPageMode( bValue );
384 			}
385 			break;
386 
387 		case DrawController::PROPERTY_LAYERMODE:
388 			{
389 				sal_Bool bValue = sal_False;
390 				rValue >>= bValue;
391 				setLayerMode( bValue );
392 			}
393 
394 		case DrawController::PROPERTY_ACTIVE_LAYER:
395 			{
396 				Reference<drawing::XLayer> xLayer;
397 				rValue >>= xLayer;
398 				setActiveLayer (xLayer);
399 			}
400 			break;
401 		case DrawController::PROPERTY_ZOOMVALUE:
402 			{
403 				sal_Int16 nZoom = 0;
404 				rValue >>= nZoom;
405 				SetZoom( nZoom );
406 			}
407 			break;
408 		case DrawController::PROPERTY_ZOOMTYPE:
409 			{
410 				sal_Int16 nType = 0;
411 				rValue >>= nType;
412 				SetZoomType( nType );
413 			}
414 			break;
415 		case DrawController::PROPERTY_VIEWOFFSET:
416 			{
417 				awt::Point aOffset;
418 				rValue >>= aOffset;
419 				SetViewOffset( aOffset );
420 			}
421 			break;
422         default:
423             throw beans::UnknownPropertyException();
424 	}
425 }
426 
427 
428 
429 
getFastPropertyValue(sal_Int32 nHandle)430 Any SAL_CALL SdUnoDrawView::getFastPropertyValue (
431     sal_Int32 nHandle)
432     throw(css::beans::UnknownPropertyException,
433         css::lang::WrappedTargetException,
434         css::uno::RuntimeException)
435 {
436     Any aValue;
437 	switch( nHandle )
438 	{
439 		case DrawController::PROPERTY_CURRENTPAGE:
440 			aValue <<= (const_cast<SdUnoDrawView*>(this))->getCurrentPage();
441 			break;
442 
443 		case DrawController::PROPERTY_MASTERPAGEMODE:
444 			aValue <<= getMasterPageMode();
445 			break;
446 
447 		case DrawController::PROPERTY_LAYERMODE:
448 			aValue <<= getLayerMode();
449 			break;
450 
451 		case DrawController::PROPERTY_ACTIVE_LAYER:
452 			aValue <<= (const_cast<SdUnoDrawView*>(this))->getActiveLayer();
453 			break;
454 
455 		case DrawController::PROPERTY_ZOOMVALUE:
456 			aValue <<= GetZoom();
457 			break;
458 		case DrawController::PROPERTY_ZOOMTYPE:
459 			aValue <<= (sal_Int16)com::sun::star::view::DocumentZoomType::BY_VALUE;
460 			break;
461 		case DrawController::PROPERTY_VIEWOFFSET:
462 			aValue <<= GetViewOffset();
463 			break;
464 
465 		case DrawController::PROPERTY_DRAWVIEWMODE:
466 			aValue = getDrawViewMode();
467 			break;
468 
469         default:
470             throw beans::UnknownPropertyException();
471 	}
472 
473     return aValue;
474 }
475 
476 
477 
478 
479 // XDrawView
480 
481 
setCurrentPage(const Reference<drawing::XDrawPage> & xPage)482 void SAL_CALL SdUnoDrawView::setCurrentPage (
483     const Reference< drawing::XDrawPage >& xPage )
484 	throw(RuntimeException)
485 {
486     SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
487     SdrPage *pSdrPage = pDrawPage ? pDrawPage->GetSdrPage() : NULL;
488 
489     if(pSdrPage)
490     {
491         // End editing of text.  Otherwise the edited text object would
492         // still be visible on the new page.
493         mrDrawViewShell.GetView()->SdrEndTextEdit();
494 
495         setMasterPageMode( pSdrPage->IsMasterPage() );
496         mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
497         mrDrawViewShell.WriteFrameViewData();
498     }
499 }
500 
501 //----------------------------------------------------------------------
502 
getCurrentPage()503 Reference< drawing::XDrawPage > SAL_CALL SdUnoDrawView::getCurrentPage()
504 	throw(RuntimeException)
505 {
506 	Reference< drawing::XDrawPage >  xPage;
507 
508     SdrPageView *pPV = mrView.GetSdrPageView();
509     SdrPage* pPage = pPV ? pPV->GetPage() : NULL;
510 
511     if(pPage)
512         xPage = Reference< drawing::XDrawPage >::query( pPage->getUnoPage() );
513 
514 	return xPage;
515 }
516 
517 
GetZoom(void) const518 sal_Int16 SdUnoDrawView::GetZoom(void) const
519 {
520 	if (mrDrawViewShell.GetActiveWindow() )
521 	{
522 		return (sal_Int16)mrDrawViewShell.GetActiveWindow()->GetZoom();
523 	}
524 	else
525 	{
526 		return 0;
527 	}
528 }
529 
SetZoom(sal_Int16 nZoom)530 void SdUnoDrawView::SetZoom( sal_Int16 nZoom )
531 {
532 	SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, nZoom );
533 
534     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
535     if( pViewFrame )
536     {
537         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
538         if( pDispatcher )
539         {
540             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
541         }
542     }
543 }
544 
545 
SetViewOffset(const awt::Point & rWinPos)546 void SdUnoDrawView::SetViewOffset(const awt::Point& rWinPos )
547 {
548     Point aWinPos( rWinPos.X, rWinPos.Y );
549     aWinPos += mrDrawViewShell.GetViewOrigin();
550     mrDrawViewShell.SetWinViewPos( aWinPos, true );
551 }
552 
GetViewOffset() const553 awt::Point SdUnoDrawView::GetViewOffset() const
554 {
555 	Point aRet;
556 
557     aRet = mrDrawViewShell.GetWinViewPos();
558     aRet -= mrDrawViewShell.GetViewOrigin();
559 
560 	return awt::Point( aRet.X(), aRet.Y() );
561 }
562 
SetZoomType(sal_Int16 nType)563 void SdUnoDrawView::SetZoomType ( sal_Int16 nType )
564 {
565     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
566     if( pViewFrame )
567     {
568         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
569         if( pDispatcher )
570         {
571             SvxZoomType eZoomType;
572             switch( nType )
573             {
574                 case com::sun::star::view::DocumentZoomType::OPTIMAL:
575                     eZoomType = SVX_ZOOM_OPTIMAL;
576                     break;
577 
578                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH:
579                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH_EXACT:
580                     eZoomType = SVX_ZOOM_PAGEWIDTH;
581                     break;
582 
583                 case com::sun::star::view::DocumentZoomType::ENTIRE_PAGE:
584                     eZoomType = SVX_ZOOM_WHOLEPAGE;
585                     break;
586 
587                 default:
588                     return;
589             }
590             SvxZoomItem aZoomItem( eZoomType );
591             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
592         }
593     }
594 }
595 
596 
597 
598 
GetModel(void) const599 SdXImpressDocument* SdUnoDrawView::GetModel (void) const throw()
600 {
601 	if (mrView.GetDocSh()!=NULL)
602 	{
603 		Reference<frame::XModel> xModel (mrView.GetDocSh()->GetModel());
604 		return SdXImpressDocument::getImplementation(xModel);
605 	}
606 	else
607 		return NULL;
608 }
609 
getDrawViewMode() const610 Any SdUnoDrawView::getDrawViewMode() const
611 {
612 	Any aRet;
613 	switch( mrDrawViewShell.GetPageKind() )
614 	{
615 	case PK_NOTES:	aRet <<= DrawViewMode_NOTES; break;
616 	case PK_HANDOUT: aRet <<= DrawViewMode_HANDOUT; break;
617 	case PK_STANDARD: aRet <<= DrawViewMode_DRAW; break;
618 	}
619 	return aRet;
620 }
621 
622 // XServiceInfo
getImplementationName()623 OUString SAL_CALL SdUnoDrawView::getImplementationName(  ) throw (RuntimeException)
624 {
625 	return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.sd.SdUnoDrawView") );
626 }
627 
supportsService(const OUString & ServiceName)628 sal_Bool SAL_CALL SdUnoDrawView::supportsService( const OUString& ServiceName ) throw (RuntimeException)
629 {
630 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
631 }
632 
getSupportedServiceNames()633 Sequence< OUString > SAL_CALL SdUnoDrawView::getSupportedServiceNames(  ) throw (RuntimeException)
634 {
635 	OUString aSN( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.DrawingDocumentDrawView") );
636 	uno::Sequence< OUString > aSeq( &aSN, 1 );
637 	return aSeq;
638 }
639 
640 } // end of namespace sd
641 
642