update credits
[LibreOffice.git] / svtools / source / table / tablecontrol.cxx
blob877f32d12e3f4c7f96221448a0290ff8ac1d36a3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "svtools/table/tablecontrol.hxx"
22 #include "tablegeometry.hxx"
23 #include "tablecontrol_impl.hxx"
24 #include "tabledatawindow.hxx"
26 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <tools/diagnose_ex.h>
32 using namespace ::com::sun::star::uno;
33 using ::com::sun::star::accessibility::XAccessible;
34 using namespace ::com::sun::star::accessibility;
35 using namespace ::com::sun::star::lang;
36 using namespace utl;
37 //......................................................................................................................
38 namespace svt { namespace table
40 //......................................................................................................................
42 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
44 //==================================================================================================================
45 //= TableControl
46 //==================================================================================================================
47 // -----------------------------------------------------------------------------------------------------------------
48 TableControl::TableControl( Window* _pParent, WinBits _nStyle )
49 :Control( _pParent, _nStyle )
50 ,m_pImpl( new TableControl_Impl( *this ) )
52 TableDataWindow& rDataWindow = m_pImpl->getDataWindow();
53 rDataWindow.SetSelectHdl( LINK( this, TableControl, ImplSelectHdl ) );
55 // by default, use the background as determined by the style settings
56 const Color aWindowColor( GetSettings().GetStyleSettings().GetFieldColor() );
57 SetBackground( Wallpaper( aWindowColor ) );
58 SetFillColor( aWindowColor );
60 SetCompoundControl( true );
63 // -----------------------------------------------------------------------------------------------------------------
64 TableControl::~TableControl()
66 ImplCallEventListeners( VCLEVENT_OBJECT_DYING );
68 m_pImpl->setModel( PTableModel() );
69 m_pImpl->disposeAccessible();
70 m_pImpl.reset();
73 // -----------------------------------------------------------------------------------------------------------------
74 void TableControl::GetFocus()
76 if ( !m_pImpl->getInputHandler()->GetFocus( *m_pImpl ) )
77 Control::GetFocus();
80 // -----------------------------------------------------------------------------------------------------------------
81 void TableControl::LoseFocus()
83 if ( !m_pImpl->getInputHandler()->LoseFocus( *m_pImpl ) )
84 Control::LoseFocus();
87 // -----------------------------------------------------------------------------------------------------------------
88 void TableControl::KeyInput( const KeyEvent& rKEvt )
90 if ( !m_pImpl->getInputHandler()->KeyInput( *m_pImpl, rKEvt ) )
91 Control::KeyInput( rKEvt );
92 else
94 if ( m_pImpl->isAccessibleAlive() )
96 m_pImpl->commitCellEvent( AccessibleEventId::STATE_CHANGED,
97 makeAny( AccessibleStateType::FOCUSED ),
98 Any()
100 // Huh? What the heck? Why do we unconditionally notify a STATE_CHANGE/FOCUSED after each and every
101 // (handled) key stroke?
103 m_pImpl->commitTableEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
104 Any(),
105 Any()
107 // ditto: Why do we notify this unconditionally? We should find the right place to notify the
108 // ACTIVE_DESCENDANT_CHANGED event.
109 // Also, we should check if STATE_CHANGED/FOCUSED is really necessary: finally, the children are
110 // transient, aren't they?
116 // -----------------------------------------------------------------------------------------------------------------
117 void TableControl::StateChanged( StateChangedType i_nStateChange )
119 Control::StateChanged( i_nStateChange );
121 // forward certain settings to the data window
122 switch ( i_nStateChange )
124 case STATE_CHANGE_CONTROL_FOCUS:
125 m_pImpl->invalidateSelectedRows();
126 break;
128 case STATE_CHANGE_CONTROLBACKGROUND:
129 if ( IsControlBackground() )
130 getDataWindow().SetControlBackground( GetControlBackground() );
131 else
132 getDataWindow().SetControlBackground();
133 break;
135 case STATE_CHANGE_CONTROLFOREGROUND:
136 if ( IsControlForeground() )
137 getDataWindow().SetControlForeground( GetControlForeground() );
138 else
139 getDataWindow().SetControlForeground();
140 break;
142 case STATE_CHANGE_CONTROLFONT:
143 if ( IsControlFont() )
144 getDataWindow().SetControlFont( GetControlFont() );
145 else
146 getDataWindow().SetControlFont();
147 break;
151 // -----------------------------------------------------------------------------------------------------------------
152 void TableControl::Resize()
154 Control::Resize();
155 m_pImpl->onResize();
158 // -----------------------------------------------------------------------------------------------------------------
159 void TableControl::SetModel( PTableModel _pModel )
161 m_pImpl->setModel( _pModel );
164 // -----------------------------------------------------------------------------------------------------------------
165 PTableModel TableControl::GetModel() const
167 return m_pImpl->getModel();
170 // -----------------------------------------------------------------------------------------------------------------
171 sal_Int32 TableControl::GetCurrentRow() const
173 return m_pImpl->getCurrentRow();
176 // -----------------------------------------------------------------------------------------------------------------
177 sal_Int32 TableControl::GetCurrentColumn() const
179 return m_pImpl->getCurrentColumn();
182 // -----------------------------------------------------------------------------------------------------------------
183 bool TableControl::GoTo( ColPos _nColumn, RowPos _nRow )
185 return m_pImpl->goTo( _nColumn, _nRow );
188 // -----------------------------------------------------------------------------------------------------------------
189 sal_Bool TableControl::GoToCell(sal_Int32 _nColPos, sal_Int32 _nRowPos)
191 return m_pImpl->goTo( _nColPos, _nRowPos );
194 //------------------------------------------------------------------------------------------------------------------
195 sal_Int32 TableControl::GetSelectedRowCount() const
197 return sal_Int32( m_pImpl->getSelectedRowCount() );
200 //------------------------------------------------------------------------------------------------------------------
201 sal_Int32 TableControl::GetSelectedRowIndex( sal_Int32 const i_selectionIndex ) const
203 return sal_Int32( m_pImpl->getSelectedRowIndex( i_selectionIndex ) );
206 //------------------------------------------------------------------------------------------------------------------
207 bool TableControl::IsRowSelected( sal_Int32 const i_rowIndex ) const
209 return m_pImpl->isRowSelected( i_rowIndex );
212 // -----------------------------------------------------------------------------------------------------------------
213 void TableControl::SelectRow( RowPos const i_rowIndex, bool const i_select )
215 ENSURE_OR_RETURN_VOID( ( i_rowIndex >= 0 ) && ( i_rowIndex < m_pImpl->getModel()->getRowCount() ),
216 "TableControl::SelectRow: invalid row index!" );
218 if ( i_select )
220 if ( !m_pImpl->markRowAsSelected( i_rowIndex ) )
221 // nothing to do
222 return;
224 else
226 m_pImpl->markRowAsDeselected( i_rowIndex );
229 m_pImpl->invalidateRowRange( i_rowIndex, i_rowIndex );
230 Select();
233 // -----------------------------------------------------------------------------------------------------------------
234 void TableControl::SelectAllRows( bool const i_select )
236 if ( i_select )
238 if ( !m_pImpl->markAllRowsAsSelected() )
239 // nothing to do
240 return;
242 else
244 if ( !m_pImpl->markAllRowsAsDeselected() )
245 // nothing to do
246 return;
250 Invalidate();
251 // TODO: can't we do better than this, and invalidate only the rows which changed?
252 Select();
255 // -----------------------------------------------------------------------------------------------------------------
256 ITableControl& TableControl::getTableControlInterface()
258 return *m_pImpl;
261 // -----------------------------------------------------------------------------------------------------------------
262 SelectionEngine* TableControl::getSelEngine()
264 return m_pImpl->getSelEngine();
267 // -----------------------------------------------------------------------------------------------------------------
268 Window& TableControl::getDataWindow()
270 return m_pImpl->getDataWindow();
273 // -----------------------------------------------------------------------------------------------------------------
274 Reference< XAccessible > TableControl::CreateAccessible()
276 Window* pParent = GetAccessibleParentWindow();
277 ENSURE_OR_RETURN( pParent, "TableControl::CreateAccessible - parent not found", NULL );
279 return m_pImpl->getAccessible( *pParent );
282 // -----------------------------------------------------------------------------------------------------------------
283 Reference<XAccessible> TableControl::CreateAccessibleControl( sal_Int32 _nIndex )
285 (void)_nIndex;
286 DBG_ASSERT( sal_False, "TableControl::CreateAccessibleControl: to be overwritten!" );
287 return NULL;
290 // -----------------------------------------------------------------------------------------------------------------
291 OUString TableControl::GetAccessibleObjectName( AccessibleTableControlObjType eObjType, sal_Int32 _nRow, sal_Int32 _nCol) const
293 OUString aRetText;
294 //Window* pWin;
295 switch( eObjType )
297 case TCTYPE_GRIDCONTROL:
298 aRetText = OUString( "Grid control" );
299 break;
300 case TCTYPE_TABLE:
301 aRetText = OUString( "Grid conrol" );
302 break;
303 case TCTYPE_ROWHEADERBAR:
304 aRetText = OUString( "RowHeaderBar" );
305 break;
306 case TCTYPE_COLUMNHEADERBAR:
307 aRetText = OUString( "ColumnHeaderBar" );
308 break;
309 case TCTYPE_TABLECELL:
310 //the name of the cell constists of column name and row name if defined
311 //if the name is equal to cell content, it'll be read twice
312 if(GetModel()->hasColumnHeaders())
314 aRetText = GetColumnName(_nCol);
315 aRetText += OUString::createFromAscii(" , ");
317 if(GetModel()->hasRowHeaders())
319 aRetText += GetRowName(_nRow);
320 aRetText += OUString::createFromAscii(" , ");
322 //aRetText = GetAccessibleCellText(_nRow, _nCol);
323 break;
324 case TCTYPE_ROWHEADERCELL:
325 aRetText = GetRowName(_nRow);
326 break;
327 case TCTYPE_COLUMNHEADERCELL:
328 aRetText = GetColumnName(_nCol);
329 break;
330 default:
331 OSL_FAIL("GridControl::GetAccessibleName: invalid enum!");
333 return aRetText;
336 //------------------------------------------------------------------------------------------------------------------
337 OUString TableControl::GetAccessibleObjectDescription( AccessibleTableControlObjType eObjType, sal_Int32 ) const
339 OUString aRetText;
340 switch( eObjType )
342 case TCTYPE_GRIDCONTROL:
343 aRetText = OUString( "Grid control description" );
344 break;
345 case TCTYPE_TABLE:
346 aRetText = OUString( "TABLE description" );
347 break;
348 case TCTYPE_ROWHEADERBAR:
349 aRetText = OUString( "ROWHEADERBAR description" );
350 break;
351 case TCTYPE_COLUMNHEADERBAR:
352 aRetText = OUString( "COLUMNHEADERBAR description" );
353 break;
354 case TCTYPE_TABLECELL:
355 // the description of the cell consists of column name and row name if defined
356 // if the name is equal to cell content, it'll be read twice
357 if ( GetModel()->hasColumnHeaders() )
359 aRetText = GetColumnName( GetCurrentColumn() );
360 aRetText += OUString::createFromAscii( " , " );
362 if ( GetModel()->hasRowHeaders() )
364 aRetText += GetRowName( GetCurrentRow() );
366 break;
367 case TCTYPE_ROWHEADERCELL:
368 aRetText = OUString( "ROWHEADERCELL description" );
369 break;
370 case TCTYPE_COLUMNHEADERCELL:
371 aRetText = OUString( "COLUMNHEADERCELL description" );
372 break;
374 return aRetText;
377 //------------------------------------------------------------------------------------------------------------------
378 OUString TableControl::GetRowDescription( sal_Int32 _nRow) const
380 (void)_nRow;
381 return OUString( "row description" );
384 //------------------------------------------------------------------------------------------------------------------
385 OUString TableControl::GetRowName( sal_Int32 _nIndex) const
387 OUString sRowName;
388 GetModel()->getRowHeading( _nIndex ) >>= sRowName;
389 return sRowName;
392 //------------------------------------------------------------------------------------------------------------------
393 OUString TableControl::GetColumnDescription( sal_uInt16 _nColumn) const
395 (void)_nColumn;
396 return OUString( "col description" );
399 //------------------------------------------------------------------------------------------------------------------
400 OUString TableControl::GetColumnName( sal_Int32 _nIndex) const
402 return GetModel()->getColumnModel(_nIndex)->getName();
405 //------------------------------------------------------------------------------------------------------------------
406 ::com::sun::star::uno::Any TableControl::GetCellContent( sal_Int32 _nRowPos, sal_Int32 _nColPos ) const
408 Any aCellContent;
409 GetModel()->getCellContent( _nColPos, _nRowPos, aCellContent );
410 return aCellContent;
413 //------------------------------------------------------------------------------------------------------------------
414 OUString TableControl::GetAccessibleCellText( sal_Int32 _nRowPos, sal_Int32 _nColPos) const
416 return m_pImpl->getCellContentAsString( _nRowPos, _nColPos );
419 //------------------------------------------------------------------------------------------------------------------
420 void TableControl::FillAccessibleStateSet(
421 ::utl::AccessibleStateSetHelper& rStateSet,
422 AccessibleTableControlObjType eObjType ) const
424 switch( eObjType )
426 case TCTYPE_GRIDCONTROL:
427 case TCTYPE_TABLE:
429 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
431 if ( m_pImpl->getSelEngine()->GetSelectionMode() == MULTIPLE_SELECTION )
432 rStateSet.AddState( AccessibleStateType::MULTI_SELECTABLE);
434 if ( HasChildPathFocus() )
435 rStateSet.AddState( AccessibleStateType::FOCUSED );
437 if ( IsActive() )
438 rStateSet.AddState( AccessibleStateType::ACTIVE );
440 if ( m_pImpl->getDataWindow().IsEnabled() )
442 rStateSet.AddState( AccessibleStateType::ENABLED );
443 rStateSet.AddState( AccessibleStateType::SENSITIVE );
446 if ( IsReallyVisible() )
447 rStateSet.AddState( AccessibleStateType::VISIBLE );
449 if ( eObjType == TCTYPE_TABLE )
450 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
451 break;
453 case TCTYPE_ROWHEADERBAR:
454 rStateSet.AddState( AccessibleStateType::VISIBLE );
455 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
456 break;
458 case TCTYPE_COLUMNHEADERBAR:
459 rStateSet.AddState( AccessibleStateType::VISIBLE );
460 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
461 break;
463 case TCTYPE_TABLECELL:
465 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
466 if ( HasChildPathFocus() )
467 rStateSet.AddState( AccessibleStateType::FOCUSED );
468 rStateSet.AddState( AccessibleStateType::ACTIVE );
469 rStateSet.AddState( AccessibleStateType::TRANSIENT );
470 rStateSet.AddState( AccessibleStateType::SELECTABLE);
471 rStateSet.AddState( AccessibleStateType::VISIBLE );
472 rStateSet.AddState( AccessibleStateType::SHOWING );
473 if ( IsRowSelected( GetCurrentRow() ) )
474 // Hmm? Wouldn't we expect the affected row to be a parameter to this function?
475 rStateSet.AddState( AccessibleStateType::SELECTED );
477 break;
479 case TCTYPE_ROWHEADERCELL:
480 rStateSet.AddState( AccessibleStateType::VISIBLE );
481 rStateSet.AddState( AccessibleStateType::TRANSIENT );
482 break;
484 case TCTYPE_COLUMNHEADERCELL:
485 rStateSet.AddState( AccessibleStateType::VISIBLE );
486 break;
490 //------------------------------------------------------------------------------------------------------------------
491 void TableControl::commitCellEventIfAccessibleAlive( sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue )
493 if ( m_pImpl->isAccessibleAlive() )
494 m_pImpl->commitCellEvent( i_eventID, i_newValue, i_oldValue );
497 //------------------------------------------------------------------------------------------------------------------
498 void TableControl::commitTableEventIfAccessibleAlive( sal_Int16 const i_eventID, const Any& i_newValue, const Any& i_oldValue )
500 if ( m_pImpl->isAccessibleAlive() )
501 m_pImpl->commitTableEvent( i_eventID, i_newValue, i_oldValue );
504 //------------------------------------------------------------------------------------------------------------------
505 Rectangle TableControl::GetWindowExtentsRelative( Window *pRelativeWindow ) const
507 return Control::GetWindowExtentsRelative( pRelativeWindow );
510 //------------------------------------------------------------------------------------------------------------------
511 void TableControl::GrabFocus()
513 Control::GrabFocus();
516 //------------------------------------------------------------------------------------------------------------------
517 Reference< XAccessible > TableControl::GetAccessible( sal_Bool bCreate )
519 return Control::GetAccessible( bCreate );
522 //------------------------------------------------------------------------------------------------------------------
523 Window* TableControl::GetAccessibleParentWindow() const
525 return Control::GetAccessibleParentWindow();
528 //------------------------------------------------------------------------------------------------------------------
529 Window* TableControl::GetWindowInstance()
531 return this;
534 //------------------------------------------------------------------------------------------------------------------
535 sal_Bool TableControl::HasRowHeader()
537 return GetModel()->hasRowHeaders();
540 //------------------------------------------------------------------------------------------------------------------
541 sal_Bool TableControl::HasColHeader()
543 return GetModel()->hasColumnHeaders();
546 //------------------------------------------------------------------------------------------------------------------
547 sal_Int32 TableControl::GetAccessibleControlCount() const
549 // TC_TABLE is always defined, no matter whether empty or not
550 sal_Int32 count = 1;
551 if ( GetModel()->hasRowHeaders() )
552 ++count;
553 if ( GetModel()->hasColumnHeaders() )
554 ++count;
555 return count;
558 //------------------------------------------------------------------------------------------------------------------
559 sal_Bool TableControl::ConvertPointToControlIndex( sal_Int32& _rnIndex, const Point& _rPoint )
561 sal_Int32 nRow = m_pImpl->getRowAtPoint( _rPoint );
562 sal_Int32 nCol = m_pImpl->getColAtPoint( _rPoint );
563 _rnIndex = nRow * GetColumnCount() + nCol;
564 return nRow >= 0 ? sal_True : sal_False;
567 //------------------------------------------------------------------------------------------------------------------
568 long TableControl::GetRowCount() const
570 return GetModel()->getRowCount();
573 //------------------------------------------------------------------------------------------------------------------
574 long TableControl::GetColumnCount() const
576 return GetModel()->getColumnCount();
579 //------------------------------------------------------------------------------------------------------------------
580 sal_Bool TableControl::HasRowHeader() const
582 return GetModel()->hasRowHeaders();
585 //------------------------------------------------------------------------------------------------------------------
586 sal_Bool TableControl::ConvertPointToCellAddress( sal_Int32& _rnRow, sal_Int32& _rnColPos, const Point& _rPoint )
588 _rnRow = m_pImpl->getRowAtPoint( _rPoint );
589 _rnColPos = m_pImpl->getColAtPoint( _rPoint );
590 return _rnRow >= 0 ? sal_True : sal_False;
593 //------------------------------------------------------------------------------------------------------------------
594 void TableControl::FillAccessibleStateSetForCell( ::utl::AccessibleStateSetHelper& _rStateSet, sal_Int32 _nRow, sal_uInt16 _nColumnPos ) const
596 if ( IsRowSelected( _nRow ) )
597 _rStateSet.AddState( AccessibleStateType::SELECTED );
598 if ( HasChildPathFocus() )
599 _rStateSet.AddState( AccessibleStateType::FOCUSED );
600 else // only transient when column is not focused
601 _rStateSet.AddState( AccessibleStateType::TRANSIENT );
603 _rStateSet.AddState( AccessibleStateType::VISIBLE );
604 _rStateSet.AddState( AccessibleStateType::SHOWING );
605 _rStateSet.AddState( AccessibleStateType::ENABLED );
606 _rStateSet.AddState( AccessibleStateType::SENSITIVE );
607 _rStateSet.AddState( AccessibleStateType::ACTIVE );
609 (void)_nColumnPos;
612 //------------------------------------------------------------------------------------------------------------------
613 Rectangle TableControl::GetFieldCharacterBounds(sal_Int32 _nRow,sal_Int32 _nColumnPos,sal_Int32 nIndex)
615 (void)_nRow;
616 (void)_nColumnPos;
617 return GetCharacterBounds(nIndex);
620 //------------------------------------------------------------------------------------------------------------------
621 sal_Int32 TableControl::GetFieldIndexAtPoint(sal_Int32 _nRow,sal_Int32 _nColumnPos,const Point& _rPoint)
623 (void)_nRow;
624 (void)_nColumnPos;
625 return GetIndexForPoint(_rPoint);
628 //------------------------------------------------------------------------------------------------------------------
629 Rectangle TableControl::calcHeaderRect(sal_Bool _bIsColumnBar,sal_Bool _bOnScreen)
631 (void)_bOnScreen;
632 return m_pImpl->calcHeaderRect( _bIsColumnBar ? false : true );
635 //------------------------------------------------------------------------------------------------------------------
636 Rectangle TableControl::calcHeaderCellRect( sal_Bool _bIsColumnBar, sal_Int32 nPos )
638 return m_pImpl->calcHeaderCellRect( _bIsColumnBar, nPos );
641 //------------------------------------------------------------------------------------------------------------------
642 Rectangle TableControl::calcTableRect(sal_Bool _bOnScreen)
644 (void)_bOnScreen;
645 return m_pImpl->calcTableRect();
648 //------------------------------------------------------------------------------------------------------------------
649 Rectangle TableControl::calcCellRect( sal_Int32 _nRowPos, sal_Int32 _nColPos )
651 return m_pImpl->calcCellRect( _nRowPos, _nColPos );
654 //------------------------------------------------------------------------------------------------------------------
655 IMPL_LINK_NOARG(TableControl, ImplSelectHdl)
657 Select();
658 return 1;
661 //------------------------------------------------------------------------------------------------------------------
662 void TableControl::Select()
664 ImplCallEventListenersAndHandler( VCLEVENT_TABLEROW_SELECT, m_pImpl->getSelectHandler(), this );
666 if ( m_pImpl->isAccessibleAlive() )
668 m_pImpl->commitAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
670 m_pImpl->commitTableEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, Any(), Any() );
671 // TODO: why do we notify this when the *selection* changed? Shouldn't we find a better place for this,
672 // actually, when the active descendant, i.e. the current cell, *really* changed?
676 }} // namespace svt::table
678 //......................................................................................................................
679 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */