xref: /AOO41X/main/svtools/source/table/tablecontrol_impl.cxx (revision 5900e8ec128faec89519683efce668ccd8cc6084)
1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "svtools/table/tablecontrol.hxx"
28cdf0e10cSrcweir #include "svtools/table/defaultinputhandler.hxx"
29cdf0e10cSrcweir #include "svtools/table/tablemodel.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "tabledatawindow.hxx"
32cdf0e10cSrcweir #include "tablecontrol_impl.hxx"
33cdf0e10cSrcweir #include "tablegeometry.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** === begin UNO includes === **/
36cdf0e10cSrcweir #include <com/sun/star/accessibility/XAccessible.hpp>
37cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleTableModelChange.hpp>
38cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleEventId.hpp>
39cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleTableModelChangeType.hpp>
40cdf0e10cSrcweir /** === end UNO includes === **/
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <comphelper/flagguard.hxx>
43cdf0e10cSrcweir #include <vcl/scrbar.hxx>
44cdf0e10cSrcweir #include <vcl/seleng.hxx>
45cdf0e10cSrcweir #include <rtl/ref.hxx>
46cdf0e10cSrcweir #include <vcl/image.hxx>
47cdf0e10cSrcweir #include <tools/diagnose_ex.h>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <functional>
50cdf0e10cSrcweir #include <numeric>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #define MIN_COLUMN_WIDTH_PIXEL  4
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //......................................................................................................................
55cdf0e10cSrcweir namespace svt { namespace table
56cdf0e10cSrcweir {
57cdf0e10cSrcweir //......................................................................................................................
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /** === begin UNO using === **/
60cdf0e10cSrcweir     using ::com::sun::star::accessibility::AccessibleTableModelChange;
61cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
62cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
63cdf0e10cSrcweir     using ::com::sun::star::accessibility::XAccessible;
64cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
65cdf0e10cSrcweir     /** === end UNO using === **/
66cdf0e10cSrcweir     namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
67cdf0e10cSrcweir     namespace AccessibleTableModelChangeType = ::com::sun::star::accessibility::AccessibleTableModelChangeType;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     //==================================================================================================================
70cdf0e10cSrcweir 	//= SuppressCursor
71cdf0e10cSrcweir     //==================================================================================================================
72cdf0e10cSrcweir     class SuppressCursor
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir     private:
75cdf0e10cSrcweir         ITableControl&  m_rTable;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     public:
78cdf0e10cSrcweir         SuppressCursor( ITableControl& _rTable )
79cdf0e10cSrcweir             :m_rTable( _rTable )
80cdf0e10cSrcweir         {
81cdf0e10cSrcweir             m_rTable.hideCursor();
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         ~SuppressCursor()
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             m_rTable.showCursor();
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     //====================================================================
90cdf0e10cSrcweir 	//= EmptyTableModel
91cdf0e10cSrcweir 	//====================================================================
92cdf0e10cSrcweir     /** default implementation of an ->ITableModel, used as fallback when no
93cdf0e10cSrcweir         real model is present
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         Instances of this class are static in any way, and provide the least
96cdf0e10cSrcweir         necessary default functionality for a table model.
97cdf0e10cSrcweir     */
98cdf0e10cSrcweir     class EmptyTableModel : public ITableModel
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     public:
101cdf0e10cSrcweir         EmptyTableModel()
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         // ITableModel overridables
106cdf0e10cSrcweir         virtual TableSize           getColumnCount() const
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             return 0;
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         virtual TableSize           getRowCount() const
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             return 0;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         virtual bool                hasColumnHeaders() const
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             return false;
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         virtual bool                hasRowHeaders() const
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             return false;
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         virtual bool                isCellEditable( ColPos col, RowPos row ) const
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             (void)col;
125cdf0e10cSrcweir             (void)row;
126cdf0e10cSrcweir             return false;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir         virtual PColumnModel        getColumnModel( ColPos column )
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             DBG_ERROR( "EmptyTableModel::getColumnModel: invalid call!" );
131cdf0e10cSrcweir             (void)column;
132cdf0e10cSrcweir             return PColumnModel();
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir         virtual PTableRenderer      getRenderer() const
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             return PTableRenderer();
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         virtual PTableInputHandler  getInputHandler() const
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             return PTableInputHandler();
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir         virtual TableMetrics        getRowHeight() const
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             return 5 * 100;
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir 		virtual void setRowHeight(TableMetrics _nRowHeight)
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             (void)_nRowHeight;
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         virtual TableMetrics        getColumnHeaderHeight() const
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             return 0;
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir         virtual TableMetrics        getRowHeaderWidth() const
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             return 0;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir 	    virtual ScrollbarVisibility getVerticalScrollbarVisibility() const
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir 		    return ScrollbarShowNever;
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         virtual ScrollbarVisibility getHorizontalScrollbarVisibility() const
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir 		    return ScrollbarShowNever;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         virtual void addTableModelListener( const PTableModelListener& i_listener )
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             (void)i_listener;
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir         virtual void removeTableModelListener( const PTableModelListener& i_listener )
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             (void)i_listener;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir         virtual ::boost::optional< ::Color > getLineColor() const
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             return ::boost::optional< ::Color >();
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         virtual ::boost::optional< ::Color > getHeaderBackgroundColor() const
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             return ::boost::optional< ::Color >();
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         virtual ::boost::optional< ::Color > getHeaderTextColor() const
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             return ::boost::optional< ::Color >();
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         virtual ::boost::optional< ::Color >    getActiveSelectionBackColor() const
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             return ::boost::optional< ::Color >();
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir         virtual ::boost::optional< ::Color >    getInactiveSelectionBackColor() const
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             return ::boost::optional< ::Color >();
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir         virtual ::boost::optional< ::Color >    getActiveSelectionTextColor() const
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir             return ::boost::optional< ::Color >();
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         virtual ::boost::optional< ::Color >    getInactiveSelectionTextColor() const
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             return ::boost::optional< ::Color >();
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir         virtual ::boost::optional< ::Color > getTextColor() const
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir             return ::boost::optional< ::Color >();
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir         virtual ::boost::optional< ::Color > getTextLineColor() const
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir             return ::boost::optional< ::Color >();
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir         virtual ::boost::optional< ::std::vector< ::Color > > getRowBackgroundColors() const
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             return ::boost::optional< ::std::vector< ::Color > >();
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir         virtual ::com::sun::star::style::VerticalAlignment getVerticalAlign() const
215cdf0e10cSrcweir         {
216cdf0e10cSrcweir 	        return com::sun::star::style::VerticalAlignment(0);
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir         virtual ITableDataSort* getSortAdapter()
219cdf0e10cSrcweir         {
220cdf0e10cSrcweir             return NULL;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         virtual void getCellContent( ColPos const i_col, RowPos const i_row, ::com::sun::star::uno::Any& o_cellContent )
223cdf0e10cSrcweir         {
224cdf0e10cSrcweir             (void)i_row;
225cdf0e10cSrcweir             (void)i_col;
226cdf0e10cSrcweir             o_cellContent.clear();
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         virtual void getCellToolTip( ColPos const, RowPos const, ::com::sun::star::uno::Any& )
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir         virtual Any getRowHeading( RowPos const i_rowPos ) const
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir             (void)i_rowPos;
234cdf0e10cSrcweir             return Any();
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir     };
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     //====================================================================
240cdf0e10cSrcweir 	//= TableControl_Impl
241cdf0e10cSrcweir 	//====================================================================
242cdf0e10cSrcweir     DBG_NAME( TableControl_Impl )
243cdf0e10cSrcweir 
244cdf0e10cSrcweir #if DBG_UTIL
245cdf0e10cSrcweir     //====================================================================
246cdf0e10cSrcweir 	//= SuspendInvariants
247cdf0e10cSrcweir 	//====================================================================
248cdf0e10cSrcweir     class SuspendInvariants
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir     private:
251cdf0e10cSrcweir         const TableControl_Impl&    m_rTable;
252cdf0e10cSrcweir         sal_Int32                   m_nSuspendFlags;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     public:
255cdf0e10cSrcweir         SuspendInvariants( const TableControl_Impl& _rTable, sal_Int32 _nSuspendFlags )
256cdf0e10cSrcweir             :m_rTable( _rTable )
257cdf0e10cSrcweir             ,m_nSuspendFlags( _nSuspendFlags )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             //DBG_ASSERT( ( m_rTable.m_nRequiredInvariants & m_nSuspendFlags ) == m_nSuspendFlags,
260cdf0e10cSrcweir             //    "SuspendInvariants: cannot suspend what is already suspended!" );
261cdf0e10cSrcweir             const_cast< TableControl_Impl& >( m_rTable ).m_nRequiredInvariants &= ~m_nSuspendFlags;
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         ~SuspendInvariants()
264cdf0e10cSrcweir         {
265cdf0e10cSrcweir             const_cast< TableControl_Impl& >( m_rTable ).m_nRequiredInvariants |= m_nSuspendFlags;
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir     };
268cdf0e10cSrcweir     #define DBG_SUSPEND_INV( flags ) \
269cdf0e10cSrcweir         SuspendInvariants aSuspendInv( *this, flags );
270cdf0e10cSrcweir #else
271cdf0e10cSrcweir     #define DBG_SUSPEND_INV( flags )
272cdf0e10cSrcweir #endif
273cdf0e10cSrcweir 
274cdf0e10cSrcweir #if DBG_UTIL
275cdf0e10cSrcweir 	//====================================================================
276cdf0e10cSrcweir     const char* TableControl_Impl_checkInvariants( const void* _pInstance )
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         return static_cast< const TableControl_Impl* >( _pInstance )->impl_checkInvariants();
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     namespace
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         template< typename SCALAR_TYPE >
284cdf0e10cSrcweir         bool lcl_checkLimitsExclusive( SCALAR_TYPE _nValue, SCALAR_TYPE _nMin, SCALAR_TYPE _nMax )
285cdf0e10cSrcweir         {
286cdf0e10cSrcweir             return ( _nValue > _nMin ) && ( _nValue < _nMax );
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         template< typename SCALAR_TYPE >
290cdf0e10cSrcweir         bool lcl_checkLimitsExclusive_OrDefault_OrFallback( SCALAR_TYPE _nValue, SCALAR_TYPE _nMin, SCALAR_TYPE _nMax,
291cdf0e10cSrcweir             PTableModel _pModel, SCALAR_TYPE _nDefaultOrFallback )
292cdf0e10cSrcweir         {
293cdf0e10cSrcweir             if ( !_pModel )
294cdf0e10cSrcweir                 return _nValue == _nDefaultOrFallback;
295cdf0e10cSrcweir             if ( _nMax <= _nMin )
296cdf0e10cSrcweir                 return _nDefaultOrFallback == _nValue;
297cdf0e10cSrcweir             return lcl_checkLimitsExclusive( _nValue, _nMin, _nMax );
298cdf0e10cSrcweir         }
299cdf0e10cSrcweir     }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
302cdf0e10cSrcweir     const sal_Char* TableControl_Impl::impl_checkInvariants() const
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         if ( !m_pModel )
305cdf0e10cSrcweir             return "no model, not even an EmptyTableModel";
306cdf0e10cSrcweir 
307cdf0e10cSrcweir         if ( !m_pDataWindow )
308cdf0e10cSrcweir             return "invalid data window!";
309cdf0e10cSrcweir 
310cdf0e10cSrcweir         if ( m_pModel->getColumnCount() != m_nColumnCount )
311cdf0e10cSrcweir             return "column counts are inconsistent!";
312cdf0e10cSrcweir 
313cdf0e10cSrcweir         if ( m_pModel->getRowCount() != m_nRowCount )
314cdf0e10cSrcweir             return "row counts are inconsistent!";
315cdf0e10cSrcweir 
316cdf0e10cSrcweir         if ( ( m_nCurColumn != COL_INVALID ) && !m_aColumnWidths.empty() && ( m_nCurColumn < 0 ) || ( m_nCurColumn >= (ColPos)m_aColumnWidths.size() ) )
317cdf0e10cSrcweir             return "current column is invalid!";
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         if ( !lcl_checkLimitsExclusive_OrDefault_OrFallback( m_nTopRow, (RowPos)-1, m_nRowCount, getModel(), (RowPos)0 ) )
320cdf0e10cSrcweir             return "invalid top row value!";
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         if ( !lcl_checkLimitsExclusive_OrDefault_OrFallback( m_nCurRow, (RowPos)-1, m_nRowCount, getModel(), ROW_INVALID ) )
323cdf0e10cSrcweir             return "invalid current row value!";
324cdf0e10cSrcweir 
325cdf0e10cSrcweir         if ( !lcl_checkLimitsExclusive_OrDefault_OrFallback( m_nLeftColumn, (ColPos)-1, m_nColumnCount, getModel(), (ColPos)0 ) )
326cdf0e10cSrcweir             return "invalid current column value!";
327cdf0e10cSrcweir 
328cdf0e10cSrcweir         if ( !lcl_checkLimitsExclusive_OrDefault_OrFallback( m_nCurColumn, (ColPos)-1, m_nColumnCount, getModel(), COL_INVALID ) )
329cdf0e10cSrcweir             return "invalid current column value!";
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         if  ( m_pInputHandler != m_pModel->getInputHandler() )
332cdf0e10cSrcweir             return "input handler is not the model-provided one!";
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         // m_aSelectedRows should have reasonable content
335cdf0e10cSrcweir         {
336cdf0e10cSrcweir             if ( m_aSelectedRows.size() > size_t( m_pModel->getRowCount() ) )
337cdf0e10cSrcweir                 return "there are more rows selected than actually exist";
338cdf0e10cSrcweir             for (   ::std::vector< RowPos >::const_iterator selRow = m_aSelectedRows.begin();
339cdf0e10cSrcweir                     selRow != m_aSelectedRows.end();
340cdf0e10cSrcweir                     ++selRow
341cdf0e10cSrcweir                 )
342cdf0e10cSrcweir             {
343cdf0e10cSrcweir                 if ( ( *selRow < 0 ) || ( *selRow >= m_pModel->getRowCount() ) )
344cdf0e10cSrcweir                     return "a non-existent row is selected";
345cdf0e10cSrcweir             }
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         // m_nColHeaderHeightPixel consistent with the model's value?
349cdf0e10cSrcweir         {
350cdf0e10cSrcweir             TableMetrics nHeaderHeight = m_pModel->hasColumnHeaders() ? m_pModel->getColumnHeaderHeight() : 0;
351cdf0e10cSrcweir 			nHeaderHeight = m_rAntiImpl.LogicToPixel( Size( 0, nHeaderHeight ), MAP_APPFONT ).Height();
352cdf0e10cSrcweir             if ( nHeaderHeight != m_nColHeaderHeightPixel )
353cdf0e10cSrcweir                 return "column header heights are inconsistent!";
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir         bool isDummyModel = dynamic_cast< const EmptyTableModel* >( m_pModel.get() ) != NULL;
357cdf0e10cSrcweir         if ( !isDummyModel )
358cdf0e10cSrcweir         {
359cdf0e10cSrcweir             TableMetrics nRowHeight = m_pModel->getRowHeight();
360cdf0e10cSrcweir 			nRowHeight = m_rAntiImpl.LogicToPixel( Size( 0, nRowHeight ), MAP_APPFONT).Height();
361cdf0e10cSrcweir             if ( nRowHeight != m_nRowHeightPixel )
362cdf0e10cSrcweir                 return "row heights are inconsistent!";
363cdf0e10cSrcweir         }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         // m_nRowHeaderWidthPixel consistent with the model's value?
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             TableMetrics nHeaderWidth = m_pModel->hasRowHeaders() ? m_pModel->getRowHeaderWidth() : 0;
368cdf0e10cSrcweir 			nHeaderWidth = m_rAntiImpl.LogicToPixel( Size( nHeaderWidth, 0 ), MAP_APPFONT ).Width();
369cdf0e10cSrcweir             if ( nHeaderWidth != m_nRowHeaderWidthPixel )
370cdf0e10cSrcweir                 return "row header widths are inconsistent!";
371cdf0e10cSrcweir         }
372cdf0e10cSrcweir 
373cdf0e10cSrcweir         // m_aColumnWidths consistency
374cdf0e10cSrcweir         if ( size_t( m_nColumnCount ) != m_aColumnWidths.size() )
375cdf0e10cSrcweir             return "wrong number of cached column widths";
376cdf0e10cSrcweir 
377cdf0e10cSrcweir         for (   ColumnPositions::const_iterator col = m_aColumnWidths.begin();
378cdf0e10cSrcweir                 col != m_aColumnWidths.end();
379cdf0e10cSrcweir             )
380cdf0e10cSrcweir         {
381cdf0e10cSrcweir             if ( col->getEnd() < col->getStart() )
382cdf0e10cSrcweir                 return "column widths: 'end' is expected to not be smaller than start";
383cdf0e10cSrcweir 
384cdf0e10cSrcweir             ColumnPositions::const_iterator nextCol = col + 1;
385cdf0e10cSrcweir             if ( nextCol != m_aColumnWidths.end() )
386cdf0e10cSrcweir                 if ( col->getEnd() != nextCol->getStart() )
387cdf0e10cSrcweir                     return "column widths: one column's end should be the next column's start";
388cdf0e10cSrcweir             col = nextCol;
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir         if ( m_nLeftColumn < m_nColumnCount )
392cdf0e10cSrcweir             if ( m_aColumnWidths[ m_nLeftColumn ].getStart() != m_nRowHeaderWidthPixel )
393cdf0e10cSrcweir                 return "the left-most column should start immediately after the row header";
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         if ( m_nCursorHidden < 0 )
396cdf0e10cSrcweir             return "invalid hidden count for the cursor!";
397cdf0e10cSrcweir 
398cdf0e10cSrcweir         if ( ( m_nRequiredInvariants & INV_SCROLL_POSITION ) && m_pVScroll )
399cdf0e10cSrcweir         {
400cdf0e10cSrcweir             DBG_SUSPEND_INV( INV_SCROLL_POSITION );
401cdf0e10cSrcweir                 // prevent infinite recursion
402cdf0e10cSrcweir 
403cdf0e10cSrcweir             if ( m_nLeftColumn < 0 )
404cdf0e10cSrcweir                 return "invalid left-most column index";
405cdf0e10cSrcweir             if ( m_pVScroll->GetThumbPos() != m_nTopRow )
406cdf0e10cSrcweir                 return "vertical scroll bar |position| is incorrect!";
407cdf0e10cSrcweir             if ( m_pVScroll->GetRange().Max() != m_nRowCount )
408cdf0e10cSrcweir                 return "vertical scroll bar |range| is incorrect!";
409cdf0e10cSrcweir             if ( m_pVScroll->GetVisibleSize() != impl_getVisibleRows( false ) )
410cdf0e10cSrcweir                 return "vertical scroll bar |visible size| is incorrect!";
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir         if ( ( m_nRequiredInvariants & INV_SCROLL_POSITION ) && m_pHScroll )
414cdf0e10cSrcweir         {
415cdf0e10cSrcweir             DBG_SUSPEND_INV( INV_SCROLL_POSITION );
416cdf0e10cSrcweir                 // prevent infinite recursion
417cdf0e10cSrcweir 
418cdf0e10cSrcweir             if ( m_pHScroll->GetThumbPos() != m_nLeftColumn )
419cdf0e10cSrcweir                 return "horizontal scroll bar |position| is incorrect!";
420cdf0e10cSrcweir             if ( m_pHScroll->GetRange().Max() != m_nColumnCount )
421cdf0e10cSrcweir                 return "horizontal scroll bar |range| is incorrect!";
422cdf0e10cSrcweir             if ( m_pHScroll->GetVisibleSize() != impl_getVisibleColumns( false ) )
423cdf0e10cSrcweir                 return "horizontal scroll bar |visible size| is incorrect!";
424cdf0e10cSrcweir         }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir         return NULL;
427cdf0e10cSrcweir     }
428cdf0e10cSrcweir #endif
429cdf0e10cSrcweir 
430cdf0e10cSrcweir #define DBG_CHECK_ME() \
431cdf0e10cSrcweir     DBG_CHKTHIS( TableControl_Impl, TableControl_Impl_checkInvariants )
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
434cdf0e10cSrcweir     TableControl_Impl::TableControl_Impl( TableControl& _rAntiImpl )
435cdf0e10cSrcweir         :m_rAntiImpl            ( _rAntiImpl                    )
436cdf0e10cSrcweir         ,m_pModel               ( new EmptyTableModel           )
437cdf0e10cSrcweir         ,m_pInputHandler        (                               )
438cdf0e10cSrcweir         ,m_nRowHeightPixel      ( 15                            )
439cdf0e10cSrcweir         ,m_nColHeaderHeightPixel( 0                             )
440cdf0e10cSrcweir         ,m_nRowHeaderWidthPixel ( 0                             )
441cdf0e10cSrcweir         ,m_nColumnCount         ( 0                             )
442cdf0e10cSrcweir         ,m_nRowCount            ( 0                             )
443cdf0e10cSrcweir         ,m_bColumnsFit          ( true                          )
444cdf0e10cSrcweir         ,m_nCurColumn           ( COL_INVALID                   )
445cdf0e10cSrcweir         ,m_nCurRow              ( ROW_INVALID                   )
446cdf0e10cSrcweir         ,m_nLeftColumn          ( 0                             )
447cdf0e10cSrcweir         ,m_nTopRow              ( 0                             )
448cdf0e10cSrcweir         ,m_nCursorHidden        ( 1                             )
449cdf0e10cSrcweir         ,m_pDataWindow          ( new TableDataWindow( *this )  )
450cdf0e10cSrcweir         ,m_pVScroll             ( NULL                          )
451cdf0e10cSrcweir         ,m_pHScroll             ( NULL                          )
452cdf0e10cSrcweir         ,m_pScrollCorner        ( NULL                          )
453cdf0e10cSrcweir         ,m_pSelEngine           (                               )
454cdf0e10cSrcweir         ,m_aSelectedRows        (                               )
455cdf0e10cSrcweir         ,m_pTableFunctionSet    ( new TableFunctionSet( this  ) )
456cdf0e10cSrcweir         ,m_nAnchor              ( -1                            )
457cdf0e10cSrcweir         ,m_bUpdatingColWidths   ( false                         )
458cdf0e10cSrcweir         ,m_pAccessibleTable     ( NULL                          )
459cdf0e10cSrcweir #if DBG_UTIL
460cdf0e10cSrcweir         ,m_nRequiredInvariants ( INV_SCROLL_POSITION )
461cdf0e10cSrcweir #endif
462cdf0e10cSrcweir     {
463cdf0e10cSrcweir         DBG_CTOR( TableControl_Impl, TableControl_Impl_checkInvariants );
464cdf0e10cSrcweir         m_pSelEngine = new SelectionEngine( m_pDataWindow.get(), m_pTableFunctionSet );
465cdf0e10cSrcweir         m_pSelEngine->SetSelectionMode(SINGLE_SELECTION);
466cdf0e10cSrcweir         m_pDataWindow->SetPosPixel( Point( 0, 0 ) );
467cdf0e10cSrcweir         m_pDataWindow->Show();
468cdf0e10cSrcweir     }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
471cdf0e10cSrcweir     TableControl_Impl::~TableControl_Impl()
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir         DBG_DTOR( TableControl_Impl, TableControl_Impl_checkInvariants );
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         DELETEZ( m_pVScroll );
476cdf0e10cSrcweir         DELETEZ( m_pHScroll );
477cdf0e10cSrcweir         DELETEZ( m_pScrollCorner );
478cdf0e10cSrcweir 		DELETEZ( m_pTableFunctionSet );
479cdf0e10cSrcweir 		DELETEZ( m_pSelEngine );
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
483cdf0e10cSrcweir     void TableControl_Impl::setModel( PTableModel _pModel )
484cdf0e10cSrcweir     {
485cdf0e10cSrcweir         DBG_CHECK_ME();
486cdf0e10cSrcweir 
487cdf0e10cSrcweir         SuppressCursor aHideCursor( *this );
488cdf0e10cSrcweir 
489cdf0e10cSrcweir         if ( !!m_pModel )
490cdf0e10cSrcweir             m_pModel->removeTableModelListener( shared_from_this() );
491cdf0e10cSrcweir 
492cdf0e10cSrcweir         m_pModel = _pModel;
493cdf0e10cSrcweir         if ( !m_pModel)
494cdf0e10cSrcweir             m_pModel.reset( new EmptyTableModel );
495cdf0e10cSrcweir 
496cdf0e10cSrcweir         m_pModel->addTableModelListener( shared_from_this() );
497cdf0e10cSrcweir 
498cdf0e10cSrcweir         m_nCurRow = ROW_INVALID;
499cdf0e10cSrcweir         m_nCurColumn = COL_INVALID;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir         // recalc some model-dependent cached info
502cdf0e10cSrcweir         impl_ni_updateCachedModelValues();
503cdf0e10cSrcweir         impl_ni_relayout();
504cdf0e10cSrcweir 
505cdf0e10cSrcweir         // completely invalidate
506cdf0e10cSrcweir         m_rAntiImpl.Invalidate();
507cdf0e10cSrcweir 
508cdf0e10cSrcweir         // reset cursor to (0,0)
509cdf0e10cSrcweir         if ( m_nRowCount ) m_nCurRow = 0;
510cdf0e10cSrcweir         if ( m_nColumnCount ) m_nCurColumn = 0;
511cdf0e10cSrcweir     }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
514cdf0e10cSrcweir     namespace
515cdf0e10cSrcweir     {
516cdf0e10cSrcweir         bool lcl_adjustSelectedRows( ::std::vector< RowPos >& io_selectionIndexes, RowPos const i_firstAffectedRowIndex, TableSize const i_offset )
517cdf0e10cSrcweir         {
518cdf0e10cSrcweir             bool didChanges = false;
519cdf0e10cSrcweir             for (   ::std::vector< RowPos >::iterator selPos = io_selectionIndexes.begin();
520cdf0e10cSrcweir                     selPos != io_selectionIndexes.end();
521cdf0e10cSrcweir                     ++selPos
522cdf0e10cSrcweir                 )
523cdf0e10cSrcweir             {
524cdf0e10cSrcweir                 if ( *selPos < i_firstAffectedRowIndex )
525cdf0e10cSrcweir                     continue;
526cdf0e10cSrcweir                 *selPos += i_offset;
527cdf0e10cSrcweir                 didChanges = true;
528cdf0e10cSrcweir             }
529cdf0e10cSrcweir             return didChanges;
530cdf0e10cSrcweir         }
531cdf0e10cSrcweir     }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
534cdf0e10cSrcweir     void TableControl_Impl::rowsInserted( RowPos i_first, RowPos i_last )
535cdf0e10cSrcweir     {
536cdf0e10cSrcweir         DBG_CHECK_ME();
537cdf0e10cSrcweir         OSL_PRECOND( i_last >= i_first, "TableControl_Impl::rowsInserted: invalid row indexes!" );
538cdf0e10cSrcweir 
539cdf0e10cSrcweir         TableSize const insertedRows = i_last - i_first + 1;
540cdf0e10cSrcweir 
541cdf0e10cSrcweir         // adjust selection, if necessary
542cdf0e10cSrcweir         bool const selectionChanged = lcl_adjustSelectedRows( m_aSelectedRows, i_first, insertedRows );
543cdf0e10cSrcweir 
544cdf0e10cSrcweir         // adjust our cached row count
545cdf0e10cSrcweir         m_nRowCount = m_pModel->getRowCount();
546cdf0e10cSrcweir 
547cdf0e10cSrcweir         // if the rows have been inserted before the current row, adjust this
548cdf0e10cSrcweir         if ( i_first <= m_nCurRow )
549cdf0e10cSrcweir             goTo( m_nCurColumn, m_nCurRow + insertedRows );
550cdf0e10cSrcweir 
551cdf0e10cSrcweir         // relayout, since the scrollbar need might have changed
552cdf0e10cSrcweir         impl_ni_relayout();
553cdf0e10cSrcweir 
554cdf0e10cSrcweir         // notify A1YY events
555cdf0e10cSrcweir         if ( impl_isAccessibleAlive() )
556cdf0e10cSrcweir 	    {
557cdf0e10cSrcweir             impl_commitAccessibleEvent( AccessibleEventId::TABLE_MODEL_CHANGED,
558cdf0e10cSrcweir                 makeAny( AccessibleTableModelChange( AccessibleTableModelChangeType::INSERT, i_first, i_last, 0, m_pModel->getColumnCount() ) ),
559cdf0e10cSrcweir 			    Any()
560cdf0e10cSrcweir             );
561cdf0e10cSrcweir 	    }
562cdf0e10cSrcweir 
563cdf0e10cSrcweir         // schedule repaint
564cdf0e10cSrcweir         invalidateRowRange( i_first, ROW_INVALID );
565cdf0e10cSrcweir 
566cdf0e10cSrcweir         // call selection handlers, if necessary
567cdf0e10cSrcweir         if ( selectionChanged )
568cdf0e10cSrcweir             m_rAntiImpl.Select();
569cdf0e10cSrcweir     }
570cdf0e10cSrcweir 
571cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
572cdf0e10cSrcweir     void TableControl_Impl::rowsRemoved( RowPos i_first, RowPos i_last )
573cdf0e10cSrcweir     {
574cdf0e10cSrcweir         sal_Int32 firstRemovedRow = i_first;
575cdf0e10cSrcweir         sal_Int32 lastRemovedRow = i_last;
576cdf0e10cSrcweir 
577cdf0e10cSrcweir         // adjust selection, if necessary
578cdf0e10cSrcweir         bool selectionChanged = false;
579cdf0e10cSrcweir 	    if ( i_first == -1 )
580cdf0e10cSrcweir 	    {
581cdf0e10cSrcweir             selectionChanged = markAllRowsAsDeselected();
582cdf0e10cSrcweir 
583cdf0e10cSrcweir             firstRemovedRow = 0;
584cdf0e10cSrcweir             lastRemovedRow = m_nRowCount - 1;
585cdf0e10cSrcweir 	    }
586cdf0e10cSrcweir 	    else
587cdf0e10cSrcweir 	    {
588cdf0e10cSrcweir             ENSURE_OR_RETURN_VOID( i_last >= i_first, "TableControl_Impl::rowsRemoved: illegal indexes!" );
589cdf0e10cSrcweir 
590cdf0e10cSrcweir             for ( sal_Int32 row = i_first; row <= i_last; ++row )
591cdf0e10cSrcweir             {
592cdf0e10cSrcweir                 if ( markRowAsDeselected( row ) )
593cdf0e10cSrcweir                     selectionChanged = true;
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir             if ( lcl_adjustSelectedRows( m_aSelectedRows, i_last + 1, i_first - i_last - 1 ) )
597cdf0e10cSrcweir                 selectionChanged = true;
598cdf0e10cSrcweir 	    }
599cdf0e10cSrcweir 
600cdf0e10cSrcweir         // adjust cached row count
601cdf0e10cSrcweir         m_nRowCount = m_pModel->getRowCount();
602cdf0e10cSrcweir 
603cdf0e10cSrcweir         // adjust the current row, if it is larger than the row count now
604cdf0e10cSrcweir         if ( m_nCurRow >= m_nRowCount )
605cdf0e10cSrcweir         {
606cdf0e10cSrcweir             if ( m_nRowCount > 0 )
607cdf0e10cSrcweir                 goTo( m_nCurColumn, m_nRowCount - 1 );
608cdf0e10cSrcweir             else
609cdf0e10cSrcweir                 m_nCurRow = ROW_INVALID;
610cdf0e10cSrcweir         }
611cdf0e10cSrcweir 
612cdf0e10cSrcweir         // relayout, since the scrollbar need might have changed
613cdf0e10cSrcweir         impl_ni_relayout();
614cdf0e10cSrcweir 
615cdf0e10cSrcweir         // notify A11Y events
616cdf0e10cSrcweir         if ( impl_isAccessibleAlive() )
617cdf0e10cSrcweir 	    {
618cdf0e10cSrcweir 		    commitTableEvent(
619cdf0e10cSrcweir                 AccessibleEventId::TABLE_MODEL_CHANGED,
620cdf0e10cSrcweir                 makeAny( AccessibleTableModelChange(
621cdf0e10cSrcweir                     AccessibleTableModelChangeType::DELETE,
622cdf0e10cSrcweir                     firstRemovedRow,
623cdf0e10cSrcweir                     lastRemovedRow,
624cdf0e10cSrcweir                     0,
625cdf0e10cSrcweir                     m_pModel->getColumnCount()
626cdf0e10cSrcweir                 ) ),
627cdf0e10cSrcweir 			    Any()
628cdf0e10cSrcweir             );
629cdf0e10cSrcweir 	    }
630cdf0e10cSrcweir 
631cdf0e10cSrcweir         // schedule a repaint
632cdf0e10cSrcweir         invalidateRowRange( firstRemovedRow, ROW_INVALID );
633cdf0e10cSrcweir 
634cdf0e10cSrcweir         // call selection handlers, if necessary
635cdf0e10cSrcweir         if ( selectionChanged )
636cdf0e10cSrcweir             m_rAntiImpl.Select();
637cdf0e10cSrcweir     }
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
640cdf0e10cSrcweir     void TableControl_Impl::columnInserted( ColPos const i_colIndex )
641cdf0e10cSrcweir     {
642cdf0e10cSrcweir         m_nColumnCount = m_pModel->getColumnCount();
643cdf0e10cSrcweir         impl_ni_relayout();
644cdf0e10cSrcweir 
645cdf0e10cSrcweir         m_rAntiImpl.Invalidate();
646cdf0e10cSrcweir 
647cdf0e10cSrcweir         OSL_UNUSED( i_colIndex );
648cdf0e10cSrcweir    }
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
651cdf0e10cSrcweir     void TableControl_Impl::columnRemoved( ColPos const i_colIndex )
652cdf0e10cSrcweir     {
653cdf0e10cSrcweir         m_nColumnCount = m_pModel->getColumnCount();
654cdf0e10cSrcweir 
655cdf0e10cSrcweir         // adjust the current column, if it is larger than the column count now
656cdf0e10cSrcweir         if ( m_nCurColumn >= m_nColumnCount )
657cdf0e10cSrcweir         {
658cdf0e10cSrcweir             if ( m_nColumnCount > 0 )
659cdf0e10cSrcweir                 goTo( m_nCurColumn - 1, m_nCurRow );
660cdf0e10cSrcweir             else
661cdf0e10cSrcweir                 m_nCurColumn = COL_INVALID;
662cdf0e10cSrcweir         }
663cdf0e10cSrcweir 
664cdf0e10cSrcweir         impl_ni_relayout();
665cdf0e10cSrcweir 
666cdf0e10cSrcweir         m_rAntiImpl.Invalidate();
667cdf0e10cSrcweir 
668cdf0e10cSrcweir         OSL_UNUSED( i_colIndex );
669cdf0e10cSrcweir     }
670cdf0e10cSrcweir 
671cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
672cdf0e10cSrcweir     void TableControl_Impl::allColumnsRemoved()
673cdf0e10cSrcweir     {
674cdf0e10cSrcweir         m_nColumnCount = m_pModel->getColumnCount();
675cdf0e10cSrcweir         impl_ni_relayout();
676cdf0e10cSrcweir 
677cdf0e10cSrcweir         m_rAntiImpl.Invalidate();
678cdf0e10cSrcweir     }
679cdf0e10cSrcweir 
680cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
681cdf0e10cSrcweir     void TableControl_Impl::cellsUpdated( ColPos const i_firstCol, ColPos i_lastCol, RowPos const i_firstRow, RowPos const i_lastRow )
682cdf0e10cSrcweir     {
683cdf0e10cSrcweir 		invalidateRowRange( i_firstRow, i_lastRow );
684cdf0e10cSrcweir 
685cdf0e10cSrcweir         OSL_UNUSED( i_firstCol );
686cdf0e10cSrcweir         OSL_UNUSED( i_lastCol );
687cdf0e10cSrcweir     }
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
690cdf0e10cSrcweir     void TableControl_Impl::tableMetricsChanged()
691cdf0e10cSrcweir     {
692cdf0e10cSrcweir         impl_ni_updateCachedTableMetrics();
693cdf0e10cSrcweir         impl_ni_relayout();
694cdf0e10cSrcweir         m_rAntiImpl.Invalidate();
695cdf0e10cSrcweir     }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
698cdf0e10cSrcweir     void TableControl_Impl::impl_invalidateColumn( ColPos const i_column )
699cdf0e10cSrcweir     {
700cdf0e10cSrcweir         DBG_CHECK_ME();
701cdf0e10cSrcweir 
702cdf0e10cSrcweir         Rectangle const aAllCellsArea( impl_getAllVisibleCellsArea() );
703cdf0e10cSrcweir 
704cdf0e10cSrcweir         const TableColumnGeometry aColumn( *this, aAllCellsArea, i_column );
705cdf0e10cSrcweir         if ( aColumn.isValid() )
706cdf0e10cSrcweir             m_rAntiImpl.Invalidate( aColumn.getRect() );
707cdf0e10cSrcweir     }
708cdf0e10cSrcweir 
709cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
710cdf0e10cSrcweir     void TableControl_Impl::columnChanged( ColPos const i_column, ColumnAttributeGroup const i_attributeGroup )
711cdf0e10cSrcweir     {
712cdf0e10cSrcweir         ColumnAttributeGroup nGroup( i_attributeGroup );
713cdf0e10cSrcweir         if ( nGroup & COL_ATTRS_APPEARANCE )
714cdf0e10cSrcweir         {
715cdf0e10cSrcweir             impl_invalidateColumn( i_column );
716cdf0e10cSrcweir             nGroup &= ~COL_ATTRS_APPEARANCE;
717cdf0e10cSrcweir         }
718cdf0e10cSrcweir 
719cdf0e10cSrcweir         if ( nGroup & COL_ATTRS_WIDTH )
720cdf0e10cSrcweir         {
721cdf0e10cSrcweir             if ( !m_bUpdatingColWidths )
722cdf0e10cSrcweir             {
723cdf0e10cSrcweir                 impl_ni_relayout( i_column );
724cdf0e10cSrcweir                 invalidate( TableAreaAll );
725cdf0e10cSrcweir             }
726cdf0e10cSrcweir 
727cdf0e10cSrcweir             nGroup &= ~COL_ATTRS_WIDTH;
728cdf0e10cSrcweir         }
729cdf0e10cSrcweir 
730cdf0e10cSrcweir         OSL_ENSURE( ( nGroup == COL_ATTRS_NONE ) || ( i_attributeGroup == COL_ATTRS_ALL ),
731cdf0e10cSrcweir             "TableControl_Impl::columnChanged: don't know how to handle this change!" );
732cdf0e10cSrcweir     }
733cdf0e10cSrcweir 
734cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
735cdf0e10cSrcweir     Rectangle TableControl_Impl::impl_getAllVisibleCellsArea() const
736cdf0e10cSrcweir     {
737cdf0e10cSrcweir         DBG_CHECK_ME();
738cdf0e10cSrcweir 
739cdf0e10cSrcweir         Rectangle aArea( Point( 0, 0 ), Size( 0, 0 ) );
740cdf0e10cSrcweir 
741cdf0e10cSrcweir         // determine the right-most border of the last column which is
742cdf0e10cSrcweir         // at least partially visible
743cdf0e10cSrcweir         aArea.Right() = m_nRowHeaderWidthPixel;
744cdf0e10cSrcweir         if ( !m_aColumnWidths.empty() )
745cdf0e10cSrcweir         {
746cdf0e10cSrcweir             // the number of pixels which are scrolled out of the left hand
747cdf0e10cSrcweir             // side of the window
748cdf0e10cSrcweir             const long nScrolledOutLeft = m_nLeftColumn == 0 ? 0 : m_aColumnWidths[ m_nLeftColumn - 1 ].getEnd();
749cdf0e10cSrcweir 
750cdf0e10cSrcweir             ColumnPositions::const_reverse_iterator loop = m_aColumnWidths.rbegin();
751cdf0e10cSrcweir             do
752cdf0e10cSrcweir             {
753cdf0e10cSrcweir                 aArea.Right() = loop->getEnd() - nScrolledOutLeft + m_nRowHeaderWidthPixel;
754cdf0e10cSrcweir                 ++loop;
755cdf0e10cSrcweir             }
756cdf0e10cSrcweir             while ( (   loop != m_aColumnWidths.rend() )
757cdf0e10cSrcweir                  && (   loop->getEnd() - nScrolledOutLeft >= aArea.Right() )
758cdf0e10cSrcweir                  );
759cdf0e10cSrcweir         }
760cdf0e10cSrcweir         // so far, aArea.Right() denotes the first pixel *after* the cell area
761cdf0e10cSrcweir         --aArea.Right();
762cdf0e10cSrcweir 
763cdf0e10cSrcweir         // determine the last row which is at least partially visible
764cdf0e10cSrcweir         aArea.Bottom() =
765cdf0e10cSrcweir                 m_nColHeaderHeightPixel
766cdf0e10cSrcweir             +   impl_getVisibleRows( true ) * m_nRowHeightPixel
767cdf0e10cSrcweir             -   1;
768cdf0e10cSrcweir 
769cdf0e10cSrcweir         return aArea;
770cdf0e10cSrcweir     }
771cdf0e10cSrcweir 
772cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
773cdf0e10cSrcweir     Rectangle TableControl_Impl::impl_getAllVisibleDataCellArea() const
774cdf0e10cSrcweir     {
775cdf0e10cSrcweir         DBG_CHECK_ME();
776cdf0e10cSrcweir 
777cdf0e10cSrcweir         Rectangle aArea( impl_getAllVisibleCellsArea() );
778cdf0e10cSrcweir         aArea.Left() = m_nRowHeaderWidthPixel;
779cdf0e10cSrcweir         aArea.Top() = m_nColHeaderHeightPixel;
780cdf0e10cSrcweir         return aArea;
781cdf0e10cSrcweir     }
782cdf0e10cSrcweir 
783cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
784cdf0e10cSrcweir     void TableControl_Impl::impl_ni_updateCachedTableMetrics()
785cdf0e10cSrcweir     {
786cdf0e10cSrcweir         m_nRowHeightPixel = m_rAntiImpl.LogicToPixel( Size( 0, m_pModel->getRowHeight() ), MAP_APPFONT ).Height();
787cdf0e10cSrcweir 
788cdf0e10cSrcweir         m_nColHeaderHeightPixel = 0;
789cdf0e10cSrcweir 	    if ( m_pModel->hasColumnHeaders() )
790cdf0e10cSrcweir            m_nColHeaderHeightPixel = m_rAntiImpl.LogicToPixel( Size( 0, m_pModel->getColumnHeaderHeight() ), MAP_APPFONT ).Height();
791cdf0e10cSrcweir 
792cdf0e10cSrcweir         m_nRowHeaderWidthPixel = 0;
793cdf0e10cSrcweir         if ( m_pModel->hasRowHeaders() )
794cdf0e10cSrcweir             m_nRowHeaderWidthPixel = m_rAntiImpl.LogicToPixel( Size( m_pModel->getRowHeaderWidth(), 0 ), MAP_APPFONT).Width();
795cdf0e10cSrcweir     }
796cdf0e10cSrcweir 
797cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
798cdf0e10cSrcweir     void TableControl_Impl::impl_ni_updateCachedModelValues()
799cdf0e10cSrcweir     {
800cdf0e10cSrcweir         m_pInputHandler = m_pModel->getInputHandler();
801cdf0e10cSrcweir         if ( !m_pInputHandler )
802cdf0e10cSrcweir             m_pInputHandler.reset( new DefaultInputHandler );
803cdf0e10cSrcweir 
804cdf0e10cSrcweir         m_nColumnCount = m_pModel->getColumnCount();
805cdf0e10cSrcweir         if ( m_nLeftColumn >= m_nColumnCount )
806cdf0e10cSrcweir             m_nLeftColumn = ( m_nColumnCount > 0 ) ? m_nColumnCount - 1 : 0;
807cdf0e10cSrcweir 
808cdf0e10cSrcweir         m_nRowCount = m_pModel->getRowCount();
809cdf0e10cSrcweir         if ( m_nTopRow >= m_nRowCount )
810cdf0e10cSrcweir             m_nTopRow = ( m_nRowCount > 0 ) ? m_nRowCount - 1 : 0;
811cdf0e10cSrcweir 
812cdf0e10cSrcweir         impl_ni_updateCachedTableMetrics();
813cdf0e10cSrcweir     }
814cdf0e10cSrcweir 
815cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
816cdf0e10cSrcweir     namespace
817cdf0e10cSrcweir     {
818cdf0e10cSrcweir 	    //..............................................................................................................
819cdf0e10cSrcweir         /// determines whether a scrollbar is needed for the given values
820cdf0e10cSrcweir         bool lcl_determineScrollbarNeed( long const i_position, ScrollbarVisibility const i_visibility,
821cdf0e10cSrcweir             long const i_availableSpace, long const i_neededSpace )
822cdf0e10cSrcweir         {
823cdf0e10cSrcweir             if ( i_visibility == ScrollbarShowNever )
824cdf0e10cSrcweir                 return false;
825cdf0e10cSrcweir             if ( i_visibility == ScrollbarShowAlways )
826cdf0e10cSrcweir                 return true;
827cdf0e10cSrcweir             if ( i_position > 0 )
828cdf0e10cSrcweir                 return true;
829cdf0e10cSrcweir             if ( i_availableSpace >= i_neededSpace )
830cdf0e10cSrcweir                 return false;
831cdf0e10cSrcweir             return true;
832cdf0e10cSrcweir         }
833cdf0e10cSrcweir 
834cdf0e10cSrcweir 	    //..............................................................................................................
835cdf0e10cSrcweir         void lcl_setButtonRepeat( Window& _rWindow, sal_uLong _nDelay )
836cdf0e10cSrcweir         {
837cdf0e10cSrcweir             AllSettings aSettings = _rWindow.GetSettings();
838cdf0e10cSrcweir 	        MouseSettings aMouseSettings = aSettings.GetMouseSettings();
839cdf0e10cSrcweir 
840cdf0e10cSrcweir             aMouseSettings.SetButtonRepeat( _nDelay );
841cdf0e10cSrcweir 	        aSettings.SetMouseSettings( aMouseSettings );
842cdf0e10cSrcweir 
843cdf0e10cSrcweir 	        _rWindow.SetSettings( aSettings, sal_True );
844cdf0e10cSrcweir         }
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 	    //..............................................................................................................
847cdf0e10cSrcweir         bool lcl_updateScrollbar( Window& _rParent, ScrollBar*& _rpBar,
848cdf0e10cSrcweir             bool const i_needBar, long _nVisibleUnits,
849cdf0e10cSrcweir             long _nPosition, long _nLineSize, long _nRange,
850cdf0e10cSrcweir             bool _bHorizontal, const Link& _rScrollHandler )
851cdf0e10cSrcweir         {
852cdf0e10cSrcweir             // do we currently have the scrollbar?
853cdf0e10cSrcweir             bool bHaveBar = _rpBar != NULL;
854cdf0e10cSrcweir 
855cdf0e10cSrcweir             // do we need to correct the scrollbar visibility?
856cdf0e10cSrcweir             if ( bHaveBar && !i_needBar )
857cdf0e10cSrcweir             {
858cdf0e10cSrcweir                 if ( _rpBar->IsTracking() )
859cdf0e10cSrcweir                     _rpBar->EndTracking();
860cdf0e10cSrcweir                 DELETEZ( _rpBar );
861cdf0e10cSrcweir             }
862cdf0e10cSrcweir             else if ( !bHaveBar && i_needBar )
863cdf0e10cSrcweir             {
864cdf0e10cSrcweir                 _rpBar = new ScrollBar(
865cdf0e10cSrcweir                     &_rParent,
866cdf0e10cSrcweir                     WB_DRAG | ( _bHorizontal ? WB_HSCROLL : WB_VSCROLL )
867cdf0e10cSrcweir                 );
868cdf0e10cSrcweir                 _rpBar->SetScrollHdl( _rScrollHandler );
869cdf0e10cSrcweir                 // get some speed into the scrolling ....
870cdf0e10cSrcweir                 lcl_setButtonRepeat( *_rpBar, 0 );
871cdf0e10cSrcweir             }
872cdf0e10cSrcweir 
873cdf0e10cSrcweir             if ( _rpBar )
874cdf0e10cSrcweir             {
875cdf0e10cSrcweir                 _rpBar->SetRange( Range( 0, _nRange ) );
876cdf0e10cSrcweir                 _rpBar->SetVisibleSize( _nVisibleUnits );
877cdf0e10cSrcweir                 _rpBar->SetPageSize( _nVisibleUnits );
878cdf0e10cSrcweir                 _rpBar->SetLineSize( _nLineSize );
879cdf0e10cSrcweir                 _rpBar->SetThumbPos( _nPosition );
880cdf0e10cSrcweir                 _rpBar->Show();
881cdf0e10cSrcweir             }
882cdf0e10cSrcweir 
883cdf0e10cSrcweir             return ( bHaveBar != i_needBar );
884cdf0e10cSrcweir         }
885cdf0e10cSrcweir 
886cdf0e10cSrcweir 	    //..............................................................................................................
887cdf0e10cSrcweir         /** returns the number of rows fitting into the given range,
888cdf0e10cSrcweir             for the given row height. Partially fitting rows are counted, too, if the
889cdf0e10cSrcweir             respective parameter says so.
890cdf0e10cSrcweir         */
891cdf0e10cSrcweir         TableSize lcl_getRowsFittingInto( long _nOverallHeight, long _nRowHeightPixel, bool _bAcceptPartialRow = false )
892cdf0e10cSrcweir         {
893cdf0e10cSrcweir             return  _bAcceptPartialRow
894cdf0e10cSrcweir                 ?   ( _nOverallHeight + ( _nRowHeightPixel - 1 ) ) / _nRowHeightPixel
895cdf0e10cSrcweir                 :   _nOverallHeight / _nRowHeightPixel;
896cdf0e10cSrcweir         }
897cdf0e10cSrcweir 
898cdf0e10cSrcweir 	    //..............................................................................................................
899cdf0e10cSrcweir         /** returns the number of columns fitting into the given area,
900cdf0e10cSrcweir             with the first visible column as given. Partially fitting columns are counted, too,
901cdf0e10cSrcweir             if the respective parameter says so.
902cdf0e10cSrcweir         */
903cdf0e10cSrcweir         TableSize lcl_getColumnsVisibleWithin( const Rectangle& _rArea, ColPos _nFirstVisibleColumn,
904cdf0e10cSrcweir             const TableControl_Impl& _rControl, bool _bAcceptPartialRow )
905cdf0e10cSrcweir         {
906cdf0e10cSrcweir             TableSize visibleColumns = 0;
907cdf0e10cSrcweir             TableColumnGeometry aColumn( _rControl, _rArea, _nFirstVisibleColumn );
908cdf0e10cSrcweir             while ( aColumn.isValid() )
909cdf0e10cSrcweir             {
910cdf0e10cSrcweir                 if ( !_bAcceptPartialRow )
911cdf0e10cSrcweir                     if ( aColumn.getRect().Right() > _rArea.Right() )
912cdf0e10cSrcweir                         // this column is only partially visible, and this is not allowed
913cdf0e10cSrcweir                         break;
914cdf0e10cSrcweir 
915cdf0e10cSrcweir                 aColumn.moveRight();
916cdf0e10cSrcweir                 ++visibleColumns;
917cdf0e10cSrcweir             }
918cdf0e10cSrcweir             return visibleColumns;
919cdf0e10cSrcweir         }
920cdf0e10cSrcweir 
921cdf0e10cSrcweir     }
922cdf0e10cSrcweir 
923cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
924cdf0e10cSrcweir     long TableControl_Impl::impl_ni_calculateColumnWidths( ColPos const i_assumeInflexibleColumnsUpToIncluding,
925cdf0e10cSrcweir         bool const i_assumeVerticalScrollbar, ::std::vector< long >& o_newColWidthsPixel ) const
926cdf0e10cSrcweir     {
927cdf0e10cSrcweir         // the available horizontal space
928cdf0e10cSrcweir 		long gridWidthPixel = m_rAntiImpl.GetOutputSizePixel().Width();
929cdf0e10cSrcweir         ENSURE_OR_RETURN( !!m_pModel, "TableControl_Impl::impl_ni_calculateColumnWidths: not allowed without a model!", gridWidthPixel );
930cdf0e10cSrcweir 		if ( m_pModel->hasRowHeaders() && ( gridWidthPixel != 0 ) )
931cdf0e10cSrcweir 		{
932cdf0e10cSrcweir 			gridWidthPixel -= m_nRowHeaderWidthPixel;
933cdf0e10cSrcweir 		}
934cdf0e10cSrcweir 
935cdf0e10cSrcweir         if ( i_assumeVerticalScrollbar && ( m_pModel->getVerticalScrollbarVisibility() != ScrollbarShowNever ) )
936cdf0e10cSrcweir 		{
937cdf0e10cSrcweir 	        long nScrollbarMetrics = m_rAntiImpl.GetSettings().GetStyleSettings().GetScrollBarSize();
938cdf0e10cSrcweir 			gridWidthPixel -= nScrollbarMetrics;
939cdf0e10cSrcweir 		}
940cdf0e10cSrcweir 
941cdf0e10cSrcweir         // no need to do anything without columns
942cdf0e10cSrcweir         TableSize const colCount = m_pModel->getColumnCount();
943cdf0e10cSrcweir 		if ( colCount == 0 )
944cdf0e10cSrcweir             return gridWidthPixel;
945cdf0e10cSrcweir 
946cdf0e10cSrcweir         // collect some meta data for our columns:
947cdf0e10cSrcweir         // - their current (pixel) metrics
948cdf0e10cSrcweir         long accumulatedCurrentWidth = 0;
949cdf0e10cSrcweir         ::std::vector< long > currentColWidths;
950cdf0e10cSrcweir         currentColWidths.reserve( colCount );
951cdf0e10cSrcweir         typedef ::std::vector< ::std::pair< long, long > >   ColumnLimits;
952cdf0e10cSrcweir         ColumnLimits effectiveColumnLimits;
953cdf0e10cSrcweir         effectiveColumnLimits.reserve( colCount );
954cdf0e10cSrcweir         long accumulatedMinWidth = 0;
955cdf0e10cSrcweir         long accumulatedMaxWidth = 0;
956cdf0e10cSrcweir         // - their relative flexibility
957cdf0e10cSrcweir         ::std::vector< ::sal_Int32 > columnFlexibilities;
958cdf0e10cSrcweir         columnFlexibilities.reserve( colCount );
959cdf0e10cSrcweir         long flexibilityDenominator = 0;
960cdf0e10cSrcweir         size_t flexibleColumnCount = 0;
961cdf0e10cSrcweir         for ( ColPos col = 0; col < colCount; ++col )
962cdf0e10cSrcweir         {
963cdf0e10cSrcweir 			PColumnModel const pColumn = m_pModel->getColumnModel( col );
964cdf0e10cSrcweir             ENSURE_OR_THROW( !!pColumn, "invalid column returned by the model!" );
965cdf0e10cSrcweir 
966cdf0e10cSrcweir             // current width
967cdf0e10cSrcweir             long const currentWidth = appFontWidthToPixel( pColumn->getWidth() );
968cdf0e10cSrcweir             currentColWidths.push_back( currentWidth );
969cdf0e10cSrcweir 
970cdf0e10cSrcweir             // accumulated width
971cdf0e10cSrcweir             accumulatedCurrentWidth += currentWidth;
972cdf0e10cSrcweir 
973cdf0e10cSrcweir             // flexibility
974cdf0e10cSrcweir             ::sal_Int32 flexibility = pColumn->getFlexibility();
975cdf0e10cSrcweir             OSL_ENSURE( flexibility >= 0, "TableControl_Impl::impl_ni_calculateColumnWidths: a column's flexibility should be non-negative." );
976cdf0e10cSrcweir             if  (   ( flexibility < 0 )                                 // normalization
977cdf0e10cSrcweir                 ||  ( !pColumn->isResizable() )                         // column not resizeable => no auto-resize
978cdf0e10cSrcweir                 ||  ( col <= i_assumeInflexibleColumnsUpToIncluding )   // column shall be treated as inflexible => respec this
979cdf0e10cSrcweir                 )
980cdf0e10cSrcweir                 flexibility = 0;
981cdf0e10cSrcweir 
982cdf0e10cSrcweir             // min/max width
983cdf0e10cSrcweir             long effectiveMin = currentWidth, effectiveMax = currentWidth;
984cdf0e10cSrcweir             // if the column is not flexible, it will not be asked for min/max, but we assume the current width as limit then
985cdf0e10cSrcweir             if ( flexibility > 0 )
986cdf0e10cSrcweir             {
987cdf0e10cSrcweir                 long const minWidth = appFontWidthToPixel( pColumn->getMinWidth() );
988cdf0e10cSrcweir                 if ( minWidth > 0 )
989cdf0e10cSrcweir                     effectiveMin = minWidth;
990cdf0e10cSrcweir                 else
991cdf0e10cSrcweir                     effectiveMin = MIN_COLUMN_WIDTH_PIXEL;
992cdf0e10cSrcweir 
993cdf0e10cSrcweir                 long const maxWidth = appFontWidthToPixel( pColumn->getMaxWidth() );
994cdf0e10cSrcweir                 OSL_ENSURE( minWidth <= maxWidth, "TableControl_Impl::impl_ni_calculateColumnWidths: pretty undecided 'bout its width limits, this column!" );
995cdf0e10cSrcweir                 if ( ( maxWidth > 0 ) && ( maxWidth >= minWidth ) )
996cdf0e10cSrcweir                     effectiveMax = maxWidth;
997cdf0e10cSrcweir                 else
998cdf0e10cSrcweir                     effectiveMax = gridWidthPixel; // TODO: any better guess here?
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir                 if ( effectiveMin == effectiveMax )
1001cdf0e10cSrcweir                     // if the min and the max are identical, this implies no flexibility at all
1002cdf0e10cSrcweir                     flexibility = 0;
1003cdf0e10cSrcweir             }
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir             columnFlexibilities.push_back( flexibility );
1006cdf0e10cSrcweir             flexibilityDenominator += flexibility;
1007cdf0e10cSrcweir             if ( flexibility > 0 )
1008cdf0e10cSrcweir                 ++flexibleColumnCount;
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir             effectiveColumnLimits.push_back( ::std::pair< long, long >( effectiveMin, effectiveMax ) );
1011cdf0e10cSrcweir             accumulatedMinWidth += effectiveMin;
1012cdf0e10cSrcweir             accumulatedMaxWidth += effectiveMax;
1013cdf0e10cSrcweir         }
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir         o_newColWidthsPixel = currentColWidths;
1016cdf0e10cSrcweir         if ( flexibilityDenominator == 0 )
1017cdf0e10cSrcweir         {
1018cdf0e10cSrcweir             // no column is flexible => don't adjust anything
1019cdf0e10cSrcweir         }
1020cdf0e10cSrcweir         else if ( gridWidthPixel > accumulatedCurrentWidth )
1021cdf0e10cSrcweir         {   // we have space to give away ...
1022cdf0e10cSrcweir             long distributePixel = gridWidthPixel - accumulatedCurrentWidth;
1023cdf0e10cSrcweir             if ( gridWidthPixel > accumulatedMaxWidth )
1024cdf0e10cSrcweir             {
1025cdf0e10cSrcweir                 // ... but the column's maximal widths are still less than we have
1026cdf0e10cSrcweir                 // => set them all to max
1027cdf0e10cSrcweir                 for ( size_t i = 0; i < size_t( colCount ); ++i )
1028cdf0e10cSrcweir                 {
1029cdf0e10cSrcweir                     o_newColWidthsPixel[i] = effectiveColumnLimits[i].second;
1030cdf0e10cSrcweir                 }
1031cdf0e10cSrcweir             }
1032cdf0e10cSrcweir             else
1033cdf0e10cSrcweir             {
1034cdf0e10cSrcweir                 bool startOver = false;
1035cdf0e10cSrcweir                 do
1036cdf0e10cSrcweir                 {
1037cdf0e10cSrcweir                     startOver = false;
1038cdf0e10cSrcweir                     // distribute the remaining space amongst all columns with a positive flexibility
1039cdf0e10cSrcweir                     for ( size_t i=0; i<o_newColWidthsPixel.size() && !startOver; ++i )
1040cdf0e10cSrcweir                     {
1041cdf0e10cSrcweir                         long const columnFlexibility = columnFlexibilities[i];
1042cdf0e10cSrcweir                         if ( columnFlexibility == 0 )
1043cdf0e10cSrcweir                             continue;
1044cdf0e10cSrcweir 
1045cdf0e10cSrcweir                         long newColWidth = currentColWidths[i] + columnFlexibility * distributePixel / flexibilityDenominator;
1046cdf0e10cSrcweir 
1047cdf0e10cSrcweir                         if ( newColWidth > effectiveColumnLimits[i].second )
1048cdf0e10cSrcweir                         {   // that was too much, we hit the col's maximum
1049cdf0e10cSrcweir                             // set the new width to exactly this maximum
1050cdf0e10cSrcweir                             newColWidth = effectiveColumnLimits[i].second;
1051cdf0e10cSrcweir                             // adjust the flexibility denominator ...
1052cdf0e10cSrcweir                             flexibilityDenominator -= columnFlexibility;
1053cdf0e10cSrcweir                             columnFlexibilities[i] = 0;
1054cdf0e10cSrcweir                             --flexibleColumnCount;
1055cdf0e10cSrcweir                             // ... and the remaining width ...
1056cdf0e10cSrcweir                             long const difference = newColWidth - currentColWidths[i];
1057cdf0e10cSrcweir                             distributePixel -= difference;
1058cdf0e10cSrcweir                             // ... this way, we ensure that the width not taken up by this column is consumed by the other
1059cdf0e10cSrcweir                             // flexible ones (if there are some)
1060cdf0e10cSrcweir 
1061cdf0e10cSrcweir                             // and start over with the first column, since there might be earlier columns which need
1062cdf0e10cSrcweir                             // to be recalculated now
1063cdf0e10cSrcweir                             startOver = true;
1064cdf0e10cSrcweir                         }
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir                         o_newColWidthsPixel[i] = newColWidth;
1067cdf0e10cSrcweir                     }
1068cdf0e10cSrcweir                 }
1069cdf0e10cSrcweir                 while ( startOver );
1070cdf0e10cSrcweir 
1071cdf0e10cSrcweir                 // are there pixels left (might be caused by rounding errors)?
1072cdf0e10cSrcweir                 distributePixel = gridWidthPixel - ::std::accumulate( o_newColWidthsPixel.begin(), o_newColWidthsPixel.end(), 0 );
1073cdf0e10cSrcweir                 while ( ( distributePixel > 0 ) && ( flexibleColumnCount > 0 ) )
1074cdf0e10cSrcweir                 {
1075cdf0e10cSrcweir                     // yes => ignore relative flexibilities, and subsequently distribute single pixels to all flexible
1076cdf0e10cSrcweir                     // columns which did not yet reach their maximum.
1077cdf0e10cSrcweir                     for ( size_t i=0; ( i < o_newColWidthsPixel.size() ) && ( distributePixel > 0 ); ++i )
1078cdf0e10cSrcweir                     {
1079cdf0e10cSrcweir                         if ( columnFlexibilities[i] == 0 )
1080cdf0e10cSrcweir                             continue;
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir                         OSL_ENSURE( o_newColWidthsPixel[i] <= effectiveColumnLimits[i].second,
1083cdf0e10cSrcweir                             "TableControl_Impl::impl_ni_calculateColumnWidths: inconsitency!" );
1084cdf0e10cSrcweir                         if ( o_newColWidthsPixel[i] >= effectiveColumnLimits[i].first )
1085cdf0e10cSrcweir                         {
1086cdf0e10cSrcweir                             columnFlexibilities[i] = 0;
1087cdf0e10cSrcweir                             --flexibleColumnCount;
1088cdf0e10cSrcweir                             continue;
1089cdf0e10cSrcweir                         }
1090cdf0e10cSrcweir 
1091cdf0e10cSrcweir                         ++o_newColWidthsPixel[i];
1092cdf0e10cSrcweir                         --distributePixel;
1093cdf0e10cSrcweir                     }
1094cdf0e10cSrcweir                 }
1095cdf0e10cSrcweir             }
1096cdf0e10cSrcweir         }
1097cdf0e10cSrcweir         else if ( gridWidthPixel < accumulatedCurrentWidth )
1098cdf0e10cSrcweir         {   // we need to take away some space from the columns which allow it ...
1099cdf0e10cSrcweir             long takeAwayPixel = accumulatedCurrentWidth - gridWidthPixel;
1100cdf0e10cSrcweir             if ( gridWidthPixel < accumulatedMinWidth )
1101cdf0e10cSrcweir             {
1102cdf0e10cSrcweir                 // ... but the column's minimal widths are still more than we have
1103cdf0e10cSrcweir                 // => set them all to min
1104cdf0e10cSrcweir                 for ( size_t i = 0; i < size_t( colCount ); ++i )
1105cdf0e10cSrcweir                 {
1106cdf0e10cSrcweir                     o_newColWidthsPixel[i] = effectiveColumnLimits[i].first;
1107cdf0e10cSrcweir                 }
1108cdf0e10cSrcweir             }
1109cdf0e10cSrcweir             else
1110cdf0e10cSrcweir             {
1111cdf0e10cSrcweir                 bool startOver = false;
1112cdf0e10cSrcweir                 do
1113cdf0e10cSrcweir                 {
1114cdf0e10cSrcweir                     startOver = false;
1115cdf0e10cSrcweir                     // take away the space we need from the columns with a positive flexibility
1116cdf0e10cSrcweir                     for ( size_t i=0; i<o_newColWidthsPixel.size() && !startOver; ++i )
1117cdf0e10cSrcweir                     {
1118cdf0e10cSrcweir                         long const columnFlexibility = columnFlexibilities[i];
1119cdf0e10cSrcweir                         if ( columnFlexibility == 0 )
1120cdf0e10cSrcweir                             continue;
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir                         long newColWidth = currentColWidths[i] - columnFlexibility * takeAwayPixel / flexibilityDenominator;
1123cdf0e10cSrcweir 
1124cdf0e10cSrcweir                         if ( newColWidth < effectiveColumnLimits[i].first )
1125cdf0e10cSrcweir                         {   // that was too much, we hit the col's minimum
1126cdf0e10cSrcweir                             // set the new width to exactly this minimum
1127cdf0e10cSrcweir                             newColWidth = effectiveColumnLimits[i].first;
1128cdf0e10cSrcweir                             // adjust the flexibility denominator ...
1129cdf0e10cSrcweir                             flexibilityDenominator -= columnFlexibility;
1130cdf0e10cSrcweir                             columnFlexibilities[i] = 0;
1131cdf0e10cSrcweir                             --flexibleColumnCount;
1132cdf0e10cSrcweir                             // ... and the remaining width ...
1133cdf0e10cSrcweir                             long const difference = currentColWidths[i] - newColWidth;
1134cdf0e10cSrcweir                             takeAwayPixel -= difference;
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir                             // and start over with the first column, since there might be earlier columns which need
1137cdf0e10cSrcweir                             // to be recalculated now
1138cdf0e10cSrcweir                             startOver = true;
1139cdf0e10cSrcweir                         }
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir                         o_newColWidthsPixel[i] = newColWidth;
1142cdf0e10cSrcweir                     }
1143cdf0e10cSrcweir                 }
1144cdf0e10cSrcweir                 while ( startOver );
1145cdf0e10cSrcweir 
1146cdf0e10cSrcweir                 // are there pixels left (might be caused by rounding errors)?
1147cdf0e10cSrcweir                 takeAwayPixel = ::std::accumulate( o_newColWidthsPixel.begin(), o_newColWidthsPixel.end(), 0 ) - gridWidthPixel;
1148cdf0e10cSrcweir                 while ( ( takeAwayPixel > 0 ) && ( flexibleColumnCount > 0 ) )
1149cdf0e10cSrcweir                 {
1150cdf0e10cSrcweir                     // yes => ignore relative flexibilities, and subsequently take away pixels from all flexible
1151cdf0e10cSrcweir                     // columns which did not yet reach their minimum.
1152cdf0e10cSrcweir                     for ( size_t i=0; ( i < o_newColWidthsPixel.size() ) && ( takeAwayPixel > 0 ); ++i )
1153cdf0e10cSrcweir                     {
1154cdf0e10cSrcweir                         if ( columnFlexibilities[i] == 0 )
1155cdf0e10cSrcweir                             continue;
1156cdf0e10cSrcweir 
1157cdf0e10cSrcweir                         OSL_ENSURE( o_newColWidthsPixel[i] >= effectiveColumnLimits[i].first,
1158cdf0e10cSrcweir                             "TableControl_Impl::impl_ni_calculateColumnWidths: inconsitency!" );
1159cdf0e10cSrcweir                         if ( o_newColWidthsPixel[i] <= effectiveColumnLimits[i].first )
1160cdf0e10cSrcweir                         {
1161cdf0e10cSrcweir                             columnFlexibilities[i] = 0;
1162cdf0e10cSrcweir                             --flexibleColumnCount;
1163cdf0e10cSrcweir                             continue;
1164cdf0e10cSrcweir                         }
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir                         --o_newColWidthsPixel[i];
1167cdf0e10cSrcweir                         --takeAwayPixel;
1168cdf0e10cSrcweir                     }
1169cdf0e10cSrcweir                 }
1170cdf0e10cSrcweir             }
1171cdf0e10cSrcweir         }
1172cdf0e10cSrcweir 
1173cdf0e10cSrcweir         return gridWidthPixel;
1174cdf0e10cSrcweir     }
1175cdf0e10cSrcweir 
1176cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1177cdf0e10cSrcweir     void TableControl_Impl::impl_ni_relayout( ColPos const i_assumeInflexibleColumnsUpToIncluding )
1178cdf0e10cSrcweir     {
1179cdf0e10cSrcweir         ENSURE_OR_RETURN_VOID( !m_bUpdatingColWidths, "TableControl_Impl::impl_ni_relayout: recursive call detected!" );
1180cdf0e10cSrcweir 
1181cdf0e10cSrcweir         m_aColumnWidths.resize( 0 );
1182cdf0e10cSrcweir         if ( !m_pModel )
1183cdf0e10cSrcweir             return;
1184cdf0e10cSrcweir 
1185cdf0e10cSrcweir         ::comphelper::FlagRestorationGuard const aWidthUpdateFlag( m_bUpdatingColWidths, true );
1186cdf0e10cSrcweir         SuppressCursor aHideCursor( *this );
1187cdf0e10cSrcweir 
1188cdf0e10cSrcweir         // layouting steps:
1189cdf0e10cSrcweir         //
1190cdf0e10cSrcweir         // 1. adjust column widths, leaving space for a vertical scrollbar
1191cdf0e10cSrcweir         // 2. determine need for a vertical scrollbar
1192cdf0e10cSrcweir         //    - V-YES: all fine, result from 1. is still valid
1193cdf0e10cSrcweir         //    - V-NO: result from 1. is still under consideration
1194cdf0e10cSrcweir         //
1195cdf0e10cSrcweir         // 3. determine need for a horizontal scrollbar
1196cdf0e10cSrcweir         //   - H-NO: all fine, result from 2. is still valid
1197cdf0e10cSrcweir         //   - H-YES: reconsider need for a vertical scrollbar, if result of 2. was V-NO
1198cdf0e10cSrcweir         //     - V-YES: all fine, result from 1. is still valid
1199cdf0e10cSrcweir         //     - V-NO: redistribute the remaining space (if any) amongst all columns which allow it
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir         ::std::vector< long > newWidthsPixel;
1202cdf0e10cSrcweir         long gridWidthPixel = impl_ni_calculateColumnWidths( i_assumeInflexibleColumnsUpToIncluding, true, newWidthsPixel );
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir         // the width/height of a scrollbar, needed several times below
1205cdf0e10cSrcweir 	    long const nScrollbarMetrics = m_rAntiImpl.GetSettings().GetStyleSettings().GetScrollBarSize();
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir         // determine the playground for the data cells (excluding headers)
1208cdf0e10cSrcweir         // TODO: what if the control is smaller than needed for the headers/scrollbars?
1209cdf0e10cSrcweir         Rectangle aDataCellPlayground( Point( 0, 0 ), m_rAntiImpl.GetOutputSizePixel() );
1210cdf0e10cSrcweir         aDataCellPlayground.Left() = m_nRowHeaderWidthPixel;
1211cdf0e10cSrcweir         aDataCellPlayground.Top() = m_nColHeaderHeightPixel;
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir         OSL_ENSURE( ( m_nRowCount == m_pModel->getRowCount() ) && ( m_nColumnCount == m_pModel->getColumnCount() ),
1214cdf0e10cSrcweir             "TableControl_Impl::impl_ni_relayout: how is this expected to work with invalid data?" );
1215cdf0e10cSrcweir         long const nAllColumnsWidth = ::std::accumulate( newWidthsPixel.begin(), newWidthsPixel.end(), 0 );
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir         ScrollbarVisibility const eVertScrollbar = m_pModel->getVerticalScrollbarVisibility();
1218cdf0e10cSrcweir         ScrollbarVisibility const eHorzScrollbar = m_pModel->getHorizontalScrollbarVisibility();
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir         // do we need a vertical scrollbar?
1221cdf0e10cSrcweir         bool bNeedVerticalScrollbar = lcl_determineScrollbarNeed(
1222cdf0e10cSrcweir             m_nTopRow, eVertScrollbar, aDataCellPlayground.GetHeight(), m_nRowHeightPixel * m_nRowCount );
1223cdf0e10cSrcweir         bool bFirstRoundVScrollNeed = false;
1224cdf0e10cSrcweir         if ( bNeedVerticalScrollbar )
1225cdf0e10cSrcweir         {
1226cdf0e10cSrcweir             aDataCellPlayground.Right() -= nScrollbarMetrics;
1227cdf0e10cSrcweir             bFirstRoundVScrollNeed = true;
1228cdf0e10cSrcweir         }
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir         // do we need a horizontal scrollbar?
1231cdf0e10cSrcweir         bool const bNeedHorizontalScrollbar = lcl_determineScrollbarNeed(
1232cdf0e10cSrcweir             m_nLeftColumn, eHorzScrollbar, aDataCellPlayground.GetWidth(), nAllColumnsWidth );
1233cdf0e10cSrcweir         if ( bNeedHorizontalScrollbar )
1234cdf0e10cSrcweir         {
1235cdf0e10cSrcweir             aDataCellPlayground.Bottom() -= nScrollbarMetrics;
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir             // now that we just found that we need a horizontal scrollbar,
1238cdf0e10cSrcweir             // the need for a vertical one may have changed, since the horizontal
1239cdf0e10cSrcweir             // SB might just occupy enough space so that not all rows do fit
1240cdf0e10cSrcweir             // anymore
1241cdf0e10cSrcweir             if  ( !bFirstRoundVScrollNeed )
1242cdf0e10cSrcweir             {
1243cdf0e10cSrcweir                 bNeedVerticalScrollbar = lcl_determineScrollbarNeed(
1244cdf0e10cSrcweir                     m_nTopRow, eVertScrollbar, aDataCellPlayground.GetHeight(), m_nRowHeightPixel * m_nRowCount );
1245cdf0e10cSrcweir                 if ( bNeedVerticalScrollbar )
1246cdf0e10cSrcweir                 {
1247cdf0e10cSrcweir                     aDataCellPlayground.Right() -= nScrollbarMetrics;
1248cdf0e10cSrcweir                 }
1249cdf0e10cSrcweir             }
1250cdf0e10cSrcweir         }
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir         // the initial call to impl_ni_calculateColumnWidths assumed that we need a vertical scrollbar. If, by now,
1253cdf0e10cSrcweir         // we know that this is not the case, re-calculate the column widths.
1254cdf0e10cSrcweir         if ( !bNeedVerticalScrollbar )
1255cdf0e10cSrcweir             gridWidthPixel = impl_ni_calculateColumnWidths( i_assumeInflexibleColumnsUpToIncluding, false, newWidthsPixel );
1256cdf0e10cSrcweir 
1257cdf0e10cSrcweir         // update the column objects with the new widths we finally calculated
1258cdf0e10cSrcweir         TableSize const colCount = m_pModel->getColumnCount();
1259cdf0e10cSrcweir         m_aColumnWidths.reserve( colCount );
1260cdf0e10cSrcweir         long accumulatedWidthPixel = m_nRowHeaderWidthPixel;
1261cdf0e10cSrcweir         bool anyColumnWidthChanged = false;
1262cdf0e10cSrcweir         for ( ColPos col = 0; col < colCount; ++col )
1263cdf0e10cSrcweir         {
1264cdf0e10cSrcweir             const long columnStart = accumulatedWidthPixel;
1265cdf0e10cSrcweir             const long columnEnd = columnStart + newWidthsPixel[col];
1266cdf0e10cSrcweir 			m_aColumnWidths.push_back( MutableColumnMetrics( columnStart, columnEnd ) );
1267cdf0e10cSrcweir             accumulatedWidthPixel = columnEnd;
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir             // and don't forget to forward this to the column models
1270cdf0e10cSrcweir 			PColumnModel const pColumn = m_pModel->getColumnModel( col );
1271cdf0e10cSrcweir             ENSURE_OR_THROW( !!pColumn, "invalid column returned by the model!" );
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir             long const oldColumnWidthAppFont = pColumn->getWidth();
1274cdf0e10cSrcweir             long const newColumnWidthAppFont = pixelWidthToAppFont( newWidthsPixel[col] );
1275cdf0e10cSrcweir             pColumn->setWidth( newColumnWidthAppFont );
1276cdf0e10cSrcweir 
1277cdf0e10cSrcweir             anyColumnWidthChanged |= ( oldColumnWidthAppFont != newColumnWidthAppFont );
1278cdf0e10cSrcweir         }
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir         // if the column widths changed, ensure everything is repainted
1281cdf0e10cSrcweir         if ( anyColumnWidthChanged )
1282cdf0e10cSrcweir             invalidate( TableAreaAll );
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir         // if the column resizing happened to leave some space at the right, but there are columns
1285cdf0e10cSrcweir         // scrolled out to the left, scroll them in
1286cdf0e10cSrcweir         while   (   ( m_nLeftColumn > 0 )
1287cdf0e10cSrcweir                 &&  ( accumulatedWidthPixel - m_aColumnWidths[ m_nLeftColumn - 1 ].getStart() <= gridWidthPixel )
1288cdf0e10cSrcweir                 )
1289cdf0e10cSrcweir         {
1290cdf0e10cSrcweir             --m_nLeftColumn;
1291cdf0e10cSrcweir         }
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir         // now adjust the column metrics, since they currently ignore the horizontal scroll position
1294cdf0e10cSrcweir         if ( m_nLeftColumn > 0 )
1295cdf0e10cSrcweir         {
1296cdf0e10cSrcweir             const long offsetPixel = m_aColumnWidths[ 0 ].getStart() - m_aColumnWidths[ m_nLeftColumn ].getStart();
1297cdf0e10cSrcweir             for (   ColumnPositions::iterator colPos = m_aColumnWidths.begin();
1298cdf0e10cSrcweir                     colPos != m_aColumnWidths.end();
1299cdf0e10cSrcweir                     ++colPos
1300cdf0e10cSrcweir                  )
1301cdf0e10cSrcweir             {
1302cdf0e10cSrcweir                 colPos->move( offsetPixel );
1303cdf0e10cSrcweir             }
1304cdf0e10cSrcweir         }
1305cdf0e10cSrcweir 
1306cdf0e10cSrcweir         // show or hide the scrollbars as needed, and position the data window
1307cdf0e10cSrcweir         impl_ni_positionChildWindows( aDataCellPlayground, bNeedVerticalScrollbar, bNeedHorizontalScrollbar );
1308cdf0e10cSrcweir     }
1309cdf0e10cSrcweir 
1310cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1311cdf0e10cSrcweir     void TableControl_Impl::impl_ni_positionChildWindows( Rectangle const & i_dataCellPlayground,
1312cdf0e10cSrcweir         bool const i_verticalScrollbar, bool const i_horizontalScrollbar )
1313cdf0e10cSrcweir     {
1314cdf0e10cSrcweir 	    long const nScrollbarMetrics = m_rAntiImpl.GetSettings().GetStyleSettings().GetScrollBarSize();
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir         // create or destroy the vertical scrollbar, as needed
1317cdf0e10cSrcweir         lcl_updateScrollbar(
1318cdf0e10cSrcweir             m_rAntiImpl,
1319cdf0e10cSrcweir             m_pVScroll,
1320cdf0e10cSrcweir             i_verticalScrollbar,
1321cdf0e10cSrcweir             lcl_getRowsFittingInto( i_dataCellPlayground.GetHeight(), m_nRowHeightPixel ),
1322cdf0e10cSrcweir                                                                     // visible units
1323cdf0e10cSrcweir             m_nTopRow,                                              // current position
1324cdf0e10cSrcweir             1,                                                      // line size
1325cdf0e10cSrcweir             m_nRowCount,                                            // range
1326cdf0e10cSrcweir             false,                                                  // vertical
1327cdf0e10cSrcweir             LINK( this, TableControl_Impl, OnScroll )               // scroll handler
1328cdf0e10cSrcweir         );
1329cdf0e10cSrcweir 
1330cdf0e10cSrcweir         // position it
1331cdf0e10cSrcweir         if ( m_pVScroll )
1332cdf0e10cSrcweir         {
1333cdf0e10cSrcweir             Rectangle aScrollbarArea(
1334cdf0e10cSrcweir                 Point( i_dataCellPlayground.Right() + 1, 0 ),
1335cdf0e10cSrcweir                 Size( nScrollbarMetrics, i_dataCellPlayground.Bottom() + 1 )
1336cdf0e10cSrcweir             );
1337cdf0e10cSrcweir             m_pVScroll->SetPosSizePixel(
1338cdf0e10cSrcweir                 aScrollbarArea.TopLeft(), aScrollbarArea.GetSize() );
1339cdf0e10cSrcweir         }
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir         // create or destroy the horizontal scrollbar, as needed
1342cdf0e10cSrcweir         lcl_updateScrollbar(
1343cdf0e10cSrcweir             m_rAntiImpl,
1344cdf0e10cSrcweir             m_pHScroll,
1345cdf0e10cSrcweir             i_horizontalScrollbar,
1346cdf0e10cSrcweir             lcl_getColumnsVisibleWithin( i_dataCellPlayground, m_nLeftColumn, *this, false ),
1347cdf0e10cSrcweir                                                                     // visible units
1348cdf0e10cSrcweir             m_nLeftColumn,                                          // current position
1349cdf0e10cSrcweir             1,                                                      // line size
1350cdf0e10cSrcweir             m_nColumnCount,                                         // range
1351cdf0e10cSrcweir             true,                                                   // horizontal
1352cdf0e10cSrcweir             LINK( this, TableControl_Impl, OnScroll )               // scroll handler
1353cdf0e10cSrcweir         );
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir         // position it
1356cdf0e10cSrcweir         if ( m_pHScroll )
1357cdf0e10cSrcweir         {
1358cdf0e10cSrcweir 			TableSize const nVisibleUnits = lcl_getColumnsVisibleWithin( i_dataCellPlayground, m_nLeftColumn, *this, false );
1359cdf0e10cSrcweir 			TableMetrics const nRange = m_nColumnCount;
1360cdf0e10cSrcweir 			if( m_nLeftColumn + nVisibleUnits == nRange - 1 )
1361cdf0e10cSrcweir 			{
1362cdf0e10cSrcweir 				if ( m_aColumnWidths[ nRange - 1 ].getStart() - m_aColumnWidths[ m_nLeftColumn ].getEnd() + m_aColumnWidths[ nRange-1 ].getWidth() > i_dataCellPlayground.GetWidth() )
1363cdf0e10cSrcweir 				{
1364cdf0e10cSrcweir 					m_pHScroll->SetVisibleSize( nVisibleUnits -1 );
1365cdf0e10cSrcweir 					m_pHScroll->SetPageSize( nVisibleUnits - 1 );
1366cdf0e10cSrcweir 				}
1367cdf0e10cSrcweir 			}
1368cdf0e10cSrcweir             Rectangle aScrollbarArea(
1369cdf0e10cSrcweir                 Point( 0, i_dataCellPlayground.Bottom() + 1 ),
1370cdf0e10cSrcweir                 Size( i_dataCellPlayground.Right() + 1, nScrollbarMetrics )
1371cdf0e10cSrcweir             );
1372cdf0e10cSrcweir             m_pHScroll->SetPosSizePixel(
1373cdf0e10cSrcweir                 aScrollbarArea.TopLeft(), aScrollbarArea.GetSize() );
1374cdf0e10cSrcweir         }
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir         // the corner window connecting the two scrollbars in the lower right corner
1377cdf0e10cSrcweir         bool bHaveScrollCorner = NULL != m_pScrollCorner;
1378cdf0e10cSrcweir         bool bNeedScrollCorner = ( NULL != m_pHScroll ) && ( NULL != m_pVScroll );
1379cdf0e10cSrcweir         if ( bHaveScrollCorner && !bNeedScrollCorner )
1380cdf0e10cSrcweir         {
1381cdf0e10cSrcweir             DELETEZ( m_pScrollCorner );
1382cdf0e10cSrcweir         }
1383cdf0e10cSrcweir         else if ( !bHaveScrollCorner && bNeedScrollCorner )
1384cdf0e10cSrcweir         {
1385cdf0e10cSrcweir             m_pScrollCorner = new ScrollBarBox( &m_rAntiImpl );
1386cdf0e10cSrcweir             m_pScrollCorner->SetSizePixel( Size( nScrollbarMetrics, nScrollbarMetrics ) );
1387cdf0e10cSrcweir             m_pScrollCorner->SetPosPixel( Point( i_dataCellPlayground.Right() + 1, i_dataCellPlayground.Bottom() + 1 ) );
1388cdf0e10cSrcweir             m_pScrollCorner->Show();
1389cdf0e10cSrcweir         }
1390cdf0e10cSrcweir 		else if(bHaveScrollCorner && bNeedScrollCorner)
1391cdf0e10cSrcweir 		{
1392cdf0e10cSrcweir 			m_pScrollCorner->SetPosPixel( Point( i_dataCellPlayground.Right() + 1, i_dataCellPlayground.Bottom() + 1 ) );
1393cdf0e10cSrcweir             m_pScrollCorner->Show();
1394cdf0e10cSrcweir         }
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir         // resize the data window
1397cdf0e10cSrcweir         m_pDataWindow->SetSizePixel( Size(
1398cdf0e10cSrcweir             i_dataCellPlayground.GetWidth() + m_nRowHeaderWidthPixel,
1399cdf0e10cSrcweir             i_dataCellPlayground.GetHeight() + m_nColHeaderHeightPixel
1400cdf0e10cSrcweir         ) );
1401cdf0e10cSrcweir     }
1402cdf0e10cSrcweir 
1403cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1404cdf0e10cSrcweir     void TableControl_Impl::onResize()
1405cdf0e10cSrcweir     {
1406cdf0e10cSrcweir         DBG_CHECK_ME();
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir         impl_ni_relayout();
1409cdf0e10cSrcweir 		checkCursorPosition();
1410cdf0e10cSrcweir     }
1411cdf0e10cSrcweir 
1412cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1413cdf0e10cSrcweir     void TableControl_Impl::doPaintContent( const Rectangle& _rUpdateRect )
1414cdf0e10cSrcweir     {
1415cdf0e10cSrcweir         DBG_CHECK_ME();
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir         if ( !getModel() )
1418cdf0e10cSrcweir             return;
1419cdf0e10cSrcweir         PTableRenderer pRenderer = getModel()->getRenderer();
1420cdf0e10cSrcweir         DBG_ASSERT( !!pRenderer, "TableDataWindow::doPaintContent: invalid renderer!" );
1421cdf0e10cSrcweir         if ( !pRenderer )
1422cdf0e10cSrcweir             return;
1423cdf0e10cSrcweir 
1424cdf0e10cSrcweir         // our current style settings, to be passed to the renderer
1425cdf0e10cSrcweir         const StyleSettings& rStyle = m_rAntiImpl.GetSettings().GetStyleSettings();
1426cdf0e10cSrcweir 		m_nRowCount = m_pModel->getRowCount();
1427cdf0e10cSrcweir         // the area occupied by all (at least partially) visible cells, including
1428cdf0e10cSrcweir         // headers
1429cdf0e10cSrcweir         Rectangle const aAllCellsWithHeaders( impl_getAllVisibleCellsArea() );
1430cdf0e10cSrcweir 
1431cdf0e10cSrcweir         // ............................
1432cdf0e10cSrcweir         // draw the header column area
1433cdf0e10cSrcweir         if ( m_pModel->hasColumnHeaders() )
1434cdf0e10cSrcweir         {
1435cdf0e10cSrcweir             TableRowGeometry const aHeaderRow( *this, Rectangle( Point( 0, 0 ),
1436cdf0e10cSrcweir                 aAllCellsWithHeaders.BottomRight() ), ROW_COL_HEADERS );
1437cdf0e10cSrcweir 			Rectangle const aColRect(aHeaderRow.getRect());
1438cdf0e10cSrcweir             pRenderer->PaintHeaderArea(
1439cdf0e10cSrcweir                 *m_pDataWindow, aColRect, true, false, rStyle
1440cdf0e10cSrcweir             );
1441cdf0e10cSrcweir             // Note that strictly, aHeaderRow.getRect() also contains the intersection between column
1442cdf0e10cSrcweir             // and row header area. However, below we go to paint this intersection, again,
1443cdf0e10cSrcweir             // so this hopefully doesn't hurt if we already paint it here.
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir             for ( TableCellGeometry aCell( aHeaderRow, m_nLeftColumn );
1446cdf0e10cSrcweir                   aCell.isValid();
1447cdf0e10cSrcweir                   aCell.moveRight()
1448cdf0e10cSrcweir                 )
1449cdf0e10cSrcweir             {
1450cdf0e10cSrcweir                 if ( _rUpdateRect.GetIntersection( aCell.getRect() ).IsEmpty() )
1451cdf0e10cSrcweir                     continue;
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir                 bool isActiveColumn = ( aCell.getColumn() == getCurrentColumn() );
1454cdf0e10cSrcweir                 bool isSelectedColumn = false;
1455cdf0e10cSrcweir                 pRenderer->PaintColumnHeader( aCell.getColumn(), isActiveColumn, isSelectedColumn,
1456cdf0e10cSrcweir                     *m_pDataWindow, aCell.getRect(), rStyle );
1457cdf0e10cSrcweir             }
1458cdf0e10cSrcweir         }
1459cdf0e10cSrcweir         // the area occupied by the row header, if any
1460cdf0e10cSrcweir         Rectangle aRowHeaderArea;
1461cdf0e10cSrcweir         if ( m_pModel->hasRowHeaders() )
1462cdf0e10cSrcweir         {
1463cdf0e10cSrcweir             aRowHeaderArea = aAllCellsWithHeaders;
1464cdf0e10cSrcweir             aRowHeaderArea.Right() = m_nRowHeaderWidthPixel - 1;
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir 		    TableSize const nVisibleRows = impl_getVisibleRows( true );
1467cdf0e10cSrcweir             TableSize nActualRows = nVisibleRows;
1468cdf0e10cSrcweir 		    if ( m_nTopRow + nActualRows > m_nRowCount )
1469cdf0e10cSrcweir 			    nActualRows = m_nRowCount - m_nTopRow;
1470cdf0e10cSrcweir 			aRowHeaderArea.Bottom() = m_nColHeaderHeightPixel + m_nRowHeightPixel * nActualRows - 1;
1471cdf0e10cSrcweir 
1472cdf0e10cSrcweir             pRenderer->PaintHeaderArea( *m_pDataWindow, aRowHeaderArea, false, true, rStyle );
1473cdf0e10cSrcweir             // Note that strictly, aRowHeaderArea also contains the intersection between column
1474cdf0e10cSrcweir             // and row header area. However, below we go to paint this intersection, again,
1475cdf0e10cSrcweir             // so this hopefully doesn't hurt if we already paint it here.
1476cdf0e10cSrcweir 
1477cdf0e10cSrcweir             if ( m_pModel->hasColumnHeaders() )
1478cdf0e10cSrcweir             {
1479cdf0e10cSrcweir                 TableCellGeometry const aIntersection( *this, Rectangle( Point( 0, 0 ),
1480cdf0e10cSrcweir                     aAllCellsWithHeaders.BottomRight() ), COL_ROW_HEADERS, ROW_COL_HEADERS );
1481cdf0e10cSrcweir 				Rectangle const aInters( aIntersection.getRect() );
1482cdf0e10cSrcweir                 pRenderer->PaintHeaderArea(
1483cdf0e10cSrcweir                     *m_pDataWindow, aInters, true, true, rStyle
1484cdf0e10cSrcweir                 );
1485cdf0e10cSrcweir             }
1486cdf0e10cSrcweir 		}
1487cdf0e10cSrcweir 
1488cdf0e10cSrcweir         // ............................
1489cdf0e10cSrcweir         // draw the table content row by row
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir         TableSize colCount = getModel()->getColumnCount();
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir         // paint all rows
1494cdf0e10cSrcweir         Rectangle const aAllDataCellsArea( impl_getAllVisibleDataCellArea() );
1495cdf0e10cSrcweir         for ( TableRowGeometry aRowIterator( *this, aAllCellsWithHeaders, getTopRow() );
1496cdf0e10cSrcweir               aRowIterator.isValid();
1497cdf0e10cSrcweir               aRowIterator.moveDown() )
1498cdf0e10cSrcweir         {
1499cdf0e10cSrcweir             if ( _rUpdateRect.GetIntersection( aRowIterator.getRect() ).IsEmpty() )
1500cdf0e10cSrcweir 				continue;
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir             bool const isControlFocused = m_rAntiImpl.HasControlFocus();
1503cdf0e10cSrcweir 			bool const isSelectedRow = isRowSelected( aRowIterator.getRow() );
1504cdf0e10cSrcweir 
1505cdf0e10cSrcweir             Rectangle const aRect = aRowIterator.getRect().GetIntersection( aAllDataCellsArea );
1506cdf0e10cSrcweir 
1507cdf0e10cSrcweir             // give the redenderer a chance to prepare the row
1508cdf0e10cSrcweir             pRenderer->PrepareRow(
1509cdf0e10cSrcweir                 aRowIterator.getRow(), isControlFocused, isSelectedRow,
1510cdf0e10cSrcweir 			    *m_pDataWindow, aRect, rStyle
1511cdf0e10cSrcweir             );
1512cdf0e10cSrcweir 
1513cdf0e10cSrcweir             // paint the row header
1514cdf0e10cSrcweir             if ( m_pModel->hasRowHeaders() )
1515cdf0e10cSrcweir             {
1516cdf0e10cSrcweir 				const Rectangle aCurrentRowHeader( aRowHeaderArea.GetIntersection( aRowIterator.getRect() ) );
1517cdf0e10cSrcweir 				pRenderer->PaintRowHeader( isControlFocused, isSelectedRow, *m_pDataWindow, aCurrentRowHeader,
1518cdf0e10cSrcweir                     rStyle );
1519cdf0e10cSrcweir             }
1520cdf0e10cSrcweir 
1521cdf0e10cSrcweir             if ( !colCount )
1522cdf0e10cSrcweir                 continue;
1523cdf0e10cSrcweir 
1524cdf0e10cSrcweir             // paint all cells in this row
1525cdf0e10cSrcweir             for ( TableCellGeometry aCell( aRowIterator, m_nLeftColumn );
1526cdf0e10cSrcweir                   aCell.isValid();
1527cdf0e10cSrcweir                   aCell.moveRight()
1528cdf0e10cSrcweir                 )
1529cdf0e10cSrcweir             {
1530cdf0e10cSrcweir 				bool isSelectedColumn = false;
1531cdf0e10cSrcweir                 pRenderer->PaintCell( aCell.getColumn(), isSelectedRow || isSelectedColumn, isControlFocused,
1532cdf0e10cSrcweir 								*m_pDataWindow, aCell.getRect(), rStyle );
1533cdf0e10cSrcweir 			}
1534cdf0e10cSrcweir         }
1535cdf0e10cSrcweir     }
1536cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1537cdf0e10cSrcweir     void TableControl_Impl::hideCursor()
1538cdf0e10cSrcweir     {
1539cdf0e10cSrcweir         DBG_CHECK_ME();
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir         if ( ++m_nCursorHidden == 1 )
1542cdf0e10cSrcweir             impl_ni_doSwitchCursor( false );
1543cdf0e10cSrcweir     }
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1546cdf0e10cSrcweir     void TableControl_Impl::showCursor()
1547cdf0e10cSrcweir     {
1548cdf0e10cSrcweir         DBG_CHECK_ME();
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir         DBG_ASSERT( m_nCursorHidden > 0, "TableControl_Impl::showCursor: cursor not hidden!" );
1551cdf0e10cSrcweir         if ( --m_nCursorHidden == 0 )
1552cdf0e10cSrcweir             impl_ni_doSwitchCursor( true );
1553cdf0e10cSrcweir     }
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1556cdf0e10cSrcweir     bool TableControl_Impl::dispatchAction( TableControlAction _eAction )
1557cdf0e10cSrcweir     {
1558cdf0e10cSrcweir         DBG_CHECK_ME();
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir         bool bSuccess = false;
1561cdf0e10cSrcweir         bool selectionChanged = false;
1562cdf0e10cSrcweir 
1563cdf0e10cSrcweir         switch ( _eAction )
1564cdf0e10cSrcweir         {
1565cdf0e10cSrcweir         case cursorDown:
1566cdf0e10cSrcweir 		if ( m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION )
1567cdf0e10cSrcweir 		{
1568cdf0e10cSrcweir 			//if other rows already selected, deselect them
1569cdf0e10cSrcweir 			if ( m_aSelectedRows.size()>0 )
1570cdf0e10cSrcweir 			{
1571cdf0e10cSrcweir                 invalidateSelectedRows();
1572cdf0e10cSrcweir 				m_aSelectedRows.clear();
1573cdf0e10cSrcweir 			}
1574cdf0e10cSrcweir 			if ( m_nCurRow < m_nRowCount-1 )
1575cdf0e10cSrcweir 			{
1576cdf0e10cSrcweir 				++m_nCurRow;
1577cdf0e10cSrcweir 				m_aSelectedRows.push_back(m_nCurRow);
1578cdf0e10cSrcweir 			}
1579cdf0e10cSrcweir 			else
1580cdf0e10cSrcweir 				m_aSelectedRows.push_back(m_nCurRow);
1581cdf0e10cSrcweir 			invalidateRow( m_nCurRow );
1582cdf0e10cSrcweir 			ensureVisible(m_nCurColumn,m_nCurRow,false);
1583cdf0e10cSrcweir             selectionChanged = true;
1584cdf0e10cSrcweir 			bSuccess = true;
1585cdf0e10cSrcweir 		}
1586cdf0e10cSrcweir 		else
1587cdf0e10cSrcweir 		{
1588cdf0e10cSrcweir 			if ( m_nCurRow < m_nRowCount - 1 )
1589cdf0e10cSrcweir 				bSuccess = goTo( m_nCurColumn, m_nCurRow + 1 );
1590cdf0e10cSrcweir 		}
1591cdf0e10cSrcweir             break;
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir         case cursorUp:
1594cdf0e10cSrcweir 		if(m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION)
1595cdf0e10cSrcweir 		{
1596cdf0e10cSrcweir 			if(m_aSelectedRows.size()>0)
1597cdf0e10cSrcweir 			{
1598cdf0e10cSrcweir                 invalidateSelectedRows();
1599cdf0e10cSrcweir 				m_aSelectedRows.clear();
1600cdf0e10cSrcweir 			}
1601cdf0e10cSrcweir 			if(m_nCurRow>0)
1602cdf0e10cSrcweir 			{
1603cdf0e10cSrcweir 				--m_nCurRow;
1604cdf0e10cSrcweir 				m_aSelectedRows.push_back(m_nCurRow);
1605cdf0e10cSrcweir 				invalidateRow( m_nCurRow );
1606cdf0e10cSrcweir 			}
1607cdf0e10cSrcweir 			else
1608cdf0e10cSrcweir 			{
1609cdf0e10cSrcweir 				m_aSelectedRows.push_back(m_nCurRow);
1610cdf0e10cSrcweir 				invalidateRow( m_nCurRow );
1611cdf0e10cSrcweir 			}
1612cdf0e10cSrcweir 			ensureVisible(m_nCurColumn,m_nCurRow,false);
1613cdf0e10cSrcweir 			selectionChanged = true;
1614cdf0e10cSrcweir 			bSuccess = true;
1615cdf0e10cSrcweir 		}
1616cdf0e10cSrcweir 		else
1617cdf0e10cSrcweir 		{
1618cdf0e10cSrcweir 			if ( m_nCurRow > 0 )
1619cdf0e10cSrcweir 				bSuccess = goTo( m_nCurColumn, m_nCurRow - 1 );
1620cdf0e10cSrcweir 		}
1621cdf0e10cSrcweir 		break;
1622cdf0e10cSrcweir         case cursorLeft:
1623cdf0e10cSrcweir             if ( m_nCurColumn > 0 )
1624cdf0e10cSrcweir                 bSuccess = goTo( m_nCurColumn - 1, m_nCurRow );
1625cdf0e10cSrcweir             else
1626cdf0e10cSrcweir                 if ( ( m_nCurColumn == 0) && ( m_nCurRow > 0 ) )
1627cdf0e10cSrcweir                     bSuccess = goTo( m_nColumnCount - 1, m_nCurRow - 1 );
1628cdf0e10cSrcweir             break;
1629cdf0e10cSrcweir 
1630cdf0e10cSrcweir         case cursorRight:
1631cdf0e10cSrcweir             if ( m_nCurColumn < m_nColumnCount - 1 )
1632cdf0e10cSrcweir                 bSuccess = goTo( m_nCurColumn + 1, m_nCurRow );
1633cdf0e10cSrcweir             else
1634cdf0e10cSrcweir                 if ( ( m_nCurColumn == m_nColumnCount - 1 ) && ( m_nCurRow < m_nRowCount - 1 ) )
1635cdf0e10cSrcweir                     bSuccess = goTo( 0, m_nCurRow + 1 );
1636cdf0e10cSrcweir             break;
1637cdf0e10cSrcweir 
1638cdf0e10cSrcweir         case cursorToLineStart:
1639cdf0e10cSrcweir             bSuccess = goTo( 0, m_nCurRow );
1640cdf0e10cSrcweir             break;
1641cdf0e10cSrcweir 
1642cdf0e10cSrcweir         case cursorToLineEnd:
1643cdf0e10cSrcweir             bSuccess = goTo( m_nColumnCount - 1, m_nCurRow );
1644cdf0e10cSrcweir             break;
1645cdf0e10cSrcweir 
1646cdf0e10cSrcweir         case cursorToFirstLine:
1647cdf0e10cSrcweir             bSuccess = goTo( m_nCurColumn, 0 );
1648cdf0e10cSrcweir             break;
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir         case cursorToLastLine:
1651cdf0e10cSrcweir             bSuccess = goTo( m_nCurColumn, m_nRowCount - 1 );
1652cdf0e10cSrcweir             break;
1653cdf0e10cSrcweir 
1654cdf0e10cSrcweir         case cursorPageUp:
1655cdf0e10cSrcweir         {
1656cdf0e10cSrcweir             RowPos nNewRow = ::std::max( (RowPos)0, m_nCurRow - impl_getVisibleRows( false ) );
1657cdf0e10cSrcweir             bSuccess = goTo( m_nCurColumn, nNewRow );
1658cdf0e10cSrcweir         }
1659cdf0e10cSrcweir         break;
1660cdf0e10cSrcweir 
1661cdf0e10cSrcweir         case cursorPageDown:
1662cdf0e10cSrcweir         {
1663cdf0e10cSrcweir             RowPos nNewRow = ::std::min( m_nRowCount - 1, m_nCurRow + impl_getVisibleRows( false ) );
1664cdf0e10cSrcweir             bSuccess = goTo( m_nCurColumn, nNewRow );
1665cdf0e10cSrcweir         }
1666cdf0e10cSrcweir         break;
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir         case cursorTopLeft:
1669cdf0e10cSrcweir             bSuccess = goTo( 0, 0 );
1670cdf0e10cSrcweir             break;
1671cdf0e10cSrcweir 
1672cdf0e10cSrcweir         case cursorBottomRight:
1673cdf0e10cSrcweir             bSuccess = goTo( m_nColumnCount - 1, m_nRowCount - 1 );
1674cdf0e10cSrcweir             break;
1675cdf0e10cSrcweir 
1676cdf0e10cSrcweir 	    case cursorSelectRow:
1677cdf0e10cSrcweir 	    {
1678cdf0e10cSrcweir 		    if(m_pSelEngine->GetSelectionMode() == NO_SELECTION)
1679cdf0e10cSrcweir 			    return bSuccess = false;
1680cdf0e10cSrcweir 		    //pos is the position of the current row in the vector of selected rows, if current row is selected
1681cdf0e10cSrcweir 		    int pos = getRowSelectedNumber(m_aSelectedRows, m_nCurRow);
1682cdf0e10cSrcweir 		    //if current row is selected, it should be deselected, when ALT+SPACE are pressed
1683cdf0e10cSrcweir 		    if(pos>-1)
1684cdf0e10cSrcweir 		    {
1685cdf0e10cSrcweir 			    m_aSelectedRows.erase(m_aSelectedRows.begin()+pos);
1686cdf0e10cSrcweir 			    if(m_aSelectedRows.empty() && m_nAnchor != -1)
1687cdf0e10cSrcweir 				    m_nAnchor = -1;
1688cdf0e10cSrcweir 		    }
1689cdf0e10cSrcweir 		    //else select the row->put it in the vector
1690cdf0e10cSrcweir 		    else
1691cdf0e10cSrcweir 			    m_aSelectedRows.push_back(m_nCurRow);
1692cdf0e10cSrcweir 		    invalidateRow( m_nCurRow );
1693cdf0e10cSrcweir 		    selectionChanged = true;
1694cdf0e10cSrcweir 		    bSuccess = true;
1695cdf0e10cSrcweir 	    }
1696cdf0e10cSrcweir             break;
1697cdf0e10cSrcweir 	    case cursorSelectRowUp:
1698cdf0e10cSrcweir 	    {
1699cdf0e10cSrcweir 		    if(m_pSelEngine->GetSelectionMode() == NO_SELECTION)
1700cdf0e10cSrcweir 			    return bSuccess = false;
1701cdf0e10cSrcweir 		    else if(m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION)
1702cdf0e10cSrcweir 		    {
1703cdf0e10cSrcweir 			    //if there are other selected rows, deselect them
1704cdf0e10cSrcweir 			    return false;
1705cdf0e10cSrcweir 		    }
1706cdf0e10cSrcweir 		    else
1707cdf0e10cSrcweir 		    {
1708cdf0e10cSrcweir 			    //there are other selected rows
1709cdf0e10cSrcweir 			    if(m_aSelectedRows.size()>0)
1710cdf0e10cSrcweir 			    {
1711cdf0e10cSrcweir 				    //the anchor wasn't set -> a region is not selected, that's why clear all selection
1712cdf0e10cSrcweir 				    //and select the current row
1713cdf0e10cSrcweir 				    if(m_nAnchor==-1)
1714cdf0e10cSrcweir 				    {
1715cdf0e10cSrcweir                         invalidateSelectedRows();
1716cdf0e10cSrcweir 					    m_aSelectedRows.clear();
1717cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1718cdf0e10cSrcweir 					    invalidateRow( m_nCurRow );
1719cdf0e10cSrcweir 				    }
1720cdf0e10cSrcweir 				    else
1721cdf0e10cSrcweir 				    {
1722cdf0e10cSrcweir 					    //a region is already selected, prevRow is last selected row and the row above - nextRow - should be selected
1723cdf0e10cSrcweir 					    int prevRow = getRowSelectedNumber(m_aSelectedRows, m_nCurRow);
1724cdf0e10cSrcweir 					    int nextRow = getRowSelectedNumber(m_aSelectedRows, m_nCurRow-1);
1725cdf0e10cSrcweir 					    if(prevRow>-1)
1726cdf0e10cSrcweir  					    {
1727cdf0e10cSrcweir  						    //if m_nCurRow isn't the upper one, can move up, otherwise not
1728cdf0e10cSrcweir 						    if(m_nCurRow>0)
1729cdf0e10cSrcweir  							    m_nCurRow--;
1730cdf0e10cSrcweir  						    else
1731cdf0e10cSrcweir  							    return bSuccess = true;
1732cdf0e10cSrcweir  						    //if nextRow already selected, deselect it, otherwise select it
1733cdf0e10cSrcweir  						    if(nextRow>-1 && m_aSelectedRows[nextRow] == m_nCurRow)
1734cdf0e10cSrcweir  						    {
1735cdf0e10cSrcweir  							    m_aSelectedRows.erase(m_aSelectedRows.begin()+prevRow);
1736cdf0e10cSrcweir  							    invalidateRow( m_nCurRow + 1 );
1737cdf0e10cSrcweir  						    }
1738cdf0e10cSrcweir  						    else
1739cdf0e10cSrcweir 						    {
1740cdf0e10cSrcweir  							    m_aSelectedRows.push_back(m_nCurRow);
1741cdf0e10cSrcweir  							    invalidateRow( m_nCurRow );
1742cdf0e10cSrcweir  						    }
1743cdf0e10cSrcweir  					    }
1744cdf0e10cSrcweir 					    else
1745cdf0e10cSrcweir 					    {
1746cdf0e10cSrcweir 						    if(m_nCurRow>0)
1747cdf0e10cSrcweir 						    {
1748cdf0e10cSrcweir 							    m_aSelectedRows.push_back(m_nCurRow);
1749cdf0e10cSrcweir 							    m_nCurRow--;
1750cdf0e10cSrcweir 							    m_aSelectedRows.push_back(m_nCurRow);
1751cdf0e10cSrcweir 							    invalidateSelectedRegion( m_nCurRow+1, m_nCurRow );
1752cdf0e10cSrcweir 						    }
1753cdf0e10cSrcweir 					    }
1754cdf0e10cSrcweir 				    }
1755cdf0e10cSrcweir 			    }
1756cdf0e10cSrcweir 			    else
1757cdf0e10cSrcweir 			    {
1758cdf0e10cSrcweir 				    //if nothing is selected and the current row isn't the upper one
1759cdf0e10cSrcweir 				    //select the current and one row above
1760cdf0e10cSrcweir 				    //otherwise select only the upper row
1761cdf0e10cSrcweir 				    if(m_nCurRow>0)
1762cdf0e10cSrcweir 				    {
1763cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1764cdf0e10cSrcweir 					    m_nCurRow--;
1765cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1766cdf0e10cSrcweir 					    invalidateSelectedRegion( m_nCurRow+1, m_nCurRow );
1767cdf0e10cSrcweir 				    }
1768cdf0e10cSrcweir 				    else
1769cdf0e10cSrcweir 				    {
1770cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1771cdf0e10cSrcweir 					    invalidateRow( m_nCurRow );
1772cdf0e10cSrcweir 				    }
1773cdf0e10cSrcweir 			    }
1774cdf0e10cSrcweir 			    m_pSelEngine->SetAnchor(sal_True);
1775cdf0e10cSrcweir 			    m_nAnchor = m_nCurRow;
1776cdf0e10cSrcweir 			    ensureVisible(m_nCurColumn, m_nCurRow, false);
1777cdf0e10cSrcweir 			    selectionChanged = true;
1778cdf0e10cSrcweir 			    bSuccess = true;
1779cdf0e10cSrcweir 		    }
1780cdf0e10cSrcweir 	    }
1781cdf0e10cSrcweir 	    break;
1782cdf0e10cSrcweir 	    case cursorSelectRowDown:
1783cdf0e10cSrcweir 	    {
1784cdf0e10cSrcweir 		    if(m_pSelEngine->GetSelectionMode() == NO_SELECTION)
1785cdf0e10cSrcweir 			    bSuccess = false;
1786cdf0e10cSrcweir 		    else if(m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION)
1787cdf0e10cSrcweir 		    {
1788cdf0e10cSrcweir 			    bSuccess = false;
1789cdf0e10cSrcweir 		    }
1790cdf0e10cSrcweir 		    else
1791cdf0e10cSrcweir 		    {
1792cdf0e10cSrcweir 			    if(m_aSelectedRows.size()>0)
1793cdf0e10cSrcweir 			    {
1794cdf0e10cSrcweir 				    //the anchor wasn't set -> a region is not selected, that's why clear all selection
1795cdf0e10cSrcweir 				    //and select the current row
1796cdf0e10cSrcweir 				    if(m_nAnchor==-1)
1797cdf0e10cSrcweir 				    {
1798cdf0e10cSrcweir                         invalidateSelectedRows();
1799cdf0e10cSrcweir 					    m_aSelectedRows.clear();
1800cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1801cdf0e10cSrcweir 					    invalidateRow( m_nCurRow );
1802cdf0e10cSrcweir 					    }
1803cdf0e10cSrcweir 				    else
1804cdf0e10cSrcweir 				    {
1805cdf0e10cSrcweir 					    //a region is already selected, prevRow is last selected row and the row beneath - nextRow - should be selected
1806cdf0e10cSrcweir 					    int prevRow = getRowSelectedNumber(m_aSelectedRows, m_nCurRow);
1807cdf0e10cSrcweir 					    int nextRow = getRowSelectedNumber(m_aSelectedRows, m_nCurRow+1);
1808cdf0e10cSrcweir 					    if(prevRow>-1)
1809cdf0e10cSrcweir  					    {
1810cdf0e10cSrcweir  						    //if m_nCurRow isn't the last one, can move down, otherwise not
1811cdf0e10cSrcweir  						    if(m_nCurRow<m_nRowCount-1)
1812cdf0e10cSrcweir  							    m_nCurRow++;
1813cdf0e10cSrcweir  						    else
1814cdf0e10cSrcweir 							    return bSuccess = true;
1815cdf0e10cSrcweir  						    //if next row already selected, deselect it, otherwise select it
1816cdf0e10cSrcweir  						    if(nextRow>-1 && m_aSelectedRows[nextRow] == m_nCurRow)
1817cdf0e10cSrcweir  						    {
1818cdf0e10cSrcweir  							    m_aSelectedRows.erase(m_aSelectedRows.begin()+prevRow);
1819cdf0e10cSrcweir  							    invalidateRow( m_nCurRow - 1 );
1820cdf0e10cSrcweir  						    }
1821cdf0e10cSrcweir  						    else
1822cdf0e10cSrcweir  						    {
1823cdf0e10cSrcweir  							    m_aSelectedRows.push_back(m_nCurRow);
1824cdf0e10cSrcweir  							    invalidateRow( m_nCurRow );
1825cdf0e10cSrcweir  						    }
1826cdf0e10cSrcweir 					    }
1827cdf0e10cSrcweir 					    else
1828cdf0e10cSrcweir 					    {
1829cdf0e10cSrcweir 						    if(m_nCurRow<m_nRowCount-1)
1830cdf0e10cSrcweir 						    {
1831cdf0e10cSrcweir 							    m_aSelectedRows.push_back(m_nCurRow);
1832cdf0e10cSrcweir 							    m_nCurRow++;
1833cdf0e10cSrcweir 							    m_aSelectedRows.push_back(m_nCurRow);
1834cdf0e10cSrcweir 							    invalidateSelectedRegion( m_nCurRow-1, m_nCurRow );
1835cdf0e10cSrcweir 						    }
1836cdf0e10cSrcweir 					    }
1837cdf0e10cSrcweir 				    }
1838cdf0e10cSrcweir 			    }
1839cdf0e10cSrcweir 			    else
1840cdf0e10cSrcweir 			    {
1841cdf0e10cSrcweir 				    //there wasn't any selection, select current and row beneath, otherwise only row beneath
1842cdf0e10cSrcweir 				    if(m_nCurRow<m_nRowCount-1)
1843cdf0e10cSrcweir 				    {
1844cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1845cdf0e10cSrcweir 					    m_nCurRow++;
1846cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1847cdf0e10cSrcweir 					    invalidateSelectedRegion( m_nCurRow-1, m_nCurRow );
1848cdf0e10cSrcweir 				    }
1849cdf0e10cSrcweir 				    else
1850cdf0e10cSrcweir 				    {
1851cdf0e10cSrcweir 					    m_aSelectedRows.push_back(m_nCurRow);
1852cdf0e10cSrcweir 					    invalidateRow( m_nCurRow );
1853cdf0e10cSrcweir 				    }
1854cdf0e10cSrcweir 			    }
1855cdf0e10cSrcweir 			    m_pSelEngine->SetAnchor(sal_True);
1856cdf0e10cSrcweir 			    m_nAnchor = m_nCurRow;
1857cdf0e10cSrcweir 			    ensureVisible(m_nCurColumn, m_nCurRow, false);
1858cdf0e10cSrcweir 			    selectionChanged = true;
1859cdf0e10cSrcweir 			    bSuccess = true;
1860cdf0e10cSrcweir 		    }
1861cdf0e10cSrcweir 	    }
1862cdf0e10cSrcweir         break;
1863cdf0e10cSrcweir 
1864cdf0e10cSrcweir 	    case cursorSelectRowAreaTop:
1865cdf0e10cSrcweir 	    {
1866cdf0e10cSrcweir 		    if(m_pSelEngine->GetSelectionMode() == NO_SELECTION)
1867cdf0e10cSrcweir 			    bSuccess = false;
1868cdf0e10cSrcweir 		    else if(m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION)
1869cdf0e10cSrcweir 			    bSuccess = false;
1870cdf0e10cSrcweir 		    else
1871cdf0e10cSrcweir 		    {
1872cdf0e10cSrcweir 			    //select the region between the current and the upper row
1873cdf0e10cSrcweir 			    RowPos iter = m_nCurRow;
1874cdf0e10cSrcweir 			    invalidateSelectedRegion( m_nCurRow, 0 );
1875cdf0e10cSrcweir 			    //put the rows in vector
1876cdf0e10cSrcweir 			    while(iter>=0)
1877cdf0e10cSrcweir 			    {
1878cdf0e10cSrcweir 				    if ( !isRowSelected( iter ) )
1879cdf0e10cSrcweir 					    m_aSelectedRows.push_back(iter);
1880cdf0e10cSrcweir 				    --iter;
1881cdf0e10cSrcweir 			    }
1882cdf0e10cSrcweir 			    m_nCurRow = 0;
1883cdf0e10cSrcweir 			    m_nAnchor = m_nCurRow;
1884cdf0e10cSrcweir 			    m_pSelEngine->SetAnchor(sal_True);
1885cdf0e10cSrcweir 			    ensureVisible(m_nCurColumn, 0, false);
1886cdf0e10cSrcweir 			    selectionChanged = true;
1887cdf0e10cSrcweir 			    bSuccess = true;
1888cdf0e10cSrcweir 		    }
1889cdf0e10cSrcweir 	    }
1890cdf0e10cSrcweir         break;
1891cdf0e10cSrcweir 
1892cdf0e10cSrcweir 	    case cursorSelectRowAreaBottom:
1893cdf0e10cSrcweir 	    {
1894cdf0e10cSrcweir 		    if(m_pSelEngine->GetSelectionMode() == NO_SELECTION)
1895cdf0e10cSrcweir 			    return bSuccess = false;
1896cdf0e10cSrcweir 		    else if(m_pSelEngine->GetSelectionMode() == SINGLE_SELECTION)
1897cdf0e10cSrcweir 			    return bSuccess = false;
1898cdf0e10cSrcweir 		    //select the region between the current and the last row
1899cdf0e10cSrcweir 		    RowPos iter = m_nCurRow;
1900cdf0e10cSrcweir 		    invalidateSelectedRegion( m_nCurRow, m_nRowCount-1 );
1901cdf0e10cSrcweir 		    //put the rows in the vector
1902cdf0e10cSrcweir 		    while(iter<=m_nRowCount)
1903cdf0e10cSrcweir 		    {
1904cdf0e10cSrcweir 			    if ( !isRowSelected( iter ) )
1905cdf0e10cSrcweir 				    m_aSelectedRows.push_back(iter);
1906cdf0e10cSrcweir 			    ++iter;
1907cdf0e10cSrcweir 		    }
1908cdf0e10cSrcweir 		    m_nCurRow = m_nRowCount-1;
1909cdf0e10cSrcweir 		    m_nAnchor = m_nCurRow;
1910cdf0e10cSrcweir 		    m_pSelEngine->SetAnchor(sal_True);
1911cdf0e10cSrcweir 		    ensureVisible(m_nCurColumn, m_nRowCount-1, false);
1912cdf0e10cSrcweir 		    selectionChanged = true;
1913cdf0e10cSrcweir 		    bSuccess = true;
1914cdf0e10cSrcweir 	    }
1915cdf0e10cSrcweir         break;
1916cdf0e10cSrcweir         default:
1917cdf0e10cSrcweir             DBG_ERROR( "TableControl_Impl::dispatchAction: unsupported action!" );
1918cdf0e10cSrcweir             break;
1919cdf0e10cSrcweir         }
1920cdf0e10cSrcweir 
1921cdf0e10cSrcweir         if ( bSuccess && selectionChanged )
1922cdf0e10cSrcweir         {
1923cdf0e10cSrcweir             m_rAntiImpl.Select();
1924cdf0e10cSrcweir         }
1925cdf0e10cSrcweir 
1926cdf0e10cSrcweir         return bSuccess;
1927cdf0e10cSrcweir     }
1928cdf0e10cSrcweir 
1929cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
1930cdf0e10cSrcweir     void TableControl_Impl::impl_ni_doSwitchCursor( bool _bShow )
1931cdf0e10cSrcweir     {
1932cdf0e10cSrcweir         PTableRenderer pRenderer = !!m_pModel ? m_pModel->getRenderer() : PTableRenderer();
1933cdf0e10cSrcweir         if ( !!pRenderer )
1934cdf0e10cSrcweir         {
1935cdf0e10cSrcweir             Rectangle aCellRect;
1936cdf0e10cSrcweir             impl_getCellRect( m_nCurColumn, m_nCurRow, aCellRect );
1937cdf0e10cSrcweir 			if ( _bShow )
1938cdf0e10cSrcweir 				pRenderer->ShowCellCursor( *m_pDataWindow, aCellRect );
1939cdf0e10cSrcweir 			else
1940cdf0e10cSrcweir 				pRenderer->HideCellCursor( *m_pDataWindow, aCellRect );
1941cdf0e10cSrcweir         }
1942cdf0e10cSrcweir     }
1943cdf0e10cSrcweir 
1944cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1945cdf0e10cSrcweir     void TableControl_Impl::impl_getCellRect( ColPos _nColumn, RowPos _nRow, Rectangle& _rCellRect ) const
1946cdf0e10cSrcweir     {
1947cdf0e10cSrcweir         DBG_CHECK_ME();
1948cdf0e10cSrcweir 
1949cdf0e10cSrcweir         if  (   !m_pModel
1950cdf0e10cSrcweir             ||  ( COL_INVALID == _nColumn )
1951cdf0e10cSrcweir             ||  ( ROW_INVALID == _nRow )
1952cdf0e10cSrcweir             )
1953cdf0e10cSrcweir         {
1954cdf0e10cSrcweir             _rCellRect.SetEmpty();
1955cdf0e10cSrcweir             return;
1956cdf0e10cSrcweir         }
1957cdf0e10cSrcweir 
1958cdf0e10cSrcweir         TableCellGeometry aCell( *this, impl_getAllVisibleCellsArea(), _nColumn, _nRow );
1959cdf0e10cSrcweir         _rCellRect = aCell.getRect();
1960cdf0e10cSrcweir     }
1961cdf0e10cSrcweir 
1962cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1963cdf0e10cSrcweir     RowPos TableControl_Impl::getRowAtPoint( const Point& rPoint ) const
1964cdf0e10cSrcweir     {
1965cdf0e10cSrcweir 	    DBG_CHECK_ME();
1966cdf0e10cSrcweir         return impl_getRowForAbscissa( rPoint.Y() );
1967cdf0e10cSrcweir     }
1968cdf0e10cSrcweir 
1969cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1970cdf0e10cSrcweir     ColPos TableControl_Impl::getColAtPoint( const Point& rPoint ) const
1971cdf0e10cSrcweir     {
1972cdf0e10cSrcweir 	    DBG_CHECK_ME();
1973cdf0e10cSrcweir         return impl_getColumnForOrdinate( rPoint.X() );
1974cdf0e10cSrcweir     }
1975cdf0e10cSrcweir 
1976cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1977cdf0e10cSrcweir     TableCell TableControl_Impl::hitTest( Point const & i_point ) const
1978cdf0e10cSrcweir     {
1979cdf0e10cSrcweir         TableCell aCell( getColAtPoint( i_point ), getRowAtPoint( i_point ) );
1980cdf0e10cSrcweir         if ( aCell.nColumn > COL_ROW_HEADERS )
1981cdf0e10cSrcweir         {
1982cdf0e10cSrcweir             PColumnModel const pColumn = m_pModel->getColumnModel( aCell.nColumn );
1983cdf0e10cSrcweir             MutableColumnMetrics const & rColInfo( m_aColumnWidths[ aCell.nColumn ] );
1984cdf0e10cSrcweir             if  (   ( rColInfo.getEnd() - 3 <= i_point.X() )
1985cdf0e10cSrcweir                 &&  ( rColInfo.getEnd() >= i_point.X() )
1986cdf0e10cSrcweir                 &&  pColumn->isResizable()
1987cdf0e10cSrcweir                 )
1988cdf0e10cSrcweir             {
1989cdf0e10cSrcweir                 aCell.eArea = ColumnDivider;
1990cdf0e10cSrcweir             }
1991cdf0e10cSrcweir         }
1992cdf0e10cSrcweir         return aCell;
1993cdf0e10cSrcweir     }
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
1996cdf0e10cSrcweir     ColumnMetrics TableControl_Impl::getColumnMetrics( ColPos const i_column ) const
1997cdf0e10cSrcweir     {
1998cdf0e10cSrcweir         DBG_CHECK_ME();
1999cdf0e10cSrcweir 
2000cdf0e10cSrcweir         ENSURE_OR_RETURN( ( i_column >= 0 ) && ( i_column < m_pModel->getColumnCount() ),
2001cdf0e10cSrcweir             "TableControl_Impl::getColumnMetrics: illegal column index!", ColumnMetrics() );
2002cdf0e10cSrcweir         return (ColumnMetrics const &)m_aColumnWidths[ i_column ];
2003cdf0e10cSrcweir     }
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2006cdf0e10cSrcweir     PTableModel TableControl_Impl::getModel() const
2007cdf0e10cSrcweir     {
2008cdf0e10cSrcweir         return m_pModel;
2009cdf0e10cSrcweir     }
2010cdf0e10cSrcweir 
2011cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2012cdf0e10cSrcweir     RowPos TableControl_Impl::getCurrentColumn() const
2013cdf0e10cSrcweir     {
2014cdf0e10cSrcweir         return m_nCurColumn;
2015cdf0e10cSrcweir     }
2016cdf0e10cSrcweir 
2017cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2018cdf0e10cSrcweir     RowPos TableControl_Impl::getCurrentRow() const
2019cdf0e10cSrcweir     {
2020cdf0e10cSrcweir         return m_nCurRow;
2021cdf0e10cSrcweir     }
2022cdf0e10cSrcweir 
2023cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2024cdf0e10cSrcweir     ::Size TableControl_Impl::getTableSizePixel() const
2025cdf0e10cSrcweir     {
2026cdf0e10cSrcweir         return m_pDataWindow->GetOutputSizePixel();
2027cdf0e10cSrcweir     }
2028cdf0e10cSrcweir 
2029cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2030cdf0e10cSrcweir     void TableControl_Impl::setPointer( Pointer const & i_pointer )
2031cdf0e10cSrcweir     {
2032cdf0e10cSrcweir         DBG_CHECK_ME();
2033cdf0e10cSrcweir         m_pDataWindow->SetPointer( i_pointer );
2034cdf0e10cSrcweir     }
2035cdf0e10cSrcweir 
2036cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2037cdf0e10cSrcweir     void TableControl_Impl::captureMouse()
2038cdf0e10cSrcweir     {
2039cdf0e10cSrcweir         m_pDataWindow->CaptureMouse();
2040cdf0e10cSrcweir     }
2041cdf0e10cSrcweir 
2042cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2043cdf0e10cSrcweir     void TableControl_Impl::releaseMouse()
2044cdf0e10cSrcweir     {
2045cdf0e10cSrcweir         m_pDataWindow->ReleaseMouse();
2046cdf0e10cSrcweir     }
2047cdf0e10cSrcweir 
2048cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2049cdf0e10cSrcweir     void TableControl_Impl::invalidate( TableArea const i_what )
2050cdf0e10cSrcweir     {
2051cdf0e10cSrcweir         switch ( i_what )
2052cdf0e10cSrcweir         {
2053cdf0e10cSrcweir         case TableAreaColumnHeaders:
2054cdf0e10cSrcweir             m_pDataWindow->Invalidate( calcHeaderRect( true ) );
2055cdf0e10cSrcweir             break;
2056cdf0e10cSrcweir 
2057cdf0e10cSrcweir         case TableAreaRowHeaders:
2058cdf0e10cSrcweir             m_pDataWindow->Invalidate( calcHeaderRect( false ) );
2059cdf0e10cSrcweir             break;
2060cdf0e10cSrcweir 
2061cdf0e10cSrcweir         case TableAreaDataArea:
2062cdf0e10cSrcweir             m_pDataWindow->Invalidate( impl_getAllVisibleDataCellArea() );
2063cdf0e10cSrcweir             break;
2064cdf0e10cSrcweir 
2065cdf0e10cSrcweir         case TableAreaAll:
2066cdf0e10cSrcweir             m_pDataWindow->Invalidate();
2067cdf0e10cSrcweir             break;
2068cdf0e10cSrcweir         }
2069cdf0e10cSrcweir     }
2070cdf0e10cSrcweir 
2071cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2072cdf0e10cSrcweir     long TableControl_Impl::pixelWidthToAppFont( long const i_pixels ) const
2073cdf0e10cSrcweir     {
2074cdf0e10cSrcweir         return m_pDataWindow->PixelToLogic( Size( i_pixels, 0 ), MAP_APPFONT ).Width();
2075cdf0e10cSrcweir     }
2076cdf0e10cSrcweir 
2077cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2078cdf0e10cSrcweir     long TableControl_Impl::appFontWidthToPixel( long const i_appFontUnits ) const
2079cdf0e10cSrcweir     {
2080cdf0e10cSrcweir         return m_pDataWindow->LogicToPixel( Size( i_appFontUnits, 0 ), MAP_APPFONT ).Width();
2081cdf0e10cSrcweir     }
2082cdf0e10cSrcweir 
2083cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2084cdf0e10cSrcweir     void TableControl_Impl::hideTracking()
2085cdf0e10cSrcweir     {
2086cdf0e10cSrcweir         m_pDataWindow->HideTracking();
2087cdf0e10cSrcweir     }
2088cdf0e10cSrcweir 
2089cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2090cdf0e10cSrcweir     void TableControl_Impl::showTracking( Rectangle const & i_location, sal_uInt16 const i_flags )
2091cdf0e10cSrcweir     {
2092cdf0e10cSrcweir         m_pDataWindow->ShowTracking( i_location, i_flags );
2093cdf0e10cSrcweir     }
2094cdf0e10cSrcweir 
2095cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2096cdf0e10cSrcweir     bool TableControl_Impl::activateCell( ColPos const i_col, RowPos const i_row )
2097cdf0e10cSrcweir     {
2098cdf0e10cSrcweir 	    DBG_CHECK_ME();
2099cdf0e10cSrcweir         return goTo( i_col, i_row );
2100cdf0e10cSrcweir     }
2101cdf0e10cSrcweir 
2102cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2103cdf0e10cSrcweir     void TableControl_Impl::invalidateSelectedRegion( RowPos _nPrevRow, RowPos _nCurRow )
2104cdf0e10cSrcweir     {
2105cdf0e10cSrcweir 	    DBG_CHECK_ME();
2106cdf0e10cSrcweir 	    // get the visible area of the table control and set the Left and right border of the region to be repainted
2107cdf0e10cSrcweir 	    Rectangle const aAllCells( impl_getAllVisibleCellsArea() );
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir         Rectangle aInvalidateRect;
2110cdf0e10cSrcweir 	    aInvalidateRect.Left() = aAllCells.Left();
2111cdf0e10cSrcweir 	    aInvalidateRect.Right() = aAllCells.Right();
2112cdf0e10cSrcweir 	    // if only one row is selected
2113cdf0e10cSrcweir 	    if ( _nPrevRow == _nCurRow )
2114cdf0e10cSrcweir 	    {
2115cdf0e10cSrcweir 	        Rectangle aCellRect;
2116cdf0e10cSrcweir 		    impl_getCellRect( m_nCurColumn, _nCurRow, aCellRect );
2117cdf0e10cSrcweir 		    aInvalidateRect.Top() = aCellRect.Top();
2118cdf0e10cSrcweir 		    aInvalidateRect.Bottom() = aCellRect.Bottom();
2119cdf0e10cSrcweir 	    }
2120cdf0e10cSrcweir 	    //if the region is above the current row
2121cdf0e10cSrcweir 	    else if(_nPrevRow < _nCurRow )
2122cdf0e10cSrcweir 	    {
2123cdf0e10cSrcweir 	        Rectangle aCellRect;
2124cdf0e10cSrcweir 		    impl_getCellRect( m_nCurColumn, _nPrevRow, aCellRect );
2125cdf0e10cSrcweir 		    aInvalidateRect.Top() = aCellRect.Top();
2126cdf0e10cSrcweir 		    impl_getCellRect( m_nCurColumn, _nCurRow, aCellRect );
2127cdf0e10cSrcweir 		    aInvalidateRect.Bottom() = aCellRect.Bottom();
2128cdf0e10cSrcweir 	    }
2129cdf0e10cSrcweir 	    //if the region is beneath the current row
2130cdf0e10cSrcweir 	    else
2131cdf0e10cSrcweir 	    {
2132cdf0e10cSrcweir             Rectangle aCellRect;
2133cdf0e10cSrcweir 		    impl_getCellRect( m_nCurColumn, _nCurRow, aCellRect );
2134cdf0e10cSrcweir 		    aInvalidateRect.Top() = aCellRect.Top();
2135cdf0e10cSrcweir 		    impl_getCellRect( m_nCurColumn, _nPrevRow, aCellRect );
2136cdf0e10cSrcweir 		    aInvalidateRect.Bottom() = aCellRect.Bottom();
2137cdf0e10cSrcweir 	    }
2138cdf0e10cSrcweir 	    m_pDataWindow->Invalidate( aInvalidateRect );
2139cdf0e10cSrcweir     }
2140cdf0e10cSrcweir 
2141cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2142cdf0e10cSrcweir     void TableControl_Impl::invalidateSelectedRows()
2143cdf0e10cSrcweir     {
2144cdf0e10cSrcweir         for (   ::std::vector< RowPos >::iterator selRow = m_aSelectedRows.begin();
2145cdf0e10cSrcweir 				selRow != m_aSelectedRows.end();
2146cdf0e10cSrcweir                 ++selRow
2147cdf0e10cSrcweir             )
2148cdf0e10cSrcweir 		{
2149cdf0e10cSrcweir             invalidateRow( *selRow );
2150cdf0e10cSrcweir 		}
2151cdf0e10cSrcweir     }
2152cdf0e10cSrcweir 
2153cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2154cdf0e10cSrcweir     void TableControl_Impl::invalidateRowRange( RowPos const i_firstRow, RowPos const i_lastRow )
2155cdf0e10cSrcweir     {
2156cdf0e10cSrcweir         RowPos const firstRow = i_firstRow < m_nTopRow ? m_nTopRow : i_firstRow;
2157cdf0e10cSrcweir         RowPos const lastVisibleRow = m_nTopRow + impl_getVisibleRows( true ) - 1;
2158cdf0e10cSrcweir         RowPos const lastRow = ( ( i_lastRow == ROW_INVALID ) || ( i_lastRow > lastVisibleRow ) ) ? lastVisibleRow : i_lastRow;
2159cdf0e10cSrcweir 
2160cdf0e10cSrcweir         Rectangle aInvalidateRect;
2161cdf0e10cSrcweir 
2162cdf0e10cSrcweir         Rectangle const aVisibleCellsArea( impl_getAllVisibleCellsArea() );
2163cdf0e10cSrcweir         TableRowGeometry aRow( *this, aVisibleCellsArea, firstRow, true );
2164cdf0e10cSrcweir         while ( aRow.isValid() && ( aRow.getRow() <= lastRow ) )
2165cdf0e10cSrcweir         {
2166cdf0e10cSrcweir             aInvalidateRect.Union( aRow.getRect() );
2167cdf0e10cSrcweir             aRow.moveDown();
2168cdf0e10cSrcweir         }
2169cdf0e10cSrcweir 
2170cdf0e10cSrcweir         if ( i_lastRow == ROW_INVALID )
2171cdf0e10cSrcweir             aInvalidateRect.Bottom() = m_pDataWindow->GetOutputSizePixel().Height();
2172cdf0e10cSrcweir 
2173cdf0e10cSrcweir         m_pDataWindow->Invalidate( aInvalidateRect );
2174cdf0e10cSrcweir     }
2175cdf0e10cSrcweir 
2176cdf0e10cSrcweir     //------------------------------------------------------------------------------
2177cdf0e10cSrcweir     void TableControl_Impl::checkCursorPosition()
2178cdf0e10cSrcweir     {
2179cdf0e10cSrcweir         DBG_CHECK_ME();
2180cdf0e10cSrcweir 
2181cdf0e10cSrcweir 		TableSize nVisibleRows = impl_getVisibleRows(true);
2182cdf0e10cSrcweir 		TableSize nVisibleCols = impl_getVisibleColumns(true);
2183cdf0e10cSrcweir 		if  (   ( m_nTopRow + nVisibleRows > m_nRowCount )
2184cdf0e10cSrcweir             &&  ( m_nRowCount >= nVisibleRows )
2185cdf0e10cSrcweir             )
2186cdf0e10cSrcweir         {
2187cdf0e10cSrcweir 			--m_nTopRow;
2188cdf0e10cSrcweir         }
2189cdf0e10cSrcweir 		else
2190cdf0e10cSrcweir         {
2191cdf0e10cSrcweir 			m_nTopRow = 0;
2192cdf0e10cSrcweir         }
2193cdf0e10cSrcweir 
2194cdf0e10cSrcweir 		if  (   ( m_nLeftColumn + nVisibleCols > m_nColumnCount )
2195cdf0e10cSrcweir             &&  ( m_nColumnCount >= nVisibleCols )
2196cdf0e10cSrcweir             )
2197cdf0e10cSrcweir         {
2198cdf0e10cSrcweir 			--m_nLeftColumn;
2199cdf0e10cSrcweir         }
2200cdf0e10cSrcweir 		else
2201cdf0e10cSrcweir         {
2202cdf0e10cSrcweir 			m_nLeftColumn = 0;
2203cdf0e10cSrcweir         }
2204cdf0e10cSrcweir 
2205cdf0e10cSrcweir 		m_pDataWindow->Invalidate();
2206cdf0e10cSrcweir     }
2207cdf0e10cSrcweir 
2208cdf0e10cSrcweir     //--------------------------------------------------------------------
2209cdf0e10cSrcweir     TableSize TableControl_Impl::impl_getVisibleRows( bool _bAcceptPartialRow ) const
2210cdf0e10cSrcweir     {
2211cdf0e10cSrcweir         DBG_CHECK_ME();
2212cdf0e10cSrcweir 
2213cdf0e10cSrcweir         DBG_ASSERT( m_pDataWindow, "TableControl_Impl::impl_getVisibleRows: no data window!" );
2214cdf0e10cSrcweir 
2215cdf0e10cSrcweir         return lcl_getRowsFittingInto(
2216cdf0e10cSrcweir             m_pDataWindow->GetOutputSizePixel().Height() - m_nColHeaderHeightPixel,
2217cdf0e10cSrcweir             m_nRowHeightPixel,
2218cdf0e10cSrcweir             _bAcceptPartialRow
2219cdf0e10cSrcweir         );
2220cdf0e10cSrcweir     }
2221cdf0e10cSrcweir 
2222cdf0e10cSrcweir     //--------------------------------------------------------------------
2223cdf0e10cSrcweir     TableSize TableControl_Impl::impl_getVisibleColumns( bool _bAcceptPartialCol ) const
2224cdf0e10cSrcweir     {
2225cdf0e10cSrcweir         DBG_CHECK_ME();
2226cdf0e10cSrcweir 
2227cdf0e10cSrcweir         DBG_ASSERT( m_pDataWindow, "TableControl_Impl::impl_getVisibleColumns: no data window!" );
2228cdf0e10cSrcweir 
2229cdf0e10cSrcweir         return lcl_getColumnsVisibleWithin(
2230cdf0e10cSrcweir             Rectangle( Point( 0, 0 ), m_pDataWindow->GetOutputSizePixel() ),
2231cdf0e10cSrcweir             m_nLeftColumn,
2232cdf0e10cSrcweir             *this,
2233cdf0e10cSrcweir             _bAcceptPartialCol
2234cdf0e10cSrcweir         );
2235cdf0e10cSrcweir     }
2236cdf0e10cSrcweir 
2237cdf0e10cSrcweir     //--------------------------------------------------------------------
2238cdf0e10cSrcweir     bool TableControl_Impl::goTo( ColPos _nColumn, RowPos _nRow )
2239cdf0e10cSrcweir     {
2240cdf0e10cSrcweir         DBG_CHECK_ME();
2241cdf0e10cSrcweir 
2242cdf0e10cSrcweir         // TODO: give veto listeners a chance
2243cdf0e10cSrcweir 
2244cdf0e10cSrcweir         if  (  ( _nColumn < 0 ) || ( _nColumn >= m_nColumnCount )
2245cdf0e10cSrcweir             || ( _nRow < 0 ) || ( _nRow >= m_nRowCount )
2246cdf0e10cSrcweir             )
2247cdf0e10cSrcweir         {
2248cdf0e10cSrcweir             OSL_ENSURE( false, "TableControl_Impl::goTo: invalid row or column index!" );
2249cdf0e10cSrcweir             return false;
2250cdf0e10cSrcweir         }
2251cdf0e10cSrcweir 
2252cdf0e10cSrcweir         SuppressCursor aHideCursor( *this );
2253cdf0e10cSrcweir         m_nCurColumn = _nColumn;
2254cdf0e10cSrcweir         m_nCurRow = _nRow;
2255cdf0e10cSrcweir 
2256cdf0e10cSrcweir         // ensure that the new cell is visible
2257cdf0e10cSrcweir         ensureVisible( m_nCurColumn, m_nCurRow, false );
2258cdf0e10cSrcweir         return true;
2259cdf0e10cSrcweir     }
2260cdf0e10cSrcweir 
2261cdf0e10cSrcweir     //--------------------------------------------------------------------
2262cdf0e10cSrcweir     void TableControl_Impl::ensureVisible( ColPos _nColumn, RowPos _nRow, bool _bAcceptPartialVisibility )
2263cdf0e10cSrcweir     {
2264cdf0e10cSrcweir         DBG_CHECK_ME();
2265cdf0e10cSrcweir         DBG_ASSERT( ( _nColumn >= 0 ) && ( _nColumn < m_nColumnCount )
2266cdf0e10cSrcweir                  && ( _nRow >= 0 ) && ( _nRow < m_nRowCount ),
2267cdf0e10cSrcweir                  "TableControl_Impl::ensureVisible: invalid coordinates!" );
2268cdf0e10cSrcweir 
2269cdf0e10cSrcweir         SuppressCursor aHideCursor( *this );
2270cdf0e10cSrcweir 
2271cdf0e10cSrcweir         if ( _nColumn < m_nLeftColumn )
2272cdf0e10cSrcweir             impl_scrollColumns( _nColumn - m_nLeftColumn );
2273cdf0e10cSrcweir         else
2274cdf0e10cSrcweir         {
2275cdf0e10cSrcweir             TableSize nVisibleColumns = impl_getVisibleColumns( _bAcceptPartialVisibility );
2276cdf0e10cSrcweir             if ( _nColumn > m_nLeftColumn + nVisibleColumns - 1 )
2277cdf0e10cSrcweir             {
2278cdf0e10cSrcweir                 impl_scrollColumns( _nColumn - ( m_nLeftColumn + nVisibleColumns - 1 ) );
2279cdf0e10cSrcweir                 // TODO: since not all columns have the same width, this might in theory result
2280cdf0e10cSrcweir                 // in the column still not being visible.
2281cdf0e10cSrcweir             }
2282cdf0e10cSrcweir         }
2283cdf0e10cSrcweir 
2284cdf0e10cSrcweir         if ( _nRow < m_nTopRow )
2285cdf0e10cSrcweir             impl_scrollRows( _nRow - m_nTopRow );
2286cdf0e10cSrcweir         else
2287cdf0e10cSrcweir         {
2288cdf0e10cSrcweir             TableSize nVisibleRows = impl_getVisibleRows( _bAcceptPartialVisibility );
2289cdf0e10cSrcweir             if ( _nRow > m_nTopRow + nVisibleRows - 1 )
2290cdf0e10cSrcweir                 impl_scrollRows( _nRow - ( m_nTopRow + nVisibleRows - 1 ) );
2291cdf0e10cSrcweir         }
2292cdf0e10cSrcweir     }
2293cdf0e10cSrcweir 
2294cdf0e10cSrcweir     //--------------------------------------------------------------------
2295cdf0e10cSrcweir     ::rtl::OUString TableControl_Impl::getCellContentAsString( RowPos const i_row, ColPos const i_col )
2296cdf0e10cSrcweir     {
2297cdf0e10cSrcweir         Any aCellValue;
2298cdf0e10cSrcweir         m_pModel->getCellContent( i_col, i_row, aCellValue );
2299cdf0e10cSrcweir 
2300cdf0e10cSrcweir         ::rtl::OUString sCellStringContent;
2301cdf0e10cSrcweir         m_pModel->getRenderer()->GetFormattedCellString( aCellValue, i_col, i_row, sCellStringContent );
2302cdf0e10cSrcweir 
2303cdf0e10cSrcweir         return sCellStringContent;
2304cdf0e10cSrcweir     }
2305cdf0e10cSrcweir 
2306cdf0e10cSrcweir     //--------------------------------------------------------------------
2307cdf0e10cSrcweir     TableSize TableControl_Impl::impl_ni_ScrollRows( TableSize _nRowDelta )
2308cdf0e10cSrcweir     {
2309cdf0e10cSrcweir         // compute new top row
2310cdf0e10cSrcweir         RowPos nNewTopRow =
2311cdf0e10cSrcweir             ::std::max(
2312cdf0e10cSrcweir                 ::std::min( (RowPos)( m_nTopRow + _nRowDelta ), (RowPos)( m_nRowCount - 1 ) ),
2313cdf0e10cSrcweir                 (RowPos)0
2314cdf0e10cSrcweir             );
2315cdf0e10cSrcweir 
2316cdf0e10cSrcweir         RowPos nOldTopRow = m_nTopRow;
2317cdf0e10cSrcweir         m_nTopRow = nNewTopRow;
2318cdf0e10cSrcweir 
2319cdf0e10cSrcweir         // if updates are enabled currently, scroll the viewport
2320cdf0e10cSrcweir         if ( m_nTopRow != nOldTopRow )
2321cdf0e10cSrcweir         {
2322cdf0e10cSrcweir             DBG_SUSPEND_INV( INV_SCROLL_POSITION );
2323cdf0e10cSrcweir             SuppressCursor aHideCursor( *this );
2324cdf0e10cSrcweir             // TODO: call a onStartScroll at our listener (or better an own onStartScroll,
2325cdf0e10cSrcweir             // which hides the cursor and then calls the listener)
2326cdf0e10cSrcweir             // Same for onEndScroll
2327cdf0e10cSrcweir 
2328cdf0e10cSrcweir             // scroll the view port, if possible
2329cdf0e10cSrcweir             long nPixelDelta = m_nRowHeightPixel * ( m_nTopRow - nOldTopRow );
2330cdf0e10cSrcweir 
2331cdf0e10cSrcweir             Rectangle aDataArea( Point( 0, m_nColHeaderHeightPixel ), m_pDataWindow->GetOutputSizePixel() );
2332cdf0e10cSrcweir 
2333cdf0e10cSrcweir             if  (   m_pDataWindow->GetBackground().IsScrollable()
2334cdf0e10cSrcweir                 &&  abs( nPixelDelta ) < aDataArea.GetHeight()
2335cdf0e10cSrcweir                 )
2336cdf0e10cSrcweir             {
2337cdf0e10cSrcweir                 m_pDataWindow->Scroll( 0, (long)-nPixelDelta, aDataArea, SCROLL_CLIP | SCROLL_UPDATE | SCROLL_CHILDREN);
2338cdf0e10cSrcweir             }
2339cdf0e10cSrcweir             else
2340cdf0e10cSrcweir                 m_pDataWindow->Invalidate( INVALIDATE_UPDATE );
2341cdf0e10cSrcweir 
2342cdf0e10cSrcweir             // update the position at the vertical scrollbar
2343cdf0e10cSrcweir             if ( m_pVScroll != NULL )
2344cdf0e10cSrcweir                 m_pVScroll->SetThumbPos( m_nTopRow );
2345cdf0e10cSrcweir         }
2346cdf0e10cSrcweir 
2347cdf0e10cSrcweir         // The scroll bar availaility might change when we scrolled.
2348cdf0e10cSrcweir         // For instance, imagine a view with 10 rows, if which 5 fit into the window, numbered 1 to 10.
2349cdf0e10cSrcweir         // Now let
2350cdf0e10cSrcweir         // - the user scroll to row number 6, so the last 5 rows are visible
2351cdf0e10cSrcweir         // - somebody remove the last 4 rows
2352cdf0e10cSrcweir         // - the user scroll to row number 5 being the top row, so the last two rows are visible
2353cdf0e10cSrcweir         // - somebody remove row number 6
2354cdf0e10cSrcweir         // - the user scroll to row number 1
2355cdf0e10cSrcweir         // => in this case, the need for the scrollbar vanishes immediately.
2356cdf0e10cSrcweir         if ( m_nTopRow == 0 )
2357cdf0e10cSrcweir             m_rAntiImpl.PostUserEvent( LINK( this, TableControl_Impl, OnUpdateScrollbars ) );
2358cdf0e10cSrcweir 
2359cdf0e10cSrcweir         return (TableSize)( m_nTopRow - nOldTopRow );
2360cdf0e10cSrcweir     }
2361cdf0e10cSrcweir 
2362cdf0e10cSrcweir     //--------------------------------------------------------------------
2363cdf0e10cSrcweir     TableSize TableControl_Impl::impl_scrollRows( TableSize const i_rowDelta )
2364cdf0e10cSrcweir     {
2365cdf0e10cSrcweir         DBG_CHECK_ME();
2366cdf0e10cSrcweir         return impl_ni_ScrollRows( i_rowDelta );
2367cdf0e10cSrcweir     }
2368cdf0e10cSrcweir 
2369cdf0e10cSrcweir     //--------------------------------------------------------------------
2370cdf0e10cSrcweir     TableSize TableControl_Impl::impl_ni_ScrollColumns( TableSize _nColumnDelta )
2371cdf0e10cSrcweir     {
2372cdf0e10cSrcweir         // compute new left column
2373cdf0e10cSrcweir         const ColPos nNewLeftColumn =
2374cdf0e10cSrcweir             ::std::max(
2375cdf0e10cSrcweir                 ::std::min( (ColPos)( m_nLeftColumn + _nColumnDelta ), (ColPos)( m_nColumnCount - 1 ) ),
2376cdf0e10cSrcweir                 (ColPos)0
2377cdf0e10cSrcweir             );
2378cdf0e10cSrcweir 
2379cdf0e10cSrcweir         const ColPos nOldLeftColumn = m_nLeftColumn;
2380cdf0e10cSrcweir         m_nLeftColumn = nNewLeftColumn;
2381cdf0e10cSrcweir 
2382cdf0e10cSrcweir         // if updates are enabled currently, scroll the viewport
2383cdf0e10cSrcweir         if ( m_nLeftColumn != nOldLeftColumn )
2384cdf0e10cSrcweir         {
2385cdf0e10cSrcweir             DBG_SUSPEND_INV( INV_SCROLL_POSITION );
2386cdf0e10cSrcweir             SuppressCursor aHideCursor( *this );
2387cdf0e10cSrcweir             // TODO: call a onStartScroll at our listener (or better an own onStartScroll,
2388cdf0e10cSrcweir             // which hides the cursor and then calls the listener)
2389cdf0e10cSrcweir             // Same for onEndScroll
2390cdf0e10cSrcweir 
2391cdf0e10cSrcweir             // scroll the view port, if possible
2392cdf0e10cSrcweir             const Rectangle aDataArea( Point( m_nRowHeaderWidthPixel, 0 ), m_pDataWindow->GetOutputSizePixel() );
2393cdf0e10cSrcweir 
2394cdf0e10cSrcweir             long nPixelDelta =
2395cdf0e10cSrcweir                     m_aColumnWidths[ nOldLeftColumn ].getStart()
2396cdf0e10cSrcweir                 -   m_aColumnWidths[ m_nLeftColumn ].getStart();
2397cdf0e10cSrcweir 
2398cdf0e10cSrcweir             // update our column positions
2399cdf0e10cSrcweir             // Do this *before* scrolling, as SCROLL_UPDATE will trigger a paint, which already needs the correct
2400cdf0e10cSrcweir             // information in m_aColumnWidths
2401cdf0e10cSrcweir             for (   ColumnPositions::iterator colPos = m_aColumnWidths.begin();
2402cdf0e10cSrcweir                     colPos != m_aColumnWidths.end();
2403cdf0e10cSrcweir                     ++colPos
2404cdf0e10cSrcweir                  )
2405cdf0e10cSrcweir             {
2406cdf0e10cSrcweir                 colPos->move( nPixelDelta );
2407cdf0e10cSrcweir             }
2408cdf0e10cSrcweir 
2409cdf0e10cSrcweir             // scroll the window content (if supported and possible), or invalidate the complete window
2410cdf0e10cSrcweir             if  (   m_pDataWindow->GetBackground().IsScrollable()
2411cdf0e10cSrcweir                 &&  abs( nPixelDelta ) < aDataArea.GetWidth()
2412cdf0e10cSrcweir                 )
2413cdf0e10cSrcweir             {
2414cdf0e10cSrcweir                 m_pDataWindow->Scroll( nPixelDelta, 0, aDataArea, SCROLL_CLIP | SCROLL_UPDATE );
2415cdf0e10cSrcweir             }
2416cdf0e10cSrcweir             else
2417cdf0e10cSrcweir                 m_pDataWindow->Invalidate( INVALIDATE_UPDATE );
2418cdf0e10cSrcweir 
2419cdf0e10cSrcweir             // update the position at the horizontal scrollbar
2420cdf0e10cSrcweir             if ( m_pHScroll != NULL )
2421cdf0e10cSrcweir                 m_pHScroll->SetThumbPos( m_nLeftColumn );
2422cdf0e10cSrcweir         }
2423cdf0e10cSrcweir 
2424cdf0e10cSrcweir         // The scroll bar availaility might change when we scrolled. This is because we do not hide
2425cdf0e10cSrcweir         // the scrollbar when it is, in theory, unnecessary, but currently at a position > 0. In this case, it will
2426cdf0e10cSrcweir         // be auto-hidden when it's scrolled back to pos 0.
2427cdf0e10cSrcweir         if ( m_nLeftColumn == 0 )
2428cdf0e10cSrcweir             m_rAntiImpl.PostUserEvent( LINK( this, TableControl_Impl, OnUpdateScrollbars ) );
2429cdf0e10cSrcweir 
2430cdf0e10cSrcweir         return (TableSize)( m_nLeftColumn - nOldLeftColumn );
2431cdf0e10cSrcweir     }
2432cdf0e10cSrcweir 
2433cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2434cdf0e10cSrcweir     TableSize TableControl_Impl::impl_scrollColumns( TableSize const i_columnDelta )
2435cdf0e10cSrcweir     {
2436cdf0e10cSrcweir         DBG_CHECK_ME();
2437cdf0e10cSrcweir         return impl_ni_ScrollColumns( i_columnDelta );
2438cdf0e10cSrcweir     }
2439cdf0e10cSrcweir 
2440cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2441cdf0e10cSrcweir     SelectionEngine* TableControl_Impl::getSelEngine()
2442cdf0e10cSrcweir     {
2443cdf0e10cSrcweir 		return m_pSelEngine;
2444cdf0e10cSrcweir     }
2445cdf0e10cSrcweir 
2446cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2447cdf0e10cSrcweir     ScrollBar* TableControl_Impl::getHorzScrollbar()
2448cdf0e10cSrcweir     {
2449cdf0e10cSrcweir 		return m_pHScroll;
2450cdf0e10cSrcweir     }
2451cdf0e10cSrcweir 
2452cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2453cdf0e10cSrcweir     ScrollBar* TableControl_Impl::getVertScrollbar()
2454cdf0e10cSrcweir     {
2455cdf0e10cSrcweir 		return m_pVScroll;
2456cdf0e10cSrcweir     }
2457cdf0e10cSrcweir 
2458cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2459cdf0e10cSrcweir     bool TableControl_Impl::isRowSelected( RowPos i_row ) const
2460cdf0e10cSrcweir     {
2461cdf0e10cSrcweir 		return ::std::find( m_aSelectedRows.begin(), m_aSelectedRows.end(), i_row ) != m_aSelectedRows.end();
2462cdf0e10cSrcweir     }
2463cdf0e10cSrcweir 
2464cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2465cdf0e10cSrcweir     RowPos TableControl_Impl::getSelectedRowIndex( size_t const i_selectionIndex ) const
2466cdf0e10cSrcweir     {
2467cdf0e10cSrcweir         if ( i_selectionIndex < m_aSelectedRows.size() )
2468cdf0e10cSrcweir             return m_aSelectedRows[ i_selectionIndex ];
2469cdf0e10cSrcweir         return ROW_INVALID;
2470cdf0e10cSrcweir     }
2471cdf0e10cSrcweir 
2472cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2473cdf0e10cSrcweir     int TableControl_Impl::getRowSelectedNumber(const ::std::vector<RowPos>& selectedRows, RowPos current)
2474cdf0e10cSrcweir     {
2475cdf0e10cSrcweir 		std::vector<RowPos>::const_iterator it = ::std::find(selectedRows.begin(),selectedRows.end(),current);
2476cdf0e10cSrcweir 		if ( it != selectedRows.end() )
2477cdf0e10cSrcweir 		{
2478cdf0e10cSrcweir 			return it - selectedRows.begin();
2479cdf0e10cSrcweir 		}
2480cdf0e10cSrcweir 		return -1;
2481cdf0e10cSrcweir     }
2482cdf0e10cSrcweir 
2483cdf0e10cSrcweir     //--------------------------------------------------------------------
2484cdf0e10cSrcweir     ColPos TableControl_Impl::impl_getColumnForOrdinate( long const i_ordinate ) const
2485cdf0e10cSrcweir     {
2486cdf0e10cSrcweir         DBG_CHECK_ME();
2487cdf0e10cSrcweir 
2488cdf0e10cSrcweir         if ( ( m_aColumnWidths.empty() ) || ( i_ordinate < 0 ) )
2489cdf0e10cSrcweir             return COL_INVALID;
2490cdf0e10cSrcweir 
2491cdf0e10cSrcweir         if ( i_ordinate < m_nRowHeaderWidthPixel )
2492cdf0e10cSrcweir             return COL_ROW_HEADERS;
2493cdf0e10cSrcweir 
2494cdf0e10cSrcweir         ColumnPositions::const_iterator lowerBound = ::std::lower_bound(
2495cdf0e10cSrcweir             m_aColumnWidths.begin(),
2496cdf0e10cSrcweir             m_aColumnWidths.end(),
2497cdf0e10cSrcweir             i_ordinate + 1,
2498cdf0e10cSrcweir             ColumnInfoPositionLess()
2499cdf0e10cSrcweir         );
2500cdf0e10cSrcweir         if ( lowerBound == m_aColumnWidths.end() )
2501cdf0e10cSrcweir         {
2502cdf0e10cSrcweir             // point is *behind* the start of the last column ...
2503cdf0e10cSrcweir             if ( i_ordinate < m_aColumnWidths.rbegin()->getEnd() )
2504cdf0e10cSrcweir                 // ... but still before its end
2505cdf0e10cSrcweir                 return m_nColumnCount - 1;
2506cdf0e10cSrcweir             return COL_INVALID;
2507cdf0e10cSrcweir         }
2508cdf0e10cSrcweir         return lowerBound - m_aColumnWidths.begin();
2509cdf0e10cSrcweir     }
2510cdf0e10cSrcweir 
2511cdf0e10cSrcweir     //--------------------------------------------------------------------
2512cdf0e10cSrcweir     RowPos TableControl_Impl::impl_getRowForAbscissa( long const i_abscissa ) const
2513cdf0e10cSrcweir     {
2514cdf0e10cSrcweir         DBG_CHECK_ME();
2515cdf0e10cSrcweir 
2516cdf0e10cSrcweir         if ( i_abscissa < 0 )
2517cdf0e10cSrcweir             return ROW_INVALID;
2518cdf0e10cSrcweir 
2519cdf0e10cSrcweir         if ( i_abscissa < m_nColHeaderHeightPixel )
2520cdf0e10cSrcweir             return ROW_COL_HEADERS;
2521cdf0e10cSrcweir 
2522cdf0e10cSrcweir         long const abscissa = i_abscissa - m_nColHeaderHeightPixel;
2523cdf0e10cSrcweir         long const row = m_nTopRow + abscissa / m_nRowHeightPixel;
2524cdf0e10cSrcweir         return row < m_pModel->getRowCount() ? row : ROW_INVALID;
2525cdf0e10cSrcweir     }
2526cdf0e10cSrcweir 
2527cdf0e10cSrcweir     //--------------------------------------------------------------------
2528cdf0e10cSrcweir     bool TableControl_Impl::markRowAsDeselected( RowPos const i_rowIndex )
2529cdf0e10cSrcweir     {
2530cdf0e10cSrcweir         DBG_CHECK_ME();
2531cdf0e10cSrcweir 
2532cdf0e10cSrcweir         ::std::vector< RowPos >::iterator selPos = ::std::find( m_aSelectedRows.begin(), m_aSelectedRows.end(), i_rowIndex );
2533cdf0e10cSrcweir         if ( selPos == m_aSelectedRows.end() )
2534cdf0e10cSrcweir             return false;
2535cdf0e10cSrcweir 
2536cdf0e10cSrcweir         m_aSelectedRows.erase( selPos );
2537cdf0e10cSrcweir         return true;
2538cdf0e10cSrcweir     }
2539cdf0e10cSrcweir 
2540cdf0e10cSrcweir     //--------------------------------------------------------------------
2541cdf0e10cSrcweir     bool TableControl_Impl::markRowAsSelected( RowPos const i_rowIndex )
2542cdf0e10cSrcweir     {
2543cdf0e10cSrcweir         DBG_CHECK_ME();
2544cdf0e10cSrcweir 
2545cdf0e10cSrcweir         if ( isRowSelected( i_rowIndex ) )
2546cdf0e10cSrcweir             return false;
2547cdf0e10cSrcweir 
2548cdf0e10cSrcweir         SelectionMode const eSelMode = getSelEngine()->GetSelectionMode();
2549cdf0e10cSrcweir         switch ( eSelMode )
2550cdf0e10cSrcweir         {
2551cdf0e10cSrcweir         case SINGLE_SELECTION:
2552cdf0e10cSrcweir             if ( !m_aSelectedRows.empty() )
2553cdf0e10cSrcweir             {
2554cdf0e10cSrcweir                 OSL_ENSURE( m_aSelectedRows.size() == 1, "TableControl::markRowAsSelected: SingleSelection with more than one selected element?" );
2555cdf0e10cSrcweir                 m_aSelectedRows[0] = i_rowIndex;
2556cdf0e10cSrcweir                 break;
2557cdf0e10cSrcweir             }
2558cdf0e10cSrcweir             // fall through
2559cdf0e10cSrcweir 
2560cdf0e10cSrcweir         case MULTIPLE_SELECTION:
2561cdf0e10cSrcweir             m_aSelectedRows.push_back( i_rowIndex );
2562cdf0e10cSrcweir             break;
2563cdf0e10cSrcweir 
2564cdf0e10cSrcweir         default:
2565cdf0e10cSrcweir             OSL_ENSURE( false, "TableControl_Impl::markRowAsSelected: unsupported selection mode!" );
2566cdf0e10cSrcweir             return false;
2567cdf0e10cSrcweir         }
2568cdf0e10cSrcweir 
2569cdf0e10cSrcweir         return true;
2570cdf0e10cSrcweir     }
2571cdf0e10cSrcweir 
2572cdf0e10cSrcweir     //--------------------------------------------------------------------
2573cdf0e10cSrcweir     bool TableControl_Impl::markAllRowsAsDeselected()
2574cdf0e10cSrcweir     {
2575cdf0e10cSrcweir         if ( m_aSelectedRows.empty() )
2576cdf0e10cSrcweir             return false;
2577cdf0e10cSrcweir 
2578cdf0e10cSrcweir         m_aSelectedRows.clear();
2579cdf0e10cSrcweir         return true;
2580cdf0e10cSrcweir     }
2581cdf0e10cSrcweir 
2582cdf0e10cSrcweir     //--------------------------------------------------------------------
2583cdf0e10cSrcweir     bool TableControl_Impl::markAllRowsAsSelected()
2584cdf0e10cSrcweir     {
2585cdf0e10cSrcweir         DBG_CHECK_ME();
2586cdf0e10cSrcweir 
2587cdf0e10cSrcweir         SelectionMode const eSelMode = getSelEngine()->GetSelectionMode();
2588cdf0e10cSrcweir         ENSURE_OR_RETURN_FALSE( eSelMode == MULTIPLE_SELECTION, "TableControl_Impl::markAllRowsAsSelected: unsupported selection mode!" );
2589cdf0e10cSrcweir 
2590cdf0e10cSrcweir         if ( m_aSelectedRows.size() == size_t( m_pModel->getRowCount() ) )
2591cdf0e10cSrcweir         {
2592cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 0
2593cdf0e10cSrcweir             for ( TableSize row = 0; row < m_pModel->getRowCount(); ++row )
2594cdf0e10cSrcweir             {
2595cdf0e10cSrcweir                 OSL_ENSURE( isRowSelected( row ), "TableControl_Impl::markAllRowsAsSelected: inconsistency in the selected rows!" );
2596cdf0e10cSrcweir             }
2597cdf0e10cSrcweir         #endif
2598cdf0e10cSrcweir             // already all rows marked as selected
2599cdf0e10cSrcweir             return false;
2600cdf0e10cSrcweir         }
2601cdf0e10cSrcweir 
2602cdf0e10cSrcweir         m_aSelectedRows.clear();
2603cdf0e10cSrcweir         for ( RowPos i=0; i < m_pModel->getRowCount(); ++i )
2604cdf0e10cSrcweir             m_aSelectedRows.push_back(i);
2605cdf0e10cSrcweir 
2606cdf0e10cSrcweir         return true;
2607cdf0e10cSrcweir     }
2608cdf0e10cSrcweir 
2609cdf0e10cSrcweir     //--------------------------------------------------------------------
2610cdf0e10cSrcweir     void TableControl_Impl::commitAccessibleEvent( sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue )
2611cdf0e10cSrcweir     {
2612cdf0e10cSrcweir         impl_commitAccessibleEvent( i_eventID, i_newValue, i_oldValue );
2613cdf0e10cSrcweir     }
2614cdf0e10cSrcweir 
2615cdf0e10cSrcweir     //--------------------------------------------------------------------
2616cdf0e10cSrcweir     void TableControl_Impl::commitCellEvent( sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue )
2617cdf0e10cSrcweir     {
2618cdf0e10cSrcweir         DBG_CHECK_ME();
2619cdf0e10cSrcweir         if ( impl_isAccessibleAlive() )
2620cdf0e10cSrcweir  	        m_pAccessibleTable->commitCellEvent( i_eventID, i_newValue, i_oldValue );
2621cdf0e10cSrcweir     }
2622cdf0e10cSrcweir 
2623cdf0e10cSrcweir     //--------------------------------------------------------------------
2624cdf0e10cSrcweir     void TableControl_Impl::commitTableEvent( sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue )
2625cdf0e10cSrcweir     {
2626cdf0e10cSrcweir         DBG_CHECK_ME();
2627cdf0e10cSrcweir         if ( impl_isAccessibleAlive() )
2628cdf0e10cSrcweir  	        m_pAccessibleTable->commitTableEvent( i_eventID, i_newValue, i_oldValue );
2629cdf0e10cSrcweir     }
2630cdf0e10cSrcweir 
2631cdf0e10cSrcweir     //--------------------------------------------------------------------
2632cdf0e10cSrcweir 	Rectangle TableControl_Impl::calcHeaderRect(bool bColHeader)
2633cdf0e10cSrcweir 	{
2634cdf0e10cSrcweir         Rectangle const aRectTableWithHeaders( impl_getAllVisibleCellsArea() );
2635cdf0e10cSrcweir 		Size const aSizeTableWithHeaders( aRectTableWithHeaders.GetSize() );
2636cdf0e10cSrcweir 		if ( bColHeader )
2637cdf0e10cSrcweir 			return Rectangle( aRectTableWithHeaders.TopLeft(), Size( aSizeTableWithHeaders.Width(), m_nColHeaderHeightPixel ) );
2638cdf0e10cSrcweir 		else
2639cdf0e10cSrcweir 			return Rectangle( aRectTableWithHeaders.TopLeft(), Size( m_nRowHeaderWidthPixel, aSizeTableWithHeaders.Height() ) );
2640cdf0e10cSrcweir 	}
2641cdf0e10cSrcweir 
2642cdf0e10cSrcweir     //--------------------------------------------------------------------
2643cdf0e10cSrcweir     Rectangle TableControl_Impl::calcHeaderCellRect( bool bColHeader, sal_Int32 nPos )
2644cdf0e10cSrcweir     {
2645cdf0e10cSrcweir         Rectangle const aHeaderRect = calcHeaderRect( bColHeader );
2646cdf0e10cSrcweir         TableCellGeometry const aGeometry(
2647cdf0e10cSrcweir             *this, aHeaderRect,
2648cdf0e10cSrcweir             bColHeader ? nPos : COL_ROW_HEADERS,
2649cdf0e10cSrcweir             bColHeader ? ROW_COL_HEADERS : nPos
2650cdf0e10cSrcweir         );
2651cdf0e10cSrcweir         return aGeometry.getRect();
2652cdf0e10cSrcweir     }
2653cdf0e10cSrcweir 
2654cdf0e10cSrcweir     //--------------------------------------------------------------------
2655cdf0e10cSrcweir 	Rectangle TableControl_Impl::calcTableRect()
2656cdf0e10cSrcweir 	{
2657cdf0e10cSrcweir 		return impl_getAllVisibleDataCellArea();
2658cdf0e10cSrcweir 	}
2659cdf0e10cSrcweir 
2660cdf0e10cSrcweir     //--------------------------------------------------------------------
2661cdf0e10cSrcweir     Rectangle TableControl_Impl::calcCellRect( sal_Int32 nRow, sal_Int32 nCol )
2662cdf0e10cSrcweir     {
2663cdf0e10cSrcweir         Rectangle aCellRect;
2664cdf0e10cSrcweir         impl_getCellRect( nRow, nCol, aCellRect );
2665cdf0e10cSrcweir         return aCellRect;
2666cdf0e10cSrcweir     }
2667cdf0e10cSrcweir 
2668cdf0e10cSrcweir     //--------------------------------------------------------------------
2669cdf0e10cSrcweir     IMPL_LINK( TableControl_Impl, OnUpdateScrollbars, void*, /**/ )
2670cdf0e10cSrcweir     {
2671cdf0e10cSrcweir         DBG_CHECK_ME();
2672cdf0e10cSrcweir         // TODO: can't we simply use lcl_updateScrollbar here, so the scrollbars ranges are updated, instead of
2673cdf0e10cSrcweir         // doing a complete re-layout?
2674cdf0e10cSrcweir         impl_ni_relayout();
2675cdf0e10cSrcweir         return 1L;
2676cdf0e10cSrcweir     }
2677cdf0e10cSrcweir 
2678cdf0e10cSrcweir     //--------------------------------------------------------------------
2679cdf0e10cSrcweir     IMPL_LINK( TableControl_Impl, OnScroll, ScrollBar*, _pScrollbar )
2680cdf0e10cSrcweir     {
2681cdf0e10cSrcweir         DBG_ASSERT( ( _pScrollbar == m_pVScroll ) || ( _pScrollbar == m_pHScroll ),
2682cdf0e10cSrcweir             "TableControl_Impl::OnScroll: where did this come from?" );
2683cdf0e10cSrcweir 
2684cdf0e10cSrcweir         if ( _pScrollbar == m_pVScroll )
2685cdf0e10cSrcweir             impl_ni_ScrollRows( _pScrollbar->GetDelta() );
2686cdf0e10cSrcweir         else
2687cdf0e10cSrcweir             impl_ni_ScrollColumns( _pScrollbar->GetDelta() );
2688cdf0e10cSrcweir 
2689cdf0e10cSrcweir         return 0L;
2690cdf0e10cSrcweir     }
2691cdf0e10cSrcweir 
2692cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2693cdf0e10cSrcweir     Reference< XAccessible > TableControl_Impl::getAccessible( Window& i_parentWindow )
2694cdf0e10cSrcweir     {
2695cdf0e10cSrcweir         DBG_TESTSOLARMUTEX();
2696cdf0e10cSrcweir 	    if ( m_pAccessibleTable == NULL )
2697cdf0e10cSrcweir 		{
2698cdf0e10cSrcweir 			Reference< XAccessible > const xAccParent = i_parentWindow.GetAccessible();
2699cdf0e10cSrcweir 			if ( xAccParent.is() )
2700cdf0e10cSrcweir 			{
2701cdf0e10cSrcweir 				m_pAccessibleTable = m_aFactoryAccess.getFactory().createAccessibleTableControl(
2702cdf0e10cSrcweir 					xAccParent, m_rAntiImpl
2703cdf0e10cSrcweir 				);
2704cdf0e10cSrcweir 			}
2705cdf0e10cSrcweir 		}
2706cdf0e10cSrcweir 
2707cdf0e10cSrcweir 		Reference< XAccessible > xAccessible;
2708cdf0e10cSrcweir 		if ( m_pAccessibleTable )
2709cdf0e10cSrcweir 			xAccessible = m_pAccessibleTable->getMyself();
2710cdf0e10cSrcweir 		return xAccessible;
2711cdf0e10cSrcweir     }
2712cdf0e10cSrcweir 
2713cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2714cdf0e10cSrcweir     void TableControl_Impl::disposeAccessible()
2715cdf0e10cSrcweir     {
2716cdf0e10cSrcweir         if ( m_pAccessibleTable )
2717cdf0e10cSrcweir             m_pAccessibleTable->dispose();
2718cdf0e10cSrcweir         m_pAccessibleTable = NULL;
2719cdf0e10cSrcweir     }
2720cdf0e10cSrcweir 
2721cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2722cdf0e10cSrcweir     bool TableControl_Impl::impl_isAccessibleAlive() const
2723cdf0e10cSrcweir     {
2724cdf0e10cSrcweir         DBG_CHECK_ME();
2725cdf0e10cSrcweir         return ( NULL != m_pAccessibleTable ) && m_pAccessibleTable->isAlive();
2726cdf0e10cSrcweir     }
2727cdf0e10cSrcweir 
2728cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2729cdf0e10cSrcweir     void TableControl_Impl::impl_commitAccessibleEvent( sal_Int16 const i_eventID, Any const & i_newValue, Any const & i_oldValue )
2730cdf0e10cSrcweir     {
2731cdf0e10cSrcweir         DBG_CHECK_ME();
2732cdf0e10cSrcweir         if ( impl_isAccessibleAlive() )
2733cdf0e10cSrcweir  	        m_pAccessibleTable->commitEvent( i_eventID, i_newValue, i_oldValue );
2734cdf0e10cSrcweir     }
2735cdf0e10cSrcweir 
2736cdf0e10cSrcweir     //==================================================================================================================
2737cdf0e10cSrcweir     //= TableFunctionSet
2738cdf0e10cSrcweir     //==================================================================================================================
2739cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2740cdf0e10cSrcweir     TableFunctionSet::TableFunctionSet(TableControl_Impl* _pTableControl)
2741cdf0e10cSrcweir     	:m_pTableControl( _pTableControl)
2742cdf0e10cSrcweir 	    ,m_nCurrentRow( ROW_INVALID )
2743cdf0e10cSrcweir     {
2744cdf0e10cSrcweir     }
2745cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2746cdf0e10cSrcweir     TableFunctionSet::~TableFunctionSet()
2747cdf0e10cSrcweir     {
2748cdf0e10cSrcweir     }
2749cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2750cdf0e10cSrcweir     void TableFunctionSet::BeginDrag()
2751cdf0e10cSrcweir     {
2752cdf0e10cSrcweir     }
2753cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2754cdf0e10cSrcweir     void TableFunctionSet::CreateAnchor()
2755cdf0e10cSrcweir     {
2756cdf0e10cSrcweir 	    m_pTableControl->setAnchor( m_pTableControl->getCurRow() );
2757cdf0e10cSrcweir     }
2758cdf0e10cSrcweir 
2759cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2760cdf0e10cSrcweir     void TableFunctionSet::DestroyAnchor()
2761cdf0e10cSrcweir     {
2762cdf0e10cSrcweir 	    m_pTableControl->setAnchor( ROW_INVALID );
2763cdf0e10cSrcweir     }
2764cdf0e10cSrcweir 
2765cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2766cdf0e10cSrcweir     sal_Bool TableFunctionSet::SetCursorAtPoint(const Point& rPoint, sal_Bool bDontSelectAtCursor)
2767cdf0e10cSrcweir     {
2768cdf0e10cSrcweir 	    sal_Bool bHandled = sal_False;
2769cdf0e10cSrcweir 	    // newRow is the row which includes the point, getCurRow() is the last selected row, before the mouse click
2770cdf0e10cSrcweir 	    RowPos newRow = m_pTableControl->getRowAtPoint( rPoint );
2771cdf0e10cSrcweir         if ( newRow == ROW_COL_HEADERS )
2772cdf0e10cSrcweir             newRow = m_pTableControl->getTopRow();
2773cdf0e10cSrcweir 
2774cdf0e10cSrcweir         ColPos newCol = m_pTableControl->getColAtPoint( rPoint );
2775cdf0e10cSrcweir         if ( newCol == COL_ROW_HEADERS )
2776cdf0e10cSrcweir             newCol = m_pTableControl->getLeftColumn();
2777cdf0e10cSrcweir 
2778cdf0e10cSrcweir 	    if ( ( newRow == ROW_INVALID ) || ( newCol == COL_INVALID ) )
2779cdf0e10cSrcweir 		    return sal_False;
2780cdf0e10cSrcweir 
2781cdf0e10cSrcweir 	    if ( bDontSelectAtCursor )
2782cdf0e10cSrcweir 	    {
2783cdf0e10cSrcweir 		    if ( m_pTableControl->getSelectedRowCount() > 1 )
2784cdf0e10cSrcweir 			    m_pTableControl->getSelEngine()->AddAlways(sal_True);
2785cdf0e10cSrcweir 		    bHandled = sal_True;
2786cdf0e10cSrcweir 	    }
2787cdf0e10cSrcweir 	    else if ( m_pTableControl->getAnchor() == m_pTableControl->getCurRow() )
2788cdf0e10cSrcweir 	    {
2789cdf0e10cSrcweir 		    //selecting region,
2790cdf0e10cSrcweir 		    int diff = m_pTableControl->getCurRow() - newRow;
2791cdf0e10cSrcweir 		    //selected region lies above the last selection
2792cdf0e10cSrcweir 		    if( diff >= 0)
2793cdf0e10cSrcweir 		    {
2794cdf0e10cSrcweir 			    //put selected rows in vector
2795cdf0e10cSrcweir 			    while ( m_pTableControl->getAnchor() >= newRow )
2796cdf0e10cSrcweir 			    {
2797cdf0e10cSrcweir 				    m_pTableControl->markRowAsSelected( m_pTableControl->getAnchor() );
2798cdf0e10cSrcweir 				    m_pTableControl->setAnchor( m_pTableControl->getAnchor() - 1 );
2799cdf0e10cSrcweir 				    diff--;
2800cdf0e10cSrcweir 			    }
2801cdf0e10cSrcweir 			    m_pTableControl->setAnchor( m_pTableControl->getAnchor() + 1 );
2802cdf0e10cSrcweir 		    }
2803cdf0e10cSrcweir 		    //selected region lies beneath the last selected row
2804cdf0e10cSrcweir 		    else
2805cdf0e10cSrcweir 		    {
2806cdf0e10cSrcweir 			    while ( m_pTableControl->getAnchor() <= newRow )
2807cdf0e10cSrcweir 			    {
2808cdf0e10cSrcweir 				    m_pTableControl->markRowAsSelected( m_pTableControl->getAnchor() );
2809cdf0e10cSrcweir 				    m_pTableControl->setAnchor( m_pTableControl->getAnchor() + 1 );
2810cdf0e10cSrcweir 				    diff++;
2811cdf0e10cSrcweir 			    }
2812cdf0e10cSrcweir 			    m_pTableControl->setAnchor( m_pTableControl->getAnchor() - 1 );
2813cdf0e10cSrcweir 		    }
2814cdf0e10cSrcweir 		    m_pTableControl->invalidateSelectedRegion( m_pTableControl->getCurRow(), newRow );
2815cdf0e10cSrcweir 		    bHandled = sal_True;
2816cdf0e10cSrcweir 	    }
2817cdf0e10cSrcweir 	    //no region selected
2818cdf0e10cSrcweir 	    else
2819cdf0e10cSrcweir 	    {
2820cdf0e10cSrcweir 		    if ( !m_pTableControl->hasRowSelection() )
2821cdf0e10cSrcweir                 m_pTableControl->markRowAsSelected( newRow );
2822cdf0e10cSrcweir 		    else
2823cdf0e10cSrcweir 		    {
2824cdf0e10cSrcweir 			    if ( m_pTableControl->getSelEngine()->GetSelectionMode() == SINGLE_SELECTION )
2825cdf0e10cSrcweir 			    {
2826cdf0e10cSrcweir 				    DeselectAll();
2827cdf0e10cSrcweir                     m_pTableControl->markRowAsSelected( newRow );
2828cdf0e10cSrcweir 			    }
2829cdf0e10cSrcweir 			    else
2830cdf0e10cSrcweir 			    {
2831cdf0e10cSrcweir                     m_pTableControl->markRowAsSelected( newRow );
2832cdf0e10cSrcweir 			    }
2833cdf0e10cSrcweir 		    }
2834cdf0e10cSrcweir 		    if ( m_pTableControl->getSelectedRowCount() > 1 && m_pTableControl->getSelEngine()->GetSelectionMode() != SINGLE_SELECTION )
2835cdf0e10cSrcweir 			    m_pTableControl->getSelEngine()->AddAlways(sal_True);
2836cdf0e10cSrcweir 
2837cdf0e10cSrcweir 		    m_pTableControl->invalidateRow( newRow );
2838cdf0e10cSrcweir 		    bHandled = sal_True;
2839cdf0e10cSrcweir 	    }
2840cdf0e10cSrcweir 	    m_pTableControl->goTo( newCol, newRow );
2841cdf0e10cSrcweir 	    return bHandled;
2842cdf0e10cSrcweir     }
2843cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2844cdf0e10cSrcweir     sal_Bool TableFunctionSet::IsSelectionAtPoint( const Point& rPoint )
2845cdf0e10cSrcweir     {
2846cdf0e10cSrcweir 	    m_pTableControl->getSelEngine()->AddAlways(sal_False);
2847cdf0e10cSrcweir 	    if ( !m_pTableControl->hasRowSelection() )
2848cdf0e10cSrcweir 		    return sal_False;
2849cdf0e10cSrcweir 	    else
2850cdf0e10cSrcweir 	    {
2851cdf0e10cSrcweir 		    RowPos curRow = m_pTableControl->getRowAtPoint( rPoint );
2852cdf0e10cSrcweir 		    m_pTableControl->setAnchor( ROW_INVALID );
2853cdf0e10cSrcweir 		    bool selected = m_pTableControl->isRowSelected( curRow );
2854cdf0e10cSrcweir 		    m_nCurrentRow = curRow;
2855cdf0e10cSrcweir 		    return selected;
2856cdf0e10cSrcweir 	    }
2857cdf0e10cSrcweir     }
2858cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2859cdf0e10cSrcweir     void TableFunctionSet::DeselectAtPoint( const Point& rPoint )
2860cdf0e10cSrcweir     {
2861cdf0e10cSrcweir 	    (void)rPoint;
2862cdf0e10cSrcweir 	    m_pTableControl->invalidateRow( m_nCurrentRow );
2863cdf0e10cSrcweir 	    m_pTableControl->markRowAsDeselected( m_nCurrentRow );
2864cdf0e10cSrcweir     }
2865cdf0e10cSrcweir 
2866cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
2867cdf0e10cSrcweir     void TableFunctionSet::DeselectAll()
2868cdf0e10cSrcweir     {
2869cdf0e10cSrcweir 	    if ( m_pTableControl->hasRowSelection() )
2870cdf0e10cSrcweir 	    {
2871cdf0e10cSrcweir             for ( size_t i=0; i<m_pTableControl->getSelectedRowCount(); ++i )
2872cdf0e10cSrcweir 		    {
2873cdf0e10cSrcweir                 RowPos const rowIndex = m_pTableControl->getSelectedRowIndex(i);
2874cdf0e10cSrcweir 			    m_pTableControl->invalidateRow( rowIndex );
2875cdf0e10cSrcweir 		    }
2876cdf0e10cSrcweir 
2877cdf0e10cSrcweir 		    m_pTableControl->markAllRowsAsDeselected();
2878cdf0e10cSrcweir 	    }
2879cdf0e10cSrcweir     }
2880cdf0e10cSrcweir 
2881cdf0e10cSrcweir //......................................................................................................................
2882cdf0e10cSrcweir } } // namespace svt::table
2883cdf0e10cSrcweir //......................................................................................................................
2884