merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / view / invmerge.cxx
blobacb0b8dde865f5ab70b2aa5762569996d37a4cd8
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: invmerge.cxx,v $
10 * $Revision: 1.7 $
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_sc.hxx"
36 #include <vcl/window.hxx>
37 #include <tools/debug.hxx>
39 #include "invmerge.hxx"
41 //------------------------------------------------------------------
43 ScInvertMerger::ScInvertMerger( Window* pWindow ) :
44 pWin( pWindow ),
45 pRects( NULL )
47 // both rectangles empty
50 ScInvertMerger::ScInvertMerger( ::std::vector< Rectangle >* pRectangles ) :
51 pWin( NULL ),
52 pRects( pRectangles )
54 // collect rectangles instead of inverting
57 ScInvertMerger::~ScInvertMerger()
59 Flush();
62 void ScInvertMerger::Flush()
64 FlushLine();
65 FlushTotal();
67 DBG_ASSERT( aLineRect.IsEmpty() && aTotalRect.IsEmpty(), "Flush: not empty" );
69 if ( pRects )
72 // also join vertically if there are non-adjacent columns involved
75 size_t nComparePos = 0;
76 while ( nComparePos < pRects->size() )
78 Rectangle aCompRect = (*pRects)[nComparePos];
79 sal_Int32 nBottom = aCompRect.Bottom();
80 size_t nOtherPos = nComparePos + 1;
82 while ( nOtherPos < pRects->size() )
84 Rectangle aOtherRect = (*pRects)[nOtherPos];
85 if ( aOtherRect.Top() > nBottom + 1 )
87 // rectangles are sorted, so we can stop searching
88 break;
90 if ( aOtherRect.Top() == nBottom + 1 &&
91 aOtherRect.Left() == aCompRect.Left() &&
92 aOtherRect.Right() == aCompRect.Right() )
94 // extend first rectangle
95 nBottom = aOtherRect.Bottom();
96 aCompRect.Bottom() = nBottom;
97 (*pRects)[nComparePos].Bottom() = nBottom;
99 // remove second rectangle
100 pRects->erase( pRects->begin() + nOtherPos );
102 // continue at unmodified nOtherPos
104 else
105 ++nOtherPos;
108 ++nComparePos;
113 void ScInvertMerger::FlushTotal()
115 if( aTotalRect.IsEmpty() )
116 return; // nothing to do
118 if ( pWin )
119 pWin->Invert( aTotalRect, INVERT_HIGHLIGHT );
120 else if ( pRects )
121 pRects->push_back( aTotalRect );
123 aTotalRect.SetEmpty();
126 void ScInvertMerger::FlushLine()
128 if( aLineRect.IsEmpty() )
129 return; // nothing to do
131 if ( aTotalRect.IsEmpty() )
133 aTotalRect = aLineRect; // start new total rect
135 else
137 if ( aLineRect.Left() == aTotalRect.Left() &&
138 aLineRect.Right() == aTotalRect.Right() &&
139 aLineRect.Top() == aTotalRect.Bottom() + 1 )
141 // extend total rect
142 aTotalRect.Bottom() = aLineRect.Bottom();
144 else
146 FlushTotal(); // draw old total rect
147 aTotalRect = aLineRect; // and start new one
151 aLineRect.SetEmpty();
154 void ScInvertMerger::AddRect( const Rectangle& rRect )
156 Rectangle aJustified = rRect;
157 if ( rRect.Left() > rRect.Right() ) // switch for RTL layout
159 aJustified.Left() = rRect.Right();
160 aJustified.Right() = rRect.Left();
163 if ( aLineRect.IsEmpty() )
165 aLineRect = aJustified; // start new line rect
167 else
169 BOOL bDone = FALSE;
170 if ( aJustified.Top() == aLineRect.Top() &&
171 aJustified.Bottom() == aLineRect.Bottom() )
173 // try to extend line rect
174 if ( aJustified.Left() == aLineRect.Right() + 1 )
176 aLineRect.Right() = aJustified.Right();
177 bDone = TRUE;
179 else if ( aJustified.Right() + 1 == aLineRect.Left() ) // for RTL layout
181 aLineRect.Left() = aJustified.Left();
182 bDone = TRUE;
185 if (!bDone)
187 FlushLine(); // use old line rect for total rect
188 aLineRect = aJustified; // and start new one