xref: /AOO41X/main/sc/source/ui/view/drawutil.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 
32 
33 // INCLUDE ---------------------------------------------------------------
34 
35 #include <vcl/outdev.hxx>
36 
37 #include "drawutil.hxx"
38 #include "document.hxx"
39 #include "global.hxx"
40 #include "viewdata.hxx"
41 
42 // STATIC DATA -----------------------------------------------------------
43 
44 // -----------------------------------------------------------------------
45 
46 
47 inline Fraction MakeFraction( long nA, long nB )
48 {
49 	return ( nA && nB ) ? Fraction(nA,nB) : Fraction(1,1);
50 }
51 
52 void ScDrawUtil::CalcScale( ScDocument* pDoc, SCTAB nTab,
53 							SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow,
54 							OutputDevice* pDev,
55 							const Fraction& rZoomX, const Fraction& rZoomY,
56 							double nPPTX, double nPPTY,
57 							Fraction& rScaleX, Fraction& rScaleY )
58 {
59 	long nPixelX = 0;
60 	long nTwipsX = 0;
61 	long nPixelY = 0;
62 	long nTwipsY = 0;
63 	for (SCCOL i=nStartCol; i<nEndCol; i++)
64 	{
65 		sal_uInt16 nWidth = pDoc->GetColWidth(i,nTab);
66 		nTwipsX += (long) nWidth;
67 		nPixelX += ScViewData::ToPixel( nWidth, nPPTX );
68 	}
69 
70     for (SCROW nRow = nStartRow; nRow <= nEndRow-1; ++nRow)
71 	{
72         SCROW nLastRow = nRow;
73         if (pDoc->RowHidden(nRow, nTab, NULL, &nLastRow))
74         {
75             nRow = nLastRow;
76             continue;
77         }
78 
79         sal_uInt16 nHeight = pDoc->GetRowHeight(nRow, nTab);
80         nTwipsY += static_cast<long>(nHeight);
81         nPixelY += ScViewData::ToPixel(nHeight, nPPTY);
82 	}
83 
84     // #i116848# To get a large-enough number for PixelToLogic, multiply the integer values
85     // instead of using a larger number of rows
86     long nMultiply = 2000000 / nTwipsY;
87     if ( nMultiply > 1 )
88     {
89         nTwipsY *= nMultiply;
90         nPixelY *= nMultiply;
91     }
92 
93 	MapMode aHMMMode( MAP_100TH_MM, Point(), rZoomX, rZoomY );
94 	Point aPixelLog = pDev->PixelToLogic( Point( nPixelX,nPixelY ), aHMMMode );
95 
96 	//	Fraction(double) ctor can be used here (and avoid overflows of PixelLog * Zoom)
97 	//	because ReduceInaccurate is called later anyway.
98 
99 	if ( aPixelLog.X() && nTwipsX )
100 		rScaleX = Fraction( ((double)aPixelLog.X()) *
101 							((double)rZoomX.GetNumerator()) /
102 							((double)nTwipsX) /
103 							((double)HMM_PER_TWIPS) /
104 							((double)rZoomX.GetDenominator()) );
105 	else
106 		rScaleX = Fraction( 1, 1 );
107 
108 	if ( aPixelLog.Y() && nTwipsY )
109 		rScaleY = Fraction( ((double)aPixelLog.Y()) *
110 							((double)rZoomY.GetNumerator()) /
111 							((double)nTwipsY) /
112 							((double)HMM_PER_TWIPS) /
113 							((double)rZoomY.GetDenominator()) );
114 	else
115 		rScaleY = Fraction( 1, 1 );
116 
117     //	25 bits of accuracy are needed to always hit the right part of
118     //	cells in the last rows (was 17 before 1M rows).
119 	rScaleX.ReduceInaccurate( 25 );
120 	rScaleY.ReduceInaccurate( 25 );
121 }
122 
123 
124 
125 
126