summaryrefslogtreecommitdiff
path: root/sw/source/ui/docvw/PostItMgr.cxx
blob: 317bdfad69196efa96a728c231e5a66cd0a16d80 (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
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
/*************************************************************************
 *
 * 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: PostItMgr.cxx,v $
 *
 * $Revision: 1.6 $
 *
 * 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 "PostItMgr.hxx"

#include <svtools/smplhint.hxx>

#include <vcl/svapp.hxx>
#include <vcl/scrbar.hxx>

#ifndef _SWMODULE_HXX

#include <swmodule.hxx>

#endif
#include <viewopt.hxx>

#include <view.hxx>
#include <docsh.hxx>
#include <wrtsh.hxx>
#include <doc.hxx>
#include <fldbas.hxx>
#include <fmtfld.hxx>
#include <docufld.hxx>
#include <edtwin.hxx>
#include <postit.hxx>
#include <txtfld.hxx>
#include <ndtxt.hxx>
#include <SwRewriter.hxx>
#include <undobj.hxx>
#include <tools/color.hxx>

#include <sfx2/request.hxx>

#include "cmdid.h"

#include "../../core/inc/frame.hxx"
#include "../../core/inc/cntfrm.hxx"
#include "../../core/inc/pagefrm.hxx"
#include "../../core/inc/rootfrm.hxx"
#include "../../core/inc/frmtool.hxx"

#include <sfx2/event.hxx>
#include <svx/langitem.hxx>
#include <vcl/outdev.hxx>

#include <i18npool/mslangid.hxx>
#include <i18npool/lang.h>

#include "swevent.hxx"
#include <sfx2/event.hxx>

// distance between ankor Y and initial note position
#define POSTIT_INITIAL_ANKOR_DISTANCE       20
//distance between two postits
#define POSTIT_SPACE_BETWEEN                8
#define POSTIT_MINIMUMSIZE_WITH_META        60
#define POSTIT_SCROLL_SIDEBAR_HEIGHT        20

// if we layout more often we stop, this should never happen
#define MAX_LOOP_COUNT                      50

bool comp_author( const SwPostItItem* a, const SwPostItItem* b)
{
    return a->pFmtFld->GetFld()->GetPar1() < b->pFmtFld->GetFld()->GetPar1();
}

bool comp_date( const SwPostItItem* a, const SwPostItItem* b)
{
    return static_cast<SwPostItField*>(a->pFmtFld->GetFld())->GetDate()  < static_cast<SwPostItField*>(b->pFmtFld->GetFld())->GetDate();
}

bool comp_pos(const SwPostItItem *a, const SwPostItItem *b)
{
    // if notes are on the same line, sort by x position, otherwise by y position
    return (a->mPos.Bottom() == b->mPos.Bottom()) ? a->mPos.Left() < b->mPos.Left() : a->mPos.Bottom() < b->mPos.Bottom();
}

bool comp_id(const SwPostItItem *a, const SwPostItItem *b)
{
    #define TXTFLD pFmtFld->GetTxtFld()
    if (a->TXTFLD->GetTxtNode().FindFlyStartNode() || b->TXTFLD->GetTxtNode().FindFlyStartNode() ||
        a->TXTFLD->GetTxtNode().FindHeaderStartNode() || a->TXTFLD->GetTxtNode().FindFooterStartNode() ||
        b->TXTFLD->GetTxtNode().FindHeaderStartNode() || b->TXTFLD->GetTxtNode().FindFooterStartNode())
        return (a->mPos.Bottom() == b->mPos.Bottom()) ? a->mPos.Left() < b->mPos.Left() : a->mPos.Bottom() < b->mPos.Bottom();
    else
        return ((*a->TXTFLD->GetPosition()) < (*b->TXTFLD->GetPosition()));
}

SwPostItMgr::SwPostItMgr(SwView* pView)
    : mpView(pView)
    , mpWrtShell(mpView->GetDocShell()->GetWrtShell())
    , mpEditWin(&mpView->GetEditWin())
    , mnEventId(0)
    , mbWaitingForCalcRects(false)
    , mpActivePostIt(0)
    , mbLayout(false)
    , mbLayoutHeight(0)
    , mbLayouting(false)
    , mbDeletingSeveral(false)
{
    if(!mpView->GetDrawView() )
        mpView->GetWrtShell().MakeDrawView();
    // collect all PostIts that exist after loading the document
    // don't check for existance for any of them, don't focus them
    AddPostIts(false,false);
    StartListening(*mpView->GetDocShell());
    if (!mvPostItFlds.empty())
    {
        mbWaitingForCalcRects = true;
        mnEventId = Application::PostUserEvent( LINK( this, SwPostItMgr, CalcHdl), 0 );
    }
}

SwPostItMgr::~SwPostItMgr()
{
    if ( mnEventId )
        Application::RemoveUserEvent( mnEventId );
    // forget about all PostItFields
    RemovePostIts();
    EndListening( *mpView->GetDocShell() );

    for(std::vector<SwPostItPageItem*>::iterator i = mPages.begin(); i!= mPages.end() ; i++)
        delete (*i);
    mPages.clear();
}

void SwPostItMgr::CheckForRemovedPostIts()
{
    bool bRemoved = false;
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end(); )
    {
        std::list<SwPostItItem*>::iterator it = i++;
        if ( !(*it)->pFmtFld->IsFldInDoc() )
        {
            SwPostItItem* p = (*it);
            mvPostItFlds.remove(*it);
            delete p->pPostIt;
            delete p;
            bRemoved = true;
        }
    }

    if ( bRemoved )
    {
        // make sure that no deleted items remain in page lists
        // todo: only remove deleted ones?!
        if ( mvPostItFlds.empty() )
            PreparePageContainer();
        else
            // if postits are their make sure that page lists are not empty
            // otherwise sudden paints can cause pain (in BorderOverPageBorder)
            CalcRects();
    }
}

void SwPostItMgr::InsertFld(SwFmtFld* aField, bool bCheckExistance, bool bFocus)
{
    if (bCheckExistance)
    {
        for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        {
            if ( (*i)->pFmtFld == aField )
                return;
        }
    }
    mbLayout = bFocus;
    mvPostItFlds.push_back(new SwPostItItem(aField, true, bFocus) );
    // listen for removal of field
    StartListening( *aField );
}

void SwPostItMgr::RemoveFld( SfxBroadcaster* pFld )
{
    bool bRemoved = false;
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        if ( (*i)->pFmtFld == pFld )
        {
            SwPostItItem* p = (*i);
            mvPostItFlds.remove(*i);
            if (GetActivePostIt() == p->pPostIt)
                SetActivePostIt(0);
            //use lazyDelete due to assertion: "object still in use"
            if (p->pPostIt)
                p->pPostIt->doLazyDelete();
            delete p;
            bRemoved = true;
            break;
        }
    }

    if ( bRemoved )
    {
        // make sure that no deleted items remain in page lists
        // todo: only remove deleted ones?!
        if ( mvPostItFlds.empty() )
            PreparePageContainer();
        else
        {
            // if postits are there make sure that page lists are not empty
            // otherwise sudden paints can cause pain (in BorderOverPageBorder)
            if (!mbDeletingSeveral)
            {
                mbLayout = true;
                CalcRects();
                LayoutPostIts();
            }
        }
    }
}

void SwPostItMgr::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
{
    if ( rHint.IsA(TYPE(SfxEventHint) ) )
    {
        sal_uInt32 nId = ((SfxEventHint&)rHint).GetEventId();
        if ( nId == SW_EVENT_LAYOUT_FINISHED )
        {
            if ( !mbWaitingForCalcRects && !mvPostItFlds.empty())
            {
                mbWaitingForCalcRects = true;
                mnEventId = Application::PostUserEvent( LINK( this, SwPostItMgr, CalcHdl), 0 );
            }
        }
    }
    else if ( rHint.IsA(TYPE(SfxSimpleHint) ) )
    {
        sal_uInt32 nId = ((SfxSimpleHint&)rHint).GetId();
        switch ( nId )
        {
            case SFX_HINT_MODECHANGED:
            {
                SetReadOnlyState();
            }
            break;
            case SFX_HINT_DOCCHANGED:
            {
                if ( mpView->GetDocShell() == &rBC )
                {
                    if ( !mbWaitingForCalcRects && !mvPostItFlds.empty())
                    {
                        mbWaitingForCalcRects = true;
                        mnEventId = Application::PostUserEvent( LINK( this, SwPostItMgr, CalcHdl), 0 );
                    }
                }
                break;
            }
            case SFX_HINT_DYING:
            {
                if ( mpView->GetDocShell() != &rBC )
                {
                    // field to be removed is the broadcaster
                    DBG_ERROR("Notification for removed SwFmtFld was not sent!");
                    RemoveFld(&rBC);
                    PrepareView();
                }
                break;
            }
        }
    }
    else if ( rHint.IsA(TYPE(SwFmtFldHint) ) )
    {
        SwFmtFld* pFld = const_cast <SwFmtFld*>( ((SwFmtFldHint&)rHint).GetField() );
        switch ( ((SwFmtFldHint&)rHint).Which() )
        {
            case SWFMTFLD_INSERTED :
            {
                if (!pFld)
                {
                    AddPostIts(true);
                    break;
                }

                // get field to be inserted from hint
                if ( pFld->IsFldInDoc() )
                {
                    bool bEmpty = mvPostItFlds.empty();
                    InsertFld( pFld, true, false );
                    if (bEmpty && !mvPostItFlds.empty())
                    {
                        if ( !mbWaitingForCalcRects)
                        {
                            mbWaitingForCalcRects = true;
                            mnEventId = Application::PostUserEvent( LINK( this, SwPostItMgr, CalcHdl), 0 );
                        }

                        //mpView->DocSzChgd( mpWrtShell->GetDocSize() );
                        SwRootFrm* pLayout = mpWrtShell->GetLayout();
                        if ( pLayout )
                            pLayout->SetSidebarChanged();

                        mpEditWin->Invalidate();
                    }
                }
                else
                    DBG_ERROR( "Inserted field not in document!" );
                break;
            }
            case SWFMTFLD_REMOVED:
            {
                if (!pFld)
                {
                    CheckForRemovedPostIts();
                    break;
                }

                // get field to be removed from hint
                EndListening( *pFld );
                RemoveFld(pFld);
                PrepareView();
                break;
            }
            case SWFMTFLD_FOCUS:
            {
                if (!mpWrtShell->GetViewOptions()->IsPostIts())
                {
                    SfxRequest aRequest(mpView->GetViewFrame(),FN_VIEW_NOTES);
                    mpView->ExecViewOptions(aRequest);
                }
                // field to get the focus is the broadcaster, SwFmtFld in Hint may be NULL
                SwFmtFld* pFmtFld = dynamic_cast<SwFmtFld*>(&rBC);
                for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
                {
                    if ( pFmtFld == (*i)->pFmtFld )
                    {
                        const SwPageFrm* aPage = mpWrtShell->GetLayout()->GetPageAtPos(mpWrtShell->GetCharRect().Pos());
                        if ((*i)->pPostIt)
                        {
                            (*i)->pPostIt->GrabFocus();
                            Rectangle aNoteRect ((*i)->pPostIt->GetPosPixel(),(*i)->pPostIt->GetSizePixel());
                            mpWrtShell->MakeVisible(SwRect(mpEditWin->PixelToLogic(aNoteRect)));
                            //if this page has a scrollbar, note might be not visible
                            AutoScroll((*i)->pPostIt,aPage->GetPhyPageNum());
                        }
                        else
                        {
                            // when the layout algorithm starts, this postit is created and receives focus
                            (*i)->bFocus = true;
                        }
                    }
                }
                break;
            }
            case SWFMTFLD_CHANGED:
            {
                // field to get the focus is the broadcaster, SwFmtFld in Hint may be NULL
                SwFmtFld* pFmtFld = dynamic_cast<SwFmtFld*>(&rBC);
                for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
                {
                    if ( pFmtFld == (*i)->pFmtFld )
                    {
                        (*i)->pPostIt->SetPostItText();
                    }
                }
                break;
            }
        }
    }
}

bool SwPostItMgr::CalcRects()
{
    if ( mnEventId )
    {
        // if CalcRects() was forced and an event is still pending: remove it
        // it is superfluous and also may cause reentrance problems if triggered while layouting
        Application::RemoveUserEvent( mnEventId );
        mnEventId = 0;
    }

    bool bChange = false;
    bool bRepair = false;
    PreparePageContainer();
    if ( !mvPostItFlds.empty() )
    {
        mpWrtShell->SwCrsrShell::Push();
           const BOOL bOldLockView = mpWrtShell->IsViewLocked();
        mpWrtShell->LockView( TRUE );

        for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        {
            SwPostItItem* pItem = (*i);
            if ( !pItem->pFmtFld->IsFldInDoc() )
            {
                DBG_ERROR("PostIt is not in doc!");
                bRepair = true;
                continue;
            }

            //save old rect
            SwRect aOldRect(pItem->mPos);
            // set new rect
            mpWrtShell->GotoFld(*pItem->pFmtFld);
            pItem->mPos = mpWrtShell->GetRectOfCurrentChar();
            bChange = (pItem->mPos != aOldRect) || bChange;

            pItem->mPagePos = mpWrtShell->GetAnyCurRect(RECT_PAGE);
            const SwRect aPageFrm( mpWrtShell->GetAnyCurRect( RECT_PAGE_PRT ) );
            pItem->mFramePos = aPageFrm;
            pItem->mFramePos.Pos() += pItem->mPagePos.Pos();
        }

        // show notes in right order in navigator
        //prevent ankors during layout to overlap, e.g. when moving a frame
        Sort(SORT_POS);

        // sort the items into the right page vector, so layout can be done by page
        for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        {
            SwPostItItem* pItem = (*i);
            if ( !pItem->pFmtFld->IsFldInDoc() )
                continue;

            const SwPageFrm* pPage = mpWrtShell->GetLayout()->GetPageAtPos((pItem)->mPos.Pos());
            const unsigned long aPageNum = pPage ? pPage->GetPhyPageNum() : 1;
            //DBG_ASSERT(aPageNum <= mPages.size(),"SwPostItMgr: PageNum larger than page vector");
            if (aPageNum > mPages.size())
            {
                const unsigned long nNumberOfPages = mPages.size();
                for (unsigned int j=0; j<aPageNum - nNumberOfPages; ++j)
                    mPages.push_back( new SwPostItPageItem());
            }
            mPages[aPageNum-1]->mList->push_back(pItem);
            mPages[aPageNum-1]->mPageRect = (pItem)->mPagePos;
            mPages[aPageNum-1]->bMarginSide = pPage ? pPage->MarginSide() : false;
        }

        if (!bChange && mpWrtShell->getIDocumentSettingAccess()->get(IDocumentSettingAccess::BROWSE_MODE))
        {
            if ( mpWrtShell->GetLayout()->Frm().Height() > mbLayoutHeight)
            {
                if (mPages[0]->bScrollbar || HasScrollbars())
                    bChange = true;
            }
            else
            if (mpWrtShell->GetLayout()->Frm().Height() < mbLayoutHeight)
            {
                if (mPages[0]->bScrollbar || !BorderOverPageBorder(1))
                    bChange = true;
            }
        }

        mpWrtShell->LockView( bOldLockView );
        mpWrtShell->SwCrsrShell::Pop( FALSE );
    }

    if ( bRepair )
        CheckForRemovedPostIts();

    mbLayoutHeight = mpWrtShell->GetLayout()->Frm().Height();
    mbWaitingForCalcRects = false;
    return bChange;
}

bool SwPostItMgr::HasScrollbars() const
{
    for(std::list<SwPostItItem*>::const_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        if ((*i)->bShow && (*i)->pPostIt->Scrollbar())
            return true;
    }
    return false;
}

void SwPostItMgr::PreparePageContainer()
{
    // we do not just delete the SwPostItPageItem, so offset/scrollbar is not lost
    long lPageSize = mpWrtShell->GetNumPages();
    long lContainerSize = mPages.size();

    if (lContainerSize < lPageSize)
    {
        for (int i=0; i<lPageSize - lContainerSize;i++)
            mPages.push_back( new SwPostItPageItem());
    }
    else
    if (lContainerSize > lPageSize)
    {
        for (int i=mPages.size()-1; i >= lPageSize;--i)
        {
            delete mPages[i];
            mPages.pop_back();
        }
    }
    // only clear the list, DO NOT delete the objects itself
    for(std::vector<SwPostItPageItem*>::iterator i = mPages.begin(); i!= mPages.end() ; i++)
    {
        (*i)->mList->clear();
        if (mvPostItFlds.empty())
            (*i)->bScrollbar = false;

    }
}

void SwPostItMgr::LayoutPostIts()
{
    if ( !mvPostItFlds.empty() && !mbWaitingForCalcRects )
    {
        mbLayouting = true;
        if (ShowNotes())
        {
            //loop over all pages and do the layout
            // - create SwPostIt if neccessary
            // - place SwPostIts on their initial position
            // - calculate neccessary height for all PostIts together
            bool bUpdate = false;
            for (unsigned long n=0;n<mPages.size();n++)
            {
                // only layout if there are notes on this page
                if (mPages[n]->mList->size()>0)
                {
                    std::list<SwPostIt*>    aVisiblePostItList;
                    unsigned long           lNeededHeight = 0;
                    long                    mlPageBorder = 0;
                    long                    mlPageEnd = 0;

                    for(SwPostItItem_iterator i = mPages[n]->mList->begin(); i!= mPages[n]->mList->end(); i++)
                    {
                        SwPostItItem* pItem = (*i);
                        SwFmtFld* pFmtFld = pItem->pFmtFld;
                        SwPostIt* pPostIt = pItem->pPostIt;

                        if (mPages[n]->bMarginSide)
                        {
                            // x value for notes positioning
                            mlPageBorder = mpEditWin->LogicToPixel( Point( mPages[n]->mPageRect.Left(), 0)).X() - GetSidebarWidth(true);// - GetSidebarBorderWidth(true);
                            //bending point
                            mlPageEnd = mpWrtShell->getIDocumentSettingAccess()->get( IDocumentSettingAccess::BROWSE_MODE) ? pItem->mFramePos.Left() : mPages[n]->mPageRect.Left() + 350;
                        }
                        else
                        {
                            // x value for notes positioning
                            mlPageBorder = mpEditWin->LogicToPixel( Point(mPages[n]->mPageRect.Right(), 0)).X() + GetSidebarBorderWidth(true);
                            //bending point
                            mlPageEnd = mpWrtShell->getIDocumentSettingAccess()->get( IDocumentSettingAccess::BROWSE_MODE) ? pItem->mFramePos.Right() : mPages[n]->mPageRect.Right() - 350;
                        }

                        if (pItem->bShow)
                        {
                            long Y = mpEditWin->LogicToPixel( Point(0,pItem->mPos.Bottom())).Y();
                            long aPostItHeight = 0;
                            if (!pPostIt)
                            {
                                pPostIt = new SwPostIt(static_cast<Window*>(&mpView->GetEditWin()),WINDOW_CONTROL,pFmtFld,this, mPages[n]->bMarginSide);
                                pPostIt->SetReadonly( mpView->GetDocShell()->IsReadOnly() );
                                SetColors(pPostIt,static_cast<SwPostItField*>(pFmtFld->GetFld()));
                                pItem->pPostIt = pPostIt;
                            }
                            else
                            {
                                pPostIt->HideNote();
                            }
                            aPostItHeight = ( pPostIt->GetPostItTextHeight() < pPostIt->GetMinimumSizeWithoutMeta() ? pPostIt->GetMinimumSizeWithoutMeta() : pPostIt->GetPostItTextHeight() ) + pPostIt->GetMetaHeight();
                            pPostIt->SetPosSizePixelRect( mlPageBorder ,Y-GetInitialAnchorDistance(), GetNoteWidth() ,aPostItHeight,pItem->mPos, mlPageEnd);

                            if (pItem->bFocus)
                            {
                                mbLayout = true;
                                pPostIt->GrabFocus();
                                pItem->bFocus = false;
                            }
                            // only the visible postits are used for the final layout
                            aVisiblePostItList.push_back(pPostIt);
                            lNeededHeight += aPostItHeight+GetSpaceBetween();
                        }
                        else // we don't want to see it
                        {
                            if (pPostIt)
                                pPostIt->HideNote();
                        }
                    }

                    if (aVisiblePostItList.size()>0)
                    {
                        bool bOldScrollbar = mPages[n]->bScrollbar;
                        mPages[n]->bScrollbar = LayoutByPage(aVisiblePostItList, mPages[n]->mPageRect.SVRect(), lNeededHeight);
                        if (!mPages[n]->bScrollbar)
                        {
                            mPages[n]->lOffset = 0;
                        }
                        else
                        {
                            //when we changed our zoom level, the offset value can be to big, so lets check for the largest possible zoom value
                            long aAvailableHeight = mpEditWin->LogicToPixel(Size(0,mPages[n]->mPageRect.Height())).Height() - 2 * GetSidebarScrollerHeight();
                            long lOffset = -1 * GetScrollSize() * (aVisiblePostItList.size() - aAvailableHeight / GetScrollSize());
                            if (mPages[n]->lOffset < lOffset)
                                mPages[n]->lOffset = lOffset;
                        }
                        bUpdate = (bOldScrollbar != mPages[n]->bScrollbar) || bUpdate;
                        const long aSidebarheight = mPages[n]->bScrollbar ? mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height() : 0;
                        /*
                       TODO:

                       - if ( (oldposition-newposition) < 5) --> set position to old value, so notes do not jump up and down
                       - enlarge all notes till GetNextBorder(), as we resized to average value before
                           (remember to subtract POSTIT_SPACE_BETWEEN (GetSpaceBetween()) somewhere, can we do this in GetNextBorder()? )
                           (only do it if we don't have scrollbars)

                       */
                        // lets hide the ones which overlap the page
                        for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
                        {
                            if (mPages[n]->lOffset != 0)
                                (*i)->TranslateTopPosition(mPages[n]->lOffset);

                            bool bBottom  = mpEditWin->PixelToLogic(Point(0,(*i)->GetPosPixel().Y()+(*i)->GetSizePixel().Height())).Y() <= (mPages[n]->mPageRect.Bottom()-aSidebarheight);
                            bool bTop = mpEditWin->PixelToLogic(Point(0,(*i)->GetPosPixel().Y())).Y() >= (mPages[n]->mPageRect.Top()+aSidebarheight);
                            if ( bBottom && bTop )
                            {
                                (*i)->ShowNote();
                            }
                            else
                            {
                                if (mpEditWin->PixelToLogic(Point(0,(*i)->GetPosPixel().Y())).Y() < (mPages[n]->mPageRect.Top()+aSidebarheight))
                                {

                                    if (mPages[n]->bMarginSide)
                                        (*i)->ShowAnkorOnly(Point(mPages[n]->mPageRect.Left(),mPages[n]->mPageRect.Top()));
                                    else
                                        (*i)->ShowAnkorOnly(Point(mPages[n]->mPageRect.Right(),mPages[n]->mPageRect.Top()));
                                }
                                else
                                {
                                    if (mPages[n]->bMarginSide)
                                        (*i)->ShowAnkorOnly(Point(mPages[n]->mPageRect.Left(),mPages[n]->mPageRect.Bottom()));
                                    else
                                        (*i)->ShowAnkorOnly(Point(mPages[n]->mPageRect.Right(),mPages[n]->mPageRect.Bottom()));
                                }
                                DBG_ASSERT(mPages[n]->bScrollbar,"SwPostItMgr::LayoutByPage(): note overlaps, but bScrollbar is not true");
                            }
                        }
                        // do some magic so we really see the focused note
                        for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
                        {
                            if ((*i)->HasChildPathFocus())
                            {
                                AutoScroll((*i),n+1);
                                Rectangle aNoteRect ((*i)->GetPosPixel(),(*i)->GetSizePixel());
                                mpWrtShell->MakeVisible(SwRect(mpEditWin->PixelToLogic(aNoteRect)));
                                break;
                            }
                        }
                    }
                    aVisiblePostItList.clear();
                }
                else
                {
                    bUpdate = true;
                    mPages[n]->bScrollbar = false;
                }
            }

            // notes scrollbar is otherwise not drawn correctly for some cases
            // scrollbar area is enough
            if (bUpdate)
                mpEditWin->Invalidate();
        }
        else
        {   // we do not want to see the notes anymore -> Options-Writer-View-Notes
            bool bRepair = false;
            for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
            {
                SwPostItItem* pItem = (*i);
                if ( !pItem->pFmtFld->IsFldInDoc() )
                {
                    DBG_ERROR("PostIt is not in doc!");
                    bRepair = true;
                    continue;
                }

                if ((*i)->pPostIt)
                {
                    (*i)->pPostIt->HideNote();
                    if ((*i)->pPostIt->HasChildPathFocus())
                    {
                        SetActivePostIt(0);
                        (*i)->pPostIt->GrabFocusToDocument();
                    }
                }
            }

            if ( bRepair )
                CheckForRemovedPostIts();
        }

        mbLayouting = false;
    }
}

bool SwPostItMgr::BorderOverPageBorder(unsigned long aPage) const
{
    if ( mPages[aPage-1]->mList->empty() )
    {
        DBG_ERROR("Notes SidePane painted but no rects and page lists calculated!")
        return false;
    }

    SwPostItItem_iterator aItem = mPages[aPage-1]->mList->end();
    --aItem;
    const long aSidebarheight = mPages[aPage-1]->bScrollbar ? mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height() : 0;
    const long aEndValue = mpEditWin->PixelToLogic(Point(0,(*aItem)->pPostIt->GetPosPixel().Y()+(*aItem)->pPostIt->GetSizePixel().Height())).Y();
    return aEndValue <= mPages[aPage-1]->mPageRect.Bottom()-aSidebarheight;
}


void SwPostItMgr::Scroll(const long lScroll,const unsigned long aPage)
{
    DBG_ASSERT((lScroll % GetScrollSize() )==0,"SwPostItMgr::Scroll: scrolling by wrong value");
    // do not scroll more than neccessary up or down
    if ( ((mPages[aPage-1]->lOffset == 0) && (lScroll>0)) || ( BorderOverPageBorder(aPage) && (lScroll<0)) )
        return;

    const bool bOldUp = ArrowEnabled(KEY_PAGEUP,aPage);
    const bool bOldDown = ArrowEnabled(KEY_PAGEDOWN,aPage);
    const long aSidebarheight = mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height();
    for(SwPostItItem_iterator i = mPages[aPage-1]->mList->begin(); i!= mPages[aPage-1]->mList->end(); i++)
    {
        SwPostIt* pPostIt = (*i)->pPostIt;
        pPostIt->HideNote();
        pPostIt->TranslateTopPosition(lScroll);

        if ((*i)->bShow)
        {
            bool bBottom  = mpEditWin->PixelToLogic(Point(0,pPostIt->GetPosPixel().Y()+pPostIt->GetSizePixel().Height())).Y() <= (mPages[aPage-1]->mPageRect.Bottom()-aSidebarheight);
            bool bTop = mpEditWin->PixelToLogic(Point(0,pPostIt->GetPosPixel().Y())).Y() >=  (mPages[aPage-1]->mPageRect.Top()+aSidebarheight);
            if ( bBottom && bTop)
            {
                    pPostIt->ShowNote();
            }
            else
            {
                if ( mpEditWin->PixelToLogic(Point(0,pPostIt->GetPosPixel().Y())).Y() < (mPages[aPage-1]->mPageRect.Top()+aSidebarheight))
                {
                    if (mPages[aPage-1]->bMarginSide)
                        pPostIt->ShowAnkorOnly(Point(mPages[aPage-1]->mPageRect.Left(),mPages[aPage-1]->mPageRect.Top()));
                    else
                        pPostIt->ShowAnkorOnly(Point(mPages[aPage-1]->mPageRect.Right(),mPages[aPage-1]->mPageRect.Top()));
                }
                else
                {
                    if (mPages[aPage-1]->bMarginSide)
                        pPostIt->ShowAnkorOnly(Point(mPages[aPage-1]->mPageRect.Left(),mPages[aPage-1]->mPageRect.Bottom()));
                    else
                        pPostIt->ShowAnkorOnly(Point(mPages[aPage-1]->mPageRect.Right(),mPages[aPage-1]->mPageRect.Bottom()));
                }
            }
        }
    }
    mPages[aPage-1]->lOffset += lScroll;
    if ( (bOldUp != ArrowEnabled(KEY_PAGEUP,aPage)) ||(bOldDown != ArrowEnabled(KEY_PAGEDOWN,aPage)) )
    {
        mpEditWin->Invalidate(GetBottomScrollRect(aPage));
        mpEditWin->Invalidate(GetTopScrollRect(aPage));
    }
}
void SwPostItMgr::AutoScroll(const SwPostIt* pPostIt)
{
    for (unsigned long n=0;n<mPages.size();n++)
    {
        if (mPages[n]->mList->size()>0)
        {
            for(SwPostItItem_iterator i = mPages[n]->mList->begin(); i!= mPages[n]->mList->end(); i++)
            {
                if ((*i)->pPostIt==pPostIt)
                {
                    AutoScroll(pPostIt,n+1);
                    return;
                }
            }
        }
    }
}

void SwPostItMgr::AutoScroll(const SwPostIt* pPostIt,const unsigned long aPage )
{
    // otherwise all notes are visible
    if (mPages[aPage-1]->bScrollbar)
    {
        const long aSidebarheight = mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height();
        const bool bBottom  = mpEditWin->PixelToLogic(Point(0,pPostIt->GetPosPixel().Y()+pPostIt->GetSizePixel().Height())).Y() <= (mPages[aPage-1]->mPageRect.Bottom()-aSidebarheight);
        const bool bTop = mpEditWin->PixelToLogic(Point(0,pPostIt->GetPosPixel().Y())).Y() >= (mPages[aPage-1]->mPageRect.Top()+aSidebarheight);
        if ( !(bBottom && bTop))
        {
            const long aDiff = bBottom ? mpEditWin->LogicToPixel(Point(0,mPages[aPage-1]->mPageRect.Top() + aSidebarheight)).Y() - pPostIt->GetPosPixel().Y() :
                                            mpEditWin->LogicToPixel(Point(0,mPages[aPage-1]->mPageRect.Bottom() - aSidebarheight)).Y() - (pPostIt->GetPosPixel().Y()+pPostIt->GetSizePixel().Height());
            // this just adds the missing value to get the next a* GetScrollSize() after aDiff
            // e.g aDiff= 61 POSTIT_SCOLL=50 --> lScroll = 100
            const long lScroll = bBottom ? (aDiff + ( GetScrollSize() - (aDiff % GetScrollSize()))) : (aDiff - (GetScrollSize() + (aDiff % GetScrollSize())));
            Scroll(lScroll, aPage);
        }
    }
}

bool SwPostItMgr::ArrowEnabled(USHORT aDirection,unsigned long aPage) const
{
    switch (aDirection)
    {
        case KEY_PAGEUP:
            {
                if (mPages[aPage-1]->lOffset == 0)
                    return false;
                else
                    return true;
            }
        case KEY_PAGEDOWN:
            {
                if (BorderOverPageBorder(aPage))
                    return false;
                else
                    return true;
            }
        default: return false;
    }
}

Color SwPostItMgr::GetArrowColor(USHORT aDirection,unsigned long aPage) const
{
    if (ArrowEnabled(aDirection,aPage))
    {
        if (Application::GetSettings().GetStyleSettings().GetHighContrastMode())
            return Color(COL_WHITE);
        else
            return COL_NOTES_SIDEPANE_ARROW_ENABLED;
    }
    else
    {
        return COL_NOTES_SIDEPANE_ARROW_DISABLED;
    }
}

bool SwPostItMgr::LayoutByPage(std::list<SwPostIt*> &aVisiblePostItList,const Rectangle aBorder, long lNeededHeight)
{
    /*** General layout idea:***/
    //  - if we have space left, we always move the current one up,
    //    otherwise the next one down
    //  - first all notes are resized
    //  - then the real layout starts
    /*************************************************************/

    //rBorder is the page rect
    const Rectangle rBorder         = mpEditWin->LogicToPixel( aBorder);
    long            lTopBorder      = rBorder.Top() + 5;
    long            lBottomBorder   = rBorder.Bottom() - 5;
    const long      lVisibleHeight  = rBorder.GetHeight() ;
    long            lSpaceUsed      = 0;
    long            lTranslatePos   = 0;
    int             loop            = 0;
    bool            bDone           = false;
    bool            bScrollbars     = false;

    // do all neccessary resizings
    /*
    if (lVisibleHeight < lNeededHeight)
    {
        // resize the one we showed bigger on purpose and recalculate lNeededHeight
        lNeededHeight = 0;
        unsigned long aPostItHeight = 0;
        for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
        {
            aPostItHeight = ( (*i)->GetTextHeight()==0 ? 30 : (*i)->GetTextHeight() ) + (*i)->GetMetaHeight();
            if ((*i)->GetTextHeight() < (*i)->GetMinimumSizeWithoutMeta())
                (*i)->SetSizePixel(Size((*i)->GetSizePixel().getWidth(), aPostItHeight));
            lNeededHeight += aPostItHeight+POSTIT_SPACE_BETWEEN;
        }
        // do we still need to resize now?
        */
        if (lVisibleHeight < lNeededHeight)
        {
            // ok, now we have to really resize and adding scrollbars
            const long lAverageHeight = (lVisibleHeight - aVisiblePostItList.size()*GetSpaceBetween()) / aVisiblePostItList.size();
            if (lAverageHeight<GetMinimumSizeWithMeta())
            {
                bScrollbars = true;
                lTopBorder += GetSidebarScrollerHeight() + 10;
                lBottomBorder -= (GetSidebarScrollerHeight() + 10);

                for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
                        (*i)->SetSizePixel(Size((*i)->GetSizePixel().getWidth(),(*i)->GetMinimumSizeWithMeta()));
            }
            else
            {
                for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
                {
                    if ( (*i)->GetSizePixel().getHeight() > lAverageHeight)
                        (*i)->SetSizePixel(Size((*i)->GetSizePixel().getWidth(),lAverageHeight));
                }
            }
        }
    //}

    //start the real layout so nothing overlaps anymore
    if (aVisiblePostItList.size()>1)
    {
        // if no window is moved anymore we are finished
        while (!bDone)
        {
            loop++;
              bDone = true;
            lSpaceUsed = lTopBorder + GetSpaceBetween();
            for(SwPostIt_iterator i = aVisiblePostItList.begin(); i!= aVisiblePostItList.end() ; i++)
            {
                SwPostIt_iterator aNextPostIt = i;
                ++aNextPostIt;

                if (aNextPostIt !=aVisiblePostItList.end())
                {
                    lTranslatePos = ( (*i)->GetPosPixel().Y() + (*i)->GetSizePixel().Height()) - (*aNextPostIt)->GetPosPixel().Y();
                    if (lTranslatePos > 0) // note windows overlaps the next one
                    {
                        // we are not done yet, loop at least once more
                        bDone = false;
                        // if there is space left, move the current note up
                        // it could also happen that there is no space left for the first note due to a scrollbar
                        // then we also jump into, so we move the current one up and the next one down
                        if ( (lSpaceUsed <= (*i)->GetPosPixel().Y()) || (i==aVisiblePostItList.begin()))
                        {
                            // we have space left, so let's move the current one up
                            if ( ((*i)->GetPosPixel().Y()- lTranslatePos - GetSpaceBetween()) > lTopBorder)
                            {
                                (*i)->TranslateTopPosition(-1*(lTranslatePos+GetSpaceBetween()));
                            }
                            else
                            {
                                long lMoveUp = (*i)->GetPosPixel().Y() - lTopBorder;
                                (*i)->TranslateTopPosition(-1* lMoveUp);
                                (*aNextPostIt)->TranslateTopPosition( (lTranslatePos+GetSpaceBetween()) - lMoveUp);
                            }
                        }
                        else
                        {
                            // no space left, left move the next one down
                            (*aNextPostIt)->TranslateTopPosition(lTranslatePos+GetSpaceBetween());
                        }
                    }
                    else
                    {
                        // the first one could overlap the topborder instead of a second note
                        if (i==aVisiblePostItList.begin())
                        {
                            long lMoveDown = lTopBorder - (*i)->GetPosPixel().Y();
                            if (lMoveDown>0)
                                (*i)->TranslateTopPosition( lMoveDown);
                        }
                    }
                    lSpaceUsed += (*i)->GetSizePixel().Height() + GetSpaceBetween();
                }
                else
                {
                    //(*i) is the last visible item
                    SwPostIt_iterator aPrevPostIt = i;
                    --aPrevPostIt;
                    lTranslatePos = ( (*aPrevPostIt)->GetPosPixel().Y() + (*aPrevPostIt)->GetSizePixel().Height()) - (*i)->GetPosPixel().Y();
                    if (lTranslatePos > 0)
                    {
                        bDone = false;
                        if ( ((*i)->GetPosPixel().Y()+ (*i)->GetSizePixel().Height()+lTranslatePos) < lBottomBorder)
                        {
                            (*i)->TranslateTopPosition(lTranslatePos+GetSpaceBetween());
                        }
                        else
                        {
                            (*i)->TranslateTopPosition(lBottomBorder - ((*i)->GetPosPixel().Y()+ (*i)->GetSizePixel().Height()) );
                        }
                    }
                    else
                    {
                        // note does not overlap, but we might be over the lower border
                        // only do this if there are no scrollbars, otherwise notes are supposed to overlap the border
                        if (!bScrollbars && ((*i)->GetPosPixel().Y()+ (*i)->GetSizePixel().Height() > lBottomBorder) )
                        {
                            bDone = false;
                            (*i)->TranslateTopPosition(lBottomBorder - ((*i)->GetPosPixel().Y()+ (*i)->GetSizePixel().Height()));
                        }
                    }
                }
            }
            // security check so we don't loop forever
            if (loop>MAX_LOOP_COUNT)
            {
                DBG_ERROR("PostItMgr::Layout(): We are looping forever");
                break;
            }
        }
    }
    else
    {
        // only one left, make sure it is not hidden at the top or bottom
        SwPostIt_iterator i = aVisiblePostItList.begin();
        lTranslatePos = lTopBorder - (*i)->GetPosPixel().Y();
        if (lTranslatePos>0)
        {
            (*i)->TranslateTopPosition(lTranslatePos+GetSpaceBetween());
        }
        lTranslatePos = lBottomBorder - ((*i)->GetPosPixel().Y()+ (*i)->GetSizePixel().Height());
        if (lTranslatePos<0)
        {
            (*i)->TranslateTopPosition(lTranslatePos);
        }
    }
    return bScrollbars;
 }

void SwPostItMgr::AddPostIts(bool bCheckExistance, bool bFocus)
{
    bool bEmpty = mvPostItFlds.empty();
    SwFieldType* pType = mpView->GetDocShell()->GetDoc()->GetFldType(RES_POSTITFLD, aEmptyStr,false);
    SwClientIter aIter( *pType );
    SwClient * pFirst = aIter.GoStart();
    while(pFirst)
    {
        SwFmtFld* pSwFmtFld = static_cast<SwFmtFld*>(pFirst);
        if ( pSwFmtFld->GetTxtFld())
        {
            if ( pSwFmtFld->IsFldInDoc() )
                InsertFld(pSwFmtFld,bCheckExistance,bFocus);
        }
        pFirst = aIter++;
    }

    // if we just added the first one we have to update the view for centering
    if (bEmpty && !mvPostItFlds.empty())
    {
        SwRootFrm* pLayout = mpWrtShell->GetLayout();
        if ( pLayout )
            pLayout->SetSidebarChanged();
        //mpView->DocSzChgd( mpWrtShell->GetDocSize() );

        mpEditWin->Invalidate();
    }
}

void SwPostItMgr::RemovePostIts()
{
    if (!mvPostItFlds.empty())
    {
        for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        {
            EndListening( *((*i)->pFmtFld) );
            SwPostIt* pPostIt = (*i)->pPostIt;
            delete pPostIt;
            delete (*i);
        }
        mvPostItFlds.clear();
    }

    // all postits removed, no items should be left in pages
    PreparePageContainer();
}

void SwPostItMgr::Delete(String aAuthor)
{
    mbDeletingSeveral = true;
    mpWrtShell->StartAllAction();
    SwRewriter aRewriter;
    aRewriter.AddRule(UNDO_ARG1, SW_RES(STR_NOTE) );
    mpWrtShell->StartUndo( UNDO_DELETE, &aRewriter );
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end(); )
        {
            SwPostItItem* pItem = (*i);
            SwPostItField* pPostItField = static_cast<SwPostItField*>(pItem->pFmtFld->GetFld());
            if (pPostItField->GetPar1() == aAuthor)
            {
                // stop listening, we delete ourselves
                EndListening( *(pItem->pFmtFld) );
                // delete the actual SwPostItField
                mpWrtShell->GotoFld(*pItem->pFmtFld);
                mpWrtShell->DelRight();
                i = mvPostItFlds.erase(i);
                // delete visual representation
                //use lazyDelete due to assertion: "object still in use"
                if (pItem->pPostIt == GetActivePostIt())
                    SetActivePostIt(0);
                pItem->pPostIt->doLazyDelete();
                // delete struct saving the pointers
                delete pItem;
            }
            else
                ++i;
        }
    mpWrtShell->EndUndo( UNDO_DELETE );
    mpWrtShell->EndAllAction();
    PrepareView();
    mbLayout = true;
    CalcRects();
    LayoutPostIts();
    mbDeletingSeveral = false;
}

void SwPostItMgr::Delete()
{
    mbDeletingSeveral = true;
    mpWrtShell->StartAllAction();
    SwRewriter aRewriter;
    aRewriter.AddRule(UNDO_ARG1, SW_RES(STR_NOTE) );
    mpWrtShell->StartUndo( UNDO_DELETE, &aRewriter );
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        SwPostItItem* pItem = (*i);
        // stop listening, we delete ourselves
        EndListening( *(pItem->pFmtFld) );
        // delete the actual SwPostItField
        mpWrtShell->GotoFld(*   pItem->pFmtFld);
        mpWrtShell->DelRight();
        // delete visual representation
        //use lazyDelete due to assertion: "object still in use"
        pItem->pPostIt->doLazyDelete();
        // delete struct saving the pointers
        delete pItem;
    }
    mvPostItFlds.clear();
    mpWrtShell->EndUndo( UNDO_DELETE );
    mpWrtShell->EndAllAction();
    PreparePageContainer();
    PrepareView();
    mbDeletingSeveral = false;
}

void SwPostItMgr::Hide(SwPostItField* aPostItField, bool All)
{
    String aAuthor = aPostItField->GetPar1();
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        SwPostItField* pPostItField = static_cast<SwPostItField*>((*i)->pFmtFld->GetFld());
        if ( aAuthor == pPostItField->GetPar1() )
        {
            if (pPostItField==aPostItField)
            {
                (*i)->bShow = false;
                (*i)->pPostIt->HideNote();
            }
            else
            {
                if (All)
                {
                    (*i)->bShow  = false;
                    (*i)->pPostIt->HideNote();
                }
            }
        }
    }
    LayoutPostIts();
}

void SwPostItMgr::Hide()
{
    for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        (*i)->bShow = false;
        (*i)->pPostIt->HideNote();
    }
    LayoutPostIts();
}


void SwPostItMgr::Show()
{
    for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        (*i)->bShow = true;
    }
    LayoutPostIts();
}

void SwPostItMgr::Sort(const short aType)
{
    if (mvPostItFlds.size()>1 )
    {
        switch (aType)
        {
            case SORT_POS:
                mvPostItFlds.sort(comp_pos);
                //mvPostItFlds.sort(comp_id);
                break;
            case SORT_AUTHOR:
                mvPostItFlds.sort(comp_author);
                break;
            case SORT_DATE:
                mvPostItFlds.sort(comp_date);
                break;
        }
    }
}

SwPostIt* SwPostItMgr::GetNextPostIt(USHORT aDirection, SwPostIt* aPostIt)
{
    if (mvPostItFlds.size()>1)
    {
        for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        {
            if ( (*i)->pPostIt ==aPostIt)
            {
                SwPostItItem_iterator iNextPostIt   = i;
                /*
                const Rectangle &aVisRect           = mpView->GetVisArea();
                bool bVisible                       = false;
                bool bDone                          = false;

                while (!bDone)
                {
                */
                    if (aDirection==KEY_PAGEUP)
                    {
                        if ( iNextPostIt==mvPostItFlds.begin() )
                        {
                            iNextPostIt = mvPostItFlds.end();
                        }
                        --iNextPostIt;
                    }
                    else
                    {
                        iNextPostIt++;
                        if ( iNextPostIt==mvPostItFlds.end() )
                        {
                            iNextPostIt = mvPostItFlds.begin();
                        }
                    }
                    // lets quit, we are back at the beginng
                    if ( (*iNextPostIt)->pPostIt==aPostIt)
                        return NULL;

                    //bVisible = (*iNextPostIt)->pFmtFld->GetTxtFld()->GetTxtNode().IsInVisibleArea();
                    //bVisible = bVisible && ((*iNextPostIt)->mPos.Bottom() < aVisRect.Bottom()) && ((*iNextPostIt)->mPos.Bottom() > aVisRect.Top());

                    /*
                    const bool bBottom  = mpEditWin->PixelToLogic(Point(0,(*iNextPostIt)->pPostIt->GetPosPixel().Y()+(*iNextPostIt)->pPostIt->GetSizePixel().Height())).Y() <= aVisRect.Bottom();
                    const bool bTop = mpEditWin->PixelToLogic(Point(0,(*iNextPostIt)->pPostIt->GetPosPixel().Y())).Y() >= aVisRect.Top();
                    bVisible = bBottom && bTop;

                    bDone = bVisible && (*iNextPostIt)->bShow;

                }
                */
                return (*iNextPostIt)->pPostIt;
            }
        }
        return NULL;
    }
    else
        return NULL;
}

long SwPostItMgr::GetNextBorder()
{
    for (unsigned long n=0;n<mPages.size();n++)
    {
        for(SwPostItItem_iterator b = mPages[n]->mList->begin(); b!= mPages[n]->mList->end(); b++)
        {
            if ((*b)->pPostIt == mpActivePostIt)
            {
                if (mPages[n]->bScrollbar)
                {
                    return -1;
                }
                else
                {
                    //if this is the last item, return the bottom border otherwise the next item
                    SwPostItItem_iterator aNext = b;
                    aNext++;
                    if (aNext == mPages[n]->mList->end())
                    {
                        return mpEditWin->LogicToPixel(Point(0,mPages[n]->mPageRect.Bottom())).Y();
                    }
                    else
                    {
                        return (*aNext)->pPostIt->GetPosPixel().Y();
                    }
                }
            }
        }
    }

    DBG_ERROR("SwPostItMgr::GetNextBorder(): We have to find a next border here");
    return -1;
}

SwFmtFld* SwPostItMgr::GetFmtFld(SwPostIt* mpPostIt)
{
    for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
    {
        if ( (*i)->pPostIt == mpPostIt)
            return (*i)->pFmtFld;
    }
    DBG_WARNING("SwPostItMgr::GetFmtFld(): PostIt not found, something major must have gone wrong here");
    return NULL;
}

void SwPostItMgr::PrepareView(bool bIgnoreCount)
{
    if (mvPostItFlds.empty() || bIgnoreCount)
    {
        // easy  to implement ;-) , sidebar area should be enough though
        // remove possible left over stuff from sidebar
        if (mvPostItFlds.empty())
            mpEditWin->Invalidate();

        //mpView->DocSzChgd( mpWrtShell->GetDocSize() );
        SwRootFrm* pLayout = mpWrtShell->GetLayout();
        if ( pLayout )
            pLayout->SetSidebarChanged();

        if ( mpWrtShell->getIDocumentSettingAccess()->get( IDocumentSettingAccess::BROWSE_MODE ) )
            pLayout->InvalidateBrowseWidth();
    }
}

bool SwPostItMgr::ShowScrollbar(const unsigned long aPage) const
{
    if (mPages.size() > aPage-1)
        return mPages[aPage-1]->bScrollbar;
    else
        return false;
}

bool SwPostItMgr::IsHit(const Point &aPointPixel)
{
    if (HasNotes() && ShowNotes())
    {
        const Point aPoint = mpEditWin->PixelToLogic(aPointPixel);
        const SwRootFrm* pLayout = mpWrtShell->GetLayout();
        const SwFrm* pPage = pLayout->GetPageAtPos( aPoint, 0, true );
        if (pPage)
        {
            Rectangle aRect;
            const unsigned long aPageNum = pPage->GetPhyPageNum();
            DBG_ASSERT(mPages.size()>aPageNum-1,"SwPostitMgr:: page container size wrong");
            aRect = mPages[aPageNum-1]->bMarginSide ? Rectangle(Point(pPage->Frm().Left()-GetSidebarWidth()-GetSidebarBorderWidth(),pPage->Frm().Top()),Size(GetSidebarWidth(),pPage->Frm().Height())) :
                            Rectangle( Point(pPage->Frm().Right()+GetSidebarBorderWidth(),pPage->Frm().Top()) , Size(GetSidebarWidth(),pPage->Frm().Height()));
            if (aRect.IsInside(aPoint))
            {
                // we hit the note's sidebar
                // lets now test for the arrow area
                if (mPages[aPageNum-1]->bScrollbar)
                    ScrollbarHit(aPageNum,aPoint);
                // lets return true here, even if only the sidebar was hit
                return true;
            }
        }
    }
    return false;
}
Rectangle SwPostItMgr::GetBottomScrollRect(const unsigned long aPage) const
{
    SwRect aPageRect = mPages[aPage-1]->mPageRect;
    Point aPointBottom = mPages[aPage-1]->bMarginSide ? Point(aPageRect.Left() - GetSidebarWidth() - GetSidebarBorderWidth() + mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Bottom()- mpEditWin->PixelToLogic(Size(0,2+GetSidebarScrollerHeight())).Height()) :
                                    Point(aPageRect.Right() + GetSidebarBorderWidth() + mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Bottom()- mpEditWin->PixelToLogic(Size(0,2+GetSidebarScrollerHeight())).Height());
    Size aSize(GetSidebarWidth() - mpEditWin->PixelToLogic(Size(4,0)).Width(), mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height()) ;
    return Rectangle(aPointBottom,aSize);

}

Rectangle SwPostItMgr::GetTopScrollRect(const unsigned long aPage) const
{
    SwRect aPageRect = mPages[aPage-1]->mPageRect;
    Point aPointTop = mPages[aPage-1]->bMarginSide ?    Point(aPageRect.Left() - GetSidebarWidth() -GetSidebarBorderWidth()+ mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Top() + mpEditWin->PixelToLogic(Size(0,2)).Height()) :
                                Point(aPageRect.Right() + GetSidebarBorderWidth() + mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Top() + mpEditWin->PixelToLogic(Size(0,2)).Height());
    Size aSize(GetSidebarWidth() - mpEditWin->PixelToLogic(Size(4,0)).Width(), mpEditWin->PixelToLogic(Size(0,GetSidebarScrollerHeight())).Height()) ;
    return Rectangle(aPointTop,aSize);
}


//IMPORTANT: if you change the rects here, also change SwPageFrm::PaintNotesSidebar()
bool SwPostItMgr::ScrollbarHit(const unsigned long aPage,const Point &aPoint)
{
    SwRect aPageRect = mPages[aPage-1]->mPageRect;
    Point aPointBottom = mPages[aPage-1]->bMarginSide ? Point(aPageRect.Left() - GetSidebarWidth()-GetSidebarBorderWidth() + mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Bottom()- mpEditWin->PixelToLogic(Size(0,2+GetSidebarScrollerHeight())).Height()) :
                                    Point(aPageRect.Right() + GetSidebarBorderWidth()+ mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Bottom()- mpEditWin->PixelToLogic(Size(0,2+GetSidebarScrollerHeight())).Height());

    Point aPointTop = mPages[aPage-1]->bMarginSide ?    Point(aPageRect.Left() - GetSidebarWidth()-GetSidebarBorderWidth()+ mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Top() + mpEditWin->PixelToLogic(Size(0,2)).Height()) :
                                Point(aPageRect.Right()+GetSidebarBorderWidth()+ mpEditWin->PixelToLogic(Size(2,0)).Width(),aPageRect.Top() + mpEditWin->PixelToLogic(Size(0,2)).Height());

    Rectangle aRectBottom(GetBottomScrollRect(aPage));
    Rectangle aRectTop(GetTopScrollRect(aPage));

    if (aRectBottom.IsInside(aPoint))
    {
        if (aPoint.X() < long((aPointBottom.X() + GetSidebarWidth()/3)))
            Scroll( GetScrollSize(),aPage);
        else
            Scroll( -1*GetScrollSize(), aPage);
        return true;
    }
    else
    if (aRectTop.IsInside(aPoint))
    {
        if (aPoint.X() < long((aPointTop.X() + GetSidebarWidth()/3*2)))
            Scroll(GetScrollSize(), aPage);
        else
            Scroll(-1*GetScrollSize(), aPage);
        return true;
    }
    return false;
}

void SwPostItMgr::CorrectPositions()
{
    SwPostIt* pFirstPostIt = (*mvPostItFlds.begin())->pPostIt;
    if (pFirstPostIt && !mbWaitingForCalcRects)
    {
        long aPxPos = pFirstPostIt->GetPosPixel().Y();
        long aPxAnkorPos = mpEditWin->LogicToPixel( Point(0,(long)(pFirstPostIt->Ankor()->GetSixthPosition().getY()))).Y() + 1;
        if (aPxPos != aPxAnkorPos)
        {
            for(SwPostItItem_iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
            {
                SwPostIt* pPostIt = (*i)->pPostIt;
                if (pPostIt)
                {
                    long aY = mpEditWin->LogicToPixel( Point(0,(long)(pPostIt->Ankor()->GetSixthPosition().getY()))).Y() + 1;
                    pPostIt->SetPosPixel(Point(pPostIt->GetPosPixel().X(),aY));
                }

            }
        }
    }
}

bool SwPostItMgr::ShowNotes() const
{
    // we only want to see notes if Options - Writer - View - Notes is ticked
    return mpWrtShell->GetViewOptions()->IsPostIts();
}

bool SwPostItMgr::HasNotes() const
{
    //we just want to know if there are notes, no matter if visible or not
    return !mvPostItFlds.empty();
}

unsigned long SwPostItMgr::GetSidebarWidth(bool bPx) const
{
    unsigned long aWidth = (unsigned long)(mpWrtShell->GetViewOptions()->GetZoom() * 1.8);
    if (bPx)
        return aWidth;
    else
        return mpEditWin->PixelToLogic(Size( aWidth ,0)).Width();
}

unsigned long SwPostItMgr::GetSidebarBorderWidth(bool bPx) const
{
    if (bPx)
        return 2;
    else
        return mpEditWin->PixelToLogic(Size(2,0)).Width();
}

unsigned long SwPostItMgr::GetNoteWidth()
{
    return GetSidebarWidth(true);
}

void SwPostItMgr::SetColors(SwPostIt* pPostIt,SwPostItField* pFld)
{
    if (!Application::GetSettings().GetStyleSettings().GetHighContrastMode())
    {
        static const Color aArrayNormal[] = {
            COL_AUTHOR1_NORMAL,     COL_AUTHOR2_NORMAL,     COL_AUTHOR3_NORMAL,
            COL_AUTHOR4_NORMAL,     COL_AUTHOR5_NORMAL,     COL_AUTHOR6_NORMAL,
            COL_AUTHOR7_NORMAL,     COL_AUTHOR8_NORMAL,     COL_AUTHOR9_NORMAL };

        static const Color aArrayLight[] = {
            COL_AUTHOR1_LIGHT,      COL_AUTHOR2_LIGHT,      COL_AUTHOR3_LIGHT,
            COL_AUTHOR4_LIGHT,      COL_AUTHOR5_LIGHT,      COL_AUTHOR6_LIGHT,
            COL_AUTHOR7_LIGHT,      COL_AUTHOR8_LIGHT,      COL_AUTHOR9_LIGHT };

        static const Color aArrayAnkor[] = {
            COL_AUTHOR1_DARK,       COL_AUTHOR2_DARK,       COL_AUTHOR3_DARK,
            COL_AUTHOR4_DARK,       COL_AUTHOR5_DARK,       COL_AUTHOR6_DARK,
            COL_AUTHOR7_DARK,       COL_AUTHOR8_DARK,       COL_AUTHOR9_DARK };

        sal_uInt16 aAuthorIndex = SW_MOD()->InsertRedlineAuthor(pFld->GetPar1());

        Color aColorDark(   aArrayNormal[ aAuthorIndex % (sizeof( aArrayNormal )/ sizeof( aArrayNormal[0] ))]);
        Color aColorLight(  aArrayLight[  aAuthorIndex % (sizeof( aArrayLight ) / sizeof( aArrayLight[0] ))]);
        Color aColorAnkor(  aArrayAnkor[  aAuthorIndex % (sizeof( aArrayAnkor ) / sizeof( aArrayAnkor[0] ))]);

        pPostIt->SetColor(aColorDark,aColorLight,aColorAnkor);
    }
    else
    {
        Color aWhite(COL_WHITE);
        pPostIt->SetColor(aWhite,aWhite,aWhite);
    }
}

void SwPostItMgr::SetActivePostIt( SwPostIt* p)
{
    if ( p != mpActivePostIt )
    {
        mpActivePostIt = p;
        if (p)
            mpView->AttrChangedNotify(0);
    }
}

IMPL_LINK( SwPostItMgr, CalcHdl, void*, /* pVoid*/  )
{
    mnEventId = 0;
    if ( mbLayouting )
    {
        DBG_ERROR("Reentrance problem in Layout Manager!");
        return 0;
    }

    if ( CalcRects() || mbLayout )
    {
        mbLayout = false;
        LayoutPostIts();
    }
    return 0;
}

void SwPostItMgr::Rescale()
{
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        if ( (*i)->pPostIt )
            (*i)->pPostIt->Rescale();
}

sal_Int32 SwPostItMgr::GetInitialAnchorDistance() const
{
    const Fraction& f( mpEditWin->GetMapMode().GetScaleY() );
    return POSTIT_INITIAL_ANKOR_DISTANCE * f.GetNumerator() / f.GetDenominator();
}

sal_Int32 SwPostItMgr::GetSpaceBetween() const
{
    const Fraction& f( mpEditWin->GetMapMode().GetScaleY() );
    return ( POSTIT_SPACE_BETWEEN ) * f.GetNumerator() / f.GetDenominator();
}

sal_Int32 SwPostItMgr::GetScrollSize() const
{
    const Fraction& f( mpEditWin->GetMapMode().GetScaleY() );
    return ( POSTIT_SPACE_BETWEEN + POSTIT_MINIMUMSIZE_WITH_META ) * f.GetNumerator() / f.GetDenominator();
}

sal_Int32 SwPostItMgr::GetMinimumSizeWithMeta() const
{
    const Fraction& f( mpEditWin->GetMapMode().GetScaleY() );
    return POSTIT_MINIMUMSIZE_WITH_META * f.GetNumerator() / f.GetDenominator();
}

sal_Int32 SwPostItMgr::GetSidebarScrollerHeight() const
{
    const Fraction& f( mpEditWin->GetMapMode().GetScaleY() );
    return POSTIT_SCROLL_SIDEBAR_HEIGHT * f.GetNumerator() / f.GetDenominator();
}

void SwPostItMgr::SetSpellChecking(bool bEnable)
{
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        if ( (*i)->pPostIt )
            (*i)->pPostIt->SetSpellChecking(bEnable);
}

void SwPostItMgr::SetReadOnlyState()
{
    bool bReadOnly = mpView->GetDocShell()->IsReadOnly();
    for(std::list<SwPostItItem*>::iterator i = mvPostItFlds.begin(); i!= mvPostItFlds.end() ; i++)
        if ( (*i)->pPostIt )
            (*i)->pPostIt->SetReadonly( bReadOnly );
}