Update ooo320-m1
[ooovba.git] / sd / source / ui / view / bmcache.cxx
blob00b6d2cf582f281a805a6d8327340bd19db3c50e
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: bmcache.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_sd.hxx"
35 #include <limits.h> // LONG_MAX
36 #include <tools/solar.h>
37 #include <goodies/grfmgr.hxx>
38 #include "bmcache.hxx"
40 // eine Struktur fuer die Cache-Eintraege
41 struct BitmapCacheEntry
43 const SdPage* pPage;
44 GraphicObject* pGraphicObject;
45 sal_uInt32 nSizeBytes;
46 long nZoom;
49 /*************************************************************************
51 |* Destruktor, loescht die gespeicherten Bitmaps
53 \************************************************************************/
55 BitmapCache::~BitmapCache()
57 for( void* pEntry = aEntries.First(); pEntry; pEntry = aEntries.Next() )
59 delete static_cast< BitmapCacheEntry* >( pEntry )->pGraphicObject;
60 delete static_cast< BitmapCacheEntry* >( pEntry );
64 /*************************************************************************
66 |* Cache-Eintrag einfuegen
68 \************************************************************************/
70 void BitmapCache::Add(const SdPage* pPage, const Bitmap& rBmp, long nZoom)
72 BitmapCacheEntry* pEntry = NULL;
73 ULONG nSizeOfBitmap = rBmp.GetSizeBytes();
75 if( nSizeOfBitmap < nMaxSize )
77 while (nCurSize + nSizeOfBitmap > nMaxSize)
79 if( aEntries.Count() )
81 pEntry = (BitmapCacheEntry*) aEntries.Remove(aEntries.Count() - 1);
83 if( pEntry && pEntry->pGraphicObject )
85 nCurSize -= pEntry->nSizeBytes;
86 delete pEntry->pGraphicObject;
89 delete pEntry;
91 else
92 break;
95 pEntry = new BitmapCacheEntry;
96 pEntry->pPage = pPage;
97 pEntry->pGraphicObject = new GraphicObject( rBmp );
98 pEntry->nSizeBytes = nSizeOfBitmap;
99 pEntry->nZoom = nZoom;
101 aEntries.Insert( pEntry, (ULONG) 0 );
102 nCurSize += nSizeOfBitmap;
106 /*************************************************************************
108 |* Cache-Eintrag suchen, Bitmap-Zeiger zurueckgeben (NULL, wenn Bitmap nicht
109 |* im Cache)
110 |* - auf rZoomPercent wird der Zoomfaktor der Bitmap geschrieben
111 |* - abs(nZoomTolerance) gibt die maximal erlaubte Abweichung des
112 |* Zoomfaktors an; ist die Toleranz negativ, so sucht der Cache den
113 |* "best fit", sonst den "first fit",
114 |* - es werden nur Bitmaps mit Zoomfaktoren kleiner oder gleich rZoomPercent
115 |* beruecksichtigt, um ein Verkleinern der Bitmap beim Zeichnen zu vermeiden
117 \************************************************************************/
119 const GraphicObject* BitmapCache::Get( const SdPage* pPage, long& rZoomPercent, long nZoomTolerancePercent)
121 BitmapCacheEntry* pEntry = NULL;
122 GraphicObject* pGraphicObject = NULL;
124 if( nZoomTolerancePercent < 0 )
126 // "best fit"-Suche
127 long nTolerance = -nZoomTolerancePercent;
128 BitmapCacheEntry* pBest = NULL;
129 long nBest = LONG_MAX;
130 long nTest = 0L;
132 for( ULONG nPos = 0; nPos < aEntries.Count(); nPos++ )
134 pEntry = (BitmapCacheEntry*) aEntries.GetObject( nPos );
136 if( pEntry->pPage == pPage )
138 nTest = rZoomPercent - pEntry->nZoom;
140 if( nTest >= 0 && nTest < nBest && nTest <= nTolerance )
141 pBest = pEntry;
145 pEntry = pBest;
147 else
149 // "first fit"-suche
150 for( ULONG nPos = 0; nPos < aEntries.Count(); nPos++ )
152 pEntry = (BitmapCacheEntry*)aEntries.GetObject( nPos );
154 if (pEntry->pPage == pPage && Abs( pEntry->nZoom - rZoomPercent ) <= nZoomTolerancePercent )
155 break;
156 else
157 pEntry = NULL;
161 // was passendes gefunden?
162 if( pEntry )
164 pGraphicObject = pEntry->pGraphicObject;
165 aEntries.Remove( pEntry );
166 aEntries.Insert( pEntry, (ULONG) 0 );
167 rZoomPercent = pEntry->nZoom;
170 return pGraphicObject;
173 void BitmapCache::Remove( const SdPage* pPage )
175 for( ULONG nPos = 0; nPos < aEntries.Count(); )
177 BitmapCacheEntry* pCand = (BitmapCacheEntry*) aEntries.GetObject( nPos );
179 if( pCand->pPage == pPage )
181 pCand = (BitmapCacheEntry*) aEntries.Remove((ULONG)nPos);
182 delete pCand->pGraphicObject;
183 delete pCand;
185 else
186 nPos++;