LibreOffice Module canvas (master)  1
bufferedgraphicdevicebase.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #pragma once
21 
22 #include <com/sun/star/awt/XWindow2.hpp>
23 
24 #include <canvas/canvastools.hxx>
26 
27 namespace com::sun::star::awt { class XTopWindow; }
28 
29 
30 /* Definition of BufferedGraphicDeviceBase class */
31 
32 namespace canvas
33 {
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 >
77  {
78  public:
80  typedef Mutex MutexType;
81 
83  mxWindow(),
84  maBounds(),
85  mbIsVisible( false ),
86  mbIsTopLevel( false )
87  {
90  [this] () { return this->getXWindow(); }));
91  }
92 
93  // XGraphicDevice
94  virtual css::uno::Reference< css::rendering::XBufferController > SAL_CALL getBufferController( ) override
95  {
96  return this;
97  }
98 
99  // XBufferController
100  virtual ::sal_Int32 SAL_CALL createBuffers( ::sal_Int32 nBuffers ) override
101  {
102  tools::verifyRange( nBuffers, sal_Int32(1) );
103 
104  return 1;
105  }
106 
107  virtual void SAL_CALL destroyBuffers( ) override
108  {
109  }
110 
111  virtual sal_Bool SAL_CALL showBuffer( sal_Bool bUpdateAll ) override
112  {
113  MutexType aGuard( BaseType::m_aMutex );
114 
115  return BaseType::maDeviceHelper.showBuffer( mbIsVisible, bUpdateAll );
116  }
117 
118  virtual sal_Bool SAL_CALL switchBuffer( sal_Bool bUpdateAll ) override
119  {
120  MutexType aGuard( BaseType::m_aMutex );
121 
122  return BaseType::maDeviceHelper.switchBuffer( mbIsVisible, bUpdateAll );
123  }
124 
125 
137  void setWindow( const css::uno::Reference< css::awt::XWindow2 >& rWindow )
138  {
139  if( mxWindow.is() )
140  mxWindow->removeWindowListener( this );
141 
142  mxWindow = rWindow;
143 
144  if( mxWindow.is() )
145  {
146  mbIsVisible = mxWindow->isVisible();
147  mbIsTopLevel =
148  css::uno::Reference< css::awt::XTopWindow >(
149  mxWindow,
150  css::uno::UNO_QUERY ).is();
151 
152  maBounds = transformBounds( mxWindow->getPosSize() );
153  mxWindow->addWindowListener( this );
154  }
155  }
156 
157  css::uno::Any getXWindow() const
158  {
159  return css::uno::makeAny(mxWindow);
160  }
161 
162  virtual void disposeThis() override
163  {
164  typename BaseType::MutexType aGuard( BaseType::m_aMutex );
165 
166  if( mxWindow.is() )
167  {
168  mxWindow->removeWindowListener(this);
169  mxWindow.clear();
170  }
171 
172  // pass on to base class
174  }
175 
176  css::awt::Rectangle transformBounds( const css::awt::Rectangle& rBounds )
177  {
178  // notifySizeUpdate's bounds are relative to the toplevel
179  // window
180  if( !mbIsTopLevel )
182  rBounds,
183  mxWindow );
184  else
185  return css::awt::Rectangle( 0,0,rBounds.Width,rBounds.Height );
186  }
187 
188  void boundsChanged( const css::awt::WindowEvent& e )
189  {
190  typename BaseType::MutexType aGuard( BaseType::m_aMutex );
191 
192  const css::awt::Rectangle& rNewBounds(
193  transformBounds( css::awt::Rectangle( e.X,
194  e.Y,
195  e.Width,
196  e.Height )));
197 
198  if( rNewBounds.X != maBounds.X ||
199  rNewBounds.Y != maBounds.Y ||
200  rNewBounds.Width != maBounds.Width ||
201  rNewBounds.Height != maBounds.Height )
202  {
203  maBounds = rNewBounds;
204  BaseType::maDeviceHelper.notifySizeUpdate( maBounds );
205  }
206  }
207 
208  // XWindowListener
209  virtual void disposeEventSource( const css::lang::EventObject& Source ) override
210  {
211  typename BaseType::MutexType aGuard( BaseType::m_aMutex );
212 
213  if( Source.Source == mxWindow )
214  mxWindow.clear();
215 
216  BaseType::disposeEventSource(Source);
217  }
218 
219  virtual void SAL_CALL windowResized( const css::awt::WindowEvent& e ) override
220  {
221  boundsChanged( e );
222  }
223 
224  virtual void SAL_CALL windowMoved( const css::awt::WindowEvent& e ) override
225  {
226  boundsChanged( e );
227  }
228 
229  virtual void SAL_CALL windowShown( const css::lang::EventObject& ) override
230  {
231  typename BaseType::MutexType aGuard( BaseType::m_aMutex );
232 
233  mbIsVisible = true;
234  }
235 
236  virtual void SAL_CALL windowHidden( const css::lang::EventObject& ) override
237  {
238  typename BaseType::MutexType aGuard( BaseType::m_aMutex );
239 
240  mbIsVisible = false;
241  }
242 
243  protected:
244  css::uno::Reference< css::awt::XWindow2 > mxWindow;
245 
247  css::awt::Rectangle maBounds;
248 
251 
252  private:
255  };
256 }
257 
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool mbIsVisible
True, if the window this canvas is contained in, is visible.
virtual void SAL_CALL windowHidden(const css::lang::EventObject &) override
virtual void SAL_CALL windowMoved(const css::awt::WindowEvent &e) override
awt::Rectangle getAbsoluteWindowRect(const awt::Rectangle &rRect, const uno::Reference< awt::XWindow2 > &xWin)
virtual ::sal_Int32 SAL_CALL createBuffers(::sal_Int32 nBuffers) override
virtual void disposeThis() override
bool mbIsTopLevel
True, if the window this canvas is contained in, is a toplevel window.
virtual void SAL_CALL windowShown(const css::lang::EventObject &) override
virtual sal_Bool SAL_CALL showBuffer(sal_Bool bUpdateAll) override
Base
unsigned char sal_Bool
void verifyRange(NumType arg, NumType lowerBound, NumType upperBound)
Range checker, which throws css::lang::IllegalArgument exception, when range is violated.
void setWindow(const css::uno::Reference< css::awt::XWindow2 > &rWindow)
Set corresponding canvas window.
virtual void SAL_CALL destroyBuffers() override
void addProperties(const InputMap &rMap)
Add given properties to helper.
virtual void disposeEventSource(const css::lang::EventObject &Source) override
virtual void SAL_CALL windowResized(const css::awt::WindowEvent &e) override
Helper template base class for XGraphicDevice implementations.
void boundsChanged(const css::awt::WindowEvent &e)
css::awt::Rectangle maBounds
Current bounds of the owning Window.
virtual css::uno::Reference< css::rendering::XBufferController > SAL_CALL getBufferController() override
GraphicDeviceBase< Base, DeviceHelper, Mutex, UnambiguousBase > BaseType
Helper template base class for XGraphicDevice implementations on windows.
virtual sal_Bool SAL_CALL switchBuffer(sal_Bool bUpdateAll) override
css::awt::Rectangle transformBounds(const css::awt::Rectangle &rBounds)
css::uno::Reference< css::awt::XWindow2 > mxWindow