Bump version to 5.0-14
[LibreOffice.git] / svtools / source / uno / svtxgridcontrol.cxx
blobb1fb0a2e0cbdd4274101a9aaae837a94911150b1
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 "svtxgridcontrol.hxx"
21 #include <com/sun/star/view/SelectionType.hpp>
22 #include "table/tablecontrolinterface.hxx"
23 #include "table/gridtablerenderer.hxx"
24 #include "table/tablecontrol.hxx"
25 #include "unocontroltablemodel.hxx"
26 #include <comphelper/sequence.hxx>
27 #include <rtl/ref.hxx>
28 #include <tools/diagnose_ex.h>
29 #include <toolkit/helper/property.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/awt/grid/XGridColumn.hpp>
33 #include <com/sun/star/awt/XControl.hpp>
34 #include <com/sun/star/awt/grid/GridInvalidDataException.hpp>
35 #include <com/sun/star/awt/grid/GridInvalidModelException.hpp>
36 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
37 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
38 #include <com/sun/star/util/Color.hpp>
39 #include <com/sun/star/awt/FontDescriptor.hpp>
41 #include <vcl/svapp.hxx>
43 using ::com::sun::star::uno::RuntimeException;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::UNO_QUERY;
47 using ::com::sun::star::uno::UNO_QUERY_THROW;
48 using ::com::sun::star::uno::Any;
49 using ::com::sun::star::uno::makeAny;
50 using ::com::sun::star::uno::Sequence;
51 using ::com::sun::star::awt::grid::XGridSelectionListener;
52 using ::com::sun::star::style::VerticalAlignment;
53 using ::com::sun::star::style::VerticalAlignment_TOP;
54 using ::com::sun::star::view::SelectionType;
55 using ::com::sun::star::view::SelectionType_NONE;
56 using ::com::sun::star::view::SelectionType_RANGE;
57 using ::com::sun::star::view::SelectionType_SINGLE;
58 using ::com::sun::star::view::SelectionType_MULTI;
59 using ::com::sun::star::awt::grid::XGridDataModel;
60 using ::com::sun::star::awt::grid::GridInvalidDataException;
61 using ::com::sun::star::lang::EventObject;
62 using ::com::sun::star::lang::IndexOutOfBoundsException;
63 using ::com::sun::star::awt::grid::XGridColumnModel;
64 using ::com::sun::star::awt::grid::GridSelectionEvent;
65 using ::com::sun::star::awt::grid::XGridColumn;
66 using ::com::sun::star::container::ContainerEvent;
67 using ::com::sun::star::awt::grid::GridDataEvent;
68 using ::com::sun::star::awt::grid::GridInvalidModelException;
69 using ::com::sun::star::util::VetoException;
71 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
72 namespace AccessibleStateType = ::com::sun::star::accessibility::AccessibleStateType;
74 using namespace ::svt::table;
76 typedef ::com::sun::star::util::Color UnoColor;
79 SVTXGridControl::SVTXGridControl()
80 :m_xTableModel( new UnoControlTableModel() )
81 ,m_bTableModelInitCompleted( false )
82 ,m_aSelectionListeners( *this )
87 SVTXGridControl::~SVTXGridControl()
92 void SVTXGridControl::SetWindow( const VclPtr< vcl::Window > &pWindow )
94 SVTXGridControl_Base::SetWindow( pWindow );
95 impl_checkTableModelInit();
99 void SVTXGridControl::impl_checkColumnIndex_throw( ::svt::table::TableControl const & i_table, sal_Int32 const i_columnIndex ) const
101 if ( ( i_columnIndex < 0 ) || ( i_columnIndex >= i_table.GetColumnCount() ) )
102 throw IndexOutOfBoundsException( OUString(), *const_cast< SVTXGridControl* >( this ) );
106 void SVTXGridControl::impl_checkRowIndex_throw( ::svt::table::TableControl const & i_table, sal_Int32 const i_rowIndex ) const
108 if ( ( i_rowIndex < 0 ) || ( i_rowIndex >= i_table.GetRowCount() ) )
109 throw IndexOutOfBoundsException( OUString(), *const_cast< SVTXGridControl* >( this ) );
113 sal_Int32 SAL_CALL SVTXGridControl::getRowAtPoint(::sal_Int32 x, ::sal_Int32 y) throw (RuntimeException, std::exception)
115 SolarMutexGuard aGuard;
117 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
118 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getRowAtPoint: no control (anymore)!", -1 );
120 TableCell const tableCell = pTable->getTableControlInterface().hitTest( Point( x, y ) );
121 return ( tableCell.nRow >= 0 ) ? tableCell.nRow : -1;
125 sal_Int32 SAL_CALL SVTXGridControl::getColumnAtPoint(::sal_Int32 x, ::sal_Int32 y) throw (RuntimeException, std::exception)
127 SolarMutexGuard aGuard;
129 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
130 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getColumnAtPoint: no control (anymore)!", -1 );
132 TableCell const tableCell = pTable->getTableControlInterface().hitTest( Point( x, y ) );
133 return ( tableCell.nColumn >= 0 ) ? tableCell.nColumn : -1;
137 sal_Int32 SAL_CALL SVTXGridControl::getCurrentColumn( ) throw (RuntimeException, std::exception)
139 SolarMutexGuard aGuard;
141 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
142 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getCurrentColumn: no control (anymore)!", -1 );
144 sal_Int32 const nColumn = pTable->GetCurrentColumn();
145 return ( nColumn >= 0 ) ? nColumn : -1;
149 sal_Int32 SAL_CALL SVTXGridControl::getCurrentRow( ) throw (RuntimeException, std::exception)
151 SolarMutexGuard aGuard;
153 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
154 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getCurrentRow: no control (anymore)!", -1 );
156 sal_Int32 const nRow = pTable->GetCurrentRow();
157 return ( nRow >= 0 ) ? nRow : -1;
161 void SAL_CALL SVTXGridControl::goToCell( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, VetoException, std::exception)
163 SolarMutexGuard aGuard;
165 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
166 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::getCurrentRow: no control (anymore)!" );
168 impl_checkColumnIndex_throw( *pTable, i_columnIndex );
169 impl_checkRowIndex_throw( *pTable, i_rowIndex );
171 pTable->GoTo( i_columnIndex, i_rowIndex );
175 void SAL_CALL SVTXGridControl::addSelectionListener(const Reference< XGridSelectionListener > & listener) throw (RuntimeException, std::exception)
177 m_aSelectionListeners.addInterface(listener);
181 void SAL_CALL SVTXGridControl::removeSelectionListener(const Reference< XGridSelectionListener > & listener) throw (RuntimeException, std::exception)
183 m_aSelectionListeners.removeInterface(listener);
187 void SVTXGridControl::setProperty( const OUString& PropertyName, const Any& aValue) throw(RuntimeException, std::exception)
189 SolarMutexGuard aGuard;
191 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
192 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::setProperty: no control (anymore)!" );
194 switch( GetPropertyId( PropertyName ) )
196 case BASEPROPERTY_ROW_HEADER_WIDTH:
198 sal_Int32 rowHeaderWidth( -1 );
199 aValue >>= rowHeaderWidth;
200 if ( rowHeaderWidth <= 0 )
202 SAL_WARN( "svtools.uno", "SVTXGridControl::setProperty: illegal row header width!" );
203 break;
206 m_xTableModel->setRowHeaderWidth( rowHeaderWidth );
207 // TODO: the model should broadcast this change itself, and the table should invalidate itself as needed
208 pTable->Invalidate();
210 break;
212 case BASEPROPERTY_COLUMN_HEADER_HEIGHT:
214 sal_Int32 columnHeaderHeight = 0;
215 if ( !aValue.hasValue() )
217 columnHeaderHeight = pTable->PixelToLogic( Size( 0, pTable->GetTextHeight() + 3 ), MAP_APPFONT ).Height();
219 else
221 aValue >>= columnHeaderHeight;
223 if ( columnHeaderHeight <= 0 )
225 SAL_WARN( "svtools.uno", "SVTXGridControl::setProperty: illegal column header width!" );
226 break;
229 m_xTableModel->setColumnHeaderHeight( columnHeaderHeight );
230 // TODO: the model should broadcast this change itself, and the table should invalidate itself as needed
231 pTable->Invalidate();
233 break;
235 case BASEPROPERTY_USE_GRID_LINES:
237 GridTableRenderer* pGridRenderer = dynamic_cast< GridTableRenderer* >(
238 m_xTableModel->getRenderer().get() );
239 if ( !pGridRenderer )
241 SAL_WARN( "svtools.uno", "SVTXGridControl::setProperty(UseGridLines): invalid renderer!" );
242 break;
245 bool bUseGridLines = false;
246 OSL_VERIFY( aValue >>= bUseGridLines );
247 pGridRenderer->useGridLines( bUseGridLines );
248 pTable->Invalidate();
250 break;
252 case BASEPROPERTY_ROW_HEIGHT:
254 sal_Int32 rowHeight = 0;
255 if ( !aValue.hasValue() )
257 rowHeight = pTable->PixelToLogic( Size( 0, pTable->GetTextHeight() + 3 ), MAP_APPFONT ).Height();
259 else
261 aValue >>= rowHeight;
263 m_xTableModel->setRowHeight( rowHeight );
264 if ( rowHeight <= 0 )
266 SAL_WARN( "svtools.uno", "SVTXGridControl::setProperty: illegal row height!" );
267 break;
270 // TODO: the model should broadcast this change itself, and the table should invalidate itself as needed
271 pTable->Invalidate();
273 break;
275 case BASEPROPERTY_BACKGROUNDCOLOR:
277 // let the base class handle this for the TableControl
278 VCLXWindow::setProperty( PropertyName, aValue );
279 // and forward to the grid control's data window
280 if ( pTable->IsBackground() )
281 pTable->getDataWindow().SetBackground( pTable->GetBackground() );
282 else
283 pTable->getDataWindow().SetBackground();
285 break;
287 case BASEPROPERTY_GRID_SELECTIONMODE:
289 SelectionType eSelectionType;
290 if( aValue >>= eSelectionType )
292 SelectionMode eSelMode;
293 switch( eSelectionType )
295 case SelectionType_SINGLE: eSelMode = SINGLE_SELECTION; break;
296 case SelectionType_RANGE: eSelMode = RANGE_SELECTION; break;
297 case SelectionType_MULTI: eSelMode = MULTIPLE_SELECTION; break;
298 default: eSelMode = NO_SELECTION; break;
300 if( pTable->getSelEngine()->GetSelectionMode() != eSelMode )
301 pTable->getSelEngine()->SetSelectionMode( eSelMode );
303 break;
305 case BASEPROPERTY_HSCROLL:
307 bool bHScroll = true;
308 if( aValue >>= bHScroll )
309 m_xTableModel->setHorizontalScrollbarVisibility( bHScroll ? ScrollbarShowAlways : ScrollbarShowSmart );
310 break;
313 case BASEPROPERTY_VSCROLL:
315 bool bVScroll = true;
316 if( aValue >>= bVScroll )
318 m_xTableModel->setVerticalScrollbarVisibility( bVScroll ? ScrollbarShowAlways : ScrollbarShowSmart );
320 break;
323 case BASEPROPERTY_GRID_SHOWROWHEADER:
325 bool rowHeader = true;
326 if( aValue >>= rowHeader )
328 m_xTableModel->setRowHeaders(rowHeader);
330 break;
333 case BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS:
334 m_xTableModel->setRowBackgroundColors( aValue );
335 pTable->Invalidate();
336 break;
338 case BASEPROPERTY_GRID_LINE_COLOR:
339 m_xTableModel->setLineColor( aValue );
340 pTable->Invalidate();
341 break;
343 case BASEPROPERTY_GRID_HEADER_BACKGROUND:
344 m_xTableModel->setHeaderBackgroundColor( aValue );
345 pTable->Invalidate();
346 break;
348 case BASEPROPERTY_GRID_HEADER_TEXT_COLOR:
349 m_xTableModel->setHeaderTextColor( aValue );
350 pTable->Invalidate();
351 break;
353 case BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR:
354 m_xTableModel->setActiveSelectionBackColor( aValue );
355 pTable->Invalidate();
356 break;
358 case BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR:
359 m_xTableModel->setInactiveSelectionBackColor( aValue );
360 pTable->Invalidate();
361 break;
363 case BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR:
364 m_xTableModel->setActiveSelectionTextColor( aValue );
365 pTable->Invalidate();
366 break;
368 case BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR:
369 m_xTableModel->setInactiveSelectionTextColor( aValue );
370 pTable->Invalidate();
371 break;
374 case BASEPROPERTY_TEXTCOLOR:
375 m_xTableModel->setTextColor( aValue );
376 pTable->Invalidate();
377 break;
379 case BASEPROPERTY_TEXTLINECOLOR:
380 m_xTableModel->setTextLineColor( aValue );
381 pTable->Invalidate();
382 break;
384 case BASEPROPERTY_VERTICALALIGN:
386 VerticalAlignment eAlign( VerticalAlignment_TOP );
387 if ( aValue >>= eAlign )
388 m_xTableModel->setVerticalAlign( eAlign );
389 break;
392 case BASEPROPERTY_GRID_SHOWCOLUMNHEADER:
394 bool colHeader = true;
395 if( aValue >>= colHeader )
397 m_xTableModel->setColumnHeaders(colHeader);
399 break;
401 case BASEPROPERTY_GRID_DATAMODEL:
403 Reference< XGridDataModel > const xDataModel( aValue, UNO_QUERY );
404 if ( !xDataModel.is() )
405 throw GridInvalidDataException("Invalid data model.", *this );
407 m_xTableModel->setDataModel( xDataModel );
408 impl_checkTableModelInit();
410 break;
412 case BASEPROPERTY_GRID_COLUMNMODEL:
414 // obtain new col model
415 Reference< XGridColumnModel > const xColumnModel( aValue, UNO_QUERY );
416 if ( !xColumnModel.is() )
417 throw GridInvalidModelException("Invalid column model.", *this );
419 // remove all old columns
420 m_xTableModel->removeAllColumns();
422 // announce to the TableModel
423 m_xTableModel->setColumnModel( xColumnModel );
424 impl_checkTableModelInit();
426 // add new columns
427 impl_updateColumnsFromModel_nothrow();
428 break;
430 default:
431 VCLXWindow::setProperty( PropertyName, aValue );
432 break;
437 void SVTXGridControl::impl_checkTableModelInit()
439 if ( !m_bTableModelInitCompleted && m_xTableModel->hasColumnModel() && m_xTableModel->hasDataModel() )
441 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
442 if ( pTable )
444 pTable->SetModel( PTableModel( m_xTableModel ) );
446 m_bTableModelInitCompleted = true;
448 // ensure default columns exist, if they have not previously been added
449 Reference< XGridDataModel > const xDataModel( m_xTableModel->getDataModel(), UNO_QUERY_THROW );
450 Reference< XGridColumnModel > const xColumnModel( m_xTableModel->getColumnModel(), UNO_QUERY_THROW );
452 sal_Int32 const nDataColumnCount = xDataModel->getColumnCount();
453 if ( ( nDataColumnCount > 0 ) && ( xColumnModel->getColumnCount() == 0 ) )
454 xColumnModel->setDefaultColumns( nDataColumnCount );
455 // this will trigger notifications, which in turn will let us update our m_xTableModel
460 namespace
462 void lcl_convertColor( ::boost::optional< ::Color > const & i_color, Any & o_colorValue )
464 if ( !i_color )
465 o_colorValue.clear();
466 else
467 o_colorValue <<= i_color->GetColor();
471 Any SVTXGridControl::getProperty( const OUString& PropertyName ) throw(RuntimeException, std::exception)
473 SolarMutexGuard aGuard;
475 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
476 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getProperty: no control (anymore)!", Any() );
478 Any aPropertyValue;
480 const sal_uInt16 nPropId = GetPropertyId( PropertyName );
481 switch(nPropId)
483 case BASEPROPERTY_GRID_SELECTIONMODE:
485 SelectionType eSelectionType;
487 SelectionMode eSelMode = pTable->getSelEngine()->GetSelectionMode();
488 switch( eSelMode )
490 case SINGLE_SELECTION: eSelectionType = SelectionType_SINGLE; break;
491 case RANGE_SELECTION: eSelectionType = SelectionType_RANGE; break;
492 case MULTIPLE_SELECTION:eSelectionType = SelectionType_MULTI; break;
493 default: eSelectionType = SelectionType_NONE; break;
495 aPropertyValue <<= eSelectionType;
496 break;
499 case BASEPROPERTY_GRID_SHOWROWHEADER:
500 aPropertyValue <<= m_xTableModel->hasRowHeaders();
501 break;
503 case BASEPROPERTY_GRID_SHOWCOLUMNHEADER:
504 aPropertyValue <<= m_xTableModel->hasColumnHeaders();
505 break;
507 case BASEPROPERTY_GRID_DATAMODEL:
508 aPropertyValue <<= m_xTableModel->getDataModel();
509 break;
511 case BASEPROPERTY_GRID_COLUMNMODEL:
512 aPropertyValue <<= m_xTableModel->getColumnModel();
513 break;
515 case BASEPROPERTY_HSCROLL:
517 bool const bHasScrollbar = ( m_xTableModel->getHorizontalScrollbarVisibility() != ScrollbarShowNever );
518 aPropertyValue <<= bHasScrollbar;
519 break;
522 case BASEPROPERTY_VSCROLL:
524 bool const bHasScrollbar = ( m_xTableModel->getVerticalScrollbarVisibility() != ScrollbarShowNever );
525 aPropertyValue <<= bHasScrollbar;
526 break;
529 case BASEPROPERTY_USE_GRID_LINES:
531 GridTableRenderer* pGridRenderer = dynamic_cast< GridTableRenderer* >(
532 m_xTableModel->getRenderer().get() );
533 if ( !pGridRenderer )
535 SAL_WARN( "svtools.uno", "SVTXGridControl::getProperty(UseGridLines): invalid renderer!" );
536 break;
539 aPropertyValue <<= pGridRenderer->useGridLines();
541 break;
543 case BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS:
545 ::boost::optional< ::std::vector< ::Color > > aColors( m_xTableModel->getRowBackgroundColors() );
546 if ( !aColors )
547 aPropertyValue.clear();
548 else
550 Sequence< UnoColor > aAPIColors( aColors->size() );
551 for ( size_t i=0; i<aColors->size(); ++i )
553 aAPIColors[i] = aColors->at(i).GetColor();
555 aPropertyValue <<= aAPIColors;
558 break;
560 case BASEPROPERTY_GRID_LINE_COLOR:
561 lcl_convertColor( m_xTableModel->getLineColor(), aPropertyValue );
562 break;
564 case BASEPROPERTY_GRID_HEADER_BACKGROUND:
565 lcl_convertColor( m_xTableModel->getHeaderBackgroundColor(), aPropertyValue );
566 break;
568 case BASEPROPERTY_GRID_HEADER_TEXT_COLOR:
569 lcl_convertColor( m_xTableModel->getHeaderTextColor(), aPropertyValue );
570 break;
572 case BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR:
573 lcl_convertColor( m_xTableModel->getActiveSelectionBackColor(), aPropertyValue );
574 break;
576 case BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR:
577 lcl_convertColor( m_xTableModel->getInactiveSelectionBackColor(), aPropertyValue );
578 break;
580 case BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR:
581 lcl_convertColor( m_xTableModel->getActiveSelectionTextColor(), aPropertyValue );
582 break;
584 case BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR:
585 lcl_convertColor( m_xTableModel->getInactiveSelectionTextColor(), aPropertyValue );
586 break;
588 case BASEPROPERTY_TEXTCOLOR:
589 lcl_convertColor( m_xTableModel->getTextColor(), aPropertyValue );
590 break;
592 case BASEPROPERTY_TEXTLINECOLOR:
593 lcl_convertColor( m_xTableModel->getTextLineColor(), aPropertyValue );
594 break;
596 default:
597 aPropertyValue = VCLXWindow::getProperty( PropertyName );
598 break;
601 return aPropertyValue;
605 void SAL_CALL SVTXGridControl::rowsInserted( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
607 SolarMutexGuard aGuard;
608 m_xTableModel->notifyRowsInserted( i_event );
612 void SAL_CALL
613 SVTXGridControl::rowsRemoved( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
615 SolarMutexGuard aGuard;
616 m_xTableModel->notifyRowsRemoved( i_event );
620 void SAL_CALL SVTXGridControl::dataChanged( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
622 SolarMutexGuard aGuard;
624 m_xTableModel->notifyDataChanged( i_event );
626 // if the data model is sortable, a dataChanged event is also fired in case the sort order changed.
627 // So, just in case, invalidate the column header area, too.
628 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
629 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::dataChanged: no control (anymore)!" );
630 pTable->getTableControlInterface().invalidate( TableAreaColumnHeaders );
634 void SAL_CALL SVTXGridControl::rowHeadingChanged( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
636 SolarMutexGuard aGuard;
637 OSL_UNUSED( i_event );
639 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
640 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::rowHeadingChanged: no control (anymore)!" );
642 // TODO: we could do better than this - invalidate the header area only
643 pTable->getTableControlInterface().invalidate( TableAreaRowHeaders );
647 void SAL_CALL SVTXGridControl::elementInserted( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
649 SolarMutexGuard aGuard;
651 Reference< XGridColumn > const xGridColumn( i_event.Element, UNO_QUERY_THROW );
653 sal_Int32 nIndex( m_xTableModel->getColumnCount() );
654 OSL_VERIFY( i_event.Accessor >>= nIndex );
655 m_xTableModel->insertColumn( nIndex, xGridColumn );
659 void SAL_CALL SVTXGridControl::elementRemoved( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
661 SolarMutexGuard aGuard;
663 sal_Int32 nIndex( -1 );
664 OSL_VERIFY( i_event.Accessor >>= nIndex );
665 m_xTableModel->removeColumn( nIndex );
669 void SAL_CALL SVTXGridControl::elementReplaced( const ContainerEvent& i_event ) throw (RuntimeException, std::exception)
671 OSL_ENSURE( false, "SVTXGridControl::elementReplaced: not implemented!" );
672 // at the moment, the XGridColumnModel API does not allow replacing columns
673 OSL_UNUSED( i_event );
674 // TODO: replace the respective column in our table model
679 void SAL_CALL SVTXGridControl::disposing( const EventObject& Source ) throw(RuntimeException, std::exception)
681 VCLXWindow::disposing( Source );
685 void SAL_CALL SVTXGridControl::selectRow( ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, std::exception )
687 SolarMutexGuard aGuard;
689 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
690 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::selectRow: no control (anymore)!" );
692 impl_checkRowIndex_throw( *pTable, i_rowIndex );
694 pTable->SelectRow( i_rowIndex, true );
698 void SAL_CALL SVTXGridControl::selectAllRows() throw (RuntimeException, std::exception)
700 SolarMutexGuard aGuard;
702 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
703 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::selectAllRows: no control (anymore)!" );
705 pTable->SelectAllRows( true );
709 void SAL_CALL SVTXGridControl::deselectRow( ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, std::exception )
711 SolarMutexGuard aGuard;
713 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
714 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::deselectRow: no control (anymore)!" );
716 impl_checkRowIndex_throw( *pTable, i_rowIndex );
718 pTable->SelectRow( i_rowIndex, false );
722 void SAL_CALL SVTXGridControl::deselectAllRows() throw (RuntimeException, std::exception)
724 SolarMutexGuard aGuard;
726 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
727 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::deselectAllRows: no control (anymore)!" );
729 pTable->SelectAllRows( false );
733 Sequence< ::sal_Int32 > SAL_CALL SVTXGridControl::getSelectedRows() throw (RuntimeException, std::exception)
735 SolarMutexGuard aGuard;
737 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
738 ENSURE_OR_RETURN( pTable, "SVTXGridControl::getSelectedRows: no control (anymore)!", Sequence< sal_Int32 >() );
740 sal_Int32 selectionCount = pTable->GetSelectedRowCount();
741 Sequence< sal_Int32 > selectedRows( selectionCount );
742 for ( sal_Int32 i=0; i<selectionCount; ++i )
743 selectedRows[i] = pTable->GetSelectedRowIndex(i);
744 return selectedRows;
748 sal_Bool SAL_CALL SVTXGridControl::hasSelectedRows() throw (RuntimeException, std::exception)
750 SolarMutexGuard aGuard;
752 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
753 ENSURE_OR_RETURN( pTable, "SVTXGridControl::hasSelectedRows: no control (anymore)!", sal_True );
755 return pTable->GetSelectedRowCount() > 0;
759 sal_Bool SAL_CALL SVTXGridControl::isRowSelected( ::sal_Int32 index ) throw (RuntimeException, std::exception)
761 SolarMutexGuard aGuard;
763 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
764 ENSURE_OR_RETURN( pTable, "SVTXGridControl::isRowSelected: no control (anymore)!", sal_False );
766 return pTable->IsRowSelected( index );
770 void SVTXGridControl::dispose() throw(RuntimeException, std::exception)
772 EventObject aObj;
773 aObj.Source = (::cppu::OWeakObject*)this;
774 m_aSelectionListeners.disposeAndClear( aObj );
775 VCLXWindow::dispose();
779 void SVTXGridControl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
781 SolarMutexGuard aGuard;
783 Reference< XWindow > xKeepAlive( this );
785 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
786 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::ProcessWindowEvent: no control (anymore)!" );
788 bool handled = false;
789 switch ( rVclWindowEvent.GetId() )
791 case VCLEVENT_TABLEROW_SELECT:
793 if ( m_aSelectionListeners.getLength() )
794 ImplCallItemListeners();
795 handled = true;
797 break;
799 case VCLEVENT_CONTROL_GETFOCUS:
801 // TODO: this doesn't belong here. It belongs into the TableControl/_Impl, so A11Y also
802 // works when the control is used outside the UNO context
803 if ( pTable->GetRowCount()>0 )
805 pTable->commitCellEventIfAccessibleAlive(
806 AccessibleEventId::STATE_CHANGED,
807 makeAny( AccessibleStateType::FOCUSED ),
808 Any()
810 pTable->commitTableEventIfAccessibleAlive(
811 AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
812 Any(),
813 Any()
816 else
818 pTable->commitTableEventIfAccessibleAlive(
819 AccessibleEventId::STATE_CHANGED,
820 makeAny( AccessibleStateType::FOCUSED ),
821 Any()
825 break;
827 case VCLEVENT_CONTROL_LOSEFOCUS:
829 // TODO: this doesn't belong here. It belongs into the TableControl/_Impl, so A11Y also
830 // works when the control is used outside the UNO context
831 if ( pTable->GetRowCount()>0 )
833 pTable->commitCellEventIfAccessibleAlive(
834 AccessibleEventId::STATE_CHANGED,
835 Any(),
836 makeAny( AccessibleStateType::FOCUSED )
839 else
841 pTable->commitTableEventIfAccessibleAlive(
842 AccessibleEventId::STATE_CHANGED,
843 Any(),
844 makeAny( AccessibleStateType::FOCUSED )
848 break;
851 if ( !handled )
852 VCLXWindow::ProcessWindowEvent( rVclWindowEvent );
856 void SVTXGridControl::setEnable( sal_Bool bEnable ) throw(::com::sun::star::uno::RuntimeException, std::exception)
858 SolarMutexGuard aGuard;
860 m_xTableModel->setEnabled( bEnable );
861 vcl::Window * pWindow = GetWindow();
862 if ( pWindow )
864 pWindow->Enable( bEnable, true );
865 pWindow->EnableInput( bEnable );
866 pWindow->Invalidate();
871 void SVTXGridControl::ImplCallItemListeners()
873 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
874 ENSURE_OR_RETURN_VOID( pTable, "SVTXGridControl::ImplCallItemListeners: no control (anymore)!" );
876 if ( m_aSelectionListeners.getLength() )
878 GridSelectionEvent aEvent;
879 aEvent.Source = (::cppu::OWeakObject*)this;
881 sal_Int32 const nSelectedRowCount( pTable->GetSelectedRowCount() );
882 aEvent.SelectedRowIndexes.realloc( nSelectedRowCount );
883 for ( sal_Int32 i=0; i<nSelectedRowCount; ++i )
884 aEvent.SelectedRowIndexes[i] = pTable->GetSelectedRowIndex( i );
885 m_aSelectionListeners.selectionChanged( aEvent );
890 void SVTXGridControl::impl_updateColumnsFromModel_nothrow()
892 Reference< XGridColumnModel > const xColumnModel( m_xTableModel->getColumnModel() );
893 ENSURE_OR_RETURN_VOID( xColumnModel.is(), "no model!" );
894 VclPtr< TableControl > pTable = GetAsDynamic< TableControl >();
895 ENSURE_OR_RETURN_VOID( pTable, "no table!" );
899 const Sequence< Reference< XGridColumn > > columns = xColumnModel->getColumns();
900 for ( const Reference< XGridColumn >* colRef = columns.getConstArray();
901 colRef != columns.getConstArray() + columns.getLength();
902 ++colRef
905 if ( !colRef->is() )
907 SAL_WARN( "svtools.uno", "illegal column!" );
908 continue;
911 m_xTableModel->appendColumn( *colRef );
914 catch( const Exception& )
916 DBG_UNHANDLED_EXCEPTION();
920 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */