xref: /aoo41x/main/canvas/source/tools/page.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_CANVAS_PAGE_HXX
29 #define INCLUDED_CANVAS_PAGE_HXX
30 
31 #include <basegfx/vector/b2isize.hxx>
32 #include <basegfx/range/b2irectangle.hxx>
33 #include <canvas/rendering/icolorbuffer.hxx>
34 #include <canvas/rendering/irendermodule.hxx>
35 #include <canvas/rendering/isurface.hxx>
36 
37 #include <list>
38 #include <vector>
39 #include <boost/shared_ptr.hpp>
40 #include "surfacerect.hxx"
41 
42 namespace canvas
43 {
44 	class PageFragment;
45 
46 	typedef ::boost::shared_ptr< PageFragment > FragmentSharedPtr;
47 
48     /** One page of IRenderModule-provided texture space
49      */
50 	class Page
51 	{
52     public:
53         Page( const IRenderModuleSharedPtr& rRenderModule );
54 
55         FragmentSharedPtr        allocateSpace( const ::basegfx::B2ISize& rSize );
56         bool                     nakedFragment( const FragmentSharedPtr& pFragment );
57         void                     free( const FragmentSharedPtr& pFragment );
58         const ISurfaceSharedPtr& getSurface() const { return mpSurface; }
59         bool                     isValid() const;
60         void                     validate();
61 
62     private:
63         typedef std::list<FragmentSharedPtr> FragmentContainer_t;
64 
65         IRenderModuleSharedPtr  mpRenderModule;
66         ISurfaceSharedPtr       mpSurface;
67         FragmentContainer_t     mpFragments;
68 
69         bool insert( SurfaceRect& r );
70         bool isValidLocation( const SurfaceRect& r ) const;
71 	};
72 
73 	typedef ::boost::shared_ptr< Page > PageSharedPtr;
74 
75 
76     /** A part of a page, which gets allocated to a surface
77      */
78 	class PageFragment
79 	{
80     public:
81         PageFragment( const SurfaceRect& r,
82                       Page*              pPage ) :
83             mpPage(pPage),
84             maRect(r),
85             mpBuffer(),
86             maSourceOffset()
87         {
88         }
89 
90         /// Creates a 'naked' fragment.
91         PageFragment( const ::basegfx::B2ISize& rSize ) :
92             mpPage(NULL),
93             maRect(rSize),
94             mpBuffer(),
95             maSourceOffset()
96         {
97         }
98 
99         bool                        isNaked() const { return (mpPage == NULL); }
100         const SurfaceRect&          getRect() const { return maRect; }
101         const ::basegfx::B2IPoint&  getPos() const { return maRect.maPos; }
102         const ::basegfx::B2ISize&   getSize() const { return maRect.maSize; }
103         void                        setColorBuffer( const IColorBufferSharedPtr& pColorBuffer ) { mpBuffer=pColorBuffer; }
104         void                        setSourceOffset( const ::basegfx::B2IPoint& rOffset ) { maSourceOffset=rOffset; }
105         void                        setPage( Page* pPage ) { mpPage=pPage; }
106 
107         void free( const FragmentSharedPtr& pFragment )
108         {
109             if(mpPage)
110                 mpPage->free(pFragment);
111 
112             mpPage=NULL;
113         }
114 
115         bool select( bool bRefresh )
116         {
117             // request was made to select this fragment,
118             // but this fragment has not been located on any
119             // of the available pages, we need to hurry now.
120             if(!(mpPage))
121                 return false;
122 
123             ISurfaceSharedPtr pSurface(mpPage->getSurface());
124 
125             // select this surface before wiping the contents
126             // since a specific implementation could trigger
127             // a rendering operation here...
128             if(!(pSurface->selectTexture()))
129                 return false;
130 
131             // call refresh() if requested, otherwise we're up to date...
132             return bRefresh ? refresh() : true;
133         }
134 
135         bool refresh()
136         {
137             if(!(mpPage))
138                 return false;
139 
140             ISurfaceSharedPtr pSurface(mpPage->getSurface());
141 
142             return pSurface->update( maRect.maPos,
143                                      ::basegfx::B2IRectangle(
144                                          maSourceOffset,
145                                          maSourceOffset + maRect.maSize ),
146                                      *mpBuffer );
147         }
148 
149     private:
150         Page*                 mpPage;
151         SurfaceRect           maRect;
152         IColorBufferSharedPtr mpBuffer;
153         ::basegfx::B2IPoint   maSourceOffset;
154 	};
155 }
156 
157 #endif
158