merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / slidesorter / controller / SlsFocusManager.cxx
blob9cb8c7d38c3d66f3503b5d49e7f5b4e6d4b4e1c0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsFocusManager.cxx,v $
10 * $Revision: 1.14 $
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>
44 #include "Window.hxx"
45 #include "sdpage.hxx"
47 namespace sd { namespace slidesorter { namespace controller {
49 FocusManager::FocusManager (SlideSorter& rSlideSorter)
50 : mrSlideSorter(rSlideSorter),
51 mnPageIndex(0),
52 mbPageIsFocused(false)
54 if (mrSlideSorter.GetModel().GetPageCount() > 0)
55 mnPageIndex = 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());
75 switch (eDirection)
77 case FMD_NONE:
78 if (mnPageIndex >= mrSlideSorter.GetModel().GetPageCount())
79 mnPageIndex = mrSlideSorter.GetModel().GetPageCount() - 1;
80 break;
82 case FMD_LEFT:
83 mnPageIndex -= 1;
84 if (mnPageIndex < 0)
86 mnPageIndex = mrSlideSorter.GetModel().GetPageCount() - 1;
87 SetFocusToToolBox();
89 break;
91 case FMD_RIGHT:
92 mnPageIndex += 1;
93 if (mnPageIndex >= mrSlideSorter.GetModel().GetPageCount())
95 mnPageIndex = 0;
96 SetFocusToToolBox();
98 break;
100 case FMD_UP:
102 int nColumn = mnPageIndex % nColumnCount;
103 mnPageIndex -= nColumnCount;
104 if (mnPageIndex < 0)
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
114 - nColumnCount
115 + (nColumn - nCandidateColumn);
116 else
117 mnPageIndex = nCandidate;
120 break;
122 case FMD_DOWN:
124 int nColumn = mnPageIndex % nColumnCount;
125 mnPageIndex += nColumnCount;
126 if (mnPageIndex >= mrSlideSorter.GetModel().GetPageCount())
128 // Wrap arround to the correct column.
129 mnPageIndex = nColumn;
132 break;
135 if (mbPageIsFocused)
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)
165 if (mbPageIsFocused)
166 HideFocus ();
167 else
168 ShowFocus ();
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
194 return mnPageIndex;
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())
210 ShowFocus();
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)
303 HideFocus();
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();
314 if (pDockingWindow)
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)
355 if (mbFocusVisible)
356 mrManager.ShowFocus();
359 } } } // end of namespace ::sd::slidesorter::controller