bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / window / taskpanelist.cxx
blob35828b7d14290a564a07f73db07157561615c4bd
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() );
65 static void ImplTaskPaneListGrabFocus( vcl::Window *pWindow, bool bForward )
67 // put focus in child of floating windows which is typically a toolbar
68 // that can deal with the focus
69 if( pWindow->ImplIsFloatingWindow() && pWindow->GetWindow( GetWindowType::FirstChild ) )
70 pWindow = pWindow->GetWindow( GetWindowType::FirstChild );
71 pWindow->ImplGrabFocus( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
74 TaskPaneList::TaskPaneList()
78 TaskPaneList::~TaskPaneList()
82 void TaskPaneList::AddWindow( vcl::Window *pWindow )
84 if( pWindow )
86 auto insertionPos = dynamic_cast<MenuBarWindow*>(pWindow) ? mTaskPanes.begin() : mTaskPanes.end();
87 for ( auto p = mTaskPanes.begin(); p != mTaskPanes.end(); ++p )
89 if ( *p == pWindow )
90 // avoid duplicates
91 return;
93 // If the new window is the child of an existing pane window, or vice versa,
94 // ensure that in our pane list, *first* the child window appears, *then*
95 // the ancestor window.
96 // This is necessary for HandleKeyEvent: There, the list is traveled from the
97 // beginning, until the first window is found which has the ChildPathFocus. Now
98 // if this would be the ancestor window of another pane window, this would fudge
99 // the result
100 if ( pWindow->IsWindowOrChild( *p ) )
102 insertionPos = p + 1;
103 break;
105 if ( (*p)->IsWindowOrChild( pWindow ) )
107 insertionPos = p;
108 break;
112 mTaskPanes.insert( insertionPos, pWindow );
113 pWindow->ImplIsInTaskPaneList( true );
117 void TaskPaneList::RemoveWindow( vcl::Window *pWindow )
119 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
120 if( p != mTaskPanes.end() )
122 mTaskPanes.erase( p );
123 pWindow->ImplIsInTaskPaneList( false );
127 bool TaskPaneList::IsInList( vcl::Window *pWindow )
129 auto p = ::std::find( mTaskPanes.begin(), mTaskPanes.end(), VclPtr<vcl::Window>(pWindow) );
130 return p != mTaskPanes.end();
133 bool TaskPaneList::IsCycleKey(const vcl::KeyCode& rKeyCode)
135 return rKeyCode.GetCode() == KEY_F6 && !rKeyCode.IsMod2(); // F6
138 bool TaskPaneList::HandleKeyEvent(const KeyEvent& rKeyEvent)
141 // F6 cycles through everything and works always
143 // MAV, #i104204#
144 // The old design was the following one:
145 // < Ctrl-TAB cycles through Menubar, Toolbars and Floatingwindows only and is
146 // < only active if one of those items has the focus
148 // Since the design of Ctrl-Tab looks to be inconsistent ( non-modal dialogs are not reachable
149 // and the shortcut conflicts with tab-control shortcut ), it is no more supported
150 vcl::KeyCode aKeyCode = rKeyEvent.GetKeyCode();
151 bool bForward = !aKeyCode.IsShift();
152 if (TaskPaneList::IsCycleKey(aKeyCode))
154 bool bSplitterOnly = aKeyCode.IsMod1() && aKeyCode.IsShift();
156 // is the focus in the list ?
157 auto p = std::find_if(mTaskPanes.begin(), mTaskPanes.end(),
158 [](const VclPtr<vcl::Window>& rWinPtr) { return rWinPtr->HasChildPathFocus( true ); });
159 if( p != mTaskPanes.end() )
161 vcl::Window *pWin = p->get();
163 // Ctrl-F6 goes directly to the document
164 if( !pWin->IsDialog() && aKeyCode.IsMod1() && !aKeyCode.IsShift() )
166 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 );
167 return true;
170 // activate next task pane
171 vcl::Window *pNextWin = nullptr;
173 if( bSplitterOnly )
174 pNextWin = FindNextSplitter( *p );
175 else
176 pNextWin = FindNextFloat( *p, bForward );
178 if( pNextWin != pWin )
180 ImplGetSVData()->maWinData.mbNoSaveFocus = true;
181 ImplTaskPaneListGrabFocus( pNextWin, bForward );
182 ImplGetSVData()->maWinData.mbNoSaveFocus = false;
184 else
186 // forward key if no splitter found
187 if( bSplitterOnly )
188 return false;
190 // we did not find another taskpane, so
191 // put focus back into document
192 pWin->ImplGrabFocusToDocument( GetFocusFlags::F6 | (bForward ? GetFocusFlags::Forward : GetFocusFlags::Backward));
195 return true;
198 // the focus is not in the list: activate first float if F6 was pressed
199 vcl::Window *pWin;
200 if( bSplitterOnly )
201 pWin = FindNextSplitter( nullptr );
202 else
203 pWin = FindNextFloat( nullptr, bForward );
204 if( pWin )
206 ImplTaskPaneListGrabFocus( pWin, bForward );
207 return true;
211 return false;
214 // returns next splitter
215 vcl::Window* TaskPaneList::FindNextSplitter( vcl::Window *pWindow )
217 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
219 auto p = mTaskPanes.begin();
220 if( pWindow )
221 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
223 if( p != mTaskPanes.end() )
225 unsigned n = mTaskPanes.size();
226 while( --n )
228 if( pWindow ) // increment before test
229 ++p;
230 if( p == mTaskPanes.end() )
231 p = mTaskPanes.begin();
232 if( (*p)->ImplIsSplitter() && (*p)->IsReallyVisible() && !(*p)->IsDialog() && (*p)->GetParent()->HasChildPathFocus() )
234 pWindow = (*p).get();
235 break;
237 if( !pWindow ) // increment after test, otherwise first element is skipped
238 ++p;
242 return pWindow;
245 // returns first valid item (regardless of type) if pWindow==0, otherwise returns next valid float
246 vcl::Window* TaskPaneList::FindNextFloat( vcl::Window *pWindow, bool bForward )
248 ::std::stable_sort( mTaskPanes.begin(), mTaskPanes.end(), LTRSort() );
250 if ( !bForward )
251 ::std::reverse( mTaskPanes.begin(), mTaskPanes.end() );
253 auto p = mTaskPanes.begin();
254 if( pWindow )
255 p = std::find(mTaskPanes.begin(), mTaskPanes.end(), pWindow);
257 while( p != mTaskPanes.end() )
259 if( pWindow ) // increment before test
260 ++p;
261 if( p == mTaskPanes.end() )
262 break; // do not wrap, send focus back to document at end of list
263 /* #i83908# do not use the menubar if it is native and invisible
265 if( (*p)->IsReallyVisible() && !(*p)->ImplIsSplitter() &&
266 ( (*p)->GetType() != WindowType::MENUBARWINDOW || static_cast<MenuBarWindow*>(p->get())->CanGetFocus() ) )
268 pWindow = (*p).get();
269 break;
271 if( !pWindow ) // increment after test, otherwise first element is skipped
272 ++p;
275 if ( !bForward )
276 ::std::reverse( mTaskPanes.begin(), mTaskPanes.end() );
278 return pWindow;
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */