summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/view/sdwindow.cxx
blob: 6d5481318e8e91784e68b5a1948260431e8947c3 (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
/* -*- 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 <Window.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/request.hxx>

#include <sfx2/viewfrm.hxx>
#include <svx/svxids.hrc>

#include <editeng/outliner.hxx>
#include <editeng/editview.hxx>
#include <editeng/editeng.hxx>

#include <app.hrc>
#include <ViewShell.hxx>
#include <DrawViewShell.hxx>
#include <DrawDocShell.hxx>
#include <PresentationViewShell.hxx>
#include <View.hxx>
#include <FrameView.hxx>
#include <OutlineViewShell.hxx>
#include <OutlineView.hxx>
#include <drawdoc.hxx>
#include <WindowUpdater.hxx>
#include <ViewShellBase.hxx>
#include <uiobject.hxx>

#include <officecfg/Office/Common.hxx>
#include <sal/log.hxx>
#include <tools/debug.hxx>
#include <vcl/commandevent.hxx>
#include <vcl/settings.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/lokhelper.hxx>

namespace sd {

#define SCROLL_LINE_FACT   0.05     ///< factor for line scrolling
#define SCROLL_PAGE_FACT   0.5      ///< factor for page scrolling
#define SCROLL_SENSITIVE   20       ///< sensitive area in pixel
#define ZOOM_MULTIPLICATOR 10000    ///< multiplier to avoid rounding errors
#define MIN_ZOOM           5        ///< minimal zoom factor
#define MAX_ZOOM           3000     ///< maximal zoom factor

Window::Window(vcl::Window* pParent)
    : vcl::DocWindow(pParent, WinBits(WB_CLIPCHILDREN | WB_DIALOGCONTROL)),
      DropTargetHelper( this ),
      maWinPos(0, 0),           // precautionary; but the values should be set
      maViewOrigin(0, 0),       // again from the owner of the window
      maViewSize(1000, 1000),
      maPrevSize(-1,-1),
      mnMinZoom(MIN_ZOOM),
      mnMaxZoom(MAX_ZOOM),
      mbMinZoomAutoCalc(false),
      mbCenterAllowed(true),
      mnTicks (0),
      mpViewShell(nullptr),
      mbUseDropScroll (true)
{
    SetDialogControlFlags( DialogControlFlags::Return | DialogControlFlags::WantFocus );

    MapMode aMap(GetMapMode());
    aMap.SetMapUnit(MapUnit::Map100thMM);
    SetMapMode(aMap);

    // with it, the vcl::WindowColor is used in the slide mode
    SetBackground( Wallpaper( GetSettings().GetStyleSettings().GetWindowColor() ) );

    // adjust contrast mode initially
    bool bUseContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
    GetOutDev()->SetDrawMode( bUseContrast
        ? sd::OUTPUT_DRAWMODE_CONTRAST
        : sd::OUTPUT_DRAWMODE_COLOR );

    // #i78183# Added after discussed with AF
    EnableRTL(false);
}

Window::~Window()
{
    disposeOnce();
}

void Window::dispose()
{
    if (mpViewShell != nullptr)
    {
        WindowUpdater* pWindowUpdater = mpViewShell->GetWindowUpdater();
        if (pWindowUpdater != nullptr)
            pWindowUpdater->UnregisterWindow (this);
    }
    DropTargetHelper::dispose();
    vcl::Window::dispose();
}

void Window::SetViewShell (ViewShell* pViewSh)
{
    WindowUpdater* pWindowUpdater = nullptr;
    // Unregister at device updater of old view shell.
    if (mpViewShell != nullptr)
    {
        pWindowUpdater = mpViewShell->GetWindowUpdater();
        if (pWindowUpdater != nullptr)
            pWindowUpdater->UnregisterWindow (this);
    }

    mpViewShell = pViewSh;

    // Register at device updater of new view shell
    if (mpViewShell != nullptr)
    {
        pWindowUpdater = mpViewShell->GetWindowUpdater();
        if (pWindowUpdater != nullptr)
            pWindowUpdater->RegisterWindow (this);
    }
}

ViewShell* Window::GetViewShell()
{
    return mpViewShell;
}

void Window::CalcMinZoom()
{
    // Are we entitled to change the minimal zoom factor?
    if ( !mbMinZoomAutoCalc )
        return;

    // Get current zoom factor.
    ::tools::Long nZoom = GetZoom();

    // Get the rectangle of the output area in logical coordinates
    // and calculate the scaling factors that would lead to the view
    // area (also called application area) to completely fill the
    // window.
    Size aWinSize = PixelToLogic(GetOutputSizePixel());
    sal_uLong nX = static_cast<sal_uLong>(static_cast<double>(aWinSize.Width())
        * double(ZOOM_MULTIPLICATOR) / static_cast<double>(maViewSize.Width()));
    sal_uLong nY = static_cast<sal_uLong>(static_cast<double>(aWinSize.Height())
        * double(ZOOM_MULTIPLICATOR) / static_cast<double>(maViewSize.Height()));

    // Decide whether to take the larger or the smaller factor.
    sal_uLong nFact = std::min(nX, nY);

    // The factor is transformed according to the current zoom factor.
    nFact = nFact * nZoom / ZOOM_MULTIPLICATOR;
    mnMinZoom = std::max(sal_uInt16(MIN_ZOOM), static_cast<sal_uInt16>(nFact));

    // If the current zoom factor is smaller than the calculated minimal
    // zoom factor then set the new minimal factor as the current zoom
    // factor.
    if ( nZoom < static_cast<::tools::Long>(mnMinZoom) )
        SetZoomFactor(mnMinZoom);
}

void Window::SetMinZoom (::tools::Long nMin)
{
    mnMinZoom = static_cast<sal_uInt16>(nMin);
}

void Window::SetMaxZoom (::tools::Long nMax)
{
    mnMaxZoom = static_cast<sal_uInt16>(nMax);
}

::tools::Long Window::GetZoom() const
{
    if( GetMapMode().GetScaleX().GetDenominator() )
    {
        return ::tools::Long(GetMapMode().GetScaleX() * 100);
    }
    else
    {
        return 0;
    }
}

void Window::Resize()
{
    vcl::Window::Resize();
    CalcMinZoom();

    if( mpViewShell && mpViewShell->GetViewFrame() )
        mpViewShell->GetViewFrame()->GetBindings().Invalidate( SID_ATTR_ZOOMSLIDER );
}

void Window::PrePaint(vcl::RenderContext& /*rRenderContext*/)
{
    if ( mpViewShell )
        mpViewShell->PrePaint();
}

void Window::Paint(vcl::RenderContext& /*rRenderContext*/, const ::tools::Rectangle& rRect)
{
    if ( mpViewShell )
        mpViewShell->Paint(rRect, this);
}

void Window::KeyInput(const KeyEvent& rKEvt)
{
    if (getenv("SD_DEBUG") && rKEvt.GetKeyCode().GetCode() == KEY_F12 && mpViewShell)
    {
        mpViewShell->GetDoc()->dumpAsXml(nullptr);
        if (OutlinerView *pOLV = mpViewShell->GetView()->GetTextEditOutlinerView())
            pOLV->GetEditView().GetEditEngine()->dumpAsXmlEditDoc(nullptr);
        return;
    }

    if (!(mpViewShell && mpViewShell->KeyInput(rKEvt, this)))
    {
        if (mpViewShell && rKEvt.GetKeyCode().GetCode() == KEY_ESCAPE)
        {
            mpViewShell->GetViewShell()->Escape();
        }
        else
        {
            vcl::Window::KeyInput(rKEvt);
        }
    }
}

void Window::MouseButtonDown(const MouseEvent& rMEvt)
{
    if ( mpViewShell )
        mpViewShell->MouseButtonDown(rMEvt, this);
}

void Window::MouseMove(const MouseEvent& rMEvt)
{
    if ( mpViewShell )
        mpViewShell->MouseMove(rMEvt, this);
}

void Window::MouseButtonUp(const MouseEvent& rMEvt)
{
    mnTicks = 0;

    if ( mpViewShell )
        mpViewShell->MouseButtonUp(rMEvt, this);
}

void Window::Command(const CommandEvent& rCEvt)
{
    if (mpViewShell)
        mpViewShell->Command(rCEvt, this);
    //pass at least alt press/release to parent impl
    if (rCEvt.GetCommand() == CommandEventId::ModKeyChange)
        vcl::Window::Command(rCEvt);
    //show the text edit outliner view cursor
    else if (mpViewShell && !HasFocus() && rCEvt.GetCommand() == CommandEventId::CursorPos)
    {
        // tdf#138855 Getting Focus may destroy TextEditOutlinerView so Grab if
        // text editing active, but fetch the TextEditOutlinerView post-grab
        if (mpViewShell->GetView()->IsTextEdit())
        {
            GrabFocus();
            OutlinerView* pOLV = mpViewShell->GetView()->GetTextEditOutlinerView();
            if (pOLV && this == pOLV->GetWindow())
                pOLV->ShowCursor();
        }
    }
}

bool Window::EventNotify( NotifyEvent& rNEvt )
{
    bool bResult = false;
    if ( mpViewShell )
    {
        bResult = mpViewShell->Notify(rNEvt, this);
    }
    if( !bResult )
        bResult = vcl::Window::EventNotify(rNEvt);

    return bResult;
}

void Window::RequestHelp(const HelpEvent& rEvt)
{
    if (!mpViewShell || !mpViewShell->RequestHelp(rEvt))
        vcl::Window::RequestHelp( rEvt );
}

/**
 * Set the position of the upper left corner from the visible area of the
 * window.
 */
void Window::SetWinViewPos(const Point& rPnt)
{
    maWinPos = rPnt;
}

/**
 * Set origin of the representation in respect to the whole working area.
 */
void Window::SetViewOrigin(const Point& rPnt)
{
    maViewOrigin = rPnt;
}

/**
 * Set size of the whole working area which can be seen with the window.
 */
void Window::SetViewSize(const Size& rSize)
{
    maViewSize = rSize;
    CalcMinZoom();
}

void Window::SetCenterAllowed (bool bIsAllowed)
{
    mbCenterAllowed = bIsAllowed;
}

::tools::Long Window::SetZoomFactor(::tools::Long nZoom)
{
    // Clip the zoom factor to the valid range marked by nMinZoom as
    // calculated by CalcMinZoom() and the constant MAX_ZOOM.
    if ( nZoom > MAX_ZOOM )
        nZoom = MAX_ZOOM;
    if ( nZoom < static_cast<::tools::Long>(mnMinZoom) )
        nZoom = mnMinZoom;

    // Set the zoom factor at the window's map mode.
    if (!comphelper::LibreOfficeKit::isActive())
    {
        MapMode aMap(GetMapMode());
        aMap.SetScaleX(Fraction(nZoom, 100));
        aMap.SetScaleY(Fraction(nZoom, 100));
        SetMapMode(aMap);
    }

    // invalidate previous size - it was relative to the old scaling
    maPrevSize = Size(-1,-1);

    // Update the map mode's origin (to what effect?).
    UpdateMapOrigin();

    // Update the view's snapping to the new zoom factor.
    if ( auto pDrawViewShell = dynamic_cast< DrawViewShell *>( mpViewShell ) )
        pDrawViewShell->GetView()->RecalcLogicSnapMagnetic(*GetOutDev());

    // Return the zoom factor just in case it has been changed above to lie
    // inside the valid range.
    return nZoom;
}

void Window::SetZoomIntegral(::tools::Long nZoom)
{
    // Clip the zoom factor to the valid range marked by nMinZoom as
    // previously calculated by <member>CalcMinZoom()</member> and the
    // MAX_ZOOM constant.
    if ( nZoom > MAX_ZOOM )
        nZoom = MAX_ZOOM;
    if ( nZoom < static_cast<::tools::Long>(mnMinZoom) )
        nZoom = mnMinZoom;

    // Calculate the window's new origin.
    Size aSize = PixelToLogic(GetOutputSizePixel());
    ::tools::Long nW = aSize.Width()  * GetZoom() / nZoom;
    ::tools::Long nH = aSize.Height() * GetZoom() / nZoom;
    maWinPos.AdjustX((aSize.Width()  - nW) / 2 );
    maWinPos.AdjustY((aSize.Height() - nH) / 2 );
    if ( maWinPos.X() < 0 ) maWinPos.setX( 0 );
    if ( maWinPos.Y() < 0 ) maWinPos.setY( 0 );

    // Finally update this window's map mode to the given zoom factor that
    // has been clipped to the valid range.
    SetZoomFactor(nZoom);
}

::tools::Long Window::GetZoomForRect( const ::tools::Rectangle& rZoomRect )
{
    ::tools::Long nRetZoom = 100;

    if( (rZoomRect.GetWidth() != 0) && (rZoomRect.GetHeight() != 0))
    {
        // Calculate the scale factors which will lead to the given
        // rectangle being fully visible (when translated accordingly) as
        // large as possible in the output area independently in both
        // coordinate directions .
        sal_uLong nX(0);
        sal_uLong nY(0);

        const Size aWinSize( PixelToLogic(GetOutputSizePixel()) );
        if(rZoomRect.GetHeight())
        {
            nX = static_cast<sal_uLong>(static_cast<double>(aWinSize.Height())
               * double(ZOOM_MULTIPLICATOR) / static_cast<double>(rZoomRect.GetHeight()));
        }

        if(rZoomRect.GetWidth())
        {
            nY = static_cast<sal_uLong>(static_cast<double>(aWinSize.Width())
                * double(ZOOM_MULTIPLICATOR) / static_cast<double>(rZoomRect.GetWidth()));
        }

        // Use the smaller one of both so that the zoom rectangle will be
        // fully visible with respect to both coordinate directions.
        sal_uLong nFact = std::min(nX, nY);

        // Transform the current zoom factor so that it leads to the desired
        // scaling.
        nRetZoom = nFact * GetZoom() / ZOOM_MULTIPLICATOR;

        // Calculate the new origin.
        if ( nFact == 0 )
        {
            // Don't change anything if the scale factor is degenerate.
            nRetZoom = GetZoom();
        }
        else
        {
            // Clip the zoom factor to the valid range marked by nMinZoom as
            // previously calculated by <member>CalcMinZoom()</member> and the
            // MAX_ZOOM constant.
            if ( nRetZoom > MAX_ZOOM )
                nRetZoom = MAX_ZOOM;
            if ( nRetZoom < static_cast<::tools::Long>(mnMinZoom) )
                nRetZoom = mnMinZoom;
       }
    }

    return nRetZoom;
}

/** Recalculate the zoom factor and translation so that the given rectangle
    is displayed centered and as large as possible while still being fully
    visible in the window.
*/
::tools::Long Window::SetZoomRect (const ::tools::Rectangle& rZoomRect)
{
    ::tools::Long nNewZoom = 100;

    if (rZoomRect.GetWidth() == 0 || rZoomRect.GetHeight() == 0)
    {
        // The given rectangle is degenerate.  Use the default zoom factor
        // (above) of 100%.
        SetZoomIntegral(nNewZoom);
    }
    else
    {
        Point aPos = rZoomRect.TopLeft();
        // Transform the output area from pixel coordinates into logical
        // coordinates.
        Size aWinSize = PixelToLogic(GetOutputSizePixel());
        // Paranoia!  The degenerate case of zero width or height has been
        // taken care of above.
        DBG_ASSERT(rZoomRect.GetWidth(), "ZoomRect-Width = 0!");
        DBG_ASSERT(rZoomRect.GetHeight(), "ZoomRect-Height = 0!");

        // Calculate the scale factors which will lead to the given
        // rectangle being fully visible (when translated accordingly) as
        // large as possible in the output area independently in both
        // coordinate directions .
        sal_uLong nX(0);
        sal_uLong nY(0);

        if(rZoomRect.GetHeight())
        {
            nX = static_cast<sal_uLong>(static_cast<double>(aWinSize.Height())
               * double(ZOOM_MULTIPLICATOR) / static_cast<double>(rZoomRect.GetHeight()));
        }

        if(rZoomRect.GetWidth())
        {
            nY = static_cast<sal_uLong>(static_cast<double>(aWinSize.Width())
                * double(ZOOM_MULTIPLICATOR) / static_cast<double>(rZoomRect.GetWidth()));
        }

        // Use the smaller one of both so that the zoom rectangle will be
        // fully visible with respect to both coordinate directions.
        sal_uLong nFact = std::min(nX, nY);

        // Transform the current zoom factor so that it leads to the desired
        // scaling.
        ::tools::Long nZoom = nFact * GetZoom() / ZOOM_MULTIPLICATOR;

        // Calculate the new origin.
        if ( nFact == 0 )
        {
            // Don't change anything if the scale factor is degenerate.
            nNewZoom = GetZoom();
        }
        else
        {
            // Calculate the new window position that centers the given
            // rectangle on the screen.
            if ( nZoom > MAX_ZOOM )
                nFact = nFact * MAX_ZOOM / nZoom;

            maWinPos = maViewOrigin + aPos;

            aWinSize.setWidth( static_cast<::tools::Long>(static_cast<double>(aWinSize.Width()) * double(ZOOM_MULTIPLICATOR) / static_cast<double>(nFact)) );
            maWinPos.AdjustX((rZoomRect.GetWidth() - aWinSize.Width()) / 2 );
            aWinSize.setHeight( static_cast<::tools::Long>(static_cast<double>(aWinSize.Height()) * double(ZOOM_MULTIPLICATOR) / static_cast<double>(nFact)) );
            maWinPos.AdjustY((rZoomRect.GetHeight() - aWinSize.Height()) / 2 );

            if ( maWinPos.X() < 0 ) maWinPos.setX( 0 );
            if ( maWinPos.Y() < 0 ) maWinPos.setY( 0 );

            // Adapt the window's map mode to the new zoom factor.
            nNewZoom = SetZoomFactor(nZoom);
        }
    }

    return nNewZoom;
}

