summaryrefslogtreecommitdiff
path: root/sw/source/core/undo/docundo.cxx
blob: 71b4f0926e7ced453c66c39a9d8d4af99bc2f236 (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * 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_sw.hxx"

#include <svx/svdmodel.hxx>

#include <vcl/wrkwin.hxx>
#include <doc.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <swundo.hxx>       // fuer die UndoIds
#include <undobj.hxx>
#include <rolbck.hxx>
#include <docary.hxx>
#ifndef _UNDO_HRC
#include <undo.hrc>
#endif


using namespace ::com::sun::star;


sal_uInt16 SwDoc::nUndoActions = UNDO_ACTION_COUNT;     // anzahl von Undo-Action

// the undo array should never grow beyond this limit:
#define UNDO_ACTION_LIMIT (USHRT_MAX - 1000)


SV_IMPL_PTRARR( SwUndoIds, SwUndoIdAndNamePtr )

//#define _SHOW_UNDORANGE
#ifdef _SHOW_UNDORANGE


class UndoArrStatus : public WorkWindow
{
    sal_uInt16 nUndo, nUndoNds;
    virtual void Paint( const Rectangle& );
public:
    UndoArrStatus();
    void Set( sal_uInt16, sal_uInt16 );
};
static UndoArrStatus* pUndoMsgWin = 0;


UndoArrStatus::UndoArrStatus()
    : WorkWindow( APP_GETAPPWINDOW() ), nUndo(0), nUndoNds(0)
{
    SetSizePixel( Size( 200, 100 ));
    SetFont( Font( "Courier", Size( 0, 10 )) );
    Show();
}


void UndoArrStatus::Set( sal_uInt16 n1, sal_uInt16 n2 )
{
    nUndo = n1; nUndoNds = n2;
    Invalidate();
}


void UndoArrStatus::Paint( const Rectangle& )
{
    String s;
    DrawRect( Rectangle( Point(0,0), GetOutputSize() ));
    ( s = "Undos: " ) += nUndo;
    DrawText( Point( 0, 0 ), s );
    ( s = "UndoNodes: " ) += nUndoNds;
    DrawText( Point( 0, 15 ), s );
}

#endif

void SwDoc::SetUndoNoResetModified()
{
    nUndoSavePos = USHRT_MAX;
}

bool SwDoc::IsUndoNoResetModified() const
{
    return USHRT_MAX == nUndoSavePos;
}

void SwDoc::DoUndo(bool bUn)
{
    mbUndo = bUn;

    SdrModel* pSdrModel = GetDrawModel();
    if( pSdrModel )
        pSdrModel->EnableUndo(bUn);
}

bool SwDoc::DoesUndo() const
{
    return mbUndo;
}

void SwDoc::DoGroupUndo(bool bUn)
{
    mbGroupUndo = bUn;
}

bool SwDoc::DoesGroupUndo() const
{
    return mbGroupUndo;
}

sal_uInt16 SwDoc::GetUndoActionCount()
{
    return nUndoActions;
}

void SwDoc::SetUndoActionCount( sal_uInt16 nNew )
{
    nUndoActions = nNew;
}

const SwNodes* SwDoc::GetUndoNds() const
{
    return &aUndoNodes;
}

void SwDoc::AppendUndo( SwUndo* pUndo )
{
    if( nsRedlineMode_t::REDLINE_NONE == pUndo->GetRedlineMode() )
        pUndo->SetRedlineMode( GetRedlineMode() );

    // Unfortunately, the silly SvPtrArr can only store a little less than
    // USHRT_MAX elements. Of course it doesn't see any necessity for asserting
    // or even doing error handling. pUndos should definitely be replaced by an
    // STL container that doesn't have this problem. cf #95884#
    DBG_ASSERT( pUndos->Count() < USHRT_MAX - 16,
                "Writer will crash soon. I apologize for the inconvenience." );

    pUndos->Insert( pUndo, nUndoPos );
    ++nUndoPos;
    switch( pUndo->GetId() )
    {
    case UNDO_START:        ++nUndoSttEnd;
                            break;

    case UNDO_END:          ASSERT( nUndoSttEnd, "Undo-Ende ohne Start" );
                            --nUndoSttEnd;
                            // kein break !!!
    default:
        if( pUndos->Count() != nUndoPos && UNDO_END != pUndo->GetId() )
            ClearRedo();
        else {
            ASSERT( pUndos->Count() == nUndoPos || UNDO_END == pUndo->GetId(),
                    "Redo history not deleted!" );
        }
        if( !nUndoSttEnd )
            ++nUndoCnt;
        break;
    }

#ifdef _SHOW_UNDORANGE
    // zur Anzeige der aktuellen Undo-Groessen
    if( !pUndoMsgWin )
            pUndoMsgWin = new UndoArrStatus;
    pUndoMsgWin->Set( pUndos->Count(), aUndoNodes.Count() );
#endif

    // noch eine offene Klammerung, kann man sich den Rest schenken
    if( nUndoSttEnd )
        return;

    // folgende Array-Grenzen muessen ueberwacht werden:
    //  - Undo,             Grenze: fester Wert oder USHRT_MAX - 1000
    //  - UndoNodes,        Grenze:  USHRT_MAX - 1000
    //  - AttrHistory       Grenze:  USHRT_MAX - 1000
    // (defined in UNDO_ACTION_LIMIT at the top of this file)

    sal_uInt16 nEnde = UNDO_ACTION_LIMIT;

// nur zum Testen der neuen DOC-Member
#ifdef DBG_UTIL
{
    SwUndoId nId = UNDO_EMPTY;
    sal_uInt16 nUndosCnt = 0, nSttEndCnt = 0;
    for( sal_uInt16 nCnt = 0; nCnt < nUndoPos; ++nCnt )
    {
        if( UNDO_START == ( nId = (*pUndos)[ nCnt ]->GetId()) )
            ++nSttEndCnt;
        else if( UNDO_END == nId )
            --nSttEndCnt;
        if( !nSttEndCnt )
            ++nUndosCnt;
    }
    ASSERT( nSttEndCnt == nUndoSttEnd, "Start-Ende Count ungleich" );
    ASSERT( nUndosCnt == nUndoCnt, "Undo Count ungleich" );
}
#endif

    if( SwDoc::nUndoActions < nUndoCnt )
        // immer 1/10 loeschen
        //JP 23.09.95: oder wenn neu eingestellt wurde um die Differenz
        //JP 29.5.2001: Task #83891#: remove only the overlapping actions
        DelUndoObj( nUndoCnt - SwDoc::nUndoActions );
    else
    {
        sal_uInt16 nUndosCnt = nUndoCnt;
            // immer 1/10 loeschen bis der "Ausloeser" behoben ist
        while( aUndoNodes.Count() && nEnde < aUndoNodes.Count() )
            DelUndoObj( nUndosCnt / 10 );
    }
}



void SwDoc::ClearRedo()
{
    if( DoesUndo() && nUndoPos != pUndos->Count() )
    {
//?? why ??     if( !nUndoSttEnd )
        {
            // setze UndoCnt auf den neuen Wert
            SwUndo* pUndo;
            for( sal_uInt16 nCnt = pUndos->Count(); nUndoPos < nCnt; --nUndoCnt )
                // Klammerung ueberspringen
                if( UNDO_END == (pUndo = (*pUndos)[ --nCnt ])->GetId() )
                    nCnt = nCnt - ((SwUndoEnd*)pUndo)->GetSttOffset();
        }

        // loesche die Undo-Aktionen (immer von hinten !)
        pUndos->DeleteAndDestroy( nUndoPos, pUndos->Count() - nUndoPos );
    }
}


    // loescht die gesamten UndoObjecte
void SwDoc::DelAllUndoObj()
{
    ClearRedo();

    DoUndo( sal_False );

    // Offene Undo-Klammerungen erhalten !!
    SwUndo* pUndo;
    sal_uInt16 nSize = pUndos->Count();
    while( nSize )
        if( UNDO_START != ( pUndo = (*pUndos)[ --nSize ] )->GetId() ||
            ((SwUndoStart*)pUndo)->GetEndOffset() )
            // keine offenen Gruppierung ?
            pUndos->DeleteAndDestroy( nSize, 1 );

    nUndoCnt = 0;
    nUndoPos = pUndos->Count();

/*
    while( nUndoPos )
        aUndos.DelDtor( --nUndoPos, 1 );
    nUndoCnt = nUndoSttEnd = nUndoPos = 0;
*/
    nUndoSavePos = USHRT_MAX;
    DoUndo( sal_True );
}


    // loescht alle UndoObjecte vom Anfang bis zum angegebenen Ende
sal_Bool SwDoc::DelUndoObj( sal_uInt16 nEnde )
{
    if( !nEnde )                    // sollte mal 0 uebergeben werden,
    {
        if( !pUndos->Count() )
            return sal_False;
        ++nEnde;                    // dann korrigiere es auf 1
    }

    DoUndo( sal_False );

    // pruefe erstmal, wo das Ende steht
    SwUndoId nId = UNDO_EMPTY;
    sal_uInt16 nSttEndCnt = 0;
    sal_uInt16 nCnt;

    for( nCnt = 0; nEnde && nCnt < nUndoPos; ++nCnt )
    {
        if( UNDO_START == ( nId = (*pUndos)[ nCnt ]->GetId() ))
            ++nSttEndCnt;
        else if( UNDO_END == nId )
            --nSttEndCnt;
        if( !nSttEndCnt )
            --nEnde, --nUndoCnt;
    }

    ASSERT( nCnt < nUndoPos || nUndoPos == pUndos->Count(),
            "Undo-Del-Ende liegt in einer Redo-Aktion" );

    // dann setze ab Ende bis Undo-Ende bei allen Undo-Objecte die Werte um
    nSttEndCnt = nCnt;          // Position merken
    if( nUndoSavePos < nSttEndCnt )     // SavePos wird aufgegeben
        nUndoSavePos = USHRT_MAX;
    else if( nUndoSavePos != USHRT_MAX )
        nUndoSavePos = nUndoSavePos - nSttEndCnt;

    while( nSttEndCnt )
        pUndos->DeleteAndDestroy( --nSttEndCnt, 1 );
    nUndoPos = pUndos->Count();

    DoUndo( sal_True );
    return sal_True;
}

/**************** UNDO ******************/

void SwDoc::setUndoNoModifiedPosition( SwUndoNoModifiedPosition nNew )
{
    nUndoSavePos = nNew;
    if( !pUndos->Count() || nUndoSavePos > pUndos->Count() - 1 )
        nUndoSavePos = USHRT_MAX;
}

SwUndoNoModifiedPosition SwDoc::getUndoNoModifiedPosition() const
{
    return nUndoSavePos;
}


bool SwDoc::HasUndoId(SwUndoId eId) const
{
    sal_uInt16 nSize = nUndoPos;
    SwUndo * pUndo;
    while( nSize-- )
        if( ( pUndo = (*pUndos)[nSize])->GetId() == eId ||
            ( UNDO_START == pUndo->GetId() &&
                ((SwUndoStart*)pUndo)->GetUserId() == eId )
            || ( UNDO_END == pUndo->GetId() &&
                ((SwUndoEnd*)pUndo)->GetUserId() == eId ) )
        {
            return sal_True;
        }

    return sal_False;
}


bool SwDoc::Undo( SwUndoIter& rUndoIter )
{
    if ( (rUndoIter.GetId()!=0) && (!HasUndoId(rUndoIter.GetId())) )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }
    if( !nUndoPos )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }

    SwUndo *pUndo = (*pUndos)[ --nUndoPos ];

    RedlineMode_t eOld = GetRedlineMode();
    RedlineMode_t eTmpMode = (RedlineMode_t)pUndo->GetRedlineMode();
    if( (nsRedlineMode_t::REDLINE_SHOW_MASK & eTmpMode) != (nsRedlineMode_t::REDLINE_SHOW_MASK & eOld) &&
        UNDO_START != pUndo->GetId() && UNDO_END != pUndo->GetId() )
        SetRedlineMode( eTmpMode );

    SetRedlineMode_intern((RedlineMode_t)(eTmpMode | nsRedlineMode_t::REDLINE_IGNORE));
    // Undo ausfuehren

    // zum spaeteren ueberpruefen
    SwUndoId nAktId = pUndo->GetId();
    //JP 11.05.98: FlyFormate ueber die EditShell selektieren, nicht aus dem
    //              Undo heraus
    switch( nAktId )
    {
    case UNDO_START:
    case UNDO_END:
    case UNDO_INSDRAWFMT:
        break;

    default:
        rUndoIter.ClearSelections();
    }

    pUndo->Undo( rUndoIter );

    SetRedlineMode( eOld );

    // Besonderheit von Undo-Replace (interne History)
    if( UNDO_REPLACE == nAktId && ((SwUndoReplace*)pUndo)->nAktPos )
    {
        ++nUndoPos;
        return sal_True;
    }

    // Objekt aus History entfernen und zerstoeren
    if( nUndoPos && !rUndoIter.bWeiter &&
        UNDO_START == ( pUndo = (*pUndos)[ nUndoPos-1 ] )->GetId() )
        --nUndoPos;

    // JP 29.10.96: Start und End setzen kein Modify-Flag.
    //              Sonst gibt es Probleme mit der autom. Aufnahme von Ausnahmen
    //              bei der Autokorrektur
    if( UNDO_START != nAktId && UNDO_END != nAktId )
        SetModified();      // default: immer setzen, kann zurueck gesetzt werden

    // ist die History leer und wurde nicht wegen Speichermangel
    // verworfen, so kann das Dokument als unveraendert gelten
    if( nUndoSavePos == nUndoPos )
        ResetModified();

    return sal_True;
}


