summaryrefslogtreecommitdiff
path: root/svx/source/sdr/overlay/overlaymanagerbuffered.cxx
blob: 38ac0ce98a9619130cd43d64c810ac1d75dd3dbf (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: overlaymanagerbuffered.cxx,v $
 * $Revision: 1.11 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"
#include <svx/sdr/overlay/overlaymanagerbuffered.hxx>
#include <vcl/outdev.hxx>
#include <basegfx/point/b2dpoint.hxx>
#include <basegfx/range/b2drange.hxx>
#include <vcl/salbtype.hxx>
#include <vcl/window.hxx>

// #i72754# 
#include <vcl/bitmap.hxx>
#include <tools/stream.hxx>

// #i75163#
#include <basegfx/matrix/b2dhommatrix.hxx>

//////////////////////////////////////////////////////////////////////////////

namespace sdr
{
    namespace overlay
    {
        void OverlayManagerBuffered::ImpPrepareBufferDevice()
        {
            // compare size of maBufferDevice with size of visible area
            if(maBufferDevice.GetOutputSizePixel() != getOutputDevice().GetOutputSizePixel())
            {
                // set new buffer size, copy as much content as possible (use bool parameter for vcl).
                // Newly uncovered regions will be repainted.
                maBufferDevice.SetOutputSizePixel(getOutputDevice().GetOutputSizePixel(), false);
            }

            // compare the MapModes for zoom/scroll changes
            if(maBufferDevice.GetMapMode() != getOutputDevice().GetMapMode())
            {
                const sal_Bool bZoomed(
                    maBufferDevice.GetMapMode().GetScaleX() != getOutputDevice().GetMapMode().GetScaleX()
                    || maBufferDevice.GetMapMode().GetScaleY() != getOutputDevice().GetMapMode().GetScaleY());

                if(!bZoomed)
                {
                    const Point& rOriginOld = maBufferDevice.GetMapMode().GetOrigin();
                    const Point& rOriginNew = getOutputDevice().GetMapMode().GetOrigin();
                    const sal_Bool bScrolled(rOriginOld != rOriginNew);

                    if(bScrolled)
                    {
                        // get pixel bounds
                        const Point aOriginOldPixel(maBufferDevice.LogicToPixel(rOriginOld));
                        const Point aOriginNewPixel(maBufferDevice.LogicToPixel(rOriginNew));
                        const Size aOutputSizePixel(maBufferDevice.GetOutputSizePixel());

                        // remember and switch off MapMode
                        const sal_Bool bMapModeWasEnabled(maBufferDevice.IsMapModeEnabled());
                        maBufferDevice.EnableMapMode(sal_False);

                        // scroll internally buffered stuff
                        const Point aDestinationOffsetPixel(aOriginNewPixel - aOriginOldPixel);
                        maBufferDevice.DrawOutDev(
                            aDestinationOffsetPixel, aOutputSizePixel, // destination
                            Point(), aOutputSizePixel); // source

                        // restore MapMode
                        maBufferDevice.EnableMapMode(bMapModeWasEnabled);

                        // scroll remembered region, too.
                        if(!maBufferRememberedRangePixel.isEmpty())
                        {
                            const basegfx::B2IPoint aIPointDestinationOffsetPixel(aDestinationOffsetPixel.X(), aDestinationOffsetPixel.Y());
                            const basegfx::B2IPoint aNewMinimum(maBufferRememberedRangePixel.getMinimum() + aIPointDestinationOffsetPixel);
                            const basegfx::B2IPoint aNewMaximum(maBufferRememberedRangePixel.getMaximum() + aIPointDestinationOffsetPixel);
                            maBufferRememberedRangePixel = basegfx::B2IRange(aNewMinimum, aNewMaximum);
                        }
                    }
                }

                // copy new MapMode
                maBufferDevice.SetMapMode(getOutputDevice().GetMapMode());
            }

            // #i29186#
            maBufferDevice.SetDrawMode(getOutputDevice().GetDrawMode());
            maBufferDevice.SetSettings(getOutputDevice().GetSettings());
            maBufferDevice.SetAntialiasing(getOutputDevice().GetAntialiasing());
        }

        void OverlayManagerBuffered::ImpRestoreBackground() const
        {
            const Rectangle aRegionRectanglePixel(
                maBufferRememberedRangePixel.getMinX(), maBufferRememberedRangePixel.getMinY(),
                maBufferRememberedRangePixel.getMaxX(), maBufferRememberedRangePixel.getMaxY());
            const Region aRegionPixel(aRegionRectanglePixel);

            ImpRestoreBackground(aRegionPixel);
        }

        void OverlayManagerBuffered::ImpRestoreBackground(const Region& rRegionPixel) const
        {
            // local region
            Region aRegionPixel(rRegionPixel);
            RegionHandle aRegionHandle(aRegionPixel.BeginEnumRects());
            Rectangle aRegionRectanglePixel;
            
            // MapModes off
            const sal_Bool bMapModeWasEnabledDest(getOutputDevice().IsMapModeEnabled());
            const sal_Bool bMapModeWasEnabledSource(maBufferDevice.IsMapModeEnabled());
            getOutputDevice().EnableMapMode(sal_False);
            ((OverlayManagerBuffered*)this)->maBufferDevice.EnableMapMode(sal_False);

            while(aRegionPixel.GetEnumRects(aRegionHandle, aRegionRectanglePixel))
            {
#ifdef DBG_UTIL
                // #i72754# possible graphical region test only with non-pro
                static bool bDoPaintForVisualControl(false);
                if(bDoPaintForVisualControl)
                {
                    getOutputDevice().SetLineColor(COL_LIGHTGREEN);
                    getOutputDevice().SetFillColor();
                    getOutputDevice().DrawRect(aRegionRectanglePixel);
                }
#endif

                // restore the area
                const Point aTopLeft(aRegionRectanglePixel.TopLeft());
                const Size aSize(aRegionRectanglePixel.GetSize());

                getOutputDevice().DrawOutDev(
                    aTopLeft, aSize, // destination
                    aTopLeft, aSize, // source
                    maBufferDevice);
            }

            aRegionPixel.EndEnumRects(aRegionHandle);

            // restore MapModes
            getOutputDevice().EnableMapMode(bMapModeWasEnabledDest);
            ((OverlayManagerBuffered*)this)->maBufferDevice.EnableMapMode(bMapModeWasEnabledSource);
        }

        void OverlayManagerBuffered::ImpSaveBackground(const Region& rRegion, OutputDevice* pPreRenderDevice)
        {
            // prepare source
            OutputDevice& rSource = (pPreRenderDevice) ? *pPreRenderDevice : getOutputDevice();

            // Ensure buffer is valid
            ImpPrepareBufferDevice();

            // build region which needs to be copied
            Region aRegion(rSource.LogicToPixel(rRegion));

            // limit to PaintRegion if it's a window. This will be evtl. the expanded one,
            // but always the exact redraw area
            if(OUTDEV_WINDOW == rSource.GetOutDevType())
            {
                Window& rWindow = (Window&)rSource;
                Region aPaintRegionPixel = rWindow.LogicToPixel(rWindow.GetPaintRegion());
                aRegion.Intersect(aPaintRegionPixel);

                // #i72754# Make sure content is completetly rendered, the window
                // will be used as source of a DrawOutDev soon
                rWindow.Flush();
            }

            // also limit to buffer size
            const Rectangle aBufferDeviceRectanglePixel = Rectangle(Point(), maBufferDevice.GetOutputSizePixel());
            aRegion.Intersect(aBufferDeviceRectanglePixel);

            // prepare to iterate over the rectangles from the region in pixels
            RegionHandle aRegionHandle(aRegion.BeginEnumRects());
            Rectangle aRegionRectanglePixel;

            // MapModes off
            const sal_Bool bMapModeWasEnabledDest(rSource.IsMapModeEnabled());
            const sal_Bool bMapModeWasEnabledSource(maBufferDevice.IsMapModeEnabled());
            rSource.EnableMapMode(sal_False);
            maBufferDevice.EnableMapMode(sal_False);

            while(aRegion.GetEnumRects(aRegionHandle, aRegionRectanglePixel))
            {
                // for each rectangle, save the area
                Point aTopLeft(aRegionRectanglePixel.TopLeft());
                Size aSize(aRegionRectanglePixel.GetSize());

                maBufferDevice.DrawOutDev(
                    aTopLeft, aSize, // destination
                    aTopLeft, aSize, // source
                    rSource);

#ifdef DBG_UTIL
                // #i72754# possible graphical region test only with non-pro
                static bool bDoPaintForVisualControl(false);
                if(bDoPaintForVisualControl)
                {
                    const sal_Bool bMapModeWasEnabledTest(getOutputDevice().IsMapModeEnabled());
                    getOutputDevice().EnableMapMode(false);
                    getOutputDevice().SetLineColor(COL_LIGHTRED);
                    getOutputDevice().SetFillColor();
                    getOutputDevice().DrawRect(aRegionRectanglePixel);
                    getOutputDevice().EnableMapMode(bMapModeWasEnabledTest);
                }

                static bool bDoSaveForVisualControl(false);
                if(bDoSaveForVisualControl)
                {
                    const Bitmap aBitmap(maBufferDevice.GetBitmap(aTopLeft, aSize));
                    SvFileStream aNew((const String&)String(ByteString( "c:\\test.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
                    aNew << aBitmap;
                }
#endif
            }

            aRegion.EndEnumRects(aRegionHandle);

            // restore MapModes
            rSource.EnableMapMode(bMapModeWasEnabledDest);
            maBufferDevice.EnableMapMode(bMapModeWasEnabledSource);
        }

        IMPL_LINK(OverlayManagerBuffered, ImpBufferTimerHandler, AutoTimer*, /*pTimer*/)
        {
            // stop timer
            maBufferTimer.Stop();

            if(!maBufferRememberedRangePixel.isEmpty())
            {
                // logic size for impDrawMember call
                basegfx::B2DRange aBufferRememberedRangeLogic(
                    maBufferRememberedRangePixel.getMinX(), maBufferRememberedRangePixel.getMinY(),
                    maBufferRememberedRangePixel.getMaxX(), maBufferRememberedRangePixel.getMaxY());
                aBufferRememberedRangeLogic.transform(getOutputDevice().GetInverseViewTransformation());

                const bool bTargetIsWindow(OUTDEV_WINDOW == rmOutputDevice.GetOutDevType());
                Cursor* pCursor = 0;

                // #i75172# switch off VCL cursor during overlay refresh
                if(bTargetIsWindow)
                {
                    Window& rWindow = static_cast< Window& >(rmOutputDevice);
                    pCursor = rWindow.GetCursor();
                    rWindow.SetCursor(0);
                }

                if(DoRefreshWithPreRendering())
                {
                    // #i73602# ensure valid and sized maOutputBufferDevice
                    const Size aDestinationSizePixel(maBufferDevice.GetOutputSizePixel());
                    const Size aOutputBufferSizePixel(maOutputBufferDevice.GetOutputSizePixel());

                    if(aDestinationSizePixel != aOutputBufferSizePixel)
                    {
                        maOutputBufferDevice.SetOutputSizePixel(aDestinationSizePixel);
                    }

                    maOutputBufferDevice.SetMapMode(getOutputDevice().GetMapMode());
                    maOutputBufferDevice.EnableMapMode(sal_False);
                    maOutputBufferDevice.SetDrawMode(maBufferDevice.GetDrawMode());
                    maOutputBufferDevice.SetSettings(maBufferDevice.GetSettings());
                    maOutputBufferDevice.SetAntialiasing(maBufferDevice.GetAntialiasing());

                    // calculate sizes
                    Rectangle aRegionRectanglePixel(
                        maBufferRememberedRangePixel.getMinX(), maBufferRememberedRangePixel.getMinY(),
                        maBufferRememberedRangePixel.getMaxX(), maBufferRememberedRangePixel.getMaxY());

                    // truncate aRegionRectanglePixel to destination pixel size, more does
                    // not need to be prepared since destination is a buffer for a window. So,
                    // maximum size indirectly shall be limited to getOutputDevice().GetOutputSizePixel()
                    if(aRegionRectanglePixel.Left() < 0L)
                    {
                        aRegionRectanglePixel.Left() = 0L;
                    }

                    if(aRegionRectanglePixel.Top() < 0L)
                    {
                        aRegionRectanglePixel.Top() = 0L;
                    }

                    if(aRegionRectanglePixel.Right() > aDestinationSizePixel.getWidth())
                    {
                        aRegionRectanglePixel.Right() = aDestinationSizePixel.getWidth();
                    }

                    if(aRegionRectanglePixel.Bottom() > aDestinationSizePixel.getHeight())
                    {
                        aRegionRectanglePixel.Bottom() = aDestinationSizePixel.getHeight();
                    }

                    // get sizes
                    const Point aTopLeft(aRegionRectanglePixel.TopLeft());
                    const Size aSize(aRegionRectanglePixel.GetSize());

                    {
                        const sal_Bool bMapModeWasEnabledDest(maBufferDevice.IsMapModeEnabled());
                        maBufferDevice.EnableMapMode(sal_False);

                        maOutputBufferDevice.DrawOutDev(
                            aTopLeft, aSize, // destination
                            aTopLeft, aSize, // source
                            maBufferDevice);

                        // restore MapModes
                        maBufferDevice.EnableMapMode(bMapModeWasEnabledDest);
                    }

                    // paint overlay content for remembered region, use
                    // method from base class directly
                    maOutputBufferDevice.EnableMapMode(sal_True);
                    OverlayManager::ImpDrawMembers(aBufferRememberedRangeLogic, maOutputBufferDevice);
                    maOutputBufferDevice.EnableMapMode(sal_False);

                    // copy to output
                    {
                        const sal_Bool bMapModeWasEnabledDest(getOutputDevice().IsMapModeEnabled());
                        getOutputDevice().EnableMapMode(sal_False);

                        getOutputDevice().DrawOutDev(
                            aTopLeft, aSize, // destination
                            aTopLeft, aSize, // source
                            maOutputBufferDevice);

                        // debug
                        /*getOutputDevice().SetLineColor(COL_RED);
                        getOutputDevice().SetFillColor();
                        getOutputDevice().DrawRect(Rectangle(aTopLeft, aSize));*/

                        // restore MapModes
                        getOutputDevice().EnableMapMode(bMapModeWasEnabledDest);
                    }
                }
                else
                {
                    // Restore all rectangles for remembered region from buffer
                    ImpRestoreBackground();

                    // paint overlay content for remembered region, use
                    // method from base class directly
                    OverlayManager::ImpDrawMembers(aBufferRememberedRangeLogic, getOutputDevice());
                }

                // VCL hack for transparent child windows
                // Problem is e.g. a radiobuttion form control in life mode. The used window
                // is a transparence vcl childwindow. This flag only allows the parent window to
                // paint into the child windows area, but there is no mechanism which takes
                // care for a repaint of the child window. A transparent child window is NOT
                // a window which always keeps it's content consistent over the parent, but it's
                // more like just a paint flag for the parent.
                // To get the update, the windows in question are updated manulally here.
                if(bTargetIsWindow)
                {
                    Window& rWindow = static_cast< Window& >(rmOutputDevice);

                    if(rWindow.IsChildTransparentModeEnabled() && rWindow.GetChildCount())
                    {
                        const Rectangle aRegionRectanglePixel(
                            maBufferRememberedRangePixel.getMinX(), maBufferRememberedRangePixel.getMinY(),
                            maBufferRememberedRangePixel.getMaxX(), maBufferRememberedRangePixel.getMaxY());

                        for(sal_uInt16 a(0); a < rWindow.GetChildCount(); a++)
                        {
                            Window* pCandidate = rWindow.GetChild(a);

                            if(pCandidate && pCandidate->IsPaintTransparent())
                            {
                                const Rectangle aCandidatePosSizePixel(pCandidate->GetPosPixel(), pCandidate->GetSizePixel());

                                if(aCandidatePosSizePixel.IsOver(aRegionRectanglePixel))
                                {
                                    pCandidate->Invalidate(INVALIDATE_NOTRANSPARENT|INVALIDATE_CHILDREN);
                                    pCandidate->Update();
                                }
                            }
                        }
                    }
                }

                // #i75172# restore VCL cursor
                if(bTargetIsWindow)
                {
                    Window& rWindow = static_cast< Window& >(rmOutputDevice);
                    rWindow.SetCursor(pCursor);
                }

                // forget remembered Region
                maBufferRememberedRangePixel.reset();
            }

            return 0;
        }

        OverlayManagerBuffered::OverlayManagerBuffered(OutputDevice& rOutputDevice, sal_Bool bRefreshWithPreRendering)
        :	OverlayManager(rOutputDevice),
            mbRefreshWithPreRendering(bRefreshWithPreRendering)
        {
            // Init timer
            maBufferTimer.SetTimeout(1);
            maBufferTimer.SetTimeoutHdl(LINK(this, OverlayManagerBuffered, ImpBufferTimerHandler));
        }

        OverlayManagerBuffered::~OverlayManagerBuffered()
        {
            // Clear timer
            maBufferTimer.Stop();

            if(!maBufferRememberedRangePixel.isEmpty())
            {
                // Restore all rectangles for remembered region from buffer
                ImpRestoreBackground();
            }
        }

        void OverlayManagerBuffered::completeRedraw(const Region& rRegion, OutputDevice* pPreRenderDevice) const
        {
            if(!rRegion.IsEmpty())
            {
                // save new background
                ((OverlayManagerBuffered*)this)->ImpSaveBackground(rRegion, pPreRenderDevice);
            }

            // call parent
            OverlayManager::completeRedraw(rRegion, pPreRenderDevice);
        }

        void OverlayManagerBuffered::flush()
        {
            // call timer handler direct
            ImpBufferTimerHandler(0);
        }

        // #i68597# part of content gets copied, react on it
        void OverlayManagerBuffered::copyArea(const Point& rDestPt, const Point& rSrcPt, const Size& rSrcSize)
        {
            // scroll local buffered area
            maBufferDevice.CopyArea(rDestPt, rSrcPt, rSrcSize);
        }

        void OverlayManagerBuffered::restoreBackground(const Region& rRegion) const
        {
            // restore
            const Region aRegionPixel(getOutputDevice().LogicToPixel(rRegion));
            ImpRestoreBackground(aRegionPixel);

            // call parent
            OverlayManager::restoreBackground(rRegion);
        }

        void OverlayManagerBuffered::invalidateRange(const basegfx::B2DRange& rRange)
        {
            if(!rRange.isEmpty())
            {
                // buffered output, do not invalidate but use the timer
                // to trigger a timer event for refresh
                maBufferTimer.Start();

                // add the discrete range to the remembered region
                // #i75163# use double precision and floor/ceil rounding to get overlapped pixel region, even
                // when the given logic region has a width/height of 0.0. This does NOT work with LogicToPixel
                // since it just transforms the top left and bottom right points equally without taking
                // discrete pixel coverage into account. An empty B2DRange and thus empty logic Rectangle translated
                // to an also empty discrete pixel rectangle - what is wrong.
                basegfx::B2DRange aDiscreteRange(rRange);
                aDiscreteRange.transform(getOutputDevice().GetViewTransformation());

                if(maDrawinglayerOpt.IsAntiAliasing())
                {
                    // assume AA needs one pixel more and invalidate one pixel more
                    const double fDiscreteOne(getDiscreteOne());
                    const basegfx::B2IPoint aTopLeft(
                        (sal_Int32)floor(aDiscreteRange.getMinX() - fDiscreteOne), 
                        (sal_Int32)floor(aDiscreteRange.getMinY() - fDiscreteOne));
                    const basegfx::B2IPoint aBottomRight(
                        (sal_Int32)ceil(aDiscreteRange.getMaxX() + fDiscreteOne), 
                        (sal_Int32)ceil(aDiscreteRange.getMaxY() + fDiscreteOne));

                    maBufferRememberedRangePixel.expand(aTopLeft);
                    maBufferRememberedRangePixel.expand(aBottomRight);
                }
                else
                {
                    const basegfx::B2IPoint aTopLeft((sal_Int32)floor(aDiscreteRange.getMinX()), (sal_Int32)floor(aDiscreteRange.getMinY()));
                    const basegfx::B2IPoint aBottomRight((sal_Int32)ceil(aDiscreteRange.getMaxX()), (sal_Int32)ceil(aDiscreteRange.getMaxY()));

                    maBufferRememberedRangePixel.expand(aTopLeft);
                    maBufferRememberedRangePixel.expand(aBottomRight);
                }
            }
        }

        void OverlayManagerBuffered::SetRefreshWithPreRendering(sal_Bool bNew)
        {
            if(mbRefreshWithPreRendering != bNew)
            {
                mbRefreshWithPreRendering = bNew;
            }
        }
    } // end of namespace overlay
} // end of namespace sdr

//////////////////////////////////////////////////////////////////////////////
// eof