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 
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 
76 SdUnoDrawView::~SdUnoDrawView() throw()
77 {
78 }
79 
80 
81 
82 
83 sal_Bool SdUnoDrawView::getMasterPageMode(void) const throw()
84 {
85     return (mrDrawViewShell.GetEditMode() == EM_MASTERPAGE);
86 }
87 
88 
89 
90 
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 
104 sal_Bool SdUnoDrawView::getLayerMode(void) const throw()
105 {
106     return mrDrawViewShell.IsLayerModeActive();
107 }
108 
109 
110 
111 
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 
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 
155     return xCurrentLayer;
156 }
157 
158 
159 
160 
161 void SdUnoDrawView::setActiveLayer (const Reference<drawing::XLayer>& rxLayer) throw ()
162 {
163     do
164     {
165         // Get the SdrLayer object corresponding to the given reference.
166         if ( ! rxLayer.is())
167             break;
168 
169         SdLayer* pLayer = SdLayer::getImplementation (rxLayer);
170         if (pLayer == NULL)
171             break;
172 
173         SdrLayer* pSdrLayer = pLayer->GetSdrLayer();
174         if (pSdrLayer == NULL)
175             break;
176 
177         // Set the new active layer and make the change visible.
178         mrView.SetActiveLayer (pSdrLayer->GetName());
179         mrDrawViewShell.ResetActualLayer ();
180     }
181     while (false);
182 }
183 
184 
185 
186 
187 // XSelectionSupplier
188 
189 
190 sal_Bool SAL_CALL SdUnoDrawView::select( const Any& aSelection )
191 	throw(lang::IllegalArgumentException, RuntimeException)
192 {
193     bool bOk = true;
194 
195     ::std::vector<SdrObject*> aObjects;
196 
197     SdrPage* pSdrPage = NULL;
198 
199     Reference< drawing::XShape > xShape;
200     aSelection >>= xShape;
201 
202     if(xShape.is())
203     {
204         SvxShape* pShape = SvxShape::getImplementation( xShape );
205         if( pShape && (pShape->GetSdrObject() != NULL) )
206         {
207             SdrObject* pObj = pShape->GetSdrObject();
208             pSdrPage = pObj->GetPage();
209             aObjects.push_back( pObj );
210         }
211         else
212         {
213             bOk = false;
214         }
215     }
216     else
217     {
218         Reference< drawing::XShapes > xShapes;
219         aSelection >>= xShapes;
220         if( xShapes.is() )
221         {
222             const sal_uInt32 nCount = xShapes->getCount();
223             for( sal_uInt32 i = 0; i < nCount; i++ )
224             {
225                 xShapes->getByIndex(i) >>= xShape;
226                 if( xShape.is() )
227                 {
228                     SvxShape* pShape = SvxShape::getImplementation(xShape);
229                     if( (pShape == NULL) || (pShape->GetSdrObject() == NULL) )
230                     {
231                         bOk = false;
232                         break;
233                     }
234 
235                     SdrObject* pObj = pShape->GetSdrObject();
236 
237                     if( pSdrPage == NULL )
238                     {
239                         pSdrPage = pObj->GetPage();
240                     }
241                     else if( pSdrPage != pObj->GetPage() )
242                     {
243                         bOk = false;
244                         break;
245                     }
246 
247                     aObjects.push_back( pObj );
248                 }
249             }
250         }
251     }
252 
253     if( bOk )
254     {
255         if( pSdrPage )
256         {
257             setMasterPageMode( pSdrPage->IsMasterPage() );
258             mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
259             mrDrawViewShell.WriteFrameViewData();
260         }
261 
262         SdrPageView *pPV = mrView.GetSdrPageView();
263 
264         if(pPV)
265         {
266             // first deselect all
267             mrView.UnmarkAllObj( pPV );
268 
269             ::std::vector<SdrObject*>::iterator aIter( aObjects.begin() );
270             const ::std::vector<SdrObject*>::iterator aEnd( aObjects.end() );
271             while( aIter != aEnd )
272             {
273                 SdrObject* pObj = (*aIter++);
274                 mrView.MarkObj( pObj, pPV );
275             }
276         }
277         else
278         {
279             bOk = false;
280         }
281     }
282 
283 	return bOk;
284 }
285 
286 //----------------------------------------------------------------------
287 
288 Any SAL_CALL SdUnoDrawView::getSelection()
289 	throw(RuntimeException)
290 {
291 	Any aAny;
292 
293     if( mrView.IsTextEdit() )
294         mrView.getTextSelection( aAny );
295 
296 
297     if( !aAny.hasValue() )
298     {
299         const SdrMarkList& rMarkList = mrView.GetMarkedObjectList();
300         sal_uInt32 nCount = rMarkList.GetMarkCount();
301         if( nCount )
302         {
303             Reference< drawing::XShapes > xShapes( SvxShapeCollection_NewInstance(), UNO_QUERY );
304             for( sal_uInt32 nNum = 0; nNum < nCount; nNum++)
305             {
306                 SdrMark *pMark = rMarkList.GetMark(nNum);
307                 if(pMark==NULL)
308                     continue;
309 
310                 SdrObject *pObj = pMark->GetMarkedSdrObj();
311                 if(pObj==NULL || pObj->GetPage() == NULL)
312                     continue;
313 
314                 Reference< drawing::XDrawPage > xPage( pObj->GetPage()->getUnoPage(), UNO_QUERY);
315 
316                 if(!xPage.is())
317                     continue;
318 
319                 SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
320 
321                 if(pDrawPage==NULL)
322                     continue;
323 
324                 Reference< drawing::XShape > xShape( pObj->getUnoShape(), UNO_QUERY );
325 
326                 if(xShape.is())
327                     xShapes->add(xShape);
328             }
329             aAny <<= xShapes;
330         }
331     }
332 
333 	return aAny;
334 }
335 
336 
337 
338 
339 void SAL_CALL SdUnoDrawView::addSelectionChangeListener (
340     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
341     throw(css::uno::RuntimeException)
342 {
343     (void)rxListener;
344 }
345 
346 
347 
348 
349 void SAL_CALL SdUnoDrawView::removeSelectionChangeListener (
350     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
351     throw(css::uno::RuntimeException)
352 {
353     (void)rxListener;
354 }
355 
356 
357 
358 
359 void SdUnoDrawView::setFastPropertyValue (
360 	sal_Int32 nHandle,
361         const Any& rValue)
362     throw(css::beans::UnknownPropertyException,
363         css::beans::PropertyVetoException,
364         css::lang::IllegalArgumentException,
365         css::lang::WrappedTargetException,
366         css::uno::RuntimeException)
367 {
368 	switch( nHandle )
369 	{
370 		case DrawController::PROPERTY_CURRENTPAGE:
371 			{
372 				Reference< drawing::XDrawPage > xPage;
373 				rValue >>= xPage;
374 				setCurrentPage( xPage );
375 			}
376 			break;
377 
378 		case DrawController::PROPERTY_MASTERPAGEMODE:
379 			{
380 				sal_Bool bValue = sal_False;
381 				rValue >>= bValue;
382 				setMasterPageMode( bValue );
383 			}
384 			break;
385 
386 		case DrawController::PROPERTY_LAYERMODE:
387 			{
388 				sal_Bool bValue = sal_False;
389 				rValue >>= bValue;
390 				setLayerMode( bValue );
391 			}
392 
393 		case DrawController::PROPERTY_ACTIVE_LAYER:
394 			{
395 				Reference<drawing::XLayer> xLayer;
396 				rValue >>= xLayer;
397 				setActiveLayer (xLayer);
398 			}
399 			break;
400 		case DrawController::PROPERTY_ZOOMVALUE:
401 			{
402 				sal_Int16 nZoom = 0;
403 				rValue >>= nZoom;
404 				SetZoom( nZoom );
405 			}
406 			break;
407 		case DrawController::PROPERTY_ZOOMTYPE:
408 			{
409 				sal_Int16 nType = 0;
410 				rValue >>= nType;
411 				SetZoomType( nType );
412 			}
413 			break;
414 		case DrawController::PROPERTY_VIEWOFFSET:
415 			{
416 				awt::Point aOffset;
417 				rValue >>= aOffset;
418 				SetViewOffset( aOffset );
419 			}
420 			break;
421         default:
422             throw beans::UnknownPropertyException();
423 	}
424 }
425 
426 
427 
428 
429 Any SAL_CALL SdUnoDrawView::getFastPropertyValue (
430     sal_Int32 nHandle)
431     throw(css::beans::UnknownPropertyException,
432         css::lang::WrappedTargetException,
433         css::uno::RuntimeException)
434 {
435     Any aValue;
436 	switch( nHandle )
437 	{
438 		case DrawController::PROPERTY_CURRENTPAGE:
439 			aValue <<= (const_cast<SdUnoDrawView*>(this))->getCurrentPage();
440 			break;
441 
442 		case DrawController::PROPERTY_MASTERPAGEMODE:
443 			aValue <<= getMasterPageMode();
444 			break;
445 
446 		case DrawController::PROPERTY_LAYERMODE:
447 			aValue <<= getLayerMode();
448 			break;
449 
450 		case DrawController::PROPERTY_ACTIVE_LAYER:
451 			aValue <<= (const_cast<SdUnoDrawView*>(this))->getActiveLayer();
452 			break;
453 
454 		case DrawController::PROPERTY_ZOOMVALUE:
455 			aValue <<= GetZoom();
456 			break;
457 		case DrawController::PROPERTY_ZOOMTYPE:
458 			aValue <<= (sal_Int16)com::sun::star::view::DocumentZoomType::BY_VALUE;
459 			break;
460 		case DrawController::PROPERTY_VIEWOFFSET:
461 			aValue <<= GetViewOffset();
462 			break;
463 
464 		case DrawController::PROPERTY_DRAWVIEWMODE:
465 			aValue = getDrawViewMode();
466 			break;
467 
468         default:
469             throw beans::UnknownPropertyException();
470 	}
471 
472     return aValue;
473 }
474 
475 
476 
477 
478 // XDrawView
479 
480 
481 void SAL_CALL SdUnoDrawView::setCurrentPage (
482     const Reference< drawing::XDrawPage >& xPage )
483 	throw(RuntimeException)
484 {
485     SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
486     SdrPage *pSdrPage = pDrawPage ? pDrawPage->GetSdrPage() : NULL;
487 
488     if(pSdrPage)
489     {
490         // End editing of text.  Otherwise the edited text object would
491         // still be visible on the new page.
492         mrDrawViewShell.GetView()->SdrEndTextEdit();
493 
494         setMasterPageMode( pSdrPage->IsMasterPage() );
495         mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
496         mrDrawViewShell.WriteFrameViewData();
497     }
498 }
499 
500 //----------------------------------------------------------------------
501 
502 Reference< drawing::XDrawPage > SAL_CALL SdUnoDrawView::getCurrentPage()
503 	throw(RuntimeException)
504 {
505 	Reference< drawing::XDrawPage >  xPage;
506 
507     SdrPageView *pPV = mrView.GetSdrPageView();
508     SdrPage* pPage = pPV ? pPV->GetPage() : NULL;
509 
510     if(pPage)
511         xPage = Reference< drawing::XDrawPage >::query( pPage->getUnoPage() );
512 
513 	return xPage;
514 }
515 
516 
517 sal_Int16 SdUnoDrawView::GetZoom(void) const
518 {
519 	if (mrDrawViewShell.GetActiveWindow() )
520 	{
521 		return (sal_Int16)mrDrawViewShell.GetActiveWindow()->GetZoom();
522 	}
523 	else
524 	{
525 		return 0;
526 	}
527 }
528 
529 void SdUnoDrawView::SetZoom( sal_Int16 nZoom )
530 {
531 	SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, nZoom );
532 
533     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
534     if( pViewFrame )
535     {
536         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
537         if( pDispatcher )
538         {
539             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
540         }
541     }
542 }
543 
544 
545 void SdUnoDrawView::SetViewOffset(const awt::Point& rWinPos )
546 {
547     Point aWinPos( rWinPos.X, rWinPos.Y );
548     aWinPos += mrDrawViewShell.GetViewOrigin();
549     mrDrawViewShell.SetWinViewPos( aWinPos, true );
550 }
551 
552 awt::Point SdUnoDrawView::GetViewOffset() const
553 {
554 	Point aRet;
555 
556     aRet = mrDrawViewShell.GetWinViewPos();
557     aRet -= mrDrawViewShell.GetViewOrigin();
558 
559 	return awt::Point( aRet.X(), aRet.Y() );
560 }
561 
562 void SdUnoDrawView::SetZoomType ( sal_Int16 nType )
563 {
564     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
565     if( pViewFrame )
566     {
567         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
568         if( pDispatcher )
569         {
570             SvxZoomType eZoomType;
571             switch( nType )
572             {
573                 case com::sun::star::view::DocumentZoomType::OPTIMAL:
574                     eZoomType = SVX_ZOOM_OPTIMAL;
575                     break;
576 
577                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH:
578                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH_EXACT:
579                     eZoomType = SVX_ZOOM_PAGEWIDTH;
580                     break;
581 
582                 case com::sun::star::view::DocumentZoomType::ENTIRE_PAGE:
583                     eZoomType = SVX_ZOOM_WHOLEPAGE;
584                     break;
585 
586                 default:
587                     return;
588             }
589             SvxZoomItem aZoomItem( eZoomType );
590             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
591         }
592     }
593 }
594 
595 
596 
597 
598 SdXImpressDocument* SdUnoDrawView::GetModel (void) const throw()
599 {
600 	if (mrView.GetDocSh()!=NULL)
601 	{
602 		Reference<frame::XModel> xModel (mrView.GetDocSh()->GetModel());
603 		return SdXImpressDocument::getImplementation(xModel);
604 	}
605 	else
606 		return NULL;
607 }
608 
609 Any SdUnoDrawView::getDrawViewMode() const
610 {
611 	Any aRet;
612 	switch( mrDrawViewShell.GetPageKind() )
613 	{
614 	case PK_NOTES:	aRet <<= DrawViewMode_NOTES; break;
615 	case PK_HANDOUT: aRet <<= DrawViewMode_HANDOUT; break;
616 	case PK_STANDARD: aRet <<= DrawViewMode_DRAW; break;
617 	}
618 	return aRet;
619 }
620 
621 // XServiceInfo
622 OUString SAL_CALL SdUnoDrawView::getImplementationName(  ) throw (RuntimeException)
623 {
624 	return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.sd.SdUnoDrawView") );
625 }
626 
627 sal_Bool SAL_CALL SdUnoDrawView::supportsService( const OUString& ServiceName ) throw (RuntimeException)
628 {
629 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
630 }
631 
632 Sequence< OUString > SAL_CALL SdUnoDrawView::getSupportedServiceNames(  ) throw (RuntimeException)
633 {
634 	OUString aSN( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.DrawingDocumentDrawView") );
635 	uno::Sequence< OUString > aSeq( &aSN, 1 );
636 	return aSeq;
637 }
638 
639 } // end of namespace sd
640 
641