// setzt Undoklammerung auf, liefert nUndoId der Klammerung


SwUndoId SwDoc::StartUndo( SwUndoId eUndoId, const SwRewriter * pRewriter )
{
    if( !mbUndo )
        return UNDO_EMPTY;

    if( !eUndoId )
        eUndoId = UNDO_START;

    SwUndoStart * pUndo = new SwUndoStart( eUndoId );

    if (pRewriter)
        pUndo->SetRewriter(*pRewriter);

    AppendUndo(pUndo);

    return eUndoId;
}
// schliesst Klammerung der nUndoId, nicht vom UI benutzt


SwUndoId SwDoc::EndUndo(SwUndoId eUndoId, const SwRewriter * pRewriter)
{
    sal_uInt16 nSize = nUndoPos;
    if( !mbUndo || !nSize-- )
        return UNDO_EMPTY;

    if( UNDO_START == eUndoId || !eUndoId )
        eUndoId = UNDO_END;

    SwUndo* pUndo = (*pUndos)[ nSize ];
    if( UNDO_START == pUndo->GetId() )
    {
        // leere Start/End-Klammerung ??
        pUndos->DeleteAndDestroy( nSize );
        --nUndoPos;
        --nUndoSttEnd;
        return UNDO_EMPTY;
    }

    // exist above any redo objects? If yes, delete them
    if( nUndoPos != pUndos->Count() )
    {
        // setze UndoCnt auf den neuen Wert
        for( sal_uInt16 nCnt = pUndos->Count(); nUndoPos < nCnt; --nUndoCnt )
            // Klammerung ueberspringen
            if( UNDO_END == (pUndo = (*pUndos)[ --nCnt ])->GetId() )
                nCnt = nCnt - ((SwUndoEnd*)pUndo)->GetSttOffset();

        pUndos->DeleteAndDestroy( nUndoPos, pUndos->Count() - nUndoPos );
    }

    // suche den Anfang dieser Klammerung
    SwUndoId nId = UNDO_EMPTY;
    while( nSize )
        if( UNDO_START == ( nId = (pUndo = (*pUndos)[ --nSize ] )->GetId()) &&
            !((SwUndoStart*)pUndo)->GetEndOffset() )
            break;      // Start gefunden

    if( nId != UNDO_START )
    {
        // kann eigentlich nur beim Abspielen von Macros passieren, die
        // Undo/Redo/Repeat benutzen und die eine exitierende Selection
        // durch Einfuegen loeschen
        ASSERT( !this, "kein entsprechendes Ende gefunden" );
        // kein entsprechenden Start gefunden -> Ende nicht einfuegen
        // und die Member am Doc updaten

        nUndoSttEnd = 0;
        nUndoCnt = 0;
        // setze UndoCnt auf den neuen Wert
        SwUndo* pTmpUndo;
        for( sal_uInt16 nCnt = 0; nCnt < pUndos->Count(); ++nCnt, ++nUndoCnt )
            // Klammerung ueberspringen
            if( UNDO_START == (pTmpUndo = (*pUndos)[ nCnt ])->GetId() )
                nCnt = nCnt + ((SwUndoStart*)pTmpUndo)->GetEndOffset();
        return UNDO_EMPTY;

    }

    // Klammerung um eine einzelne Action muss nicht sein!
    // Aussnahme: es ist eine eigene ID definiert
    if(  2 == pUndos->Count() - nSize &&
        (UNDO_END == eUndoId || eUndoId == (*pUndos)[ nSize+1 ]->GetId() ))
    {
        pUndos->DeleteAndDestroy( nSize );
        nUndoPos = pUndos->Count();
        if( !--nUndoSttEnd )
        {
            ++nUndoCnt;
            if( SwDoc::nUndoActions < nUndoCnt )
                // immer 1/10 loeschen
                //JP 23.09.95: oder wenn neu eingestellt wurde um die Differenz
                //JP 29.5.2001: Task #83891#: remove only the overlapping actions
                DelUndoObj( nUndoCnt - SwDoc::nUndoActions );
            else
            {
                sal_uInt16 nEnde = USHRT_MAX - 1000;
                sal_uInt16 nUndosCnt = nUndoCnt;
                    // immer 1/10 loeschen bis der "Ausloeser" behoben ist
                while( aUndoNodes.Count() && nEnde < aUndoNodes.Count() )
                    DelUndoObj( nUndosCnt / 10 );
            }
        }
        return eUndoId;
    }

    // setze die Klammerung am Start/End-Undo
    nSize = pUndos->Count() - nSize;
    ((SwUndoStart*)pUndo)->SetEndOffset( nSize );

    SwUndoEnd* pUndoEnd = new SwUndoEnd( eUndoId );
    pUndoEnd->SetSttOffset( nSize );

// nur zum Testen der Start/End-Verpointerung vom Start/End Undo
#ifdef DBG_UTIL
    {
        sal_uInt16 nEndCnt = 1, nCnt = pUndos->Count();
        SwUndoId nTmpId = UNDO_EMPTY;
        while( nCnt )
        {
            if( UNDO_START == ( nTmpId = (*pUndos)[ --nCnt ]->GetId()) )
            {
                if( !nEndCnt ) // falls mal ein Start ohne Ende vorhanden ist
                    continue;
                --nEndCnt;
                if( !nEndCnt )      // hier ist der Anfang
                    break;
            }
            else if( UNDO_END == nTmpId )
                ++nEndCnt;
            else if( !nEndCnt )
                break;
        }
        ASSERT( nCnt == pUndos->Count() - nSize,
                "Start-Ende falsch geklammert" );
    }
#endif

    if (pRewriter)
    {
        ((SwUndoStart *) pUndo)->SetRewriter(*pRewriter);
        pUndoEnd->SetRewriter(*pRewriter);
    }
    else
        pUndoEnd->SetRewriter(((SwUndoStart *) pUndo)->GetRewriter());

    AppendUndo( pUndoEnd );
    return eUndoId;
}

// liefert die Id der letzten Undofaehigen Aktion zurueck oder 0
// fuellt ggf. VARARR mit User-UndoIds

String SwDoc::GetUndoIdsStr( String* pStr, SwUndoIds *pUndoIds) const
{
    String aTmpStr;

    if (pStr != NULL)
    {
        GetUndoIds( pStr, pUndoIds);
        aTmpStr = *pStr;
    }
    else
        GetUndoIds( &aTmpStr, pUndoIds);

    return aTmpStr;
}

/*-- 24.11.2004 16:11:21---------------------------------------------------

  -----------------------------------------------------------------------*/
sal_Bool SwDoc::RestoreInvisibleContent()
{
    sal_Bool bRet = sal_False;
    if(nUndoPos > 0 )
    {
        SwUndo * pUndo = (*pUndos)[ nUndoPos - 1 ];
        if( ( pUndo->GetId() == UNDO_END &&
            static_cast<SwUndoEnd *>(pUndo)->GetUserId() == UNDO_UI_DELETE_INVISIBLECNTNT) )
        {
            SwPaM aPam( GetNodes().GetEndOfPostIts() );
            SwUndoIter aUndoIter( &aPam );
            do
            {
                Undo( aUndoIter );
            }
            while ( aUndoIter.IsNextUndo() );
            ClearRedo();
            bRet = sal_True;
        }
    }
    return bRet;
}


/**
   Returns id and comment for a certain undo object in an undo stack.

   Remark: In the following the object type referred to is always the
   effective object type. If an UNDO_START or UNDO_END has a user type
   it is referred to as this type.

   If the queried object is an UNDO_END and has no user id the result
   is taken from the first object that is not an UNDO_END nor an
   UNDO_START preceeding the queried object.

   If the queried object is an UNDO_START and has no user id the
   result is taken from the first object that is not an UNDO_END nor
   an UNDO_START preceeding the UNDO_END object belonging to the
   queried object.

   In all other cases the result is taken from the queried object.

   @param rUndos           the undo stack
   @param nPos             position of the undo object to query

   @return SwUndoIdAndName object containing the query result
 */
SwUndoIdAndName * lcl_GetUndoIdAndName(const SwUndos & rUndos, sal_uInt16 nPos )
{
    SwUndo * pUndo = rUndos[ nPos ];
    SwUndoId nId = UNDO_EMPTY;
    String sStr("??", RTL_TEXTENCODING_ASCII_US);

    ASSERT( nPos < rUndos.Count(), "nPos out of range");

    switch (pUndo->GetId())
    {
    case UNDO_START:
        {
            SwUndoStart * pUndoStart = (SwUndoStart *) pUndo;
            nId = pUndoStart->GetUserId();

            if (nId <= UNDO_END)
            {
                /**
                   Start at the according UNDO_END.  Search backwards
                   for first objects that is not a UNDO_END.
                 */
                int nTmpPos = nPos + pUndoStart->GetEndOffset();
                int nSubstitute = -1;

                // --> OD 2009-09-30 #i105457#
                if ( nTmpPos > 0 )
                // <--
                {
                    SwUndo * pTmpUndo;
                    do
                    {
                        nTmpPos--;
                        pTmpUndo = rUndos[ static_cast<sal_uInt16>(nTmpPos) ];

                        if (pTmpUndo->GetEffectiveId() > UNDO_END)
                            nSubstitute = nTmpPos;
                    }
                    while (nSubstitute < 0 && nTmpPos > nPos);

                    if (nSubstitute >= 0)
                    {
                        SwUndo * pSubUndo = rUndos[ static_cast<sal_uInt16>(nSubstitute) ];
                        nId = pSubUndo->GetEffectiveId();
                        sStr = pSubUndo->GetComment();
                    }
                }
            }
            else
                sStr = pUndo->GetComment();
        }

        break;

    case UNDO_END:
        {
            SwUndoEnd * pUndoEnd = (SwUndoEnd *) pUndo;
            nId = pUndoEnd->GetUserId();

            if (nId <= UNDO_END)
            {
                /**
                   Start at this UNDO_END.  Search backwards
                   for first objects that is not a UNDO_END.
                 */

                int nTmpPos = nPos;
                int nUndoStart = nTmpPos - pUndoEnd->GetSttOffset();
                int nSubstitute = -1;

                if (nTmpPos > 0)
                {
                    SwUndo * pTmpUndo;

                    do
                    {
                        nTmpPos--;
                        pTmpUndo = rUndos[ static_cast<sal_uInt16>(nTmpPos) ];

                        if (pTmpUndo->GetEffectiveId() > UNDO_END)
                            nSubstitute = nTmpPos;
                    }
                    while (nSubstitute < 0 && nTmpPos > nUndoStart);

                    if (nSubstitute >= 0)
                    {
                        SwUndo * pSubUndo = rUndos[ static_cast<sal_uInt16>(nSubstitute) ];
                        nId = pSubUndo->GetEffectiveId();
                        sStr = pSubUndo->GetComment();
                    }
                }
            }
            else
                sStr = pUndo->GetComment();
        }

        break;

    default:
        nId = pUndo->GetId();
        sStr = pUndo->GetComment();
    }

    return new SwUndoIdAndName(nId, &sStr);
}

SwUndoId SwDoc::GetUndoIds( String* pStr, SwUndoIds *pUndoIds) const
{
    int nTmpPos = nUndoPos - 1;
    SwUndoId nId = UNDO_EMPTY;

    while (nTmpPos >= 0)
    {
        SwUndo * pUndo = (*pUndos)[ static_cast<sal_uInt16>(nTmpPos) ];

        SwUndoIdAndName * pIdAndName = lcl_GetUndoIdAndName( *pUndos, static_cast<sal_uInt16>(nTmpPos) );

        if (nTmpPos == nUndoPos - 1)
        {
            nId = pIdAndName->GetUndoId();

            if (pStr)
                *pStr = *pIdAndName->GetUndoStr();
        }

        if (pUndoIds)
            pUndoIds->Insert(pIdAndName, pUndoIds->Count());
        else
            break;

        if (pUndo->GetId() == UNDO_END)
            nTmpPos -= ((SwUndoEnd *) pUndo)->GetSttOffset();

        nTmpPos--;
    }

    return nId;
}

bool SwDoc::HasTooManyUndos() const
{
    // AppendUndo checks the UNDO_ACTION_LIMIT, unless there's a nested undo.
    // So HasTooManyUndos() may only occur when undos are nested; else
    // AppendUndo has some sort of bug.
    DBG_ASSERT( (nUndoSttEnd != 0) || (pUndos->Count() < UNDO_ACTION_LIMIT),
                "non-nested undos should have been handled in AppendUndo" );
    return (pUndos->Count() >= UNDO_ACTION_LIMIT);
}


