1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 // my own includes
28 #include "helpers.hxx"
29 #include <threadhelp/resetableguard.hxx>
30 #include <services.h>
31 
32 // interface includes
33 #include <com/sun/star/ui/DockingArea.hpp>
34 #include <com/sun/star/awt/XTopWindow.hpp>
35 #include <com/sun/star/frame/XDispatchHelper.hpp>
36 #include <com/sun/star/awt/XDockableWindow.hpp>
37 #include <com/sun/star/awt/XDockableWindowListener.hpp>
38 #include <com/sun/star/awt/XWindowListener.hpp>
39 #include <com/sun/star/ui/XUIElement.hpp>
40 
41 // other includes
42 #include <comphelper/mediadescriptor.hxx>
43 #include <vcl/svapp.hxx>
44 #include <vos/mutex.hxx>
45 #include <toolkit/unohlp.hxx>
46 
47 // namespace
48 using namespace com::sun::star;
49 
50 namespace framework
51 {
52 
hasEmptySize(const::Size & aSize)53 bool hasEmptySize( const:: Size& aSize )
54 {
55     return ( aSize.Width() == 0 ) && ( aSize.Height() == 0 );
56 }
57 
hasDefaultPosValue(const::Point & aPos)58 bool hasDefaultPosValue( const ::Point& aPos )
59 {
60     return (( aPos.X() == SAL_MAX_INT32 ) || ( aPos.Y() == SAL_MAX_INT32 ));
61 }
62 
isDefaultPos(const::com::sun::star::awt::Point & aPos)63 bool isDefaultPos( const ::com::sun::star::awt::Point& aPos )
64 {
65     return (( aPos.X == SAL_MAX_INT32 ) && ( aPos.Y == SAL_MAX_INT32 ));
66 }
67 
isDefaultPos(const::Point & aPos)68 bool isDefaultPos( const ::Point& aPos )
69 {
70     return (( aPos.X() == SAL_MAX_INT32 ) && ( aPos.Y() == SAL_MAX_INT32 ));
71 }
72 
isReverseOrderDockingArea(const sal_Int32 nDockArea)73 bool isReverseOrderDockingArea( const sal_Int32 nDockArea )
74 {
75     ui::DockingArea eDockArea = static_cast< ui::DockingArea >( nDockArea );
76     return (( eDockArea == ui::DockingArea_DOCKINGAREA_BOTTOM ) ||
77             ( eDockArea == ui::DockingArea_DOCKINGAREA_RIGHT ));
78 }
79 
isToolboxHorizontalAligned(ToolBox * pToolBox)80 bool isToolboxHorizontalAligned( ToolBox* pToolBox )
81 {
82     if ( pToolBox )
83         return (( pToolBox->GetAlign() == WINDOWALIGN_TOP ) || ( pToolBox->GetAlign() == WINDOWALIGN_BOTTOM ));
84     return false;
85 }
86 
isHorizontalDockingArea(const ui::DockingArea & nDockingArea)87 bool isHorizontalDockingArea( const ui::DockingArea& nDockingArea )
88 {
89     return (( nDockingArea == ui::DockingArea_DOCKINGAREA_TOP ) ||
90             ( nDockingArea == ui::DockingArea_DOCKINGAREA_BOTTOM ));
91 }
92 
isHorizontalDockingArea(const sal_Int32 nDockArea)93 bool isHorizontalDockingArea( const sal_Int32 nDockArea )
94 {
95   return isHorizontalDockingArea(static_cast< ui::DockingArea >( nDockArea ));
96 }
97 
retrieveToolbarNameFromHelpURL(Window * pWindow)98 ::rtl::OUString retrieveToolbarNameFromHelpURL( Window* pWindow )
99 {
100     ::rtl::OUString aToolbarName;
101 
102     if ( pWindow->GetType() == WINDOW_TOOLBOX )
103     {
104         ToolBox* pToolBox = dynamic_cast<ToolBox *>( pWindow );
105         if ( pToolBox )
106         {
107             aToolbarName = rtl::OStringToOUString( pToolBox->GetHelpId(), RTL_TEXTENCODING_UTF8 );
108             sal_Int32 i = aToolbarName.lastIndexOf( ':' );
109             if (( aToolbarName.getLength() > 0 ) && ( i > 0 ) && (( i+ 1 ) < aToolbarName.getLength() ))
110                 aToolbarName = aToolbarName.copy( i+1 ); // Remove ".HelpId:" protocol from toolbar name
111             else
112               aToolbarName = ::rtl::OUString();
113         }
114     }
115     return aToolbarName;
116 }
117 
getToolboxPtr(Window * pWindow)118 ToolBox* getToolboxPtr( Window* pWindow )
119 {
120     ToolBox* pToolbox(NULL);
121     if ( pWindow->GetType() == WINDOW_TOOLBOX )
122         pToolbox = dynamic_cast<ToolBox*>( pWindow );
123     return pToolbox;
124 }
125 
getWindowFromXUIElement(const uno::Reference<ui::XUIElement> & xUIElement)126 Window* getWindowFromXUIElement( const uno::Reference< ui::XUIElement >& xUIElement )
127 {
128     vos::OGuard aGuard( Application::GetSolarMutex() );
129     uno::Reference< awt::XWindow > xWindow;
130     if ( xUIElement.is() )
131         xWindow = uno::Reference< awt::XWindow >( xUIElement->getRealInterface(), uno::UNO_QUERY );
132     return VCLUnoHelper::GetWindow( xWindow );
133 }
134 
getTopSystemWindow(const uno::Reference<awt::XWindow> & xWindow)135 SystemWindow* getTopSystemWindow( const uno::Reference< awt::XWindow >& xWindow )
136 {
137     Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
138     while ( pWindow && !pWindow->IsSystemWindow() )
139         pWindow = pWindow->GetParent();
140 
141     if ( pWindow )
142         return (SystemWindow *)pWindow;
143     else
144         return 0;
145 }
146 
setZeroRectangle(::Rectangle & rRect)147 void setZeroRectangle( ::Rectangle& rRect )
148 {
149     rRect.setX(0);
150     rRect.setY(0);
151     rRect.setWidth(0);
152     rRect.setHeight(0);
153 }
154 
155 // ATTENTION!
156 // This value is directly copied from the sfx2 project.
157 // You have to change BOTH values, see sfx2/inc/sfx2/sfxsids.hrc (SID_DOCKWIN_START)
158 static const sal_Int32 DOCKWIN_ID_BASE = 9800;
159 
lcl_checkUIElement(const uno::Reference<ui::XUIElement> & xUIElement,awt::Rectangle & _rPosSize,uno::Reference<awt::XWindow> & _xWindow)160 bool lcl_checkUIElement(const uno::Reference< ui::XUIElement >& xUIElement, awt::Rectangle& _rPosSize, uno::Reference< awt::XWindow >& _xWindow)
161 {
162     bool bRet = xUIElement.is();
163     if ( bRet )
164     {
165         vos::OGuard     aGuard( Application::GetSolarMutex() );
166         _xWindow.set( xUIElement->getRealInterface(), uno::UNO_QUERY );
167         _rPosSize = _xWindow->getPosSize();
168 
169         Window* pWindow = VCLUnoHelper::GetWindow( _xWindow );
170         if ( pWindow->GetType() == WINDOW_TOOLBOX )
171         {
172             ::Size aSize = ((ToolBox*)pWindow)->CalcWindowSizePixel( 1 );
173             _rPosSize.Width = aSize.Width();
174             _rPosSize.Height = aSize.Height();
175         }
176     } // if ( xUIElement.is() )
177     return bRet;
178 }
179 
createToolkitWindow(const uno::Reference<lang::XMultiServiceFactory> & rFactory,const uno::Reference<awt::XWindowPeer> & rParent,const char * pService)180 uno::Reference< awt::XWindowPeer > createToolkitWindow( const uno::Reference< lang::XMultiServiceFactory >& rFactory, const uno::Reference< awt::XWindowPeer >& rParent, const char* pService )
181 {
182     const rtl::OUString aAWTToolkit( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ));
183 
184     uno::Reference< awt::XWindowPeer > xPeer;
185     if ( rFactory.is() )
186     {
187         uno::Reference< awt::XToolkit > xToolkit( rFactory->createInstance( aAWTToolkit ), uno::UNO_QUERY_THROW );
188         if ( xToolkit.is() )
189         {
190             // describe window properties.
191             css::awt::WindowDescriptor aDescriptor;
192             aDescriptor.Type                =   awt::WindowClass_SIMPLE;
193             aDescriptor.WindowServiceName   =   ::rtl::OUString::createFromAscii( pService );
194             aDescriptor.ParentIndex         =   -1;
195             aDescriptor.Parent              =   uno::Reference< awt::XWindowPeer >( rParent, uno::UNO_QUERY );
196             aDescriptor.Bounds              =   awt::Rectangle(0,0,0,0);
197             aDescriptor.WindowAttributes    =   0;
198 
199             // create a awt window
200             xPeer = xToolkit->createWindow( aDescriptor );
201         }
202     }
203 
204     return xPeer;
205 }
206 
207 // convert alignment constant to vcl's WindowAlign type
ImplConvertAlignment(sal_Int16 aAlignment)208 WindowAlign ImplConvertAlignment( sal_Int16 aAlignment )
209 {
210     if ( aAlignment == ui::DockingArea_DOCKINGAREA_LEFT )
211         return WINDOWALIGN_LEFT;
212     else if ( aAlignment == ui::DockingArea_DOCKINGAREA_RIGHT )
213         return WINDOWALIGN_RIGHT;
214     else if ( aAlignment == ui::DockingArea_DOCKINGAREA_TOP )
215         return WINDOWALIGN_TOP;
216     else
217         return WINDOWALIGN_BOTTOM;
218 }
219 
getElementTypeFromResourceURL(const::rtl::OUString & aResourceURL)220 ::rtl::OUString getElementTypeFromResourceURL( const ::rtl::OUString& aResourceURL )
221 {
222     ::rtl::OUString aType;
223 
224     ::rtl::OUString aUIResourceURL( UIRESOURCE_URL );
225     if ( aResourceURL.indexOf( aUIResourceURL ) == 0 )
226     {
227         sal_Int32       nIndex = 0;
228         ::rtl::OUString aPathPart   = aResourceURL.copy( aUIResourceURL.getLength() );
229         ::rtl::OUString aUIResource = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
230 
231         return aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
232     }
233 
234     return aType;
235 }
236 
parseResourceURL(const rtl::OUString & aResourceURL,rtl::OUString & aElementType,rtl::OUString & aElementName)237 void parseResourceURL( const rtl::OUString& aResourceURL, rtl::OUString& aElementType, rtl::OUString& aElementName )
238 {
239     ::rtl::OUString aUIResourceURL( UIRESOURCE_URL );
240     if ( aResourceURL.indexOf( aUIResourceURL ) == 0 )
241     {
242         sal_Int32       nIndex = 0;
243         ::rtl::OUString aPathPart   = aResourceURL.copy( aUIResourceURL.getLength() );
244         ::rtl::OUString aUIResource = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
245 
246         aElementType = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
247         aElementName = aPathPart.getToken( 0, (sal_Unicode)'/', nIndex );
248     }
249 }
250 
putRectangleValueToAWT(const::Rectangle & rRect)251 ::com::sun::star::awt::Rectangle putRectangleValueToAWT( const ::Rectangle& rRect )
252 {
253     css::awt::Rectangle aRect;
254     aRect.X = rRect.Left();
255     aRect.Y = rRect.Top();
256     aRect.Width = rRect.Right();
257     aRect.Height = rRect.Bottom();
258 
259     return aRect;
260 }
261 
putAWTToRectangle(const::com::sun::star::awt::Rectangle & rRect)262 ::Rectangle putAWTToRectangle( const ::com::sun::star::awt::Rectangle& rRect )
263 {
264     ::Rectangle aRect;
265     aRect.Left() = rRect.X;
266     aRect.Top() = rRect.Y;
267     aRect.Right() = rRect.Width;
268     aRect.Bottom() = rRect.Height;
269 
270     return aRect;
271 }
272 
convertRectangleToAWT(const::Rectangle & rRect)273 css::awt::Rectangle convertRectangleToAWT( const ::Rectangle& rRect )
274 {
275     css::awt::Rectangle aRect;
276     aRect.X = rRect.Left();
277     aRect.Y = rRect.Top();
278     aRect.Width = rRect.GetWidth();
279     aRect.Height = rRect.GetHeight();
280     return aRect;
281 }
282 
convertAWTToRectangle(const::com::sun::star::awt::Rectangle & rRect)283 ::Rectangle convertAWTToRectangle( const ::com::sun::star::awt::Rectangle& rRect )
284 {
285     ::Rectangle aRect;
286     aRect.Left()   = rRect.X;
287     aRect.Top()    = rRect.Y;
288     aRect.Right()  = rRect.X + rRect.Width;
289     aRect.Bottom() = rRect.Y + rRect.Height;
290 
291     return aRect;
292 }
293 
equalRectangles(const css::awt::Rectangle & rRect1,const css::awt::Rectangle & rRect2)294 bool equalRectangles( const css::awt::Rectangle& rRect1,
295                       const css::awt::Rectangle& rRect2 )
296 {
297     return (( rRect1.X == rRect2.X ) &&
298             ( rRect1.Y == rRect2.Y ) &&
299             ( rRect1.Width == rRect2.Width ) &&
300             ( rRect1.Height == rRect2.Height ));
301 }
302 
impl_getModelFromFrame(const uno::Reference<frame::XFrame> & rFrame)303 uno::Reference< frame::XModel > impl_getModelFromFrame( const uno::Reference< frame::XFrame >& rFrame )
304 {
305     // Query for the model to get check the context information
306     uno::Reference< frame::XModel > xModel;
307         if ( rFrame.is() )
308         {
309         uno::Reference< frame::XController > xController( rFrame->getController(), uno::UNO_QUERY );
310             if ( xController.is() )
311                 xModel = xController->getModel();
312         }
313 
314     return xModel;
315 }
316 
implts_isPreviewModel(const uno::Reference<frame::XModel> & xModel)317 sal_Bool implts_isPreviewModel( const uno::Reference< frame::XModel >& xModel )
318 {
319     if ( xModel.is() )
320     {
321         ::comphelper::MediaDescriptor aDesc( xModel->getArgs() );
322         return aDesc.getUnpackedValueOrDefault(::comphelper::MediaDescriptor::PROP_PREVIEW(), (sal_Bool)sal_False);
323     }
324     else
325         return sal_False;
326 }
327 
implts_isFrameOrWindowTop(const uno::Reference<frame::XFrame> & xFrame)328 sal_Bool implts_isFrameOrWindowTop( const uno::Reference< frame::XFrame >& xFrame )
329 {
330     if (xFrame->isTop())
331         return sal_True;
332 
333     uno::Reference< awt::XTopWindow > xWindowCheck(xFrame->getContainerWindow(), uno::UNO_QUERY); // dont use _THROW here ... its a check only
334     if (xWindowCheck.is())
335     {
336         // --> PB 2007-06-18 #i76867# top and system window is required.
337         ::vos::OGuard aSolarLock(&Application::GetSolarMutex());
338         uno::Reference< awt::XWindow > xWindow( xWindowCheck, uno::UNO_QUERY );
339         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
340         return ( pWindow && pWindow->IsSystemWindow() );
341         // <--
342     }
343 
344     return sal_False;
345 }
346 
impl_setDockingWindowVisibility(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMGR,const css::uno::Reference<css::frame::XFrame> & rFrame,const::rtl::OUString & rDockingWindowName,bool bVisible)347 void impl_setDockingWindowVisibility( const css::uno::Reference< css::lang::XMultiServiceFactory>& rSMGR, const css::uno::Reference< css::frame::XFrame >& rFrame, const ::rtl::OUString& rDockingWindowName, bool bVisible )
348 {
349     const ::rtl::OUString aDockWinPrefixCommand( RTL_CONSTASCII_USTRINGPARAM( "DockingWindow" ));
350     css::uno::WeakReference< css::frame::XDispatchHelper > xDispatchHelper;
351 
352     sal_Int32 nID    = rDockingWindowName.toInt32();
353     sal_Int32 nIndex = nID - DOCKWIN_ID_BASE;
354 
355     css::uno::Reference< css::frame::XDispatchProvider > xProvider(rFrame, css::uno::UNO_QUERY);
356     if ( nIndex >= 0 && xProvider.is() )
357     {
358         ::rtl::OUString aDockWinCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
359         ::rtl::OUString aDockWinArgName( aDockWinPrefixCommand );
360 
361         aDockWinArgName += ::rtl::OUString::valueOf( nIndex );
362 
363         css::uno::Sequence< css::beans::PropertyValue > aArgs(1);
364         aArgs[0].Name  = aDockWinArgName;
365         aArgs[0].Value = css::uno::makeAny( bVisible );
366 
367         css::uno::Reference< css::frame::XDispatchHelper > xDispatcher( xDispatchHelper );
368         if ( !xDispatcher.is())
369         {
370             xDispatcher = css::uno::Reference< css::frame::XDispatchHelper >(
371                 rSMGR->createInstance(SERVICENAME_DISPATCHHELPER), css::uno::UNO_QUERY_THROW);
372         }
373 
374         aDockWinCommand = aDockWinCommand + aDockWinArgName;
375         xDispatcher->executeDispatch(
376             xProvider,
377             aDockWinCommand,
378             ::rtl::OUString::createFromAscii("_self"),
379             0,
380             aArgs);
381     }
382 }
383 
impl_addWindowListeners(const css::uno::Reference<css::uno::XInterface> & xThis,const css::uno::Reference<css::ui::XUIElement> & xUIElement)384 void impl_addWindowListeners(
385     const css::uno::Reference< css::uno::XInterface >& xThis,
386     const css::uno::Reference< css::ui::XUIElement >& xUIElement )
387 {
388     css::uno::Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
389     css::uno::Reference< css::awt::XDockableWindow > xDockWindow( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
390     if ( xDockWindow.is() && xWindow.is() )
391     {
392         try
393         {
394             xDockWindow->addDockableWindowListener(
395                 css::uno::Reference< css::awt::XDockableWindowListener >(
396                     xThis, css::uno::UNO_QUERY ));
397             xWindow->addWindowListener(
398                 css::uno::Reference< css::awt::XWindowListener >(
399                     xThis, css::uno::UNO_QUERY ));
400             xDockWindow->enableDocking( sal_True );
401         }
402         catch ( css::uno::Exception& )
403         {
404         }
405     }
406 }
407 
408 } // namespace framework
409