Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / table / tablegeometry.cxx
blob5b18826c50f7bb5ce7e11ec91f6d32a9d8644d59
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 .
21 #include "tablegeometry.hxx"
22 #include "tablecontrol_impl.hxx"
25 namespace svt::table
29 //= TableRowGeometry
32 TableRowGeometry::TableRowGeometry( TableControl_Impl const & _rControl, tools::Rectangle const & _rBoundaries,
33 RowPos const _nRow, bool const i_allowVirtualRows )
34 :TableGeometry( _rControl, _rBoundaries )
35 ,m_nRowPos( _nRow )
36 ,m_bAllowVirtualRows( i_allowVirtualRows )
38 if ( m_nRowPos == ROW_COL_HEADERS )
40 m_aRect.SetTop( 0 );
41 m_aRect.SetBottom( m_rControl.m_nColHeaderHeightPixel - 1 );
43 else
45 impl_initRect();
50 void TableRowGeometry::impl_initRect()
52 if ( ( m_nRowPos >= m_rControl.m_nTopRow ) && impl_isValidRow( m_nRowPos ) )
54 m_aRect.SetTop( m_rControl.m_nColHeaderHeightPixel + ( m_nRowPos - m_rControl.m_nTopRow ) * m_rControl.m_nRowHeightPixel );
55 m_aRect.SetBottom( m_aRect.Top() + m_rControl.m_nRowHeightPixel - 1 );
57 else
58 m_aRect.SetEmpty();
62 bool TableRowGeometry::impl_isValidRow( RowPos const i_row ) const
64 return m_bAllowVirtualRows || ( i_row < m_rControl.m_pModel->getRowCount() );
68 bool TableRowGeometry::moveDown()
70 if ( m_nRowPos == ROW_COL_HEADERS )
72 m_nRowPos = m_rControl.m_nTopRow;
73 impl_initRect();
75 else
77 if ( impl_isValidRow( ++m_nRowPos ) )
78 m_aRect.Move( 0, m_rControl.m_nRowHeightPixel );
79 else
80 m_aRect.SetEmpty();
82 return isValid();
86 //= TableColumnGeometry
89 TableColumnGeometry::TableColumnGeometry( TableControl_Impl const & _rControl, tools::Rectangle const & _rBoundaries,
90 ColPos const _nCol )
91 :TableGeometry( _rControl, _rBoundaries )
92 ,m_nColPos( _nCol )
94 if ( m_nColPos == COL_ROW_HEADERS )
96 m_aRect.SetLeft( 0 );
97 m_aRect.SetRight( m_rControl.m_nRowHeaderWidthPixel - 1 );
99 else
101 impl_initRect();
106 void TableColumnGeometry::impl_initRect()
108 ColPos nLeftColumn = m_rControl.m_nLeftColumn;
109 if ( ( m_nColPos >= nLeftColumn ) && impl_isValidColumn( m_nColPos ) )
111 m_aRect.SetLeft( m_rControl.m_nRowHeaderWidthPixel );
112 // TODO: take into account any possibly frozen columns
114 for ( ColPos col = nLeftColumn; col < m_nColPos; ++col )
115 m_aRect.AdjustLeft(m_rControl.m_aColumnWidths[ col ].getWidth() );
116 m_aRect.SetRight( m_aRect.Left() + m_rControl.m_aColumnWidths[ m_nColPos ].getWidth() - 1 );
118 else
119 m_aRect.SetEmpty();
123 bool TableColumnGeometry::impl_isValidColumn( ColPos const i_column ) const
125 return i_column < ColPos( m_rControl.m_aColumnWidths.size() );
129 bool TableColumnGeometry::moveRight()
131 if ( m_nColPos == COL_ROW_HEADERS )
133 m_nColPos = m_rControl.m_nLeftColumn;
134 impl_initRect();
136 else
138 if ( impl_isValidColumn( ++m_nColPos ) )
140 m_aRect.SetLeft( m_aRect.Right() + 1 );
141 m_aRect.AdjustRight(m_rControl.m_aColumnWidths[ m_nColPos ].getWidth() );
143 else
144 m_aRect.SetEmpty();
147 return isValid();
151 } // namespace svt::table
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */