LibreOffice Module framework (master) 1
helpers.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "helpers.hxx"
21
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>
30
31#include <comphelper/lok.hxx>
34#include <vcl/svapp.hxx>
35#include <o3tl/string_view.hxx>
37
38using namespace com::sun::star;
39
40namespace framework
41{
42
43bool hasEmptySize( const css::awt::Size& rSize )
44{
45 return ( rSize.Width == 0 ) && ( rSize.Height == 0 );
46}
47
48bool hasDefaultPosValue( const css::awt::Point& rPos )
49{
50 return (( rPos.X == SAL_MAX_INT32 ) || ( rPos.Y == SAL_MAX_INT32 ));
51}
52
53bool isDefaultPos( const css::awt::Point& rPos )
54{
55 return (( rPos.X == SAL_MAX_INT32 ) && ( rPos.Y == SAL_MAX_INT32 ));
56}
57
58bool isReverseOrderDockingArea( const sal_Int32 nDockArea )
59{
60 ui::DockingArea eDockArea = static_cast< ui::DockingArea >( nDockArea );
61 return (( eDockArea == ui::DockingArea_DOCKINGAREA_BOTTOM ) ||
62 ( eDockArea == ui::DockingArea_DOCKINGAREA_RIGHT ));
63}
64
65bool isToolboxHorizontalAligned( ToolBox const * pToolBox )
66{
67 if ( pToolBox )
68 return (( pToolBox->GetAlign() == WindowAlign::Top ) || ( pToolBox->GetAlign() == WindowAlign::Bottom ));
69 return false;
70}
71
72bool isHorizontalDockingArea( const ui::DockingArea& nDockingArea )
73{
74 return (( nDockingArea == ui::DockingArea_DOCKINGAREA_TOP ) ||
75 ( nDockingArea == ui::DockingArea_DOCKINGAREA_BOTTOM ));
76}
77
78bool isHorizontalDockingArea( const sal_Int32 nDockArea )
79{
80 return isHorizontalDockingArea(static_cast< ui::DockingArea >( nDockArea ));
81}
82
84{
85 OUString aToolbarName;
86
87 if ( pWindow->GetType() == WindowType::TOOLBOX )
88 {
89 ToolBox* pToolBox = dynamic_cast<ToolBox *>( pWindow );
90 if ( pToolBox )
91 {
92 aToolbarName = pToolBox->GetHelpId();
93 sal_Int32 i = aToolbarName.lastIndexOf( ':' );
94 if ( !aToolbarName.isEmpty() && ( i > 0 ) && (( i + 1 ) < aToolbarName.getLength() ))
95 aToolbarName = aToolbarName.copy( i+1 ); // Remove ".HelpId:" protocol from toolbar name
96 else
97 aToolbarName.clear();
98 }
99 }
100 return aToolbarName;
101}
102
104{
105 ToolBox* pToolbox(nullptr);
106 if ( pWindow->GetType() == WindowType::TOOLBOX )
107 pToolbox = dynamic_cast<ToolBox*>( pWindow );
108 return pToolbox;
109}
110
111vcl::Window* getWindowFromXUIElement( const uno::Reference< ui::XUIElement >& xUIElement )
112{
113 SolarMutexGuard aGuard;
114 uno::Reference< awt::XWindow > xWindow;
115 if ( xUIElement.is() )
116 xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
117 return VCLUnoHelper::GetWindow( xWindow );
118}
119
120SystemWindow* getTopSystemWindow( const uno::Reference< awt::XWindow >& xWindow )
121{
122 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
123 while ( pWindow && !pWindow->IsSystemWindow() )
124 pWindow = pWindow->GetParent();
125
126 if ( pWindow )
127 return static_cast<SystemWindow *>(pWindow.get());
128 else
129 return nullptr;
130}
131
132// ATTENTION!
133// This value is directly copied from the sfx2 project.
134// You have to change BOTH values, see sfx2/inc/sfx2/sfxsids.hrc (SID_DOCKWIN_START)
135const sal_Int32 DOCKWIN_ID_BASE = 9800;
136
137bool lcl_checkUIElement(const uno::Reference< ui::XUIElement >& xUIElement, awt::Rectangle& _rPosSize, uno::Reference< awt::XWindow >& _xWindow)
138{
139 bool bRet = xUIElement.is();
140 if ( bRet )
141 {
142 SolarMutexGuard aGuard;
143 _xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
144 _rPosSize = _xWindow->getPosSize();
145
146 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( _xWindow );
147 if ( pWindow->GetType() == WindowType::TOOLBOX )
148 {
149 ::Size aSize = static_cast<ToolBox*>(pWindow.get())->CalcWindowSizePixel( 1 );
150 _rPosSize.Width = aSize.Width();
151 _rPosSize.Height = aSize.Height();
152 }
153 } // if ( xUIElement.is() )
154 return bRet;
155}
156
157uno::Reference< awt::XVclWindowPeer > createToolkitWindow( const uno::Reference< uno::XComponentContext >& rxContext, const uno::Reference< awt::XVclWindowPeer >& rParent, const char* pService )
158{
159 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create( rxContext );
160
161 // describe window properties.
162 css::awt::WindowDescriptor aDescriptor;
163 aDescriptor.Type = awt::WindowClass_SIMPLE;
164 aDescriptor.WindowServiceName = OUString::createFromAscii( pService );
165 aDescriptor.ParentIndex = -1;
166 aDescriptor.Parent = rParent;
167 aDescriptor.Bounds = awt::Rectangle(0,0,0,0);
168 aDescriptor.WindowAttributes = 0;
169
170 // create an awt window
171 uno::Reference< awt::XWindowPeer > xPeer = xToolkit->createWindow( aDescriptor );
172 uno::Reference< awt::XVclWindowPeer > xVclPeer(xPeer, uno::UNO_QUERY);
173 assert(xVclPeer || !xPeer);
174 return xVclPeer;
175}
176
177// convert alignment constant to vcl's WindowAlign type
178WindowAlign ImplConvertAlignment( ui::DockingArea aAlignment )
179{
180 if ( aAlignment == ui::DockingArea_DOCKINGAREA_LEFT )
181 return WindowAlign::Left;
182 else if ( aAlignment == ui::DockingArea_DOCKINGAREA_RIGHT )
183 return WindowAlign::Right;
184 else if ( aAlignment == ui::DockingArea_DOCKINGAREA_TOP )
185 return WindowAlign::Top;
186 else
187 return WindowAlign::Bottom;
188}
189
190std::u16string_view getElementTypeFromResourceURL( std::u16string_view aResourceURL )
191{
192 if ( o3tl::starts_with(aResourceURL, UIRESOURCE_URL ) )
193 {
194 sal_Int32 nIndex{ UIRESOURCE_URL.getLength() };
195 return o3tl::getToken(aResourceURL, 1, '/', nIndex );
196 }
197
198 return std::u16string_view();
199}
200
201void parseResourceURL( std::u16string_view aResourceURL, OUString& aElementType, OUString& aElementName )
202{
203 if ( o3tl::starts_with(aResourceURL, UIRESOURCE_URL) )
204 {
205 sal_Int32 nIndex{ UIRESOURCE_URL.getLength() };
206 aElementType = o3tl::getToken(aResourceURL, 1, '/', nIndex );
207 aElementName = o3tl::getToken(aResourceURL, 0, '/', nIndex );
208 }
209}
210
211css::awt::Rectangle putRectangleValueToAWT( const ::tools::Rectangle& rRect )
212{
213 css::awt::Rectangle aRect;
214 aRect.X = rRect.Left();
215 aRect.Y = rRect.Top();
216 aRect.Width = rRect.Right();
217 aRect.Height = rRect.Bottom();
218
219 return aRect;
220}
221
222::tools::Rectangle putAWTToRectangle( const css::awt::Rectangle& rRect )
223{
224 ::tools::Rectangle aRect;
225 aRect.SetLeft( rRect.X );
226 aRect.SetTop( rRect.Y );
227 aRect.SetRight( rRect.Width );
228 aRect.SetBottom( rRect.Height );
229
230 return aRect;
231}
232
233bool equalRectangles( const css::awt::Rectangle& rRect1,
234 const css::awt::Rectangle& rRect2 )
235{
236 return (( rRect1.X == rRect2.X ) &&
237 ( rRect1.Y == rRect2.Y ) &&
238 ( rRect1.Width == rRect2.Width ) &&
239 ( rRect1.Height == rRect2.Height ));
240}
241
242uno::Reference< frame::XModel > impl_getModelFromFrame( const uno::Reference< frame::XFrame >& rFrame )
243{
244 // Query for the model to get check the context information
245 uno::Reference< frame::XModel > xModel;
246 if ( rFrame.is() )
247 {
248 uno::Reference< frame::XController > xController = rFrame->getController();
249 if ( xController.is() )
250 xModel = xController->getModel();
251 }
252
253 return xModel;
254}
255
256bool implts_isPreviewModel( const uno::Reference< frame::XModel >& xModel )
257{
258 // the cost in calc of calling getArgs for this property
259 // includes measuring the entire sheet - which is extremely slow.
261 return false;
262
263 if ( xModel.is() )
264 {
265 utl::MediaDescriptor aDesc( xModel->getArgs() );
266 return aDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_PREVIEW, false);
267 }
268 else
269 return false;
270}
271
272bool implts_isFrameOrWindowTop( const uno::Reference< frame::XFrame >& xFrame )
273{
274 if (xFrame->isTop())
275 return true;
276
277 uno::Reference< awt::XTopWindow > xWindowCheck(xFrame->getContainerWindow(), uno::UNO_QUERY); // don't use _THROW here ... it's a check only
278 if (xWindowCheck.is())
279 {
280 // #i76867# top and system window is required.
281 SolarMutexGuard aGuard;
282 uno::Reference< awt::XWindow > xWindow( xWindowCheck, uno::UNO_QUERY );
283 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
284 return pWindow && pWindow->IsSystemWindow();
285 }
286
287 return false;
288}
289
290void impl_setDockingWindowVisibility( const css::uno::Reference< css::uno::XComponentContext>& rxContext, const css::uno::Reference< css::frame::XFrame >& rFrame, std::u16string_view rDockingWindowName, bool bVisible )
291{
292 sal_Int32 nID = o3tl::toInt32(rDockingWindowName);
293 sal_Int32 nIndex = nID - DOCKWIN_ID_BASE;
294
295 css::uno::Reference< css::frame::XDispatchProvider > xProvider(rFrame, css::uno::UNO_QUERY);
296 if ( !(nIndex >= 0 && xProvider.is()) )
297 return;
298
299 OUString aDockWinArgName = "DockingWindow" + OUString::number( nIndex );
300
301 css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
302 aDockWinArgName, bVisible) };
303
304 css::uno::Reference< css::frame::XDispatchHelper > xDispatcher = css::frame::DispatchHelper::create( rxContext );
305
306 OUString aDockWinCommand = ".uno:" + aDockWinArgName;
307 xDispatcher->executeDispatch(
308 xProvider,
309 aDockWinCommand,
310 "_self",
311 0,
312 aArgs);
313}
314
316 const css::uno::Reference< css::uno::XInterface >& xThis,
317 const css::uno::Reference< css::ui::XUIElement >& xUIElement )
318{
319 css::uno::Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
320 css::uno::Reference< css::awt::XDockableWindow > xDockWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
321 if ( !(xDockWindow.is() && xWindow.is()) )
322 return;
323
324 try
325 {
326 xDockWindow->addDockableWindowListener(
327 css::uno::Reference< css::awt::XDockableWindowListener >(
328 xThis, css::uno::UNO_QUERY ));
329 xWindow->addWindowListener(
330 css::uno::Reference< css::awt::XWindowListener >(
331 xThis, css::uno::UNO_QUERY ));
332 xDockWindow->enableDocking( true );
333 }
334 catch ( const css::uno::Exception& )
335 {
336 }
337}
338
339} // namespace framework
340
341/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long Height() const
constexpr tools::Long Width() const
WindowAlign GetAlign() const
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
reference_type * get() const
constexpr void SetLeft(tools::Long v)
constexpr void SetTop(tools::Long v)
constexpr void SetRight(tools::Long v)
constexpr void SetBottom(tools::Long v)
static constexpr OUStringLiteral PROP_PREVIEW
WindowType GetType() const
const OUString & GetHelpId() const
constexpr OUStringLiteral UIRESOURCE_URL
Definition: helpers.hxx:33
sal_Int32 nIndex
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::awt::Rectangle putRectangleValueToAWT(const ::tools::Rectangle &rRect)
Definition: helpers.cxx:211
const sal_Int32 DOCKWIN_ID_BASE
Definition: helpers.cxx:135
WindowAlign ImplConvertAlignment(ui::DockingArea aAlignment)
Definition: helpers.cxx:178
void parseResourceURL(std::u16string_view aResourceURL, OUString &aElementType, OUString &aElementName)
Definition: helpers.cxx:201
OUString retrieveToolbarNameFromHelpURL(vcl::Window *pWindow)
Definition: helpers.cxx:83
bool isHorizontalDockingArea(const ui::DockingArea &nDockingArea)
Definition: helpers.cxx:72
ToolBox * getToolboxPtr(vcl::Window *pWindow)
Definition: helpers.cxx:103
uno::Reference< awt::XVclWindowPeer > createToolkitWindow(const uno::Reference< uno::XComponentContext > &rxContext, const uno::Reference< awt::XVclWindowPeer > &rParent, const char *pService)
Definition: helpers.cxx:157
void impl_addWindowListeners(const css::uno::Reference< css::uno::XInterface > &xThis, const css::uno::Reference< css::ui::XUIElement > &xUIElement)
Definition: helpers.cxx:315
::tools::Rectangle putAWTToRectangle(const css::awt::Rectangle &rRect)
Definition: helpers.cxx:222
bool isToolboxHorizontalAligned(ToolBox const *pToolBox)
Definition: helpers.cxx:65
SystemWindow * getTopSystemWindow(const uno::Reference< awt::XWindow > &xWindow)
Definition: helpers.cxx:120
vcl::Window * getWindowFromXUIElement(const uno::Reference< ui::XUIElement > &xUIElement)
Definition: helpers.cxx:111
uno::Reference< frame::XModel > impl_getModelFromFrame(const uno::Reference< frame::XFrame > &rFrame)
Definition: helpers.cxx:242
void impl_setDockingWindowVisibility(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &rFrame, std::u16string_view rDockingWindowName, bool bVisible)
Definition: helpers.cxx:290
std::u16string_view getElementTypeFromResourceURL(std::u16string_view aResourceURL)
Definition: helpers.cxx:190
bool implts_isPreviewModel(const uno::Reference< frame::XModel > &xModel)
Definition: helpers.cxx:256
bool hasEmptySize(const css::awt::Size &rSize)
Definition: helpers.cxx:43
bool implts_isFrameOrWindowTop(const uno::Reference< frame::XFrame > &xFrame)
Definition: helpers.cxx:272
bool equalRectangles(const css::awt::Rectangle &rRect1, const css::awt::Rectangle &rRect2)
Definition: helpers.cxx:233
bool isReverseOrderDockingArea(const sal_Int32 nDockArea)
Definition: helpers.cxx:58
bool lcl_checkUIElement(const uno::Reference< ui::XUIElement > &xUIElement, awt::Rectangle &_rPosSize, uno::Reference< awt::XWindow > &_xWindow)
Definition: helpers.cxx:137
bool isDefaultPos(const css::awt::Point &rPos)
Definition: helpers.cxx:53
bool hasDefaultPosValue(const css::awt::Point &rPos)
Definition: helpers.cxx:48
int i
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
bool bVisible
#define SAL_MAX_INT32
WindowAlign