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