desktop.c 7.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Desktop window class.
 *
 * Copyright 1994 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
20

21 22
#include "config.h"

23
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24 25
#include <stdio.h>
#include <string.h>
26 27 28
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
29

30
#include "windef.h"
31
#include "winbase.h"
32
#include "wingdi.h"
33
#include "winnls.h"
34
#include "controls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36 37 38 39
static HBRUSH hbrushPattern;
static HBITMAP hbitmapWallPaper;
static SIZE bitmapSize;
static BOOL fTileWallPaper;
40 41 42 43 44 45 46


/*********************************************************************
 * desktop class descriptor
 */
const struct builtin_class_descr DESKTOP_builtin_class =
{
47
    (LPCWSTR)DESKTOP_CLASS_ATOM, /* name */
48
    CS_DBLCLKS,           /* style */
49
    WINPROC_DESKTOP,      /* proc */
50
    0,                    /* extra */
51
    IDC_ARROW,            /* cursor */
52
    (HBRUSH)(COLOR_BACKGROUND+1)    /* brush */
53 54
};

55

Alexandre Julliard's avatar
Alexandre Julliard committed
56 57 58 59 60
/***********************************************************************
 *           DESKTOP_LoadBitmap
 *
 * Load a bitmap from a file. Used by SetDeskWallPaper().
 */
61
static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
Alexandre Julliard's avatar
Alexandre Julliard committed
62 63 64
{
    BITMAPFILEHEADER *fileHeader;
    BITMAPINFO *bitmapInfo;
65 66
    HBITMAP hbitmap;
    HFILE file;
Alexandre Julliard's avatar
Alexandre Julliard committed
67 68
    LPSTR buffer;
    LONG size;
Alexandre Julliard's avatar
Alexandre Julliard committed
69

Alexandre Julliard's avatar
Alexandre Julliard committed
70
    /* Read all the file into memory */
Alexandre Julliard's avatar
Alexandre Julliard committed
71

72
    if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
Alexandre Julliard's avatar
Alexandre Julliard committed
73
    {
74
        UINT len = GetWindowsDirectoryA( NULL, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
75 76
        if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
                                  len + strlen(filename) + 2 )))
Alexandre Julliard's avatar
Alexandre Julliard committed
77
            return 0;
78
        GetWindowsDirectoryA( buffer, len + 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
79
        strcat( buffer, "\\" );
Alexandre Julliard's avatar
Alexandre Julliard committed
80
        strcat( buffer, filename );
81
        file = _lopen( buffer, OF_READ );
Alexandre Julliard's avatar
Alexandre Julliard committed
82
        HeapFree( GetProcessHeap(), 0, buffer );
Alexandre Julliard's avatar
Alexandre Julliard committed
83
    }
84 85
    if (file == HFILE_ERROR) return 0;
    size = _llseek( file, 0, 2 );
Alexandre Julliard's avatar
Alexandre Julliard committed
86
    if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
Alexandre Julliard's avatar
Alexandre Julliard committed
87
    {
88
	_lclose( file );
Alexandre Julliard's avatar
Alexandre Julliard committed
89 90
	return 0;
    }
91 92 93
    _llseek( file, 0, 0 );
    size = _lread( file, buffer, size );
    _lclose( file );
Alexandre Julliard's avatar
Alexandre Julliard committed
94 95
    fileHeader = (BITMAPFILEHEADER *)buffer;
    bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
96

Alexandre Julliard's avatar
Alexandre Julliard committed
97 98 99
      /* Check header content */
    if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
100
	HeapFree( GetProcessHeap(), 0, buffer );
Alexandre Julliard's avatar
Alexandre Julliard committed
101 102
	return 0;
    }
103
    hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
                                buffer + fileHeader->bfOffBits,
                                bitmapInfo, DIB_RGB_COLORS );
Alexandre Julliard's avatar
Alexandre Julliard committed
106
    HeapFree( GetProcessHeap(), 0, buffer );
Alexandre Julliard's avatar
Alexandre Julliard committed
107 108
    return hbitmap;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
109

Alexandre Julliard's avatar
Alexandre Julliard committed
110 111 112


/***********************************************************************
113
 *           DesktopWndProc
Alexandre Julliard's avatar
Alexandre Julliard committed
114
 */
115
LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
116
{
117 118
    if (message == WM_NCCREATE) return TRUE;
    return 0;  /* all other messages are ignored */
Alexandre Julliard's avatar
Alexandre Julliard committed
119 120
}

Alexandre Julliard's avatar
Alexandre Julliard committed
121
/***********************************************************************
122
 *           PaintDesktop   (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124
 *
 */
125
BOOL WINAPI PaintDesktop(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
126
{
127
    HWND hwnd = GetDesktopWindow();
Alexandre Julliard's avatar
Alexandre Julliard committed
128

129 130
    /* check for an owning thread; otherwise don't paint anything (non-desktop mode) */
    if (GetWindowThreadProcessId( hwnd, NULL ))
131 132 133 134 135 136 137
    {
        RECT rect;

        GetClientRect( hwnd, &rect );

        /* Paint desktop pattern (only if wall paper does not cover everything) */

138 139
        if (!hbitmapWallPaper ||
            (!fTileWallPaper && ((bitmapSize.cx < rect.right) || (bitmapSize.cy < rect.bottom))))
140
        {
141
            HBRUSH brush = hbrushPattern;
142
            if (!brush) brush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND );
143 144 145 146 147 148 149 150
            /* Set colors in case pattern is a monochrome bitmap */
            SetBkColor( hdc, RGB(0,0,0) );
            SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
            FillRect( hdc, &rect, brush );
        }

        /* Paint wall paper */

151
        if (hbitmapWallPaper)
152 153 154 155
        {
            INT x, y;
            HDC hMemDC = CreateCompatibleDC( hdc );

156
            SelectObject( hMemDC, hbitmapWallPaper );
157

158
            if (fTileWallPaper)
159
            {
160 161 162
                for (y = 0; y < rect.bottom; y += bitmapSize.cy)
                    for (x = 0; x < rect.right; x += bitmapSize.cx)
                        BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
163 164 165
            }
            else
            {
166 167
                x = (rect.left + rect.right - bitmapSize.cx) / 2;
                y = (rect.top + rect.bottom - bitmapSize.cy) / 2;
168 169
                if (x < 0) x = 0;
                if (y < 0) y = 0;
170
                BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
171 172 173 174 175
            }
            DeleteDC( hMemDC );
        }
    }
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
176
}
Alexandre Julliard's avatar
Alexandre Julliard committed
177

Alexandre Julliard's avatar
Alexandre Julliard committed
178
/***********************************************************************
179
 *           SetDeskWallPaper   (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
180 181 182
 *
 * FIXME: is there a unicode version?
 */
183
BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
Alexandre Julliard's avatar
Alexandre Julliard committed
184
{
185 186
    HBITMAP hbitmap;
    HDC hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
187 188 189 190
    char buffer[256];

    if (filename == (LPSTR)-1)
    {
191
	GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
Alexandre Julliard's avatar
Alexandre Julliard committed
192 193
	filename = buffer;
    }
194
    hdc = GetDC( 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
195
    hbitmap = DESKTOP_LoadBitmap( hdc, filename );
196
    ReleaseDC( 0, hdc );
197 198 199
    if (hbitmapWallPaper) DeleteObject( hbitmapWallPaper );
    hbitmapWallPaper = hbitmap;
    fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
200 201
    if (hbitmap)
    {
202 203
	BITMAP bmp;
	GetObjectA( hbitmap, sizeof(bmp), &bmp );
204 205
	bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
	bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
206
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209 210 211 212 213 214 215
    return TRUE;
}


/***********************************************************************
 *           DESKTOP_SetPattern
 *
 * Set the desktop pattern.
 */
216
BOOL DESKTOP_SetPattern( LPCWSTR pattern )
Alexandre Julliard's avatar
Alexandre Julliard committed
217 218 219
{
    int pat[8];

220
    if (hbrushPattern) DeleteObject( hbrushPattern );
221
    hbrushPattern = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
222
    memset( pat, 0, sizeof(pat) );
223
    if (pattern)
Alexandre Julliard's avatar
Alexandre Julliard committed
224
    {
225 226 227 228 229 230
        char buffer[64];
        WideCharToMultiByte( CP_ACP, 0, pattern, -1, buffer, sizeof(buffer), NULL, NULL );
        if (sscanf( buffer, " %d %d %d %d %d %d %d %d",
                    &pat[0], &pat[1], &pat[2], &pat[3],
                    &pat[4], &pat[5], &pat[6], &pat[7] ))
        {
231
            WORD ptrn[8];
232 233 234
            HBITMAP hbitmap;
            int i;

235 236
            for (i = 0; i < 8; i++) ptrn[i] = pat[i] & 0xffff;
            hbitmap = CreateBitmap( 8, 8, 1, 1, ptrn );
237 238 239
            hbrushPattern = CreatePatternBrush( hbitmap );
            DeleteObject( hbitmap );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
240 241 242
    }
    return TRUE;
}