bump product version to 4.1.6.2
[LibreOffice.git] / toolkit / source / helper / unowrapper.cxx
blobd138da4898e5e5121b2e9996618a174068a7277e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/awt/WindowEvent.hpp>
21 #include <comphelper/processfactory.hxx>
23 #include <toolkit/helper/unowrapper.hxx>
24 #include <toolkit/helper/vclunohelper.hxx>
25 #include <toolkit/helper/convert.hxx>
26 #include <toolkit/awt/vclxwindow.hxx>
27 #include <toolkit/awt/vclxwindows.hxx>
28 #include <toolkit/awt/vclxcontainer.hxx>
29 #include <toolkit/awt/vclxtopwindow.hxx>
30 #include <toolkit/awt/vclxgraphics.hxx>
32 #include "toolkit/dllapi.h"
33 #include <vcl/svapp.hxx>
34 #include <vcl/syswin.hxx>
35 #include <vcl/menu.hxx>
37 #include <tools/debug.hxx>
39 using namespace ::com::sun::star;
41 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > CreateXWindow( Window* pWindow )
43 switch ( pWindow->GetType() )
45 case WINDOW_IMAGERADIOBUTTON:
46 case WINDOW_IMAGEBUTTON:
47 case WINDOW_SPINBUTTON:
48 case WINDOW_MENUBUTTON:
49 case WINDOW_MOREBUTTON:
50 case WINDOW_PUSHBUTTON:
51 case WINDOW_HELPBUTTON:
52 case WINDOW_OKBUTTON:
53 case WINDOW_CANCELBUTTON: return new VCLXButton;
54 case WINDOW_CHECKBOX: return new VCLXCheckBox;
55 // #i95042#
56 // A Window of type <MetricBox> is inherited from type <ComboBox>.
57 // Thus, it does make more sense to return a <VCLXComboBox> instance
58 // instead of only a <VCLXWindow> instance, especially regarding its
59 // corresponding accessibility API.
60 case WINDOW_METRICBOX:
61 case WINDOW_COMBOBOX: return new VCLXComboBox;
62 case WINDOW_SPINFIELD:
63 case WINDOW_NUMERICFIELD:
64 case WINDOW_CURRENCYFIELD: return new VCLXNumericField;
65 case WINDOW_DATEFIELD: return new VCLXDateField;
66 case WINDOW_MULTILINEEDIT:
67 case WINDOW_EDIT: return new VCLXEdit;
68 case WINDOW_METRICFIELD: return new VCLXSpinField;
69 case WINDOW_MESSBOX:
70 case WINDOW_INFOBOX:
71 case WINDOW_WARNINGBOX:
72 case WINDOW_QUERYBOX:
73 case WINDOW_ERRORBOX: return new VCLXMessageBox;
74 case WINDOW_FIXEDIMAGE: return new VCLXImageControl;
75 case WINDOW_FIXEDTEXT: return new VCLXFixedText;
76 case WINDOW_MULTILISTBOX:
77 case WINDOW_LISTBOX: return new VCLXListBox;
78 case WINDOW_LONGCURRENCYFIELD: return new VCLXCurrencyField;
79 case WINDOW_DIALOG:
80 case WINDOW_MODALDIALOG:
81 case WINDOW_TABDIALOG:
82 case WINDOW_BUTTONDIALOG:
83 case WINDOW_MODELESSDIALOG: return new VCLXDialog;
84 case WINDOW_PATTERNFIELD: return new VCLXPatternField;
85 case WINDOW_RADIOBUTTON: return new VCLXRadioButton;
86 case WINDOW_SCROLLBAR: return new VCLXScrollBar;
87 case WINDOW_TIMEFIELD: return new VCLXTimeField;
89 case WINDOW_SYSWINDOW:
90 case WINDOW_WORKWINDOW:
91 case WINDOW_DOCKINGWINDOW:
92 case WINDOW_FLOATINGWINDOW:
93 case WINDOW_HELPTEXTWINDOW: return new VCLXTopWindow;
95 case WINDOW_WINDOW:
96 case WINDOW_TABPAGE: return new VCLXContainer;
98 case WINDOW_TOOLBOX: return new VCLXToolBox;
99 case WINDOW_TABCONTROL: return new VCLXMultiPage;
101 // case WINDOW_FIXEDLINE:
102 // case WINDOW_FIXEDBITMAP:
103 // case WINDOW_DATEBOX:
104 // case WINDOW_GROUPBOX:
105 // case WINDOW_LONGCURRENCYBOX:
106 // case WINDOW_SPLITTER:
107 // case WINDOW_STATUSBAR:
108 // case WINDOW_TABCONTROL:
109 // case WINDOW_NUMERICBOX:
110 // case WINDOW_TRISTATEBOX:
111 // case WINDOW_TIMEBOX:
112 // case WINDOW_SPLITWINDOW:
113 // case WINDOW_SCROLLBARBOX:
114 // case WINDOW_PATTERNBOX:
115 // case WINDOW_CURRENCYBOX:
116 default: return new VCLXWindow( true );
120 // ----------------------------------------------------
121 // class UnoWrapper
122 // ----------------------------------------------------
124 extern "C" {
126 TOOLKIT_DLLPUBLIC UnoWrapperBase* CreateUnoWrapper()
128 return new UnoWrapper( NULL );
131 } // extern "C"
134 UnoWrapper::UnoWrapper( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit>& rxToolkit )
136 mxToolkit = rxToolkit;
139 void UnoWrapper::Destroy()
141 delete this;
144 UnoWrapper::~UnoWrapper()
148 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> UnoWrapper::GetVCLToolkit()
150 if ( !mxToolkit.is() )
151 mxToolkit = VCLUnoHelper::CreateToolkit();
152 return mxToolkit.get();
155 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> UnoWrapper::GetWindowInterface( Window* pWindow, sal_Bool bCreate )
157 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xPeer = pWindow->GetWindowPeer();
158 if ( !xPeer.is() && bCreate )
160 xPeer = CreateXWindow( pWindow );
161 SetWindowInterface( pWindow, xPeer );
163 return xPeer;
166 void UnoWrapper::SetWindowInterface( Window* pWindow, ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xIFace )
168 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( xIFace );
170 DBG_ASSERT( pVCLXWindow, "SetComponentInterface - unsupported type" );
171 if ( pVCLXWindow )
173 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xPeer = pWindow->GetWindowPeer();
174 if( xPeer.is() )
176 bool bSameInstance( pVCLXWindow == dynamic_cast< VCLXWindow* >( xPeer.get() ));
177 DBG_ASSERT( bSameInstance, "UnoWrapper::SetWindowInterface: there already *is* a WindowInterface for this window!" );
178 if ( bSameInstance )
179 return;
181 pVCLXWindow->SetWindow( pWindow );
182 pWindow->SetWindowPeer( xIFace, pVCLXWindow );
186 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics> UnoWrapper::CreateGraphics( OutputDevice* pOutDev )
188 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics> xGrf;
189 VCLXGraphics* pGrf = new VCLXGraphics;
190 xGrf = pGrf;
191 pGrf->Init( pOutDev );
192 return xGrf;
195 void UnoWrapper::ReleaseAllGraphics( OutputDevice* pOutDev )
197 VCLXGraphicsList_impl* pLst = pOutDev->GetUnoGraphicsList();
198 if ( pLst )
200 for ( size_t n = 0; n < pLst->size(); n++ )
202 VCLXGraphics* pGrf = (*pLst)[ n ];
203 pGrf->SetOutputDevice( NULL );
209 // It was once called in the Window-CTOR to make listeners of the Container
210 // to react. This didn't really work, as the interface within the Window-CTOR
211 // was not ready.
212 // => Call only the listener, when created through ::com::sun::star::awt::Toolkit
215 void ImplSmartWindowCreated( Window* pNewWindow )
217 UNOWindowData* pParentUNOData = pNewWindow->GetParent() ?
218 pNewWindow->GetParent()->GetUNOData() : NULL;
220 if ( pParentUNOData && pParentUNOData->GetListeners( EL_CONTAINER ) )
222 UNOWindowData* pUNOData = pNewWindow->GetUNOData();
223 if ( !pUNOData )
224 pUNOData = ImplSmartCreateUNOData( pNewWindow );
226 ::com::sun::star::awt::VclContainerEvent aEvent;
227 aEvent.Source = (UsrObject*)pParentUNOData->GetWindowPeer();
228 aEvent.Id = VCLCOMPONENT_ADDED;
229 aEvent.Child = (UsrObject*)pUNOData->GetWindowPeer();
231 EventList* pLst = pParentUNOData->GetListeners( EL_CONTAINER );
232 for ( sal_uInt32 n = 0; n < pLst->Count(); n++ )
234 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > * pRef = pLst->GetObject( n );
235 ((::com::sun::star::awt::XVclContainerListener*)(::com::sun::star::lang::XEventListener*)*pRef)->windowAdded( aEvent );
241 static sal_Bool lcl_ImplIsParent( Window* pParentWindow, Window* pPossibleChild )
243 Window* pWindow = ( pPossibleChild != pParentWindow ) ? pPossibleChild : NULL;
244 while ( pWindow && ( pWindow != pParentWindow ) )
245 pWindow = pWindow->GetParent();
247 return pWindow ? sal_True : sal_False;
250 void UnoWrapper::WindowDestroyed( Window* pWindow )
252 // their still might be some children created with ::com::sun::star::loader::Java
253 // that would otherwise not be destroyed until the garbage collector cleans up
254 Window* pChild = pWindow->GetWindow( WINDOW_FIRSTCHILD );
255 while ( pChild )
257 Window* pNextChild = pChild->GetWindow( WINDOW_NEXT );
259 Window* pClient = pChild->GetWindow( WINDOW_CLIENT );
260 if ( pClient->GetWindowPeer() )
262 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp( pClient->GetComponentInterface( sal_False ), ::com::sun::star::uno::UNO_QUERY );
263 xComp->dispose();
266 pChild = pNextChild;
269 // ::com::sun::star::chaos::System-Windows suchen...
270 Window* pOverlap = pWindow->GetWindow( WINDOW_OVERLAP );
271 if ( pOverlap )
273 pOverlap = pOverlap->GetWindow( WINDOW_FIRSTOVERLAP );
274 while ( pOverlap )
276 Window* pNextOverlap = pOverlap->GetWindow( WINDOW_NEXT );
277 Window* pClient = pOverlap->GetWindow( WINDOW_CLIENT );
279 if ( pClient->GetWindowPeer() && lcl_ImplIsParent( pWindow, pClient ) )
281 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp( pClient->GetComponentInterface( sal_False ), ::com::sun::star::uno::UNO_QUERY );
282 xComp->dispose();
285 pOverlap = pNextOverlap;
289 Window* pParent = pWindow->GetParent();
290 if ( pParent && pParent->GetWindowPeer() )
291 pParent->GetWindowPeer()->notifyWindowRemoved( *pWindow );
293 VCLXWindow* pWindowPeer = pWindow->GetWindowPeer();
294 uno::Reference< lang::XComponent > xWindowPeerComp( pWindow->GetComponentInterface( sal_False ), uno::UNO_QUERY );
295 OSL_ENSURE( ( pWindowPeer != NULL ) == ( xWindowPeerComp.is() == sal_True ),
296 "UnoWrapper::WindowDestroyed: inconsistency in the window's peers!" );
297 if ( pWindowPeer )
299 pWindowPeer->SetWindow( NULL );
300 pWindow->SetWindowPeer( NULL, NULL );
302 if ( xWindowPeerComp.is() )
303 xWindowPeerComp->dispose();
305 // #102132# Iterate over frames after setting Window peer to NULL,
306 // because while destroying other frames, we get get into the method again and try
307 // to destroy this window again...
308 // #i42462#/#116855# no, don't loop: Instead, just ensure that all our top-window-children
309 // are disposed, too (which should also be a valid fix for #102132#, but doesn't have the extreme
310 // performance penalties)
311 if ( pWindow )
313 Window* pTopWindowChild = pWindow->GetWindow( WINDOW_FIRSTTOPWINDOWCHILD );
314 while ( pTopWindowChild )
316 OSL_ENSURE( pTopWindowChild->GetParent() == pWindow,
317 "UnoWrapper::WindowDestroyed: inconsistency in the SystemWindow relationship!" );
319 Window* pNextTopChild = pTopWindowChild->GetWindow( WINDOW_NEXTTOPWINDOWSIBLING );
321 //the window still could be on the stack, so we have to
322 // use lazy delete ( it will automatically
323 // disconnect from the currently destroyed parent window )
324 pTopWindowChild->doLazyDelete();
326 pTopWindowChild = pNextTopChild;
331 // ----------------------------------------------------------------------------
332 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > UnoWrapper::CreateAccessible( Menu* pMenu, sal_Bool bIsMenuBar )
334 return maAccessibleFactoryAccess.getFactory().createAccessible( pMenu, bIsMenuBar );
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */