Line data Source code
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 : #include <threadhelp/resetableguard.hxx>
22 : #include <services.h>
23 :
24 : #include <com/sun/star/ui/DockingArea.hpp>
25 : #include <com/sun/star/awt/Toolkit.hpp>
26 : #include <com/sun/star/awt/XTopWindow.hpp>
27 : #include <com/sun/star/frame/DispatchHelper.hpp>
28 : #include <com/sun/star/awt/XDockableWindow.hpp>
29 : #include <com/sun/star/awt/XDockableWindowListener.hpp>
30 : #include <com/sun/star/awt/XWindowListener.hpp>
31 : #include <com/sun/star/ui/XUIElement.hpp>
32 :
33 : #include <comphelper/processfactory.hxx>
34 : #include <comphelper/mediadescriptor.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <toolkit/unohlp.hxx>
37 :
38 : using namespace com::sun::star;
39 :
40 : namespace framework
41 : {
42 :
43 0 : bool hasEmptySize( const ::com::sun::star::awt::Size& rSize )
44 : {
45 0 : return ( rSize.Width == 0 ) && ( rSize.Height == 0 );
46 : }
47 :
48 0 : bool hasDefaultPosValue( const ::com::sun::star::awt::Point& rPos )
49 : {
50 0 : return (( rPos.X == SAL_MAX_INT32 ) || ( rPos.Y == SAL_MAX_INT32 ));
51 : }
52 :
53 731 : bool isDefaultPos( const ::com::sun::star::awt::Point& rPos )
54 : {
55 731 : return (( rPos.X == SAL_MAX_INT32 ) && ( rPos.Y == SAL_MAX_INT32 ));
56 : }
57 :
58 2944 : bool isReverseOrderDockingArea( const sal_Int32 nDockArea )
59 : {
60 2944 : ui::DockingArea eDockArea = static_cast< ui::DockingArea >( nDockArea );
61 : return (( eDockArea == ui::DockingArea_DOCKINGAREA_BOTTOM ) ||
62 2944 : ( eDockArea == ui::DockingArea_DOCKINGAREA_RIGHT ));
63 : }
64 :
65 0 : bool isToolboxHorizontalAligned( ToolBox* pToolBox )
66 : {
67 0 : if ( pToolBox )
68 0 : return (( pToolBox->GetAlign() == WINDOWALIGN_TOP ) || ( pToolBox->GetAlign() == WINDOWALIGN_BOTTOM ));
69 0 : return false;
70 : }
71 :
72 3731 : bool isHorizontalDockingArea( const ui::DockingArea& nDockingArea )
73 : {
74 : return (( nDockingArea == ui::DockingArea_DOCKINGAREA_TOP ) ||
75 3731 : ( nDockingArea == ui::DockingArea_DOCKINGAREA_BOTTOM ));
76 : }
77 :
78 2471 : bool isHorizontalDockingArea( const sal_Int32 nDockArea )
79 : {
80 2471 : return isHorizontalDockingArea(static_cast< ui::DockingArea >( nDockArea ));
81 : }
82 :
83 3588 : ::rtl::OUString retrieveToolbarNameFromHelpURL( Window* pWindow )
84 : {
85 3588 : ::rtl::OUString aToolbarName;
86 :
87 3588 : if ( pWindow->GetType() == WINDOW_TOOLBOX )
88 : {
89 3588 : ToolBox* pToolBox = dynamic_cast<ToolBox *>( pWindow );
90 3588 : if ( pToolBox )
91 : {
92 3588 : aToolbarName = rtl::OStringToOUString( pToolBox->GetHelpId(), RTL_TEXTENCODING_UTF8 );
93 3588 : sal_Int32 i = aToolbarName.lastIndexOf( ':' );
94 3588 : if ( !aToolbarName.isEmpty() && ( i > 0 ) && (( i + 1 ) < aToolbarName.getLength() ))
95 3588 : aToolbarName = aToolbarName.copy( i+1 ); // Remove ".HelpId:" protocol from toolbar name
96 : else
97 0 : aToolbarName = ::rtl::OUString();
98 : }
99 : }
100 3588 : return aToolbarName;
101 : }
102 :
103 3588 : ToolBox* getToolboxPtr( Window* pWindow )
104 : {
105 3588 : ToolBox* pToolbox(NULL);
106 3588 : if ( pWindow->GetType() == WINDOW_TOOLBOX )
107 3588 : pToolbox = dynamic_cast<ToolBox*>( pWindow );
108 3588 : return pToolbox;
109 : }
110 :
111 1102 : Window* getWindowFromXUIElement( const uno::Reference< ui::XUIElement >& xUIElement )
112 : {
113 1102 : SolarMutexGuard aGuard;
114 1102 : uno::Reference< awt::XWindow > xWindow;
115 1102 : if ( xUIElement.is() )
116 1102 : xWindow = uno::Reference< awt::XWindow >( xUIElement->getRealInterface(), uno::UNO_QUERY );
117 1102 : return VCLUnoHelper::GetWindow( xWindow );
118 : }
119 :
120 1555 : SystemWindow* getTopSystemWindow( const uno::Reference< awt::XWindow >& xWindow )
121 : {
122 1555 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
123 3110 : while ( pWindow && !pWindow->IsSystemWindow() )
124 0 : pWindow = pWindow->GetParent();
125 :
126 1555 : if ( pWindow )
127 1555 : return (SystemWindow *)pWindow;
128 : else
129 0 : return 0;
130 : }
131 :
132 1500 : void setZeroRectangle( ::Rectangle& rRect )
133 : {
134 1500 : rRect.setX(0);
135 1500 : rRect.setY(0);
136 1500 : rRect.setWidth(0);
137 1500 : rRect.setHeight(0);
138 1500 : }
139 :
140 : // ATTENTION!
141 : // This value is directly copied from the sfx2 project.
142 : // You have to change BOTH values, see sfx2/inc/sfx2/sfxsids.hrc (SID_DOCKWIN_START)
143 : static const sal_Int32 DOCKWIN_ID_BASE = 9800;
144 :
145 788 : bool lcl_checkUIElement(const uno::Reference< ui::XUIElement >& xUIElement, awt::Rectangle& _rPosSize, uno::Reference< awt::XWindow >& _xWindow)
146 : {
147 788 : bool bRet = xUIElement.is();
148 788 : if ( bRet )
149 : {
150 788 : SolarMutexGuard aGuard;
151 788 : _xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
152 788 : _rPosSize = _xWindow->getPosSize();
153 :
154 788 : Window* pWindow = VCLUnoHelper::GetWindow( _xWindow );
155 788 : if ( pWindow->GetType() == WINDOW_TOOLBOX )
156 : {
157 788 : ::Size aSize = ((ToolBox*)pWindow)->CalcWindowSizePixel( 1 );
158 788 : _rPosSize.Width = aSize.Width();
159 788 : _rPosSize.Height = aSize.Height();
160 788 : }
161 : } // if ( xUIElement.is() )
162 788 : return bRet;
163 : }
164 :
165 1920 : uno::Reference< awt::XWindowPeer > createToolkitWindow( const uno::Reference< uno::XComponentContext >& rxContext, const uno::Reference< awt::XWindowPeer >& rParent, const char* pService )
166 : {
167 1920 : uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create( rxContext );
168 :
169 : // describe window properties.
170 1920 : css::awt::WindowDescriptor aDescriptor;
171 1920 : aDescriptor.Type = awt::WindowClass_SIMPLE;
172 1920 : aDescriptor.WindowServiceName = ::rtl::OUString::createFromAscii( pService );
173 1920 : aDescriptor.ParentIndex = -1;
174 1920 : aDescriptor.Parent = uno::Reference< awt::XWindowPeer >( rParent, uno::UNO_QUERY );
175 1920 : aDescriptor.Bounds = awt::Rectangle(0,0,0,0);
176 1920 : aDescriptor.WindowAttributes = 0;
177 :
178 : // create a awt window
179 1920 : uno::Reference< awt::XWindowPeer > xPeer = xToolkit->createWindow( aDescriptor );
180 :
181 1920 : return xPeer;
182 : }
183 :
184 : // convert alignment constant to vcl's WindowAlign type
185 731 : WindowAlign ImplConvertAlignment( sal_Int16 aAlignment )
186 : {
187 731 : if ( aAlignment == ui::DockingArea_DOCKINGAREA_LEFT )
188 0 : return WINDOWALIGN_LEFT;
189 731 : else if ( aAlignment == ui::DockingArea_DOCKINGAREA_RIGHT )
190 0 : return WINDOWALIGN_RIGHT;
191 731 : else if ( aAlignment == ui::DockingArea_DOCKINGAREA_TOP )
192 710 : return WINDOWALIGN_TOP;
193 : else
194 21 : return WINDOWALIGN_BOTTOM;
195 : }
196 :
197 0 : ::rtl::OUString getElementTypeFromResourceURL( const ::rtl::OUString& aResourceURL )
198 : {
199 0 : ::rtl::OUString aType;
200 :
201 0 : ::rtl::OUString aUIResourceURL( UIRESOURCE_URL );
202 0 : if ( aResourceURL.indexOf( aUIResourceURL ) == 0 )
203 : {
204 0 : sal_Int32 nIndex = 0;
205 0 : ::rtl::OUString aPathPart = aResourceURL.copy( aUIResourceURL.getLength() );
206 0 : aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
207 :
208 0 : return aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
209 : }
210 :
211 0 : return aType;
212 : }
213 :
214 22564 : void parseResourceURL( const rtl::OUString& aResourceURL, rtl::OUString& aElementType, rtl::OUString& aElementName )
215 : {
216 22564 : ::rtl::OUString aUIResourceURL( UIRESOURCE_URL );
217 22564 : if ( aResourceURL.indexOf( aUIResourceURL ) == 0 )
218 : {
219 22564 : sal_Int32 nIndex = 0;
220 22564 : ::rtl::OUString aPathPart = aResourceURL.copy( aUIResourceURL.getLength() );
221 22564 : aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
222 :
223 22564 : aElementType = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
224 22564 : aElementName = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
225 22564 : }
226 22564 : }
227 :
228 1522 : ::com::sun::star::awt::Rectangle putRectangleValueToAWT( const ::Rectangle& rRect )
229 : {
230 1522 : css::awt::Rectangle aRect;
231 1522 : aRect.X = rRect.Left();
232 1522 : aRect.Y = rRect.Top();
233 1522 : aRect.Width = rRect.Right();
234 1522 : aRect.Height = rRect.Bottom();
235 :
236 1522 : return aRect;
237 : }
238 :
239 736 : ::Rectangle putAWTToRectangle( const ::com::sun::star::awt::Rectangle& rRect )
240 : {
241 736 : ::Rectangle aRect;
242 736 : aRect.Left() = rRect.X;
243 736 : aRect.Top() = rRect.Y;
244 736 : aRect.Right() = rRect.Width;
245 736 : aRect.Bottom() = rRect.Height;
246 :
247 736 : return aRect;
248 : }
249 :
250 786 : bool equalRectangles( const css::awt::Rectangle& rRect1,
251 : const css::awt::Rectangle& rRect2 )
252 : {
253 : return (( rRect1.X == rRect2.X ) &&
254 : ( rRect1.Y == rRect2.Y ) &&
255 : ( rRect1.Width == rRect2.Width ) &&
256 786 : ( rRect1.Height == rRect2.Height ));
257 : }
258 :
259 480 : uno::Reference< frame::XModel > impl_getModelFromFrame( const uno::Reference< frame::XFrame >& rFrame )
260 : {
261 : // Query for the model to get check the context information
262 480 : uno::Reference< frame::XModel > xModel;
263 480 : if ( rFrame.is() )
264 : {
265 240 : uno::Reference< frame::XController > xController( rFrame->getController(), uno::UNO_QUERY );
266 240 : if ( xController.is() )
267 240 : xModel = xController->getModel();
268 : }
269 :
270 480 : return xModel;
271 : }
272 :
273 240 : sal_Bool implts_isPreviewModel( const uno::Reference< frame::XModel >& xModel )
274 : {
275 240 : if ( xModel.is() )
276 : {
277 0 : ::comphelper::MediaDescriptor aDesc( xModel->getArgs() );
278 0 : return aDesc.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_PREVIEW(), (sal_Bool)sal_False);
279 : }
280 : else
281 240 : return sal_False;
282 : }
283 :
284 1223 : sal_Bool implts_isFrameOrWindowTop( const uno::Reference< frame::XFrame >& xFrame )
285 : {
286 1223 : if (xFrame->isTop())
287 1223 : return sal_True;
288 :
289 0 : uno::Reference< awt::XTopWindow > xWindowCheck(xFrame->getContainerWindow(), uno::UNO_QUERY); // dont use _THROW here ... its a check only
290 0 : if (xWindowCheck.is())
291 : {
292 : // #i76867# top and system window is required.
293 0 : SolarMutexGuard aGuard;
294 0 : uno::Reference< awt::XWindow > xWindow( xWindowCheck, uno::UNO_QUERY );
295 0 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
296 0 : return ( pWindow && pWindow->IsSystemWindow() );
297 : }
298 :
299 0 : return sal_False;
300 : }
301 :
302 0 : void impl_setDockingWindowVisibility( const css::uno::Reference< css::uno::XComponentContext>& rxContext, const css::uno::Reference< css::frame::XFrame >& rFrame, const ::rtl::OUString& rDockingWindowName, bool bVisible )
303 : {
304 0 : const ::rtl::OUString aDockWinPrefixCommand( RTL_CONSTASCII_USTRINGPARAM( "DockingWindow" ));
305 :
306 0 : sal_Int32 nID = rDockingWindowName.toInt32();
307 0 : sal_Int32 nIndex = nID - DOCKWIN_ID_BASE;
308 :
309 0 : css::uno::Reference< css::frame::XDispatchProvider > xProvider(rFrame, css::uno::UNO_QUERY);
310 0 : if ( nIndex >= 0 && xProvider.is() )
311 : {
312 0 : ::rtl::OUString aDockWinCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
313 0 : ::rtl::OUString aDockWinArgName( aDockWinPrefixCommand );
314 :
315 0 : aDockWinArgName += ::rtl::OUString::valueOf( nIndex );
316 :
317 0 : css::uno::Sequence< css::beans::PropertyValue > aArgs(1);
318 0 : aArgs[0].Name = aDockWinArgName;
319 0 : aArgs[0].Value = css::uno::makeAny( bVisible );
320 :
321 0 : css::uno::Reference< css::frame::XDispatchHelper > xDispatcher = css::frame::DispatchHelper::create( rxContext );
322 :
323 0 : aDockWinCommand = aDockWinCommand + aDockWinArgName;
324 0 : xDispatcher->executeDispatch(
325 : xProvider,
326 : aDockWinCommand,
327 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")),
328 : 0,
329 0 : aArgs);
330 0 : }
331 0 : }
332 :
333 0 : void impl_addWindowListeners(
334 : const css::uno::Reference< css::uno::XInterface >& xThis,
335 : const css::uno::Reference< css::ui::XUIElement >& xUIElement )
336 : {
337 0 : css::uno::Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
338 0 : css::uno::Reference< css::awt::XDockableWindow > xDockWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
339 0 : if ( xDockWindow.is() && xWindow.is() )
340 : {
341 : try
342 : {
343 0 : xDockWindow->addDockableWindowListener(
344 : css::uno::Reference< css::awt::XDockableWindowListener >(
345 0 : xThis, css::uno::UNO_QUERY ));
346 0 : xWindow->addWindowListener(
347 : css::uno::Reference< css::awt::XWindowListener >(
348 0 : xThis, css::uno::UNO_QUERY ));
349 0 : xDockWindow->enableDocking( sal_True );
350 : }
351 0 : catch ( const css::uno::Exception& )
352 : {
353 : }
354 0 : }
355 0 : }
356 :
357 : } // namespace framework
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|