1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basecontrol.hxx>
21 #include <multiplexer.hxx>
23 #include <com/sun/star/awt/XDevice.hpp>
24 #include <com/sun/star/awt/WindowAttribute.hpp>
25 #include <com/sun/star/awt/PosSize.hpp>
26 #include <com/sun/star/awt/Toolkit.hpp>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <cppuhelper/typeprovider.hxx>
33 using namespace ::cppu
;
34 using namespace ::osl
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::awt
;
39 namespace unocontrols
{
43 #define DEFAULT_WIDTH 100
44 #define DEFAULT_HEIGHT 100
45 #define DEFAULT_VISIBLE false
46 #define DEFAULT_INDESIGNMODE false
47 #define DEFAULT_ENABLE true
51 BaseControl::BaseControl( const Reference
< XComponentContext
>& rxContext
)
52 : IMPL_MutexContainer ( )
53 , OComponentHelper ( m_aMutex
)
54 , m_xComponentContext ( rxContext
)
57 , m_nWidth ( DEFAULT_WIDTH
)
58 , m_nHeight ( DEFAULT_HEIGHT
)
59 , m_bVisible ( DEFAULT_VISIBLE
)
60 , m_bInDesignMode ( DEFAULT_INDESIGNMODE
)
61 , m_bEnable ( DEFAULT_ENABLE
)
65 BaseControl::~BaseControl()
71 Any SAL_CALL
BaseControl::queryInterface( const Type
& rType
)
74 if ( m_xDelegator
.is() )
76 // If a delegator exists, forward question to its queryInterface.
77 // Delegator will ask its own queryAggregation!
78 aReturn
= m_xDelegator
->queryInterface( rType
);
82 // If a delegator is unknown, forward question to own queryAggregation.
83 aReturn
= queryAggregation( rType
);
91 void SAL_CALL
BaseControl::acquire() throw()
94 // Don't use mutex or guard in this method!!! Is a method of XInterface.
96 // Forward to baseclass
97 OComponentHelper::acquire();
102 void SAL_CALL
BaseControl::release() throw()
105 // Don't use mutex or guard in this method!!! Is a method of XInterface.
107 // Forward to baseclass
108 OComponentHelper::release();
113 Sequence
< Type
> SAL_CALL
BaseControl::getTypes()
115 static OTypeCollection
ourTypeCollection(
116 cppu::UnoType
<XPaintListener
>::get(),
117 cppu::UnoType
<XWindowListener
>::get(),
118 cppu::UnoType
<XView
>::get(),
119 cppu::UnoType
<XWindow
>::get(),
120 cppu::UnoType
<XServiceInfo
>::get(),
121 cppu::UnoType
<XControl
>::get(),
122 OComponentHelper::getTypes() );
124 return ourTypeCollection
.getTypes();
129 Sequence
< sal_Int8
> SAL_CALL
BaseControl::getImplementationId()
131 return css::uno::Sequence
<sal_Int8
>();
136 void SAL_CALL
BaseControl::setDelegator( const Reference
< XInterface
>& xDel
)
138 // Ready for multithreading
139 MutexGuard
aGuard( m_aMutex
);
145 Any SAL_CALL
BaseControl::queryAggregation( const Type
& aType
)
147 // Ask for my own supported interfaces ...
148 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
149 Any
aReturn ( ::cppu::queryInterface( aType
,
150 static_cast< XPaintListener
*> ( this ) ,
151 static_cast< XWindowListener
*> ( this ) ,
152 static_cast< XView
* > ( this ) ,
153 static_cast< XWindow
* > ( this ) ,
154 static_cast< XServiceInfo
* > ( this ) ,
155 static_cast< XControl
* > ( this )
159 // If searched interface supported by this class ...
160 if ( aReturn
.hasValue() )
162 // ... return this information.
167 // Else; ... ask baseclass for interfaces!
168 return OComponentHelper::queryAggregation( aType
);
174 OUString SAL_CALL
BaseControl::getImplementationName()
181 sal_Bool SAL_CALL
BaseControl::supportsService( const OUString
& sServiceName
)
183 return cppu::supportsService(this, sServiceName
);
188 Sequence
< OUString
> SAL_CALL
BaseControl::getSupportedServiceNames()
190 return Sequence
< OUString
>();
195 void SAL_CALL
BaseControl::dispose()
197 // Ready for multithreading
198 MutexGuard
aGuard( m_aMutex
);
200 if ( m_xMultiplexer
.is() )
202 // to all other paint, focus, etc.
203 m_xMultiplexer
->disposeAndClear();
206 // set the service manager to disposed
207 OComponentHelper::dispose();
209 // release context and peer
213 if ( m_xGraphicsPeer
.is() )
215 removePaintListener( this );
216 removeWindowListener( this );
217 m_xGraphicsPeer
.clear();
221 m_xPeerWindow
.clear();
224 if ( m_xMultiplexer
.is() )
226 // take changes on multiplexer
227 m_xMultiplexer
->setPeer( Reference
< XWindow
>() );
232 if ( m_xGraphicsView
.is() )
234 m_xGraphicsView
.clear();
240 void SAL_CALL
BaseControl::addEventListener( const Reference
< XEventListener
>& xListener
)
242 // Ready for multithreading
243 MutexGuard
aGuard( m_aMutex
);
244 OComponentHelper::addEventListener( xListener
);
249 void SAL_CALL
BaseControl::removeEventListener( const Reference
< XEventListener
>& xListener
)
251 // Ready for multithreading
252 MutexGuard
aGuard( m_aMutex
);
253 OComponentHelper::removeEventListener( xListener
);
258 void SAL_CALL
BaseControl::createPeer( const Reference
< XToolkit
>& xToolkit
,
259 const Reference
< XWindowPeer
>& xParentPeer
)
261 // Ready for multithreading
262 MutexGuard
aGuard( m_aMutex
);
266 // use method "BaseControl::getWindowDescriptor()" to change window attributes!
267 WindowDescriptor aDescriptor
= impl_getWindowDescriptor( xParentPeer
);
271 aDescriptor
.WindowAttributes
|= WindowAttribute::SHOW
;
274 // very slow under remote conditions!
275 // create the window on the server
276 Reference
< XToolkit
> xLocalToolkit
= xToolkit
;
277 if ( !xLocalToolkit
.is() )
279 // but first create well known toolkit, if it not exist
280 xLocalToolkit
.set( Toolkit::create(m_xComponentContext
), UNO_QUERY_THROW
);
282 m_xPeer
= xLocalToolkit
->createWindow( aDescriptor
);
283 m_xPeerWindow
.set( m_xPeer
, UNO_QUERY
);
285 if ( m_xPeerWindow
.is() )
287 if ( m_xMultiplexer
.is() )
289 m_xMultiplexer
->setPeer( m_xPeerWindow
);
292 // create new reference to xgraphics for painting on a peer
293 // and add a paint listener
294 Reference
< XDevice
> xDevice( m_xPeerWindow
, UNO_QUERY
);
298 m_xGraphicsPeer
= xDevice
->createGraphics();
301 if ( m_xGraphicsPeer
.is() )
303 addPaintListener( this );
304 addWindowListener( this );
307 m_xPeerWindow
->setPosSize( m_nX
, m_nY
, m_nWidth
, m_nHeight
, PosSize::POSSIZE
);
308 m_xPeerWindow
->setEnable( m_bEnable
);
309 m_xPeerWindow
->setVisible( m_bVisible
&& !m_bInDesignMode
);
316 void SAL_CALL
BaseControl::setContext( const Reference
< XInterface
>& xContext
)
318 // Ready for multithreading
319 MutexGuard
aGuard( m_aMutex
);
320 m_xContext
= xContext
;
325 void SAL_CALL
BaseControl::setDesignMode( sal_Bool bOn
)
327 // Ready for multithreading
328 MutexGuard
aGuard( m_aMutex
);
329 m_bInDesignMode
= bOn
;
334 Reference
< XInterface
> SAL_CALL
BaseControl::getContext()
336 // Ready for multithreading
337 MutexGuard
aGuard( m_aMutex
);
343 Reference
< XWindowPeer
> SAL_CALL
BaseControl::getPeer()
345 // Ready for multithreading
346 MutexGuard
aGuard( m_aMutex
);
352 Reference
< XView
> SAL_CALL
BaseControl::getView()
354 // Ready for multithreading
355 MutexGuard
aGuard( m_aMutex
);
361 sal_Bool SAL_CALL
BaseControl::isDesignMode()
363 // Ready for multithreading
364 MutexGuard
aGuard( m_aMutex
);
365 return m_bInDesignMode
;
370 sal_Bool SAL_CALL
BaseControl::isTransparent()
377 void SAL_CALL
BaseControl::setPosSize( sal_Int32 nX
,
383 // - change size and position of window and save the values
385 // Ready for multithreading
386 MutexGuard
aGuard( m_aMutex
);
388 bool bChanged
= false;
390 if ( nFlags
& PosSize::X
)
392 bChanged
|= m_nX
!= nX
;
396 if ( nFlags
& PosSize::Y
)
398 bChanged
|= m_nY
!= nY
;
402 if ( nFlags
& PosSize::WIDTH
)
404 bChanged
|= m_nWidth
!= nWidth
;
408 if ( nFlags
& PosSize::HEIGHT
)
410 bChanged
|= m_nHeight
!= nHeight
;
414 if ( bChanged
&& m_xPeerWindow
.is() )
416 m_xPeerWindow
->setPosSize( m_nX
, m_nY
, m_nWidth
, m_nHeight
, nFlags
);
422 void SAL_CALL
BaseControl::setVisible( sal_Bool bVisible
)
424 // Ready for multithreading
425 MutexGuard
aGuard( m_aMutex
);
427 // Set new state of flag
428 m_bVisible
= bVisible
;
430 if ( m_xPeerWindow
.is() )
432 // Set it also on peerwindow
433 m_xPeerWindow
->setVisible( m_bVisible
);
439 void SAL_CALL
BaseControl::setEnable( sal_Bool bEnable
)
441 // Ready for multithreading
442 MutexGuard
aGuard( m_aMutex
);
444 // Set new state of flag
447 if ( m_xPeerWindow
.is() )
449 // Set it also on peerwindow
450 m_xPeerWindow
->setEnable( m_bEnable
);
456 void SAL_CALL
BaseControl::setFocus()
458 // Ready for multithreading
459 MutexGuard
aGuard( m_aMutex
);
461 if ( m_xPeerWindow
.is() )
463 m_xPeerWindow
->setFocus();
469 Rectangle SAL_CALL
BaseControl::getPosSize()
471 // Ready for multithreading
472 MutexGuard
aGuard( m_aMutex
);
473 return Rectangle( m_nX
, m_nY
, m_nWidth
, m_nHeight
);
478 void SAL_CALL
BaseControl::addWindowListener( const Reference
< XWindowListener
>& xListener
)
480 impl_getMultiplexer()->advise( cppu::UnoType
<XWindowListener
>::get(), xListener
);
485 void SAL_CALL
BaseControl::addFocusListener( const Reference
< XFocusListener
>& xListener
)
487 impl_getMultiplexer()->advise( cppu::UnoType
<XFocusListener
>::get(), xListener
);
492 void SAL_CALL
BaseControl::addKeyListener( const Reference
< XKeyListener
>& xListener
)
494 impl_getMultiplexer()->advise( cppu::UnoType
<XKeyListener
>::get(), xListener
);
499 void SAL_CALL
BaseControl::addMouseListener( const Reference
< XMouseListener
>& xListener
)
501 impl_getMultiplexer()->advise( cppu::UnoType
<XMouseListener
>::get(), xListener
);
506 void SAL_CALL
BaseControl::addMouseMotionListener( const Reference
< XMouseMotionListener
>& xListener
)
508 impl_getMultiplexer()->advise( cppu::UnoType
<XMouseMotionListener
>::get(), xListener
);
513 void SAL_CALL
BaseControl::addPaintListener( const Reference
< XPaintListener
>& xListener
)
515 impl_getMultiplexer()->advise( cppu::UnoType
<XPaintListener
>::get(), xListener
);
520 void SAL_CALL
BaseControl::removeWindowListener( const Reference
< XWindowListener
>& xListener
)
522 impl_getMultiplexer()->unadvise( cppu::UnoType
<XWindowListener
>::get(), xListener
);
527 void SAL_CALL
BaseControl::removeFocusListener( const Reference
< XFocusListener
>& xListener
)
529 impl_getMultiplexer()->unadvise( cppu::UnoType
<XFocusListener
>::get(), xListener
);
534 void SAL_CALL
BaseControl::removeKeyListener( const Reference
< XKeyListener
>& xListener
)
536 impl_getMultiplexer()->unadvise( cppu::UnoType
<XKeyListener
>::get(), xListener
);
541 void SAL_CALL
BaseControl::removeMouseListener( const Reference
< XMouseListener
>& xListener
)
543 impl_getMultiplexer()->unadvise( cppu::UnoType
<XMouseListener
>::get(), xListener
);
548 void SAL_CALL
BaseControl::removeMouseMotionListener( const Reference
< XMouseMotionListener
>& xListener
)
550 impl_getMultiplexer()->unadvise( cppu::UnoType
<XMouseMotionListener
>::get(), xListener
);
555 void SAL_CALL
BaseControl::removePaintListener( const Reference
< XPaintListener
>& xListener
)
557 impl_getMultiplexer()->unadvise( cppu::UnoType
<XPaintListener
>::get(), xListener
);
562 void SAL_CALL
BaseControl::draw( sal_Int32 nX
,
565 // Ready for multithreading
566 MutexGuard
aGuard( m_aMutex
);
569 // - use the method "paint()"
570 // - see also "windowPaint()"
571 impl_paint( nX
, nY
, m_xGraphicsView
);
576 sal_Bool SAL_CALL
BaseControl::setGraphics( const Reference
< XGraphics
>& xDevice
)
578 // - set the graphics for a view
579 // - in this class exist 2 graphics-member ... one for peer[_xGraphicsPeer] and one for view[_xGraphicsView]
580 // - they are used by "windowPaint() and draw()", forwarded to "paint ()"
581 bool bReturn
= false;
584 // Ready for multithreading
585 MutexGuard
aGuard( m_aMutex
);
587 m_xGraphicsView
= xDevice
;
596 void SAL_CALL
BaseControl::setZoom( float /*fZoomX*/ ,
599 // Not implemented yet
604 Reference
< XGraphics
> SAL_CALL
BaseControl::getGraphics()
606 // Ready for multithreading
607 MutexGuard
aGuard( m_aMutex
);
608 return m_xGraphicsView
;
613 Size SAL_CALL
BaseControl::getSize()
615 // Ready for multithreading
616 MutexGuard
aGuard( m_aMutex
);
617 return Size( m_nWidth
, m_nHeight
);
622 void SAL_CALL
BaseControl::disposing( const EventObject
& /*aSource*/ )
624 // Ready for multithreading
625 MutexGuard
aGuard( m_aMutex
);
627 // - release ALL references
629 if ( m_xGraphicsPeer
.is() )
631 removePaintListener( this );
632 removeWindowListener( this );
633 m_xGraphicsPeer
.clear();
636 if ( m_xGraphicsView
.is() )
638 m_xGraphicsView
.clear();
644 void SAL_CALL
BaseControl::windowPaint( const PaintEvent
& /*aEvent*/ )
646 // Ready for multithreading
647 MutexGuard
aGuard( m_aMutex
);
649 // - repaint the peer
650 // - use the method "paint ()" for painting on a peer and a print device !!!
651 // - see also "draw ()"
652 impl_paint( 0, 0, m_xGraphicsPeer
);
657 void SAL_CALL
BaseControl::windowResized( const WindowEvent
& aEvent
)
659 // Ready for multithreading
660 MutexGuard
aGuard( m_aMutex
);
662 m_nWidth
= aEvent
.Width
;
663 m_nHeight
= aEvent
.Height
;
664 WindowEvent aMappedEvent
= aEvent
;
667 impl_recalcLayout( aMappedEvent
);
672 void SAL_CALL
BaseControl::windowMoved( const WindowEvent
& aEvent
)
674 // Ready for multithreading
675 MutexGuard
aGuard( m_aMutex
);
677 m_nWidth
= aEvent
.Width
;
678 m_nHeight
= aEvent
.Height
;
679 WindowEvent aMappedEvent
= aEvent
;
682 impl_recalcLayout( aMappedEvent
);
687 void SAL_CALL
BaseControl::windowShown( const EventObject
& /*aEvent*/ )
693 void SAL_CALL
BaseControl::windowHidden( const EventObject
& /*aEvent*/ )
699 WindowDescriptor
BaseControl::impl_getWindowDescriptor( const Reference
< XWindowPeer
>& xParentPeer
)
701 // - used from "createPeer()" to set the values of a css::awt::WindowDescriptor !!!
702 // - if you will change the descriptor-values, you must override this virtual function
703 // - the caller must release the memory for this dynamical descriptor !!!
705 WindowDescriptor aDescriptor
;
707 aDescriptor
.Type
= WindowClass_SIMPLE
;
708 aDescriptor
.WindowServiceName
= "window";
709 aDescriptor
.ParentIndex
= -1;
710 aDescriptor
.Parent
= xParentPeer
;
711 aDescriptor
.Bounds
= getPosSize ();
712 aDescriptor
.WindowAttributes
= 0;
719 void BaseControl::impl_paint( sal_Int32
/*nX*/ ,
721 const Reference
< XGraphics
>& /*xGraphics*/ )
723 // - one paint method for peer AND view !!!
724 // (see also => "windowPaint()" and "draw()")
725 // - not used in this implementation, but it's not necessary to make it pure virtual !!!
730 void BaseControl::impl_recalcLayout( const WindowEvent
& /*aEvent*/ )
732 // We need as virtual function to support automatically resizing of derived controls!
733 // But we make it not pure virtual because it's not necessary for all derived classes!
738 OMRCListenerMultiplexerHelper
* BaseControl::impl_getMultiplexer()
740 if ( !m_xMultiplexer
.is() )
742 m_xMultiplexer
= new OMRCListenerMultiplexerHelper( static_cast<XWindow
*>(this), m_xPeerWindow
);
745 return m_xMultiplexer
.get();
748 } // namespace unocontrols
750 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */