xref: /aoo41x/main/toolkit/source/awt/vclxtoolkit.cxx (revision ff7e158d)
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_toolkit.hxx"
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 
28 #ifndef _SVWIN_HXX
29 #include <tools/svwin.h>
30 #endif
31 #include <stdio.h>
32 #include <com/sun/star/awt/ImageScaleMode.hpp>
33 #include <com/sun/star/awt/WindowAttribute.hpp>
34 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
35 #include <com/sun/star/awt/WindowClass.hpp>
36 #include <com/sun/star/awt/MessageBoxButtons.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/lang/SystemDependent.hpp>
39 #include <com/sun/star/awt/FocusEvent.hpp>
40 #include <com/sun/star/awt/KeyEvent.hpp>
41 #include <com/sun/star/awt/KeyModifier.hpp>
42 #include <com/sun/star/lang/EventObject.hpp>
43 #include <com/sun/star/uno/Reference.hxx>
44 #include <com/sun/star/uno/Sequence.hxx>
45 #include <com/sun/star/uno/XInterface.hpp>
46 #include <com/sun/star/beans/NamedValue.hpp>
47 #include <cppuhelper/typeprovider.hxx>
48 #include <osl/conditn.hxx>
49 #include <rtl/memory.h>
50 #include <rtl/uuid.h>
51 #include <rtl/process.h>
52 
53 #ifdef WNT
54 #include <tools/prewin.h>
55 #include <windows.h>
56 #include <tools/postwin.h>
57 #elif defined ( OS2 )
58 #include <svpm.h>
59 #elif (defined QUARTZ)
60 #include "premac.h"
61 #include <Cocoa/Cocoa.h>
62 #include "postmac.h"
63 #endif
64 #include <vcl/sysdata.hxx>
65 
66 #include <toolkit/awt/vclxwindows.hxx>
67 #include <toolkit/awt/vclxsystemdependentwindow.hxx>
68 #include <toolkit/awt/vclxregion.hxx>
69 #include <toolkit/awt/vclxtoolkit.hxx>
70 #include <toolkit/awt/vclxtabpagecontainer.hxx>
71 #include <toolkit/awt/vclxtabpagemodel.hxx>
72 
73 #include <toolkit/awt/xsimpleanimation.hxx>
74 #include <toolkit/awt/xthrobber.hxx>
75 #include <toolkit/awt/animatedimagespeer.hxx>
76 #include <toolkit/awt/vclxtopwindow.hxx>
77 #include <toolkit/awt/vclxwindow.hxx>
78 #include <toolkit/helper/vclunohelper.hxx>
79 #include <toolkit/helper/unowrapper.hxx>
80 #include <toolkit/helper/servicenames.hxx>
81 
82 
83 #include <toolkit/helper/macros.hxx>
84 #include <toolkit/helper/convert.hxx>
85 #include <vcl/unohelp.hxx>
86 #include <vcl/btndlg.hxx>
87 #ifndef _SV_BUTTON_HXX
88 #include <vcl/button.hxx>
89 #endif
90 #include <vcl/combobox.hxx>
91 #include <vcl/ctrl.hxx>
92 #include <vcl/dialog.hxx>
93 #include <vcl/dockingarea.hxx>
94 #include <vcl/dockwin.hxx>
95 #include <vcl/edit.hxx>
96 #include <vcl/field.hxx>
97 #include <vcl/fixed.hxx>
98 #include <vcl/floatwin.hxx>
99 #include <vcl/group.hxx>
100 #include <vcl/imgctrl.hxx>
101 #include <vcl/longcurr.hxx>
102 #include <vcl/lstbox.hxx>
103 #include <vcl/menubtn.hxx>
104 #include <vcl/morebtn.hxx>
105 #include <vcl/msgbox.hxx>
106 #include <vcl/scrbar.hxx>
107 #include <vcl/spin.hxx>
108 #include <vcl/split.hxx>
109 #include <vcl/splitwin.hxx>
110 #include <vcl/status.hxx>
111 #include <vcl/svapp.hxx>
112 #include <vcl/syschild.hxx>
113 #include <vcl/tabctrl.hxx>
114 #include <vcl/tabdlg.hxx>
115 #include <vcl/tabpage.hxx>
116 #include <vcl/toolbox.hxx>
117 #include <vcl/virdev.hxx>
118 #include <vcl/window.hxx>
119 #include <vcl/wrkwin.hxx>
120 #include <vcl/throbber.hxx>
121 #include "toolkit/awt/vclxspinbutton.hxx"
122 
123 #include <tools/debug.hxx>
124 #include <comphelper/processfactory.hxx>
125 
126 namespace css = ::com::sun::star;
127 
128 #define VCLWINDOW_FRAMEWINDOW				0x1000
129 #define VCLWINDOW_SYSTEMCHILDWINDOW			0x1001
130 
131 #if (defined WNT)
132 #define SYSTEM_DEPENDENT_TYPE ::com::sun::star::lang::SystemDependent::SYSTEM_WIN32
133 #elif (defined OS2)
134 #define SYSTEM_DEPENDENT_TYPE ::com::sun::star::lang::SystemDependent::SYSTEM_OS2
135 #elif (defined QUARTZ)
136 #define SYSTEM_DEPENDENT_TYPE ::com::sun::star::lang::SystemDependent::SYSTEM_MAC
137 #elif (defined UNX)
138 #define SYSTEM_DEPENDENT_TYPE ::com::sun::star::lang::SystemDependent::SYSTEM_XWINDOW
139 #endif
140 
141 TOOLKIT_DLLPUBLIC WinBits ImplGetWinBits( sal_uInt32 nComponentAttribs, sal_uInt16 nCompType )
142 {
143 	WinBits nWinBits = 0;
144 
145 	sal_Bool bMessBox = sal_False;
146 	if ( ( nCompType == WINDOW_INFOBOX ) ||
147 		 ( nCompType == WINDOW_MESSBOX ) ||
148 		 ( nCompType == WINDOW_QUERYBOX ) ||
149 		 ( nCompType == WINDOW_WARNINGBOX ) ||
150 		 ( nCompType == WINDOW_ERRORBOX ) )
151 	{
152 		bMessBox = sal_True;
153 	}
154 
155     bool bDecoratedWindow = false;
156     if  (   bMessBox
157         ||  ( nCompType == WINDOW_DIALOG )
158         ||  ( nCompType == WINDOW_MODELESSDIALOG )
159         ||  ( nCompType == WINDOW_MODALDIALOG )
160         ||  ( nCompType == WINDOW_SYSTEMDIALOG )
161         ||  ( nCompType == WINDOW_PATHDIALOG )
162         ||  ( nCompType == WINDOW_FILEDIALOG )
163         ||  ( nCompType == WINDOW_PRINTERSETUPDIALOG )
164         ||  ( nCompType == WINDOW_PRINTDIALOG )
165         ||  ( nCompType == WINDOW_COLORDIALOG )
166         ||  ( nCompType == WINDOW_FONTDIALOG )
167         ||  ( nCompType == WINDOW_DOCKINGWINDOW )
168         ||  ( nCompType == WINDOW_TABDIALOG )
169         ||  ( nCompType == WINDOW_BUTTONDIALOG )
170         ||  ( nCompType == WINDOW_SYSTEMCHILDWINDOW )
171         )
172     {
173         bDecoratedWindow = true;
174     }
175 
176     if( nComponentAttribs & ::com::sun::star::awt::WindowAttribute::BORDER )
177 		nWinBits |= WB_BORDER;
178 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::NOBORDER )
179 		nWinBits |= WB_NOBORDER;
180 	if( nComponentAttribs & ::com::sun::star::awt::WindowAttribute::SIZEABLE )
181 		nWinBits |= WB_SIZEABLE;
182 	if( nComponentAttribs & ::com::sun::star::awt::WindowAttribute::MOVEABLE )
183 		nWinBits |= WB_MOVEABLE;
184 	if( nComponentAttribs & ::com::sun::star::awt::WindowAttribute::CLOSEABLE )
185 		nWinBits |= WB_CLOSEABLE;
186 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::HSCROLL )
187 		nWinBits |= WB_HSCROLL;
188 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::VSCROLL )
189 		nWinBits |= WB_VSCROLL;
190 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::LEFT )
191 		nWinBits |= WB_LEFT;
192 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::CENTER )
193 		nWinBits |= WB_CENTER;
194 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::RIGHT )
195 		nWinBits |= WB_RIGHT;
196 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::SPIN )
197 		nWinBits |= WB_SPIN;
198 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::SORT )
199 		nWinBits |= WB_SORT;
200 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DROPDOWN )
201 		nWinBits |= WB_DROPDOWN;
202 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEFBUTTON )
203 		nWinBits |= WB_DEFBUTTON;
204 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::READONLY )
205 		nWinBits |= WB_READONLY;
206 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::CLIPCHILDREN )
207 		nWinBits |= WB_CLIPCHILDREN;
208 	if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::GROUP )
209 		nWinBits |= WB_GROUP;
210     if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::NOLABEL ) //added for issue79712
211 		nWinBits |= WB_NOLABEL;
212 
213 	// These bits are not uniqe
214 	if ( bMessBox )
215 	{
216 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::OK )
217 			nWinBits |= WB_OK;
218 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::OK_CANCEL )
219 			nWinBits |= WB_OK_CANCEL;
220 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::YES_NO )
221 			nWinBits |= WB_YES_NO;
222 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::YES_NO_CANCEL )
223 			nWinBits |= WB_YES_NO_CANCEL;
224 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::RETRY_CANCEL )
225 			nWinBits |= WB_RETRY_CANCEL;
226 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEF_OK )
227 			nWinBits |= WB_DEF_OK;
228 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEF_CANCEL )
229 			nWinBits |= WB_DEF_CANCEL;
230 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEF_RETRY )
231 			nWinBits |= WB_DEF_RETRY;
232 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEF_YES )
233 			nWinBits |= WB_DEF_YES;
234 		if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::DEF_NO )
235 			nWinBits |= WB_DEF_NO;
236 	}
237     if ( nCompType == WINDOW_MULTILINEEDIT )
238     {
239         if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::AUTOHSCROLL )
240             nWinBits |= WB_AUTOHSCROLL;
241         if( nComponentAttribs & ::com::sun::star::awt::VclWindowPeerAttribute::AUTOVSCROLL )
242             nWinBits |= WB_AUTOVSCROLL;
243     }
244 
245 
246     if ( bDecoratedWindow )
247     {
248         if( nComponentAttribs & ::com::sun::star::awt::WindowAttribute::NODECORATION )
249         {
250             // No decoration removes several window attributes and must
251             // set WB_NOBORDER!
252             nWinBits &= ~WB_BORDER;
253 	        nWinBits &= ~WB_SIZEABLE;
254 	        nWinBits &= ~WB_MOVEABLE;
255 	        nWinBits &= ~WB_CLOSEABLE;
256             nWinBits |= WB_NOBORDER;
257         }
258     }
259 
260 	return nWinBits;
261 }
262 
263 struct ComponentInfo
264 {
265 	const char*		pName;
266 	WindowType		nWinType;
267 };
268 
269 static ComponentInfo __FAR_DATA aComponentInfos [] =
270 {
271 	{ "buttondialog", 		WINDOW_BUTTONDIALOG },
272 	{ "cancelbutton",		WINDOW_CANCELBUTTON },
273 	{ "checkbox",			WINDOW_CHECKBOX },
274 	{ "combobox",			WINDOW_COMBOBOX },
275 	{ "control",			WINDOW_CONTROL },
276 	{ "currencybox",		WINDOW_CURRENCYBOX },
277 	{ "currencyfield",		WINDOW_CURRENCYFIELD },
278 	{ "datebox",			WINDOW_DATEBOX },
279 	{ "datefield",			WINDOW_DATEFIELD },
280 	{ "dialog",				WINDOW_DIALOG },
281 	{ "dockingarea",		WINDOW_DOCKINGAREA },
282 	{ "dockingwindow",		WINDOW_DOCKINGWINDOW },
283 	{ "edit",				WINDOW_EDIT },
284 	{ "errorbox",			WINDOW_ERRORBOX },
285 	{ "fixedbitmap",		WINDOW_FIXEDBITMAP },
286 	{ "fixedimage",			WINDOW_FIXEDIMAGE },
287 	{ "fixedline",			WINDOW_FIXEDLINE },
288 	{ "fixedtext",			WINDOW_FIXEDTEXT },
289 	{ "floatingwindow",		WINDOW_FLOATINGWINDOW },
290 	{ "framewindow",		VCLWINDOW_FRAMEWINDOW },
291 	{ "groupbox",			WINDOW_GROUPBOX },
292 	{ "helpbutton",			WINDOW_HELPBUTTON },
293 	{ "imagebutton",		WINDOW_IMAGEBUTTON },
294 	{ "imageradiobutton",	WINDOW_IMAGERADIOBUTTON },
295 	{ "infobox",			WINDOW_INFOBOX },
296 	{ "listbox",			WINDOW_LISTBOX },
297 	{ "longcurrencybox",	WINDOW_LONGCURRENCYBOX },
298 	{ "longcurrencyfield",	WINDOW_LONGCURRENCYFIELD },
299 	{ "menubutton",			WINDOW_MENUBUTTON },
300 	{ "messbox",			WINDOW_MESSBOX },
301 	{ "metricbox",			WINDOW_METRICBOX },
302 	{ "metricfield",		WINDOW_METRICFIELD },
303 	{ "modaldialog",		WINDOW_MODALDIALOG },
304 	{ "modelessdialog",		WINDOW_MODELESSDIALOG },
305 	{ "morebutton",			WINDOW_MOREBUTTON },
306     { "multilineedit",		WINDOW_MULTILINEEDIT },
307 	{ "multilistbox",		WINDOW_MULTILISTBOX },
308 	{ "numericbox",			WINDOW_NUMERICBOX },
309 	{ "numericfield",		WINDOW_NUMERICFIELD },
310 	{ "okbutton",			WINDOW_OKBUTTON },
311 	{ "patternbox",			WINDOW_PATTERNBOX },
312 	{ "patternfield",		WINDOW_PATTERNFIELD },
313 	{ "pushbutton",			WINDOW_PUSHBUTTON },
314 	{ "querybox",			WINDOW_QUERYBOX },
315 	{ "radiobutton",		WINDOW_RADIOBUTTON },
316 	{ "scrollbar",			WINDOW_SCROLLBAR },
317 	{ "scrollbarbox",		WINDOW_SCROLLBARBOX },
318     { "simpleanimation",	WINDOW_CONTROL },
319     { "animatedimages",     WINDOW_CONTROL },
320 	{ "spinbutton",			WINDOW_SPINBUTTON },
321 	{ "spinfield",			WINDOW_SPINFIELD },
322     { "throbber",        	WINDOW_CONTROL },
323 	{ "splitter",			WINDOW_SPLITTER },
324 	{ "splitwindow",		WINDOW_SPLITWINDOW },
325 	{ "statusbar",			WINDOW_STATUSBAR },
326 	{ "systemchildwindow",	VCLWINDOW_SYSTEMCHILDWINDOW },
327 	{ "tabcontrol",			WINDOW_TABCONTROL },
328 	{ "tabdialog",			WINDOW_TABDIALOG },
329 	{ "tabpage",			WINDOW_TABPAGE },
330 	{ "timebox",			WINDOW_TIMEBOX },
331 	{ "timefield",			WINDOW_TIMEFIELD },
332 	{ "toolbox",			WINDOW_TOOLBOX },
333 	{ "tristatebox",		WINDOW_TRISTATEBOX },
334 	{ "warningbox",			WINDOW_WARNINGBOX },
335 	{ "window",				WINDOW_WINDOW },
336 	{ "workwindow",			WINDOW_WORKWINDOW },
337 	{ "tabpagecontainer",	WINDOW_CONTROL },
338 	{ "tabpagemodel",		WINDOW_TABPAGE }
339 };
340 
341 extern "C"
342 {
343 static int
344 #if defined( WNT )
345  __cdecl
346 #endif
347 #if defined( ICC ) && defined( OS2 )
348 _Optlink
349 #endif
350  	ComponentInfoCompare( const void* pFirst, const void* pSecond)
351 {
352 	return( strcmp( ((ComponentInfo*)pFirst)->pName,
353 		    		((ComponentInfo*)pSecond)->pName ) );
354 }
355 }
356 
357 sal_uInt16 ImplGetComponentType( const String& rServiceName )
358 {
359 	static sal_Bool bSorted = sal_False;
360 	if( !bSorted )
361 	{
362 		qsort( 	(void*) aComponentInfos,
363 				sizeof( aComponentInfos ) / sizeof( ComponentInfo ),
364 				sizeof( ComponentInfo ),
365 				ComponentInfoCompare );
366 		bSorted = sal_True;
367 	}
368 
369 
370 	ComponentInfo aSearch;
371 	ByteString aServiceName( rServiceName, gsl_getSystemTextEncoding() );
372 	aServiceName.ToLowerAscii();
373 	if ( aServiceName.Len() )
374 		aSearch.pName = aServiceName.GetBuffer();
375 	else
376 		aSearch.pName = "window";
377 
378 	ComponentInfo* pInf = (ComponentInfo*) bsearch( &aSearch,
379 						(void*) aComponentInfos,
380 						sizeof( aComponentInfos ) / sizeof( ComponentInfo ),
381 						sizeof( ComponentInfo ),
382 						ComponentInfoCompare );
383 
384 	return pInf ? pInf->nWinType : 0;
385 }
386 
387 
388 //	----------------------------------------------------
389 //	class VCLXToolkit
390 //	----------------------------------------------------
391 
392 static sal_Int32							nVCLToolkitInstanceCount = 0;
393 static sal_Bool									bInitedByVCLToolkit = sal_False;
394 //static cppu::OInterfaceContainerHelper *	pToolkits = 0;
395 
396 static osl::Mutex & getInitMutex()
397 {
398 	static osl::Mutex * pM;
399 	if( !pM )
400 	{
401 		osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
402 		if( !pM )
403 		{
404 			static osl::Mutex aMutex;
405 			pM = &aMutex;
406 		}
407 	}
408 	return *pM;
409 }
410 
411 static osl::Condition & getInitCondition()
412 {
413 	static osl::Condition * pC = 0;
414 	if( !pC )
415 	{
416 		osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
417 		if( !pC )
418 		{
419 			static osl::Condition aCondition;
420 			pC = &aCondition;
421 		}
422 	}
423 	return *pC;
424 }
425 
426 struct ToolkitThreadData
427 {
428 	VCLXToolkit * pTk;
429 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
430 
431 	ToolkitThreadData( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & rSMgr, VCLXToolkit * pTk_ )
432 		: pTk( pTk_ )
433 		, xSMgr( rSMgr )
434 	{
435 	}
436 };
437 
438 extern "C"
439 {
440 static void SAL_CALL ToolkitWorkerFunction( void* pArgs )
441 {
442 	ToolkitThreadData * pTTD = (ToolkitThreadData *)pArgs;
443 	bInitedByVCLToolkit = InitVCL( pTTD->xSMgr );
444 	if( bInitedByVCLToolkit )
445 	{
446 		UnoWrapper* pUnoWrapper = new UnoWrapper( pTTD->pTk );
447 		Application::SetUnoWrapper( pUnoWrapper );
448 	}
449 	getInitCondition().set();
450 	if( bInitedByVCLToolkit )
451 	{
452 		{
453 		osl::Guard< vos::IMutex > aGuard( Application::GetSolarMutex() );
454 		Application::Execute();
455 		}
456 		try
457 		{
458 			pTTD->pTk->dispose();
459 		}
460 		catch( com::sun::star::uno::Exception & )
461 		{
462 		}
463 		/*
464 		if( pToolkits )
465 		{
466 			cppu::OInterfaceIteratorHelper aIt( *pToolkits );
467 			::com::sun::star::uno::XInterface *	pI;
468 			while( pI = aIt.next() )
469 				((::com::sun::star::lang::XComponent *)pI)->dispose();
470 
471 			// delete toolkit container
472 			osl::Guard< osl::Mutex > aGuard( getInitMutex() );
473 			delete pToolkits;
474 			pToolkits = 0;
475 		}
476 		*/
477 		DeInitVCL();
478 	}
479     else
480     {
481         JoinMainLoopThread();
482     }
483 	delete pTTD;
484 }
485 }
486 
487 // contructor, which might initialize VCL
488 VCLXToolkit::VCLXToolkit( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & rSMgr ):
489     cppu::WeakComponentImplHelper7<
490     ::com::sun::star::awt::XToolkit,
491     ::com::sun::star::lang::XServiceInfo,
492     ::com::sun::star::awt::XSystemChildFactory,
493     ::com::sun::star::awt::XMessageBoxFactory,
494     ::com::sun::star::awt::XDataTransferProviderAccess,
495     ::com::sun::star::awt::XExtendedToolkit,
496     ::com::sun::star::awt::XReschedule>( GetMutex() ),
497     m_aTopWindowListeners(rBHelper.rMutex),
498     m_aKeyHandlers(rBHelper.rMutex),
499     m_aFocusListeners(rBHelper.rMutex),
500     m_aEventListenerLink(LINK(this, VCLXToolkit, eventListenerHandler)),
501     m_aKeyListenerLink(LINK(this, VCLXToolkit, keyListenerHandler)),
502     m_bEventListener(false),
503     m_bKeyListener(false)
504 {
505 	hSvToolsLib = NULL;
506 	fnSvtCreateWindow = NULL;
507 
508 	osl::Guard< osl::Mutex > aGuard( getInitMutex() );
509     nVCLToolkitInstanceCount++;
510     if( ( nVCLToolkitInstanceCount == 1 ) && ( !Application::IsInMain() ) )
511 	{
512 		// setup execute thread
513 		CreateMainLoopThread( ToolkitWorkerFunction, new ToolkitThreadData( rSMgr, this ) );
514 		getInitCondition().wait();
515 		/*
516 		if( bInitedByVCLToolkit )
517 		{
518 			// insert in disposing list
519 			if( !pToolkits )
520 				pToolkits = new cppu::OInterfaceContainerHelper( getInitMutex() );
521 			pToolkits->addInterface( (::com::sun::star::lang::XComponent *)this );
522 		}
523 		*/
524 	}
525 }
526 
527 VCLXToolkit::~VCLXToolkit()
528 {
529 }
530 
531 
532 void SAL_CALL VCLXToolkit::disposing()
533 {
534 	if ( hSvToolsLib )
535 	{
536 		osl_unloadModule( hSvToolsLib );
537 		hSvToolsLib = NULL;
538 		fnSvtCreateWindow = NULL;
539 	}
540 
541     {
542         osl::Guard< osl::Mutex > aGuard( getInitMutex() );
543         if( --nVCLToolkitInstanceCount == 0 )
544         {
545             if( bInitedByVCLToolkit )
546             {
547                 Application::Quit();
548                 JoinMainLoopThread();
549                 bInitedByVCLToolkit = sal_False;
550             }
551         }
552     }
553 
554     if (m_bEventListener)
555     {
556         ::Application::RemoveEventListener(m_aEventListenerLink);
557         m_bEventListener = false;
558     }
559     if (m_bKeyListener)
560     {
561         ::Application::RemoveKeyListener(m_aKeyListenerLink);
562         m_bKeyListener = false;
563     }
564     ::css::lang::EventObject aEvent(
565         static_cast< ::cppu::OWeakObject * >(this));
566     m_aTopWindowListeners.disposeAndClear(aEvent);
567     m_aKeyHandlers.disposeAndClear(aEvent);
568     m_aFocusListeners.disposeAndClear(aEvent);
569 
570 /*
571 	osl::Guard< osl::Mutex > aGuard( getInitMutex() );
572 	// insert in disposing list
573 	if( pToolkits )
574 	{
575 		// remove from the disposing list
576 		pToolkits->removeInterface( (::com::sun::star::lang::XComponent *)this );
577 	}
578 */
579 }
580 
581 
582 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > VCLXToolkit::getDesktopWindow(  ) throw(::com::sun::star::uno::RuntimeException)
583 {
584 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > xRef;
585 	// 07/00: AppWindow doesn't exist anymore...
586 	return xRef;
587 }
588 
589 ::com::sun::star::awt::Rectangle VCLXToolkit::getWorkArea(  ) throw(::com::sun::star::uno::RuntimeException)
590 {
591 	::com::sun::star::awt::Rectangle aRect;
592 	// 07/00: AppWindow doesn't exist anymore...
593 	return aRect;
594 }
595 
596 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > VCLXToolkit::createWindow( const ::com::sun::star::awt::WindowDescriptor& rDescriptor ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
597 {
598 //IAccessibility2 Implementation 2009-----
599 #ifdef WNT
600 	CEnableAccessInterface e;
601 #endif
602 //-----IAccessibility2 Implementation 2009
603     return ImplCreateWindow( rDescriptor, WinBits(0) );
604 }
605 
606 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXToolkit::createScreenCompatibleDevice( sal_Int32 Width, sal_Int32 Height ) throw(::com::sun::star::uno::RuntimeException)
607 {
608 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
609 
610 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > xRef;
611 	VCLXVirtualDevice* pVDev = new VCLXVirtualDevice;
612 
613 	osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
614 
615 	VirtualDevice* pV = new VirtualDevice;
616 	pV->SetOutputSizePixel( Size( Width, Height ) );
617 	pVDev->SetVirtualDevice( pV );
618 
619 	xRef = pVDev;
620 	return xRef;
621 }
622 
623 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XRegion > VCLXToolkit::createRegion(  ) throw(::com::sun::star::uno::RuntimeException)
624 {
625 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
626 
627 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XRegion >  xRef = new VCLXRegion;
628 	return xRef;
629 }
630 
631 Window*	VCLXToolkit::ImplCreateWindow( VCLXWindow** ppNewComp,
632 	const ::com::sun::star::awt::WindowDescriptor& rDescriptor,
633     Window* pParent, WinBits nWinBits )
634 {
635 	String aServiceName( rDescriptor.WindowServiceName );
636 	aServiceName.ToLowerAscii();
637 
638 	Window* pNewWindow = NULL;
639 	sal_uInt16 nType = ImplGetComponentType( aServiceName );
640 
641 	if ( !pParent )
642 	{
643 		// Wenn die Component einen Parent braucht, dann NULL zurueckgeben,
644 		// spaeter mal ::com::sun::star::uno::Exception...
645 		sal_Bool bException = sal_True;
646 		if  (   ( nType == WINDOW_DIALOG )
647             ||  ( nType == WINDOW_MODALDIALOG )
648             ||  ( nType == WINDOW_MODELESSDIALOG )
649             ||  ( nType == WINDOW_MESSBOX )
650             ||  ( nType == WINDOW_INFOBOX )
651             ||  ( nType == WINDOW_WARNINGBOX )
652             ||  ( nType == WINDOW_ERRORBOX )
653             ||  ( nType == WINDOW_QUERYBOX )
654             )
655 			bException = sal_False;
656 		else if ( ( nType == WINDOW_WINDOW ) ||
657 				  ( nType == WINDOW_WORKWINDOW ) ||
658 				  ( nType == VCLWINDOW_FRAMEWINDOW ) )
659 		{
660 			if ( rDescriptor.Type == ::com::sun::star::awt::WindowClass_TOP )
661 				bException = sal_False;
662 		}
663 
664 		if ( bException )
665 		{
666 			*ppNewComp = NULL;
667 			return NULL;
668 		}
669 	}
670 
671 	if ( nType )
672 	{
673 		vos::OGuard aVclGuard( Application::GetSolarMutex()  );
674 		switch ( (WindowType)nType )
675 		{
676 			case WINDOW_CANCELBUTTON:
677 				pNewWindow = new CancelButton( pParent, nWinBits );
678 				*ppNewComp = new VCLXButton;
679 			break;
680 			case WINDOW_CHECKBOX:
681 		 		pNewWindow = new CheckBox( pParent, nWinBits );
682 				*ppNewComp = new VCLXCheckBox;
683 			break;
684 			case WINDOW_COMBOBOX:
685 				pNewWindow = new ComboBox( pParent, nWinBits|WB_AUTOHSCROLL );
686 				((ComboBox*)pNewWindow)->EnableAutoSize( sal_False );
687 				*ppNewComp = new VCLXComboBox;
688 			break;
689 			case WINDOW_CURRENCYBOX:
690 				pNewWindow = new CurrencyBox( pParent, nWinBits );
691 			break;
692 			case WINDOW_CURRENCYFIELD:
693 				pNewWindow = new CurrencyField( pParent, nWinBits );
694 				static_cast<CurrencyField*>(pNewWindow)->EnableEmptyFieldValue( sal_True );
695 				*ppNewComp = new VCLXNumericField;
696 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(CurrencyField*)pNewWindow );
697 			break;
698 			case WINDOW_DATEBOX:
699 				pNewWindow = new DateBox( pParent, nWinBits );
700 			break;
701 			case WINDOW_DATEFIELD:
702 				pNewWindow = new DateField( pParent, nWinBits );
703 				static_cast<DateField*>(pNewWindow)->EnableEmptyFieldValue( sal_True );
704 				*ppNewComp = new VCLXDateField;
705 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(DateField*)pNewWindow );
706 			break;
707 			case WINDOW_DOCKINGAREA:
708 				pNewWindow = new DockingAreaWindow( pParent );
709 			break;
710 			case WINDOW_MULTILINEEDIT:
711 			case WINDOW_EDIT:
712 				pNewWindow = new Edit( pParent, nWinBits );
713 				*ppNewComp = new VCLXEdit;
714 			break;
715 			case WINDOW_ERRORBOX:
716 				pNewWindow = new ErrorBox( pParent, nWinBits, String() );
717 				*ppNewComp = new VCLXMessageBox;
718 			break;
719 			case WINDOW_FIXEDBITMAP:
720 				pNewWindow = new FixedBitmap( pParent, nWinBits );
721 			break;
722 			case WINDOW_FIXEDIMAGE:
723 				pNewWindow = new ImageControl( pParent, nWinBits );
724 				*ppNewComp = new VCLXImageControl;
725 			break;
726 			case WINDOW_FIXEDLINE:
727 				pNewWindow = new FixedLine( pParent, nWinBits );
728 			break;
729 			case WINDOW_FIXEDTEXT:
730 				pNewWindow = new FixedText( pParent, nWinBits );
731 				*ppNewComp = new VCLXFixedText;
732 			break;
733 			case WINDOW_FLOATINGWINDOW:
734 				pNewWindow = new FloatingWindow( pParent, nWinBits );
735 			break;
736 			case WINDOW_GROUPBOX:
737 				pNewWindow = new GroupBox( pParent, nWinBits );
738 			break;
739 			case WINDOW_HELPBUTTON:
740 				pNewWindow = new HelpButton( pParent, nWinBits );
741 				*ppNewComp = new VCLXButton;
742 			break;
743 			case WINDOW_IMAGEBUTTON:
744  				pNewWindow = new ImageButton( pParent, nWinBits );
745 				*ppNewComp = new VCLXButton;
746 			break;
747 			case WINDOW_IMAGERADIOBUTTON:
748 				pNewWindow = new ImageRadioButton( pParent, nWinBits );
749 				*ppNewComp = new VCLXButton;
750 			break;
751 			case WINDOW_INFOBOX:
752 				pNewWindow = new InfoBox( pParent, String() );
753 				*ppNewComp = new VCLXMessageBox;
754 			break;
755 			case WINDOW_LISTBOX:
756 				pNewWindow = new ListBox( pParent, nWinBits|WB_SIMPLEMODE|WB_AUTOHSCROLL );
757 				((ListBox*)pNewWindow)->EnableAutoSize( sal_False );
758 				*ppNewComp = new VCLXListBox;
759 			break;
760 			case WINDOW_LONGCURRENCYBOX:
761 				pNewWindow = new LongCurrencyBox( pParent, nWinBits );
762 			break;
763 			case WINDOW_LONGCURRENCYFIELD:
764 				pNewWindow = new LongCurrencyField( pParent, nWinBits );
765 				*ppNewComp = new VCLXCurrencyField;
766 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(LongCurrencyField*)pNewWindow );
767 			break;
768 			case WINDOW_MENUBUTTON:
769 				pNewWindow = new MenuButton( pParent, nWinBits );
770 				*ppNewComp = new VCLXButton;
771 			break;
772 			case WINDOW_MESSBOX:
773 				pNewWindow = new MessBox( pParent, nWinBits, String(), String() );
774 				*ppNewComp = new VCLXMessageBox;
775 			break;
776 			case WINDOW_METRICBOX:
777 				pNewWindow = new MetricBox( pParent, nWinBits );
778 			break;
779 			case WINDOW_METRICFIELD:
780 				pNewWindow = new MetricField( pParent, nWinBits );
781 				*ppNewComp = new VCLXMetricField;
782 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(MetricField*)pNewWindow );
783 			break;
784 			case WINDOW_DIALOG:
785 			case WINDOW_MODALDIALOG:
786 			case WINDOW_MODELESSDIALOG:
787             {
788 				// Modal/Modeless nur durch Show/Execute
789 				if ( (pParent == NULL ) && ( rDescriptor.ParentIndex == -1 ) )
790 					pParent = DIALOG_NO_PARENT;
791 				pNewWindow = new Dialog( pParent, nWinBits );
792 				// #i70217# Don't always create a new component object. It's possible that VCL has called
793 				// GetComponentInterface( sal_True ) in the Dialog ctor itself (see Window::IsTopWindow() )
794 				// which creates a component object.
795 				css::uno::Reference< css::awt::XWindowPeer > xWinPeer = pNewWindow->GetComponentInterface( sal_False );
796 				if ( xWinPeer.is() )
797 					*ppNewComp = dynamic_cast< VCLXDialog* >( xWinPeer.get() );
798 				else
799 				    *ppNewComp = new VCLXDialog;
800             }
801 			break;
802 			case WINDOW_MOREBUTTON:
803 				pNewWindow = new MoreButton( pParent, nWinBits );
804 				*ppNewComp = new VCLXButton;
805 			break;
806 			case WINDOW_MULTILISTBOX:
807 				pNewWindow = new MultiListBox( pParent, nWinBits );
808 				*ppNewComp = new VCLXListBox;
809 			break;
810 			case WINDOW_NUMERICBOX:
811 				pNewWindow = new NumericBox( pParent, nWinBits );
812 			break;
813 			case WINDOW_NUMERICFIELD:
814 				pNewWindow = new NumericField( pParent, nWinBits );
815 				static_cast<NumericField*>(pNewWindow)->EnableEmptyFieldValue( sal_True );
816 				*ppNewComp = new VCLXNumericField;
817 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(NumericField*)pNewWindow );
818 			break;
819 			case WINDOW_OKBUTTON:
820 				pNewWindow = new OKButton( pParent, nWinBits );
821 				*ppNewComp = new VCLXButton;
822 			break;
823 			case WINDOW_PATTERNBOX:
824 				pNewWindow = new PatternBox( pParent, nWinBits );
825 			break;
826 			case WINDOW_PATTERNFIELD:
827 				pNewWindow = new PatternField( pParent, nWinBits );
828 				*ppNewComp = new VCLXPatternField;
829 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(PatternField*)pNewWindow );
830 			break;
831 			case WINDOW_PUSHBUTTON:
832 				pNewWindow = new PushButton( pParent, nWinBits );
833 				*ppNewComp = new VCLXButton;
834 			break;
835 			case WINDOW_QUERYBOX:
836 				pNewWindow = new QueryBox( pParent, nWinBits, String() );
837 				*ppNewComp = new VCLXMessageBox;
838 			break;
839 			case WINDOW_RADIOBUTTON:
840 				pNewWindow = new RadioButton( pParent, nWinBits );
841 				*ppNewComp = new VCLXRadioButton;
842 
843 				// by default, disable RadioCheck
844 				// Since the VCLXRadioButton really cares for it's RadioCheck settings, this is important:
845 				// if we enable it, the VCLXRadioButton will use RadioButton::Check instead of RadioButton::SetState
846 				// This leads to a strange behaviour if the control is newly created: when settings the initial
847 				// state to "checked", the RadioButton::Check (called because RadioCheck=sal_True) will uncheck
848 				// _all_other_ radio buttons in the same group. However, at this moment the grouping of the controls
849 				// is not really valid: the controls are grouped after they have been created, but we're still in
850 				// the creation process, so the RadioButton::Check relies on invalid grouping information.
851 				// 07.08.2001 - #87254# - frank.schoenheit@sun.com
852 				static_cast<RadioButton*>(pNewWindow)->EnableRadioCheck( sal_False );
853 			break;
854 			case WINDOW_SCROLLBAR:
855 				pNewWindow = new ScrollBar( pParent, nWinBits );
856 				*ppNewComp = new VCLXScrollBar;
857 			break;
858 			case WINDOW_SCROLLBARBOX:
859 				pNewWindow = new ScrollBarBox( pParent, nWinBits );
860 			break;
861 			case WINDOW_SPINBUTTON:
862 				pNewWindow = new SpinButton( pParent, nWinBits );
863                 *ppNewComp = new ::toolkit::VCLXSpinButton;
864 			break;
865 			case WINDOW_SPINFIELD:
866 				pNewWindow = new SpinField( pParent, nWinBits );
867 				*ppNewComp = new VCLXNumericField;
868 			break;
869 			case WINDOW_SPLITTER:
870 				pNewWindow = new Splitter( pParent, nWinBits );
871 			break;
872 			case WINDOW_SPLITWINDOW:
873 				pNewWindow = new SplitWindow( pParent, nWinBits );
874 			break;
875 			case WINDOW_STATUSBAR:
876 				pNewWindow = new StatusBar( pParent, nWinBits );
877 			break;
878 			case VCLWINDOW_SYSTEMCHILDWINDOW:
879 				pNewWindow = new SystemChildWindow( pParent, nWinBits );
880 				*ppNewComp = new VCLXSystemDependentWindow();
881 			break;
882 			case WINDOW_TABCONTROL:
883 				pNewWindow = new TabControl( pParent, nWinBits );
884                 *ppNewComp = new VCLXTabPageContainer;
885 			break;
886 			case WINDOW_TABDIALOG:
887 				pNewWindow = new TabDialog( pParent, nWinBits );
888 			break;
889 			case WINDOW_TABPAGE:
890                 /*
891 				if ( rDescriptor.WindowServiceName.equalsIgnoreAsciiCase(
892                         ::rtl::OUString::createFromAscii("tabpagemodel") ) )
893                 {
894                     pNewWindow = new TabControl( pParent, nWinBits );
895                     *ppNewComp = new VCLXTabPageContainer;
896                 }
897 				else
898                 */
899 				{
900 					pNewWindow = new TabPage( pParent, nWinBits );
901 					*ppNewComp = new VCLXTabPage;
902 				}
903 			break;
904 			case WINDOW_TIMEBOX:
905 				pNewWindow = new TimeBox( pParent, nWinBits );
906 			break;
907 			case WINDOW_TIMEFIELD:
908 				pNewWindow = new TimeField( pParent, nWinBits );
909 				static_cast<TimeField*>(pNewWindow)->EnableEmptyFieldValue( sal_True );
910 				*ppNewComp = new VCLXTimeField;
911 				((VCLXFormattedSpinField*)*ppNewComp)->SetFormatter( (FormatterBase*)(TimeField*)pNewWindow );
912 			break;
913 			case WINDOW_TOOLBOX:
914 				pNewWindow = new ToolBox( pParent, nWinBits );
915 				*ppNewComp = new VCLXToolBox;
916 			break;
917 			case WINDOW_TRISTATEBOX:
918 				pNewWindow = new TriStateBox( pParent, nWinBits );
919 			break;
920 			case WINDOW_WARNINGBOX:
921 				pNewWindow = new WarningBox( pParent, nWinBits, String() );
922 				*ppNewComp = new VCLXMessageBox;
923 			break;
924 			case WINDOW_WORKWINDOW:
925 			case WINDOW_WINDOW:
926 			case VCLWINDOW_FRAMEWINDOW:
927 			case WINDOW_DOCKINGWINDOW:
928 				if ( rDescriptor.Type == ::com::sun::star::awt::WindowClass_TOP )
929 				{
930 					if (nType == WINDOW_DOCKINGWINDOW )
931 						pNewWindow = new DockingWindow( pParent, nWinBits );
932 					else
933 					{
934 						if ((pParent == NULL) && rDescriptor.Parent.is())
935 						{
936 							// try to get a system dependent window handle
937 							::com::sun::star::uno::Reference< ::com::sun::star::awt::XSystemDependentWindowPeer > xSystemDepParent(rDescriptor.Parent, ::com::sun::star::uno::UNO_QUERY);
938 
939 							if (xSystemDepParent.is())
940 							{
941 								sal_Int8 processID[16];
942 
943 								rtl_getGlobalProcessId( (sal_uInt8*)processID );
944 
945 								::com::sun::star::uno::Sequence<sal_Int8> processIdSeq(processID, 16);
946 
947 								::com::sun::star::uno::Any anyHandle = xSystemDepParent->getWindowHandle(processIdSeq, SYSTEM_DEPENDENT_TYPE);
948 
949                                 // use sal_Int64 here to accomodate all int types
950                                 // uno::Any shift operator whill upcast if necessary
951                                 sal_Int64 nWindowHandle = 0;
952                                 sal_Bool bXEmbed = sal_False;
953 
954                                 bool bUseParentData = true;
955                                 if( ! (anyHandle >>= nWindowHandle) )
956                                 {
957                                     css::uno::Sequence< css::beans::NamedValue > aProps;
958                                     if( anyHandle >>= aProps )
959                                     {
960                                         const int nProps = aProps.getLength();
961                                         const css::beans::NamedValue* pProps = aProps.getConstArray();
962                                         for( int i = 0; i < nProps; i++ )
963                                         {
964                                             if( pProps[i].Name.equalsAscii( "WINDOW" ) )
965                                                 pProps[i].Value >>= nWindowHandle;
966                                             else if( pProps[i].Name.equalsAscii( "XEMBED" ) )
967                                                 pProps[i].Value >>= bXEmbed;
968                                         }
969                                     }
970                                     else
971                                         bUseParentData = false;
972                                 }
973 
974                                 if( bUseParentData )
975                                 {
976                                     SystemParentData aParentData;
977                                     aParentData.nSize	= sizeof( aParentData );
978                                     #if defined QUARTZ
979                                     aParentData.pView	= reinterpret_cast<NSView*>(nWindowHandle);
980                                     #elif defined UNX
981                                     aParentData.aWindow = nWindowHandle;
982                                     aParentData.bXEmbedSupport = bXEmbed;
983                                     #elif defined WNT
984                                     aParentData.hWnd = reinterpret_cast<HWND>(nWindowHandle);
985                                     #elif defined OS2
986                                     aParentData.hWnd = (HWND)nWindowHandle;
987                                     #endif
988                                     pNewWindow = new WorkWindow( &aParentData );
989                                 }
990 							}
991 						}
992 
993 						if (!pNewWindow)
994 							pNewWindow = new WorkWindow( pParent, nWinBits );
995 					}
996 
997                     *ppNewComp = new VCLXTopWindow( pNewWindow->GetType() == WINDOW_WORKWINDOW );
998 				}
999 				else if ( rDescriptor.Type == ::com::sun::star::awt::WindowClass_CONTAINER )
1000 				{
1001 					if (nType == WINDOW_DOCKINGWINDOW )
1002 						pNewWindow = new DockingWindow( pParent, nWinBits );
1003 					else
1004 						pNewWindow = new Window( pParent, nWinBits );
1005 					*ppNewComp = new VCLXContainer;
1006 				}
1007 				else
1008 				{
1009 					if (nType == WINDOW_DOCKINGWINDOW )
1010 						pNewWindow = new DockingWindow( pParent, nWinBits );
1011 					else
1012 						pNewWindow = new Window( pParent, nWinBits );
1013 					*ppNewComp = new VCLXWindow;
1014 				}
1015 			break;
1016 			case WINDOW_CONTROL:
1017                 if  ( aServiceName.EqualsAscii( "simpleanimation" ) )
1018                 {
1019                     pNewWindow = new Throbber( pParent, nWinBits, Throbber::IMAGES_NONE );
1020                     ((Throbber*)pNewWindow)->SetScaleMode( css::awt::ImageScaleMode::Anisotropic );
1021                         // (compatibility)
1022                     *ppNewComp = new ::toolkit::XSimpleAnimation;
1023                 }
1024                 else if ( aServiceName.EqualsAscii( "throbber" ) )
1025                 {
1026                     pNewWindow = new Throbber( pParent, nWinBits, Throbber::IMAGES_NONE );
1027                     ((Throbber*)pNewWindow)->SetScaleMode( css::awt::ImageScaleMode::Anisotropic );
1028                         // (compatibility)
1029                     *ppNewComp = new ::toolkit::XThrobber;
1030                 }
1031 				else if ( rDescriptor.WindowServiceName.equalsIgnoreAsciiCase(
1032                         ::rtl::OUString::createFromAscii("tabpagecontainer") ) )
1033                 {
1034                     pNewWindow = new TabControl( pParent, nWinBits );
1035                     *ppNewComp = new VCLXTabPageContainer;
1036                 }
1037                 else if ( aServiceName.EqualsAscii( "animatedimages" ) )
1038                 {
1039                     pNewWindow = new Throbber( pParent, nWinBits );
1040                     *ppNewComp = new ::toolkit::AnimatedImagesPeer;
1041                 }
1042 			break;
1043             default:
1044                 OSL_ENSURE( false, "VCLXToolkit::ImplCreateWindow: unknown window type!" );
1045                 break;
1046 		}
1047 	}
1048 
1049 	return pNewWindow;
1050 }
1051 
1052 extern "C" { static void SAL_CALL thisModule() {} }
1053 
1054 css::uno::Reference< css::awt::XWindowPeer > VCLXToolkit::ImplCreateWindow(
1055     const css::awt::WindowDescriptor& rDescriptor,
1056     WinBits nForceWinBits )
1057 {
1058 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1059 
1060 	osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
1061 
1062 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > xRef;
1063 
1064 	Window* pParent = NULL;
1065 	if ( rDescriptor.Parent.is() )
1066 	{
1067 		VCLXWindow* pParentComponent = VCLXWindow::GetImplementation( rDescriptor.Parent );
1068 
1069         // #103939# Don't through assertion, may be it's a system dependend window, used in ImplCreateWindow.
1070         // DBG_ASSERT( pParentComponent, "ParentComponent not valid" );
1071 
1072 		if ( pParentComponent )
1073 			pParent = pParentComponent->GetWindow();
1074 	}
1075 
1076 	WinBits nWinBits = ImplGetWinBits( rDescriptor.WindowAttributes,
1077 		ImplGetComponentType( rDescriptor.WindowServiceName ) );
1078     nWinBits |= nForceWinBits;
1079 
1080 	VCLXWindow* pNewComp = NULL;
1081 
1082 	Window* pNewWindow = NULL;
1083 	// Try to create the window with SvTools
1084 	// (do this _before_ creating it on our own: The old mechanism (extended toolkit in SvTools) did it this way,
1085 	// and we need to stay compatible)
1086 	// try to load the lib
1087 	if ( !fnSvtCreateWindow && !hSvToolsLib )
1088 	{
1089 		::rtl::OUString aLibName = ::vcl::unohelper::CreateLibraryName( "svt", sal_True );
1090         hSvToolsLib = osl_loadModuleRelative(
1091             &thisModule, aLibName.pData, SAL_LOADMODULE_DEFAULT );
1092 		if ( hSvToolsLib )
1093 		{
1094 			::rtl::OUString aFunctionName( RTL_CONSTASCII_USTRINGPARAM( "CreateWindow" ) );
1095 			fnSvtCreateWindow = (FN_SvtCreateWindow)osl_getFunctionSymbol( hSvToolsLib, aFunctionName.pData );
1096 		}
1097 	}
1098 	// ask the SvTool creation function
1099 	if ( fnSvtCreateWindow )
1100 		pNewWindow = fnSvtCreateWindow( &pNewComp, &rDescriptor, pParent, nWinBits );
1101 
1102 	// if SvTools could not provide a window, create it ourself
1103 	if ( !pNewWindow )
1104 		pNewWindow = ImplCreateWindow( &pNewComp, rDescriptor, pParent, nWinBits );
1105 
1106 	DBG_ASSERT( pNewWindow, "createWindow: Unknown Component!" );
1107 	DBG_ASSERTWARNING( pNewComp, "createWindow: No special Interface!" );
1108 
1109 	if ( pNewWindow )
1110 	{
1111 		pNewWindow->SetCreatedWithToolkit( sal_True );
1112 		//pNewWindow->SetPosPixel( Point() ); // do not force (0,0) position, keep default pos instead
1113 
1114         if ( rDescriptor.WindowAttributes & ::com::sun::star::awt::WindowAttribute::MINSIZE )
1115 		{
1116 			pNewWindow->SetSizePixel( Size() );
1117 		}
1118 		else if ( rDescriptor.WindowAttributes & ::com::sun::star::awt::WindowAttribute::FULLSIZE )
1119 		{
1120 			if ( pParent )
1121 				pNewWindow->SetSizePixel( pParent->GetOutputSizePixel() );
1122 		}
1123 		else if ( !VCLUnoHelper::IsZero( rDescriptor.Bounds ) )
1124 		{
1125 			Rectangle aRect = VCLRectangle( rDescriptor.Bounds );
1126 			pNewWindow->SetPosSizePixel( aRect.TopLeft(), aRect.GetSize() );
1127 		}
1128 
1129 		if ( !pNewComp )
1130 		{
1131 			// Default-Interface
1132 			xRef = pNewWindow->GetComponentInterface( sal_True );
1133 		}
1134 		else
1135 		{
1136 			pNewComp->SetCreatedWithToolkit( sal_True );
1137 			xRef = pNewComp;
1138 			pNewWindow->SetComponentInterface( xRef );
1139 		}
1140         DBG_ASSERT( pNewWindow->GetComponentInterface( sal_False ) == xRef,
1141             "VCLXToolkit::createWindow: did #133706# resurge?" );
1142 
1143 		if ( rDescriptor.WindowAttributes & ::com::sun::star::awt::WindowAttribute::SHOW )
1144 			pNewWindow->Show();
1145 	}
1146 
1147 	return xRef;
1148 }
1149 
1150 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > > VCLXToolkit::createWindows( const ::com::sun::star::uno::Sequence< ::com::sun::star::awt::WindowDescriptor >& rDescriptors ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
1151 {
1152 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1153 
1154 	sal_uInt32 nComponents = rDescriptors.getLength();
1155 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > > aSeq( nComponents );
1156 	for ( sal_uInt32 n = 0; n < nComponents; n++ )
1157 	{
1158 		::com::sun::star::awt::WindowDescriptor aDescr = rDescriptors.getConstArray()[n];
1159 
1160 		if ( aDescr.ParentIndex == (-1) )
1161 			aDescr.Parent = NULL;
1162 		else if ( ( aDescr.ParentIndex >= 0 ) && ( aDescr.ParentIndex < (short)n ) )
1163 			aDescr.Parent = aSeq.getConstArray()[aDescr.ParentIndex];
1164 		aSeq.getArray()[n] = createWindow( aDescr );
1165 	}
1166 	return aSeq;
1167 }
1168 
1169 // ::com::sun::star::awt::XSystemChildFactory
1170 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > VCLXToolkit::createSystemChild( const ::com::sun::star::uno::Any& Parent, const ::com::sun::star::uno::Sequence< sal_Int8 >& /*ProcessId*/, sal_Int16 nSystemType ) throw(::com::sun::star::uno::RuntimeException)
1171 {
1172 	Window* pChildWindow = NULL;
1173 	if ( nSystemType == SYSTEM_DEPENDENT_TYPE )
1174 	{
1175         // use sal_Int64 here to accomodate all int types
1176         // uno::Any shift operator whill upcast if necessary
1177         sal_Int64 nWindowHandle = 0;
1178         sal_Bool bXEmbed = sal_False;
1179 
1180         bool bUseParentData = true;
1181         if( ! (Parent >>= nWindowHandle) )
1182         {
1183             css::uno::Sequence< css::beans::NamedValue > aProps;
1184             if( Parent >>= aProps )
1185             {
1186                 const int nProps = aProps.getLength();
1187                 const css::beans::NamedValue* pProps = aProps.getConstArray();
1188                 for( int i = 0; i < nProps; i++ )
1189                 {
1190                     if( pProps[i].Name.equalsAscii( "WINDOW" ) )
1191                         pProps[i].Value >>= nWindowHandle;
1192                     else if( pProps[i].Name.equalsAscii( "XEMBED" ) )
1193                         pProps[i].Value >>= bXEmbed;
1194                 }
1195             }
1196             else
1197                 bUseParentData = false;
1198         }
1199 
1200         if( bUseParentData )
1201         {
1202             SystemParentData aParentData;
1203             aParentData.nSize	= sizeof( aParentData );
1204             #if defined QUARTZ
1205             aParentData.pView	= reinterpret_cast<NSView*>(nWindowHandle);
1206             #elif defined UNX
1207             aParentData.aWindow = nWindowHandle;
1208             aParentData.bXEmbedSupport = bXEmbed;
1209             #elif defined WNT
1210             aParentData.hWnd = reinterpret_cast<HWND>(nWindowHandle);
1211             #elif defined OS2
1212             aParentData.hWnd = (HWND)nWindowHandle;
1213             #endif
1214             osl::Guard< vos::IMutex > aGuard( Application::GetSolarMutex() );
1215             try
1216             {
1217                 pChildWindow = new WorkWindow( &aParentData );
1218             }
1219             catch ( ::com::sun::star::uno::RuntimeException & rEx )
1220             {
1221                 // system child window could not be created
1222                 OSL_TRACE(
1223                     "VCLXToolkit::createSystemChild: caught %s\n",
1224                     ::rtl::OUStringToOString(
1225                         rEx.Message, RTL_TEXTENCODING_UTF8).getStr());
1226                 pChildWindow = NULL;
1227             }
1228         }
1229 	}
1230     else if (nSystemType == com::sun::star::lang::SystemDependent::SYSTEM_JAVA)
1231     {
1232         osl::Guard< vos::IMutex > aGuard(Application::GetSolarMutex());
1233         pChildWindow = new WorkWindow(0, Parent);
1234     }
1235 
1236 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > xPeer;
1237 	if ( pChildWindow )
1238 	{
1239         VCLXTopWindow* pPeer = new VCLXTopWindow(true);
1240         osl::Guard< vos::IMutex > aGuard( Application::GetSolarMutex() );
1241 		pPeer->SetWindow( pChildWindow );
1242 		xPeer = pPeer;
1243 	}
1244 
1245 	return xPeer;
1246 }
1247 
1248 // ::com::sun::star::awt::XMessageBoxFactory
1249 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XMessageBox > SAL_CALL VCLXToolkit::createMessageBox(
1250     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer >& aParent,
1251     const ::com::sun::star::awt::Rectangle& aPosSize,
1252     const ::rtl::OUString& aType,
1253     ::sal_Int32 aButtons,
1254     const ::rtl::OUString& aTitle,
1255     const ::rtl::OUString& aMessage ) throw (::com::sun::star::uno::RuntimeException)
1256 {
1257     ::com::sun::star::awt::WindowDescriptor aDescriptor;
1258 
1259     sal_Int32 nWindowAttributes = css::awt::WindowAttribute::BORDER|css::awt::WindowAttribute::MOVEABLE|css::awt::WindowAttribute::CLOSEABLE;
1260 
1261     // Map button definitions to window attributes
1262     if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_OK )
1263         nWindowAttributes |= css::awt::VclWindowPeerAttribute::OK;
1264     else if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_OK_CANCEL )
1265         nWindowAttributes |= css::awt::VclWindowPeerAttribute::OK_CANCEL;
1266     else if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_YES_NO )
1267         nWindowAttributes |= css::awt::VclWindowPeerAttribute::YES_NO;
1268     else if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_YES_NO_CANCEL )
1269         nWindowAttributes |= css::awt::VclWindowPeerAttribute::YES_NO_CANCEL;
1270     else if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_RETRY_CANCEL )
1271         nWindowAttributes |= css::awt::VclWindowPeerAttribute::RETRY_CANCEL;
1272 
1273     // Map default button definitions to window attributes
1274     if (sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_OK )
1275         nWindowAttributes |= css::awt::VclWindowPeerAttribute::DEF_OK;
1276     else if (sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_CANCEL )
1277         nWindowAttributes |= css::awt::VclWindowPeerAttribute::DEF_CANCEL;
1278     else if (sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_YES )
1279         nWindowAttributes |= css::awt::VclWindowPeerAttribute::DEF_YES;
1280     else if (sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_NO )
1281         nWindowAttributes |= css::awt::VclWindowPeerAttribute::DEF_NO;
1282     else if (sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_RETRY )
1283         nWindowAttributes |= css::awt::VclWindowPeerAttribute::DEF_RETRY;
1284 
1285     // No more bits for VclWindowPeerAttribute possible. Mapping must be
1286     // done explicitly using VCL methods
1287     WinBits nAddWinBits( 0 );
1288     if (( aButtons & 0x0000ffffL ) == css::awt::MessageBoxButtons::BUTTONS_ABORT_IGNORE_RETRY )
1289         nAddWinBits |= WB_ABORT_RETRY_IGNORE;
1290     if ( sal_Int32( aButtons & 0xffff0000L ) == css::awt::MessageBoxButtons::DEFAULT_BUTTON_IGNORE )
1291         nAddWinBits |= WB_DEF_IGNORE;
1292 
1293     aDescriptor.Type              = css::awt::WindowClass_MODALTOP;
1294     aDescriptor.WindowServiceName = aType;
1295     aDescriptor.ParentIndex       = -1;
1296     aDescriptor.Parent            = aParent;
1297     aDescriptor.Bounds            = aPosSize;
1298     aDescriptor.WindowAttributes  = nWindowAttributes;
1299     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XMessageBox > xMsgBox(
1300         ImplCreateWindow( aDescriptor, nAddWinBits ), css::uno::UNO_QUERY );
1301     css::uno::Reference< css::awt::XWindow > xWindow( xMsgBox, css::uno::UNO_QUERY );
1302     if ( xMsgBox.is() && xWindow.is() )
1303     {
1304 	    Window * pWindow = VCLUnoHelper::GetWindow( xWindow );
1305         if ( pWindow )
1306         {
1307             osl::Guard< vos::IMutex > aGuard(Application::GetSolarMutex());
1308             xMsgBox->setCaptionText( aTitle );
1309             xMsgBox->setMessageText( aMessage );
1310         }
1311     }
1312 
1313     return xMsgBox;
1314 }
1315 
1316 ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDragGestureRecognizer > SAL_CALL VCLXToolkit::getDragGestureRecognizer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& window ) throw(::com::sun::star::uno::RuntimeException)
1317 {
1318 	Window * pWindow = VCLUnoHelper::GetWindow( window );
1319 
1320 	if( pWindow )
1321 		return pWindow->GetDragGestureRecognizer();
1322 
1323 	return ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDragGestureRecognizer >();
1324 }
1325 
1326 ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDragSource > SAL_CALL VCLXToolkit::getDragSource( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& window ) throw(::com::sun::star::uno::RuntimeException)
1327 {
1328 	Window * pWindow = VCLUnoHelper::GetWindow( window );
1329 
1330 	if( pWindow )
1331 		return pWindow->GetDragSource();
1332 
1333 	return ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDragSource >();
1334 }
1335 
1336 ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDropTarget > SAL_CALL VCLXToolkit::getDropTarget( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >& window ) throw(::com::sun::star::uno::RuntimeException)
1337 {
1338 	Window * pWindow = VCLUnoHelper::GetWindow( window );
1339 
1340 	if( pWindow )
1341 		return pWindow->GetDropTarget();
1342 
1343 	return ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::dnd::XDropTarget >();
1344 }
1345 
1346 ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboard > SAL_CALL VCLXToolkit::getClipboard( const ::rtl::OUString& clipboardName ) throw(::com::sun::star::uno::RuntimeException)
1347 {
1348 	if( clipboardName.getLength() == 0 )
1349 	{
1350 		if( !mxClipboard.is() )
1351 		{
1352 			::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
1353 			if ( xFactory.is() )
1354 			{
1355 				// remember clipboard here
1356 				mxClipboard = ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboard > (
1357 					xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.datatransfer.clipboard.SystemClipboard" ) ), ::com::sun::star::uno::UNO_QUERY );
1358 			}
1359 		}
1360 
1361 		return mxClipboard;
1362 	}
1363 
1364 	else if( clipboardName.equals( ::rtl::OUString::createFromAscii("Selection") ) )
1365 	{
1366 		return mxSelection;
1367 	}
1368 
1369 	return ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboard >();
1370 }
1371 
1372 // XServiceInfo
1373 ::rtl::OUString VCLXToolkit::getImplementationName() throw(::com::sun::star::uno::RuntimeException)
1374 {
1375 	return rtl::OUString::createFromAscii( "stardiv.Toolkit.VCLXToolkit" );
1376 }
1377 
1378 sal_Bool VCLXToolkit::supportsService( const ::rtl::OUString& rServiceName ) throw(::com::sun::star::uno::RuntimeException)
1379 {
1380 	::osl::MutexGuard aGuard( GetMutex() );
1381 
1382     ::com::sun::star::uno::Sequence< ::rtl::OUString > aSNL = getSupportedServiceNames();
1383 	const ::rtl::OUString* pArray = aSNL.getConstArray();
1384 	const ::rtl::OUString* pArrayEnd = aSNL.getConstArray();
1385 	for (; pArray != pArrayEnd; ++pArray )
1386 		if( *pArray == rServiceName )
1387 			break;
1388 
1389 	return pArray != pArrayEnd;
1390 }
1391 
1392 ::com::sun::star::uno::Sequence< ::rtl::OUString > VCLXToolkit::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
1393 {
1394     ::rtl::OUString aServiceName( ::rtl::OUString::createFromAscii( szServiceName2_Toolkit ) );
1395     return ::com::sun::star::uno::Sequence< ::rtl::OUString >( &aServiceName, 1);
1396 }
1397 
1398 // css::awt::XExtendedToolkit:
1399 
1400 // virtual
1401 ::sal_Int32 SAL_CALL VCLXToolkit::getTopWindowCount()
1402     throw (::css::uno::RuntimeException)
1403 {
1404     return static_cast< ::sal_Int32 >(::Application::GetTopWindowCount());
1405         // XXX  numeric overflow
1406 }
1407 
1408 // virtual
1409 ::css::uno::Reference< ::css::awt::XTopWindow > SAL_CALL
1410 VCLXToolkit::getTopWindow(::sal_Int32 nIndex)
1411     throw (::css::uno::RuntimeException)
1412 {
1413     ::Window * p = ::Application::GetTopWindow(static_cast< long >(nIndex));
1414         // XXX  numeric overflow
1415     return ::css::uno::Reference< ::css::awt::XTopWindow >(
1416         p == 0 ? 0 : static_cast< ::css::awt::XWindow * >(p->GetWindowPeer()),
1417         ::css::uno::UNO_QUERY);
1418 }
1419 
1420 // virtual
1421 ::css::uno::Reference< ::css::awt::XTopWindow > SAL_CALL
1422 VCLXToolkit::getActiveTopWindow() throw (::css::uno::RuntimeException)
1423 {
1424     ::Window * p = ::Application::GetActiveTopWindow();
1425     return ::css::uno::Reference< ::css::awt::XTopWindow >(
1426         p == 0 ? 0 : static_cast< ::css::awt::XWindow * >(p->GetWindowPeer()),
1427         ::css::uno::UNO_QUERY);
1428 }
1429 
1430 // virtual
1431 void SAL_CALL VCLXToolkit::addTopWindowListener(
1432     ::css::uno::Reference< ::css::awt::XTopWindowListener > const & rListener)
1433     throw (::css::uno::RuntimeException)
1434 {
1435     OSL_ENSURE(rListener.is(), "Null rListener");
1436     ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
1437     if (rBHelper.bDisposed || rBHelper.bInDispose)
1438     {
1439         aGuard.clear();
1440         rListener->disposing(
1441             ::css::lang::EventObject(
1442                 static_cast< ::cppu::OWeakObject * >(this)));
1443     }
1444     else if (m_aTopWindowListeners.addInterface(rListener) == 1
1445              && !m_bEventListener)
1446     {
1447         m_bEventListener = true;
1448         ::Application::AddEventListener(m_aEventListenerLink);
1449     }
1450 }
1451 
1452 // virtual
1453 void SAL_CALL VCLXToolkit::removeTopWindowListener(
1454     ::css::uno::Reference< ::css::awt::XTopWindowListener > const & rListener)
1455     throw (::css::uno::RuntimeException)
1456 {
1457     ::osl::MutexGuard aGuard(rBHelper.rMutex);
1458     if (!(rBHelper.bDisposed || rBHelper.bInDispose)
1459         && m_aTopWindowListeners.removeInterface(rListener) == 0
1460         && m_aFocusListeners.getLength() == 0 && m_bEventListener)
1461     {
1462         ::Application::RemoveEventListener(m_aEventListenerLink);
1463         m_bEventListener = false;
1464     }
1465 }
1466 
1467 // virtual
1468 void SAL_CALL VCLXToolkit::addKeyHandler(
1469     ::css::uno::Reference< ::css::awt::XKeyHandler > const & rHandler)
1470     throw (::css::uno::RuntimeException)
1471 {
1472     OSL_ENSURE(rHandler.is(), "Null rHandler");
1473     ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
1474     if (rBHelper.bDisposed || rBHelper.bInDispose)
1475     {
1476         aGuard.clear();
1477         rHandler->disposing(
1478             ::css::lang::EventObject(
1479                 static_cast< ::cppu::OWeakObject * >(this)));
1480     }
1481     else if (m_aKeyHandlers.addInterface(rHandler) == 1 && !m_bKeyListener)
1482     {
1483         m_bKeyListener = true;
1484         ::Application::AddKeyListener(m_aKeyListenerLink);
1485     }
1486 }
1487 
1488 // virtual
1489 void SAL_CALL VCLXToolkit::removeKeyHandler(
1490     ::css::uno::Reference< ::css::awt::XKeyHandler > const & rHandler)
1491     throw (::css::uno::RuntimeException)
1492 {
1493     ::osl::MutexGuard aGuard(rBHelper.rMutex);
1494     if (!(rBHelper.bDisposed || rBHelper.bInDispose)
1495         && m_aKeyHandlers.removeInterface(rHandler) == 0 && m_bKeyListener)
1496     {
1497         ::Application::RemoveKeyListener(m_aKeyListenerLink);
1498         m_bKeyListener = false;
1499     }
1500 }
1501 
1502 // virtual
1503 void SAL_CALL VCLXToolkit::addFocusListener(
1504     ::css::uno::Reference< ::css::awt::XFocusListener > const & rListener)
1505     throw (::css::uno::RuntimeException)
1506 {
1507     OSL_ENSURE(rListener.is(), "Null rListener");
1508     ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
1509     if (rBHelper.bDisposed || rBHelper.bInDispose)
1510     {
1511         aGuard.clear();
1512         rListener->disposing(
1513             ::css::lang::EventObject(
1514                 static_cast< ::cppu::OWeakObject * >(this)));
1515     }
1516     else if (m_aFocusListeners.addInterface(rListener) == 1
1517              && !m_bEventListener)
1518     {
1519         m_bEventListener = true;
1520         ::Application::AddEventListener(m_aEventListenerLink);
1521     }
1522 }
1523 
1524 // virtual
1525 void SAL_CALL VCLXToolkit::removeFocusListener(
1526     ::css::uno::Reference< ::css::awt::XFocusListener > const & rListener)
1527     throw (::css::uno::RuntimeException)
1528 {
1529     ::osl::MutexGuard aGuard(rBHelper.rMutex);
1530     if (!(rBHelper.bDisposed || rBHelper.bInDispose)
1531         && m_aFocusListeners.removeInterface(rListener) == 0
1532         && m_aTopWindowListeners.getLength() == 0 && m_bEventListener)
1533     {
1534         ::Application::RemoveEventListener(m_aEventListenerLink);
1535         m_bEventListener = false;
1536     }
1537 }
1538 
1539 // virtual
1540 void SAL_CALL VCLXToolkit::fireFocusGained(
1541     ::com::sun::star::uno::Reference<
1542     ::com::sun::star::uno::XInterface > const &)
1543     throw (::com::sun::star::uno::RuntimeException)
1544 {
1545 }
1546 
1547 // virtual
1548 void SAL_CALL VCLXToolkit::fireFocusLost(
1549     ::com::sun::star::uno::Reference<
1550     ::com::sun::star::uno::XInterface > const &)
1551     throw (::com::sun::star::uno::RuntimeException)
1552 {
1553 }
1554 
1555 
1556 IMPL_LINK(VCLXToolkit, eventListenerHandler, ::VclSimpleEvent const *, pEvent)
1557 {
1558     switch (pEvent->GetId())
1559     {
1560     case VCLEVENT_WINDOW_SHOW:
1561         callTopWindowListeners(
1562             pEvent, &::css::awt::XTopWindowListener::windowOpened);
1563         break;
1564     case VCLEVENT_WINDOW_HIDE:
1565         callTopWindowListeners(
1566             pEvent, &::css::awt::XTopWindowListener::windowClosed);
1567         break;
1568     case VCLEVENT_WINDOW_ACTIVATE:
1569         callTopWindowListeners(
1570             pEvent, &::css::awt::XTopWindowListener::windowActivated);
1571         break;
1572     case VCLEVENT_WINDOW_DEACTIVATE:
1573         callTopWindowListeners(
1574             pEvent, &::css::awt::XTopWindowListener::windowDeactivated);
1575         break;
1576     case VCLEVENT_WINDOW_CLOSE:
1577         callTopWindowListeners(
1578             pEvent, &::css::awt::XTopWindowListener::windowClosing);
1579         break;
1580     case VCLEVENT_WINDOW_GETFOCUS:
1581         callFocusListeners(pEvent, true);
1582         break;
1583     case VCLEVENT_WINDOW_LOSEFOCUS:
1584         callFocusListeners(pEvent, false);
1585         break;
1586     case VCLEVENT_WINDOW_MINIMIZE:
1587         callTopWindowListeners(
1588             pEvent, &::css::awt::XTopWindowListener::windowMinimized);
1589         break;
1590     case VCLEVENT_WINDOW_NORMALIZE:
1591         callTopWindowListeners(
1592             pEvent, &::css::awt::XTopWindowListener::windowNormalized);
1593         break;
1594     }
1595     return 0;
1596 }
1597 
1598 IMPL_LINK(VCLXToolkit, keyListenerHandler, ::VclSimpleEvent const *, pEvent)
1599 {
1600     switch (pEvent->GetId())
1601     {
1602     case VCLEVENT_WINDOW_KEYINPUT:
1603         return callKeyHandlers(pEvent, true);
1604     case VCLEVENT_WINDOW_KEYUP:
1605         return callKeyHandlers(pEvent, false);
1606     }
1607     return 0;
1608 }
1609 
1610 void VCLXToolkit::callTopWindowListeners(
1611     ::VclSimpleEvent const * pEvent,
1612     void (SAL_CALL ::css::awt::XTopWindowListener::* pFn)(
1613         ::css::lang::EventObject const &))
1614 {
1615     ::Window * pWindow
1616           = static_cast< ::VclWindowEvent const * >(pEvent)->GetWindow();
1617     if (pWindow->IsTopWindow())
1618     {
1619         ::css::uno::Sequence< ::css::uno::Reference< ::css::uno::XInterface > >
1620               aListeners(m_aTopWindowListeners.getElements());
1621         if (aListeners.hasElements())
1622         {
1623             ::css::lang::EventObject aAwtEvent(
1624                 static_cast< ::css::awt::XWindow * >(pWindow->GetWindowPeer()));
1625             for (::sal_Int32 i = 0; i < aListeners.getLength(); ++i)
1626             {
1627                 ::css::uno::Reference< ::css::awt::XTopWindowListener >
1628                       xListener(aListeners[i], ::css::uno::UNO_QUERY);
1629                 try
1630                 {
1631                     (xListener.get()->*pFn)(aAwtEvent);
1632                 }
1633                 catch (::css::uno::RuntimeException & rEx)
1634                 {
1635                     OSL_TRACE(
1636                         "VCLXToolkit::callTopWindowListeners: caught %s\n",
1637                         ::rtl::OUStringToOString(
1638                             rEx.Message, RTL_TEXTENCODING_UTF8).getStr());
1639                 }
1640             }
1641         }
1642     }
1643 }
1644 
1645 long VCLXToolkit::callKeyHandlers(::VclSimpleEvent const * pEvent,
1646                                   bool bPressed)
1647 {
1648     ::css::uno::Sequence< ::css::uno::Reference< ::css::uno::XInterface > >
1649           aHandlers(m_aKeyHandlers.getElements());
1650 
1651     if (aHandlers.hasElements())
1652     {
1653         ::Window * pWindow = static_cast< ::VclWindowEvent const * >(pEvent)->GetWindow();
1654 
1655         // See implementation in vclxwindow.cxx for mapping between VCL and UNO AWT event
1656         ::KeyEvent * pKeyEvent = static_cast< ::KeyEvent * >(
1657             static_cast< ::VclWindowEvent const * >(pEvent)->GetData());
1658         ::css::awt::KeyEvent aAwtEvent(
1659             static_cast< ::css::awt::XWindow * >(pWindow->GetWindowPeer()),
1660             (pKeyEvent->GetKeyCode().IsShift()
1661              ? ::css::awt::KeyModifier::SHIFT : 0)
1662             | (pKeyEvent->GetKeyCode().IsMod1()
1663                ? ::css::awt::KeyModifier::MOD1 : 0)
1664             | (pKeyEvent->GetKeyCode().IsMod2()
1665                ? ::css::awt::KeyModifier::MOD2 : 0)
1666             | (pKeyEvent->GetKeyCode().IsMod3()
1667                ? ::css::awt::KeyModifier::MOD3 : 0),
1668             pKeyEvent->GetKeyCode().GetCode(), pKeyEvent->GetCharCode(),
1669             sal::static_int_cast< sal_Int16 >(
1670                 pKeyEvent->GetKeyCode().GetFunction()));
1671         for (::sal_Int32 i = 0; i < aHandlers.getLength(); ++i)
1672         {
1673             ::css::uno::Reference< ::css::awt::XKeyHandler > xHandler(
1674                 aHandlers[i], ::css::uno::UNO_QUERY);
1675             try
1676             {
1677                 if ((bPressed ? xHandler->keyPressed(aAwtEvent)
1678                       : xHandler->keyReleased(aAwtEvent)))
1679                     return 1;
1680             }
1681             catch (::css::uno::RuntimeException & rEx)
1682             {
1683                 OSL_TRACE(
1684                     "VCLXToolkit::callKeyHandlers: caught %s\n",
1685                     ::rtl::OUStringToOString(
1686                        rEx.Message, RTL_TEXTENCODING_UTF8).getStr());
1687             }
1688         }
1689     }
1690     return 0;
1691 }
1692 
1693 void VCLXToolkit::callFocusListeners(::VclSimpleEvent const * pEvent,
1694                                      bool bGained)
1695 {
1696     ::Window * pWindow
1697           = static_cast< ::VclWindowEvent const * >(pEvent)->GetWindow();
1698     if (pWindow->IsTopWindow())
1699     {
1700         ::css::uno::Sequence< ::css::uno::Reference< ::css::uno::XInterface > >
1701               aListeners(m_aFocusListeners.getElements());
1702         if (aListeners.hasElements())
1703         {
1704             // Ignore the interior of compound controls when determining the
1705             // window that gets the focus next (see implementation in
1706             // vclxwindow.cxx for mapping between VCL and UNO AWT event):
1707             ::css::uno::Reference< css::uno::XInterface > xNext;
1708             ::Window * pFocus = ::Application::GetFocusWindow();
1709             for (::Window * p = pFocus; p != 0; p = p->GetParent())
1710                 if (!p->IsCompoundControl())
1711                 {
1712                     pFocus = p;
1713                     break;
1714                 }
1715             if (pFocus != 0)
1716                 xNext = pFocus->GetComponentInterface(true);
1717             ::css::awt::FocusEvent aAwtEvent(
1718                 static_cast< ::css::awt::XWindow * >(pWindow->GetWindowPeer()),
1719                 pWindow->GetGetFocusFlags(), xNext, false);
1720             for (::sal_Int32 i = 0; i < aListeners.getLength(); ++i)
1721             {
1722                 ::css::uno::Reference< ::css::awt::XFocusListener > xListener(
1723                     aListeners[i], ::css::uno::UNO_QUERY);
1724                 try
1725                 {
1726                     bGained ? xListener->focusGained(aAwtEvent)
1727                         : xListener->focusLost(aAwtEvent);
1728                 }
1729                 catch (::css::uno::RuntimeException & rEx)
1730                 {
1731                     OSL_TRACE(
1732                         "VCLXToolkit::callFocusListeners: caught %s\n",
1733                         ::rtl::OUStringToOString(
1734                             rEx.Message, RTL_TEXTENCODING_UTF8).getStr());
1735                 }
1736             }
1737         }
1738     }
1739 }
1740 
1741 // css::awt::XReschedule:
1742 
1743 void SAL_CALL VCLXToolkit::reschedule()
1744     throw (::com::sun::star::uno::RuntimeException)
1745 {
1746     Application::Reschedule(true);
1747 }
1748 
1749