summaryrefslogtreecommitdiff
path: root/svx/source/table/tablemodel.cxx
blob: 83b1a95723a90fdc5768a06c3d8f29a498d20e35 (plain)
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: tablemodel.cxx,v $
 * $Revision: 1.4.264.2 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"

#include <com/sun/star/table/XMergeableCell.hpp>

#include <algorithm>
#include <boost/bind.hpp>

#include <vcl/svapp.hxx>
#include <vos/mutex.hxx>

#include "cell.hxx"
#include "cellcursor.hxx"
#include "tablemodel.hxx"
#include "tablerow.hxx"
#include "tablerows.hxx"
#include "tablecolumn.hxx"
#include "tablecolumns.hxx"
#include "tableundo.hxx"
#include "svx/svdotable.hxx"
#include "svx/svdmodel.hxx"
#include "svdstr.hrc"
#include "svdglob.hxx"

using ::rtl::OUString;
using namespace ::osl;
using namespace ::vos;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::table;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::util;

// -----------------------------------------------------------------------------

namespace sdr { namespace table {

// -----------------------------------------------------------------------------

// removes the given range from a vector
template< class Vec, class Iter > void remove_range( Vec& rVector, sal_Int32 nIndex, sal_Int32 nCount )
{
    const sal_Int32 nSize = static_cast<sal_Int32>(rVector.size());
    if( nCount && (nIndex >= 0) && (nIndex < nSize) )
    {
        if( (nIndex + nCount) >= nSize )
        {
            // remove at end
            rVector.resize( nIndex );
        }
        else
        {
            Iter aBegin( rVector.begin() );
            while( nIndex-- )
                aBegin++;
            if( nCount == 1 )
            {
                rVector.erase( aBegin );
            }
            else
            {
                Iter aEnd( aBegin );

                while( nCount-- )
                    aEnd++;
                rVector.erase( aBegin, aEnd );
            }
        }
    }
}

// -----------------------------------------------------------------------------

/** inserts a range into a vector */
template< class Vec, class Iter, class Entry > sal_Int32 insert_range( Vec& rVector, sal_Int32 nIndex, sal_Int32 nCount )
{
    if( nCount )
    {
        if( nIndex >= static_cast< sal_Int32 >( rVector.size() ) )
        {
            // append at end
            nIndex = static_cast< sal_Int32 >( rVector.size() ); // cap to end
            rVector.resize( nIndex + nCount );
        }
        else
        {
            // insert
            sal_Int32 nFind = nIndex;
            Iter aIter( rVector.begin() );
            while( nFind-- )
                aIter++;

            Entry aEmpty;
            rVector.insert( aIter, nCount, aEmpty );
        }
    }
    return nIndex;
}

// -----------------------------------------------------------------------------

TableModel::TableModel( SdrTableObj* pTableObj )
: TableModelBase( m_aMutex  )
, mpTableObj( pTableObj )
, mbModified( sal_False )
, mbNotifyPending( false )
, mnNotifyLock( 0 )
{
}

TableModel::TableModel( SdrTableObj* pTableObj, const TableModelRef& xSourceTable )
: TableModelBase( m_aMutex  )
, mpTableObj( pTableObj )
, mbModified( sal_False )
, mbNotifyPending( false )
, mnNotifyLock( 0 )
{
    if( xSourceTable.is() )
    {
        const sal_Int32 nColCount = xSourceTable->getColumnCountImpl();
        const sal_Int32 nRowCount = xSourceTable->getRowCountImpl();

        init( nColCount, nRowCount );

        sal_Int32 nRows = nRowCount;
        while( nRows-- )
            (*maRows[nRows]) = (*xSourceTable->maRows[nRows]);

        sal_Int32 nColumns = nColCount;
        while( nColumns-- )
            (*maColumns[nColumns]) = (*xSourceTable->maColumns[nColumns]);

        // copy cells
        for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
        {
            for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
            {
                CellRef xTargetCell( getCell( nCol, nRow ) );
                if( xTargetCell.is() )
                    xTargetCell->cloneFrom( xSourceTable->getCell( nCol, nRow ) );
            }
        }
    }
}

// -----------------------------------------------------------------------------

TableModel::~TableModel()
{
}

// -----------------------------------------------------------------------------

void TableModel::init( sal_Int32 nColumns, sal_Int32 nRows )
{
    if( nRows < 20 )
        maRows.reserve( 20 );

    if( nColumns < 20 )
        maColumns.reserve( 20 );

    if( nRows && nColumns )
    {
        maColumns.resize( nColumns );
        maRows.resize( nRows );

        while( nRows-- )
            maRows[nRows].set( new TableRow( this, nRows, nColumns ) );

        while( nColumns-- )
            maColumns[nColumns].set( new TableColumn( this, nColumns ) );
    }
}

// -----------------------------------------------------------------------------
// ICellRange
// -----------------------------------------------------------------------------

sal_Int32 TableModel::getLeft()
{
    return 0;
}

// -----------------------------------------------------------------------------

sal_Int32 TableModel::getTop()
{
    return 0;
}

// -----------------------------------------------------------------------------

sal_Int32 TableModel::getRight()
{
    return getColumnCount();
}

// -----------------------------------------------------------------------------

sal_Int32 TableModel::getBottom()
{
    return getRowCount();
}

// -----------------------------------------------------------------------------

Reference< XTable > TableModel::getTable()
{
    return this;
}

// -----------------------------------------------------------------------------

void TableModel::UndoInsertRows( sal_Int32 nIndex, sal_Int32 nCount )
{
    TableModelNotifyGuard aGuard( this );

    // remove the rows
    remove_range<RowVector,RowVector::iterator>( maRows, nIndex, nCount );
    updateRows();
    setModified(sal_True);
}

// -----------------------------------------------------------------------------

void TableModel::UndoRemoveRows( sal_Int32 nIndex, RowVector& aRows )
{
    TableModelNotifyGuard aGuard( this );

    const sal_Int32 nCount = sal::static_int_cast< sal_Int32 >( aRows.size() );

    nIndex = insert_range<RowVector,RowVector::iterator,TableRowRef>( maRows, nIndex, nCount );

    for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
        maRows[nIndex+nOffset] = aRows[nOffset];

    updateRows();
    setModified(sal_True);
}

// -----------------------------------------------------------------------------

void TableModel::UndoInsertColumns( sal_Int32 nIndex, sal_Int32 nCount )
{
    TableModelNotifyGuard aGuard( this );

    // now remove the columns
    remove_range<ColumnVector,ColumnVector::iterator>( maColumns, nIndex, nCount );
    sal_Int32 nRows = getRowCountImpl();
    while( nRows-- )
        maRows[nRows]->removeColumns( nIndex, nCount );

    updateColumns();
    setModified(sal_True);
}

// -----------------------------------------------------------------------------

void TableModel::UndoRemoveColumns( sal_Int32 nIndex, ColumnVector& aCols, CellVector& aCells )
{
    TableModelNotifyGuard aGuard( this );

    const sal_Int32 nCount = sal::static_int_cast< sal_Int32 >( aCols.size() );

    // assert if there are not enough cells saved
    DBG_ASSERT( (aCols.size() * maRows.size()) == aCells.size(), "sdr::table::TableModel::UndoRemoveColumns(), invalid undo data!" );

    nIndex = insert_range<ColumnVector,ColumnVector::iterator,TableColumnRef>( maColumns, nIndex, nCount );
    for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
        maColumns[nIndex+nOffset] = aCols[nOffset];

    CellVector::iterator aIter( aCells.begin() );

    sal_Int32 nRows = getRowCountImpl();
    for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
        maRows[nRow]->insertColumns( nIndex, nCount, &aIter );

    updateColumns();
    setModified(sal_True);
}

// -----------------------------------------------------------------------------
// XTable
// -----------------------------------------------------------------------------

Reference< XCellCursor > SAL_CALL TableModel::createCursor() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    return createCursorByRange( Reference< XCellRange >( this ) );
}

// -----------------------------------------------------------------------------

Reference< XCellCursor > SAL_CALL TableModel::createCursorByRange( const Reference< XCellRange >& Range ) throw (IllegalArgumentException, RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );

    ICellRange* pRange = dynamic_cast< ICellRange* >( Range.get() );
    if( (pRange == 0) || (pRange->getTable().get() != this) )
        throw IllegalArgumentException();

    TableModelRef xModel( this );
    return new CellCursor( xModel, pRange->getLeft(), pRange->getTop(), pRange->getRight(), pRange->getBottom() );
}

// -----------------------------------------------------------------------------

sal_Int32 SAL_CALL TableModel::getRowCount() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    return getRowCountImpl();
}

// -----------------------------------------------------------------------------

sal_Int32 SAL_CALL TableModel::getColumnCount() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    return getColumnCountImpl();
}

// -----------------------------------------------------------------------------
// XComponent
// -----------------------------------------------------------------------------

void TableModel::dispose() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    TableModelBase::dispose();
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::addEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
{
    TableModelBase::addEventListener( xListener );
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::removeEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
{
    TableModelBase::removeEventListener( xListener );
}

// -----------------------------------------------------------------------------
// XModifiable
// -----------------------------------------------------------------------------

sal_Bool SAL_CALL TableModel::isModified(  ) throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    return mbModified;
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::setModified( sal_Bool bModified ) throw (PropertyVetoException, RuntimeException)
{
    {
        OGuard aGuard( Application::GetSolarMutex() );
        mbModified = bModified;
    }
    if( bModified )
        notifyModification();
}

// -----------------------------------------------------------------------------
// XModifyBroadcaster
// -----------------------------------------------------------------------------

void SAL_CALL TableModel::addModifyListener( const Reference< XModifyListener >& xListener ) throw (RuntimeException)
{
    rBHelper.addListener( XModifyListener::static_type() , xListener );
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::removeModifyListener( const Reference< XModifyListener >& xListener ) throw (RuntimeException)
{
    rBHelper.removeListener( XModifyListener::static_type() , xListener );
}

// -----------------------------------------------------------------------------
// XColumnRowRange
// -----------------------------------------------------------------------------

Reference< XTableColumns > SAL_CALL TableModel::getColumns() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );

    if( !mxTableColumns.is() )
        mxTableColumns.set( new TableColumns( this ) );
    return mxTableColumns.get();
}

// -----------------------------------------------------------------------------

Reference< XTableRows > SAL_CALL TableModel::getRows() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );

    if( !mxTableRows.is() )
        mxTableRows.set( new TableRows( this ) );
    return mxTableRows.get();
}

// -----------------------------------------------------------------------------
// XCellRange
// -----------------------------------------------------------------------------

Reference< XCell > SAL_CALL TableModel::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow ) throw ( IndexOutOfBoundsException, RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );

    CellRef xCell( getCell( nColumn, nRow ) );
    if( xCell.is() )
        return xCell.get();

    throw IndexOutOfBoundsException();
}

// -----------------------------------------------------------------------------

Reference< XCellRange > SAL_CALL TableModel::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom ) throw (IndexOutOfBoundsException, RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );

    if( (nLeft >= 0) && (nTop >= 0) && (nRight >= nLeft) && (nBottom >= nTop) && (nRight < getColumnCountImpl()) && (nBottom < getRowCountImpl() ) )
    {
        TableModelRef xModel( this );
        return new CellRange( xModel, nLeft, nTop, nRight, nBottom );
    }

    throw IndexOutOfBoundsException();
}

// -----------------------------------------------------------------------------

Reference< XCellRange > SAL_CALL TableModel::getCellRangeByName( const OUString& /*aRange*/ ) throw (RuntimeException)
{
    return Reference< XCellRange >();
}

// -----------------------------------------------------------------------------
// XPropertySet
// -----------------------------------------------------------------------------

Reference< XPropertySetInfo > SAL_CALL TableModel::getPropertySetInfo(  ) throw (RuntimeException)
{
    Reference< XPropertySetInfo > xInfo;
    return xInfo;
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::setPropertyValue( const ::rtl::OUString& /*aPropertyName*/, const Any& /*aValue*/ ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------

Any SAL_CALL TableModel::getPropertyValue( const OUString& /*PropertyName*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    return Any();
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::addPropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::removePropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::addVetoableChangeListener( const OUString& /*aPropertyName*/, const Reference< XVetoableChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------

void SAL_CALL TableModel::removeVetoableChangeListener( const OUString& /*aPropertyName*/, const Reference< XVetoableChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------
// XFastPropertySet
// -----------------------------------------------------------------------------

void SAL_CALL TableModel::setFastPropertyValue( ::sal_Int32 /*nHandle*/, const Any& /*aValue*/ ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
{
}

// -----------------------------------------------------------------------------

Any SAL_CALL TableModel::getFastPropertyValue( ::sal_Int32 /*nHandle*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
{
    Any aAny;
    return aAny;
}

// -----------------------------------------------------------------------------
// internals
// -----------------------------------------------------------------------------

sal_Int32 TableModel::getRowCountImpl() const
{
    return static_cast< sal_Int32 >( maRows.size() );
}

// -----------------------------------------------------------------------------

sal_Int32 TableModel::getColumnCountImpl() const
{
    return static_cast< sal_Int32 >( maColumns.size() );
}

// -----------------------------------------------------------------------------

void TableModel::disposing()
{
    if( !maRows.empty() )
    {
        RowVector::iterator aIter( maRows.begin() );
        while( aIter != maRows.end() )
            (*aIter++)->dispose();
        RowVector().swap(maRows);
    }

    if( !maColumns.empty() )
    {
        ColumnVector::iterator aIter( maColumns.begin() );
        while( aIter != maColumns.end() )
            (*aIter++)->dispose();
        ColumnVector().swap(maColumns);
    }

    if( mxTableColumns.is() )
    {
        mxTableColumns->dispose();
        mxTableColumns.clear();
    }

    if( mxTableRows.is() )
    {
        mxTableRows->dispose();
        mxTableRows.clear();
    }

    mpTableObj = 0;
}

// -----------------------------------------------------------------------------
// XBroadcaster
// -----------------------------------------------------------------------------

void TableModel::lockBroadcasts() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    ++mnNotifyLock;
}
// -----------------------------------------------------------------------------

void TableModel::unlockBroadcasts() throw (RuntimeException)
{
    OGuard aGuard( Application::GetSolarMutex() );
    --mnNotifyLock;
    if( mnNotifyLock <= 0 )
    {
        mnNotifyLock = 0;
        if( mbNotifyPending )
            notifyModification();
    }
}

// -----------------------------------------------------------------------------
#ifdef PLEASE_DEBUG_THE_TABLES
#include <stdio.h>
#endif

void TableModel::notifyModification()
{
    ::osl::MutexGuard guard( m_aMutex );
    if( (mnNotifyLock == 0) && mpTableObj && mpTableObj->GetModel() )
    {

#ifdef PLEASE_DEBUG_THE_TABLES
        FILE* file = fopen( "e:\\table.log","a+" );

        const sal_Int32 nColCount = getColumnCountImpl();
        const sal_Int32 nRowCount = getRowCountImpl();

        fprintf( file, "<table columns=\"%ld\" rows=\"%ld\">\n\r", nColCount, nRowCount );

        for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
        {
            fprintf( file, "<column this=\"%lx\"/>\n\r", maColumns[nCol].get() );
        }

        // first check merged cells before and inside the removed rows
        for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
        {
            fprintf( file, "<row this=\"%lx\">\n\r", maRows[nRow].get() );
            for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
            {
                CellRef xCell( getCell( nCol, nRow ) );
                fprintf( file, "<cell this=\"%lx\"", xCell.get() );

                sal_Int32 nRowSpan = xCell->getRowSpan();
                sal_Int32 nColSpan = xCell->getColumnSpan();
                sal_Bool bMerged = xCell->isMerged();

                if( nColSpan > 1 )
                    fprintf( file, " column-span=\"%ld\"", nColSpan );
                if( nRowSpan > 1 )
                    fprintf( file, " row-span=\"%ld\"", nRowSpan );

                if( bMerged )
                    fprintf( file, " merged=\"true\"" );

                fprintf( file, "/>" );
            }
            fprintf( file, "\n\r</row>\n\r" );
        }

        fprintf( file, "</table>\n\r" );
        fclose( file );
#endif
        mbNotifyPending = false;

        ::cppu::OInterfaceContainerHelper * pModifyListeners = rBHelper.getContainer( XModifyListener::static_type() );
        if( pModifyListeners )
        {
            EventObject aSource;
            aSource.Source = static_cast< ::cppu::OWeakObject* >(this);
            pModifyListeners->notifyEach( &XModifyListener::modified, aSource);
        }
    }
    else
    {
        mbNotifyPending = true;
    }
}

// -----------------------------------------------------------------------------

CellRef TableModel::getCell( sal_Int32 nCol, sal_Int32 nRow ) const
{
    if( ((nRow >= 0) && (nRow < getRowCountImpl())) && (nCol >= 0) && (nCol < getColumnCountImpl()) )
    {
        return maRows[nRow]->maCells[nCol];
    }
    else
    {
        CellRef xRet;
        return xRet;
    }
}

// -----------------------------------------------------------------------------
/*
bool TableModel::getCellPos( const CellRef& xCell, ::sal_Int32& rnCol, ::sal_Int32& rnRow ) const
{
    const sal_Int32 nRowCount = getRowCount();
    const sal_Int32 nColCount = getColumnCount();
    for( rnRow = 0; rnRow < nRowCount; rnRow++ )
    {
        for( rnCol = 0; rnCol < nColCount; rnCol++ )
        {
            if( maRows[rnRow]->maCells[rnCol] == xCell )
            {
                return true;
            }
        }
    }
    return false;
}
*/

// -----------------------------------------------------------------------------

CellRef TableModel::createCell()
{
    CellRef xCell;
    if( mpTableObj )
        mpTableObj->createCell( xCell );
    return xCell;
}

// -----------------------------------------------------------------------------

void TableModel::insertColumns( sal_Int32 nIndex, sal_Int32 nCount )
{
    if( nCount && mpTableObj )
    {
        try
        {
            SdrModel* pModel = mpTableObj->GetModel();

            TableModelNotifyGuard aGuard( this );
            nIndex = insert_range<ColumnVector,ColumnVector::iterator,TableColumnRef>( maColumns, nIndex, nCount );

            sal_Int32 nRows = getRowCountImpl();
            while( nRows-- )
                maRows[nRows]->insertColumns( nIndex, nCount );

            ColumnVector aNewColumns(nCount);
            for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
            {
                TableColumnRef xNewCol( new TableColumn( this, nIndex+nOffset ) );
                maColumns[nIndex+nOffset] = xNewCol;
                aNewColumns[nOffset] = xNewCol;
            }

            const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
            if( bUndo )
            {
                pModel->BegUndo( ImpGetResStr(STR_TABLE_INSCOL) );
                pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );

                TableModelRef xThis( this );

                nRows = getRowCountImpl();
                CellVector aNewCells( nCount * nRows );
                CellVector::iterator aCellIter( aNewCells.begin() );

                nRows = getRowCountImpl();
                for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
                {
                    for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
                        (*aCellIter++) = getCell( nIndex + nOffset, nRow );
                }

                pModel->AddUndo( new InsertColUndo( xThis, nIndex, aNewColumns, aNewCells ) );
            }

            const sal_Int32 nRowCount = getRowCountImpl();
            // check if cells merge over new columns
            for( sal_Int32 nCol = 0; nCol < nIndex; ++nCol )
            {
                for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
                {
                    CellRef xCell( getCell( nCol, nRow ) );
                    sal_Int32 nColSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getColumnSpan() : 1;
                    if( (nColSpan != 1) && ((nColSpan + nCol ) > nIndex) )
                    {
                        // cell merges over newly created columns, so add the new columns to the merged cell
                        const sal_Int32 nRowSpan = xCell->getRowSpan();
                        nColSpan += nCount;
                        if( bUndo )
                            xCell->AddUndo();
                        xCell->merge( nColSpan, nRowSpan );
                        // set newly inserted cells to merged state
                        for( sal_Int32 nColOffset = 0; nColOffset < nCount; ++nColOffset )
                        {
                            for( sal_Int32 nRowOffset = 0; nRowOffset < nRowSpan; ++nRowOffset )
                            {
                                CellRef xMergedCell( getCell( nIndex + nColOffset, nRow + nRowOffset ) );
                                if( xMergedCell.is() )
                                    xMergedCell->setMerged();
                            }
                        }
                    }
                }
            }

            if( bUndo )
                pModel->EndUndo();

        }
        catch( Exception& )
        {
            DBG_ERROR("sdr::table::TableModel::insertColumns(), exception caught!");
        }
        updateColumns();
        setModified(sal_True);
    }
}

// -----------------------------------------------------------------------------

void TableModel::removeColumns( sal_Int32 nIndex, sal_Int32 nCount )
{
    sal_Int32 nColCount = getColumnCountImpl();

    if( mpTableObj && nCount && (nIndex >= 0) && (nIndex < nColCount) )
    {
        try
        {
            TableModelNotifyGuard aGuard( this );

            // clip removed columns to columns actually avalaible
            if( (nIndex + nCount) > nColCount )
                nCount = nColCount - nIndex;

            sal_Int32 nRows = getRowCountImpl();

            SdrModel* pModel = mpTableObj->GetModel();

            const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
            if( bUndo  )
            {
                pModel->BegUndo( ImpGetResStr(STR_UNDO_COL_DELETE) );
                pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );

                TableModelRef xThis( this );
                ColumnVector aRemovedCols( nCount );
                sal_Int32 nOffset;
                for( nOffset = 0; nOffset < nCount; ++nOffset )
                {
                    aRemovedCols[nOffset] = maColumns[nIndex+nOffset];
                }

                CellVector aRemovedCells( nCount * nRows );
                CellVector::iterator aCellIter( aRemovedCells.begin() );
                for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
                {
                    for( nOffset = 0; nOffset < nCount; ++nOffset )
                        (*aCellIter++) = getCell( nIndex + nOffset, nRow );
                }

                pModel->AddUndo( new RemoveColUndo( xThis, nIndex, aRemovedCols, aRemovedCells ) );
            }

            // only rows before and inside the removed rows are considered
            nColCount = nIndex + nCount + 1;

            const sal_Int32 nRowCount = getRowCountImpl();

            // first check merged cells before and inside the removed rows
            for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
            {
                for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
                {
                    CellRef xCell( getCell( nCol, nRow ) );
                    sal_Int32 nColSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getColumnSpan() : 1;
                    if( nColSpan <= 1 )
                        continue;

                    if( nCol >= nIndex )
                    {
                        // current cell is inside the removed columns
                        if( (nCol + nColSpan) > ( nIndex + nCount ) )
                        {
                            // current cells merges with columns after the removed columns
                            const sal_Int32 nRemove = nCount - nCol + nIndex;

                            CellRef xTargetCell( getCell( nIndex + nCount, nRow ) );
                            if( xTargetCell.is() )
                            {
                                if( bUndo )
                                    xTargetCell->AddUndo();
                                xTargetCell->merge( nColSpan - nRemove, xCell->getRowSpan() );
                                xTargetCell->replaceContentAndFormating( xCell );
                            }
                        }
                    }
                    else if( nColSpan > (nIndex - nCol) )
                    {
                        // current cells spans inside the removed columns, so adjust
                        const sal_Int32 nRemove = ::std::min( nCount, nCol + nColSpan - nIndex );
                        if( bUndo )
                            xCell->AddUndo();
                        xCell->merge( nColSpan - nRemove, xCell->getRowSpan() );
                    }
                }
            }

            // now remove the columns
            remove_range<ColumnVector,ColumnVector::iterator>( maColumns, nIndex, nCount );
            while( nRows-- )
                maRows[nRows]->removeColumns( nIndex, nCount );

            if( bUndo )
                pModel->EndUndo();
        }
        catch( Exception& )
        {
            DBG_ERROR("sdr::table::TableModel::removeColumns(), exception caught!");
        }

        updateColumns();
        setModified(sal_True);
    }
}

// -----------------------------------------------------------------------------

void TableModel::insertRows( sal_Int32 nIndex, sal_Int32 nCount )
{
    if( nCount && mpTableObj )
    {
        SdrModel* pModel = mpTableObj->GetModel();
        const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
        try
        {
            TableModelNotifyGuard aGuard( this );

            nIndex = insert_range<RowVector,RowVector::iterator,TableRowRef>( maRows, nIndex, nCount );

            RowVector aNewRows(nCount);
            const sal_Int32 nColCount = getColumnCountImpl();
            for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
            {
                TableRowRef xNewRow( new TableRow( this, nIndex+nOffset, nColCount ) );
                maRows[nIndex+nOffset] = xNewRow;
                aNewRows[nOffset] = xNewRow;
            }

            if( bUndo )
            {
                pModel->BegUndo( ImpGetResStr(STR_TABLE_INSROW) );
                pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );
                TableModelRef xThis( this );
                pModel->AddUndo( new InsertRowUndo( xThis, nIndex, aNewRows ) );
            }

            // check if cells merge over new columns
            for( sal_Int32 nRow = 0; nRow < nIndex; ++nRow )
            {
                for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
                {
                    CellRef xCell( getCell( nCol, nRow ) );
                    sal_Int32 nRowSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getRowSpan() : 1;
                    if( (nRowSpan > 1) && ((nRowSpan + nRow) > nIndex) )
                    {
                        // cell merges over newly created columns, so add the new columns to the merged cell
                        const sal_Int32 nColSpan = xCell->getColumnSpan();
                        nRowSpan += nCount;
                        if( bUndo )
                            xCell->AddUndo();
                        xCell->merge( nColSpan, nRowSpan );

                        // set newly inserted cells to merged state
                        for( sal_Int32 nColOffset = 1; nColOffset < nColSpan; ++nColOffset )
                        {
                            for( sal_Int32 nRowOffset = 0; nRowOffset <= nCount; ++nRowOffset )
                            {
                                CellRef xMergedCell( getCell( nCol + nColOffset - 1, nIndex + nRowOffset ) );
                                if( xMergedCell.is() )
                                    xMergedCell->setMerged();
                            }
                        }
                    }
                }
            }
        }
        catch( Exception& )
        {
            DBG_ERROR("sdr::table::TableModel::insertRows(), exception caught!");
        }
        if( bUndo )
            pModel->EndUndo();

        updateRows();
        setModified(sal_True);
    }
}

// -----------------------------------------------------------------------------

void TableModel::removeRows( sal_Int32 nIndex, sal_Int32 nCount )
{
    sal_Int32 nRowCount = getRowCountImpl();

    if( mpTableObj && nCount && (nIndex >= 0) && (nIndex < nRowCount) )
    {
        SdrModel* pModel = mpTableObj->GetModel();
        const bool bUndo = pModel && mpTableObj->IsInserted()&& pModel->IsUndoEnabled();

        try
        {
            TableModelNotifyGuard aGuard( this );

            // clip removed rows to rows actually avalaible
            if( (nIndex + nCount) > nRowCount )
                nCount = nRowCount - nIndex;

            if( bUndo )
            {
                pModel->BegUndo( ImpGetResStr(STR_UNDO_ROW_DELETE) );
                pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );

                TableModelRef xThis( this );

                RowVector aRemovedRows( nCount );
                for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
                    aRemovedRows[nOffset] = maRows[nIndex+nOffset];

                pModel->AddUndo( new RemoveRowUndo( xThis, nIndex, aRemovedRows ) );
            }

            // only rows before and inside the removed rows are considered
            nRowCount = nIndex + nCount + 1;

            const sal_Int32 nColCount = getColumnCountImpl();

            // first check merged cells before and inside the removed rows
            for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
            {
                for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
                {
                    CellRef xCell( getCell( nCol, nRow ) );
                    sal_Int32 nRowSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getRowSpan() : 1;
                    if( nRowSpan <= 1 )
                        continue;

                    if( nRow >= nIndex )
                    {
                        // current cell is inside the removed rows
                        if( (nRow + nRowSpan) > (nIndex + nCount) )
                        {
                            // current cells merges with rows after the removed rows
                            const sal_Int32 nRemove = nCount - nRow + nIndex;

                            CellRef xTargetCell( getCell( nCol, nIndex + nCount ) );
                            if( xTargetCell.is() )
                            {
                                if( bUndo )
                                    xTargetCell->AddUndo();
                                xTargetCell->merge( xCell->getColumnSpan(), nRowSpan - nRemove );
                                xTargetCell->replaceContentAndFormating( xCell );
                            }
                        }
                    }
                    else if( nRowSpan > (nIndex - nRow) )
                    {
                        // current cells spans inside the removed rows, so adjust
                        const sal_Int32 nRemove = ::std::min( nCount, nRow + nRowSpan - nIndex );
                        if( bUndo )
                            xCell->AddUndo();
                        xCell->merge( xCell->getColumnSpan(), nRowSpan - nRemove );
                    }
                }
            }

            // now remove the rows
            remove_range<RowVector,RowVector::iterator>( maRows, nIndex, nCount );

            if( bUndo )
                pModel->EndUndo();
        }
        catch( Exception& )
        {
            DBG_ERROR("sdr::table::TableModel::removeRows(), exception caught!");
        }

        updateRows();
        setModified(sal_True);
    }
}

// -----------------------------------------------------------------------------

TableRowRef TableModel::getRow( sal_Int32 nRow ) const throw (IndexOutOfBoundsException)
{
    if( (nRow >= 0) && (nRow < getRowCountImpl()) )
        return maRows[nRow];

    throw IndexOutOfBoundsException();
}

// -----------------------------------------------------------------------------

TableColumnRef TableModel::getColumn( sal_Int32 nColumn ) const throw (IndexOutOfBoundsException)
{
    if( (nColumn >= 0) && (nColumn < getColumnCountImpl()) )
        return maColumns[nColumn];

    throw IndexOutOfBoundsException();
}

// -----------------------------------------------------------------------------

/** deletes rows and columns that are completly merged. Must be called between BegUndo/EndUndo! */
void TableModel::optimize()
{
    TableModelNotifyGuard aGuard( this );

    bool bWasModified = false;

    if( !maRows.empty() && !maColumns.empty() )
    {
        sal_Int32 nCol = getColumnCountImpl() - 1;
        while( nCol > 0 )
        {
            bool bEmpty = true;
            for( sal_Int32 nRow = 0; (nRow < getRowCountImpl()) && bEmpty; nRow++ )
            {
                Reference< XMergeableCell > xCell( getCellByPosition( nCol, nRow ), UNO_QUERY );
                if( xCell.is() && !xCell->isMerged() )
                    bEmpty = false;
            }

            if( bEmpty )
            {
                if( nCol > 0 ) try
                {
                    const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM("Width") );
                    sal_Int32 nWidth1 = 0, nWidth2 = 0;
                    Reference< XPropertySet > xSet1( static_cast< XCellRange* >( maColumns[nCol].get() ), UNO_QUERY_THROW );
                    Reference< XPropertySet > xSet2( static_cast< XCellRange* >( maColumns[nCol-1].get() ), UNO_QUERY_THROW );
                    xSet1->getPropertyValue( sWidth ) >>= nWidth1;
                    xSet2->getPropertyValue( sWidth ) >>= nWidth2;
                    nWidth1 += nWidth2;
                    xSet2->setPropertyValue( sWidth, Any( nWidth1 ) );
                }
                catch( Exception& e )
                {
                    (void)e;
                    DBG_ERROR("svx::TableModel::optimize(), exception caught!");
                }

                removeColumns( nCol, 1 );
                bWasModified = true;
            }

            nCol--;
        }

        sal_Int32 nRow = getRowCountImpl() - 1;
        while( nRow > 0 )
        {
            bool bEmpty = true;
            for( nCol = 0; (nCol < getColumnCountImpl()) && bEmpty; nCol++ )
            {
                Reference< XMergeableCell > xCell( getCellByPosition( nCol, nRow ), UNO_QUERY );
                if( xCell.is() && !xCell->isMerged() )
                    bEmpty = false;
            }

            if( bEmpty )
            {
                if( nRow > 0 ) try
                {
                    const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM("Height") );
                    sal_Int32 nHeight1 = 0, nHeight2 = 0;
                    Reference< XPropertySet > xSet1( static_cast< XCellRange* >( maRows[nRow].get() ), UNO_QUERY_THROW );
                    Reference< XPropertySet > xSet2( static_cast< XCellRange* >( maRows[nRow-1].get() ), UNO_QUERY_THROW );
                    xSet1->getPropertyValue( sHeight ) >>= nHeight1;
                    xSet2->getPropertyValue( sHeight ) >>= nHeight2;
                    nHeight1 += nHeight2;
                    xSet2->setPropertyValue( sHeight, Any( nHeight1 ) );
                }
                catch( Exception& e )
                {
                    (void)e;
                    DBG_ERROR("svx::TableModel::optimize(), exception caught!");
                }

                removeRows( nRow, 1 );
                bWasModified = true;
            }

            nRow--;
        }
    }
    if( bWasModified )
        setModified(sal_True);
}

// -----------------------------------------------------------------------------

void TableModel::updateRows()
{
    sal_Int32 nRow = 0;
    RowVector::iterator iter = maRows.begin();
    while( iter != maRows.end() )
    {
        (*iter++)->mnRow = nRow++;
    }
}

// -----------------------------------------------------------------------------

void TableModel::updateColumns()
{
    sal_Int32 nColumn = 0;
    ColumnVector::iterator iter = maColumns.begin();
    while( iter != maColumns.end() )
    {
        (*iter++)->mnColumn = nColumn++;
    }
}

// -----------------------------------------------------------------------------

} }