Bump version to 21.06.18.1
[LibreOffice.git] / sd / source / ui / view / drawview.cxx
blob55ed74d5181e173dc66621ba8a8d771a693175da
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 <vcl/svapp.hxx>
21 #include <vcl/weld.hxx>
22 #include <svl/style.hxx>
23 #include <editeng/outliner.hxx>
24 #include <svx/svdotext.hxx>
25 #include <svl/poolitem.hxx>
26 #include <editeng/eeitem.hxx>
27 #include <editeng/numitem.hxx>
28 #include <svl/whiter.hxx>
29 #include <sal/log.hxx>
30 #include <tools/debug.hxx>
32 #include <svx/svdundo.hxx>
33 #include <svx/strings.hrc>
34 #include <svx/dialmgr.hxx>
36 #include <strings.hrc>
37 #include <View.hxx>
38 #include <drawview.hxx>
39 #include <drawdoc.hxx>
40 #include <DrawDocShell.hxx>
41 #include <sdpage.hxx>
42 #include <ViewShellBase.hxx>
43 #include <DrawViewShell.hxx>
44 #include <pres.hxx>
45 #include <sdresid.hxx>
46 #include <unchss.hxx>
47 #include <slideshow.hxx>
49 #include <undo/undomanager.hxx>
51 using namespace ::com::sun::star;
53 namespace sd {
56 /**
57 * Shows the first page of document at position 0,0. In the case
58 * that there is no page a page is created.
61 DrawView::DrawView(
62 DrawDocShell* pDocSh,
63 OutputDevice* pOutDev,
64 DrawViewShell* pShell)
65 : ::sd::View(*pDocSh->GetDoc(), pOutDev, pShell)
66 ,mpDocShell(pDocSh)
67 ,mpDrawViewShell(pShell)
68 ,mnPOCHSmph(0)
70 SetCurrentObj(OBJ_RECT);
73 DrawView::~DrawView()
77 /**
78 * Virtual method from SdrView, called at selection change.
81 void DrawView::MarkListHasChanged()
83 ::sd::View::MarkListHasChanged();
85 if (mpDrawViewShell)
86 mpDrawViewShell->SelectionHasChanged();
89 /**
90 * Virtual method from SdrView, called at model change.
93 void DrawView::ModelHasChanged()
95 ::sd::View::ModelHasChanged();
97 // force framer to rerender
98 SfxStyleSheetBasePool* pSSPool = mrDoc.GetStyleSheetPool();
99 pSSPool->Broadcast(SfxStyleSheetPoolHint());
101 if( mpDrawViewShell )
102 mpDrawViewShell->ModelHasChanged();
107 * Redirect attributes onto title and outline text and background
108 * rectangle of a masterpage into templates, otherwise pass on baseclass.
111 bool DrawView::SetAttributes(const SfxItemSet& rSet,
112 bool bReplaceAll, bool bSlide, bool bMaster)
114 bool bOk = false;
116 if (mpDrawViewShell && bMaster)
118 SfxStyleSheetBasePool* pStShPool = mrDoc.GetStyleSheetPool();
119 SdPage& rPage = *mpDrawViewShell->getCurrentPage();
120 SdrPage& rMasterPage = rPage.TRG_GetMasterPage();
121 size_t nObjCount = rMasterPage.GetObjCount();
122 for (size_t nObj = 0; nObj < nObjCount; ++nObj)
124 SdrObject* pObject = rMasterPage.GetObj(nObj);
125 SetMasterAttributes(pObject, rPage, rSet, pStShPool, bOk, bMaster, bSlide);
127 return bOk;
129 if (mpDrawViewShell && bSlide)
131 SfxStyleSheetBasePool* pStShPool = mrDoc.GetStyleSheetPool();
132 SdPage& rPage = *mpDrawViewShell->getCurrentPage();
133 size_t nObjCount = rPage.GetObjCount();
134 for (size_t nObj = 0; nObj < nObjCount; ++nObj)
136 SdrObject* pObject = rPage.GetObj(nObj);
137 SetMasterAttributes(pObject, rPage, rSet, pStShPool, bOk, bMaster, bSlide);
139 return bOk;
142 // is there a masterpage edit?
143 if ( mpDrawViewShell && (mpDrawViewShell->GetEditMode() == EditMode::MasterPage) )
145 SfxStyleSheetBasePool* pStShPool = mrDoc.GetStyleSheetPool();
146 SdPage& rPage = *mpDrawViewShell->getCurrentPage();
147 SdrTextObj* pEditObject = GetTextEditObject();
149 if (pEditObject)
151 // Textedit
153 SdrInventor nInv = pEditObject->GetObjInventor();
155 if (nInv == SdrInventor::Default)
157 sal_uInt16 eObjKind = pEditObject->GetObjIdentifier();
158 PresObjKind ePresObjKind = rPage.GetPresObjKind(pEditObject);
160 if ( ePresObjKind == PresObjKind::Title ||
161 ePresObjKind == PresObjKind::Notes )
163 // Presentation object (except outline)
164 SfxStyleSheet* pSheet = rPage.GetStyleSheetForPresObj( ePresObjKind );
165 DBG_ASSERT(pSheet, "StyleSheet not found");
167 SfxItemSet aTempSet( pSheet->GetItemSet() );
168 aTempSet.Put( rSet );
169 aTempSet.ClearInvalidItems();
171 // Undo-Action
172 mpDocSh->GetUndoManager()->AddUndoAction(
173 std::make_unique<StyleSheetUndoAction>(&mrDoc, pSheet, &aTempSet));
175 pSheet->GetItemSet().Put(aTempSet);
176 pSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
177 bOk = true;
179 else if (eObjKind == OBJ_OUTLINETEXT)
181 // Presentation object outline
182 OutlinerView* pOV = GetTextEditOutlinerView();
183 ::Outliner* pOutliner = pOV->GetOutliner();
185 pOutliner->SetUpdateMode(false);
186 mpDocSh->SetWaitCursor( true );
188 // replace placeholder by template name
189 OUString aComment(SdResId(STR_UNDO_CHANGE_PRES_OBJECT));
190 aComment = aComment.replaceFirst("$", SdResId(STR_PSEUDOSHEET_OUTLINE));
191 mpDocSh->GetUndoManager()->EnterListAction( aComment, OUString(), 0, mpDrawViewShell->GetViewShellBase().GetViewShellId() );
193 std::vector<Paragraph*> aSelList;
194 pOV->CreateSelectionList(aSelList);
196 std::vector<Paragraph*>::reverse_iterator iter = aSelList.rbegin();
197 Paragraph* pPara = iter != aSelList.rend() ? *iter : nullptr;
199 while (pPara)
201 sal_Int32 nParaPos = pOutliner->GetAbsPos( pPara );
202 sal_Int16 nDepth = pOutliner->GetDepth( nParaPos );
203 OUString aName = rPage.GetLayoutName() + " " +
204 OUString::number((nDepth <= 0) ? 1 : nDepth + 1);
205 SfxStyleSheet* pSheet = static_cast<SfxStyleSheet*>(pStShPool->Find(aName, SfxStyleFamily::Page));
206 //We have no stylesheet if we access outline level 10
207 //in the master preview, there is no true style backing
208 //that entry
209 SAL_WARN_IF(!pSheet, "sd", "StyleSheet " << aName << " not found");
210 if (pSheet)
212 SfxItemSet aTempSet( pSheet->GetItemSet() );
213 aTempSet.Put( rSet );
214 aTempSet.ClearInvalidItems();
216 if( nDepth > 0 && aTempSet.GetItemState( EE_PARA_NUMBULLET ) == SfxItemState::SET )
218 // no SvxNumBulletItem in outline level 1 to 8!
219 aTempSet.ClearItem( EE_PARA_NUMBULLET );
222 // Undo-Action
223 mpDocSh->GetUndoManager()->AddUndoAction(
224 std::make_unique<StyleSheetUndoAction>(&mrDoc, pSheet, &aTempSet));
226 pSheet->GetItemSet().Put(aTempSet);
227 pSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
229 // now also broadcast any child sheets
230 sal_Int16 nChild;
231 for( nChild = nDepth + 1; nChild < 9; nChild++ )
233 OUString aSheetName = rPage.GetLayoutName() + " " +
234 OUString::number((nChild <= 0) ? 1 : nChild + 1);
235 SfxStyleSheet* pOutlSheet = static_cast< SfxStyleSheet* >(pStShPool->Find(aSheetName, SfxStyleFamily::Page));
237 if( pOutlSheet )
238 pOutlSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
242 ++iter;
243 pPara = iter != aSelList.rend() ? *iter : nullptr;
245 bool bJumpToLevel1 = false;
246 if( !pPara && nDepth > 0 && rSet.GetItemState( EE_PARA_NUMBULLET ) == SfxItemState::SET )
247 bJumpToLevel1 = true;
249 if (bJumpToLevel1)
251 iter = aSelList.rend();
252 --iter;
254 if (pOutliner->GetDepth(pOutliner->GetAbsPos(*iter)) > 0)
255 pPara = pOutliner->GetParagraph( 0 ); // Put NumBulletItem in outline level 1
259 mpDocSh->SetWaitCursor( false );
260 pOV->GetOutliner()->SetUpdateMode(true);
262 mpDocSh->GetUndoManager()->LeaveListAction();
264 bOk = true;
266 else
268 bOk = ::sd::View::SetAttributes(rSet, bReplaceAll);
272 else
274 // Selection
275 const SdrMarkList& rList = GetMarkedObjectList();
276 const size_t nMarkCount = rList.GetMarkCount();
277 for (size_t nMark = 0; nMark < nMarkCount; ++nMark)
279 SdrObject* pObject = rList.GetMark(nMark)->GetMarkedSdrObj();
280 SetMasterAttributes(pObject, rPage, rSet, pStShPool, bOk, bMaster, bSlide);
283 if(!bOk)
284 bOk = ::sd::View::SetAttributes(rSet, bReplaceAll);
287 else // not at masterpage
289 bOk = ::sd::View::SetAttributes(rSet, bReplaceAll);
292 return bOk;
295 void DrawView::SetMasterAttributes( SdrObject* pObject, const SdPage& rPage, SfxItemSet rSet, SfxStyleSheetBasePool* pStShPool, bool& bOk, bool bMaster, bool bSlide )
297 SdrInventor nInv = pObject->GetObjInventor();
299 if (nInv != SdrInventor::Default)
300 return;
302 sal_uInt16 eObjKind = pObject->GetObjIdentifier();
303 PresObjKind ePresObjKind = rPage.GetPresObjKind(pObject);
304 if (bSlide && eObjKind == OBJ_TEXT)
306 // Presentation object (except outline)
307 SfxStyleSheet* pSheet = rPage.GetTextStyleSheetForObject(pObject);
308 DBG_ASSERT(pSheet, "StyleSheet not found");
310 SfxItemSet aTempSet( pSheet->GetItemSet() );
311 aTempSet.Put( rSet );
312 aTempSet.ClearInvalidItems();
314 // Undo-Action
315 mpDocSh->GetUndoManager()->AddUndoAction(
316 std::make_unique<StyleSheetUndoAction>(&mrDoc, pSheet, &aTempSet));
318 pSheet->GetItemSet().Put(aTempSet,false);
319 pSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
320 bOk = true;
323 if (!bSlide &&
324 (ePresObjKind == PresObjKind::Title ||
325 ePresObjKind == PresObjKind::Notes))
327 // Presentation object (except outline)
328 SfxStyleSheet* pSheet = rPage.GetStyleSheetForPresObj( ePresObjKind );
329 DBG_ASSERT(pSheet, "StyleSheet not found");
331 SfxItemSet aTempSet( pSheet->GetItemSet() );
332 aTempSet.Put( rSet );
333 aTempSet.ClearInvalidItems();
335 // Undo-Action
336 mpDocSh->GetUndoManager()->AddUndoAction(
337 std::make_unique<StyleSheetUndoAction>(&mrDoc, pSheet, &aTempSet));
339 pSheet->GetItemSet().Put(aTempSet,false);
340 pSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
341 bOk = true;
343 else if (eObjKind == OBJ_OUTLINETEXT)
345 if (bMaster)
347 // Presentation object outline
348 for (sal_uInt16 nLevel = 9; nLevel > 0; nLevel--)
350 OUString aName = rPage.GetLayoutName() + " " +
351 OUString::number(nLevel);
352 SfxStyleSheet* pSheet = static_cast<SfxStyleSheet*>(pStShPool->
353 Find(aName, SfxStyleFamily::Page));
354 DBG_ASSERT(pSheet, "StyleSheet not found");
356 SfxItemSet aTempSet( pSheet->GetItemSet() );
358 if( nLevel > 1 )
360 // for all levels over 1, clear all items that will be
361 // hard set to level 1
362 SfxWhichIter aWhichIter(rSet);
363 sal_uInt16 nWhich(aWhichIter.FirstWhich());
364 while( nWhich )
366 if( SfxItemState::SET == rSet.GetItemState( nWhich ) )
367 aTempSet.ClearItem( nWhich );
368 nWhich = aWhichIter.NextWhich();
372 else
374 // put the items hard into level one
375 aTempSet.Put( rSet );
378 aTempSet.ClearInvalidItems();
380 // Undo-Action
381 mpDocSh->GetUndoManager()->AddUndoAction(
382 std::make_unique<StyleSheetUndoAction>(&mrDoc, pSheet, &aTempSet));
384 pSheet->GetItemSet().Set(aTempSet,false);
385 pSheet->Broadcast(SfxHint(SfxHintId::DataChanged));
388 // remove all hard set items from shape that are now set in style
389 SfxWhichIter aWhichIter(rSet);
390 sal_uInt16 nWhich(aWhichIter.FirstWhich());
391 while( nWhich )
393 if( SfxItemState::SET == rSet.GetItemState( nWhich ) )
394 pObject->ClearMergedItem( nWhich );
395 nWhich = aWhichIter.NextWhich();
398 else
399 pObject->SetMergedItemSet(rSet);
401 bOk = true;
406 * Notify for change of site arrangement
409 void DrawView::Notify(SfxBroadcaster& rBC, const SfxHint& rHint)
411 if ( mpDrawViewShell && rHint.GetId() == SfxHintId::ThisIsAnSdrHint )
413 SdrHintKind eHintKind = static_cast<const SdrHint&>(rHint).GetKind();
415 if ( mnPOCHSmph == 0 && eHintKind == SdrHintKind::PageOrderChange )
417 mpDrawViewShell->ResetActualPage();
419 else if ( eHintKind == SdrHintKind::LayerChange || eHintKind == SdrHintKind::LayerOrderChange )
421 mpDrawViewShell->ResetActualLayer();
424 // switch to that page when it's not a master page
425 if(SdrHintKind::SwitchToPage == eHintKind)
427 // We switch page only in the current view, which triggered this event
428 // and keep other views ontouched.
429 SfxViewShell* pViewShell = SfxViewShell::Current();
430 if(pViewShell && pViewShell != &mpDrawViewShell->GetViewShellBase())
431 return;
433 const SdrPage* pPage = static_cast<const SdrHint&>(rHint).GetPage();
434 if(pPage && !pPage->IsMasterPage())
436 if(mpDrawViewShell->GetActualPage() != pPage)
438 sal_uInt16 nPageNum = (pPage->GetPageNum() - 1) / 2; // Sdr --> Sd
439 mpDrawViewShell->SwitchPage(nPageNum);
445 ::sd::View::Notify(rBC, rHint);
449 * Lock/Unlock PageOrderChangedHint
452 void DrawView::BlockPageOrderChangedHint(bool bBlock)
454 if (bBlock)
455 mnPOCHSmph++;
456 else
458 DBG_ASSERT(mnPOCHSmph, "counter overflow");
459 mnPOCHSmph--;
464 * If presentation objects are selected, intercept stylesheet-positioning at
465 * masterpage.
468 bool DrawView::SetStyleSheet(SfxStyleSheet* pStyleSheet, bool bDontRemoveHardAttr)
470 bool bResult = true;
472 // is there a masterpage edit?
473 if (mpDrawViewShell && mpDrawViewShell->GetEditMode() == EditMode::MasterPage)
475 if (IsPresObjSelected(false))
477 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(mpDrawViewShell->GetFrameWeld(),
478 VclMessageType::Info, VclButtonsType::Ok,
479 SdResId(STR_ACTION_NOTPOSSIBLE)));
480 xInfoBox->run();
481 bResult = false;
483 else
485 bResult = ::sd::View::SetStyleSheet(pStyleSheet, bDontRemoveHardAttr);
488 else
490 bResult = ::sd::View::SetStyleSheet(pStyleSheet, bDontRemoveHardAttr);
492 return bResult;
496 * Paint-method: Redirect event to the view
499 void DrawView::CompleteRedraw(OutputDevice* pOutDev, const vcl::Region& rReg, sdr::contact::ViewObjectContactRedirector* pRedirector /*=0*/)
501 bool bStandardPaint = true;
503 SdDrawDocument* pDoc = mpDocShell->GetDoc();
504 if( pDoc && pDoc->GetDocumentType() == DocumentType::Impress)
506 rtl::Reference< sd::SlideShow > xSlideshow( SlideShow::GetSlideShow( pDoc ) );
507 if(xSlideshow.is() && xSlideshow->isRunning())
509 OutputDevice* pShowWindow = xSlideshow->getShowWindow();
510 if( (pShowWindow == pOutDev) || (xSlideshow->getAnimationMode() == ANIMATIONMODE_PREVIEW) )
512 if( pShowWindow == pOutDev && mpViewSh )
513 xSlideshow->paint();
514 bStandardPaint = false;
519 if(bStandardPaint)
521 ::sd::View::CompleteRedraw(pOutDev, rReg, pRedirector);
526 * Make passed region visible (scrolling if necessary)
529 void DrawView::MakeVisible(const ::tools::Rectangle& rRect, vcl::Window& rWin)
531 if (!rRect.IsEmpty() && mpDrawViewShell)
533 mpDrawViewShell->MakeVisible(rRect, rWin);
538 * Hide page.
541 void DrawView::HideSdrPage()
543 if (mpDrawViewShell)
545 mpDrawViewShell->HidePage();
548 ::sd::View::HideSdrPage();
551 void DrawView::DeleteMarked()
553 sd::UndoManager* pUndoManager = mrDoc.GetUndoManager();
554 DBG_ASSERT( pUndoManager, "sd::DrawView::DeleteMarked(), ui action without undo manager!?" );
556 if( pUndoManager )
558 OUString aUndo(SvxResId(STR_EditDelete));
559 aUndo = aUndo.replaceFirst("%1", GetDescriptionOfMarkedObjects());
560 ViewShellId nViewShellId = mpDrawViewShell ? mpDrawViewShell->GetViewShellBase().GetViewShellId() : ViewShellId(-1);
561 pUndoManager->EnterListAction(aUndo, aUndo, 0, nViewShellId);
564 SdPage* pPage = nullptr;
565 bool bResetLayout = false;
567 const size_t nMarkCount = GetMarkedObjectList().GetMarkCount();
568 if( nMarkCount )
570 SdrMarkList aList( GetMarkedObjectList() );
571 for (size_t nMark = 0; nMark < nMarkCount; ++nMark)
573 SdrObject* pObj = aList.GetMark(nMark)->GetMarkedSdrObj();
574 if( pObj && !pObj->IsEmptyPresObj() && pObj->GetUserCall() )
576 pPage = static_cast< SdPage* >( pObj->getSdrPageFromSdrObject() );
577 if (pPage)
579 PresObjKind ePresObjKind(pPage->GetPresObjKind(pObj));
580 switch( ePresObjKind )
582 case PresObjKind::NONE:
583 continue; // ignore it
584 case PresObjKind::Graphic:
585 case PresObjKind::Object:
586 case PresObjKind::Chart:
587 case PresObjKind::OrgChart:
588 case PresObjKind::Table:
589 case PresObjKind::Calc:
590 case PresObjKind::Media:
591 ePresObjKind = PresObjKind::Outline;
592 break;
593 default:
594 break;
596 SdrTextObj* pTextObj = dynamic_cast< SdrTextObj* >( pObj );
597 bool bVertical = pTextObj && pTextObj->IsVerticalWriting();
598 ::tools::Rectangle aRect( pObj->GetLogicRect() );
599 SdrObject* pNewObj = pPage->InsertAutoLayoutShape( nullptr, ePresObjKind, bVertical, aRect, true );
601 // pUndoManager should not be NULL (see assert above)
602 // but since we have defensive code
603 // for it earlier and later in the function
604 // we might as well be consistent
605 if(pUndoManager)
607 // Move the new PresObj to the position before the
608 // object it will replace.
609 pUndoManager->AddUndoAction(
610 mrDoc.GetSdrUndoFactory().CreateUndoObjectOrdNum(
611 *pNewObj,
612 pNewObj->GetOrdNum(),
613 pObj->GetOrdNum()));
615 pPage->SetObjectOrdNum( pNewObj->GetOrdNum(), pObj->GetOrdNum() );
617 bResetLayout = true;
623 ::sd::View::DeleteMarked();
625 if( pPage && bResetLayout )
626 pPage->SetAutoLayout( pPage->GetAutoLayout() );
628 if( pUndoManager )
629 pUndoManager->LeaveListAction();
632 } // end of namespace sd
634 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */