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