LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / canvas / inc / base / bufferedgraphicdevicebase.hxx
blob4cd604148cce22398d3107f915ed99dd19f87e2f
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 #pragma once
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 */
32 namespace canvas
34 /** Helper template base class for XGraphicDevice implementations
35 on windows.
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
41 associated window.
43 @tpl Base
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.
53 @tpl DeviceHelper
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.
59 @tpl Mutex
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
64 is left.
66 @tpl UnambiguousBase
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)
72 template< class Base,
73 class DeviceHelper,
74 class Mutex=::osl::MutexGuard,
75 class UnambiguousBase = css::uno::XInterface > class BufferedGraphicDeviceBase :
76 public GraphicDeviceBase< Base, DeviceHelper, Mutex, UnambiguousBase >
78 public:
79 typedef GraphicDeviceBase< Base, DeviceHelper, Mutex, UnambiguousBase > BaseType;
80 typedef Mutex MutexType;
82 BufferedGraphicDeviceBase() :
83 mbIsVisible( false ),
84 mbIsTopLevel( false )
86 BaseType::maPropHelper.addProperties(
87 PropertySetHelper::MakeMap("Window",
88 [this] () { return this->getXWindow(); }));
91 // XGraphicDevice
92 virtual css::uno::Reference< css::rendering::XBufferController > SAL_CALL getBufferController( ) override
94 return this;
97 // XBufferController
98 virtual ::sal_Int32 SAL_CALL createBuffers( ::sal_Int32 nBuffers ) override
100 tools::verifyRange( nBuffers, sal_Int32(1) );
102 return 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
133 the frame window.
135 void setWindow( const css::uno::Reference< css::awt::XWindow2 >& rWindow )
137 if( mxWindow.is() )
138 mxWindow->removeWindowListener( this );
140 mxWindow = rWindow;
142 if( mxWindow.is() )
144 mbIsVisible = mxWindow->isVisible();
145 mbIsTopLevel =
146 css::uno::Reference< css::awt::XTopWindow >(
147 mxWindow,
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 );
164 if( mxWindow.is() )
166 mxWindow->removeWindowListener(this);
167 mxWindow.clear();
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
177 // window
178 if( !mbIsTopLevel )
179 return tools::getAbsoluteWindowRect(
180 rBounds,
181 mxWindow );
182 else
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,
192 e.Y,
193 e.Width,
194 e.Height )));
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 );
206 // XWindowListener
207 virtual void disposeEventSource( const css::lang::EventObject& Source ) override
209 typename BaseType::MutexType aGuard( BaseType::m_aMutex );
211 if( Source.Source == mxWindow )
212 mxWindow.clear();
214 BaseType::disposeEventSource(Source);
217 virtual void SAL_CALL windowResized( const css::awt::WindowEvent& e ) override
219 boundsChanged( e );
222 virtual void SAL_CALL windowMoved( const css::awt::WindowEvent& e ) override
224 boundsChanged( e );
227 virtual void SAL_CALL windowShown( const css::lang::EventObject& ) override
229 typename BaseType::MutexType aGuard( BaseType::m_aMutex );
231 mbIsVisible = true;
234 virtual void SAL_CALL windowHidden( const css::lang::EventObject& ) override
236 typename BaseType::MutexType aGuard( BaseType::m_aMutex );
238 mbIsVisible = false;
241 protected:
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
248 bool mbIsVisible;
250 private:
251 /// True, if the window this canvas is contained in, is a toplevel window
252 bool mbIsTopLevel;
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */