1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "unocontroltablemodel.hxx"
#include "unogridcolumnfacade.hxx"

#include <table/defaultinputhandler.hxx>
#include <table/gridtablerenderer.hxx>

#include <com/sun/star/awt/grid/XGridColumn.hpp>
#include <com/sun/star/awt/grid/XSortableGridData.hpp>
#include <com/sun/star/util/Color.hpp>
#include <o3tl/safeint.hxx>
#include <sal/log.hxx>
#include <cppuhelper/weakref.hxx>
#include <tools/debug.hxx>
#include <tools/diagnose_ex.h>


namespace svt::table
{


    using css::uno::Reference;
    using css::uno::Sequence;
    using css::uno::UNO_QUERY_THROW;
    using css::uno::UNO_QUERY;
    using css::awt::grid::XGridColumn;
    using css::uno::Exception;
    using css::awt::grid::XGridDataModel;
    using css::awt::grid::XGridColumnModel;
    using css::uno::Any;
    using css::style::VerticalAlignment_TOP;
    using css::style::VerticalAlignment;
    using css::uno::WeakReference;
    using css::awt::grid::GridDataEvent;
    using css::awt::grid::XSortableGridData;
    using css::beans::Pair;


    //= UnoControlTableModel_Impl

    typedef ::std::vector< PTableModelListener >    ModellListeners;
    typedef ::std::vector< PColumnModel >           ColumnModels;
    struct UnoControlTableModel_Impl
    {
        ColumnModels                                    aColumns;
        bool                                            bHasColumnHeaders;
        bool                                            bHasRowHeaders;
        ScrollbarVisibility                             eVScrollMode;
        ScrollbarVisibility                             eHScrollMode;
        PTableRenderer                                  pRenderer;
        PTableInputHandler                              pInputHandler;
        TableMetrics                                    nRowHeight;
        TableMetrics                                    nColumnHeaderHeight;
        TableMetrics                                    nRowHeaderWidth;
        ::std::optional< ::Color >                    m_aGridLineColor;
        ::std::optional< ::Color >                    m_aHeaderBackgroundColor;
        ::std::optional< ::Color >                    m_aHeaderTextColor;
        ::std::optional< ::Color >                    m_aActiveSelectionBackColor;
        ::std::optional< ::Color >                    m_aInactiveSelectionBackColor;
        ::std::optional< ::Color >                    m_aActiveSelectionTextColor;
        ::std::optional< ::Color >                    m_aInactiveSelectionTextColor;
        ::std::optional< ::Color >                    m_aTextColor;
        ::std::optional< ::Color >                    m_aTextLineColor;
        ::std::optional< ::std::vector< ::Color > >   m_aRowColors;
        VerticalAlignment                               m_eVerticalAlign;
        bool                                            bEnabled;
        ModellListeners                                 m_aListeners;
        WeakReference< XGridDataModel >                 m_aDataModel;
        WeakReference< XGridColumnModel >               m_aColumnModel;

        UnoControlTableModel_Impl()
            :aColumns                       ( )
            ,bHasColumnHeaders              ( true      )
            ,bHasRowHeaders                 ( false     )
            ,eVScrollMode                   ( ScrollbarShowNever )
            ,eHScrollMode                   ( ScrollbarShowNever )
            ,pRenderer                      ( )
            ,pInputHandler                  ( )
            ,nRowHeight                     ( 10 )
            ,nColumnHeaderHeight            ( 10 )
            ,nRowHeaderWidth                ( 10 )
            ,m_aGridLineColor               ( )
            ,m_aHeaderBackgroundColor       ( )
            ,m_aHeaderTextColor             ( )
            ,m_aActiveSelectionBackColor    ( )
            ,m_aInactiveSelectionBackColor  ( )
            ,m_aActiveSelectionTextColor    ( )
            ,m_aInactiveSelectionTextColor  ( )
            ,m_aTextColor                   ( )
            ,m_aTextLineColor               ( )
            ,m_aRowColors                   ( )
            ,m_eVerticalAlign               ( VerticalAlignment_TOP )
            ,bEnabled                       ( true )
        {
        }
    };

    //= UnoControlTableModel
#define DBG_CHECK_ME() \
    DBG_TESTSOLARMUTEX(); \

    UnoControlTableModel::UnoControlTableModel()
        :m_pImpl( new UnoControlTableModel_Impl )
    {
        m_pImpl->bHasColumnHeaders = true;
        m_pImpl->bHasRowHeaders = false;
        m_pImpl->bEnabled = true;
        m_pImpl->pRenderer = std::make_shared<GridTableRenderer>( *this );
        m_pImpl->pInputHandler = std::make_shared<DefaultInputHandler>();
    }


    UnoControlTableModel::~UnoControlTableModel()
    {
    }


    TableSize UnoControlTableModel::getColumnCount() const
    {
        DBG_CHECK_ME();
        return static_cast<TableSize>(m_pImpl->aColumns.size());
    }


    TableSize UnoControlTableModel::getRowCount() const
    {
        DBG_CHECK_ME();

        TableSize nRowCount = 0;
        try
        {
            Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
            ENSURE_OR_THROW( xDataModel.is(), "no data model anymore!" );
            nRowCount = xDataModel->getRowCount();
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
        return nRowCount;
    }


    bool UnoControlTableModel::hasColumnHeaders() const
    {
        DBG_CHECK_ME();
        return m_pImpl->bHasColumnHeaders;
    }


    bool UnoControlTableModel::hasRowHeaders() const
    {
        DBG_CHECK_ME();
        return m_pImpl->bHasRowHeaders;
    }


    void UnoControlTableModel::setRowHeaders(bool _bRowHeaders)
    {
        DBG_CHECK_ME();
        if ( m_pImpl->bHasRowHeaders == _bRowHeaders )
            return;

        m_pImpl->bHasRowHeaders = _bRowHeaders;
        impl_notifyTableMetricsChanged();
    }


    void UnoControlTableModel::setColumnHeaders(bool _bColumnHeaders)
    {
        DBG_CHECK_ME();
        if ( m_pImpl->bHasColumnHeaders == _bColumnHeaders )
            return;

        m_pImpl->bHasColumnHeaders = _bColumnHeaders;
        impl_notifyTableMetricsChanged();
    }


    PColumnModel UnoControlTableModel::getColumnModel( ColPos column )
    {
        DBG_CHECK_ME();
        ENSURE_OR_RETURN( ( column >= 0 ) && ( column < getColumnCount() ),
            "DefaultTableModel::getColumnModel: invalid index!", PColumnModel() );
        return m_pImpl->aColumns[ column ];
    }


    void UnoControlTableModel::appendColumn( Reference< XGridColumn > const & i_column )
    {
        DBG_CHECK_ME();
        insertColumn( m_pImpl->aColumns.size(), i_column );
    }


    void UnoControlTableModel::insertColumn( ColPos const i_position, Reference< XGridColumn > const & i_column )
    {
        DBG_CHECK_ME();
        ENSURE_OR_RETURN_VOID( ( i_position >= 0 ) && ( o3tl::make_unsigned( i_position ) <= m_pImpl->aColumns.size() ),
            "UnoControlTableModel::insertColumn: illegal position!" );

        const PColumnModel pColumn = std::make_shared<UnoGridColumnFacade>( *this, i_column );
        m_pImpl->aColumns.insert( m_pImpl->aColumns.begin() + i_position, pColumn );

        // notify listeners
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->columnInserted();
        }
    }


    void UnoControlTableModel::removeColumn( ColPos const i_position )
    {
        DBG_CHECK_ME();
        ENSURE_OR_RETURN_VOID( ( i_position >= 0 ) && ( o3tl::make_unsigned( i_position ) <= m_pImpl->aColumns.size() ),
            "UnoControlTableModel::removeColumn: illegal position!" );

        // remove the column
        ColumnModels::iterator pos = m_pImpl->aColumns.begin() + i_position;
        const PColumnModel pColumn = *pos;
        m_pImpl->aColumns.erase( pos );

        // notify listeners
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->columnRemoved();
        }

        // dispose the column
        UnoGridColumnFacade* pColumnImpl = dynamic_cast< UnoGridColumnFacade* >( pColumn.get() );
        OSL_ENSURE( pColumnImpl != nullptr, "UnoControlTableModel::removeColumn: illegal column implementation!" );
        if ( pColumnImpl )
            pColumnImpl->dispose();
    }


    void UnoControlTableModel::removeAllColumns()
    {
        DBG_CHECK_ME();
        if ( m_pImpl->aColumns.empty() )
            return;

        // dispose the column instances
        for (auto const& col : m_pImpl->aColumns)
        {
            UnoGridColumnFacade* pColumn = dynamic_cast< UnoGridColumnFacade* >( col.get() );
            if ( !pColumn )
            {
                SAL_WARN( "svtools.uno", "UnoControlTableModel::removeAllColumns: illegal column implementation!" );
                continue;
            }

            pColumn->dispose();
        }
        m_pImpl->aColumns.clear();

        // notify listeners
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->allColumnsRemoved();
        }
    }


    void UnoControlTableModel::impl_notifyTableMetricsChanged() const
    {
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->tableMetricsChanged();
        }
    }


    PTableRenderer UnoControlTableModel::getRenderer() const
    {
        DBG_CHECK_ME();
        return m_pImpl->pRenderer;
    }


    PTableInputHandler UnoControlTableModel::getInputHandler() const
    {
        DBG_CHECK_ME();
        return m_pImpl->pInputHandler;
    }


    TableMetrics UnoControlTableModel::getRowHeight() const
    {
        DBG_CHECK_ME();
        return m_pImpl->nRowHeight;
    }


    void UnoControlTableModel::setRowHeight(TableMetrics _nRowHeight)
    {
        DBG_CHECK_ME();
        if ( m_pImpl->nRowHeight == _nRowHeight )
            return;

        m_pImpl->nRowHeight = _nRowHeight;
        impl_notifyTableMetricsChanged();
    }


    TableMetrics UnoControlTableModel::getColumnHeaderHeight() const<--- The function 'getColumnHeaderHeight' is never used.
    {
        DBG_CHECK_ME();
        DBG_ASSERT( hasColumnHeaders(), "DefaultTableModel::getColumnHeaderHeight: invalid call!" );
        return m_pImpl->nColumnHeaderHeight;
    }


    TableMetrics UnoControlTableModel::getRowHeaderWidth() const<--- The function 'getRowHeaderWidth' is never used.
    {
        DBG_CHECK_ME();
        DBG_ASSERT( hasRowHeaders(), "DefaultTableModel::getRowHeaderWidth: invalid call!" );
        return m_pImpl->nRowHeaderWidth;
    }

    void UnoControlTableModel::setColumnHeaderHeight(TableMetrics _nHeight)
    {
        DBG_CHECK_ME();
        if ( m_pImpl->nColumnHeaderHeight == _nHeight )
            return;

        m_pImpl->nColumnHeaderHeight = _nHeight;
        impl_notifyTableMetricsChanged();
    }


    void UnoControlTableModel::setRowHeaderWidth(TableMetrics _nWidth)
    {
        DBG_CHECK_ME();
        if ( m_pImpl->nRowHeaderWidth == _nWidth )
            return;

        m_pImpl->nRowHeaderWidth = _nWidth;
        impl_notifyTableMetricsChanged();
    }


    ScrollbarVisibility UnoControlTableModel::getVerticalScrollbarVisibility() const
    {
        DBG_CHECK_ME();
        return m_pImpl->eVScrollMode;
    }


    ScrollbarVisibility UnoControlTableModel::getHorizontalScrollbarVisibility() const
    {
        DBG_CHECK_ME();
        return m_pImpl->eHScrollMode;
    }


    void UnoControlTableModel::addTableModelListener( const PTableModelListener& i_listener )<--- The function 'addTableModelListener' is never used.
    {
        DBG_CHECK_ME();
        ENSURE_OR_RETURN_VOID( !!i_listener, "illegal NULL listener" );
        m_pImpl->m_aListeners.push_back( i_listener );
    }


    void UnoControlTableModel::removeTableModelListener( const PTableModelListener& i_listener )<--- The function 'removeTableModelListener' is never used.
    {
        DBG_CHECK_ME();
        auto lookup = std::find(m_pImpl->m_aListeners.begin(), m_pImpl->m_aListeners.end(), i_listener);
        if (lookup != m_pImpl->m_aListeners.end())
        {
            m_pImpl->m_aListeners.erase( lookup );
            return;
        }
        OSL_ENSURE( false, "UnoControlTableModel::removeTableModelListener: listener is not registered - sure you're doing the right thing here?" );
    }


    void UnoControlTableModel::setVerticalScrollbarVisibility( ScrollbarVisibility const i_visibility ) const
    {
        DBG_CHECK_ME();
        m_pImpl->eVScrollMode = i_visibility;
    }


    void UnoControlTableModel::setHorizontalScrollbarVisibility( ScrollbarVisibility const i_visibility ) const
    {
        DBG_CHECK_ME();
        m_pImpl->eHScrollMode = i_visibility;
    }


    void UnoControlTableModel::setDataModel( Reference< XGridDataModel > const & i_gridDataModel )
    {
        DBG_CHECK_ME();
        m_pImpl->m_aDataModel = i_gridDataModel;
        // TODO: register as listener, so we're notified of row/data changes, and can multiplex them to our
        // own listeners
    }


    Reference< XGridDataModel > UnoControlTableModel::getDataModel() const
    {
        Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
        return xDataModel;
    }


    bool UnoControlTableModel::hasDataModel() const
    {
        return getDataModel().is();
    }


    void UnoControlTableModel::setColumnModel( Reference< XGridColumnModel > const & i_gridColumnModel )
    {
        DBG_CHECK_ME();
        m_pImpl->m_aColumnModel = i_gridColumnModel;
    }


    Reference< XGridColumnModel > UnoControlTableModel::getColumnModel() const
    {
        Reference< XGridColumnModel > const xColumnModel( m_pImpl->m_aColumnModel );
        return xColumnModel;
    }


    bool UnoControlTableModel::hasColumnModel() const
    {
        return getColumnModel().is();
    }


    void UnoControlTableModel::getCellContent( ColPos const i_col, RowPos const i_row, Any& o_cellContent )
    {
        DBG_CHECK_ME();

        o_cellContent.clear();
        try
        {
            Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
            ENSURE_OR_RETURN_VOID( xDataModel.is(), "UnoControlTableModel::getCellContent: no data model anymore!" );

            PColumnModel const pColumn = getColumnModel( i_col );
            UnoGridColumnFacade* pColumnImpl = dynamic_cast< UnoGridColumnFacade* >( pColumn.get() );<--- Assignment 'pColumnImpl=dynamic_cast(pColumn.get())', assigned value is 0
            ENSURE_OR_RETURN_VOID( pColumnImpl != nullptr, "UnoControlTableModel::getCellContent: no (valid) column at this position!" );<--- Assuming that condition 'pColumnImpl!=nullptr' is not redundant
            sal_Int32 const nDataColumnIndex = pColumnImpl->getDataColumnIndex() >= 0 ? pColumnImpl->getDataColumnIndex() : i_col;<--- Null pointer dereference

            if ( nDataColumnIndex >= xDataModel->getColumnCount() )
            {
                // this is allowed, in case the column model has been dynamically extended, but the data model does
                // not (yet?) know about it.
                // So, handle it gracefully.
            #if OSL_DEBUG_LEVEL > 0
                Reference< XGridColumnModel > const xColumnModel( m_pImpl->m_aColumnModel );
                OSL_ENSURE( xColumnModel.is() && i_col < xColumnModel->getColumnCount(),
                    "UnoControlTableModel::getCellContent: request a column's value which the ColumnModel doesn't know about!" );
            #endif
            }
            else
            {
                o_cellContent = xDataModel->getCellData( nDataColumnIndex, i_row );
            }
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
    }


    void UnoControlTableModel::getCellToolTip( ColPos const i_col, RowPos const i_row, Any& o_cellToolTip )
    {
        DBG_CHECK_ME();
        try
        {
            Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
            ENSURE_OR_THROW( xDataModel.is(), "no data model anymore!" );

            o_cellToolTip = xDataModel->getCellToolTip( i_col, i_row );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
    }


    Any UnoControlTableModel::getRowHeading( RowPos const i_rowPos ) const
    {
        DBG_CHECK_ME();

        Any aRowHeading;

        Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
        ENSURE_OR_RETURN( xDataModel.is(), "UnoControlTableModel::getRowHeading: no data model anymore!", aRowHeading );

        try
        {
            aRowHeading = xDataModel->getRowHeading( i_rowPos );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
        return aRowHeading;
    }


    namespace
    {
        void lcl_setColor( Any const & i_color, ::std::optional< ::Color > & o_convertedColor )
        {
            if ( !i_color.hasValue() )
                o_convertedColor.reset();
            else
            {
                Color nColor = COL_TRANSPARENT;
                if ( i_color >>= nColor )
                {
                    o_convertedColor = nColor;
                }
                else
                {
                    OSL_ENSURE( false, "lcl_setColor: could not extract color value!" );
                }
            }
        }
    }


    ::std::optional< ::Color > UnoControlTableModel::getLineColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aGridLineColor;
    }


    void UnoControlTableModel::setLineColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aGridLineColor );
    }


    ::std::optional< ::Color > UnoControlTableModel::getHeaderBackgroundColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aHeaderBackgroundColor;
    }


    void UnoControlTableModel::setHeaderBackgroundColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aHeaderBackgroundColor );
    }


    ::std::optional< ::Color > UnoControlTableModel::getHeaderTextColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aHeaderTextColor;
    }


    ::std::optional< ::Color > UnoControlTableModel::getActiveSelectionBackColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aActiveSelectionBackColor;
    }


    ::std::optional< ::Color > UnoControlTableModel::getInactiveSelectionBackColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aInactiveSelectionBackColor;
    }


    ::std::optional< ::Color > UnoControlTableModel::getActiveSelectionTextColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aActiveSelectionTextColor;
    }


    ::std::optional< ::Color > UnoControlTableModel::getInactiveSelectionTextColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aInactiveSelectionTextColor;
    }


    void UnoControlTableModel::setHeaderTextColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aHeaderTextColor );
    }


    void UnoControlTableModel::setActiveSelectionBackColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aActiveSelectionBackColor );
    }


    void UnoControlTableModel::setInactiveSelectionBackColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aInactiveSelectionBackColor );
    }


    void UnoControlTableModel::setActiveSelectionTextColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aActiveSelectionTextColor );
    }


    void UnoControlTableModel::setInactiveSelectionTextColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aInactiveSelectionTextColor );
    }


    ::std::optional< ::Color > UnoControlTableModel::getTextColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aTextColor;
    }


    void UnoControlTableModel::setTextColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aTextColor );
    }


    ::std::optional< ::Color > UnoControlTableModel::getTextLineColor() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aTextColor;
    }


    void UnoControlTableModel::setTextLineColor( Any const & i_color )
    {
        DBG_CHECK_ME();
        lcl_setColor( i_color, m_pImpl->m_aTextLineColor );
    }


    ::std::optional< ::std::vector< ::Color > > UnoControlTableModel::getRowBackgroundColors() const
    {
        DBG_CHECK_ME();
        return m_pImpl->m_aRowColors;
    }


    void UnoControlTableModel::setRowBackgroundColors( css::uno::Any const & i_APIValue )
    {
        DBG_CHECK_ME();
        Sequence< css::util::Color > aAPIColors;
        if ( !( i_APIValue >>= aAPIColors ) )
            m_pImpl->m_aRowColors.reset();
        else
        {
            ::std::vector< ::Color > aColors( aAPIColors.getLength() );
            std::transform(aAPIColors.begin(), aAPIColors.end(), aColors.begin(),
                [](const css::util::Color& rAPIColor) -> ::Color { return Color(rAPIColor); });
            m_pImpl->m_aRowColors = aColors;
        }
    }


    VerticalAlignment UnoControlTableModel::getVerticalAlign() const
    {
        DBG_CHECK_ME();
        return  m_pImpl->m_eVerticalAlign;
    }


    void UnoControlTableModel::setVerticalAlign( VerticalAlignment _xAlign )
    {
        DBG_CHECK_ME();
        m_pImpl->m_eVerticalAlign = _xAlign;
    }


    ColPos UnoControlTableModel::getColumnPos( UnoGridColumnFacade const & i_column ) const
    {
        DBG_CHECK_ME();
        ColPos nPos = 0;
        for (auto const& col : m_pImpl->aColumns)
        {
            if ( &i_column == col.get() )
                return nPos;
            ++nPos;
        }
        OSL_ENSURE( false, "UnoControlTableModel::getColumnPos: column not found!" );
        return COL_INVALID;
    }


    ITableDataSort* UnoControlTableModel::getSortAdapter()
    {
        DBG_CHECK_ME();

        Reference< XSortableGridData > const xSortAccess( getDataModel(), UNO_QUERY );
        if ( xSortAccess.is() )
            return this;
        return nullptr;
    }


    bool UnoControlTableModel::isEnabled() const
    {
        DBG_CHECK_ME();
        return m_pImpl->bEnabled;
    }


    void UnoControlTableModel::setEnabled( bool _bEnabled )
    {
        DBG_CHECK_ME();
        m_pImpl->bEnabled = _bEnabled;
    }


    void UnoControlTableModel::sortByColumn( ColPos const i_column, ColumnSortDirection const i_sortDirection )
    {
        DBG_CHECK_ME();

        try
        {
            Reference< XSortableGridData > const xSortAccess( getDataModel(), UNO_QUERY_THROW );
            xSortAccess->sortByColumn( i_column, i_sortDirection == ColumnSortAscending );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
    }


    ColumnSort UnoControlTableModel::getCurrentSortOrder() const
    {
        DBG_CHECK_ME();

        ColumnSort currentSort;
        try
        {
            Reference< XSortableGridData > const xSortAccess( getDataModel(), UNO_QUERY_THROW );
            Pair< ::sal_Int32, sal_Bool > const aCurrentSortOrder( xSortAccess->getCurrentSortOrder() );
            currentSort.nColumnPos = aCurrentSortOrder.First;
            currentSort.eSortDirection = aCurrentSortOrder.Second ? ColumnSortAscending : ColumnSortDescending;
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svtools.uno");
        }
        return currentSort;
    }


    void UnoControlTableModel::notifyColumnChange( ColPos const i_columnPos, ColumnAttributeGroup const i_attributeGroup ) const
    {
        DBG_CHECK_ME();
        ENSURE_OR_RETURN_VOID( ( i_columnPos >= 0 ) && ( i_columnPos < getColumnCount() ),
            "UnoControlTableModel::notifyColumnChange: invalid column index!" );

        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->columnChanged( i_columnPos, i_attributeGroup );
        }
    }


    void UnoControlTableModel::notifyRowsInserted( GridDataEvent const & i_event ) const
    {
        // check sanity of the event
        ENSURE_OR_RETURN_VOID( i_event.FirstRow >= 0, "UnoControlTableModel::notifyRowsInserted: invalid first row!" );
        ENSURE_OR_RETURN_VOID( i_event.LastRow >= i_event.FirstRow, "UnoControlTableModel::notifyRowsInserted: invalid row indexes!" );

        // check own sanity
        Reference< XGridColumnModel > const xColumnModel( m_pImpl->m_aColumnModel );
        ENSURE_OR_RETURN_VOID( xColumnModel.is(), "UnoControlTableModel::notifyRowsInserted: no column model anymore!" );

        Reference< XGridDataModel > const xDataModel( m_pImpl->m_aDataModel );
        ENSURE_OR_RETURN_VOID( xDataModel.is(), "UnoControlTableModel::notifyRowsInserted: no data model anymore!" );

        // implicitly add columns to the column model
        // TODO: is this really a good idea?
        sal_Int32 const dataColumnCount = xDataModel->getColumnCount();
        OSL_ENSURE( dataColumnCount > 0, "UnoControlTableModel::notifyRowsInserted: no columns at all?" );

        sal_Int32 const modelColumnCount = xColumnModel->getColumnCount();
        if ( ( modelColumnCount == 0 ) && ( dataColumnCount > 0 ) )
        {
            // TODO: shouldn't we clear the mutexes guard for this call?
            xColumnModel->setDefaultColumns( dataColumnCount );
        }

        // multiplex the event to our own listeners
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->rowsInserted( i_event.FirstRow, i_event.LastRow );
        }
    }


    void UnoControlTableModel::notifyRowsRemoved( GridDataEvent const & i_event ) const
    {
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->rowsRemoved( i_event.FirstRow, i_event.LastRow );
        }
    }


    void UnoControlTableModel::notifyDataChanged( css::awt::grid::GridDataEvent const & i_event ) const
    {
        RowPos const firstRow = i_event.FirstRow == -1 ? 0 : i_event.FirstRow;
        RowPos const lastRow = i_event.FirstRow == -1 ? getRowCount() - 1 : i_event.LastRow;

        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->cellsUpdated( firstRow, lastRow );
        }
    }


    void UnoControlTableModel::notifyAllDataChanged() const
    {
        ModellListeners aListeners( m_pImpl->m_aListeners );
        for (auto const& listener : aListeners)
        {
            listener->cellsUpdated( 0, getRowCount() - 1 );
        }
    }


} // svt::table

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */