lok: avoid expensive fetching of a property.
[LibreOffice.git] / framework / source / layoutmanager / helpers.cxx
blobf5fe1fc48f446d997ae645d12e967e2bd594a477
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 "helpers.hxx"
22 #include <com/sun/star/ui/DockingArea.hpp>
23 #include <com/sun/star/awt/Toolkit.hpp>
24 #include <com/sun/star/awt/XTopWindow.hpp>
25 #include <com/sun/star/frame/DispatchHelper.hpp>
26 #include <com/sun/star/awt/XDockableWindow.hpp>
27 #include <com/sun/star/awt/XDockableWindowListener.hpp>
28 #include <com/sun/star/awt/XWindowListener.hpp>
29 #include <com/sun/star/ui/XUIElement.hpp>
31 #include <comphelper/lok.hxx>
32 #include <unotools/mediadescriptor.hxx>
33 #include <vcl/svapp.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
36 using namespace com::sun::star;
38 namespace framework
41 bool hasEmptySize( const css::awt::Size& rSize )
43 return ( rSize.Width == 0 ) && ( rSize.Height == 0 );
46 bool hasDefaultPosValue( const css::awt::Point& rPos )
48 return (( rPos.X == SAL_MAX_INT32 ) || ( rPos.Y == SAL_MAX_INT32 ));
51 bool isDefaultPos( const css::awt::Point& rPos )
53 return (( rPos.X == SAL_MAX_INT32 ) && ( rPos.Y == SAL_MAX_INT32 ));
56 bool isReverseOrderDockingArea( const sal_Int32 nDockArea )
58 ui::DockingArea eDockArea = static_cast< ui::DockingArea >( nDockArea );
59 return (( eDockArea == ui::DockingArea_DOCKINGAREA_BOTTOM ) ||
60 ( eDockArea == ui::DockingArea_DOCKINGAREA_RIGHT ));
63 bool isToolboxHorizontalAligned( ToolBox const * pToolBox )
65 if ( pToolBox )
66 return (( pToolBox->GetAlign() == WindowAlign::Top ) || ( pToolBox->GetAlign() == WindowAlign::Bottom ));
67 return false;
70 bool isHorizontalDockingArea( const ui::DockingArea& nDockingArea )
72 return (( nDockingArea == ui::DockingArea_DOCKINGAREA_TOP ) ||
73 ( nDockingArea == ui::DockingArea_DOCKINGAREA_BOTTOM ));
76 bool isHorizontalDockingArea( const sal_Int32 nDockArea )
78 return isHorizontalDockingArea(static_cast< ui::DockingArea >( nDockArea ));
81 OUString retrieveToolbarNameFromHelpURL( vcl::Window* pWindow )
83 OUString aToolbarName;
85 if ( pWindow->GetType() == WindowType::TOOLBOX )
87 ToolBox* pToolBox = dynamic_cast<ToolBox *>( pWindow );
88 if ( pToolBox )
90 aToolbarName = OStringToOUString( pToolBox->GetHelpId(), RTL_TEXTENCODING_UTF8 );
91 sal_Int32 i = aToolbarName.lastIndexOf( ':' );
92 if ( !aToolbarName.isEmpty() && ( i > 0 ) && (( i + 1 ) < aToolbarName.getLength() ))
93 aToolbarName = aToolbarName.copy( i+1 ); // Remove ".HelpId:" protocol from toolbar name
94 else
95 aToolbarName.clear();
98 return aToolbarName;
101 ToolBox* getToolboxPtr( vcl::Window* pWindow )
103 ToolBox* pToolbox(nullptr);
104 if ( pWindow->GetType() == WindowType::TOOLBOX )
105 pToolbox = dynamic_cast<ToolBox*>( pWindow );
106 return pToolbox;
109 vcl::Window* getWindowFromXUIElement( const uno::Reference< ui::XUIElement >& xUIElement )
111 SolarMutexGuard aGuard;
112 uno::Reference< awt::XWindow > xWindow;
113 if ( xUIElement.is() )
114 xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
115 return VCLUnoHelper::GetWindow( xWindow );
118 SystemWindow* getTopSystemWindow( const uno::Reference< awt::XWindow >& xWindow )
120 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
121 while ( pWindow && !pWindow->IsSystemWindow() )
122 pWindow = pWindow->GetParent();
124 if ( pWindow )
125 return static_cast<SystemWindow *>(pWindow.get());
126 else
127 return nullptr;
130 void setZeroRectangle( ::tools::Rectangle& rRect )
132 rRect.setX(0);
133 rRect.setY(0);
134 rRect.setWidth(0);
135 rRect.setHeight(0);
138 // ATTENTION!
139 // This value is directly copied from the sfx2 project.
140 // You have to change BOTH values, see sfx2/inc/sfx2/sfxsids.hrc (SID_DOCKWIN_START)
141 const sal_Int32 DOCKWIN_ID_BASE = 9800;
143 bool lcl_checkUIElement(const uno::Reference< ui::XUIElement >& xUIElement, awt::Rectangle& _rPosSize, uno::Reference< awt::XWindow >& _xWindow)
145 bool bRet = xUIElement.is();
146 if ( bRet )
148 SolarMutexGuard aGuard;
149 _xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
150 _rPosSize = _xWindow->getPosSize();
152 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( _xWindow );
153 if ( pWindow->GetType() == WindowType::TOOLBOX )
155 ::Size aSize = static_cast<ToolBox*>(pWindow.get())->CalcWindowSizePixel( 1 );
156 _rPosSize.Width = aSize.Width();
157 _rPosSize.Height = aSize.Height();
159 } // if ( xUIElement.is() )
160 return bRet;
163 uno::Reference< awt::XWindowPeer > createToolkitWindow( const uno::Reference< uno::XComponentContext >& rxContext, const uno::Reference< awt::XWindowPeer >& rParent, const char* pService )
165 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create( rxContext );
167 // describe window properties.
168 css::awt::WindowDescriptor aDescriptor;
169 aDescriptor.Type = awt::WindowClass_SIMPLE;
170 aDescriptor.WindowServiceName = OUString::createFromAscii( pService );
171 aDescriptor.ParentIndex = -1;
172 aDescriptor.Parent = rParent;
173 aDescriptor.Bounds = awt::Rectangle(0,0,0,0);
174 aDescriptor.WindowAttributes = 0;
176 // create an awt window
177 uno::Reference< awt::XWindowPeer > xPeer = xToolkit->createWindow( aDescriptor );
179 return xPeer;
182 // convert alignment constant to vcl's WindowAlign type
183 WindowAlign ImplConvertAlignment( ui::DockingArea aAlignment )
185 if ( aAlignment == ui::DockingArea_DOCKINGAREA_LEFT )
186 return WindowAlign::Left;
187 else if ( aAlignment == ui::DockingArea_DOCKINGAREA_RIGHT )
188 return WindowAlign::Right;
189 else if ( aAlignment == ui::DockingArea_DOCKINGAREA_TOP )
190 return WindowAlign::Top;
191 else
192 return WindowAlign::Bottom;
195 OUString getElementTypeFromResourceURL( const OUString& aResourceURL )
197 OUString aUIResourceURL( UIRESOURCE_URL );
198 if ( aResourceURL.startsWith( aUIResourceURL ) )
200 sal_Int32 nIndex{ aUIResourceURL.getLength() };
201 return aResourceURL.getToken( 1, '/', nIndex );
204 return OUString();
207 void parseResourceURL( const OUString& aResourceURL, OUString& aElementType, OUString& aElementName )
209 OUString aUIResourceURL( UIRESOURCE_URL );
210 if ( aResourceURL.startsWith( aUIResourceURL ) )
212 sal_Int32 nIndex{ aUIResourceURL.getLength() };
213 aElementType = aResourceURL.getToken( 1, '/', nIndex );
214 aElementName = aResourceURL.getToken( 0, '/', nIndex );
218 css::awt::Rectangle putRectangleValueToAWT( const ::tools::Rectangle& rRect )
220 css::awt::Rectangle aRect;
221 aRect.X = rRect.Left();
222 aRect.Y = rRect.Top();
223 aRect.Width = rRect.Right();
224 aRect.Height = rRect.Bottom();
226 return aRect;
229 ::tools::Rectangle putAWTToRectangle( const css::awt::Rectangle& rRect )
231 ::tools::Rectangle aRect;
232 aRect.SetLeft( rRect.X );
233 aRect.SetTop( rRect.Y );
234 aRect.SetRight( rRect.Width );
235 aRect.SetBottom( rRect.Height );
237 return aRect;
240 bool equalRectangles( const css::awt::Rectangle& rRect1,
241 const css::awt::Rectangle& rRect2 )
243 return (( rRect1.X == rRect2.X ) &&
244 ( rRect1.Y == rRect2.Y ) &&
245 ( rRect1.Width == rRect2.Width ) &&
246 ( rRect1.Height == rRect2.Height ));
249 uno::Reference< frame::XModel > impl_getModelFromFrame( const uno::Reference< frame::XFrame >& rFrame )
251 // Query for the model to get check the context information
252 uno::Reference< frame::XModel > xModel;
253 if ( rFrame.is() )
255 uno::Reference< frame::XController > xController = rFrame->getController();
256 if ( xController.is() )
257 xModel = xController->getModel();
260 return xModel;
263 bool implts_isPreviewModel( const uno::Reference< frame::XModel >& xModel )
265 // the cost in calc of calling getArgs for this property
266 // includes measuring the entire sheet - which is extremely slow.
267 if (comphelper::LibreOfficeKit::isActive())
268 return false;
270 if ( xModel.is() )
272 utl::MediaDescriptor aDesc( xModel->getArgs() );
273 return aDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_PREVIEW(), false);
275 else
276 return false;
279 bool implts_isFrameOrWindowTop( const uno::Reference< frame::XFrame >& xFrame )
281 if (xFrame->isTop())
282 return true;
284 uno::Reference< awt::XTopWindow > xWindowCheck(xFrame->getContainerWindow(), uno::UNO_QUERY); // don't use _THROW here ... it's a check only
285 if (xWindowCheck.is())
287 // #i76867# top and system window is required.
288 SolarMutexGuard aGuard;
289 uno::Reference< awt::XWindow > xWindow( xWindowCheck, uno::UNO_QUERY );
290 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
291 return pWindow && pWindow->IsSystemWindow();
294 return false;
297 void impl_setDockingWindowVisibility( const css::uno::Reference< css::uno::XComponentContext>& rxContext, const css::uno::Reference< css::frame::XFrame >& rFrame, const OUString& rDockingWindowName, bool bVisible )
299 sal_Int32 nID = rDockingWindowName.toInt32();
300 sal_Int32 nIndex = nID - DOCKWIN_ID_BASE;
302 css::uno::Reference< css::frame::XDispatchProvider > xProvider(rFrame, css::uno::UNO_QUERY);
303 if ( !(nIndex >= 0 && xProvider.is()) )
304 return;
306 OUString aDockWinArgName = "DockingWindow" + OUString::number( nIndex );
308 css::uno::Sequence< css::beans::PropertyValue > aArgs(1);
309 aArgs[0].Name = aDockWinArgName;
310 aArgs[0].Value <<= bVisible;
312 css::uno::Reference< css::frame::XDispatchHelper > xDispatcher = css::frame::DispatchHelper::create( rxContext );
314 OUString aDockWinCommand = ".uno:" + aDockWinArgName;
315 xDispatcher->executeDispatch(
316 xProvider,
317 aDockWinCommand,
318 "_self",
320 aArgs);
323 void impl_addWindowListeners(
324 const css::uno::Reference< css::uno::XInterface >& xThis,
325 const css::uno::Reference< css::ui::XUIElement >& xUIElement )
327 css::uno::Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
328 css::uno::Reference< css::awt::XDockableWindow > xDockWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
329 if ( !(xDockWindow.is() && xWindow.is()) )
330 return;
334 xDockWindow->addDockableWindowListener(
335 css::uno::Reference< css::awt::XDockableWindowListener >(
336 xThis, css::uno::UNO_QUERY ));
337 xWindow->addWindowListener(
338 css::uno::Reference< css::awt::XWindowListener >(
339 xThis, css::uno::UNO_QUERY ));
340 xDockWindow->enableDocking( true );
342 catch ( const css::uno::Exception& )
347 } // namespace framework
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */