Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / sc / source / ui / view / gridmerg.cxx
blob74d6968b1575845f24cd46f580c60cc7a208e326
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/outdev.hxx>
22 #include "gridmerg.hxx"
24 //------------------------------------------------------------------
26 ScGridMerger::ScGridMerger( OutputDevice* pOutDev, long nOnePixelX, long nOnePixelY ) :
27 pDev( pOutDev ),
28 nOneX( nOnePixelX ),
29 nOneY( nOnePixelY ),
30 nCount( 0 ),
31 bVertical( false )
33 // optimize (DrawGrid) only for pixel MapMode,
34 // to avoid rounding errors
36 bOptimize = ( pDev->GetMapMode().GetMapUnit() == MAP_PIXEL );
39 ScGridMerger::~ScGridMerger()
41 Flush();
44 void ScGridMerger::AddLine( long nStart, long nEnd, long nPos )
46 if ( nCount )
48 // not first line - test fix position
49 // more than one previous line - test distance
51 if ( nStart != nFixStart || nEnd != nFixEnd )
53 if ( nCount == 1 && nPos == nVarStart &&
54 ( nStart == nFixEnd ||
55 nStart == nFixEnd + ( bVertical ? nOneY : nOneX ) ) )
57 // additional optimization: extend connected lines
58 // keep nCount at 1
59 nFixEnd = nEnd;
61 else
62 Flush();
64 else if ( nCount == 1 )
66 nVarDiff = nPos - nVarStart;
67 ++nCount;
69 else if ( nPos != nVarStart + nCount * nVarDiff ) //! keep VarEnd?
70 Flush();
71 else
72 ++nCount;
75 if ( !nCount )
77 // first line (or flushed above) - just store
79 nFixStart = nStart;
80 nFixEnd = nEnd;
81 nVarStart = nPos;
82 nVarDiff = 0;
83 nCount = 1;
87 void ScGridMerger::AddHorLine( long nX1, long nX2, long nY )
89 if ( bOptimize )
91 if ( bVertical )
93 Flush();
94 bVertical = false;
96 AddLine( nX1, nX2, nY );
98 else
99 pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
102 void ScGridMerger::AddVerLine( long nX, long nY1, long nY2 )
104 if ( bOptimize )
106 if ( !bVertical )
108 Flush();
109 bVertical = sal_True;
111 AddLine( nY1, nY2, nX );
113 else
114 pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ) );
117 void ScGridMerger::Flush()
119 if (nCount)
121 if (bVertical)
123 if ( nCount == 1 )
124 pDev->DrawLine( Point( nVarStart, nFixStart ), Point( nVarStart, nFixEnd ) );
125 else
127 long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
128 if ( nVarDiff < 0 )
130 // nVarDiff is negative in RTL layout mode
131 // Change the positions so DrawGrid is called with a positive distance
132 // (nVarStart / nVarDiff can be modified, aren't used after Flush)
134 nVarDiff = -nVarDiff;
135 long nTemp = nVarStart;
136 nVarStart = nVarEnd;
137 nVarEnd = nTemp;
139 pDev->DrawGrid( Rectangle( nVarStart, nFixStart, nVarEnd, nFixEnd ),
140 Size( nVarDiff, nFixEnd - nFixStart ),
141 GRID_VERTLINES );
144 else
146 if ( nCount == 1 )
147 pDev->DrawLine( Point( nFixStart, nVarStart ), Point( nFixEnd, nVarStart ) );
148 else
150 long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
151 pDev->DrawGrid( Rectangle( nFixStart, nVarStart, nFixEnd, nVarEnd ),
152 Size( nFixEnd - nFixStart, nVarDiff ),
153 GRID_HORZLINES );
156 nCount = 0;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */