1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
23 #include <com/sun/star/ui/DockingArea.hpp>
24 #include <com/sun/star/awt/Toolkit.hpp>
25 #include <com/sun/star/awt/XTopWindow.hpp>
26 #include <com/sun/star/frame/DispatchHelper.hpp>
27 #include <com/sun/star/awt/XDockableWindow.hpp>
28 #include <com/sun/star/awt/XDockableWindowListener.hpp>
29 #include <com/sun/star/awt/XWindowListener.hpp>
30 #include <com/sun/star/ui/XUIElement.hpp>
32 #include <comphelper/processfactory.hxx>
33 #include <unotools/mediadescriptor.hxx>
34 #include <vcl/svapp.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
37 using namespace com::sun::star
;
42 bool hasEmptySize( const ::com::sun::star::awt::Size
& rSize
)
44 return ( rSize
.Width
== 0 ) && ( rSize
.Height
== 0 );
47 bool hasDefaultPosValue( const ::com::sun::star::awt::Point
& rPos
)
49 return (( rPos
.X
== SAL_MAX_INT32
) || ( rPos
.Y
== SAL_MAX_INT32
));
52 bool isDefaultPos( const ::com::sun::star::awt::Point
& rPos
)
54 return (( rPos
.X
== SAL_MAX_INT32
) && ( rPos
.Y
== SAL_MAX_INT32
));
57 bool isReverseOrderDockingArea( const sal_Int32 nDockArea
)
59 ui::DockingArea eDockArea
= static_cast< ui::DockingArea
>( nDockArea
);
60 return (( eDockArea
== ui::DockingArea_DOCKINGAREA_BOTTOM
) ||
61 ( eDockArea
== ui::DockingArea_DOCKINGAREA_RIGHT
));
64 bool isToolboxHorizontalAligned( ToolBox
* pToolBox
)
67 return (( pToolBox
->GetAlign() == WINDOWALIGN_TOP
) || ( pToolBox
->GetAlign() == WINDOWALIGN_BOTTOM
));
71 bool isHorizontalDockingArea( const ui::DockingArea
& nDockingArea
)
73 return (( nDockingArea
== ui::DockingArea_DOCKINGAREA_TOP
) ||
74 ( nDockingArea
== ui::DockingArea_DOCKINGAREA_BOTTOM
));
77 bool isHorizontalDockingArea( const sal_Int32 nDockArea
)
79 return isHorizontalDockingArea(static_cast< ui::DockingArea
>( nDockArea
));
82 OUString
retrieveToolbarNameFromHelpURL( vcl::Window
* pWindow
)
84 OUString aToolbarName
;
86 if ( pWindow
->GetType() == WINDOW_TOOLBOX
)
88 ToolBox
* pToolBox
= dynamic_cast<ToolBox
*>( pWindow
);
91 aToolbarName
= OStringToOUString( pToolBox
->GetHelpId(), RTL_TEXTENCODING_UTF8
);
92 sal_Int32 i
= aToolbarName
.lastIndexOf( ':' );
93 if ( !aToolbarName
.isEmpty() && ( i
> 0 ) && (( i
+ 1 ) < aToolbarName
.getLength() ))
94 aToolbarName
= aToolbarName
.copy( i
+1 ); // Remove ".HelpId:" protocol from toolbar name
102 ToolBox
* getToolboxPtr( vcl::Window
* pWindow
)
104 ToolBox
* pToolbox(NULL
);
105 if ( pWindow
->GetType() == WINDOW_TOOLBOX
)
106 pToolbox
= dynamic_cast<ToolBox
*>( pWindow
);
110 vcl::Window
* getWindowFromXUIElement( const uno::Reference
< ui::XUIElement
>& xUIElement
)
112 SolarMutexGuard aGuard
;
113 uno::Reference
< awt::XWindow
> xWindow
;
114 if ( xUIElement
.is() )
115 xWindow
= uno::Reference
< awt::XWindow
>( xUIElement
->getRealInterface(), uno::UNO_QUERY
);
116 return VCLUnoHelper::GetWindow( xWindow
);
119 SystemWindow
* getTopSystemWindow( const uno::Reference
< awt::XWindow
>& xWindow
)
121 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow( xWindow
);
122 while ( pWindow
&& !pWindow
->IsSystemWindow() )
123 pWindow
= pWindow
->GetParent();
126 return static_cast<SystemWindow
*>(pWindow
);
131 void setZeroRectangle( ::Rectangle
& rRect
)
140 // This value is directly copied from the sfx2 project.
141 // You have to change BOTH values, see sfx2/inc/sfx2/sfxsids.hrc (SID_DOCKWIN_START)
142 static const sal_Int32 DOCKWIN_ID_BASE
= 9800;
144 bool lcl_checkUIElement(const uno::Reference
< ui::XUIElement
>& xUIElement
, awt::Rectangle
& _rPosSize
, uno::Reference
< awt::XWindow
>& _xWindow
)
146 bool bRet
= xUIElement
.is();
149 SolarMutexGuard aGuard
;
150 _xWindow
.set( xUIElement
->getRealInterface(), uno::UNO_QUERY
);
151 _rPosSize
= _xWindow
->getPosSize();
153 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow( _xWindow
);
154 if ( pWindow
->GetType() == WINDOW_TOOLBOX
)
156 ::Size aSize
= static_cast<ToolBox
*>(pWindow
)->CalcWindowSizePixel( 1 );
157 _rPosSize
.Width
= aSize
.Width();
158 _rPosSize
.Height
= aSize
.Height();
160 } // if ( xUIElement.is() )
164 uno::Reference
< awt::XWindowPeer
> createToolkitWindow( const uno::Reference
< uno::XComponentContext
>& rxContext
, const uno::Reference
< awt::XWindowPeer
>& rParent
, const char* pService
)
166 uno::Reference
< awt::XToolkit2
> xToolkit
= awt::Toolkit::create( rxContext
);
168 // describe window properties.
169 css::awt::WindowDescriptor aDescriptor
;
170 aDescriptor
.Type
= awt::WindowClass_SIMPLE
;
171 aDescriptor
.WindowServiceName
= OUString::createFromAscii( pService
);
172 aDescriptor
.ParentIndex
= -1;
173 aDescriptor
.Parent
= uno::Reference
< awt::XWindowPeer
>( rParent
, uno::UNO_QUERY
);
174 aDescriptor
.Bounds
= awt::Rectangle(0,0,0,0);
175 aDescriptor
.WindowAttributes
= 0;
177 // create a awt window
178 uno::Reference
< awt::XWindowPeer
> xPeer
= xToolkit
->createWindow( aDescriptor
);
183 // convert alignment constant to vcl's WindowAlign type
184 WindowAlign
ImplConvertAlignment( sal_Int16 aAlignment
)
186 if ( aAlignment
== ui::DockingArea_DOCKINGAREA_LEFT
)
187 return WINDOWALIGN_LEFT
;
188 else if ( aAlignment
== ui::DockingArea_DOCKINGAREA_RIGHT
)
189 return WINDOWALIGN_RIGHT
;
190 else if ( aAlignment
== ui::DockingArea_DOCKINGAREA_TOP
)
191 return WINDOWALIGN_TOP
;
193 return WINDOWALIGN_BOTTOM
;
196 OUString
getElementTypeFromResourceURL( const OUString
& aResourceURL
)
200 OUString
aUIResourceURL( UIRESOURCE_URL
);
201 if ( aResourceURL
.startsWith( aUIResourceURL
) )
203 sal_Int32 nIndex
= 0;
204 OUString aPathPart
= aResourceURL
.copy( aUIResourceURL
.getLength() );
205 aPathPart
.getToken( 0, (sal_Unicode
)'/', nIndex
);
207 return aPathPart
.getToken( 0, (sal_Unicode
)'/', nIndex
);
213 void parseResourceURL( const OUString
& aResourceURL
, OUString
& aElementType
, OUString
& aElementName
)
215 OUString
aUIResourceURL( UIRESOURCE_URL
);
216 if ( aResourceURL
.startsWith( aUIResourceURL
) )
218 sal_Int32 nIndex
= 0;
219 OUString aPathPart
= aResourceURL
.copy( aUIResourceURL
.getLength() );
220 aPathPart
.getToken( 0, (sal_Unicode
)'/', nIndex
);
222 aElementType
= aPathPart
.getToken( 0, (sal_Unicode
)'/', nIndex
);
223 aElementName
= aPathPart
.getToken( 0, (sal_Unicode
)'/', nIndex
);
227 ::com::sun::star::awt::Rectangle
putRectangleValueToAWT( const ::Rectangle
& rRect
)
229 css::awt::Rectangle aRect
;
230 aRect
.X
= rRect
.Left();
231 aRect
.Y
= rRect
.Top();
232 aRect
.Width
= rRect
.Right();
233 aRect
.Height
= rRect
.Bottom();
238 ::Rectangle
putAWTToRectangle( const ::com::sun::star::awt::Rectangle
& rRect
)
241 aRect
.Left() = rRect
.X
;
242 aRect
.Top() = rRect
.Y
;
243 aRect
.Right() = rRect
.Width
;
244 aRect
.Bottom() = rRect
.Height
;
249 bool equalRectangles( const css::awt::Rectangle
& rRect1
,
250 const css::awt::Rectangle
& rRect2
)
252 return (( rRect1
.X
== rRect2
.X
) &&
253 ( rRect1
.Y
== rRect2
.Y
) &&
254 ( rRect1
.Width
== rRect2
.Width
) &&
255 ( rRect1
.Height
== rRect2
.Height
));
258 uno::Reference
< frame::XModel
> impl_getModelFromFrame( const uno::Reference
< frame::XFrame
>& rFrame
)
260 // Query for the model to get check the context information
261 uno::Reference
< frame::XModel
> xModel
;
264 uno::Reference
< frame::XController
> xController( rFrame
->getController(), uno::UNO_QUERY
);
265 if ( xController
.is() )
266 xModel
= xController
->getModel();
272 bool implts_isPreviewModel( const uno::Reference
< frame::XModel
>& xModel
)
276 utl::MediaDescriptor
aDesc( xModel
->getArgs() );
277 return aDesc
.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_PREVIEW(), false);
283 bool implts_isFrameOrWindowTop( const uno::Reference
< frame::XFrame
>& xFrame
)
288 uno::Reference
< awt::XTopWindow
> xWindowCheck(xFrame
->getContainerWindow(), uno::UNO_QUERY
); // dont use _THROW here ... it's a check only
289 if (xWindowCheck
.is())
291 // #i76867# top and system window is required.
292 SolarMutexGuard aGuard
;
293 uno::Reference
< awt::XWindow
> xWindow( xWindowCheck
, uno::UNO_QUERY
);
294 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow( xWindow
);
295 return ( pWindow
&& pWindow
->IsSystemWindow() );
301 void impl_setDockingWindowVisibility( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
, const css::uno::Reference
< css::frame::XFrame
>& rFrame
, const OUString
& rDockingWindowName
, bool bVisible
)
303 const OUString
aDockWinPrefixCommand( "DockingWindow" );
305 sal_Int32 nID
= rDockingWindowName
.toInt32();
306 sal_Int32 nIndex
= nID
- DOCKWIN_ID_BASE
;
308 css::uno::Reference
< css::frame::XDispatchProvider
> xProvider(rFrame
, css::uno::UNO_QUERY
);
309 if ( nIndex
>= 0 && xProvider
.is() )
311 OUString
aDockWinCommand( ".uno:" );
312 OUString
aDockWinArgName( aDockWinPrefixCommand
);
314 aDockWinArgName
+= OUString::number( nIndex
);
316 css::uno::Sequence
< css::beans::PropertyValue
> aArgs(1);
317 aArgs
[0].Name
= aDockWinArgName
;
318 aArgs
[0].Value
= css::uno::makeAny( bVisible
);
320 css::uno::Reference
< css::frame::XDispatchHelper
> xDispatcher
= css::frame::DispatchHelper::create( rxContext
);
322 aDockWinCommand
= aDockWinCommand
+ aDockWinArgName
;
323 xDispatcher
->executeDispatch(
332 void impl_addWindowListeners(
333 const css::uno::Reference
< css::uno::XInterface
>& xThis
,
334 const css::uno::Reference
< css::ui::XUIElement
>& xUIElement
)
336 css::uno::Reference
< css::awt::XWindow
> xWindow( xUIElement
->getRealInterface(), css::uno::UNO_QUERY
);
337 css::uno::Reference
< css::awt::XDockableWindow
> xDockWindow( xUIElement
->getRealInterface(), css::uno::UNO_QUERY
);
338 if ( xDockWindow
.is() && xWindow
.is() )
342 xDockWindow
->addDockableWindowListener(
343 css::uno::Reference
< css::awt::XDockableWindowListener
>(
344 xThis
, css::uno::UNO_QUERY
));
345 xWindow
->addWindowListener(
346 css::uno::Reference
< css::awt::XWindowListener
>(
347 xThis
, css::uno::UNO_QUERY
));
348 xDockWindow
->enableDocking( sal_True
);
350 catch ( const css::uno::Exception
& )
356 } // namespace framework
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */