1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <DrawController.hxx>
32 #include <drawdoc.hxx>
34 #include <FrameView.hxx>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/frame/XController.hpp>
37 #include <osl/diagnose.h>
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::uno
;
42 using namespace ::sd::slidesorter::model
;
44 namespace sd::slidesorter::controller
{
46 CurrentSlideManager::CurrentSlideManager (SlideSorter
& rSlideSorter
)
47 : mrSlideSorter(rSlideSorter
),
48 mnCurrentSlideIndex(-1),
49 maSwitchPageDelayTimer("sd CurrentSlideManager maSwitchPageDelayTimer")
51 maSwitchPageDelayTimer
.SetTimeout(100);
52 maSwitchPageDelayTimer
.SetInvokeHandler(LINK(this,CurrentSlideManager
,SwitchPageCallback
));
55 CurrentSlideManager::~CurrentSlideManager()
59 void CurrentSlideManager::NotifyCurrentSlideChange (const SdPage
* pPage
)
62 NotifyCurrentSlideChange(
63 mrSlideSorter
.GetModel().GetIndex(
64 Reference
<drawing::XDrawPage
>(
65 const_cast<SdPage
*>(pPage
)->getUnoPage(),
68 NotifyCurrentSlideChange(-1);
71 void CurrentSlideManager::NotifyCurrentSlideChange (const sal_Int32 nSlideIndex
)
73 if (mnCurrentSlideIndex
== nSlideIndex
)
76 PageSelector::BroadcastLock
aBroadcastLock (mrSlideSorter
.GetController().GetPageSelector());
78 mrSlideSorter
.GetController().GetPageSelector().DeselectAllPages();
80 ReleaseCurrentSlide();
81 AcquireCurrentSlide(nSlideIndex
);
83 // Update the selection.
86 mrSlideSorter
.GetController().GetPageSelector().SelectPage(mpCurrentSlide
);
87 mrSlideSorter
.GetController().GetFocusManager().SetFocusedPage(mpCurrentSlide
);
91 void CurrentSlideManager::ReleaseCurrentSlide()
94 mrSlideSorter
.GetView().SetState(mpCurrentSlide
, PageDescriptor::ST_Current
, false);
96 mpCurrentSlide
.reset();
97 mnCurrentSlideIndex
= -1;
100 void CurrentSlideManager::AcquireCurrentSlide (const sal_Int32 nSlideIndex
)
102 mnCurrentSlideIndex
= nSlideIndex
;
104 // if current slide valid
105 if (mnCurrentSlideIndex
>= 0 && mnCurrentSlideIndex
<mrSlideSorter
.GetModel().GetPageCount())
107 // Get a descriptor for the XDrawPage reference. Note that the
108 // given XDrawPage may or may not be member of the slide sorter
110 mpCurrentSlide
= mrSlideSorter
.GetModel().GetPageDescriptor(mnCurrentSlideIndex
);
112 mrSlideSorter
.GetView().SetState(mpCurrentSlide
, PageDescriptor::ST_Current
, true);
116 void CurrentSlideManager::SwitchCurrentSlide (
117 const sal_Int32 nSlideIndex
)
119 SwitchCurrentSlide(mrSlideSorter
.GetModel().GetPageDescriptor(nSlideIndex
), true/*bUpdateSelection*/);
122 void CurrentSlideManager::SwitchCurrentSlide (
123 const SharedPageDescriptor
& rpDescriptor
,
124 const bool bUpdateSelection
)
126 if (!rpDescriptor
|| mpCurrentSlide
==rpDescriptor
)
129 ReleaseCurrentSlide();
130 AcquireCurrentSlide((rpDescriptor
->GetPage()->GetPageNum()-1)/2);
132 ViewShell
* pViewShell
= mrSlideSorter
.GetViewShell();
133 if (pViewShell
!= nullptr && pViewShell
->IsMainViewShell())
135 // The slide sorter is the main view.
136 FrameView
* pFrameView
= pViewShell
->GetFrameView();
137 if (pFrameView
!= nullptr)
138 pFrameView
->SetSelectedPage(sal::static_int_cast
<sal_uInt16
>(mnCurrentSlideIndex
));
139 mrSlideSorter
.GetController().GetPageSelector().SetCoreSelection();
142 // We do not tell the XController/ViewShellBase about the new
143 // slide right away. This is done asynchronously after a short
144 // delay to allow for more slide switches in the slide sorter.
145 // This goes under the assumption that slide switching inside
146 // the slide sorter is fast (no expensive redraw of the new page
147 // (unless the preview of the new slide is not yet preset)) and
148 // that slide switching in the edit view is slow (all shapes of
149 // the new slide have to be repainted.)
150 maSwitchPageDelayTimer
.Start();
152 // We have to store the (index of the) new current slide at
153 // the tab control because there are other asynchronous
154 // notifications of the slide switching that otherwise
155 // overwrite the correct value.
156 SetCurrentSlideAtTabControl(mpCurrentSlide
);
158 if (bUpdateSelection
)
160 mrSlideSorter
.GetController().GetPageSelector().DeselectAllPages();
161 mrSlideSorter
.GetController().GetPageSelector().SelectPage(rpDescriptor
);
163 mrSlideSorter
.GetController().GetFocusManager().SetFocusedPage(rpDescriptor
);
166 void CurrentSlideManager::SetCurrentSlideAtViewShellBase (const SharedPageDescriptor
& rpDescriptor
)
168 OSL_ASSERT(rpDescriptor
);
170 ViewShellBase
* pBase
= mrSlideSorter
.GetViewShellBase();
174 if (mrSlideSorter
.GetViewShell() && mrSlideSorter
.GetViewShell()->IsMainViewShell())
176 SdDrawDocument
* pDoc
= pBase
->GetDocument();
180 // deselect all pages
181 for (sal_uInt16 i
= 0; i
< pDoc
->GetSdPageCount(PageKind::Standard
); i
++)
182 pDoc
->SetSelected(pDoc
->GetSdPage(i
, PageKind::Standard
), false);
184 // select the given page
185 pDoc
->SetSelected(rpDescriptor
->GetPage(), true);
186 DrawController
* pDrawController
= pBase
->GetDrawController();
187 if (!pDrawController
)
190 pDrawController
->FireSelectionChangeListener();
191 pDrawController
->FireSwitchCurrentPage(rpDescriptor
->GetPage());
195 DrawViewShell
* pDrawViewShell
= dynamic_cast<DrawViewShell
*>(
196 pBase
->GetMainViewShell().get());
197 if (pDrawViewShell
!= nullptr)
199 sal_uInt16 nPageNumber
= (rpDescriptor
->GetPage()->GetPageNum()-1)/2;
200 pDrawViewShell
->SwitchPage(nPageNumber
);
201 TabControl
& rPageTabControl
= pDrawViewShell
->GetPageTabControl();
202 rPageTabControl
.SetCurPageId(rPageTabControl
.GetPageId(nPageNumber
));
203 pDrawViewShell
->UpdateScrollBars();
208 void CurrentSlideManager::SetCurrentSlideAtTabControl (const SharedPageDescriptor
& rpDescriptor
)
210 OSL_ASSERT(rpDescriptor
);
212 ViewShellBase
* pBase
= mrSlideSorter
.GetViewShellBase();
213 if (pBase
!= nullptr)
215 std::shared_ptr
<DrawViewShell
> pDrawViewShell (
216 std::dynamic_pointer_cast
<DrawViewShell
>(pBase
->GetMainViewShell()));
219 sal_uInt16 nPageNumber
= (rpDescriptor
->GetPage()->GetPageNum()-1)/2;
220 TabControl
& rPageTabControl
= pDrawViewShell
->GetPageTabControl();
221 rPageTabControl
.SetCurPageId(rPageTabControl
.GetPageId(nPageNumber
));
226 void CurrentSlideManager::SetCurrentSlideAtXController (const SharedPageDescriptor
& rpDescriptor
)
228 OSL_ASSERT(rpDescriptor
);
232 Reference
<beans::XPropertySet
> xSet (mrSlideSorter
.GetXController(), UNO_QUERY
);
236 aPage
<<= rpDescriptor
->GetPage()->getUnoPage();
237 xSet
->setPropertyValue( u
"CurrentPage"_ustr
, aPage
);
240 catch (const Exception
&)
242 // We have not been able to set the current page at the main view.
243 // This is sad but still leaves us in a valid state. Therefore,
244 // this exception is silently ignored.
248 void CurrentSlideManager::PrepareModelChange()
250 mpCurrentSlide
.reset();
253 void CurrentSlideManager::HandleModelChange()
255 if (mnCurrentSlideIndex
>= 0)
257 mpCurrentSlide
= mrSlideSorter
.GetModel().GetPageDescriptor(mnCurrentSlideIndex
);
259 mrSlideSorter
.GetView().SetState(mpCurrentSlide
, PageDescriptor::ST_Current
, true);
263 IMPL_LINK_NOARG(CurrentSlideManager
, SwitchPageCallback
, Timer
*, void)
267 // Set current page. At the moment we have to do this in two
268 // different ways. The UNO way is the preferable one but, alas,
269 // it does not work always correctly (after some kinds of model
270 // changes). Therefore, we call DrawViewShell::SwitchPage(),
272 SetCurrentSlideAtViewShellBase(mpCurrentSlide
);
273 SetCurrentSlideAtXController(mpCurrentSlide
);
277 } // end of namespace ::sd::slidesorter::controller
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */