xref: /trunk/main/sd/source/ui/unoidl/SdUnoDrawView.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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     mrController.mpCurrentLayer = &(xCurrentLayer) ;
155 
156     return xCurrentLayer;
157 }
158 
159 
160 
161 
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 
191 sal_Bool SAL_CALL SdUnoDrawView::select( const Any& aSelection )
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 {
290     Any aAny;
291 
292     if( mrView.IsTextEdit() )
293         mrView.getTextSelection( aAny );
294 
295 
296     if( !aAny.hasValue() )
297     {
298         const SdrMarkList& rMarkList = mrView.GetMarkedObjectList();
299         sal_uInt32 nCount = rMarkList.GetMarkCount();
300         if( nCount )
301         {
302             Reference< drawing::XShapes > xShapes( SvxShapeCollection_NewInstance(), UNO_QUERY );
303             for( sal_uInt32 nNum = 0; nNum < nCount; nNum++)
304             {
305                 SdrMark *pMark = rMarkList.GetMark(nNum);
306                 if(pMark==NULL)
307                     continue;
308 
309                 SdrObject *pObj = pMark->GetMarkedSdrObj();
310                 if(pObj==NULL || pObj->GetPage() == NULL)
311                     continue;
312 
313                 Reference< drawing::XDrawPage > xPage( pObj->GetPage()->getUnoPage(), UNO_QUERY);
314 
315                 if(!xPage.is())
316                     continue;
317 
318                 SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
319 
320                 if(pDrawPage==NULL)
321                     continue;
322 
323                 Reference< drawing::XShape > xShape( pObj->getUnoShape(), UNO_QUERY );
324 
325                 if(xShape.is())
326                     xShapes->add(xShape);
327             }
328             aAny <<= xShapes;
329         }
330     }
331 
332     return aAny;
333 }
334 
335 
336 
337 
338 void SAL_CALL SdUnoDrawView::addSelectionChangeListener (
339     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
340 {
341     (void)rxListener;
342 }
343 
344 
345 
346 
347 void SAL_CALL SdUnoDrawView::removeSelectionChangeListener (
348     const css::uno::Reference<css::view::XSelectionChangeListener>& rxListener)
349 {
350     (void)rxListener;
351 }
352 
353 
354 
355 
356 void SdUnoDrawView::setFastPropertyValue (
357     sal_Int32 nHandle,
358         const Any& rValue)
359 {
360     switch( nHandle )
361     {
362         case DrawController::PROPERTY_CURRENTPAGE:
363             {
364                 Reference< drawing::XDrawPage > xPage;
365                 rValue >>= xPage;
366                 setCurrentPage( xPage );
367             }
368             break;
369 
370         case DrawController::PROPERTY_MASTERPAGEMODE:
371             {
372                 sal_Bool bValue = sal_False;
373                 rValue >>= bValue;
374                 setMasterPageMode( bValue );
375             }
376             break;
377 
378         case DrawController::PROPERTY_LAYERMODE:
379             {
380                 sal_Bool bValue = sal_False;
381                 rValue >>= bValue;
382                 setLayerMode( bValue );
383             }
384 
385         case DrawController::PROPERTY_ACTIVE_LAYER:
386             {
387                 Reference<drawing::XLayer> xLayer;
388                 rValue >>= xLayer;
389                 setActiveLayer (xLayer);
390             }
391             break;
392         case DrawController::PROPERTY_ZOOMVALUE:
393             {
394                 sal_Int16 nZoom = 0;
395                 rValue >>= nZoom;
396                 SetZoom( nZoom );
397             }
398             break;
399         case DrawController::PROPERTY_ZOOMTYPE:
400             {
401                 sal_Int16 nType = 0;
402                 rValue >>= nType;
403                 SetZoomType( nType );
404             }
405             break;
406         case DrawController::PROPERTY_VIEWOFFSET:
407             {
408                 awt::Point aOffset;
409                 rValue >>= aOffset;
410                 SetViewOffset( aOffset );
411             }
412             break;
413         default:
414             throw beans::UnknownPropertyException();
415     }
416 }
417 
418 
419 
420 
421 Any SAL_CALL SdUnoDrawView::getFastPropertyValue (
422     sal_Int32 nHandle)
423 {
424     Any aValue;
425     switch( nHandle )
426     {
427         case DrawController::PROPERTY_CURRENTPAGE:
428             aValue <<= (const_cast<SdUnoDrawView*>(this))->getCurrentPage();
429             break;
430 
431         case DrawController::PROPERTY_MASTERPAGEMODE:
432             aValue <<= getMasterPageMode();
433             break;
434 
435         case DrawController::PROPERTY_LAYERMODE:
436             aValue <<= getLayerMode();
437             break;
438 
439         case DrawController::PROPERTY_ACTIVE_LAYER:
440             aValue <<= (const_cast<SdUnoDrawView*>(this))->getActiveLayer();
441             break;
442 
443         case DrawController::PROPERTY_ZOOMVALUE:
444             aValue <<= GetZoom();
445             break;
446         case DrawController::PROPERTY_ZOOMTYPE:
447             aValue <<= (sal_Int16)com::sun::star::view::DocumentZoomType::BY_VALUE;
448             break;
449         case DrawController::PROPERTY_VIEWOFFSET:
450             aValue <<= GetViewOffset();
451             break;
452 
453         case DrawController::PROPERTY_DRAWVIEWMODE:
454             aValue = getDrawViewMode();
455             break;
456 
457         default:
458             throw beans::UnknownPropertyException();
459     }
460 
461     return aValue;
462 }
463 
464 
465 
466 
467 // XDrawView
468 
469 
470 void SAL_CALL SdUnoDrawView::setCurrentPage (
471     const Reference< drawing::XDrawPage >& xPage )
472 {
473     SvxDrawPage* pDrawPage = SvxDrawPage::getImplementation( xPage );
474     SdrPage *pSdrPage = pDrawPage ? pDrawPage->GetSdrPage() : NULL;
475 
476     if(pSdrPage)
477     {
478         // End editing of text.  Otherwise the edited text object would
479         // still be visible on the new page.
480         mrDrawViewShell.GetView()->SdrEndTextEdit();
481 
482         setMasterPageMode( pSdrPage->IsMasterPage() );
483         mrDrawViewShell.SwitchPage( (pSdrPage->GetPageNum() - 1) >> 1 );
484         mrDrawViewShell.WriteFrameViewData();
485     }
486 }
487 
488 //----------------------------------------------------------------------
489 
490 Reference< drawing::XDrawPage > SAL_CALL SdUnoDrawView::getCurrentPage()
491 {
492     Reference< drawing::XDrawPage >  xPage;
493 
494     SdrPageView *pPV = mrView.GetSdrPageView();
495     SdrPage* pPage = pPV ? pPV->GetPage() : NULL;
496 
497     if(pPage)
498         xPage = Reference< drawing::XDrawPage >::query( pPage->getUnoPage() );
499 
500     return xPage;
501 }
502 
503 
504 sal_Int16 SdUnoDrawView::GetZoom(void) const
505 {
506     if (mrDrawViewShell.GetActiveWindow() )
507     {
508         return (sal_Int16)mrDrawViewShell.GetActiveWindow()->GetZoom();
509     }
510     else
511     {
512         return 0;
513     }
514 }
515 
516 void SdUnoDrawView::SetZoom( sal_Int16 nZoom )
517 {
518     SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, nZoom );
519 
520     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
521     if( pViewFrame )
522     {
523         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
524         if( pDispatcher )
525         {
526             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
527         }
528     }
529 }
530 
531 
532 void SdUnoDrawView::SetViewOffset(const awt::Point& rWinPos )
533 {
534     Point aWinPos( rWinPos.X, rWinPos.Y );
535     aWinPos += mrDrawViewShell.GetViewOrigin();
536     mrDrawViewShell.SetWinViewPos( aWinPos, true );
537 }
538 
539 awt::Point SdUnoDrawView::GetViewOffset() const
540 {
541     Point aRet;
542 
543     aRet = mrDrawViewShell.GetWinViewPos();
544     aRet -= mrDrawViewShell.GetViewOrigin();
545 
546     return awt::Point( aRet.X(), aRet.Y() );
547 }
548 
549 void SdUnoDrawView::SetZoomType ( sal_Int16 nType )
550 {
551     SfxViewFrame* pViewFrame = mrDrawViewShell.GetViewFrame();
552     if( pViewFrame )
553     {
554         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
555         if( pDispatcher )
556         {
557             SvxZoomType eZoomType;
558             switch( nType )
559             {
560                 case com::sun::star::view::DocumentZoomType::OPTIMAL:
561                     eZoomType = SVX_ZOOM_OPTIMAL;
562                     break;
563 
564                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH:
565                 case com::sun::star::view::DocumentZoomType::PAGE_WIDTH_EXACT:
566                     eZoomType = SVX_ZOOM_PAGEWIDTH;
567                     break;
568 
569                 case com::sun::star::view::DocumentZoomType::ENTIRE_PAGE:
570                     eZoomType = SVX_ZOOM_WHOLEPAGE;
571                     break;
572 
573                 default:
574                     return;
575             }
576             SvxZoomItem aZoomItem( eZoomType );
577             pDispatcher->Execute(SID_ATTR_ZOOM,SFX_CALLMODE_SYNCHRON,&aZoomItem, 0L);
578         }
579     }
580 }
581 
582 
583 
584 
585 SdXImpressDocument* SdUnoDrawView::GetModel (void) const throw()
586 {
587     if (mrView.GetDocSh()!=NULL)
588     {
589         Reference<frame::XModel> xModel (mrView.GetDocSh()->GetModel());
590         return SdXImpressDocument::getImplementation(xModel);
591     }
592     else
593         return NULL;
594 }
595 
596 Any SdUnoDrawView::getDrawViewMode() const
597 {
598     Any aRet;
599     switch( mrDrawViewShell.GetPageKind() )
600     {
601     case PK_NOTES:  aRet <<= DrawViewMode_NOTES; break;
602     case PK_HANDOUT: aRet <<= DrawViewMode_HANDOUT; break;
603     case PK_STANDARD: aRet <<= DrawViewMode_DRAW; break;
604     }
605     return aRet;
606 }
607 
608 // XServiceInfo
609 OUString SAL_CALL SdUnoDrawView::getImplementationName(  )
610 {
611     return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.sd.SdUnoDrawView") );
612 }
613 
614 sal_Bool SAL_CALL SdUnoDrawView::supportsService( const OUString& ServiceName )
615 {
616     return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
617 }
618 
619 Sequence< OUString > SAL_CALL SdUnoDrawView::getSupportedServiceNames(  )
620 {
621     OUString aSN( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.DrawingDocumentDrawView") );
622     uno::Sequence< OUString > aSeq( &aSN, 1 );
623     return aSeq;
624 }
625 
626 } // end of namespace sd
627