bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsCurrentSlideManager.cxx
blobd3e21aff80bdd13883982ff4c7e2f24aecd7428b
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 <SlideSorter.hxx>
21 #include <model/SlideSorterModel.hxx>
22 #include <model/SlsPageDescriptor.hxx>
23 #include <controller/SlsPageSelector.hxx>
24 #include <controller/SlideSorterController.hxx>
25 #include <controller/SlsCurrentSlideManager.hxx>
26 #include <controller/SlsFocusManager.hxx>
27 #include <view/SlideSorterView.hxx>
28 #include <ViewShellBase.hxx>
29 #include <ViewShell.hxx>
30 #include <DrawViewShell.hxx>
31 #include <sdpage.hxx>
32 #include <FrameView.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
38 using namespace ::sd::slidesorter::model;
40 namespace sd { namespace slidesorter { namespace controller {
42 CurrentSlideManager::CurrentSlideManager (SlideSorter& rSlideSorter)
43 : mrSlideSorter(rSlideSorter),
44 mnCurrentSlideIndex(-1),
45 mpCurrentSlide(),
46 maSwitchPageDelayTimer()
48 maSwitchPageDelayTimer.SetTimeout(100);
49 maSwitchPageDelayTimer.SetInvokeHandler(LINK(this,CurrentSlideManager,SwitchPageCallback));
52 CurrentSlideManager::~CurrentSlideManager()
56 void CurrentSlideManager::NotifyCurrentSlideChange (const SdPage* pPage)
58 if (pPage != nullptr)
59 NotifyCurrentSlideChange(
60 mrSlideSorter.GetModel().GetIndex(
61 Reference<drawing::XDrawPage>(
62 const_cast<SdPage*>(pPage)->getUnoPage(),
63 UNO_QUERY)));
64 else
65 NotifyCurrentSlideChange(-1);
68 void CurrentSlideManager::NotifyCurrentSlideChange (const sal_Int32 nSlideIndex)
70 if (mnCurrentSlideIndex == nSlideIndex)
71 return;
73 PageSelector::BroadcastLock aBroadcastLock (mrSlideSorter.GetController().GetPageSelector());
75 mrSlideSorter.GetController().GetPageSelector().DeselectAllPages();
77 ReleaseCurrentSlide();
78 AcquireCurrentSlide(nSlideIndex);
80 // Update the selection.
81 if (mpCurrentSlide)
83 mrSlideSorter.GetController().GetPageSelector().SelectPage(mpCurrentSlide);
84 mrSlideSorter.GetController().GetFocusManager().SetFocusedPage(mpCurrentSlide);
88 void CurrentSlideManager::ReleaseCurrentSlide()
90 if (mpCurrentSlide.get() != nullptr)
91 mrSlideSorter.GetView().SetState(mpCurrentSlide, PageDescriptor::ST_Current, false);
93 mpCurrentSlide.reset();
94 mnCurrentSlideIndex = -1;
97 void CurrentSlideManager::AcquireCurrentSlide (const sal_Int32 nSlideIndex)
99 mnCurrentSlideIndex = nSlideIndex;
101 // if current slide valid
102 if (mnCurrentSlideIndex >= 0 && mnCurrentSlideIndex<mrSlideSorter.GetModel().GetPageCount())
104 // Get a descriptor for the XDrawPage reference. Note that the
105 // given XDrawPage may or may not be member of the slide sorter
106 // document.
107 mpCurrentSlide = mrSlideSorter.GetModel().GetPageDescriptor(mnCurrentSlideIndex);
108 if (mpCurrentSlide.get() != nullptr)
109 mrSlideSorter.GetView().SetState(mpCurrentSlide, PageDescriptor::ST_Current, true);
113 void CurrentSlideManager::SwitchCurrentSlide (
114 const sal_Int32 nSlideIndex)
116 SwitchCurrentSlide(mrSlideSorter.GetModel().GetPageDescriptor(nSlideIndex), true/*bUpdateSelection*/);
119 void CurrentSlideManager::SwitchCurrentSlide (
120 const SharedPageDescriptor& rpDescriptor,
121 const bool bUpdateSelection)
123 if (rpDescriptor.get() == nullptr || mpCurrentSlide==rpDescriptor)
124 return;
126 ReleaseCurrentSlide();
127 AcquireCurrentSlide((rpDescriptor->GetPage()->GetPageNum()-1)/2);
129 ViewShell* pViewShell = mrSlideSorter.GetViewShell();
130 if (pViewShell != nullptr && pViewShell->IsMainViewShell())
132 // The slide sorter is the main view.
133 FrameView* pFrameView = pViewShell->GetFrameView();
134 if (pFrameView != nullptr)
135 pFrameView->SetSelectedPage(sal::static_int_cast<sal_uInt16>(mnCurrentSlideIndex));
136 mrSlideSorter.GetController().GetPageSelector().SetCoreSelection();
139 // We do not tell the XController/ViewShellBase about the new
140 // slide right away. This is done asynchronously after a short
141 // delay to allow for more slide switches in the slide sorter.
142 // This goes under the assumption that slide switching inside
143 // the slide sorter is fast (no expensive redraw of the new page
144 // (unless the preview of the new slide is not yet preset)) and
145 // that slide switching in the edit view is slow (all shapes of
146 // the new slide have to be repainted.)
147 maSwitchPageDelayTimer.Start();
149 // We have to store the (index of the) new current slide at
150 // the tab control because there are other asynchronous
151 // notifications of the slide switching that otherwise
152 // overwrite the correct value.
153 SetCurrentSlideAtTabControl(mpCurrentSlide);
155 if (bUpdateSelection)
157 mrSlideSorter.GetController().GetPageSelector().DeselectAllPages();
158 mrSlideSorter.GetController().GetPageSelector().SelectPage(rpDescriptor);
160 mrSlideSorter.GetController().GetFocusManager().SetFocusedPage(rpDescriptor);
163 void CurrentSlideManager::SetCurrentSlideAtViewShellBase (const SharedPageDescriptor& rpDescriptor)
165 OSL_ASSERT(rpDescriptor.get() != nullptr);
167 ViewShellBase* pBase = mrSlideSorter.GetViewShellBase();
168 if (pBase != nullptr)
170 DrawViewShell* pDrawViewShell = dynamic_cast<DrawViewShell*>(
171 pBase->GetMainViewShell().get());
172 if (pDrawViewShell != nullptr)
174 sal_uInt16 nPageNumber = (rpDescriptor->GetPage()->GetPageNum()-1)/2;
175 pDrawViewShell->SwitchPage(nPageNumber);
176 TabControl& rPageTabControl = pDrawViewShell->GetPageTabControl();
177 rPageTabControl.SetCurPageId(rPageTabControl.GetPageId(nPageNumber));
182 void CurrentSlideManager::SetCurrentSlideAtTabControl (const SharedPageDescriptor& rpDescriptor)
184 OSL_ASSERT(rpDescriptor.get() != nullptr);
186 ViewShellBase* pBase = mrSlideSorter.GetViewShellBase();
187 if (pBase != nullptr)
189 std::shared_ptr<DrawViewShell> pDrawViewShell (
190 std::dynamic_pointer_cast<DrawViewShell>(pBase->GetMainViewShell()));
191 if (pDrawViewShell)
193 sal_uInt16 nPageNumber = (rpDescriptor->GetPage()->GetPageNum()-1)/2;
194 TabControl& rPageTabControl = pDrawViewShell->GetPageTabControl();
195 rPageTabControl.SetCurPageId(rPageTabControl.GetPageId(nPageNumber));
200 void CurrentSlideManager::SetCurrentSlideAtXController (const SharedPageDescriptor& rpDescriptor)
202 OSL_ASSERT(rpDescriptor.get() != nullptr);
206 Reference<beans::XPropertySet> xSet (mrSlideSorter.GetXController(), UNO_QUERY);
207 if (xSet.is())
209 Any aPage;
210 aPage <<= rpDescriptor->GetPage()->getUnoPage();
211 xSet->setPropertyValue( "CurrentPage", aPage );
214 catch (const Exception&)
216 // We have not been able to set the current page at the main view.
217 // This is sad but still leaves us in a valid state. Therefore,
218 // this exception is silently ignored.
222 void CurrentSlideManager::PrepareModelChange()
224 mpCurrentSlide.reset();
227 void CurrentSlideManager::HandleModelChange()
229 if (mnCurrentSlideIndex >= 0)
231 mpCurrentSlide = mrSlideSorter.GetModel().GetPageDescriptor(mnCurrentSlideIndex);
232 if (mpCurrentSlide.get() != nullptr)
233 mrSlideSorter.GetView().SetState(mpCurrentSlide, PageDescriptor::ST_Current, true);
237 IMPL_LINK_NOARG(CurrentSlideManager, SwitchPageCallback, Timer *, void)
239 if (mpCurrentSlide)
241 // Set current page. At the moment we have to do this in two
242 // different ways. The UNO way is the preferable one but, alas,
243 // it does not work always correctly (after some kinds of model
244 // changes). Therefore, we call DrawViewShell::SwitchPage(),
245 // too.
246 ViewShell* pViewShell = mrSlideSorter.GetViewShell();
247 if (pViewShell==nullptr || ! pViewShell->IsMainViewShell())
248 SetCurrentSlideAtViewShellBase(mpCurrentSlide);
249 SetCurrentSlideAtXController(mpCurrentSlide);
253 } } } // end of namespace ::sd::slidesorter::controller
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */