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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <hintids.hxx>
#include <comphelper/lok.hxx>
#include <tools/mapunit.hxx>
#include <svx/svdhdl.hxx>
#include <svx/svdtrans.hxx>
#include <editeng/protitem.hxx>
#include <svx/svdpage.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/gdimtf.hxx>
#include <vcl/svapp.hxx>
#include <vcl/ptrstyle.hxx>

#include <fmtclds.hxx>
#include <fmtornt.hxx>
#include <fmtfsize.hxx>
#include <fmturl.hxx>
#include <viewsh.hxx>
#include <frmatr.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <dflyobj.hxx>
#include <flyfrm.hxx>
#include <frmfmt.hxx>
#include <viewopt.hxx>
#include <frmtool.hxx>
#include <flyfrms.hxx>
#include <ndnotxt.hxx>
#include <grfatr.hxx>
#include <pagefrm.hxx>
#include <rootfrm.hxx>
#include <textboxhelper.hxx>
#include <wrtsh.hxx>
#include <ndgrf.hxx>
#include <frmmgr.hxx>

#include <svx/sdr/properties/defaultproperties.hxx>
#include <basegfx/range/b2drange.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>

// AW: For VCOfDrawVirtObj and stuff
#include <svx/sdr/contact/viewcontactofvirtobj.hxx>
#include <drawinglayer/primitive2d/baseprimitive2d.hxx>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <sw_primitivetypes2d.hxx>
#include <drawinglayer/primitive2d/sdrdecompositiontools2d.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <notxtfrm.hxx>

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

static bool bInResize = false;


namespace sdr::contact
{
        namespace {

        /**
         * @see #i95264#
         *
         * currently needed since createViewIndependentPrimitive2DSequence() is called when
         * RecalcBoundRect() is used. There should currently no VOCs being constructed since it
         * gets not visualized (instead the corresponding SwVirtFlyDrawObj's referencing this one
         * are visualized).
         */
        class VCOfSwFlyDrawObj : public ViewContactOfSdrObj
        {
        protected:
            /** This method is responsible for creating the graphical visualisation data
             *
             * @note ONLY based on model data
             */
            virtual drawinglayer::primitive2d::Primitive2DContainer createViewIndependentPrimitive2DSequence() const override;

        public:
            /// basic constructor, used from SdrObject.
            explicit VCOfSwFlyDrawObj(SwFlyDrawObj& rObj)
            :   ViewContactOfSdrObj(rObj)
            {
            }
        };

        }

        drawinglayer::primitive2d::Primitive2DContainer VCOfSwFlyDrawObj::createViewIndependentPrimitive2DSequence() const
        {
            // currently gets not visualized, return empty sequence
            return drawinglayer::primitive2d::Primitive2DContainer();
        }

} // end of namespace sdr::contact

std::unique_ptr<sdr::properties::BaseProperties> SwFlyDrawObj::CreateObjectSpecificProperties()
{
    // create default properties
    return std::make_unique<sdr::properties::DefaultProperties>(*this);
}

std::unique_ptr<sdr::contact::ViewContact> SwFlyDrawObj::CreateObjectSpecificViewContact()
{
    // needs an own VC since createViewIndependentPrimitive2DSequence()
    // is called when RecalcBoundRect() is used
    return std::make_unique<sdr::contact::VCOfSwFlyDrawObj>(*this);
}

SwFlyDrawObj::SwFlyDrawObj(SdrModel& rSdrModel)
:   SdrObject(rSdrModel),
    mbIsTextBox(false)
{
}

SwFlyDrawObj::~SwFlyDrawObj()
{
}

// SwFlyDrawObj - Factory-Methods
SdrInventor SwFlyDrawObj::GetObjInventor() const
{
    return SdrInventor::Swg;
}

sal_uInt16 SwFlyDrawObj::GetObjIdentifier() const
{
    return SwFlyDrawObjIdentifier;
}

// TODO: Need own primitive to get the FlyFrame paint working
namespace drawinglayer::primitive2d
{
        namespace {

        class SwVirtFlyDrawObjPrimitive : public BufferedDecompositionPrimitive2D
        {
        private:
            const SwVirtFlyDrawObj&                 mrSwVirtFlyDrawObj;
            const basegfx::B2DRange                 maOuterRange;

        protected:
            /// method which is to be used to implement the local decomposition of a 2D primitive
            virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const override;

        public:
            SwVirtFlyDrawObjPrimitive(
                const SwVirtFlyDrawObj& rSwVirtFlyDrawObj,
                const basegfx::B2DRange &rOuterRange)
            :   BufferedDecompositionPrimitive2D(),
                mrSwVirtFlyDrawObj(rSwVirtFlyDrawObj),
                maOuterRange(rOuterRange)
            {
            }

            virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;

            virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const override;

            // override to allow callbacks to wrap_DoPaintObject
            virtual void get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const override;

            // data read access
            const SwVirtFlyDrawObj& getSwVirtFlyDrawObj() const { return mrSwVirtFlyDrawObj; }
            const basegfx::B2DRange& getOuterRange() const { return maOuterRange; }

            /// provide unique ID
            virtual sal_uInt32 getPrimitive2DID() const override;
        };

        }
} // end of namespace drawinglayer::primitive2d

namespace drawinglayer::primitive2d
{
        void SwVirtFlyDrawObjPrimitive::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
        {
            if(!getOuterRange().isEmpty())
            {
                // currently this SW object has no primitive representation. As long as this is the case,
                // create invisible geometry to allow correct HitTest and BoundRect calculations for the
                // object. Use a filled primitive to get 'inside' as default object hit. The special cases from
                // the old SwVirtFlyDrawObj::CheckHit implementation are handled now in SwDrawView::PickObj;
                // this removed the 'hack' to get a view from inside model data or to react on null-tolerance
                // as it was done in the old implementation
                rContainer.push_back(
                    createHiddenGeometryPrimitives2D(
                        true,
                        getOuterRange()));
            }
        }

        bool SwVirtFlyDrawObjPrimitive::operator==(const BasePrimitive2D& rPrimitive) const
        {
            if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
            {
                const SwVirtFlyDrawObjPrimitive& rCompare = static_cast<const SwVirtFlyDrawObjPrimitive&>(rPrimitive);

                return (&getSwVirtFlyDrawObj() == &rCompare.getSwVirtFlyDrawObj()
                    && getOuterRange() == rCompare.getOuterRange());
            }

            return false;
        }

        basegfx::B2DRange SwVirtFlyDrawObjPrimitive::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
        {
            return getOuterRange();
        }

        void SwVirtFlyDrawObjPrimitive::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
        {
            // This is the callback to keep the FlyFrame painting in SW alive as long as it
            // is not changed to primitives. This is the method which will be called by the processors
            // when they do not know this primitive (and they do not). Inside wrap_DoPaintObject
            // there needs to be a test that paint is only done during SW repaints (see there).
            // Using this mechanism guarantees the correct Z-Order of the VirtualObject-based FlyFrames.
            getSwVirtFlyDrawObj().wrap_DoPaintObject(rViewInformation);

            // call parent
            BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
        }

        // provide unique ID
        ImplPrimitive2DIDBlock(SwVirtFlyDrawObjPrimitive, PRIMITIVE2D_ID_SWVIRTFLYDRAWOBJPRIMITIVE2D)

} // end of namespace drawinglayer::primitive2d

// AW: own sdr::contact::ViewContact (VC) sdr::contact::ViewObjectContact (VOC) needed
// since offset is defined different from SdrVirtObj's sdr::contact::ViewContactOfVirtObj.
// For paint, that offset is used by setting at the OutputDevice; for primitives this is
// not possible since we have no OutputDevice, but define the geometry itself.

namespace sdr::contact
{
        namespace {

        class VCOfSwVirtFlyDrawObj : public ViewContactOfVirtObj
        {
        protected:
            /** This method is responsible for creating the graphical visualisation data
             *
             * @note ONLY based on model data
             */
            virtual drawinglayer::primitive2d::Primitive2DContainer createViewIndependentPrimitive2DSequence() const override;

        public:
            /// basic constructor, used from SdrObject.
            explicit VCOfSwVirtFlyDrawObj(SwVirtFlyDrawObj& rObj)
            :   ViewContactOfVirtObj(rObj)
            {
            }

            /// access to SwVirtFlyDrawObj
            SwVirtFlyDrawObj& GetSwVirtFlyDrawObj() const
            {
                return static_cast<SwVirtFlyDrawObj&>(mrObject);
            }
        };

        }
} // end of namespace sdr::contact

namespace sdr::contact
{
        drawinglayer::primitive2d::Primitive2DContainer VCOfSwVirtFlyDrawObj::createViewIndependentPrimitive2DSequence() const
        {
            drawinglayer::primitive2d::Primitive2DContainer xRetval;
            const SdrObject& rReferencedObject = GetSwVirtFlyDrawObj().GetReferencedObj();

            if(dynamic_cast<const SwFlyDrawObj*>( &rReferencedObject) !=  nullptr)
            {
                // create an own specialized primitive which is used as repaint callpoint and HitTest
                // for HitTest processor (see primitive implementation above)
                const basegfx::B2DRange aOuterRange(GetSwVirtFlyDrawObj().getOuterBound());

                if(!aOuterRange.isEmpty())
                {
                    const drawinglayer::primitive2d::Primitive2DReference xPrimitive(
                        new drawinglayer::primitive2d::SwVirtFlyDrawObjPrimitive(
                            GetSwVirtFlyDrawObj(),
                            aOuterRange));

                    xRetval = drawinglayer::primitive2d::Primitive2DContainer { xPrimitive };
                }
            }

            return xRetval;
        }

} // end of namespace sdr::contact

basegfx::B2DRange SwVirtFlyDrawObj::getOuterBound() const
{
    basegfx::B2DRange aOuterRange;
    const SdrObject& rReferencedObject = GetReferencedObj();

    if(dynamic_cast<const SwFlyDrawObj*>( &rReferencedObject) !=  nullptr)
    {
        const SwFlyFrame* pFlyFrame = GetFlyFrame();

        if(pFlyFrame)
        {
            const tools::Rectangle aOuterRectangle(pFlyFrame->getFrameArea().Pos(), pFlyFrame->getFrameArea().SSize());

            if(!aOuterRectangle.IsEmpty())
            {
                aOuterRange.expand(basegfx::B2DTuple(aOuterRectangle.Left(), aOuterRectangle.Top()));
                aOuterRange.expand(basegfx::B2DTuple(aOuterRectangle.Right(), aOuterRectangle.Bottom()));
            }
        }
    }

    return aOuterRange;
}

basegfx::B2DRange SwVirtFlyDrawObj::getInnerBound() const
{
    basegfx::B2DRange aInnerRange;
    const SdrObject& rReferencedObject = GetReferencedObj();

    if(dynamic_cast<const SwFlyDrawObj*>( &rReferencedObject) !=  nullptr)
    {
        const SwFlyFrame* pFlyFrame = GetFlyFrame();

        if(pFlyFrame)
        {
            const tools::Rectangle aInnerRectangle(pFlyFrame->getFrameArea().Pos() + pFlyFrame->getFramePrintArea().Pos(), pFlyFrame->getFramePrintArea().SSize());

            if(!aInnerRectangle.IsEmpty())
            {
                aInnerRange.expand(basegfx::B2DTuple(aInnerRectangle.Left(), aInnerRectangle.Top()));
                aInnerRange.expand(basegfx::B2DTuple(aInnerRectangle.Right(), aInnerRectangle.Bottom()));
            }
        }
    }

    return aInnerRange;
}

bool SwVirtFlyDrawObj::ContainsSwGrfNode() const
{
    // RotGrfFlyFrame: Check if this is a SwGrfNode
    const SwFlyFrame* pFlyFrame(GetFlyFrame());

    if(nullptr != pFlyFrame && pFlyFrame->Lower() && pFlyFrame->Lower()->IsNoTextFrame())
    {
        const SwNoTextFrame *const pNTF(static_cast<const SwNoTextFrame*>(pFlyFrame->Lower()));

        const SwGrfNode *const pGrfNd(pNTF->GetNode()->GetGrfNode());

        return nullptr != pGrfNd;
    }

    return false;
}

bool SwVirtFlyDrawObj::HasLimitedRotation() const
{
    // RotGrfFlyFrame: If true, this SdrObject supports only limited rotation.
    // This is the case for SwGrfNode instances
    return ContainsSwGrfNode();
}

void SwVirtFlyDrawObj::Rotate(const Point& rRef, long nAngle, double sn, double cs)
{
    if(ContainsSwGrfNode())
    {
        // RotGrfFlyFrame: Here is where the positively completed rotate interaction is executed.
        // Rotation is in 1/100th degree and may be signed (!)
        nAngle /= 10;

        while(nAngle < 0)
        {
            nAngle += 3600;
        }

        SwWrtShell *pShForAngle = nAngle ? dynamic_cast<SwWrtShell*>(GetFlyFrame()->getRootFrame()->GetCurrShell()) : nullptr;
        if (pShForAngle)
        {
            // RotGrfFlyFrame: Add transformation to placeholder object
            Size aSize;
            const sal_uInt16 nOldRot(SwVirtFlyDrawObj::getPossibleRotationFromFraphicFrame(aSize));
            SwFlyFrameAttrMgr aMgr(false, pShForAngle, Frmmgr_Type::NONE);

            aMgr.SetRotation(nOldRot, (nOldRot + static_cast<sal_uInt16>(nAngle)) % 3600, aSize);
        }
    }
    else
    {
        // call parent
        SdrVirtObj::Rotate(rRef, nAngle, sn, cs);
    }
}

std::unique_ptr<sdr::contact::ViewContact> SwVirtFlyDrawObj::CreateObjectSpecificViewContact()
{
    // need an own ViewContact (VC) to allow creation of a specialized primitive
    // for being able to visualize the FlyFrames in primitive renderers
    return std::make_unique<sdr::contact::VCOfSwVirtFlyDrawObj>(*this);
}

SwVirtFlyDrawObj::SwVirtFlyDrawObj(
    SdrModel& rSdrModel,
    SdrObject& rNew,
    SwFlyFrame* pFly)
:   SdrVirtObj(rSdrModel, rNew),
    m_pFlyFrame(pFly)
{
    const SvxProtectItem &rP = m_pFlyFrame->GetFormat()->GetProtect();
    bMovProt = rP.IsPosProtected();
    bSizProt = rP.IsSizeProtected();
}

SwVirtFlyDrawObj::~SwVirtFlyDrawObj()
{
    if ( getSdrPageFromSdrObject() )    //Withdraw SdrPage the responsibility.
        getSdrPageFromSdrObject()->RemoveObject( GetOrdNum() );
}

const SwFrameFormat *SwVirtFlyDrawObj::GetFormat() const
{
    return GetFlyFrame()->GetFormat();
}
SwFrameFormat *SwVirtFlyDrawObj::GetFormat()
{
    return GetFlyFrame()->GetFormat();
}

// --> OD #i102707#
namespace
{
    class RestoreMapMode
    {
        public:
            explicit RestoreMapMode( SwViewShell const * pViewShell )
                : mbMapModeRestored( false )
                , mpOutDev( pViewShell->GetOut() )
            {
                if ( pViewShell->getPrePostMapMode() != mpOutDev->GetMapMode() )
                {
                    mpOutDev->Push(PushFlags::MAPMODE);

                    GDIMetaFile* pMetaFile = mpOutDev->GetConnectMetaFile();
                    if ( pMetaFile &&
                         pMetaFile->IsRecord() && !pMetaFile->IsPause() )
                    {
                        OSL_FAIL( "MapMode restoration during meta file creation is somehow suspect - using <SetRelativeMapMode(..)>, but not sure, if correct." );
                        mpOutDev->SetRelativeMapMode( pViewShell->getPrePostMapMode() );
                    }
                    else
                    {
                        mpOutDev->SetMapMode( pViewShell->getPrePostMapMode() );
                    }

                    mbMapModeRestored = true;
                }
            };

            ~RestoreMapMode()
            {
                if ( mbMapModeRestored )
                {
                    mpOutDev->Pop();
                }
            };

        private:
            bool mbMapModeRestored;
            VclPtr<OutputDevice> mpOutDev;
    };
}
// <--

void SwVirtFlyDrawObj::wrap_DoPaintObject(
    drawinglayer::geometry::ViewInformation2D const& rViewInformation) const
{
    SwViewShell* pShell = m_pFlyFrame->getRootFrame()->GetCurrShell();

    // Only paint when we have a current shell and a DrawingLayer paint is in progress.
    // This avoids evtl. problems with renderers which do processing stuff,
    // but no paints. IsPaintInProgress() depends on SW repaint, so, as long
    // as SW paints self and calls DrawLayer() for Heaven and Hell, this will
    // be correct
    if ( pShell && pShell->IsDrawingLayerPaintInProgress() )
    {
        bool bDrawObject(true);

        if ( !SwFlyFrame::IsPaint( const_cast<SwVirtFlyDrawObj*>(this), pShell ) )
        {
            bDrawObject = false;
        }

        if ( bDrawObject )
        {
            // if there's no viewport set, all fly-frames will be painted,
            // which is slow, wastes memory, and can cause other trouble.
            (void) rViewInformation; // suppress "unused parameter" warning
            assert(comphelper::LibreOfficeKit::isActive() || !rViewInformation.getViewport().isEmpty());
            if ( !m_pFlyFrame->IsFlyInContentFrame() )
            {
                // it is also necessary to restore the VCL MapMode from ViewInformation since e.g.
                // the VCL PixelRenderer resets it at the used OutputDevice. Unfortunately, this
                // excludes shears and rotates which are not expressible in MapMode.
                // OD #i102707#
                // new helper class to restore MapMode - restoration, only if
                // needed and consideration of paint for meta file creation .
                RestoreMapMode aRestoreMapModeIfNeeded( pShell );

                // paint the FlyFrame (use standard VCL-Paint)
                m_pFlyFrame->PaintSwFrame( *pShell->GetOut(), GetFlyFrame()->getFrameArea() );
            }
        }
    }
}

void SwVirtFlyDrawObj::TakeObjInfo( SdrObjTransformInfoRec& rInfo ) const
{
    rInfo.bMoveAllowed =
    rInfo.bResizeFreeAllowed = rInfo.bResizePropAllowed = true;

    // RotGrfFlyFrame: Some rotation may be allowed
    rInfo.bRotateFreeAllowed = rInfo.bRotate90Allowed = HasLimitedRotation();

    rInfo.bMirrorFreeAllowed = rInfo.bMirror45Allowed =
    rInfo.bMirror90Allowed   = rInfo.bShearAllowed    =
    rInfo.bCanConvToPath     = rInfo.bCanConvToPoly   =
    rInfo.bCanConvToPathLineToArea = rInfo.bCanConvToPolyLineToArea = false;
}

// SwVirtFlyDrawObj - Size Determination

void SwVirtFlyDrawObj::SetRect() const
{
    if ( GetFlyFrame()->getFrameArea().HasArea() )
        const_cast<SwVirtFlyDrawObj*>(this)->aOutRect = GetFlyFrame()->getFrameArea().SVRect();
    else
        const_cast<SwVirtFlyDrawObj*>(this)->aOutRect = tools::Rectangle();
}

const tools::Rectangle& SwVirtFlyDrawObj::GetCurrentBoundRect() const
{
    SetRect();
    return aOutRect;
}

const tools::Rectangle& SwVirtFlyDrawObj::GetLastBoundRect() const
{
    return GetCurrentBoundRect();
}

void SwVirtFlyDrawObj::RecalcBoundRect()
{
    SetRect();
}

void SwVirtFlyDrawObj::RecalcSnapRect()
{
    SetRect();
}

const tools::Rectangle& SwVirtFlyDrawObj::GetSnapRect()  const
{
    SetRect();
    return aOutRect;
}

void SwVirtFlyDrawObj::SetSnapRect(const tools::Rectangle& )
{
    tools::Rectangle aTmp( GetLastBoundRect() );
    SetRect();
    SetChanged();
    BroadcastObjectChange();
    if (pUserCall!=nullptr)
        pUserCall->Changed(*this, SdrUserCallType::Resize, aTmp);
}

void SwVirtFlyDrawObj::NbcSetSnapRect(const tools::Rectangle& )
{
    SetRect();
}

const tools::Rectangle& SwVirtFlyDrawObj::GetLogicRect() const
{
    SetRect();
    return aOutRect;
}

void SwVirtFlyDrawObj::SetLogicRect(const tools::Rectangle& )
{
    tools::Rectangle aTmp( GetLastBoundRect() );
    SetRect();
    SetChanged();
    BroadcastObjectChange();
    if (pUserCall!=nullptr)
        pUserCall->Changed(*this, SdrUserCallType::Resize, aTmp);
}

void SwVirtFlyDrawObj::NbcSetLogicRect(const tools::Rectangle& )
{
    SetRect();
}

::basegfx::B2DPolyPolygon SwVirtFlyDrawObj::TakeXorPoly() const
{
    const tools::Rectangle aSourceRectangle(GetFlyFrame()->getFrameArea().SVRect());
    const ::basegfx::B2DRange aSourceRange = vcl::unotools::b2DRectangleFromRectangle(aSourceRectangle);
    ::basegfx::B2DPolyPolygon aRetval;

    aRetval.append(::basegfx::utils::createPolygonFromRect(aSourceRange));

    return aRetval;
}

//  SwVirtFlyDrawObj::Move() and Resize()
void SwVirtFlyDrawObj::NbcMove(const Size& rSiz)
{
    if(GetFlyFrame()->IsFlyFreeFrame() && static_cast< SwFlyFreeFrame* >(GetFlyFrame())->isTransformableSwFrame())
    {
        // RotateFlyFrame3: When we have a change and are in transformed state (e.g. rotation used),
        // we need to fall back to the un-transformed state to keep the old code below
        // working properly. Restore FrameArea and use aOutRect from old FrameArea.
        TransformableSwFrame* pTransformableSwFrame(static_cast<SwFlyFreeFrame*>(GetFlyFrame())->getTransformableSwFrame());
        pTransformableSwFrame->restoreFrameAreas();
        aOutRect = GetFlyFrame()->getFrameArea().SVRect();
    }

    aOutRect.Move( rSiz );
    const Point aOldPos( GetFlyFrame()->getFrameArea().Pos() );
    const Point aNewPos( aOutRect.TopLeft() );
    const SwRect aFlyRect( aOutRect );

    //If the Fly has an automatic align (right or top),
    //so preserve the automatic.
    SwFrameFormat *pFormat = GetFlyFrame()->GetFormat();
    const sal_Int16 eHori = pFormat->GetHoriOrient().GetHoriOrient();
    const sal_Int16 eVert = pFormat->GetVertOrient().GetVertOrient();
    const sal_Int16 eRelHori = pFormat->GetHoriOrient().GetRelationOrient();
    const sal_Int16 eRelVert = pFormat->GetVertOrient().GetRelationOrient();
    //On paragraph bound Flys starting from the new position a new
    //anchor must be set. Anchor and the new RelPos is calculated and
    //placed by the Fly itself.
    if( GetFlyFrame()->IsFlyAtContentFrame() )
    {
        static_cast<SwFlyAtContentFrame*>(GetFlyFrame())->SetAbsPos( aNewPos );
    }
    else
    {
        const SwFrameFormat *pTmpFormat = GetFormat();
        const SwFormatVertOrient &rVert = pTmpFormat->GetVertOrient();
        const SwFormatHoriOrient &rHori = pTmpFormat->GetHoriOrient();
        long lXDiff = aNewPos.X() - aOldPos.X();
        if( rHori.IsPosToggle() && text::HoriOrientation::NONE == eHori &&
            !GetFlyFrame()->FindPageFrame()->OnRightPage() )
            lXDiff = -lXDiff;

        if( GetFlyFrame()->GetAnchorFrame()->IsRightToLeft() &&
            text::HoriOrientation::NONE == eHori )
            lXDiff = -lXDiff;

        long lYDiff = aNewPos.Y() - aOldPos.Y();
        if( GetFlyFrame()->GetAnchorFrame()->IsVertical() )
        {
            //lXDiff -= rVert.GetPos();
            //lYDiff += rHori.GetPos();

            if ( GetFlyFrame()->GetAnchorFrame()->IsVertLR() )
            {
                lXDiff += rVert.GetPos();
                lXDiff = -lXDiff;
            }
            else
            {
                lXDiff -= rVert.GetPos();
                lYDiff += rHori.GetPos();
            }
        }
        else
        {
            lXDiff += rHori.GetPos();
            lYDiff += rVert.GetPos();
        }

        if( GetFlyFrame()->GetAnchorFrame()->IsRightToLeft() &&
            text::HoriOrientation::NONE != eHori )
            lXDiff = GetFlyFrame()->GetAnchorFrame()->getFrameArea().Width() -
                     aFlyRect.Width() - lXDiff;

        const Point aTmp( lXDiff, lYDiff );
        GetFlyFrame()->ChgRelPos( aTmp );
    }

    SwAttrSet aSet( pFormat->GetDoc()->GetAttrPool(),
                                            RES_VERT_ORIENT, RES_HORI_ORIENT );
    SwFormatHoriOrient aHori( pFormat->GetHoriOrient() );
    SwFormatVertOrient aVert( pFormat->GetVertOrient() );
    bool bPut = false;

    if( !GetFlyFrame()->IsFlyLayFrame() &&
        ::GetHtmlMode(pFormat->GetDoc()->GetDocShell()) )
    {
        //In HTML-Mode only automatic aligns are allowed.
        //Only we can try a snap to left/right respectively left-/right border
        const SwFrame* pAnch = GetFlyFrame()->GetAnchorFrame();
        bool bNextLine = false;

        if( !GetFlyFrame()->IsAutoPos() || text::RelOrientation::PAGE_FRAME != aHori.GetRelationOrient() )
        {
            if( text::RelOrientation::CHAR == eRelHori )
            {
                aHori.SetHoriOrient( text::HoriOrientation::LEFT );
                aHori.SetRelationOrient( text::RelOrientation::CHAR );
            }
            else
            {
                bNextLine = true;
                //Horizontal Align:
                const bool bLeftFrame =
                    aFlyRect.Left() < pAnch->getFrameArea().Left() + pAnch->getFramePrintArea().Left(),
                    bLeftPrt = aFlyRect.Left() + aFlyRect.Width() <
                               pAnch->getFrameArea().Left() + pAnch->getFramePrintArea().Width()/2;
                if ( bLeftFrame || bLeftPrt )
                {
                    aHori.SetHoriOrient( text::HoriOrientation::LEFT );
                    aHori.SetRelationOrient( bLeftFrame ? text::RelOrientation::FRAME : text::RelOrientation::PRINT_AREA );
                }
                else
                {
                    const bool bRightFrame = aFlyRect.Left() >
                                       pAnch->getFrameArea().Left() + pAnch->getFramePrintArea().Width();
                    aHori.SetHoriOrient( text::HoriOrientation::RIGHT );
                    aHori.SetRelationOrient( bRightFrame ? text::RelOrientation::FRAME : text::RelOrientation::PRINT_AREA );
                }
            }
            aSet.Put( aHori );
        }
        //Vertical alignment simply is retained principally,
        //only on manual align will be switched over.
        bool bRelChar = text::RelOrientation::CHAR == eRelVert;
        aVert.SetVertOrient( eVert != text::VertOrientation::NONE ? eVert :
                GetFlyFrame()->IsFlyInContentFrame() ? text::VertOrientation::CHAR_CENTER :
                bRelChar && bNextLine ? text::VertOrientation::CHAR_TOP : text::VertOrientation::TOP );
        if( bRelChar )
            aVert.SetRelationOrient( text::RelOrientation::CHAR );
        else
            aVert.SetRelationOrient( text::RelOrientation::PRINT_AREA );
        aSet.Put( aVert );
        bPut = true;
    }

    //We want preferably not to lose the automatic alignments.
    if ( !bPut && bInResize )
    {
        if ( text::HoriOrientation::NONE != eHori )
        {
            aHori.SetHoriOrient( eHori );
            aHori.SetRelationOrient( eRelHori );
            aSet.Put( aHori );
            bPut = true;
        }
        if ( text::VertOrientation::NONE != eVert )
        {
            aVert.SetVertOrient( eVert );
            aVert.SetRelationOrient( eRelVert );
            aSet.Put( aVert );
            bPut = true;
        }
    }
    if ( bPut )
        pFormat->SetFormatAttr( aSet );
}


void SwVirtFlyDrawObj::NbcCrop(const basegfx::B2DPoint& rRef, double fxFact, double fyFact)
{
    // Get Wrt Shell
    SwWrtShell *pSh = dynamic_cast<SwWrtShell*>( GetFlyFrame()->getRootFrame()->GetCurrShell() );

    if (!pSh)
    {
        return;
    }

    GraphicObject const *pGraphicObject = pSh->GetGraphicObj();

    if (!pGraphicObject)
    {
        return;
    }

    // Get graphic object size in 100th of mm
    const MapMode aMapMode100thmm(MapUnit::Map100thMM);
    Size aGraphicSize(pGraphicObject->GetPrefSize());

    if( MapUnit::MapPixel == pGraphicObject->GetPrefMapMode().GetMapUnit() )
    {
        aGraphicSize = Application::GetDefaultDevice()->PixelToLogic( aGraphicSize, aMapMode100thmm );
    }
    else
    {
        aGraphicSize = OutputDevice::LogicToLogic( aGraphicSize, pGraphicObject->GetPrefMapMode(), aMapMode100thmm);
    }

    if( aGraphicSize.IsEmpty() )
    {
        return ;
    }

    const bool bIsTransformableSwFrame(
        GetFlyFrame()->IsFlyFreeFrame() &&
        static_cast< SwFlyFreeFrame* >(GetFlyFrame())->isTransformableSwFrame());

    if(bIsTransformableSwFrame)
    {
        // When we have a change and are in transformed state (e.g. rotation used),
        // we need to fall back to the un-transformed state to keep the old code below
        // working properly. Restore FrameArea and use aOutRect from old FrameArea.
        TransformableSwFrame* pTransformableSwFrame(static_cast<SwFlyFreeFrame*>(GetFlyFrame())->getTransformableSwFrame());
        pTransformableSwFrame->restoreFrameAreas();
        aOutRect = GetFlyFrame()->getFrameArea().SVRect();
    }

    // Compute old and new rect. This will give us the deformation to apply to
    // the object to crop. OldRect is the inner frame, see getFullDragClone()
    // below where getFramePrintAreaTransformation is used as object geometry for Crop
    const tools::Rectangle aOldRect(
        GetFlyFrame()->getFrameArea().TopLeft() + GetFlyFrame()->getFramePrintArea().TopLeft(),
        GetFlyFrame()->getFramePrintArea().SSize());
    const long nOldWidth(aOldRect.GetWidth());
    const long nOldHeight(aOldRect.GetHeight());

    if (!nOldWidth || !nOldHeight)
    {
        return;
    }

    // rRef is relative to the Crop-Action, si in X/Y-Ranges of [0.0 .. 1.0],
    // to get the correct absolute position, transform using the old Rect
    const Point aRef(
        aOldRect.Left() + basegfx::fround(aOldRect.GetWidth() * rRef.getX()),
        aOldRect.Top() + basegfx::fround(aOldRect.GetHeight() * rRef.getY()));

    // apply transformation, use old ResizeRect for now
    tools::Rectangle aNewRect( aOldRect );
    ResizeRect(
        aNewRect,
        aRef,
        Fraction(fxFact),
        Fraction(fyFact));

    // Get old values for crop in 10th of mm
    SfxItemSet aSet( pSh->GetAttrPool(), svl::Items<RES_GRFATR_CROPGRF, RES_GRFATR_CROPGRF>{} );
    pSh->GetCurAttr( aSet );
    SwCropGrf aCrop( aSet.Get(RES_GRFATR_CROPGRF) );

    tools::Rectangle aCropRectangle(
        convertTwipToMm100(aCrop.GetLeft()),
        convertTwipToMm100(aCrop.GetTop()),
        convertTwipToMm100(aCrop.GetRight()),
        convertTwipToMm100(aCrop.GetBottom()) );

    // Compute delta to apply
    double fScaleX = ( aGraphicSize.Width() - aCropRectangle.Left() - aCropRectangle.Right() ) / static_cast<double>(nOldWidth);
    double fScaleY = ( aGraphicSize.Height() - aCropRectangle.Top() - aCropRectangle.Bottom() ) / static_cast<double>(nOldHeight);

    sal_Int32 nDiffLeft = aNewRect.Left() - aOldRect.Left();
    sal_Int32 nDiffTop = aNewRect.Top() - aOldRect.Top();
    sal_Int32 nDiffRight = aNewRect.Right() - aOldRect.Right();
    sal_Int32 nDiffBottom = aNewRect.Bottom() - aOldRect.Bottom();

    // Compute new values in 10th of mm
    sal_Int32 nLeftCrop = static_cast<sal_Int32>( aCropRectangle.Left() + nDiffLeft * fScaleX );
    sal_Int32 nTopCrop = static_cast<sal_Int32>( aCropRectangle.Top() + nDiffTop * fScaleY );
    sal_Int32 nRightCrop = static_cast<sal_Int32>( aCropRectangle.Right() - nDiffRight * fScaleX );
    sal_Int32 nBottomCrop = static_cast<sal_Int32>( aCropRectangle.Bottom() - nDiffBottom * fScaleY );

    // Apply values
    pSh->StartAllAction();
    // pSh->StartUndo(SwUndoId::START);

    // Set new crop values in twips
    aCrop.SetLeft  (convertMm100ToTwip(nLeftCrop));
    aCrop.SetTop   (convertMm100ToTwip(nTopCrop));
    aCrop.SetRight (convertMm100ToTwip(nRightCrop));
    aCrop.SetBottom(convertMm100ToTwip(nBottomCrop));
    pSh->SetAttrItem(aCrop);

    // Set new frame size
    SwFrameFormat *pFormat = GetFormat();
    SwFormatFrameSize aSz( pFormat->GetFrameSize() );
    const long aNewWidth(aNewRect.GetWidth() + (aOutRect.GetWidth() - aOldRect.GetWidth()));
    const long aNewHeight(aNewRect.GetHeight() + (aOutRect.GetHeight() - aOldRect.GetHeight()));
    aSz.SetWidth(aNewWidth);
    aSz.SetHeight(aNewHeight);
    pFormat->GetDoc()->SetAttr( aSz, *pFormat );

    // add move - to make result look better. Fill with defaults
    // for the untransformed case
    Point aNewTopLeft(aNewRect.TopLeft());
    const Point aOldTopLeft(aOldRect.TopLeft());

    if(bIsTransformableSwFrame)
    {
        // Need to correct the NewTopLeft position in transformed state to make
        // the interaction look correct. First, extract rotation
        basegfx::B2DVector aScale, aTranslate;
        double fRotate, fShearX;
        GetFlyFrame()->getFrameAreaTransformation().decompose(aScale, aTranslate, fRotate, fShearX);

        // calc the center of the unchanged object
        const basegfx::B2DPoint aFormerCenter(
            GetFlyFrame()->getFrameAreaTransformation() * basegfx::B2DPoint(0.5, 0.5));

        // define the existing rotation around that former center
        const basegfx::B2DHomMatrix aRotFormerCenter(
            basegfx::utils::createRotateAroundPoint(
                aFormerCenter.getX(),
                aFormerCenter.getY(),
                fRotate));

        // use the new center of the unrotated object, rotate it around the
        // former center
        const Point aNewCenter(aNewRect.Center());
        const basegfx::B2DPoint aRotNewCenter(
            aRotFormerCenter * basegfx::B2DPoint(aNewCenter.X(), aNewCenter.Y()));

        // Create the new TopLeft of the unrotated, cropped object by creating
        // as if re-creating the unrotated geometry
        aNewTopLeft = Point(
            basegfx::fround(aRotNewCenter.getX() - (0.5 * aNewRect.getWidth())),
            basegfx::fround(aRotNewCenter.getY() - (0.5 * aNewRect.getHeight())));
    }

    // check if we have movement and execute if yes
    const Size aDeltaMove(
        aNewTopLeft.X() - aOldTopLeft.X(),
        aNewTopLeft.Y() - aOldTopLeft.Y());

    if(0 != aDeltaMove.Width() || 0 != aDeltaMove.Height())
    {
        NbcMove(aDeltaMove);
    }

    // pSh->EndUndo(SwUndoId::END);
    pSh->EndAllAction();
}

void SwVirtFlyDrawObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
{
    const SwFrame* pTmpFrame = GetFlyFrame()->GetAnchorFrame();

    if( !pTmpFrame )
    {
        pTmpFrame = GetFlyFrame();
    }

    const bool bVertX(pTmpFrame->IsVertical());
    const bool bRTL(pTmpFrame->IsRightToLeft());
    const bool bVertL2RX(pTmpFrame->IsVertLR());
    const bool bUseRightEdge((bVertX && !bVertL2RX ) || bRTL);
    const bool bIsTransformableSwFrame(
        GetFlyFrame()->IsFlyFreeFrame() &&
        static_cast< SwFlyFreeFrame* >(GetFlyFrame())->isTransformableSwFrame());

    if(bIsTransformableSwFrame)
    {
        // When we have a change in transformed state, we need to fall back to the
        // state without possible transformations.
        // In the Resize case to correctly handle the changes, apply to the transformation
        // and extract the new, untransformed state from that modified transformation
        basegfx::B2DHomMatrix aNewMat(GetFlyFrame()->getFrameAreaTransformation());
        const basegfx::B2DPoint aRef(rRef.X(), rRef.Y());

        // apply state to already valid transformation
        aNewMat.translate(-aRef.getX(), -aRef.getY());
        aNewMat.scale(double(xFact), double(yFact));
        aNewMat.translate(aRef.getX(), aRef.getY());

        // get center of transformed state
        const basegfx::B2DPoint aCenter(aNewMat * basegfx::B2DPoint(0.5, 0.5));

        // decompose to extract scale
        basegfx::B2DVector aScale, aTranslate;
        double fRotate, fShearX;
        aNewMat.decompose(aScale, aTranslate, fRotate, fShearX);
        const basegfx::B2DVector aAbsScale(basegfx::absolute(aScale));

        // create new modified, but untransformed OutRect
        aOutRect = tools::Rectangle(
            basegfx::fround(aCenter.getX() - (0.5 * aAbsScale.getX())),
            basegfx::fround(aCenter.getY() - (0.5 * aAbsScale.getY())),
            basegfx::fround(aCenter.getX() + (0.5 * aAbsScale.getX())),
            basegfx::fround(aCenter.getY() + (0.5 * aAbsScale.getY())));

        // restore FrameAreas so that actions below not adapted to new
        // full transformations take the correct actions
        TransformableSwFrame* pTransformableSwFrame(static_cast<SwFlyFreeFrame*>(GetFlyFrame())->getTransformableSwFrame());
        pTransformableSwFrame->restoreFrameAreas();
    }
    else
    {
        ResizeRect( aOutRect, rRef, xFact, yFact );
    }

    // Position may also change, remember old one. This is now already
    // the one in the unrotated, old coordinate system
    Point aOldPos(bUseRightEdge ? GetFlyFrame()->getFrameArea().TopRight() : GetFlyFrame()->getFrameArea().Pos());

    // get target size in old coordinate system
    Size aSz( aOutRect.Right() - aOutRect.Left() + 1, aOutRect.Bottom()- aOutRect.Top()  + 1 );

    // compare with restored FrameArea
    if( aSz != GetFlyFrame()->getFrameArea().SSize() )
    {
        //The width of the columns should not be too narrow
        if ( GetFlyFrame()->Lower() && GetFlyFrame()->Lower()->IsColumnFrame() )
        {
            SwBorderAttrAccess aAccess( SwFrame::GetCache(), GetFlyFrame() );
            const SwBorderAttrs &rAttrs = *aAccess.Get();
            long nMin = rAttrs.CalcLeftLine()+rAttrs.CalcRightLine();
            const SwFormatCol& rCol = rAttrs.GetAttrSet().GetCol();
            if ( rCol.GetColumns().size() > 1 )
            {
                for ( const auto &rC : rCol.GetColumns() )
                {
                    nMin += rC.GetLeft() + rC.GetRight() + MINFLY;<--- Consider using std::accumulate algorithm instead of a raw loop.
                }
                nMin -= MINFLY;
            }
            aSz.setWidth( std::max( aSz.Width(), nMin ) );
        }

        SwFrameFormat *pFormat = GetFormat();
        const SwFormatFrameSize aOldFrameSz( pFormat->GetFrameSize() );
        GetFlyFrame()->ChgSize( aSz );
        SwFormatFrameSize aFrameSz( pFormat->GetFrameSize() );

        if ( aFrameSz.GetWidthPercent() || aFrameSz.GetHeightPercent() )
        {
            long nRelWidth, nRelHeight;
            const SwFrame *pRel = GetFlyFrame()->IsFlyLayFrame() ?
                                GetFlyFrame()->GetAnchorFrame() :
                                GetFlyFrame()->GetAnchorFrame()->GetUpper();
            const SwViewShell *pSh = GetFlyFrame()->getRootFrame()->GetCurrShell();

            if ( pSh && pRel->IsBodyFrame() &&
                 pSh->GetViewOptions()->getBrowseMode() &&
                 pSh->VisArea().HasArea() )
            {
                nRelWidth  = pSh->GetBrowseWidth();
                nRelHeight = pSh->VisArea().Height();
                const Size aBorder = pSh->GetOut()->PixelToLogic( pSh->GetBrowseBorder() );
                nRelHeight -= 2*aBorder.Height();
            }
            else
            {
                nRelWidth  = pRel->getFramePrintArea().Width();
                nRelHeight = pRel->getFramePrintArea().Height();
            }

            if ( aFrameSz.GetWidthPercent() && aFrameSz.GetWidthPercent() != SwFormatFrameSize::SYNCED &&
                 aOldFrameSz.GetWidth() != aFrameSz.GetWidth() )
            {
                aFrameSz.SetWidthPercent( sal_uInt8(aSz.Width() * 100.0 / nRelWidth + 0.5) );
            }

            if ( aFrameSz.GetHeightPercent() && aFrameSz.GetHeightPercent() != SwFormatFrameSize::SYNCED &&
                 aOldFrameSz.GetHeight() != aFrameSz.GetHeight() )
            {
                aFrameSz.SetHeightPercent( sal_uInt8(aSz.Height() * 100.0 / nRelHeight + 0.5) );
            }

            pFormat->GetDoc()->SetAttr( aFrameSz, *pFormat );
        }
    }

    //Position can also be changed, get new one
    const Point aNewPos(bUseRightEdge ? aOutRect.Right() + 1 : aOutRect.Left(), aOutRect.Top());

    if ( aNewPos != aOldPos )
    {
        // Former late change in aOutRect by ChgSize
        // is now taken into account directly by calculating
        // aNewPos *after* calling ChgSize (see old code).
        // Still need to adapt aOutRect since the 'Move' is already applied
        // here (see ResizeRect) and it's the same SdrObject
        const Size aDeltaMove(
                aNewPos.X() - aOldPos.X(),
                aNewPos.Y() - aOldPos.Y());
        aOutRect.Move(-aDeltaMove.Width(), -aDeltaMove.Height());

        // Now, move as needed (no empty delta which was a hack anyways)
        if(bIsTransformableSwFrame)
        {
            // need to save aOutRect to FrameArea, will be restored to aOutRect in
            // SwVirtFlyDrawObj::NbcMove currently for TransformableSwFrames
            SwFrameAreaDefinition::FrameAreaWriteAccess aFrm(*GetFlyFrame());
            aFrm.setSwRect(aOutRect);
        }

        // keep old hack - not clear what happens here
        bInResize = true;
        NbcMove(aDeltaMove);
        bInResize = false;
    }
}

void SwVirtFlyDrawObj::Move(const Size& rSiz)
{
    NbcMove( rSiz );
    SetChanged();
    GetFormat()->GetDoc()->GetIDocumentUndoRedo().DoDrawUndo(false);
}

void SwVirtFlyDrawObj::Resize(const Point& rRef,
                    const Fraction& xFact, const Fraction& yFact, bool /*bUnsetRelative*/)
{
    NbcResize( rRef, xFact, yFact );
    SetChanged();
    GetFormat()->GetDoc()->GetIDocumentUndoRedo().DoDrawUndo(false);
}

void SwVirtFlyDrawObj::Crop(const basegfx::B2DPoint& rRef, double fxFact, double fyFact)
{
    NbcCrop( rRef, fxFact, fyFact );
    SetChanged();
    GetFormat()->GetDoc()->GetIDocumentUndoRedo().DoDrawUndo(false);
}

// RotGrfFlyFrame: Helper to access possible rotation of Graphic contained in FlyFrame
sal_uInt16 SwVirtFlyDrawObj::getPossibleRotationFromFraphicFrame(Size& rSize) const
{
    sal_uInt16 nRetval(0);
    const SwNoTextFrame* pNoTx = dynamic_cast< const SwNoTextFrame* >(GetFlyFrame()->Lower());

    if(pNoTx)
    {
        SwNoTextNode& rNoTNd = const_cast< SwNoTextNode& >(*static_cast<const SwNoTextNode*>(pNoTx->GetNode()));
        SwGrfNode* pGrfNd = rNoTNd.GetGrfNode();

        if(nullptr != pGrfNd)
        {
            const SwAttrSet& rSet = pGrfNd->GetSwAttrSet();
            const SwRotationGrf& rRotation = rSet.GetRotationGrf();

            rSize = rRotation.GetUnrotatedSize();
            nRetval = rRotation.GetValue();
        }
    }

    return nRetval;
}

long SwVirtFlyDrawObj::GetRotateAngle() const
{
    if(ContainsSwGrfNode())
    {
        Size aSize;
        return getPossibleRotationFromFraphicFrame(aSize);
    }
    else
    {
        return SdrVirtObj::GetRotateAngle();
    }
}

SdrObjectUniquePtr SwVirtFlyDrawObj::getFullDragClone() const
{
    // call parent
    SdrObjectUniquePtr pRetval = SdrVirtObj::getFullDragClone();

    if(pRetval && GetFlyFrame() && ContainsSwGrfNode())
    {
        // RotGrfFlyFrame3: get inner bounds/transformation
        const basegfx::B2DHomMatrix aTargetTransform(GetFlyFrame()->getFramePrintAreaTransformation());

        pRetval->TRSetBaseGeometry(aTargetTransform, basegfx::B2DPolyPolygon());
    }

    return pRetval;
}

void SwVirtFlyDrawObj::addCropHandles(SdrHdlList& rTarget) const
{
    // RotGrfFlyFrame: Adapt to possible rotated Graphic contained in FlyFrame
    if(GetFlyFrame()->getFrameArea().HasArea())
    {
        // Use InnerBound, OuterBound (same as GetFlyFrame()->getFrameArea().SVRect())
        // may have a distance to InnerBound which needs to be taken into account.
        // The Graphic is mapped to InnerBound, as is the rotated Graphic.
        const basegfx::B2DRange aTargetRange(getInnerBound());

        if(!aTargetRange.isEmpty())
        {
            // RotGrfFlyFrame3: get inner bounds/transformation
            const basegfx::B2DHomMatrix aTargetTransform(GetFlyFrame()->getFramePrintAreaTransformation());

            // break up matrix
            basegfx::B2DTuple aScale;
            basegfx::B2DTuple aTranslate;
            double fRotate(0.0);
            double fShearX(0.0);
            aTargetTransform.decompose(aScale, aTranslate, fRotate, fShearX);
            basegfx::B2DPoint aPos;

            aPos = aTargetTransform * basegfx::B2DPoint(0.0, 0.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::UpperLeft, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(0.5, 0.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::Upper, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(1.0, 0.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::UpperRight, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(0.0, 0.5);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::Left , fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(1.0, 0.5);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::Right, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(0.0, 1.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::LowerLeft, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(0.5, 1.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::Lower, fShearX, fRotate));
            aPos = aTargetTransform * basegfx::B2DPoint(1.0, 1.0);
            rTarget.AddHdl(std::make_unique<SdrCropHdl>(Point(basegfx::fround(aPos.getX()), basegfx::fround(aPos.getY())), SdrHdlKind::LowerRight, fShearX, fRotate));
        }
    }
}

// Macro

PointerStyle  SwVirtFlyDrawObj::GetMacroPointer(
    const SdrObjMacroHitRec& ) const
{
    return PointerStyle::RefHand;
}

bool SwVirtFlyDrawObj::HasMacro() const
{
    const SwFormatURL &rURL = m_pFlyFrame->GetFormat()->GetURL();
    return rURL.GetMap() || !rURL.GetURL().isEmpty();
}

SdrObject* SwVirtFlyDrawObj::CheckMacroHit( const SdrObjMacroHitRec& rRec ) const
{
    const SwFormatURL &rURL = m_pFlyFrame->GetFormat()->GetURL();
    if( rURL.GetMap() || !rURL.GetURL().isEmpty() )
    {
        SwRect aRect;
        if ( m_pFlyFrame->Lower() && m_pFlyFrame->Lower()->IsNoTextFrame() )
        {
            aRect = m_pFlyFrame->getFramePrintArea();
            aRect += m_pFlyFrame->getFrameArea().Pos();
        }
        else
            aRect = m_pFlyFrame->getFrameArea();

        if( aRect.IsInside( rRec.aPos ) )
        {
            aRect.Pos().setX(aRect.Pos().getX() + rRec.nTol);
            aRect.Pos().setY(aRect.Pos().getY() + rRec.nTol);
            aRect.AddHeight( -(2 * rRec.nTol) );
            aRect.AddWidth( -(2 * rRec.nTol) );

            if( aRect.IsInside( rRec.aPos ) )
            {
                if( !rURL.GetMap() ||
                    m_pFlyFrame->GetFormat()->GetIMapObject( rRec.aPos, m_pFlyFrame ))
                    return const_cast<SwVirtFlyDrawObj*>(this);

                return nullptr;
            }
        }
    }
    return SdrObject::CheckMacroHit( rRec );
}

bool SwVirtFlyDrawObj::IsTextBox() const
{
    return SwTextBoxHelper::isTextBox(GetFormat(), RES_FLYFRMFMT);
}

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