Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / UnoControls / source / base / basecontrol.cxx
blobd926323bd26813108489ed9853cb9fc65196a778
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 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;
49 // construct/destruct
51 BaseControl::BaseControl( const Reference< XComponentContext >& rxContext )
52 : WeakComponentImplHelper ( m_aMutex )
53 , m_xComponentContext ( rxContext )
54 , m_nX ( DEFAULT_X )
55 , m_nY ( DEFAULT_Y )
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()
68 // XInterface
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.
88 return aReturn;
90 else
92 // Else; ... ask baseclass for interfaces!
93 return WeakComponentImplHelper::queryInterface( rType );
97 // XInterface
99 void SAL_CALL BaseControl::acquire() noexcept
101 // Attention:
102 // Don't use mutex or guard in this method!!! Is a method of XInterface.
104 // Forward to baseclass
105 WeakComponentImplHelper::acquire();
108 // XInterface
110 void SAL_CALL BaseControl::release() noexcept
112 // Attention:
113 // Don't use mutex or guard in this method!!! Is a method of XInterface.
115 // Forward to baseclass
116 WeakComponentImplHelper::release();
119 // XTypeProvider
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();
135 // XTypeProvider
137 Sequence< sal_Int8 > SAL_CALL BaseControl::getImplementationId()
139 return css::uno::Sequence<sal_Int8>();
142 // XServiceInfo
144 OUString SAL_CALL BaseControl::getImplementationName()
146 return OUString();
149 // XServiceInfo
151 sal_Bool SAL_CALL BaseControl::supportsService( const OUString& sServiceName )
153 return cppu::supportsService(this, sServiceName);
156 // XServiceInfo
158 Sequence< OUString > SAL_CALL BaseControl::getSupportedServiceNames()
160 return Sequence< OUString >();
163 // XComponent
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
180 m_xContext.clear();
181 if ( m_xPeer.is() )
183 if ( m_xGraphicsPeer.is() )
185 removePaintListener( this );
186 removeWindowListener( this );
187 m_xGraphicsPeer.clear();
190 m_xPeer->dispose();
191 m_xPeerWindow.clear();
192 m_xPeer.clear();
194 if ( m_xMultiplexer.is() )
196 // take changes on multiplexer
197 m_xMultiplexer->setPeer( Reference< XWindow >() );
201 // release view
202 if ( m_xGraphicsView.is() )
204 m_xGraphicsView.clear();
208 // XComponent
210 void SAL_CALL BaseControl::addEventListener( const Reference< XEventListener >& xListener )
212 // Ready for multithreading
213 MutexGuard aGuard( m_aMutex );
214 WeakComponentImplHelper::addEventListener( xListener );
217 // XComponent
219 void SAL_CALL BaseControl::removeEventListener( const Reference< XEventListener >& xListener )
221 // Ready for multithreading
222 MutexGuard aGuard( m_aMutex );
223 WeakComponentImplHelper::removeEventListener( xListener );
226 // XControl
228 void SAL_CALL BaseControl::createPeer( const Reference< XToolkit >& xToolkit ,
229 const Reference< XWindowPeer >& xParentPeer )
231 // Ready for multithreading
232 MutexGuard aGuard( m_aMutex );
234 if ( m_xPeer.is() )
235 return;
237 // use method "BaseControl::getWindowDescriptor()" to change window attributes!
238 WindowDescriptor aDescriptor = impl_getWindowDescriptor( xParentPeer );
240 if ( m_bVisible )
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() )
257 return;
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 );
268 if ( xDevice.is() )
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 );
284 // XControl
286 void SAL_CALL BaseControl::setContext( const Reference< XInterface >& xContext )
288 // Ready for multithreading
289 MutexGuard aGuard( m_aMutex );
290 m_xContext = xContext;
293 // XControl
295 void SAL_CALL BaseControl::setDesignMode( sal_Bool bOn )
297 // Ready for multithreading
298 MutexGuard aGuard( m_aMutex );
299 m_bInDesignMode = bOn;
302 // XControl
304 Reference< XInterface > SAL_CALL BaseControl::getContext()
306 // Ready for multithreading
307 MutexGuard aGuard( m_aMutex );
308 return m_xContext;
311 // XControl
313 Reference< XWindowPeer > SAL_CALL BaseControl::getPeer()
315 // Ready for multithreading
316 MutexGuard aGuard( m_aMutex );
317 return m_xPeer;
320 // XControl
322 Reference< XView > SAL_CALL BaseControl::getView()
324 // Ready for multithreading
325 MutexGuard aGuard( m_aMutex );
326 return this;
329 // XControl
331 sal_Bool SAL_CALL BaseControl::isDesignMode()
333 // Ready for multithreading
334 MutexGuard aGuard( m_aMutex );
335 return m_bInDesignMode;
338 // XControl
340 sal_Bool SAL_CALL BaseControl::isTransparent()
342 return false;
345 // XWindow
347 void SAL_CALL BaseControl::setPosSize( sal_Int32 nX ,
348 sal_Int32 nY ,
349 sal_Int32 nWidth ,
350 sal_Int32 nHeight ,
351 sal_Int16 nFlags )
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;
363 m_nX = nX;
366 if ( nFlags & PosSize::Y )
368 bChanged |= m_nY != nY;
369 m_nY = nY;
372 if ( nFlags & PosSize::WIDTH )
374 bChanged |= m_nWidth != nWidth;
375 m_nWidth = nWidth;
378 if ( nFlags & PosSize::HEIGHT )
380 bChanged |= m_nHeight != nHeight;
381 m_nHeight = nHeight;
384 if ( bChanged && m_xPeerWindow.is() )
386 m_xPeerWindow->setPosSize( m_nX, m_nY, m_nWidth, m_nHeight, nFlags );
390 // XWindow
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 );
407 // XWindow
409 void SAL_CALL BaseControl::setEnable( sal_Bool bEnable )
411 // Ready for multithreading
412 MutexGuard aGuard( m_aMutex );
414 // Set new state of flag
415 m_bEnable = bEnable;
417 if ( m_xPeerWindow.is() )
419 // Set it also on peerwindow
420 m_xPeerWindow->setEnable( m_bEnable );
424 // XWindow
426 void SAL_CALL BaseControl::setFocus()
428 // Ready for multithreading
429 MutexGuard aGuard( m_aMutex );
431 if ( m_xPeerWindow.is() )
433 m_xPeerWindow->setFocus();
437 // XWindow
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 );
446 // XWindow
448 void SAL_CALL BaseControl::addWindowListener( const Reference< XWindowListener >& xListener )
450 impl_getMultiplexer()->advise( cppu::UnoType<XWindowListener>::get(), xListener );
453 // XWindow
455 void SAL_CALL BaseControl::addFocusListener( const Reference< XFocusListener >& xListener )
457 impl_getMultiplexer()->advise( cppu::UnoType<XFocusListener>::get(), xListener );
460 // XWindow
462 void SAL_CALL BaseControl::addKeyListener( const Reference< XKeyListener >& xListener )
464 impl_getMultiplexer()->advise( cppu::UnoType<XKeyListener>::get(), xListener );
467 // XWindow
469 void SAL_CALL BaseControl::addMouseListener( const Reference< XMouseListener >& xListener )
471 impl_getMultiplexer()->advise( cppu::UnoType<XMouseListener>::get(), xListener );
474 // XWindow
476 void SAL_CALL BaseControl::addMouseMotionListener( const Reference< XMouseMotionListener >& xListener )
478 impl_getMultiplexer()->advise( cppu::UnoType<XMouseMotionListener>::get(), xListener );
481 // XWindow
483 void SAL_CALL BaseControl::addPaintListener( const Reference< XPaintListener >& xListener )
485 impl_getMultiplexer()->advise( cppu::UnoType<XPaintListener>::get(), xListener );
488 // XWindow
490 void SAL_CALL BaseControl::removeWindowListener( const Reference< XWindowListener >& xListener )
492 impl_getMultiplexer()->unadvise( cppu::UnoType<XWindowListener>::get(), xListener );
495 // XWindow
497 void SAL_CALL BaseControl::removeFocusListener( const Reference< XFocusListener >& xListener )
499 impl_getMultiplexer()->unadvise( cppu::UnoType<XFocusListener>::get(), xListener );
502 // XWindow
504 void SAL_CALL BaseControl::removeKeyListener( const Reference< XKeyListener >& xListener )
506 impl_getMultiplexer()->unadvise( cppu::UnoType<XKeyListener>::get(), xListener );
509 // XWindow
511 void SAL_CALL BaseControl::removeMouseListener( const Reference< XMouseListener >& xListener )
513 impl_getMultiplexer()->unadvise( cppu::UnoType<XMouseListener>::get(), xListener );
516 // XWindow
518 void SAL_CALL BaseControl::removeMouseMotionListener( const Reference< XMouseMotionListener >& xListener )
520 impl_getMultiplexer()->unadvise( cppu::UnoType<XMouseMotionListener>::get(), xListener );
523 // XWindow
525 void SAL_CALL BaseControl::removePaintListener( const Reference< XPaintListener >& xListener )
527 impl_getMultiplexer()->unadvise( cppu::UnoType<XPaintListener>::get(), xListener );
530 // XView
532 void SAL_CALL BaseControl::draw( sal_Int32 nX ,
533 sal_Int32 nY )
535 // Ready for multithreading
536 MutexGuard aGuard( m_aMutex );
538 // - paint to a view
539 // - use the method "paint()"
540 // - see also "windowPaint()"
541 impl_paint( nX, nY, m_xGraphicsView );
544 // XView
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;
552 if ( xDevice.is() )
554 // Ready for multithreading
555 MutexGuard aGuard( m_aMutex );
557 m_xGraphicsView = xDevice;
558 bReturn = true;
561 return bReturn;
564 // XView
566 void SAL_CALL BaseControl::setZoom( float /*fZoomX*/ ,
567 float /*fZoomY*/ )
569 // Not implemented yet
572 // XView
574 Reference< XGraphics > SAL_CALL BaseControl::getGraphics()
576 // Ready for multithreading
577 MutexGuard aGuard( m_aMutex );
578 return m_xGraphicsView;
581 // XView
583 Size SAL_CALL BaseControl::getSize()
585 // Ready for multithreading
586 MutexGuard aGuard( m_aMutex );
587 return Size( m_nWidth, m_nHeight );
590 // XEventListener
592 void SAL_CALL BaseControl::disposing( const EventObject& /*aSource*/ )
594 // Ready for multithreading
595 MutexGuard aGuard( m_aMutex );
597 // - release ALL references
598 // - it must be !!!
599 if ( m_xGraphicsPeer.is() )
601 removePaintListener( this );
602 removeWindowListener( this );
603 m_xGraphicsPeer.clear();
606 if ( m_xGraphicsView.is() )
608 m_xGraphicsView.clear();
612 // XPaintListener
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 );
625 // XWindowListener
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;
635 aMappedEvent.X = 0;
636 aMappedEvent.Y = 0;
637 impl_recalcLayout( aMappedEvent );
640 // XWindowListener
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;
650 aMappedEvent.X = 0;
651 aMappedEvent.Y = 0;
652 impl_recalcLayout( aMappedEvent );
655 // XWindowListener
657 void SAL_CALL BaseControl::windowShown( const EventObject& /*aEvent*/ )
661 // XWindowListener
663 void SAL_CALL BaseControl::windowHidden( const EventObject& /*aEvent*/ )
667 // protected method
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;
684 return aDescriptor;
687 // protected method
689 void BaseControl::impl_paint( sal_Int32 /*nX*/ ,
690 sal_Int32 /*nY*/ ,
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 !!!
698 // protected method
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!
706 // private method
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: */