/**************** REDO ******************/


bool SwDoc::Redo( SwUndoIter& rUndoIter )
{
    if( rUndoIter.GetId() && !HasUndoId( rUndoIter.GetId() ) )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }
    if( nUndoPos == pUndos->Count() )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }

    SwUndo *pUndo = (*pUndos)[ nUndoPos++ ];

    RedlineMode_t eOld = GetRedlineMode();
    RedlineMode_t eTmpMode = (RedlineMode_t)pUndo->GetRedlineMode();
    if( (nsRedlineMode_t::REDLINE_SHOW_MASK & eTmpMode) != (nsRedlineMode_t::REDLINE_SHOW_MASK & eOld) &&
        UNDO_START != pUndo->GetId() && UNDO_END != pUndo->GetId() )
        SetRedlineMode( eTmpMode );
    SetRedlineMode_intern( (RedlineMode_t)(eTmpMode | nsRedlineMode_t::REDLINE_IGNORE));

    //JP 11.05.98: FlyFormate ueber die EditShell selektieren, nicht aus dem
    //              Undo heraus
    if( UNDO_START != pUndo->GetId() && UNDO_END != pUndo->GetId() )
        rUndoIter.ClearSelections();

    pUndo->Redo( rUndoIter );

    SetRedlineMode( eOld );

    // Besonderheit von Undo-Replace (interne History)
    if( UNDO_REPLACE == pUndo->GetId() &&
        USHRT_MAX != ((SwUndoReplace*)pUndo)->nAktPos )
    {
        --nUndoPos;
        return sal_True;
    }

    if( rUndoIter.bWeiter && nUndoPos >= pUndos->Count() )
        rUndoIter.bWeiter = sal_False;

    // ist die History leer und wurde nicht wegen Speichermangel
    // verworfen, so kann das Dokument als unveraendert gelten
    if( nUndoSavePos == nUndoPos )
        ResetModified();
    else
        SetModified();
    return sal_True;
}


// liefert die Id der letzten Redofaehigen Aktion zurueck oder 0
// fuellt ggf. VARARR mit User-RedoIds

String SwDoc::GetRedoIdsStr( String* pStr, SwUndoIds *pRedoIds ) const
{
    String aTmpStr;

    if (pStr != NULL)
    {
        GetRedoIds( pStr, pRedoIds );
        aTmpStr = *pStr;
    }
    else
        GetRedoIds( &aTmpStr, pRedoIds );


    return aTmpStr;
}


SwUndoId SwDoc::GetRedoIds( String* pStr, SwUndoIds *pRedoIds ) const
{
    sal_uInt16 nTmpPos = nUndoPos;
    SwUndoId nId = UNDO_EMPTY;

    while (nTmpPos < pUndos->Count())
    {
        SwUndo * pUndo = (*pUndos)[nTmpPos];

        SwUndoIdAndName * pIdAndName = lcl_GetUndoIdAndName(*pUndos, nTmpPos);

        if (nTmpPos == nUndoPos)
        {
            nId = pIdAndName->GetUndoId();

            if (pStr)
                *pStr = *pIdAndName->GetUndoStr();
        }

        if (pRedoIds)
            pRedoIds->Insert(pIdAndName, pRedoIds->Count());
        else
            break;

        if (pUndo->GetId() == UNDO_START)
            nTmpPos = nTmpPos + ((SwUndoStart *) pUndo)->GetEndOffset();

        nTmpPos++;
    }

    return nId;
}

/**************** REPEAT ******************/


bool SwDoc::Repeat( SwUndoIter& rUndoIter, sal_uInt16 nRepeatCnt )
{
    if( rUndoIter.GetId() && !HasUndoId( rUndoIter.GetId() ) )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }
    sal_uInt16 nSize = nUndoPos;
    if( !nSize )
    {
        rUndoIter.bWeiter = sal_False;
        return sal_False;
    }

    // dann suche jetzt ueber die End/Start-Gruppen die gueltige Repeat-Aktion
    SwUndo *pUndo = (*pUndos)[ --nSize ];
    if( UNDO_END == pUndo->GetId() )
        nSize = nSize - ((SwUndoEnd*)pUndo)->GetSttOffset();

    sal_uInt16 nEndCnt = nUndoPos;
    sal_Bool bOneUndo = nSize + 1 == nUndoPos;

    SwPaM* pTmpCrsr = rUndoIter.pAktPam;
    SwUndoId nId = UNDO_EMPTY;

    if( pTmpCrsr != pTmpCrsr->GetNext() || !bOneUndo )  // Undo-Klammerung aufbauen
    {
        if (pUndo->GetId() == UNDO_END)
        {
            SwUndoStart * pStartUndo =
                (SwUndoStart *) (*pUndos)[nSize];

            nId = pStartUndo->GetUserId();
        }

        StartUndo( nId, NULL );
    }
    do {        // dann durchlaufe mal den gesamten Ring
        for( sal_uInt16 nRptCnt = nRepeatCnt; nRptCnt > 0; --nRptCnt )
        {
            rUndoIter.pLastUndoObj = 0;
            for( sal_uInt16 nCnt = nSize; nCnt < nEndCnt; ++nCnt )
                (*pUndos)[ nCnt ]->Repeat( rUndoIter );     // Repeat ausfuehren
        }
    } while( pTmpCrsr !=
        ( rUndoIter.pAktPam = (SwPaM*)rUndoIter.pAktPam->GetNext() ));
    if( pTmpCrsr != pTmpCrsr->GetNext() || !bOneUndo )
        EndUndo( nId, NULL );

    return sal_True;
}

// liefert die Id der letzten Repeatfaehigen Aktion zurueck oder 0
// fuellt ggf. VARARR mit User-RedoIds

String SwDoc::GetRepeatIdsStr(String* pStr, SwUndoIds *pRepeatIds) const
{
    String aTmpStr;
    SwUndoId nId;

    if ( pStr != NULL)
    {
        nId = GetRepeatIds(pStr, pRepeatIds);
        aTmpStr = *pStr;
    }
    else
        nId = GetRepeatIds(&aTmpStr, pRepeatIds);

    if (nId <= UNDO_END)
        return String();

    return aTmpStr;
}

SwUndoId SwDoc::GetRepeatIds(String* pStr, SwUndoIds *pRepeatIds) const
{
    SwUndoId nRepeatId = GetUndoIds( pStr, pRepeatIds );
    if( REPEAT_START <= nRepeatId && REPEAT_END > nRepeatId )
        return nRepeatId;
    return UNDO_EMPTY;
}


SwUndo* SwDoc::RemoveLastUndo( SwUndoId eUndoId )
{
    SwUndo* pUndo = (*pUndos)[ nUndoPos - 1 ];
    if( eUndoId == pUndo->GetId() && nUndoPos == pUndos->Count() )
    {
        if( !nUndoSttEnd )
            --nUndoCnt;
        --nUndoPos;
        pUndos->Remove( nUndoPos, 1 );
    }
    else
    {
        pUndo = 0;
        ASSERT( !this, "falsches Undo-Object" );
    }
    return pUndo;
}

SwUndoIdAndName::SwUndoIdAndName( SwUndoId nId, const String* pStr )
    : eUndoId( nId ), pUndoStr( pStr ? new String( *pStr ) : 0 )
{
}

SwUndoIdAndName::~SwUndoIdAndName()
{
    delete pUndoStr;
}