1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsFocusManager.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
32 #include "controller/SlsFocusManager.hxx"
34 #include "SlideSorter.hxx"
35 #include "PaneDockingWindow.hxx"
36 #include "controller/SlideSorterController.hxx"
37 #include "controller/SlsSelectionManager.hxx"
38 #include "model/SlideSorterModel.hxx"
39 #include "model/SlsPageDescriptor.hxx"
40 #include "view/SlideSorterView.hxx"
41 #include "view/SlsLayouter.hxx"
42 #include <vcl/toolbox.hxx>
47 namespace sd
{ namespace slidesorter
{ namespace controller
{
49 FocusManager::FocusManager (SlideSorter
& rSlideSorter
)
50 : mrSlideSorter(rSlideSorter
),
52 mbPageIsFocused(false)
54 if (mrSlideSorter
.GetModel().GetPageCount() > 0)
61 FocusManager::~FocusManager (void)
68 void FocusManager::MoveFocus (FocusMoveDirection eDirection
)
70 if (mnPageIndex
>= 0 && mbPageIsFocused
)
72 HideFocusIndicator (GetFocusedPageDescriptor());
74 int nColumnCount (mrSlideSorter
.GetView().GetLayouter().GetColumnCount());
78 if (mnPageIndex
>= mrSlideSorter
.GetModel().GetPageCount())
79 mnPageIndex
= mrSlideSorter
.GetModel().GetPageCount() - 1;
86 mnPageIndex
= mrSlideSorter
.GetModel().GetPageCount() - 1;
93 if (mnPageIndex
>= mrSlideSorter
.GetModel().GetPageCount())
102 int nColumn
= mnPageIndex
% nColumnCount
;
103 mnPageIndex
-= nColumnCount
;
106 // Wrap arround to the bottom row or the one above and
107 // go to the correct column.
108 int nCandidate
= mrSlideSorter
.GetModel().GetPageCount()-1;
109 int nCandidateColumn
= nCandidate
% nColumnCount
;
110 if (nCandidateColumn
> nColumn
)
111 mnPageIndex
= nCandidate
- (nCandidateColumn
-nColumn
);
112 else if (nCandidateColumn
< nColumn
)
113 mnPageIndex
= nCandidate
115 + (nColumn
- nCandidateColumn
);
117 mnPageIndex
= nCandidate
;
124 int nColumn
= mnPageIndex
% nColumnCount
;
125 mnPageIndex
+= nColumnCount
;
126 if (mnPageIndex
>= mrSlideSorter
.GetModel().GetPageCount())
128 // Wrap arround to the correct column.
129 mnPageIndex
= nColumn
;
136 ShowFocusIndicator(GetFocusedPageDescriptor());
143 void FocusManager::ShowFocus (void)
145 mbPageIsFocused
= true;
146 ShowFocusIndicator(GetFocusedPageDescriptor());
152 void FocusManager::HideFocus (void)
154 mbPageIsFocused
= false;
155 HideFocusIndicator(GetFocusedPageDescriptor());
161 bool FocusManager::ToggleFocus (void)
163 if (mnPageIndex
>= 0)
170 return mbPageIsFocused
;
176 bool FocusManager::HasFocus (void) const
178 return mrSlideSorter
.GetView().GetWindow()->HasFocus();
184 model::SharedPageDescriptor
FocusManager::GetFocusedPageDescriptor (void) const
186 return mrSlideSorter
.GetModel().GetPageDescriptor(mnPageIndex
);
192 sal_Int32
FocusManager::GetFocusedPageIndex (void) const
200 void FocusManager::FocusPage (sal_Int32 nPageIndex
)
202 if (nPageIndex
!= mnPageIndex
)
204 // Hide the focus while switching it to the specified page.
205 FocusHider
aHider (*this);
206 mnPageIndex
= nPageIndex
;
209 if (HasFocus() && !IsFocusShowing())
216 void FocusManager::SetFocusedPage (const model::SharedPageDescriptor
& rpDescriptor
)
218 if (rpDescriptor
.get() != NULL
)
220 FocusHider
aFocusHider (*this);
221 mnPageIndex
= (rpDescriptor
->GetPage()->GetPageNum()-1)/2;
228 void FocusManager::SetFocusedPage (sal_Int32 nPageIndex
)
230 FocusHider
aFocusHider (*this);
231 mnPageIndex
= nPageIndex
;
237 bool FocusManager::IsFocusShowing (void) const
239 return HasFocus() && mbPageIsFocused
;
245 void FocusManager::HideFocusIndicator (const model::SharedPageDescriptor
& rpDescriptor
)
247 if (rpDescriptor
.get() != NULL
)
249 rpDescriptor
->RemoveFocus();
250 mrSlideSorter
.GetView().RequestRepaint(rpDescriptor
);
257 void FocusManager::ShowFocusIndicator (const model::SharedPageDescriptor
& rpDescriptor
)
259 if (rpDescriptor
.get() != NULL
)
261 rpDescriptor
->SetFocus ();
263 // Scroll the focused page object into the visible area and repaint
264 // it, so that the focus indicator becomes visible.
265 view::SlideSorterView
& rView (mrSlideSorter
.GetView());
266 mrSlideSorter
.GetController().GetSelectionManager()->MakeRectangleVisible (
267 rView
.GetPageBoundingBox (
268 GetFocusedPageDescriptor(),
269 view::SlideSorterView::CS_MODEL
,
270 view::SlideSorterView::BBT_INFO
));
272 mrSlideSorter
.GetView().RequestRepaint (rpDescriptor
);
273 NotifyFocusChangeListeners();
280 void FocusManager::AddFocusChangeListener (const Link
& rListener
)
282 if (::std::find (maFocusChangeListeners
.begin(), maFocusChangeListeners
.end(), rListener
)
283 == maFocusChangeListeners
.end())
285 maFocusChangeListeners
.push_back (rListener
);
292 void FocusManager::RemoveFocusChangeListener (const Link
& rListener
)
294 maFocusChangeListeners
.erase (
295 ::std::find (maFocusChangeListeners
.begin(), maFocusChangeListeners
.end(), rListener
));
301 void FocusManager::SetFocusToToolBox (void)
305 if (mrSlideSorter
.GetViewShell() != NULL
)
307 ::Window
* pParentWindow
= mrSlideSorter
.GetViewShell()->GetParentWindow();
308 DockingWindow
* pDockingWindow
= NULL
;
309 while (pParentWindow
!=NULL
&& pDockingWindow
==NULL
)
311 pDockingWindow
= dynamic_cast<DockingWindow
*>(pParentWindow
);
312 pParentWindow
= pParentWindow
->GetParent();
316 PaneDockingWindow
* pPaneDockingWindow
= dynamic_cast<PaneDockingWindow
*>(pDockingWindow
);
317 if (pPaneDockingWindow
!= NULL
)
318 pPaneDockingWindow
->GetTitleToolBox()->GrabFocus();
326 void FocusManager::NotifyFocusChangeListeners (void) const
328 // Create a copy of the listener list to be safe when that is modified.
329 ::std::vector
<Link
> aListeners (maFocusChangeListeners
);
331 // Tell the slection change listeners that the selection has changed.
332 ::std::vector
<Link
>::iterator
iListener (aListeners
.begin());
333 ::std::vector
<Link
>::iterator
iEnd (aListeners
.end());
334 for (; iListener
!=iEnd
; ++iListener
)
336 iListener
->Call(NULL
);
343 FocusManager::FocusHider::FocusHider (FocusManager
& rManager
)
344 : mbFocusVisible(rManager
.IsFocusShowing())
345 , mrManager(rManager
)
347 mrManager
.HideFocus();
353 FocusManager::FocusHider::~FocusHider (void)
356 mrManager
.ShowFocus();
359 } } } // end of namespace ::sd::slidesorter::controller