bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / window / taskpanelist.cxx
blob833c82a0a114cc431f43a51ca1746b189ecfd5f7
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/toolkit/floatwin.hxx>
22 #include <vcl/taskpanelist.hxx>
24 #include <svdata.hxx>
25 #include "menubarwindow.hxx"
27 #include <algorithm>
29 namespace {
31 Point ImplTaskPaneListGetPos( const vcl::Window *w )
33 Point pos;
34 if( w->IsDockingWindow() )
36 pos = static_cast<const DockingWindow*>(w)->GetPosPixel();
37 vcl::Window *pF = static_cast<const DockingWindow*>(w)->GetFloatingWindow();
38 if( pF )
39 pos = pF->OutputToAbsoluteScreenPixel( pF->ScreenToOutputPixel( pos ) );
40 else
41 pos = w->OutputToAbsoluteScreenPixel( pos );
43 else
44 pos = w->OutputToAbsoluteScreenPixel( w->GetPosPixel() );
46 return pos;
49 // compares window pos left-to-right
50 struct LTRSort
52 bool operator()( const vcl::Window* w1, const vcl::Window* w2 ) const
54 Point pos1(ImplTaskPaneListGetPos( w1 ));
55 Point pos2(ImplTaskPaneListGetPos( w2 ));
57 if( pos1.X() == pos2.X() )
58 return ( pos1.Y() < pos2.Y() );
59 else
60 return ( pos1.X() < pos2.X() );
66 static void ImplTaskPaneListGrabFocus( vcl::Window *pWindow, bool bForward )
68 // put focus in child of floating windows which is typically a toolbar
69 // that can deal with the focus
70 if( pWindow->ImplIsFloatingWindow() && pWindow->GetWindow( GetWindowType::FirstChild ) )
71 pWindow = pWindow->GetWindow( GetWindowType::FirstChild );
72 pWindow->ImplGrabFocus( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
75 TaskPaneList::TaskPaneList()
79 TaskPaneList::~TaskPaneList()
83 void TaskPaneList::AddWindow( vcl::Window *pWindow )
85 if( !pWindow )
86 return;
88 auto insertionPos = dynamic_cast<MenuBarWindow*>(pWindow) ? mTaskPanes.begin() : mTaskPanes.end();
89 for ( auto p = mTaskPanes.begin(); p != mTaskPanes.end(); ++p )
91 if ( *p == pWindow )
92 // avoid duplicates
93 return;
95 // If the new window is the child of an existing pane window, or vice versa,
96 // ensure that in our pane list, *first* the child window appears, *then*
97 // the ancestor window.
98 // This is necessary for HandleKeyEvent: There, the list is traveled from the
99 // beginning, until the first window is found which has the ChildPathFocus. Now
100 // if this would be the ancestor window of another pane window, this would fudge
101 // the result
102 if ( pWindow->IsWindowOrChild( *p ) )
104 insertionPos = p + 1;
105 break;
107 if ( (*p)->IsWindowOrChild( pWindow ) )
109 insertionPos = p;
110 break;
114 mTaskPanes.insert( insertionPos, pWindow );
115 pWindow->ImplIsInTaskPaneList( true );
118 void TaskPaneList::RemoveWindow( vcl::Window *pWindow )
120 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
121 if( p != mTaskPanes.end() )
123 mTaskPanes.erase( p );
124 pWindow->ImplIsInTaskPaneList( false );
128 bool TaskPaneList::IsInList( vcl::Window *pWindow )
130 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
131 return p != mTaskPanes.end();
134 bool TaskPaneList::IsCycleKey(const vcl::KeyCode& rKeyCode)
136 return rKeyCode.GetCode() == KEY_F6 && !rKeyCode.IsMod2(); // F6
139 bool TaskPaneList::HandleKeyEvent(const KeyEvent& rKeyEvent)
142 // F6 cycles through everything and works always
144 // MAV, #i104204#
145 // The old design was the following one:
146 // < Ctrl-TAB cycles through Menubar, Toolbars and Floatingwindows only and is
147 // < only active if one of those items has the focus
149 // Since the design of Ctrl-Tab looks to be inconsistent ( non-modal dialogs are not reachable
150 // and the shortcut conflicts with tab-control shortcut ), it is no more supported
151 vcl::KeyCode aKeyCode = rKeyEvent.GetKeyCode();
152 bool bForward = !aKeyCode.IsShift();
153 if (TaskPaneList::IsCycleKey(aKeyCode))
155 bool bSplitterOnly = aKeyCode.IsMod1() && aKeyCode.IsShift();
157 // is the focus in the list ?
158 auto p = std::find_if(mTaskPanes.begin(), mTaskPanes.end(),
159 [](const VclPtr<vcl::Window>& rWinPtr) { return rWinPtr->HasChildPathFocus( true ); });
160 if( p != mTaskPanes.end() )
162 vcl::Window *pWin = p->get();
164 // Ctrl-F6 goes directly to the document
165 if( !pWin->IsDialog() && aKeyCode.IsMod1() && !aKeyCode.IsShift() )
167 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 );
168 return true;
171 // activate next task pane
172 vcl::Window *pNextWin = nullptr;
174 if( bSplitterOnly )
175 pNextWin = FindNextSplitter( *p );
176 else
177 pNextWin = FindNextFloat( *p, bForward );
179 if( pNextWin != pWin )
181 ImplGetSVData()->mpWinData->mbNoSaveFocus = true;
182 ImplTaskPaneListGrabFocus( pNextWin, bForward );
183 ImplGetSVData()->mpWinData->mbNoSaveFocus = false;
185 else
187 // forward key if no splitter found
188 if( bSplitterOnly )
189 return false;
191 // we did not find another taskpane, so
192 // put focus back into document
193 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
196 return true;
199 // the focus is not in the list: activate first float if F6 was pressed
200 vcl::Window *pWin;
201 if( bSplitterOnly )
202 pWin = FindNextSplitter( nullptr );
203 else
204 pWin = FindNextFloat( nullptr, bForward );
205 if( pWin )
207 ImplTaskPaneListGrabFocus( pWin, bForward );
208 return true;
212 return false;
215 // returns next splitter
216 vcl::Window* TaskPaneList::FindNextSplitter( vcl::Window *pWindow )
218 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
220 auto p = mTaskPanes.begin();
221 if( pWindow )
222 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
224 if( p != mTaskPanes.end() )
226 unsigned n = mTaskPanes.size();
227 while( --n )
229 if( pWindow ) // increment before test
230 ++p;
231 if( p == mTaskPanes.end() )
232 p = mTaskPanes.begin();
233 if( (*p)->ImplIsSplitter() && (*p)->IsReallyVisible() && !(*p)->IsDialog() && (*p)->GetParent()->HasChildPathFocus() )
235 pWindow = (*p).get();
236 break;
238 if( !pWindow ) // increment after test, otherwise first element is skipped
239 ++p;
243 return pWindow;
246 // returns first valid item (regardless of type) if pWindow==0, otherwise returns next valid float
247 vcl::Window* TaskPaneList::FindNextFloat( vcl::Window *pWindow, bool bForward )
249 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
251 if ( !bForward )
252 ::std::reverse( mTaskPanes.begin(), mTaskPanes.end() );
254 auto p = mTaskPanes.begin();
255 if( pWindow )
256 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
258 while( p != mTaskPanes.end() )
260 if( pWindow ) // increment before test
261 ++p;
262 if( p == mTaskPanes.end() )
263 break; // do not wrap, send focus back to document at end of list
264 /* #i83908# do not use the menubar if it is native and invisible
267 bool bSkip = false; // used to skip infobar when it has no children
268 if( (*p)->GetType() == WindowType::WINDOW && (*p)->GetChildCount() == 0 )
269 bSkip = true;
271 if( !bSkip && (*p)->IsReallyVisible() && !(*p)->ImplIsSplitter() &&
272 ( (*p)->GetType() != WindowType::MENUBARWINDOW || static_cast<MenuBarWindow*>(p->get())->CanGetFocus() ) )
274 pWindow = (*p).get();
275 break;
277 if( !pWindow ) // increment after test, otherwise first element is skipped
278 ++p;
281 if ( !bForward )
282 ::std::reverse( mTaskPanes.begin(), mTaskPanes.end() );
284 return pWindow;
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */