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 $
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
:
66 case WINDOW_CANCELBUTTON
: return new VCLXButton
;
67 case WINDOW_CHECKBOX
: return new VCLXCheckBox
;
68 case WINDOW_COMBOBOX
: return new VCLXComboBox
;
69 case WINDOW_SPINFIELD
:
70 case WINDOW_NUMERICFIELD
:
71 case WINDOW_CURRENCYFIELD
: return new VCLXNumericField
;
72 case WINDOW_DATEFIELD
: return new VCLXDateField
;
73 case WINDOW_MULTILINEEDIT
:
74 case WINDOW_EDIT
: return new VCLXEdit
;
75 case WINDOW_METRICFIELD
: return new VCLXSpinField
;
78 case WINDOW_WARNINGBOX
:
80 case WINDOW_ERRORBOX
: return new VCLXMessageBox
;
81 case WINDOW_FIXEDIMAGE
: return new VCLXImageControl
;
82 case WINDOW_FIXEDTEXT
: return new VCLXFixedText
;
83 case WINDOW_MULTILISTBOX
:
84 case WINDOW_LISTBOX
: return new VCLXListBox
;
85 case WINDOW_LONGCURRENCYFIELD
: return new VCLXCurrencyField
;
87 case WINDOW_MODALDIALOG
:
88 case WINDOW_TABDIALOG
:
89 case WINDOW_BUTTONDIALOG
:
90 case WINDOW_MODELESSDIALOG
: return new VCLXDialog
;
91 case WINDOW_PATTERNFIELD
: return new VCLXPatternField
;
92 case WINDOW_RADIOBUTTON
: return new VCLXRadioButton
;
93 case WINDOW_SCROLLBAR
: return new VCLXScrollBar
;
94 case WINDOW_TIMEFIELD
: return new VCLXTimeField
;
96 case WINDOW_SYSWINDOW
:
97 case WINDOW_WORKWINDOW
:
98 case WINDOW_DOCKINGWINDOW
:
99 case WINDOW_FLOATINGWINDOW
:
100 case WINDOW_HELPTEXTWINDOW
: return new VCLXTopWindow
;
103 case WINDOW_TABPAGE
: return new VCLXContainer
;
105 case WINDOW_TOOLBOX
: return new VCLXToolBox
;
107 // case WINDOW_FIXEDLINE:
108 // case WINDOW_FIXEDBITMAP:
109 // case WINDOW_DATEBOX:
110 // case WINDOW_GROUPBOX:
111 // case WINDOW_LONGCURRENCYBOX:
112 // case WINDOW_METRICBOX:
113 // case WINDOW_SPLITTER:
114 // case WINDOW_STATUSBAR:
115 // case WINDOW_TABCONTROL:
116 // case WINDOW_NUMERICBOX:
117 // case WINDOW_TRISTATEBOX:
118 // case WINDOW_TIMEBOX:
119 // case WINDOW_SPLITWINDOW:
120 // case WINDOW_SCROLLBARBOX:
121 // case WINDOW_PATTERNBOX:
122 // case WINDOW_CURRENCYBOX:
123 default: return new VCLXWindow( true );
127 // ----------------------------------------------------
129 // ----------------------------------------------------
133 TOOLKIT_DLLPUBLIC UnoWrapperBase
* CreateUnoWrapper()
135 return new UnoWrapper( NULL
);
141 UnoWrapper::UnoWrapper( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XToolkit
>& rxToolkit
)
143 mxToolkit
= rxToolkit
;
146 void UnoWrapper::Destroy()
151 UnoWrapper::~UnoWrapper()
155 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XToolkit
> UnoWrapper::GetVCLToolkit()
157 if ( !mxToolkit
.is() )
158 mxToolkit
= VCLUnoHelper::CreateToolkit();
159 return mxToolkit
.get();
162 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindowPeer
> UnoWrapper::GetWindowInterface( Window
* pWindow
, BOOL bCreate
)
164 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindowPeer
> xPeer
= pWindow
->GetWindowPeer();
165 if ( !xPeer
.is() && bCreate
)
167 xPeer
= CreateXWindow( pWindow
);
168 SetWindowInterface( pWindow
, xPeer
);
173 void UnoWrapper::SetWindowInterface( Window
* pWindow
, ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindowPeer
> xIFace
)
175 VCLXWindow
* pVCLXWindow
= VCLXWindow::GetImplementation( xIFace
);
177 DBG_ASSERT( pVCLXWindow
, "SetComponentInterface - unsupported type" );
180 if( pWindow
->GetWindowPeer() )
184 // DBG_ERROR( "UnoWrapper::SetWindowInterface: there already *is* a WindowInterface for this window!" );
186 pVCLXWindow
->SetWindow( pWindow
);
187 pWindow
->SetWindowPeer( xIFace
, pVCLXWindow
);
191 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> UnoWrapper::CreateGraphics( OutputDevice
* pOutDev
)
193 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XGraphics
> xGrf
;
194 VCLXGraphics
* pGrf
= new VCLXGraphics
;
196 pGrf
->Init( pOutDev
);
200 void UnoWrapper::ReleaseAllGraphics( OutputDevice
* pOutDev
)
202 List
* pLst
= pOutDev
->GetUnoGraphicsList();
205 for ( sal_uInt32 n
= 0; n
< pLst
->Count(); n
++ )
207 VCLXGraphics
* pGrf
= (VCLXGraphics
*)pLst
->GetObject( n
);
208 pGrf
->SetOutputDevice( NULL
);
214 // MT: Wurde im Window-CTOR gerufen, damit Container-Listener
215 // vom Parent reagieren, aber hat sowieso nicht richtig funktioniert,
216 // weil im Window-CTOR das Interface noch nicht da ist!
217 // => Nur Listener rufen, wenn ueber das ::com::sun::star::awt::Toolkit erzeugt
220 void ImplSmartWindowCreated( Window* pNewWindow )
222 UNOWindowData* pParentUNOData = pNewWindow->GetParent() ?
223 pNewWindow->GetParent()->GetUNOData() : NULL;
225 if ( pParentUNOData && pParentUNOData->GetListeners( EL_CONTAINER ) )
227 UNOWindowData* pUNOData = pNewWindow->GetUNOData();
229 pUNOData = ImplSmartCreateUNOData( pNewWindow );
231 ::com::sun::star::awt::VclContainerEvent aEvent;
232 aEvent.Source = (UsrObject*)pParentUNOData->GetWindowPeer();
233 aEvent.Id = VCLCOMPONENT_ADDED;
234 aEvent.Child = (UsrObject*)pUNOData->GetWindowPeer();
236 EventList* pLst = pParentUNOData->GetListeners( EL_CONTAINER );
237 for ( sal_uInt32 n = 0; n < pLst->Count(); n++ )
239 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > * pRef = pLst->GetObject( n );
240 ((::com::sun::star::awt::XVclContainerListener*)(::com::sun::star::lang::XEventListener*)*pRef)->windowAdded( aEvent );
246 sal_Bool
lcl_ImplIsParent( Window
* pParentWindow
, Window
* pPossibleChild
)
248 Window
* pWindow
= ( pPossibleChild
!= pParentWindow
) ? pPossibleChild
: NULL
;
249 while ( pWindow
&& ( pWindow
!= pParentWindow
) )
250 pWindow
= pWindow
->GetParent();
252 return pWindow
? sal_True
: sal_False
;
255 void UnoWrapper::WindowDestroyed( Window
* pWindow
)
257 // ggf. existieren noch von ::com::sun::star::loader::Java erzeugte Childs, die sonst erst
258 // im Garbage-Collector zerstoert werden...
259 Window
* pChild
= pWindow
->GetWindow( WINDOW_FIRSTCHILD
);
262 Window
* pNextChild
= pChild
->GetWindow( WINDOW_NEXT
);
264 Window
* pClient
= pChild
->GetWindow( WINDOW_CLIENT
);
265 if ( pClient
->GetWindowPeer() )
267 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> xComp( pClient
->GetComponentInterface( FALSE
), ::com::sun::star::uno::UNO_QUERY
);
274 // ::com::sun::star::chaos::System-Windows suchen...
275 Window
* pOverlap
= pWindow
->GetWindow( WINDOW_OVERLAP
);
276 pOverlap
= pOverlap
->GetWindow( WINDOW_FIRSTOVERLAP
);
279 Window
* pNextOverlap
= pOverlap
->GetWindow( WINDOW_NEXT
);
280 Window
* pClient
= pOverlap
->GetWindow( WINDOW_CLIENT
);
282 if ( pClient
->GetWindowPeer() && lcl_ImplIsParent( pWindow
, pClient
) )
284 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> xComp( pClient
->GetComponentInterface( FALSE
), ::com::sun::star::uno::UNO_QUERY
);
288 pOverlap
= pNextOverlap
;
291 Window
* pParent
= pWindow
->GetParent();
292 if ( pParent
&& pParent
->GetWindowPeer() )
293 pParent
->GetWindowPeer()->notifyWindowRemoved( *pWindow
);
295 if ( pWindow
&& pWindow
->GetWindowPeer() )
297 pWindow
->GetWindowPeer()->SetWindow( NULL
);
298 pWindow
->SetWindowPeer( NULL
, NULL
);
301 // #102132# Iterate over frames after setting Window peer to NULL,
302 // because while destroying other frames, we get get into the method again and try
303 // to destroy this window again...
304 // #i42462#/#116855# no, don't loop: Instead, just ensure that all our top-window-children
305 // are disposed, too (which should also be a valid fix for #102132#, but doesn't have the extreme
306 // performance penalties)
309 Window
* pTopWindowChild
= pWindow
->GetWindow( WINDOW_FIRSTTOPWINDOWCHILD
);
310 while ( pTopWindowChild
)
312 OSL_ENSURE( pTopWindowChild
->GetParent() == pWindow
, "UnoWrapper::WindowDestroyed: inconsistency in the SystemWindow relationship!" );
314 uno::Reference
< lang::XComponent
> xComp( pTopWindowChild
->GetComponentInterface( FALSE
), uno::UNO_QUERY
);
315 pTopWindowChild
= pTopWindowChild
->GetWindow( WINDOW_NEXTTOPWINDOWSIBLING
);
322 // ----------------------------------------------------------------------------
323 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
> UnoWrapper::CreateAccessible( Menu
* pMenu
, sal_Bool bIsMenuBar
)
325 return maAccessibleFactoryAccess
.getFactory().createAccessible( pMenu
, bIsMenuBar
);