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