void Window::SetMinZoomAutoCalc (bool bAuto)
{
    mbMinZoomAutoCalc = bAuto;
}

/**
 * Calculate and set new MapMode origin.
 * If aWinPos.X()/Y() == -1, then we center the corresponding position (e.g. for
 * initialization).
 */
void Window::UpdateMapOrigin(bool bInvalidate)
{
    bool       bChanged = false;
    const Size aWinSize = PixelToLogic(GetOutputSizePixel());

    if ( mbCenterAllowed )
    {
        if( maPrevSize != Size(-1,-1) )
        {
            // keep view centered around current pos, when window
            // resizes
            maWinPos.AdjustX( -((aWinSize.Width() - maPrevSize.Width()) / 2) );
            maWinPos.AdjustY( -((aWinSize.Height() - maPrevSize.Height()) / 2) );
            bChanged = true;
        }

        if ( maWinPos.X() > maViewSize.Width() - aWinSize.Width() )
        {
            maWinPos.setX( maViewSize.Width() - aWinSize.Width() );
            bChanged = true;
        }
        if ( maWinPos.Y() > maViewSize.Height() - aWinSize.Height() )
        {
            maWinPos.setY( maViewSize.Height() - aWinSize.Height() );
            bChanged = true;
        }
        if ( aWinSize.Width() > maViewSize.Width() || maWinPos.X() < 0 )
        {
            maWinPos.setX( maViewSize.Width()  / 2 - aWinSize.Width()  / 2 );
            bChanged = true;
        }
        if ( aWinSize.Height() > maViewSize.Height() || maWinPos.Y() < 0 )
        {
            maWinPos.setY( maViewSize.Height() / 2 - aWinSize.Height() / 2 );
            bChanged = true;
        }
    }

    UpdateMapMode ();

    maPrevSize = aWinSize;

    // When tiled rendering, the above UpdateMapMode() call doesn't touch the map mode.
    if (bChanged && bInvalidate && !comphelper::LibreOfficeKit::isActive())
        Invalidate();
}

void Window::UpdateMapMode()
{
    maWinPos -= maViewOrigin;
    Size aPix(maWinPos.X(), maWinPos.Y());
    aPix = LogicToPixel(aPix);
    // Size has to be a multiple of BRUSH_SIZE due to the correct depiction of
    // pattern
    // #i2237#
    // removed old stuff here which still forced zoom to be
    // %BRUSH_SIZE which is outdated now

    if (dynamic_cast< DrawViewShell *>( mpViewShell ))
    {
        // page should not "stick" to the window border
        if (aPix.Width() == 0)
        {
            // #i2237#
            // Since BRUSH_SIZE alignment is outdated now, i use the
            // former constant here directly
            aPix.AdjustWidth( -8 );
        }
        if (aPix.Height() == 0)
        {
            // #i2237#
            // Since BRUSH_SIZE alignment is outdated now, i use the
            // former constant here directly
            aPix.AdjustHeight( -8 );
        }
    }

    aPix = PixelToLogic(aPix);
    maWinPos.setX( aPix.Width() );
    maWinPos.setY( aPix.Height() );
    Point aNewOrigin (-maWinPos.X(), -maWinPos.Y());
    maWinPos += maViewOrigin;

    if (!comphelper::LibreOfficeKit::isActive())
    {
        MapMode aMap(GetMapMode());
        aMap.SetOrigin(aNewOrigin);
        SetMapMode(aMap);
    }
}

/**
 * @returns X position of the visible area as fraction (< 1) of the whole
 * working area.
 */
double Window::GetVisibleX() const
{
    return maViewSize.Width() == 0 ? 0 : (static_cast<double>(maWinPos.X()) / maViewSize.Width());
}

/**
 * @returns Y position of the visible area as fraction (< 1) of the whole
 * working area.
 */
double Window::GetVisibleY() const
{
    return maViewSize.Height() == 0 ? 0 : (static_cast<double>(maWinPos.Y()) / maViewSize.Height());
}

/**
 * Set x and y position of the visible area as fraction (< 1) of the whole
 * working area. Negative values are ignored.
 */
