build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / window / taskpanelist.cxx
blob0cd840779a5adb1a51e8c6bccb1bdf3017539784
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <tools/rcid.h>
22 #include <vcl/dockwin.hxx>
23 #include <vcl/taskpanelist.hxx>
25 #include <svdata.hxx>
26 #include "menubarwindow.hxx"
28 #include <functional>
29 #include <algorithm>
31 namespace {
33 Point ImplTaskPaneListGetPos( const vcl::Window *w )
35 Point pos;
36 if( w->IsDockingWindow() )
38 pos = static_cast<const DockingWindow*>(w)->GetPosPixel();
39 vcl::Window *pF = static_cast<const DockingWindow*>(w)->GetFloatingWindow();
40 if( pF )
41 pos = pF->OutputToAbsoluteScreenPixel( pF->ScreenToOutputPixel( pos ) );
42 else
43 pos = w->OutputToAbsoluteScreenPixel( pos );
45 else
46 pos = w->OutputToAbsoluteScreenPixel( w->GetPosPixel() );
48 return pos;
53 // compares window pos left-to-right
54 struct LTRSort : public ::std::binary_function< const vcl::Window*, const vcl::Window*, bool >
56 bool operator()( const vcl::Window* w1, const vcl::Window* w2 ) const
58 Point pos1(ImplTaskPaneListGetPos( w1 ));
59 Point pos2(ImplTaskPaneListGetPos( w2 ));
61 if( pos1.X() == pos2.X() )
62 return ( pos1.Y() < pos2.Y() );
63 else
64 return ( pos1.X() < pos2.X() );
67 struct LTRSortBackward : public ::std::binary_function< const vcl::Window*, const vcl::Window*, bool >
69 bool operator()( const vcl::Window* w2, const vcl::Window* w1 ) const
71 Point pos1(ImplTaskPaneListGetPos( w1 ));
72 Point pos2(ImplTaskPaneListGetPos( w2 ));
74 if( pos1.X() == pos2.X() )
75 return ( pos1.Y() < pos2.Y() );
76 else
77 return ( pos1.X() < pos2.X() );
81 static void ImplTaskPaneListGrabFocus( vcl::Window *pWindow, bool bForward )
83 // put focus in child of floating windows which is typically a toolbar
84 // that can deal with the focus
85 if( pWindow->ImplIsFloatingWindow() && pWindow->GetWindow( GetWindowType::FirstChild ) )
86 pWindow = pWindow->GetWindow( GetWindowType::FirstChild );
87 pWindow->ImplGrabFocus( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
90 TaskPaneList::TaskPaneList()
94 TaskPaneList::~TaskPaneList()
98 void TaskPaneList::AddWindow( vcl::Window *pWindow )
100 if( pWindow )
102 auto insertionPos = dynamic_cast<MenuBarWindow*>(pWindow) ? mTaskPanes.begin() : mTaskPanes.end();
103 for ( auto p = mTaskPanes.begin(); p != mTaskPanes.end(); ++p )
105 if ( *p == pWindow )
106 // avoid duplicates
107 return;
109 // If the new window is the child of an existing pane window, or vice versa,
110 // ensure that in our pane list, *first* the child window appears, *then*
111 // the ancestor window.
112 // This is necessary for HandleKeyEvent: There, the list is traveled from the
113 // beginning, until the first window is found which has the ChildPathFocus. Now
114 // if this would be the ancestor window of another pane window, this would fudge
115 // the result
116 if ( pWindow->IsWindowOrChild( *p ) )
118 insertionPos = p + 1;
119 break;
121 if ( (*p)->IsWindowOrChild( pWindow ) )
123 insertionPos = p;
124 break;
128 mTaskPanes.insert( insertionPos, pWindow );
129 pWindow->ImplIsInTaskPaneList( true );
133 void TaskPaneList::RemoveWindow( vcl::Window *pWindow )
135 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
136 if( p != mTaskPanes.end() )
138 mTaskPanes.erase( p );
139 pWindow->ImplIsInTaskPaneList( false );
143 bool TaskPaneList::IsInList( vcl::Window *pWindow )
145 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
146 if( p != mTaskPanes.end() )
147 return true;
148 else
149 return false;
152 bool TaskPaneList::IsCycleKey(const vcl::KeyCode& rKeyCode)
154 return rKeyCode.GetCode() == KEY_F6 && !rKeyCode.IsMod2(); // F6
157 bool TaskPaneList::HandleKeyEvent(const KeyEvent& rKeyEvent)
160 // F6 cycles through everything and works always
162 // MAV, #i104204#
163 // The old design was the following one:
164 // < Ctrl-TAB cycles through Menubar, Toolbars and Floatingwindows only and is
165 // < only active if one of those items has the focus
167 // Since the design of Ctrl-Tab looks to be inconsistent ( non-modal dialogs are not reachable
168 // and the shortcut conflicts with tab-control shortcut ), it is no more supported
169 vcl::KeyCode aKeyCode = rKeyEvent.GetKeyCode();
170 bool bForward = !aKeyCode.IsShift();
171 if (TaskPaneList::IsCycleKey(aKeyCode))
173 bool bSplitterOnly = aKeyCode.IsMod1() && aKeyCode.IsShift();
175 // is the focus in the list ?
176 auto p = mTaskPanes.begin();
177 while( p != mTaskPanes.end() )
179 vcl::Window *pWin = p->get();
180 if( pWin->HasChildPathFocus( true ) )
182 // Ctrl-F6 goes directly to the document
183 if( !pWin->IsDialog() && aKeyCode.IsMod1() && !aKeyCode.IsShift() )
185 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 );
186 return true;
189 // activate next task pane
190 vcl::Window *pNextWin = nullptr;
192 if( bSplitterOnly )
193 pNextWin = FindNextSplitter( *p );
194 else
195 pNextWin = FindNextFloat( *p, bForward );
197 if( pNextWin != pWin )
199 ImplGetSVData()->maWinData.mbNoSaveFocus = true;
200 ImplTaskPaneListGrabFocus( pNextWin, bForward );
201 ImplGetSVData()->maWinData.mbNoSaveFocus = false;
203 else
205 // forward key if no splitter found
206 if( bSplitterOnly )
207 return false;
209 // we did not find another taskpane, so
210 // put focus back into document
211 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
214 return true;
216 else
217 ++p;
220 // the focus is not in the list: activate first float if F6 was pressed
221 vcl::Window *pWin;
222 if( bSplitterOnly )
223 pWin = FindNextSplitter( nullptr );
224 else
225 pWin = FindNextFloat( nullptr, bForward );
226 if( pWin )
228 ImplTaskPaneListGrabFocus( pWin, bForward );
229 return true;
233 return false;
236 // returns next splitter
237 vcl::Window* TaskPaneList::FindNextSplitter( vcl::Window *pWindow )
239 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
241 auto p = mTaskPanes.begin();
242 while( p != mTaskPanes.end() )
244 if( !pWindow || *p == pWindow )
246 unsigned n = mTaskPanes.size();
247 while( --n )
249 if( pWindow ) // increment before test
250 ++p;
251 if( p == mTaskPanes.end() )
252 p = mTaskPanes.begin();
253 if( (*p)->ImplIsSplitter() && (*p)->IsReallyVisible() && !(*p)->IsDialog() && (*p)->GetParent()->HasChildPathFocus() )
255 pWindow = (*p).get();
256 break;
258 if( !pWindow ) // increment after test, otherwise first element is skipped
259 ++p;
261 break;
263 else
264 ++p;
267 return pWindow;
270 // returns first valid item (regardless of type) if pWindow==0, otherwise returns next valid float
271 vcl::Window* TaskPaneList::FindNextFloat( vcl::Window *pWindow, bool bForward )
273 if( bForward )
274 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
275 else
276 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSortBackward() );
278 auto p = mTaskPanes.begin();
279 while( p != mTaskPanes.end() )
281 if( !pWindow || *p == pWindow )
283 while( p != mTaskPanes.end() )
285 if( pWindow ) // increment before test
286 ++p;
287 if( p == mTaskPanes.end() )
288 break; // do not wrap, send focus back to document at end of list
289 /* #i83908# do not use the menubar if it is native and invisible
291 if( (*p)->IsReallyVisible() && !(*p)->ImplIsSplitter() &&
292 ( (*p)->GetType() != WINDOW_MENUBARWINDOW || static_cast<MenuBarWindow*>(p->get())->CanGetFocus() ) )
294 pWindow = (*p).get();
295 break;
297 if( !pWindow ) // increment after test, otherwise first element is skipped
298 ++p;
300 break;
302 else
303 ++p;
306 return pWindow;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */