Update ooo320-m1
[ooovba.git] / canvas / source / tools / page.cxx
blobc7f36487900592d9a02c032f967c72676dd496d1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: page.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_canvas.hxx"
34 #include <boost/bind.hpp>
35 #include "page.hxx"
37 namespace canvas
39 Page::Page( const IRenderModuleSharedPtr &rRenderModule ) :
40 mpRenderModule(rRenderModule),
41 mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
45 void Page::validate()
47 if(!(isValid()))
49 ::std::for_each( mpFragments.begin(),
50 mpFragments.end(),
51 ::boost::mem_fn(&PageFragment::refresh));
55 bool Page::isValid() const
57 return mpSurface && mpSurface->isValid();
60 FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
62 SurfaceRect rect(rSize);
63 if(insert(rect))
65 FragmentSharedPtr pFragment(new PageFragment(rect,this));
66 mpFragments.push_back(pFragment);
67 return pFragment;
70 return FragmentSharedPtr();
73 bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
75 SurfaceRect rect(pFragment->getSize());
76 if(insert(rect))
78 pFragment->setPage(this);
79 mpFragments.push_back(pFragment);
80 return true;
83 return false;
86 void Page::free( const FragmentSharedPtr& pFragment )
88 // the fragment passes as argument is no longer
89 // dedicated to this page. either it is about to
90 // be relocated to some other page or it will
91 // currently be deleted. in either case, simply
92 // remove the reference from our internal storage.
93 FragmentContainer_t::iterator it(
94 std::remove(
95 mpFragments.begin(),mpFragments.end(),pFragment));
96 mpFragments.erase(it,mpFragments.end());
99 bool Page::insert( SurfaceRect& r )
101 const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
102 FragmentContainer_t::const_iterator it(mpFragments.begin());
103 while(it != aEnd)
105 const SurfaceRect &rect = (*it)->getRect();
106 const sal_Int32 x = rect.maPos.getX();
107 const sal_Int32 y = rect.maPos.getY();
108 // to avoid interpolation artifacts from other textures,
109 // one pixel gap between them
110 const sal_Int32 w = rect.maSize.getX()+1;
111 const sal_Int32 h = rect.maSize.getY()+1;
113 // probe location to the right
114 r.maPos.setX(x+w);
115 r.maPos.setY(y);
116 if(isValidLocation(r))
117 return true;
119 // probe location at bottom
120 r.maPos.setX(x);
121 r.maPos.setY(y+h);
122 if(isValidLocation(r))
123 return true;
125 ++it;
128 r.maPos.setX(0);
129 r.maPos.setY(0);
131 return isValidLocation(r);
134 bool Page::isValidLocation( const SurfaceRect& r ) const
136 // the rectangle passed as argument has a valid
137 // location if and only if there's no intersection
138 // with existing areas.
139 SurfaceRect aBoundary(mpRenderModule->getPageSize()-basegfx::B2IVector(1,1));
140 if( !r.inside(aBoundary) )
141 return false;
143 const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
144 FragmentContainer_t::const_iterator it(mpFragments.begin());
145 while(it != aEnd)
147 if(r.intersection((*it)->getRect()))
148 return false;
150 ++it;
153 return true;