void Window::SetVisibleXY(double fX, double fY)
{
    ::tools::Long nOldX = maWinPos.X();
    ::tools::Long nOldY = maWinPos.Y();

    if ( fX >= 0 )
        maWinPos.setX( static_cast<::tools::Long>(fX * maViewSize.Width()) );
    if ( fY >= 0 )
        maWinPos.setY( static_cast<::tools::Long>(fY * maViewSize.Height()) );
    UpdateMapOrigin(false);
    Scroll(nOldX - maWinPos.X(), nOldY - maWinPos.Y(), ScrollFlags::Children);
    PaintImmediately();
}

/**
 * @returns width of the visible area in proportion to the width of the whole
 * working area.
 */
double Window::GetVisibleWidth() const
{
    Size aWinSize = PixelToLogic(GetOutputSizePixel());
    if ( aWinSize.Width() > maViewSize.Width() )
        aWinSize.setWidth( maViewSize.Width() );
    return
        maViewSize.Width() == 0 ? 0 : (static_cast<double>(aWinSize.Width()) / maViewSize.Width());
}

/**
 * @returns height of the visible area in proportion to the height of the whole
 * working area.
 */
double Window::GetVisibleHeight() const
{
    Size aWinSize = PixelToLogic(GetOutputSizePixel());
    if ( aWinSize.Height() > maViewSize.Height() )
        aWinSize.setHeight( maViewSize.Height() );
    return maViewSize.Height() == 0
        ? 0 : (static_cast<double>(aWinSize.Height()) / maViewSize.Height());
}

Point Window::GetVisibleCenter()
{
    Point aPos = ::tools::Rectangle(Point(), GetOutputSizePixel()).Center();

    // For LOK
    bool bMapModeWasEnabled(IsMapModeEnabled());
    EnableMapMode(/*true*/);
    aPos = PixelToLogic(aPos);
    EnableMapMode(bMapModeWasEnabled);

    return aPos;
}

/**
 * @returns width of a scroll column in proportion to the width of the whole
 * working area.
 */
double Window::GetScrlLineWidth() const
{
    return (GetVisibleWidth() * SCROLL_LINE_FACT);
}

/**
 * @returns height of a scroll column in proportion to the height of the whole
 * working area.
 */
double Window::GetScrlLineHeight() const
{
    return (GetVisibleHeight() * SCROLL_LINE_FACT);
}

/**
 * @returns width of a scroll page in proportion to the width of the whole
 * working area.
 */
double Window::GetScrlPageWidth() const
{
    return (GetVisibleWidth() * SCROLL_PAGE_FACT);
}

/**
 * @returns height of a scroll page in proportion to the height of the whole
 * working area.
 */
double Window::GetScrlPageHeight() const
{
    return (GetVisibleHeight() * SCROLL_PAGE_FACT);
}

/**
 * Deactivate window.
 */
void Window::LoseFocus()
{
    mnTicks = 0;
    vcl::Window::LoseFocus ();
}

/**
 * Activate window.
 */
void Window::GrabFocus()
{
    mnTicks      = 0;
    vcl::Window::GrabFocus ();
}

void Window::DataChanged( const DataChangedEvent& rDCEvt )
{
    vcl::Window::DataChanged( rDCEvt );

    /* Omit PRINTER by all documents which are not using a printer.
       Omit FONTS and FONTSUBSTITUTION if no text output is available or if the
       document does not allow text.  */

    if ( !((rDCEvt.GetType() == DataChangedEventType::PRINTER) ||
         (rDCEvt.GetType() == DataChangedEventType::DISPLAY) ||
         (rDCEvt.GetType() == DataChangedEventType::FONTS) ||
         (rDCEvt.GetType() == DataChangedEventType::FONTSUBSTITUTION) ||
         ((rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
          (rDCEvt.GetFlags() & AllSettingsFlags::STYLE))) )
        return;

    if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
         (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
    {
        /* Rearrange or initiate Resize for scroll bars since the size of
           the scroll bars my have changed. Within this, inside the resize-
           handler, the size of the scroll bars will be asked from the
           Settings. */
        Resize();

        /* Re-set data, which are from system control or from Settings. May
           have to re-set more data since the resolution may also has
           changed. */
        if( mpViewShell )
        {
            const StyleSettings&    rStyleSettings = GetSettings().GetStyleSettings();
            DrawModeFlags           nOutputMode;
            sal_uInt16              nPreviewSlot;

            if( rStyleSettings.GetHighContrastMode() )
                nOutputMode = sd::OUTPUT_DRAWMODE_CONTRAST;
            else
                nOutputMode = sd::OUTPUT_DRAWMODE_COLOR;

            if( rStyleSettings.GetHighContrastMode()
                && officecfg::Office::Common::Accessibility::IsForPagePreviews::get() )
                nPreviewSlot = SID_PREVIEW_QUALITY_CONTRAST;
            else
                nPreviewSlot = SID_PREVIEW_QUALITY_COLOR;

            if( dynamic_cast< DrawViewShell *>( mpViewShell ) !=  nullptr )
            {
                GetOutDev()->SetDrawMode( nOutputMode );
                mpViewShell->GetFrameView()->SetDrawMode( nOutputMode );
                Invalidate();
            }

            // Overwrite window color for OutlineView
            if( dynamic_cast< OutlineViewShell *>( mpViewShell ) !=  nullptr )
            {
                svtools::ColorConfig aColorConfig;
                const Color aDocColor( aColorConfig.GetColorValue( svtools::DOCCOLOR ).nColor );
                SetBackground( Wallpaper( aDocColor ) );
            }

            SfxRequest aReq( nPreviewSlot, SfxCallMode::SLOT, mpViewShell->GetDocSh()->GetDoc()->GetItemPool() );
            mpViewShell->ExecReq( aReq );
            mpViewShell->Invalidate();
            mpViewShell->ArrangeGUIElements();

            // re-create handles to show new outfit
            if(dynamic_cast< DrawViewShell *>( mpViewShell ) !=  nullptr)
            {
                mpViewShell->GetView()->AdjustMarkHdl();
            }
        }
    }

    if ( (rDCEvt.GetType() == DataChangedEventType::DISPLAY) ||
         ((rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
          (rDCEvt.GetFlags() & AllSettingsFlags::STYLE)) )
    {
        /* Virtual devices, which also depends on the resolution or the
           system control, should be updated. Otherwise, we should update
           the virtual devices at least at DataChangedEventType::DISPLAY since some
           systems allow to change the resolution and color depth during
           runtime. Or the virtual devices have to be updated when the color
           palette has changed since a different color matching can be used
           when outputting. */
    }

    if ( rDCEvt.GetType() == DataChangedEventType::FONTS )
    {
        /* If the document provides font choose boxes, we have to update
           them. I don't know how this looks like (also not really me, I
           only translated the comment ;). We may can handle it global. We
           have to discuss it with PB, but he is ill at the moment.
           Before we handle it here, discuss it with PB and me. */
    }

    if ( (rDCEvt.GetType() == DataChangedEventType::FONTS) ||
         (rDCEvt.GetType() == DataChangedEventType::FONTSUBSTITUTION) )
    {
        /* Do reformatting since the fonts of the document may no longer
           exist, or exist now, or are replaced with others. */
        if( mpViewShell )
        {
            DrawDocShell* pDocSh = mpViewShell->GetDocSh();
            if( pDocSh )
                pDocSh->SetPrinter( pDocSh->GetPrinter( true ) );
        }
    }

    if ( rDCEvt.GetType() == DataChangedEventType::PRINTER )
    {
        /* I don't know how the handling should look like. Maybe we delete a
           printer and look what we have to do. Maybe I have to add
           something to the VCL, in case the used printer is deleted.
           Otherwise I may recalculate the formatting here if the current
           printer is destroyed. */
        if( mpViewShell )
        {
            DrawDocShell* pDocSh = mpViewShell->GetDocSh();
            if( pDocSh )
                pDocSh->SetPrinter( pDocSh->GetPrinter( true ) );
        }
    }

    // Update everything
    Invalidate();
}

sal_Int8 Window::AcceptDrop( const AcceptDropEvent& rEvt )
{
    sal_Int8 nRet = DND_ACTION_NONE;

    if( mpViewShell && !mpViewShell->GetDocSh()->IsReadOnly() )
    {
        nRet = mpViewShell->AcceptDrop( rEvt, *this, this, SDRPAGE_NOTFOUND, SDRLAYER_NOTFOUND );

        if (mbUseDropScroll && dynamic_cast< OutlineViewShell *>( mpViewShell ) ==  nullptr)
            DropScroll( rEvt.maPosPixel );
    }

    return nRet;
}

sal_Int8 Window::ExecuteDrop( const ExecuteDropEvent& rEvt )
{
    sal_Int8 nRet = DND_ACTION_NONE;

    if( mpViewShell )
    {
        nRet = mpViewShell->ExecuteDrop( rEvt, *this, this, SDRPAGE_NOTFOUND, SDRLAYER_NOTFOUND );
    }

    return nRet;
}

void Window::SetUseDropScroll (bool bUseDropScroll)
{
    mbUseDropScroll = bUseDropScroll;
}

void Window::DropScroll(const Point& rMousePos)
{
    short nDx = 0;
    short nDy = 0;

    Size aSize = GetOutputSizePixel();

    if (aSize.Width() > SCROLL_SENSITIVE * 3)
    {
        if ( rMousePos.X() < SCROLL_SENSITIVE )
        {
            nDx = -1;
        }

        if ( rMousePos.X() >= aSize.Width() - SCROLL_SENSITIVE )
        {
            nDx = 1;
        }
    }

    if (aSize.Height() > SCROLL_SENSITIVE * 3)
    {
        if ( rMousePos.Y() < SCROLL_SENSITIVE )
        {
            nDy = -1;
        }

        if ( rMousePos.Y() >= aSize.Height() - SCROLL_SENSITIVE )
        {
            nDy = 1;
        }
    }

    if ( (nDx || nDy) && (rMousePos.X()!=0 || rMousePos.Y()!=0 ) )
    {
        if (mnTicks > 20)
            mpViewShell->ScrollLines(nDx, nDy);
        else
            mnTicks ++;
    }
}

css::uno::Reference<css::accessibility::XAccessible>
    Window::CreateAccessible()
{
    // If current viewshell is PresentationViewShell, just return empty because the correct ShowWin will be created later.
    if (dynamic_cast< PresentationViewShell *>( mpViewShell ))
    {
        return vcl::Window::CreateAccessible ();
    }
    css::uno::Reference< css::accessibility::XAccessible > xAcc = GetAccessible(false);
    if (xAcc)
    {
        return xAcc;
    }
    if (mpViewShell != nullptr)
    {
        xAcc = mpViewShell->CreateAccessibleDocumentView (this);
        SetAccessible(xAcc);
        return xAcc;
    }
    else
    {
        SAL_WARN("sd", "::sd::Window::CreateAccessible: no view shell");
        return vcl::Window::CreateAccessible ();
    }
}

OutlinerView* Window::GetOutlinerView() const
{
    OutlinerView *pOLV = nullptr;
    sd::View* pView = mpViewShell->GetView();
    if (mpViewShell->GetShellType() == ViewShell::ST_OUTLINE)
    {
        if (OutlineView* pOView = dynamic_cast<OutlineView*>(pView))
            pOLV = pOView->GetViewByWindow(this);
    }
    else if (pView->IsTextEdit())
    {
        pOLV = pView->GetTextEditOutlinerView();
    }
    return pOLV;
}

OUString Window::GetSurroundingText() const
{
    OutlinerView *pOLV = GetOutlinerView();
    if (pOLV)
        return pOLV->GetEditView().GetSurroundingText();
    return OUString();
}

Selection Window::GetSurroundingTextSelection() const
{
    OutlinerView *pOLV = GetOutlinerView();
    if (pOLV)
        return pOLV->GetEditView().GetSurroundingTextSelection();
    return Selection( 0, 0 );
}

bool Window::DeleteSurroundingText(const Selection& rSelection)
{
    OutlinerView *pOLV = GetOutlinerView();
    if (pOLV)
        return pOLV->GetEditView().DeleteSurroundingText(rSelection);
    return false;
}

void Window::LogicInvalidate(const ::tools::Rectangle* pRectangle)
{
    DrawViewShell* pDrawViewShell = dynamic_cast<DrawViewShell*>(mpViewShell);
    if (!pDrawViewShell || pDrawViewShell->IsInSwitchPage())
        return;

    if (!comphelper::LibreOfficeKit::isActive())
        return;
    ::tools::Rectangle aRectangle;
    ::tools::Rectangle* pResultRectangle;
    if (!pRectangle)
        pResultRectangle = nullptr;
    else
    {
        aRectangle = *pRectangle;
        if (GetMapMode().GetMapUnit() == MapUnit::Map100thMM)
        {
            aRectangle = o3tl::convert(aRectangle, o3tl::Length::mm100, o3tl::Length::twip);
        }
        pResultRectangle = &aRectangle;
    }
    SfxViewShell& rSfxViewShell = pDrawViewShell->GetViewShellBase();
    SfxLokHelper::notifyInvalidation(&rSfxViewShell, pResultRectangle);
}

FactoryFunction Window::GetUITestFactory() const
{
    if (get_id() == "impress_win")
        return ImpressWindowUIObject::create;

    return WindowUIObject::create;
}

} // end of namespace sd

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