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 .
22 #include <com/sun/star/awt/XWindow2.hpp>
24 #include <canvas/canvastools.hxx>
25 #include <base/graphicdevicebase.hxx>
27 namespace com::sun::star::awt
{ class XTopWindow
; }
30 /* Definition of BufferedGraphicDeviceBase class */
34 /** Helper template base class for XGraphicDevice implementations
37 Use this base class if your target device is a
38 window. Additionally to GraphicDeviceBase, this template
39 provides an implementation of the awt::XWindowListener
40 interface, to receive notifications about state changes of the
44 Base class to use, most probably one of the
45 WeakComponentImplHelperN templates with the appropriate
46 interfaces. At least XGraphicDevice should be among them (why else
47 would you use this template, then?). Base class must have an
48 Base( const Mutex& ) constructor (like the
49 WeakComponentImplHelperN templates have). As the very least,
50 the base class must be derived from uno::XInterface, as some
51 error reporting mechanisms rely on that.
54 Device helper implementation for the backend in question. This
55 object will be held as a member of this template class, and
56 basically gets forwarded all XGraphicDevice API calls that
57 could not be handled generically.
60 Lock strategy to use. Defaults to using the
61 BaseMutex-provided lock. Every time one of the methods is
62 entered, an object of type Mutex is created with m_aMutex as
63 the sole parameter, and destroyed again when the method scope
67 Optional unambiguous base class for XInterface of Base. It's
68 sometimes necessary to specify this parameter, e.g. if Base
69 derives from multiple UNO interface (were each provides its
70 own version of XInterface, making the conversion ambiguous)
74 class Mutex
=::osl::MutexGuard
,
75 class UnambiguousBase
= css::uno::XInterface
> class BufferedGraphicDeviceBase
:
76 public GraphicDeviceBase
< Base
, DeviceHelper
, Mutex
, UnambiguousBase
>
79 typedef GraphicDeviceBase
< Base
, DeviceHelper
, Mutex
, UnambiguousBase
> BaseType
;
80 typedef Mutex MutexType
;
82 BufferedGraphicDeviceBase() :
86 BaseType::maPropHelper
.addProperties(
87 PropertySetHelper::MakeMap("Window",
88 [this] () { return this->getXWindow(); }));
92 virtual css::uno::Reference
< css::rendering::XBufferController
> SAL_CALL
getBufferController( ) override
98 virtual ::sal_Int32 SAL_CALL
createBuffers( ::sal_Int32 nBuffers
) override
100 tools::verifyRange( nBuffers
, sal_Int32(1) );
105 virtual void SAL_CALL
destroyBuffers( ) override
109 virtual sal_Bool SAL_CALL
showBuffer( sal_Bool bUpdateAll
) override
111 MutexType
aGuard( BaseType::m_aMutex
);
113 return BaseType::maDeviceHelper
.showBuffer( mbIsVisible
, bUpdateAll
);
116 virtual sal_Bool SAL_CALL
switchBuffer( sal_Bool bUpdateAll
) override
118 MutexType
aGuard( BaseType::m_aMutex
);
120 return BaseType::maDeviceHelper
.switchBuffer( mbIsVisible
, bUpdateAll
);
124 /** Set corresponding canvas window
126 Use this method to set the window this canvas displays
127 on. Comes in handy when the canvas needs to adapt size or
128 output position to the changing window.
130 Whenever the bounds of the window change, <code>void
131 notifySizeUpdate( const awt::Rectangle& rBounds )</code>
132 is called, with rBounds the window bound rect relative to
135 void setWindow( const css::uno::Reference
< css::awt::XWindow2
>& rWindow
)
138 mxWindow
->removeWindowListener( this );
144 mbIsVisible
= mxWindow
->isVisible();
146 css::uno::Reference
< css::awt::XTopWindow
>(
148 css::uno::UNO_QUERY
).is();
150 maBounds
= transformBounds( mxWindow
->getPosSize() );
151 mxWindow
->addWindowListener( this );
155 css::uno::Any
getXWindow() const
157 return css::uno::makeAny(mxWindow
);
160 virtual void disposeThis() override
162 typename
BaseType::MutexType
aGuard( BaseType::m_aMutex
);
166 mxWindow
->removeWindowListener(this);
170 // pass on to base class
171 BaseType::disposeThis();
174 css::awt::Rectangle
transformBounds( const css::awt::Rectangle
& rBounds
)
176 // notifySizeUpdate's bounds are relative to the toplevel
179 return tools::getAbsoluteWindowRect(
183 return css::awt::Rectangle( 0,0,rBounds
.Width
,rBounds
.Height
);
186 void boundsChanged( const css::awt::WindowEvent
& e
)
188 typename
BaseType::MutexType
aGuard( BaseType::m_aMutex
);
190 const css::awt::Rectangle
& rNewBounds(
191 transformBounds( css::awt::Rectangle( e
.X
,
196 if( rNewBounds
.X
!= maBounds
.X
||
197 rNewBounds
.Y
!= maBounds
.Y
||
198 rNewBounds
.Width
!= maBounds
.Width
||
199 rNewBounds
.Height
!= maBounds
.Height
)
201 maBounds
= rNewBounds
;
202 BaseType::maDeviceHelper
.notifySizeUpdate( maBounds
);
207 virtual void disposeEventSource( const css::lang::EventObject
& Source
) override
209 typename
BaseType::MutexType
aGuard( BaseType::m_aMutex
);
211 if( Source
.Source
== mxWindow
)
214 BaseType::disposeEventSource(Source
);
217 virtual void SAL_CALL
windowResized( const css::awt::WindowEvent
& e
) override
222 virtual void SAL_CALL
windowMoved( const css::awt::WindowEvent
& e
) override
227 virtual void SAL_CALL
windowShown( const css::lang::EventObject
& ) override
229 typename
BaseType::MutexType
aGuard( BaseType::m_aMutex
);
234 virtual void SAL_CALL
windowHidden( const css::lang::EventObject
& ) override
236 typename
BaseType::MutexType
aGuard( BaseType::m_aMutex
);
242 css::uno::Reference
< css::awt::XWindow2
> mxWindow
;
244 /// Current bounds of the owning Window
245 css::awt::Rectangle maBounds
;
247 /// True, if the window this canvas is contained in, is visible
251 /// True, if the window this canvas is contained in, is a toplevel window
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */