bump product version to 6.4.0.3
[LibreOffice.git] / UnoControls / source / base / basecontrol.cxx
blobfc9d0b890d0bd0a198ea3f990a39196497b4ec89
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
31 // namespaces
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 #define DEFAULT_X 0
42 #define DEFAULT_Y 0
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
49 // construct/destruct
51 BaseControl::BaseControl( const Reference< XComponentContext >& rxContext )
52 : IMPL_MutexContainer ( )
53 , OComponentHelper ( m_aMutex )
54 , m_xComponentContext ( rxContext )
55 , m_nX ( DEFAULT_X )
56 , m_nY ( DEFAULT_Y )
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()
69 // XInterface
71 Any SAL_CALL BaseControl::queryInterface( const Type& rType )
73 Any aReturn;
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 );
80 else
82 // If a delegator is unknown, forward question to own queryAggregation.
83 aReturn = queryAggregation( rType );
86 return aReturn;
89 // XInterface
91 void SAL_CALL BaseControl::acquire() throw()
93 // Attention:
94 // Don't use mutex or guard in this method!!! Is a method of XInterface.
96 // Forward to baseclass
97 OComponentHelper::acquire();
100 // XInterface
102 void SAL_CALL BaseControl::release() throw()
104 // Attention:
105 // Don't use mutex or guard in this method!!! Is a method of XInterface.
107 // Forward to baseclass
108 OComponentHelper::release();
111 // XTypeProvider
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();
127 // XTypeProvider
129 Sequence< sal_Int8 > SAL_CALL BaseControl::getImplementationId()
131 return css::uno::Sequence<sal_Int8>();
134 // XAggregation
136 void SAL_CALL BaseControl::setDelegator( const Reference< XInterface >& xDel )
138 // Ready for multithreading
139 MutexGuard aGuard( m_aMutex );
140 m_xDelegator = xDel;
143 // XAggregation
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.
163 return aReturn;
165 else
167 // Else; ... ask baseclass for interfaces!
168 return OComponentHelper::queryAggregation( aType );
172 // XServiceInfo
174 OUString SAL_CALL BaseControl::getImplementationName()
176 return OUString();
179 // XServiceInfo
181 sal_Bool SAL_CALL BaseControl::supportsService( const OUString& sServiceName )
183 return cppu::supportsService(this, sServiceName);
186 // XServiceInfo
188 Sequence< OUString > SAL_CALL BaseControl::getSupportedServiceNames()
190 return Sequence< OUString >();
193 // XComponent
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
210 m_xContext.clear();
211 if ( m_xPeer.is() )
213 if ( m_xGraphicsPeer.is() )
215 removePaintListener( this );
216 removeWindowListener( this );
217 m_xGraphicsPeer.clear();
220 m_xPeer->dispose();
221 m_xPeerWindow.clear();
222 m_xPeer.clear();
224 if ( m_xMultiplexer.is() )
226 // take changes on multiplexer
227 m_xMultiplexer->setPeer( Reference< XWindow >() );
231 // release view
232 if ( m_xGraphicsView.is() )
234 m_xGraphicsView.clear();
238 // XComponent
240 void SAL_CALL BaseControl::addEventListener( const Reference< XEventListener >& xListener )
242 // Ready for multithreading
243 MutexGuard aGuard( m_aMutex );
244 OComponentHelper::addEventListener( xListener );
247 // XComponent
249 void SAL_CALL BaseControl::removeEventListener( const Reference< XEventListener >& xListener )
251 // Ready for multithreading
252 MutexGuard aGuard( m_aMutex );
253 OComponentHelper::removeEventListener( xListener );
256 // XControl
258 void SAL_CALL BaseControl::createPeer( const Reference< XToolkit >& xToolkit ,
259 const Reference< XWindowPeer >& xParentPeer )
261 // Ready for multithreading
262 MutexGuard aGuard( m_aMutex );
264 if ( !m_xPeer.is() )
266 // use method "BaseControl::getWindowDescriptor()" to change window attributes!
267 WindowDescriptor aDescriptor = impl_getWindowDescriptor( xParentPeer );
269 if ( m_bVisible )
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 );
296 if ( xDevice.is() )
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 );
314 // XControl
316 void SAL_CALL BaseControl::setContext( const Reference< XInterface >& xContext )
318 // Ready for multithreading
319 MutexGuard aGuard( m_aMutex );
320 m_xContext = xContext;
323 // XControl
325 void SAL_CALL BaseControl::setDesignMode( sal_Bool bOn )
327 // Ready for multithreading
328 MutexGuard aGuard( m_aMutex );
329 m_bInDesignMode = bOn;
332 // XControl
334 Reference< XInterface > SAL_CALL BaseControl::getContext()
336 // Ready for multithreading
337 MutexGuard aGuard( m_aMutex );
338 return m_xContext;
341 // XControl
343 Reference< XWindowPeer > SAL_CALL BaseControl::getPeer()
345 // Ready for multithreading
346 MutexGuard aGuard( m_aMutex );
347 return m_xPeer;
350 // XControl
352 Reference< XView > SAL_CALL BaseControl::getView()
354 // Ready for multithreading
355 MutexGuard aGuard( m_aMutex );
356 return this;
359 // XControl
361 sal_Bool SAL_CALL BaseControl::isDesignMode()
363 // Ready for multithreading
364 MutexGuard aGuard( m_aMutex );
365 return m_bInDesignMode;
368 // XControl
370 sal_Bool SAL_CALL BaseControl::isTransparent()
372 return false;
375 // XWindow
377 void SAL_CALL BaseControl::setPosSize( sal_Int32 nX ,
378 sal_Int32 nY ,
379 sal_Int32 nWidth ,
380 sal_Int32 nHeight ,
381 sal_Int16 nFlags )
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;
393 m_nX = nX;
396 if ( nFlags & PosSize::Y )
398 bChanged |= m_nY != nY;
399 m_nY = nY;
402 if ( nFlags & PosSize::WIDTH )
404 bChanged |= m_nWidth != nWidth;
405 m_nWidth = nWidth;
408 if ( nFlags & PosSize::HEIGHT )
410 bChanged |= m_nHeight != nHeight;
411 m_nHeight = nHeight;
414 if ( bChanged && m_xPeerWindow.is() )
416 m_xPeerWindow->setPosSize( m_nX, m_nY, m_nWidth, m_nHeight, nFlags );
420 // XWindow
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 );
437 // XWindow
439 void SAL_CALL BaseControl::setEnable( sal_Bool bEnable )
441 // Ready for multithreading
442 MutexGuard aGuard( m_aMutex );
444 // Set new state of flag
445 m_bEnable = bEnable;
447 if ( m_xPeerWindow.is() )
449 // Set it also on peerwindow
450 m_xPeerWindow->setEnable( m_bEnable );
454 // XWindow
456 void SAL_CALL BaseControl::setFocus()
458 // Ready for multithreading
459 MutexGuard aGuard( m_aMutex );
461 if ( m_xPeerWindow.is() )
463 m_xPeerWindow->setFocus();
467 // XWindow
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 );
476 // XWindow
478 void SAL_CALL BaseControl::addWindowListener( const Reference< XWindowListener >& xListener )
480 impl_getMultiplexer()->advise( cppu::UnoType<XWindowListener>::get(), xListener );
483 // XWindow
485 void SAL_CALL BaseControl::addFocusListener( const Reference< XFocusListener >& xListener )
487 impl_getMultiplexer()->advise( cppu::UnoType<XFocusListener>::get(), xListener );
490 // XWindow
492 void SAL_CALL BaseControl::addKeyListener( const Reference< XKeyListener >& xListener )
494 impl_getMultiplexer()->advise( cppu::UnoType<XKeyListener>::get(), xListener );
497 // XWindow
499 void SAL_CALL BaseControl::addMouseListener( const Reference< XMouseListener >& xListener )
501 impl_getMultiplexer()->advise( cppu::UnoType<XMouseListener>::get(), xListener );
504 // XWindow
506 void SAL_CALL BaseControl::addMouseMotionListener( const Reference< XMouseMotionListener >& xListener )
508 impl_getMultiplexer()->advise( cppu::UnoType<XMouseMotionListener>::get(), xListener );
511 // XWindow
513 void SAL_CALL BaseControl::addPaintListener( const Reference< XPaintListener >& xListener )
515 impl_getMultiplexer()->advise( cppu::UnoType<XPaintListener>::get(), xListener );
518 // XWindow
520 void SAL_CALL BaseControl::removeWindowListener( const Reference< XWindowListener >& xListener )
522 impl_getMultiplexer()->unadvise( cppu::UnoType<XWindowListener>::get(), xListener );
525 // XWindow
527 void SAL_CALL BaseControl::removeFocusListener( const Reference< XFocusListener >& xListener )
529 impl_getMultiplexer()->unadvise( cppu::UnoType<XFocusListener>::get(), xListener );
532 // XWindow
534 void SAL_CALL BaseControl::removeKeyListener( const Reference< XKeyListener >& xListener )
536 impl_getMultiplexer()->unadvise( cppu::UnoType<XKeyListener>::get(), xListener );
539 // XWindow
541 void SAL_CALL BaseControl::removeMouseListener( const Reference< XMouseListener >& xListener )
543 impl_getMultiplexer()->unadvise( cppu::UnoType<XMouseListener>::get(), xListener );
546 // XWindow
548 void SAL_CALL BaseControl::removeMouseMotionListener( const Reference< XMouseMotionListener >& xListener )
550 impl_getMultiplexer()->unadvise( cppu::UnoType<XMouseMotionListener>::get(), xListener );
553 // XWindow
555 void SAL_CALL BaseControl::removePaintListener( const Reference< XPaintListener >& xListener )
557 impl_getMultiplexer()->unadvise( cppu::UnoType<XPaintListener>::get(), xListener );
560 // XView
562 void SAL_CALL BaseControl::draw( sal_Int32 nX ,
563 sal_Int32 nY )
565 // Ready for multithreading
566 MutexGuard aGuard( m_aMutex );
568 // - paint to a view
569 // - use the method "paint()"
570 // - see also "windowPaint()"
571 impl_paint( nX, nY, m_xGraphicsView );
574 // XView
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;
582 if ( xDevice.is() )
584 // Ready for multithreading
585 MutexGuard aGuard( m_aMutex );
587 m_xGraphicsView = xDevice;
588 bReturn = true;
591 return bReturn;
594 // XView
596 void SAL_CALL BaseControl::setZoom( float /*fZoomX*/ ,
597 float /*fZoomY*/ )
599 // Not implemented yet
602 // XView
604 Reference< XGraphics > SAL_CALL BaseControl::getGraphics()
606 // Ready for multithreading
607 MutexGuard aGuard( m_aMutex );
608 return m_xGraphicsView;
611 // XView
613 Size SAL_CALL BaseControl::getSize()
615 // Ready for multithreading
616 MutexGuard aGuard( m_aMutex );
617 return Size( m_nWidth, m_nHeight );
620 // XEventListener
622 void SAL_CALL BaseControl::disposing( const EventObject& /*aSource*/ )
624 // Ready for multithreading
625 MutexGuard aGuard( m_aMutex );
627 // - release ALL references
628 // - it must be !!!
629 if ( m_xGraphicsPeer.is() )
631 removePaintListener( this );
632 removeWindowListener( this );
633 m_xGraphicsPeer.clear();
636 if ( m_xGraphicsView.is() )
638 m_xGraphicsView.clear();
642 // XPaintListener
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 );
655 // XWindowListener
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;
665 aMappedEvent.X = 0;
666 aMappedEvent.Y = 0;
667 impl_recalcLayout( aMappedEvent );
670 // XWindowListener
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;
680 aMappedEvent.X = 0;
681 aMappedEvent.Y = 0;
682 impl_recalcLayout( aMappedEvent );
685 // XWindowListener
687 void SAL_CALL BaseControl::windowShown( const EventObject& /*aEvent*/ )
691 // XWindowListener
693 void SAL_CALL BaseControl::windowHidden( const EventObject& /*aEvent*/ )
697 // protected method
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;
714 return aDescriptor;
717 // protected method
719 void BaseControl::impl_paint( sal_Int32 /*nX*/ ,
720 sal_Int32 /*nY*/ ,
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 !!!
728 // protected method
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!
736 // private method
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: */