summaryrefslogtreecommitdiffstats
path: root/vcl/source/outdev/wallpaper.cxx
blob: 2ae7cb2e5a26f4a8887e2fdbbf54d6f270e0ec04 (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
/* -*- 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 <vcl/metaact.hxx>
#include <vcl/virdev.hxx>

#include <cassert>

Color OutputDevice::GetReadableFontColor(const Color& rFontColor, const Color& rBgColor) const
{
    if (rBgColor.IsDark() && rFontColor.IsDark())
        return COL_WHITE;
    else if (rBgColor.IsBright() && rFontColor.IsBright())
        return COL_BLACK;
    else
        return rFontColor;
}

void OutputDevice::DrawWallpaper( const tools::Rectangle& rRect,
                                  const Wallpaper& rWallpaper )
{
    assert(!is_double_buffered_window());

    if ( mpMetaFile )
        mpMetaFile->AddAction( new MetaWallpaperAction( rRect, rWallpaper ) );

    if ( !IsDeviceOutputNecessary() || ImplIsRecordLayout() )
        return;

    if ( rWallpaper.GetStyle() != WallpaperStyle::NONE )
    {
        tools::Rectangle aRect = LogicToPixel( rRect );
        aRect.Normalize();

        if ( !aRect.IsEmpty() )
        {
            DrawWallpaper( aRect.Left(), aRect.Top(), aRect.GetWidth(), aRect.GetHeight(),
                               rWallpaper );
        }
    }

    if( mpAlphaVDev )
        mpAlphaVDev->DrawWallpaper( rRect, rWallpaper );
}

void OutputDevice::DrawWallpaper( tools::Long nX, tools::Long nY,
                                  tools::Long nWidth, tools::Long nHeight,
                                  const Wallpaper& rWallpaper )
{
    assert(!is_double_buffered_window());

    if( rWallpaper.IsBitmap() )
        DrawBitmapWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
    else if( rWallpaper.IsGradient() )
        DrawGradientWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
    else
        DrawColorWallpaper(  nX, nY, nWidth, nHeight, rWallpaper );
}

void OutputDevice::DrawColorWallpaper( tools::Long nX, tools::Long nY,
                                       tools::Long nWidth, tools::Long nHeight,
                                       const Wallpaper& rWallpaper )
{
    assert(!is_double_buffered_window());

    // draw wallpaper without border
    Color aOldLineColor = GetLineColor();
    Color aOldFillColor = GetFillColor();
    SetLineColor();
    SetFillColor( rWallpaper.GetColor() );

    bool bMap = mbMap;
    EnableMapMode( false );
    DrawRect( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );
    SetLineColor( aOldLineColor );
    SetFillColor( aOldFillColor );
    EnableMapMode( bMap );
}

void OutputDevice::Erase()
{
    if ( !IsDeviceOutputNecessary() || ImplIsRecordLayout() )
        return;

    if ( mbBackground )
    {
        RasterOp eRasterOp = GetRasterOp();
        if ( eRasterOp != RasterOp::OverPaint )
            SetRasterOp( RasterOp::OverPaint );
        DrawWallpaper( 0, 0, mnOutWidth, mnOutHeight, maBackground );
        if ( eRasterOp != RasterOp::OverPaint )
            SetRasterOp( eRasterOp );
    }

    if( mpAlphaVDev )
        mpAlphaVDev->Erase();
}

void OutputDevice::Erase(const tools::Rectangle& rRect)
{
    const RasterOp eRasterOp = GetRasterOp();
    if ( eRasterOp != RasterOp::OverPaint )
        SetRasterOp( RasterOp::OverPaint );
    DrawWallpaper(rRect, GetBackground());
    if ( eRasterOp != RasterOp::OverPaint )
        SetRasterOp( eRasterOp );

    if (mpAlphaVDev)
        mpAlphaVDev->Erase(rRect);
}

void OutputDevice::DrawBitmapWallpaper( tools::Long nX, tools::Long nY,
                                            tools::Long nWidth, tools::Long nHeight,
                                            const Wallpaper& rWallpaper )
{
    assert(!is_double_buffered_window());

    const BitmapEx* pCached = rWallpaper.ImplGetCachedBitmap();

    GDIMetaFile* pOldMetaFile = mpMetaFile;
    const bool bOldMap = mbMap;

    BitmapEx aBmpEx;
    if( pCached )
        aBmpEx = *pCached;
    else
        aBmpEx = rWallpaper.GetBitmap();

    const tools::Long nBmpWidth = aBmpEx.GetSizePixel().Width();
    const tools::Long nBmpHeight = aBmpEx.GetSizePixel().Height();
    const bool bTransparent = aBmpEx.IsAlpha();

    const WallpaperStyle eStyle = rWallpaper.GetStyle();

    bool bDrawGradientBackground = false;
    bool bDrawColorBackground = false;

    // draw background
    if( bTransparent )
    {
        if( rWallpaper.IsGradient() )
            bDrawGradientBackground = true;
        else
        {
            if( !pCached && !rWallpaper.GetColor().IsTransparent() )
            {
                ScopedVclPtrInstance< VirtualDevice > aVDev(  *this  );
                aVDev->SetBackground( rWallpaper.GetColor() );
                aVDev->SetOutputSizePixel( Size( nBmpWidth, nBmpHeight ) );
                aVDev->DrawBitmapEx( Point(), aBmpEx );
                aBmpEx = aVDev->GetBitmapEx( Point(), aVDev->GetOutputSizePixel() );
            }

            bDrawColorBackground = true;
        }
    }
    else if( eStyle != WallpaperStyle::Tile && eStyle != WallpaperStyle::Scale )
    {
        if( rWallpaper.IsGradient() )
            bDrawGradientBackground = true;
        else
            bDrawColorBackground = true;
    }

    // background of bitmap?
    if( bDrawGradientBackground )
    {
        DrawGradientWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
    }
    else if( bDrawColorBackground && bTransparent )
    {
        DrawColorWallpaper( nX, nY, nWidth, nHeight, rWallpaper );
        bDrawColorBackground = false;
    }

    Point aPos;
    Size aSize;

    // calc pos and size
    if( rWallpaper.IsRect() )
    {
        const tools::Rectangle aBound( LogicToPixel( rWallpaper.GetRect() ) );
        aPos = aBound.TopLeft();
        aSize = aBound.GetSize();
    }
    else
    {
        aPos = Point( 0, 0 );
        aSize = Size( nWidth, nHeight );
    }

    mpMetaFile = nullptr;
    EnableMapMode( false );
    Push( vcl::PushFlags::CLIPREGION );
    IntersectClipRegion( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );

    bool bDrawn = false;

    switch( eStyle )
    {
    case WallpaperStyle::Scale:
        if( !pCached || ( pCached->GetSizePixel() != aSize ) )
        {
            if( pCached )
                rWallpaper.ImplReleaseCachedBitmap();

            aBmpEx = rWallpaper.GetBitmap();
            aBmpEx.Scale( aSize );
            aBmpEx = BitmapEx( aBmpEx.GetBitmap().CreateDisplayBitmap( this ), aBmpEx.GetAlphaMask() );
        }
        break;

    case WallpaperStyle::TopLeft:
        break;

    case WallpaperStyle::Top:
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
        break;

    case WallpaperStyle::TopRight:
        aPos.AdjustX( aSize.Width() - nBmpWidth);
        break;

    case WallpaperStyle::Left:
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
        break;

    case WallpaperStyle::Center:
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
        break;

    case WallpaperStyle::Right:
        aPos.AdjustX(aSize.Width() - nBmpWidth);
        aPos.AdjustY(( aSize.Height() - nBmpHeight ) >> 1 );
        break;

    case WallpaperStyle::BottomLeft:
        aPos.AdjustY( aSize.Height() - nBmpHeight );
        break;

    case WallpaperStyle::Bottom:
        aPos.AdjustX(( aSize.Width() - nBmpWidth ) >> 1 );
        aPos.AdjustY( aSize.Height() - nBmpHeight );
        break;

    case WallpaperStyle::BottomRight:
        aPos.AdjustX( aSize.Width() - nBmpWidth );
        aPos.AdjustY( aSize.Height() - nBmpHeight );
        break;

    default:
        {
            const tools::Long nRight = nX + nWidth - 1;
            const tools::Long nBottom = nY + nHeight - 1;
            tools::Long nFirstX;
            tools::Long nFirstY;

            if( eStyle == WallpaperStyle::Tile )
            {
                nFirstX = aPos.X();
                nFirstY = aPos.Y();
            }
            else
            {
                nFirstX = aPos.X() + ( ( aSize.Width() - nBmpWidth ) >> 1 );
                nFirstY = aPos.Y() + ( ( aSize.Height() - nBmpHeight ) >> 1 );
            }

            const tools::Long nOffX = ( nFirstX - nX ) % nBmpWidth;
            const tools::Long nOffY = ( nFirstY - nY ) % nBmpHeight;
            tools::Long nStartX = nX + nOffX;
            tools::Long nStartY = nY + nOffY;

            if( nOffX > 0 )
                nStartX -= nBmpWidth;

            if( nOffY > 0 )
                nStartY -= nBmpHeight;

            for( tools::Long nBmpY = nStartY; nBmpY <= nBottom; nBmpY += nBmpHeight )
            {
                for( tools::Long nBmpX = nStartX; nBmpX <= nRight; nBmpX += nBmpWidth )
                {
                    DrawBitmapEx( Point( nBmpX, nBmpY ), aBmpEx );
                }
            }
            bDrawn = true;
        }
        break;
    }

    if( !bDrawn )
    {
        // optimized for non-transparent bitmaps
        if( bDrawColorBackground )
        {
            const Size aBmpSize( aBmpEx.GetSizePixel() );
            const Point aTmpPoint;
            const tools::Rectangle aOutRect( aTmpPoint, GetOutputSizePixel() );
            const tools::Rectangle aColRect( Point( nX, nY ), Size( nWidth, nHeight ) );

            tools::Rectangle aWorkRect( 0, 0, aOutRect.Right(), aPos.Y() - 1 );
            aWorkRect.Normalize();
            aWorkRect.Intersection( aColRect );
            if( !aWorkRect.IsEmpty() )
            {
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
                                    rWallpaper );
            }

            aWorkRect = tools::Rectangle( 0, aPos.Y(), aPos.X() - 1, aPos.Y() + aBmpSize.Height() - 1 );
            aWorkRect.Normalize();
            aWorkRect.Intersection( aColRect );
            if( !aWorkRect.IsEmpty() )
            {
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
                                    rWallpaper );
            }

            aWorkRect = tools::Rectangle( aPos.X() + aBmpSize.Width(), aPos.Y(),
                                   aOutRect.Right(), aPos.Y() + aBmpSize.Height() - 1 );
            aWorkRect.Normalize();
            aWorkRect.Intersection( aColRect );
            if( !aWorkRect.IsEmpty() )
            {
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
                                    rWallpaper );
            }

            aWorkRect = tools::Rectangle( 0, aPos.Y() + aBmpSize.Height(),
                                   aOutRect.Right(), aOutRect.Bottom() );
            aWorkRect.Normalize();
            aWorkRect.Intersection( aColRect );
            if( !aWorkRect.IsEmpty() )
            {
                DrawColorWallpaper( aWorkRect.Left(), aWorkRect.Top(),
                                    aWorkRect.GetWidth(), aWorkRect.GetHeight(),
                                    rWallpaper );
            }
        }

        DrawBitmapEx( aPos, aBmpEx );
    }

    rWallpaper.ImplSetCachedBitmap( aBmpEx );

    Pop();
    EnableMapMode( bOldMap );
    mpMetaFile = pOldMetaFile;
}

void OutputDevice::DrawGradientWallpaper( tools::Long nX, tools::Long nY,
                                          tools::Long nWidth, tools::Long nHeight,
                                          const Wallpaper& rWallpaper )
{
    assert(!is_double_buffered_window());

    tools::Rectangle aBound;
    GDIMetaFile* pOldMetaFile = mpMetaFile;
    const bool bOldMap = mbMap;

    aBound = tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) );

    mpMetaFile = nullptr;
    EnableMapMode( false );
    Push( vcl::PushFlags::CLIPREGION );
    IntersectClipRegion( tools::Rectangle( Point( nX, nY ), Size( nWidth, nHeight ) ) );

    DrawGradient( aBound, rWallpaper.GetGradient() );

    Pop();
    EnableMapMode( bOldMap );
    mpMetaFile = pOldMetaFile;
}

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