Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "controller/SlsFocusManager.hxx"
21 :
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>
32 :
33 : #include "Window.hxx"
34 : #include "sdpage.hxx"
35 :
36 : namespace sd { namespace slidesorter { namespace controller {
37 :
38 64 : FocusManager::FocusManager (SlideSorter& rSlideSorter)
39 : : mrSlideSorter(rSlideSorter),
40 : mnPageIndex(0),
41 : mbPageIsFocused(false),
42 64 : mbIsVerticalWrapActive(false)
43 : {
44 64 : if (mrSlideSorter.GetModel().GetPageCount() > 0)
45 0 : mnPageIndex = 0;
46 64 : }
47 :
48 64 : FocusManager::~FocusManager()
49 : {
50 64 : }
51 :
52 0 : void FocusManager::MoveFocus (FocusMoveDirection eDirection)
53 : {
54 0 : if (mnPageIndex >= 0 && mbPageIsFocused)
55 : {
56 0 : HideFocusIndicator (GetFocusedPageDescriptor());
57 :
58 0 : const sal_Int32 nColumnCount (mrSlideSorter.GetView().GetLayouter().GetColumnCount());
59 0 : const sal_Int32 nPageCount (mrSlideSorter.GetModel().GetPageCount());
60 0 : switch (eDirection)
61 : {
62 : case FMD_NONE:
63 : // Nothing to be done.
64 0 : break;
65 :
66 : case FMD_LEFT:
67 0 : if (mnPageIndex > 0)
68 0 : mnPageIndex -= 1;
69 0 : else if (mbIsVerticalWrapActive)
70 0 : mnPageIndex = nPageCount-1;
71 0 : break;
72 :
73 : case FMD_RIGHT:
74 0 : if (mnPageIndex < nPageCount-1)
75 0 : mnPageIndex += 1;
76 0 : else if (mbIsVerticalWrapActive)
77 0 : mnPageIndex = 0;
78 0 : break;
79 :
80 : case FMD_UP:
81 : {
82 0 : const sal_Int32 nCandidate (mnPageIndex - nColumnCount);
83 0 : if (nCandidate < 0)
84 : {
85 0 : if (mbIsVerticalWrapActive)
86 : {
87 : // Wrap around to the bottom row or the one above
88 : // and go to the correct column.
89 0 : const sal_Int32 nLastIndex (nPageCount-1);
90 0 : const sal_Int32 nLastColumn (nLastIndex % nColumnCount);
91 0 : const sal_Int32 nCurrentColumn (mnPageIndex%nColumnCount);
92 0 : if (nLastColumn >= nCurrentColumn)
93 : {
94 : // The last row contains the current column.
95 0 : mnPageIndex = nLastIndex - (nLastColumn-nCurrentColumn);
96 : }
97 : else
98 : {
99 : // Only the second to last row contains the current column.
100 0 : mnPageIndex = nLastIndex - nLastColumn
101 0 : - nColumnCount
102 0 : + nCurrentColumn;
103 : }
104 : }
105 : }
106 : else
107 : {
108 : // Move the focus the previous row.
109 0 : mnPageIndex = nCandidate;
110 : }
111 : }
112 0 : break;
113 :
114 : case FMD_DOWN:
115 : {
116 0 : const sal_Int32 nCandidate (mnPageIndex + nColumnCount);
117 0 : if (nCandidate >= nPageCount)
118 : {
119 0 : if (mbIsVerticalWrapActive)
120 : {
121 : // Wrap around to the correct column.
122 0 : mnPageIndex = mnPageIndex % nColumnCount;
123 : }
124 : else
125 : {
126 : // Do not move the focus.
127 : }
128 : }
129 : else
130 : {
131 : // Move the focus to the next row.
132 0 : mnPageIndex = nCandidate;
133 : }
134 : }
135 0 : break;
136 : }
137 :
138 0 : if (mnPageIndex < 0)
139 : {
140 : OSL_ASSERT(mnPageIndex>=0);
141 0 : mnPageIndex = 0;
142 : }
143 0 : else if (mnPageIndex >= nPageCount)
144 : {
145 : OSL_ASSERT(mnPageIndex<nPageCount);
146 0 : mnPageIndex = nPageCount - 1;
147 : }
148 :
149 0 : if (mbPageIsFocused)
150 : {
151 0 : ShowFocusIndicator(GetFocusedPageDescriptor(), true);
152 : }
153 : }
154 0 : }
155 :
156 0 : void FocusManager::ShowFocus (const bool bScrollToFocus)
157 : {
158 0 : mbPageIsFocused = true;
159 0 : ShowFocusIndicator(GetFocusedPageDescriptor(), bScrollToFocus);
160 0 : }
161 :
162 85 : void FocusManager::HideFocus()
163 : {
164 85 : mbPageIsFocused = false;
165 85 : HideFocusIndicator(GetFocusedPageDescriptor());
166 85 : }
167 :
168 0 : bool FocusManager::ToggleFocus()
169 : {
170 0 : if (mnPageIndex >= 0)
171 : {
172 0 : if (mbPageIsFocused)
173 0 : HideFocus ();
174 : else
175 0 : ShowFocus ();
176 : }
177 0 : return mbPageIsFocused;
178 : }
179 :
180 85 : bool FocusManager::HasFocus() const
181 : {
182 85 : return mrSlideSorter.GetContentWindow()->HasFocus();
183 : }
184 :
185 85 : model::SharedPageDescriptor FocusManager::GetFocusedPageDescriptor() const
186 : {
187 85 : return mrSlideSorter.GetModel().GetPageDescriptor(mnPageIndex);
188 : }
189 :
190 85 : void FocusManager::SetFocusedPage (const model::SharedPageDescriptor& rpDescriptor)
191 : {
192 85 : if (rpDescriptor.get() != NULL)
193 : {
194 85 : FocusHider aFocusHider (*this);
195 85 : mnPageIndex = (rpDescriptor->GetPage()->GetPageNum()-1)/2;
196 : }
197 85 : }
198 :
199 0 : void FocusManager::SetFocusedPage (sal_Int32 nPageIndex)
200 : {
201 0 : FocusHider aFocusHider (*this);
202 0 : mnPageIndex = nPageIndex;
203 0 : }
204 :
205 0 : void FocusManager::SetFocusedPageToCurrentPage()
206 : {
207 0 : SetFocusedPage(mrSlideSorter.GetController().GetCurrentSlideManager()->GetCurrentSlide());
208 0 : }
209 :
210 85 : bool FocusManager::IsFocusShowing() const
211 : {
212 85 : return HasFocus() && mbPageIsFocused;
213 : }
214 :
215 85 : void FocusManager::HideFocusIndicator (const model::SharedPageDescriptor& rpDescriptor)
216 : {
217 85 : if (rpDescriptor.get() != NULL)
218 : {
219 82 : mrSlideSorter.GetView().SetState(rpDescriptor, model::PageDescriptor::ST_Focused, false);
220 :
221 : // Hide focus should also fire the focus event, Currently, only accessibility add the focus listener
222 82 : NotifyFocusChangeListeners();
223 : }
224 85 : }
225 :
226 0 : void FocusManager::ShowFocusIndicator (
227 : const model::SharedPageDescriptor& rpDescriptor,
228 : const bool bScrollToFocus)
229 : {
230 0 : if (rpDescriptor.get() != NULL)
231 : {
232 0 : mrSlideSorter.GetView().SetState(rpDescriptor, model::PageDescriptor::ST_Focused, true);
233 :
234 0 : if (bScrollToFocus)
235 : {
236 : // Scroll the focused page object into the visible area and repaint
237 : // it, so that the focus indicator becomes visible.
238 0 : mrSlideSorter.GetController().GetVisibleAreaManager().RequestVisible(rpDescriptor,true);
239 : }
240 0 : mrSlideSorter.GetView().RequestRepaint(rpDescriptor);
241 :
242 0 : NotifyFocusChangeListeners();
243 : }
244 0 : }
245 :
246 0 : void FocusManager::AddFocusChangeListener (const Link<>& rListener)
247 : {
248 0 : if (::std::find (maFocusChangeListeners.begin(), maFocusChangeListeners.end(), rListener)
249 0 : == maFocusChangeListeners.end())
250 : {
251 0 : maFocusChangeListeners.push_back (rListener);
252 : }
253 0 : }
254 :
255 0 : void FocusManager::RemoveFocusChangeListener (const Link<>& rListener)
256 : {
257 : maFocusChangeListeners.erase (
258 0 : ::std::find (maFocusChangeListeners.begin(), maFocusChangeListeners.end(), rListener));
259 0 : }
260 :
261 82 : void FocusManager::NotifyFocusChangeListeners() const
262 : {
263 : // Create a copy of the listener list to be safe when that is modified.
264 82 : ::std::vector<Link<>> aListeners (maFocusChangeListeners);
265 :
266 : // Tell the selection change listeners that the selection has changed.
267 82 : ::std::vector<Link<>>::iterator iListener (aListeners.begin());
268 82 : ::std::vector<Link<>>::iterator iEnd (aListeners.end());
269 82 : for (; iListener!=iEnd; ++iListener)
270 : {
271 0 : iListener->Call(NULL);
272 82 : }
273 82 : }
274 :
275 85 : FocusManager::FocusHider::FocusHider (FocusManager& rManager)
276 85 : : mbFocusVisible(rManager.IsFocusShowing())
277 85 : , mrManager(rManager)
278 : {
279 85 : mrManager.HideFocus();
280 85 : }
281 :
282 85 : FocusManager::FocusHider::~FocusHider()
283 : {
284 85 : if (mbFocusVisible)
285 0 : mrManager.ShowFocus();
286 85 : }
287 :
288 66 : } } } // end of namespace ::sd::slidesorter::controller
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|