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 <controller/SlsFocusManager.hxx>
22 #include <SlideSorter.hxx>
23 #include <PaneDockingWindow.hxx>
24 #include <controller/SlideSorterController.hxx>
25 #include <controller/SlsCurrentSlideManager.hxx>
26 #include <controller/SlsVisibleAreaManager.hxx>
27 #include <model/SlideSorterModel.hxx>
28 #include <model/SlsPageDescriptor.hxx>
29 #include <view/SlideSorterView.hxx>
30 #include <view/SlsLayouter.hxx>
31 #include <vcl/toolbox.hxx>
36 namespace sd
{ namespace slidesorter
{ namespace controller
{
38 FocusManager::FocusManager (SlideSorter
& rSlideSorter
)
39 : mrSlideSorter(rSlideSorter
),
41 mbPageIsFocused(false)
43 if (mrSlideSorter
.GetModel().GetPageCount() > 0)
47 FocusManager::~FocusManager()
51 void FocusManager::MoveFocus (FocusMoveDirection eDirection
)
53 if (!(mnPageIndex
>= 0 && mbPageIsFocused
))
56 HideFocusIndicator (GetFocusedPageDescriptor());
58 const sal_Int32
nColumnCount (mrSlideSorter
.GetView().GetLayouter().GetColumnCount());
59 const sal_Int32
nPageCount (mrSlideSorter
.GetModel().GetPageCount());
62 case FocusMoveDirection::Left
:
67 case FocusMoveDirection::Right
:
68 if (mnPageIndex
< nPageCount
-1)
72 case FocusMoveDirection::Up
:
74 const sal_Int32
nCandidate (mnPageIndex
- nColumnCount
);
77 // Move the focus the previous row.
78 mnPageIndex
= nCandidate
;
83 case FocusMoveDirection::Down
:
85 const sal_Int32
nCandidate (mnPageIndex
+ nColumnCount
);
86 if (nCandidate
< nPageCount
)
88 // Move the focus to the next row.
89 mnPageIndex
= nCandidate
;
97 OSL_ASSERT(mnPageIndex
>=0);
100 else if (mnPageIndex
>= nPageCount
)
102 OSL_ASSERT(mnPageIndex
<nPageCount
);
103 mnPageIndex
= nPageCount
- 1;
108 ShowFocusIndicator(GetFocusedPageDescriptor(), true);
112 void FocusManager::ShowFocus (const bool bScrollToFocus
)
114 mbPageIsFocused
= true;
115 ShowFocusIndicator(GetFocusedPageDescriptor(), bScrollToFocus
);
118 void FocusManager::HideFocus()
120 mbPageIsFocused
= false;
121 HideFocusIndicator(GetFocusedPageDescriptor());
124 bool FocusManager::ToggleFocus()
126 if (mnPageIndex
>= 0)
133 return mbPageIsFocused
;
136 bool FocusManager::HasFocus() const
138 return mrSlideSorter
.GetContentWindow()->HasFocus();
141 model::SharedPageDescriptor
FocusManager::GetFocusedPageDescriptor() const
143 return mrSlideSorter
.GetModel().GetPageDescriptor(mnPageIndex
);
146 void FocusManager::SetFocusedPage (const model::SharedPageDescriptor
& rpDescriptor
)
148 if (rpDescriptor
.get() != nullptr)
150 FocusHider
aFocusHider (*this);
151 mnPageIndex
= (rpDescriptor
->GetPage()->GetPageNum()-1)/2;
155 void FocusManager::SetFocusedPage (sal_Int32 nPageIndex
)
157 FocusHider
aFocusHider (*this);
158 mnPageIndex
= nPageIndex
;
161 void FocusManager::SetFocusedPageToCurrentPage()
163 SetFocusedPage(mrSlideSorter
.GetController().GetCurrentSlideManager()->GetCurrentSlide());
166 bool FocusManager::IsFocusShowing() const
168 return HasFocus() && mbPageIsFocused
;
171 void FocusManager::HideFocusIndicator (const model::SharedPageDescriptor
& rpDescriptor
)
173 if (rpDescriptor
.get() != nullptr)
175 mrSlideSorter
.GetView().SetState(rpDescriptor
, model::PageDescriptor::ST_Focused
, false);
177 // Hide focus should also fire the focus event, Currently, only accessibility add the focus listener
178 NotifyFocusChangeListeners();
182 void FocusManager::ShowFocusIndicator (
183 const model::SharedPageDescriptor
& rpDescriptor
,
184 const bool bScrollToFocus
)
186 if (rpDescriptor
.get() == nullptr)
189 mrSlideSorter
.GetView().SetState(rpDescriptor
, model::PageDescriptor::ST_Focused
, true);
193 // Scroll the focused page object into the visible area and repaint
194 // it, so that the focus indicator becomes visible.
195 mrSlideSorter
.GetController().GetVisibleAreaManager().RequestVisible(rpDescriptor
,true);
197 mrSlideSorter
.GetView().RequestRepaint(rpDescriptor
);
199 NotifyFocusChangeListeners();
202 void FocusManager::AddFocusChangeListener (const Link
<LinkParamNone
*,void>& rListener
)
204 if (::std::find (maFocusChangeListeners
.begin(), maFocusChangeListeners
.end(), rListener
)
205 == maFocusChangeListeners
.end())
207 maFocusChangeListeners
.push_back (rListener
);
211 void FocusManager::RemoveFocusChangeListener (const Link
<LinkParamNone
*,void>& rListener
)
213 maFocusChangeListeners
.erase (
214 ::std::find (maFocusChangeListeners
.begin(), maFocusChangeListeners
.end(), rListener
));
217 void FocusManager::NotifyFocusChangeListeners() const
219 // Create a copy of the listener list to be safe when that is modified.
220 ::std::vector
<Link
<LinkParamNone
*,void>> aListeners (maFocusChangeListeners
);
222 // Tell the selection change listeners that the selection has changed.
223 for (auto& rListener
: aListeners
)
225 rListener
.Call(nullptr);
229 FocusManager::FocusHider::FocusHider (FocusManager
& rManager
)
230 : mbFocusVisible(rManager
.IsFocusShowing())
231 , mrManager(rManager
)
233 mrManager
.HideFocus();
236 FocusManager::FocusHider::~FocusHider() COVERITY_NOEXCEPT_FALSE
239 mrManager
.ShowFocus();
242 } } } // end of namespace ::sd::slidesorter::controller
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */