Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / window / taskpanelist.cxx
blob1f382b824907d1317c0d9f562034673e5aa15b37
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 <vcl/dockwin.hxx>
21 #include <vcl/taskpanelist.hxx>
23 #include <svdata.hxx>
24 #include "menubarwindow.hxx"
26 #include <algorithm>
28 namespace {
30 Point ImplTaskPaneListGetPos( const vcl::Window *w )
32 Point pos;
33 if( w->IsDockingWindow() )
35 pos = static_cast<const DockingWindow*>(w)->GetPosPixel();
36 vcl::Window *pF = static_cast<const DockingWindow*>(w)->GetFloatingWindow();
37 if( pF )
38 pos = pF->OutputToAbsoluteScreenPixel( pF->ScreenToOutputPixel( pos ) );
39 else
40 pos = w->OutputToAbsoluteScreenPixel( pos );
42 else
43 pos = w->OutputToAbsoluteScreenPixel( w->GetPosPixel() );
45 return pos;
50 // compares window pos left-to-right
51 struct LTRSort
53 bool operator()( const vcl::Window* w1, const vcl::Window* w2 ) const
55 Point pos1(ImplTaskPaneListGetPos( w1 ));
56 Point pos2(ImplTaskPaneListGetPos( w2 ));
58 if( pos1.X() == pos2.X() )
59 return ( pos1.Y() < pos2.Y() );
60 else
61 return ( pos1.X() < pos2.X() );
64 struct LTRSortBackward
66 bool operator()( const vcl::Window* w2, const vcl::Window* w1 ) const
68 Point pos1(ImplTaskPaneListGetPos( w1 ));
69 Point pos2(ImplTaskPaneListGetPos( w2 ));
71 if( pos1.X() == pos2.X() )
72 return ( pos1.Y() < pos2.Y() );
73 else
74 return ( pos1.X() < pos2.X() );
78 static void ImplTaskPaneListGrabFocus( vcl::Window *pWindow, bool bForward )
80 // put focus in child of floating windows which is typically a toolbar
81 // that can deal with the focus
82 if( pWindow->ImplIsFloatingWindow() && pWindow->GetWindow( GetWindowType::FirstChild ) )
83 pWindow = pWindow->GetWindow( GetWindowType::FirstChild );
84 pWindow->ImplGrabFocus( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
87 TaskPaneList::TaskPaneList()
91 TaskPaneList::~TaskPaneList()
95 void TaskPaneList::AddWindow( vcl::Window *pWindow )
97 if( pWindow )
99 auto insertionPos = dynamic_cast<MenuBarWindow*>(pWindow) ? mTaskPanes.begin() : mTaskPanes.end();
100 for ( auto p = mTaskPanes.begin(); p != mTaskPanes.end(); ++p )
102 if ( *p == pWindow )
103 // avoid duplicates
104 return;
106 // If the new window is the child of an existing pane window, or vice versa,
107 // ensure that in our pane list, *first* the child window appears, *then*
108 // the ancestor window.
109 // This is necessary for HandleKeyEvent: There, the list is traveled from the
110 // beginning, until the first window is found which has the ChildPathFocus. Now
111 // if this would be the ancestor window of another pane window, this would fudge
112 // the result
113 if ( pWindow->IsWindowOrChild( *p ) )
115 insertionPos = p + 1;
116 break;
118 if ( (*p)->IsWindowOrChild( pWindow ) )
120 insertionPos = p;
121 break;
125 mTaskPanes.insert( insertionPos, pWindow );
126 pWindow->ImplIsInTaskPaneList( true );
130 void TaskPaneList::RemoveWindow( vcl::Window *pWindow )
132 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
133 if( p != mTaskPanes.end() )
135 mTaskPanes.erase( p );
136 pWindow->ImplIsInTaskPaneList( false );
140 bool TaskPaneList::IsInList( vcl::Window *pWindow )
142 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
143 return p != mTaskPanes.end();
146 bool TaskPaneList::IsCycleKey(const vcl::KeyCode& rKeyCode)
148 return rKeyCode.GetCode() == KEY_F6 && !rKeyCode.IsMod2(); // F6
151 bool TaskPaneList::HandleKeyEvent(const KeyEvent& rKeyEvent)
154 // F6 cycles through everything and works always
156 // MAV, #i104204#
157 // The old design was the following one:
158 // < Ctrl-TAB cycles through Menubar, Toolbars and Floatingwindows only and is
159 // < only active if one of those items has the focus
161 // Since the design of Ctrl-Tab looks to be inconsistent ( non-modal dialogs are not reachable
162 // and the shortcut conflicts with tab-control shortcut ), it is no more supported
163 vcl::KeyCode aKeyCode = rKeyEvent.GetKeyCode();
164 bool bForward = !aKeyCode.IsShift();
165 if (TaskPaneList::IsCycleKey(aKeyCode))
167 bool bSplitterOnly = aKeyCode.IsMod1() && aKeyCode.IsShift();
169 // is the focus in the list ?
170 auto p = std::find_if(mTaskPanes.begin(), mTaskPanes.end(),
171 [](const VclPtr<vcl::Window>& rWinPtr) { return rWinPtr->HasChildPathFocus( true ); });
172 if( p != mTaskPanes.end() )
174 vcl::Window *pWin = p->get();
176 // Ctrl-F6 goes directly to the document
177 if( !pWin->IsDialog() && aKeyCode.IsMod1() && !aKeyCode.IsShift() )
179 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 );
180 return true;
183 // activate next task pane
184 vcl::Window *pNextWin = nullptr;
186 if( bSplitterOnly )
187 pNextWin = FindNextSplitter( *p );
188 else
189 pNextWin = FindNextFloat( *p, bForward );
191 if( pNextWin != pWin )
193 ImplGetSVData()->maWinData.mbNoSaveFocus = true;
194 ImplTaskPaneListGrabFocus( pNextWin, bForward );
195 ImplGetSVData()->maWinData.mbNoSaveFocus = false;
197 else
199 // forward key if no splitter found
200 if( bSplitterOnly )
201 return false;
203 // we did not find another taskpane, so
204 // put focus back into document
205 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
208 return true;
211 // the focus is not in the list: activate first float if F6 was pressed
212 vcl::Window *pWin;
213 if( bSplitterOnly )
214 pWin = FindNextSplitter( nullptr );
215 else
216 pWin = FindNextFloat( nullptr, bForward );
217 if( pWin )
219 ImplTaskPaneListGrabFocus( pWin, bForward );
220 return true;
224 return false;
227 // returns next splitter
228 vcl::Window* TaskPaneList::FindNextSplitter( vcl::Window *pWindow )
230 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
232 auto p = mTaskPanes.begin();
233 if( pWindow )
234 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
236 if( p != mTaskPanes.end() )
238 unsigned n = mTaskPanes.size();
239 while( --n )
241 if( pWindow ) // increment before test
242 ++p;
243 if( p == mTaskPanes.end() )
244 p = mTaskPanes.begin();
245 if( (*p)->ImplIsSplitter() && (*p)->IsReallyVisible() && !(*p)->IsDialog() && (*p)->GetParent()->HasChildPathFocus() )
247 pWindow = (*p).get();
248 break;
250 if( !pWindow ) // increment after test, otherwise first element is skipped
251 ++p;
255 return pWindow;
258 // returns first valid item (regardless of type) if pWindow==0, otherwise returns next valid float
259 vcl::Window* TaskPaneList::FindNextFloat( vcl::Window *pWindow, bool bForward )
261 if( bForward )
262 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
263 else
264 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSortBackward() );
266 auto p = mTaskPanes.begin();
267 if( pWindow )
268 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
270 while( p != mTaskPanes.end() )
272 if( pWindow ) // increment before test
273 ++p;
274 if( p == mTaskPanes.end() )
275 break; // do not wrap, send focus back to document at end of list
276 /* #i83908# do not use the menubar if it is native and invisible
278 if( (*p)->IsReallyVisible() && !(*p)->ImplIsSplitter() &&
279 ( (*p)->GetType() != WindowType::MENUBARWINDOW || static_cast<MenuBarWindow*>(p->get())->CanGetFocus() ) )
281 pWindow = (*p).get();
282 break;
284 if( !pWindow ) // increment after test, otherwise first element is skipped
285 ++p;
288 return pWindow;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */