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