update dev300-m58
[ooovba.git] / toolkit / source / helper / unowrapper.cxx
bloba2dabaccb51ba363c24f42327d02dc014938503e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unowrapper.cxx,v $
10 * $Revision: 1.34 $
12 * This file is part of OpenOffice.org.
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.
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).
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.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
33 #include <com/sun/star/awt/WindowEvent.hpp>
34 #include <comphelper/processfactory.hxx>
36 #include <toolkit/helper/unowrapper.hxx>
37 #include <toolkit/helper/vclunohelper.hxx>
38 #include <toolkit/helper/convert.hxx>
39 #include <toolkit/awt/vclxwindow.hxx>
40 #include <toolkit/awt/vclxwindows.hxx>
41 #include <toolkit/awt/vclxcontainer.hxx>
42 #include <toolkit/awt/vclxtopwindow.hxx>
43 #include <toolkit/awt/vclxgraphics.hxx>
45 #include "toolkit/dllapi.h"
46 #include <vcl/svapp.hxx>
47 #include <vcl/syswin.hxx>
48 #include <vcl/menu.hxx>
50 #include <tools/debug.hxx>
52 using namespace ::com::sun::star;
54 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > CreateXWindow( Window* pWindow )
56 switch ( pWindow->GetType() )
58 case WINDOW_IMAGERADIOBUTTON:
59 case WINDOW_IMAGEBUTTON:
60 case WINDOW_SPINBUTTON:
61 case WINDOW_MENUBUTTON:
62 case WINDOW_MOREBUTTON:
63 case WINDOW_PUSHBUTTON:
64 case WINDOW_HELPBUTTON:
65 case WINDOW_OKBUTTON:
66 case WINDOW_CANCELBUTTON: return new VCLXButton;
67 case WINDOW_CHECKBOX: return new VCLXCheckBox;
68 // --> OD 2009-06-29 #i95042#
69 // A Window of type <MetricBox> is inherited from type <ComboBox>.
70 // Thus, it does make more sense to return a <VCLXComboBox> instance
71 // instead of only a <VCLXWindow> instance, especially regarding its
72 // corresponding accessibility API.
73 case WINDOW_METRICBOX:
74 // <--
75 case WINDOW_COMBOBOX: return new VCLXComboBox;
76 case WINDOW_SPINFIELD:
77 case WINDOW_NUMERICFIELD:
78 case WINDOW_CURRENCYFIELD: return new VCLXNumericField;
79 case WINDOW_DATEFIELD: return new VCLXDateField;
80 case WINDOW_MULTILINEEDIT:
81 case WINDOW_EDIT: return new VCLXEdit;
82 case WINDOW_METRICFIELD: return new VCLXSpinField;
83 case WINDOW_MESSBOX:
84 case WINDOW_INFOBOX:
85 case WINDOW_WARNINGBOX:
86 case WINDOW_QUERYBOX:
87 case WINDOW_ERRORBOX: return new VCLXMessageBox;
88 case WINDOW_FIXEDIMAGE: return new VCLXImageControl;
89 case WINDOW_FIXEDTEXT: return new VCLXFixedText;
90 case WINDOW_MULTILISTBOX:
91 case WINDOW_LISTBOX: return new VCLXListBox;
92 case WINDOW_LONGCURRENCYFIELD: return new VCLXCurrencyField;
93 case WINDOW_DIALOG:
94 case WINDOW_MODALDIALOG:
95 case WINDOW_TABDIALOG:
96 case WINDOW_BUTTONDIALOG:
97 case WINDOW_MODELESSDIALOG: return new VCLXDialog;
98 case WINDOW_PATTERNFIELD: return new VCLXPatternField;
99 case WINDOW_RADIOBUTTON: return new VCLXRadioButton;
100 case WINDOW_SCROLLBAR: return new VCLXScrollBar;
101 case WINDOW_TIMEFIELD: return new VCLXTimeField;
103 case WINDOW_SYSWINDOW:
104 case WINDOW_WORKWINDOW:
105 case WINDOW_DOCKINGWINDOW:
106 case WINDOW_FLOATINGWINDOW:
107 case WINDOW_HELPTEXTWINDOW: return new VCLXTopWindow;
109 case WINDOW_WINDOW:
110 case WINDOW_TABPAGE: return new VCLXContainer;
112 case WINDOW_TOOLBOX: return new VCLXToolBox;
114 // case WINDOW_FIXEDLINE:
115 // case WINDOW_FIXEDBITMAP:
116 // case WINDOW_DATEBOX:
117 // case WINDOW_GROUPBOX:
118 // case WINDOW_LONGCURRENCYBOX:
119 // case WINDOW_SPLITTER:
120 // case WINDOW_STATUSBAR:
121 // case WINDOW_TABCONTROL:
122 // case WINDOW_NUMERICBOX:
123 // case WINDOW_TRISTATEBOX:
124 // case WINDOW_TIMEBOX:
125 // case WINDOW_SPLITWINDOW:
126 // case WINDOW_SCROLLBARBOX:
127 // case WINDOW_PATTERNBOX:
128 // case WINDOW_CURRENCYBOX:
129 default: return new VCLXWindow( true );
133 // ----------------------------------------------------
134 // class UnoWrapper
135 // ----------------------------------------------------
137 extern "C" {
139 TOOLKIT_DLLPUBLIC UnoWrapperBase* CreateUnoWrapper()
141 return new UnoWrapper( NULL );
144 } // extern "C"
147 UnoWrapper::UnoWrapper( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit>& rxToolkit )
149 mxToolkit = rxToolkit;
152 void UnoWrapper::Destroy()
154 delete this;
157 UnoWrapper::~UnoWrapper()
161 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> UnoWrapper::GetVCLToolkit()
163 if ( !mxToolkit.is() )
164 mxToolkit = VCLUnoHelper::CreateToolkit();
165 return mxToolkit.get();
168 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> UnoWrapper::GetWindowInterface( Window* pWindow, BOOL bCreate )
170 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xPeer = pWindow->GetWindowPeer();
171 if ( !xPeer.is() && bCreate )
173 xPeer = CreateXWindow( pWindow );
174 SetWindowInterface( pWindow, xPeer );
176 return xPeer;
179 void UnoWrapper::SetWindowInterface( Window* pWindow, ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xIFace )
181 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( xIFace );
183 DBG_ASSERT( pVCLXWindow, "SetComponentInterface - unsupported type" );
184 if ( pVCLXWindow )
186 if( pWindow->GetWindowPeer() )
188 int i = 0;
189 i++;
190 // DBG_ERROR( "UnoWrapper::SetWindowInterface: there already *is* a WindowInterface for this window!" );
192 pVCLXWindow->SetWindow( pWindow );
193 pWindow->SetWindowPeer( xIFace, pVCLXWindow );
197 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics> UnoWrapper::CreateGraphics( OutputDevice* pOutDev )
199 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics> xGrf;
200 VCLXGraphics* pGrf = new VCLXGraphics;
201 xGrf = pGrf;
202 pGrf->Init( pOutDev );
203 return xGrf;
206 void UnoWrapper::ReleaseAllGraphics( OutputDevice* pOutDev )
208 List* pLst = pOutDev->GetUnoGraphicsList();
209 if ( pLst )
211 for ( sal_uInt32 n = 0; n < pLst->Count(); n++ )
213 VCLXGraphics* pGrf = (VCLXGraphics*)pLst->GetObject( n );
214 pGrf->SetOutputDevice( NULL );
220 // MT: Wurde im Window-CTOR gerufen, damit Container-Listener
221 // vom Parent reagieren, aber hat sowieso nicht richtig funktioniert,
222 // weil im Window-CTOR das Interface noch nicht da ist!
223 // => Nur Listener rufen, wenn ueber das ::com::sun::star::awt::Toolkit erzeugt
226 void ImplSmartWindowCreated( Window* pNewWindow )
228 UNOWindowData* pParentUNOData = pNewWindow->GetParent() ?
229 pNewWindow->GetParent()->GetUNOData() : NULL;
231 if ( pParentUNOData && pParentUNOData->GetListeners( EL_CONTAINER ) )
233 UNOWindowData* pUNOData = pNewWindow->GetUNOData();
234 if ( !pUNOData )
235 pUNOData = ImplSmartCreateUNOData( pNewWindow );
237 ::com::sun::star::awt::VclContainerEvent aEvent;
238 aEvent.Source = (UsrObject*)pParentUNOData->GetWindowPeer();
239 aEvent.Id = VCLCOMPONENT_ADDED;
240 aEvent.Child = (UsrObject*)pUNOData->GetWindowPeer();
242 EventList* pLst = pParentUNOData->GetListeners( EL_CONTAINER );
243 for ( sal_uInt32 n = 0; n < pLst->Count(); n++ )
245 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > * pRef = pLst->GetObject( n );
246 ((::com::sun::star::awt::XVclContainerListener*)(::com::sun::star::lang::XEventListener*)*pRef)->windowAdded( aEvent );
252 sal_Bool lcl_ImplIsParent( Window* pParentWindow, Window* pPossibleChild )
254 Window* pWindow = ( pPossibleChild != pParentWindow ) ? pPossibleChild : NULL;
255 while ( pWindow && ( pWindow != pParentWindow ) )
256 pWindow = pWindow->GetParent();
258 return pWindow ? sal_True : sal_False;
261 void UnoWrapper::WindowDestroyed( Window* pWindow )
263 // ggf. existieren noch von ::com::sun::star::loader::Java erzeugte Childs, die sonst erst
264 // im Garbage-Collector zerstoert werden...
265 Window* pChild = pWindow->GetWindow( WINDOW_FIRSTCHILD );
266 while ( pChild )
268 Window* pNextChild = pChild->GetWindow( WINDOW_NEXT );
270 Window* pClient = pChild->GetWindow( WINDOW_CLIENT );
271 if ( pClient->GetWindowPeer() )
273 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp( pClient->GetComponentInterface( FALSE ), ::com::sun::star::uno::UNO_QUERY );
274 xComp->dispose();
277 pChild = pNextChild;
280 // ::com::sun::star::chaos::System-Windows suchen...
281 Window* pOverlap = pWindow->GetWindow( WINDOW_OVERLAP );
282 pOverlap = pOverlap->GetWindow( WINDOW_FIRSTOVERLAP );
283 while ( pOverlap )
285 Window* pNextOverlap = pOverlap->GetWindow( WINDOW_NEXT );
286 Window* pClient = pOverlap->GetWindow( WINDOW_CLIENT );
288 if ( pClient->GetWindowPeer() && lcl_ImplIsParent( pWindow, pClient ) )
290 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp( pClient->GetComponentInterface( FALSE ), ::com::sun::star::uno::UNO_QUERY );
291 xComp->dispose();
294 pOverlap = pNextOverlap;
297 Window* pParent = pWindow->GetParent();
298 if ( pParent && pParent->GetWindowPeer() )
299 pParent->GetWindowPeer()->notifyWindowRemoved( *pWindow );
301 VCLXWindow* pWindowPeer = pWindow->GetWindowPeer();
302 uno::Reference< lang::XComponent > xWindowPeerComp( pWindow->GetComponentInterface( FALSE ), uno::UNO_QUERY );
303 OSL_ENSURE( ( pWindowPeer != NULL ) == ( xWindowPeerComp.is() == sal_True ),
304 "UnoWrapper::WindowDestroyed: inconsistency in the window's peers!" );
305 if ( pWindowPeer )
307 pWindowPeer->SetWindow( NULL );
308 pWindow->SetWindowPeer( NULL, NULL );
310 if ( xWindowPeerComp.is() )
311 xWindowPeerComp->dispose();
313 // #102132# Iterate over frames after setting Window peer to NULL,
314 // because while destroying other frames, we get get into the method again and try
315 // to destroy this window again...
316 // #i42462#/#116855# no, don't loop: Instead, just ensure that all our top-window-children
317 // are disposed, too (which should also be a valid fix for #102132#, but doesn't have the extreme
318 // performance penalties)
319 if ( pWindow )
321 Window* pTopWindowChild = pWindow->GetWindow( WINDOW_FIRSTTOPWINDOWCHILD );
322 while ( pTopWindowChild )
324 OSL_ENSURE( pTopWindowChild->GetParent() == pWindow, "UnoWrapper::WindowDestroyed: inconsistency in the SystemWindow relationship!" );
326 uno::Reference< lang::XComponent > xComp( pTopWindowChild->GetComponentInterface( FALSE ), uno::UNO_QUERY );
327 pTopWindowChild = pTopWindowChild->GetWindow( WINDOW_NEXTTOPWINDOWSIBLING );
328 if ( xComp.is() )
329 xComp->dispose();
334 // ----------------------------------------------------------------------------
335 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > UnoWrapper::CreateAccessible( Menu* pMenu, sal_Bool bIsMenuBar )
337 return maAccessibleFactoryAccess.getFactory().createAccessible( pMenu, bIsMenuBar );