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
{
41 constexpr sal_Int32 DEFAULT_X
= 0;
42 constexpr sal_Int32 DEFAULT_Y
= 0;
43 constexpr sal_Int32 DEFAULT_WIDTH
= 100;
44 constexpr sal_Int32 DEFAULT_HEIGHT
= 100;
45 constexpr bool DEFAULT_VISIBLE
= false;
46 constexpr bool DEFAULT_INDESIGNMODE
= false;
47 constexpr bool DEFAULT_ENABLE
= true;
51 BaseControl::BaseControl( const Reference
< XComponentContext
>& rxContext
)
52 : WeakComponentImplHelper ( m_aMutex
)
53 , m_xComponentContext ( rxContext
)
56 , m_nWidth ( DEFAULT_WIDTH
)
57 , m_nHeight ( DEFAULT_HEIGHT
)
58 , m_bVisible ( DEFAULT_VISIBLE
)
59 , m_bInDesignMode ( DEFAULT_INDESIGNMODE
)
60 , m_bEnable ( DEFAULT_ENABLE
)
64 BaseControl::~BaseControl()
70 Any SAL_CALL
BaseControl::queryInterface( const Type
& rType
)
72 // Ask for my own supported interfaces ...
73 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
74 Any
aReturn ( ::cppu::queryInterface( rType
,
75 static_cast< XPaintListener
*> ( this ) ,
76 static_cast< XWindowListener
*> ( this ) ,
77 static_cast< XView
* > ( this ) ,
78 static_cast< XWindow
* > ( this ) ,
79 static_cast< XServiceInfo
* > ( this ) ,
80 static_cast< XControl
* > ( this )
84 // If searched interface supported by this class ...
85 if ( aReturn
.hasValue() )
87 // ... return this information.
92 // Else; ... ask baseclass for interfaces!
93 return WeakComponentImplHelper::queryInterface( rType
);
99 void SAL_CALL
BaseControl::acquire() noexcept
102 // Don't use mutex or guard in this method!!! Is a method of XInterface.
104 // Forward to baseclass
105 WeakComponentImplHelper::acquire();
110 void SAL_CALL
BaseControl::release() noexcept
113 // Don't use mutex or guard in this method!!! Is a method of XInterface.
115 // Forward to baseclass
116 WeakComponentImplHelper::release();
121 Sequence
< Type
> SAL_CALL
BaseControl::getTypes()
123 static OTypeCollection
ourTypeCollection(
124 cppu::UnoType
<XPaintListener
>::get(),
125 cppu::UnoType
<XWindowListener
>::get(),
126 cppu::UnoType
<XView
>::get(),
127 cppu::UnoType
<XWindow
>::get(),
128 cppu::UnoType
<XServiceInfo
>::get(),
129 cppu::UnoType
<XControl
>::get(),
130 WeakComponentImplHelper::getTypes() );
132 return ourTypeCollection
.getTypes();
137 Sequence
< sal_Int8
> SAL_CALL
BaseControl::getImplementationId()
139 return css::uno::Sequence
<sal_Int8
>();
144 OUString SAL_CALL
BaseControl::getImplementationName()
151 sal_Bool SAL_CALL
BaseControl::supportsService( const OUString
& sServiceName
)
153 return cppu::supportsService(this, sServiceName
);
158 Sequence
< OUString
> SAL_CALL
BaseControl::getSupportedServiceNames()
160 return Sequence
< OUString
>();
165 void SAL_CALL
BaseControl::dispose()
167 // Ready for multithreading
168 MutexGuard
aGuard( m_aMutex
);
170 if ( m_xMultiplexer
.is() )
172 // to all other paint, focus, etc.
173 m_xMultiplexer
->disposeAndClear();
176 // set the service manager to disposed
177 WeakComponentImplHelper::dispose();
179 // release context and peer
183 if ( m_xGraphicsPeer
.is() )
185 removePaintListener( this );
186 removeWindowListener( this );
187 m_xGraphicsPeer
.clear();
191 m_xPeerWindow
.clear();
194 if ( m_xMultiplexer
.is() )
196 // take changes on multiplexer
197 m_xMultiplexer
->setPeer( Reference
< XWindow
>() );
202 if ( m_xGraphicsView
.is() )
204 m_xGraphicsView
.clear();
210 void SAL_CALL
BaseControl::addEventListener( const Reference
< XEventListener
>& xListener
)
212 // Ready for multithreading
213 MutexGuard
aGuard( m_aMutex
);
214 WeakComponentImplHelper::addEventListener( xListener
);
219 void SAL_CALL
BaseControl::removeEventListener( const Reference
< XEventListener
>& xListener
)
221 // Ready for multithreading
222 MutexGuard
aGuard( m_aMutex
);
223 WeakComponentImplHelper::removeEventListener( xListener
);
228 void SAL_CALL
BaseControl::createPeer( const Reference
< XToolkit
>& xToolkit
,
229 const Reference
< XWindowPeer
>& xParentPeer
)
231 // Ready for multithreading
232 MutexGuard
aGuard( m_aMutex
);
237 // use method "BaseControl::getWindowDescriptor()" to change window attributes!
238 WindowDescriptor aDescriptor
= impl_getWindowDescriptor( xParentPeer
);
242 aDescriptor
.WindowAttributes
|= WindowAttribute::SHOW
;
245 // very slow under remote conditions!
246 // create the window on the server
247 Reference
< XToolkit
> xLocalToolkit
= xToolkit
;
248 if ( !xLocalToolkit
.is() )
250 // but first create well known toolkit, if it not exist
251 xLocalToolkit
.set( Toolkit::create(m_xComponentContext
), UNO_QUERY_THROW
);
253 m_xPeer
= xLocalToolkit
->createWindow( aDescriptor
);
254 m_xPeerWindow
.set( m_xPeer
, UNO_QUERY
);
256 if ( !m_xPeerWindow
.is() )
259 if ( m_xMultiplexer
.is() )
261 m_xMultiplexer
->setPeer( m_xPeerWindow
);
264 // create new reference to xgraphics for painting on a peer
265 // and add a paint listener
266 Reference
< XDevice
> xDevice( m_xPeerWindow
, UNO_QUERY
);
270 m_xGraphicsPeer
= xDevice
->createGraphics();
273 if ( m_xGraphicsPeer
.is() )
275 addPaintListener( this );
276 addWindowListener( this );
279 m_xPeerWindow
->setPosSize( m_nX
, m_nY
, m_nWidth
, m_nHeight
, PosSize::POSSIZE
);
280 m_xPeerWindow
->setEnable( m_bEnable
);
281 m_xPeerWindow
->setVisible( m_bVisible
&& !m_bInDesignMode
);
286 void SAL_CALL
BaseControl::setContext( const Reference
< XInterface
>& xContext
)
288 // Ready for multithreading
289 MutexGuard
aGuard( m_aMutex
);
290 m_xContext
= xContext
;
295 void SAL_CALL
BaseControl::setDesignMode( sal_Bool bOn
)
297 // Ready for multithreading
298 MutexGuard
aGuard( m_aMutex
);
299 m_bInDesignMode
= bOn
;
304 Reference
< XInterface
> SAL_CALL
BaseControl::getContext()
306 // Ready for multithreading
307 MutexGuard
aGuard( m_aMutex
);
313 Reference
< XWindowPeer
> SAL_CALL
BaseControl::getPeer()
315 // Ready for multithreading
316 MutexGuard
aGuard( m_aMutex
);
322 Reference
< XView
> SAL_CALL
BaseControl::getView()
324 // Ready for multithreading
325 MutexGuard
aGuard( m_aMutex
);
331 sal_Bool SAL_CALL
BaseControl::isDesignMode()
333 // Ready for multithreading
334 MutexGuard
aGuard( m_aMutex
);
335 return m_bInDesignMode
;
340 sal_Bool SAL_CALL
BaseControl::isTransparent()
347 void SAL_CALL
BaseControl::setPosSize( sal_Int32 nX
,
353 // - change size and position of window and save the values
355 // Ready for multithreading
356 MutexGuard
aGuard( m_aMutex
);
358 bool bChanged
= false;
360 if ( nFlags
& PosSize::X
)
362 bChanged
|= m_nX
!= nX
;
366 if ( nFlags
& PosSize::Y
)
368 bChanged
|= m_nY
!= nY
;
372 if ( nFlags
& PosSize::WIDTH
)
374 bChanged
|= m_nWidth
!= nWidth
;
378 if ( nFlags
& PosSize::HEIGHT
)
380 bChanged
|= m_nHeight
!= nHeight
;
384 if ( bChanged
&& m_xPeerWindow
.is() )
386 m_xPeerWindow
->setPosSize( m_nX
, m_nY
, m_nWidth
, m_nHeight
, nFlags
);
392 void SAL_CALL
BaseControl::setVisible( sal_Bool bVisible
)
394 // Ready for multithreading
395 MutexGuard
aGuard( m_aMutex
);
397 // Set new state of flag
398 m_bVisible
= bVisible
;
400 if ( m_xPeerWindow
.is() )
402 // Set it also on peerwindow
403 m_xPeerWindow
->setVisible( m_bVisible
);
409 void SAL_CALL
BaseControl::setEnable( sal_Bool bEnable
)
411 // Ready for multithreading
412 MutexGuard
aGuard( m_aMutex
);
414 // Set new state of flag
417 if ( m_xPeerWindow
.is() )
419 // Set it also on peerwindow
420 m_xPeerWindow
->setEnable( m_bEnable
);
426 void SAL_CALL
BaseControl::setFocus()
428 // Ready for multithreading
429 MutexGuard
aGuard( m_aMutex
);
431 if ( m_xPeerWindow
.is() )
433 m_xPeerWindow
->setFocus();
439 Rectangle SAL_CALL
BaseControl::getPosSize()
441 // Ready for multithreading
442 MutexGuard
aGuard( m_aMutex
);
443 return Rectangle( m_nX
, m_nY
, m_nWidth
, m_nHeight
);
448 void SAL_CALL
BaseControl::addWindowListener( const Reference
< XWindowListener
>& xListener
)
450 impl_getMultiplexer()->advise( cppu::UnoType
<XWindowListener
>::get(), xListener
);
455 void SAL_CALL
BaseControl::addFocusListener( const Reference
< XFocusListener
>& xListener
)
457 impl_getMultiplexer()->advise( cppu::UnoType
<XFocusListener
>::get(), xListener
);
462 void SAL_CALL
BaseControl::addKeyListener( const Reference
< XKeyListener
>& xListener
)
464 impl_getMultiplexer()->advise( cppu::UnoType
<XKeyListener
>::get(), xListener
);
469 void SAL_CALL
BaseControl::addMouseListener( const Reference
< XMouseListener
>& xListener
)
471 impl_getMultiplexer()->advise( cppu::UnoType
<XMouseListener
>::get(), xListener
);
476 void SAL_CALL
BaseControl::addMouseMotionListener( const Reference
< XMouseMotionListener
>& xListener
)
478 impl_getMultiplexer()->advise( cppu::UnoType
<XMouseMotionListener
>::get(), xListener
);
483 void SAL_CALL
BaseControl::addPaintListener( const Reference
< XPaintListener
>& xListener
)
485 impl_getMultiplexer()->advise( cppu::UnoType
<XPaintListener
>::get(), xListener
);
490 void SAL_CALL
BaseControl::removeWindowListener( const Reference
< XWindowListener
>& xListener
)
492 impl_getMultiplexer()->unadvise( cppu::UnoType
<XWindowListener
>::get(), xListener
);
497 void SAL_CALL
BaseControl::removeFocusListener( const Reference
< XFocusListener
>& xListener
)
499 impl_getMultiplexer()->unadvise( cppu::UnoType
<XFocusListener
>::get(), xListener
);
504 void SAL_CALL
BaseControl::removeKeyListener( const Reference
< XKeyListener
>& xListener
)
506 impl_getMultiplexer()->unadvise( cppu::UnoType
<XKeyListener
>::get(), xListener
);
511 void SAL_CALL
BaseControl::removeMouseListener( const Reference
< XMouseListener
>& xListener
)
513 impl_getMultiplexer()->unadvise( cppu::UnoType
<XMouseListener
>::get(), xListener
);
518 void SAL_CALL
BaseControl::removeMouseMotionListener( const Reference
< XMouseMotionListener
>& xListener
)
520 impl_getMultiplexer()->unadvise( cppu::UnoType
<XMouseMotionListener
>::get(), xListener
);
525 void SAL_CALL
BaseControl::removePaintListener( const Reference
< XPaintListener
>& xListener
)
527 impl_getMultiplexer()->unadvise( cppu::UnoType
<XPaintListener
>::get(), xListener
);
532 void SAL_CALL
BaseControl::draw( sal_Int32 nX
,
535 // Ready for multithreading
536 MutexGuard
aGuard( m_aMutex
);
539 // - use the method "paint()"
540 // - see also "windowPaint()"
541 impl_paint( nX
, nY
, m_xGraphicsView
);
546 sal_Bool SAL_CALL
BaseControl::setGraphics( const Reference
< XGraphics
>& xDevice
)
548 // - set the graphics for a view
549 // - in this class exist 2 graphics-member ... one for peer[_xGraphicsPeer] and one for view[_xGraphicsView]
550 // - they are used by "windowPaint() and draw()", forwarded to "paint ()"
551 bool bReturn
= false;
554 // Ready for multithreading
555 MutexGuard
aGuard( m_aMutex
);
557 m_xGraphicsView
= xDevice
;
566 void SAL_CALL
BaseControl::setZoom( float /*fZoomX*/ ,
569 // Not implemented yet
574 Reference
< XGraphics
> SAL_CALL
BaseControl::getGraphics()
576 // Ready for multithreading
577 MutexGuard
aGuard( m_aMutex
);
578 return m_xGraphicsView
;
583 Size SAL_CALL
BaseControl::getSize()
585 // Ready for multithreading
586 MutexGuard
aGuard( m_aMutex
);
587 return Size( m_nWidth
, m_nHeight
);
592 void SAL_CALL
BaseControl::disposing( const EventObject
& /*aSource*/ )
594 // Ready for multithreading
595 MutexGuard
aGuard( m_aMutex
);
597 // - release ALL references
599 if ( m_xGraphicsPeer
.is() )
601 removePaintListener( this );
602 removeWindowListener( this );
603 m_xGraphicsPeer
.clear();
606 if ( m_xGraphicsView
.is() )
608 m_xGraphicsView
.clear();
614 void SAL_CALL
BaseControl::windowPaint( const PaintEvent
& /*aEvent*/ )
616 // Ready for multithreading
617 MutexGuard
aGuard( m_aMutex
);
619 // - repaint the peer
620 // - use the method "paint ()" for painting on a peer and a print device !!!
621 // - see also "draw ()"
622 impl_paint( 0, 0, m_xGraphicsPeer
);
627 void SAL_CALL
BaseControl::windowResized( const WindowEvent
& aEvent
)
629 // Ready for multithreading
630 MutexGuard
aGuard( m_aMutex
);
632 m_nWidth
= aEvent
.Width
;
633 m_nHeight
= aEvent
.Height
;
634 WindowEvent aMappedEvent
= aEvent
;
637 impl_recalcLayout( aMappedEvent
);
642 void SAL_CALL
BaseControl::windowMoved( const WindowEvent
& aEvent
)
644 // Ready for multithreading
645 MutexGuard
aGuard( m_aMutex
);
647 m_nWidth
= aEvent
.Width
;
648 m_nHeight
= aEvent
.Height
;
649 WindowEvent aMappedEvent
= aEvent
;
652 impl_recalcLayout( aMappedEvent
);
657 void SAL_CALL
BaseControl::windowShown( const EventObject
& /*aEvent*/ )
663 void SAL_CALL
BaseControl::windowHidden( const EventObject
& /*aEvent*/ )
669 WindowDescriptor
BaseControl::impl_getWindowDescriptor( const Reference
< XWindowPeer
>& xParentPeer
)
671 // - used from "createPeer()" to set the values of a css::awt::WindowDescriptor !!!
672 // - if you will change the descriptor-values, you must override this virtual function
673 // - the caller must release the memory for this dynamical descriptor !!!
675 WindowDescriptor aDescriptor
;
677 aDescriptor
.Type
= WindowClass_SIMPLE
;
678 aDescriptor
.WindowServiceName
= "window";
679 aDescriptor
.ParentIndex
= -1;
680 aDescriptor
.Parent
= xParentPeer
;
681 aDescriptor
.Bounds
= getPosSize ();
682 aDescriptor
.WindowAttributes
= 0;
689 void BaseControl::impl_paint( sal_Int32
/*nX*/ ,
691 const Reference
< XGraphics
>& /*xGraphics*/ )
693 // - one paint method for peer AND view !!!
694 // (see also => "windowPaint()" and "draw()")
695 // - not used in this implementation, but it's not necessary to make it pure virtual !!!
700 void BaseControl::impl_recalcLayout( const WindowEvent
& /*aEvent*/ )
702 // We need as virtual function to support automatically resizing of derived controls!
703 // But we make it not pure virtual because it's not necessary for all derived classes!
708 OMRCListenerMultiplexerHelper
* BaseControl::impl_getMultiplexer()
710 if ( !m_xMultiplexer
.is() )
712 m_xMultiplexer
= new OMRCListenerMultiplexerHelper( static_cast<XWindow
*>(this), m_xPeerWindow
);
715 return m_xMultiplexer
.get();
718 } // namespace unocontrols
720 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */