Fixed some issues found by winapi_check.
[wine/hacks.git] / controls / desktop.c
blob01e1706cacfe3d7d32a4cc72a14ea9c59dbc1cfe
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "windef.h"
12 #include "wingdi.h"
13 #include "heap.h"
14 #include "user.h"
15 #include "win.h"
16 #include "controls.h"
17 #include "wine/winuser16.h"
19 typedef struct
21 HBRUSH hbrushPattern;
22 HBITMAP hbitmapWallPaper;
23 SIZE bitmapSize;
24 BOOL fTileWallPaper;
25 } DESKTOP;
27 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
30 /*********************************************************************
31 * desktop class descriptor
33 const struct builtin_class_descr DESKTOP_builtin_class =
35 DESKTOP_CLASS_ATOM, /* name */
36 CS_GLOBALCLASS, /* style */
37 NULL, /* procA (winproc is Unicode only) */
38 DesktopWndProc, /* procW */
39 sizeof(DESKTOP), /* extra */
40 IDC_ARROWA, /* cursor */
41 COLOR_BACKGROUND+1 /* brush */
45 /***********************************************************************
46 * DESKTOP_LoadBitmap
48 * Load a bitmap from a file. Used by SetDeskWallPaper().
50 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
52 BITMAPFILEHEADER *fileHeader;
53 BITMAPINFO *bitmapInfo;
54 HBITMAP hbitmap;
55 HFILE file;
56 LPSTR buffer;
57 LONG size;
59 /* Read all the file into memory */
61 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
63 UINT len = GetWindowsDirectoryA( NULL, 0 );
64 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
65 len + strlen(filename) + 2 )))
66 return 0;
67 GetWindowsDirectoryA( buffer, len + 1 );
68 strcat( buffer, "\\" );
69 strcat( buffer, filename );
70 file = _lopen( buffer, OF_READ );
71 HeapFree( GetProcessHeap(), 0, buffer );
73 if (file == HFILE_ERROR) return 0;
74 size = _llseek( file, 0, 2 );
75 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
77 _lclose( file );
78 return 0;
80 _llseek( file, 0, 0 );
81 size = _lread( file, buffer, size );
82 _lclose( file );
83 fileHeader = (BITMAPFILEHEADER *)buffer;
84 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
86 /* Check header content */
87 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
89 HeapFree( GetProcessHeap(), 0, buffer );
90 return 0;
92 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
93 buffer + fileHeader->bfOffBits,
94 bitmapInfo, DIB_RGB_COLORS );
95 HeapFree( GetProcessHeap(), 0, buffer );
96 return hbitmap;
101 /***********************************************************************
102 * DesktopWndProc
104 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
106 LRESULT retvalue = 0;
107 WND *wndPtr = WIN_FindWndPtr( hwnd );
108 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
110 if (message == WM_NCCREATE)
112 desktopPtr->hbrushPattern = 0;
113 desktopPtr->hbitmapWallPaper = 0;
114 SetDeskPattern();
115 SetDeskWallPaper( (LPSTR)-1 );
116 retvalue = 1;
118 /* all other messages are ignored */
120 WIN_ReleaseWndPtr(wndPtr);
121 return retvalue;
124 /***********************************************************************
125 * PaintDesktop (USER32.@)
128 BOOL WINAPI PaintDesktop(HDC hdc)
130 HWND hwnd = GetDesktopWindow();
131 WND *wndPtr = WIN_FindWndPtr( hwnd );
132 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
134 /* check for a queue; otherwise don't paint anything (non-desktop mode) */
135 if (wndPtr->hmemTaskQ)
137 RECT rect;
139 GetClientRect( hwnd, &rect );
141 /* Paint desktop pattern (only if wall paper does not cover everything) */
143 if (!desktopPtr->hbitmapWallPaper ||
144 (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
145 (desktopPtr->bitmapSize.cy < rect.bottom))))
147 HBRUSH brush = desktopPtr->hbrushPattern;
148 if (!brush) brush = GetClassLongA( hwnd, GCL_HBRBACKGROUND );
149 /* Set colors in case pattern is a monochrome bitmap */
150 SetBkColor( hdc, RGB(0,0,0) );
151 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
152 FillRect( hdc, &rect, brush );
155 /* Paint wall paper */
157 if (desktopPtr->hbitmapWallPaper)
159 INT x, y;
160 HDC hMemDC = CreateCompatibleDC( hdc );
162 SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
164 if (desktopPtr->fTileWallPaper)
166 for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
167 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
168 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
169 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
171 else
173 x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
174 y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
175 if (x < 0) x = 0;
176 if (y < 0) y = 0;
177 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
178 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
180 DeleteDC( hMemDC );
183 WIN_ReleaseWndPtr(wndPtr);
184 return TRUE;
187 /***********************************************************************
188 * OldSetDeskPattern (USER.279)
190 BOOL16 WINAPI SetDeskPattern(void)
192 char buffer[100];
193 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
194 return DESKTOP_SetPattern( buffer );
198 /***********************************************************************
199 * SetDeskWallPaper (USER.285)
201 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
203 return SetDeskWallPaper( filename );
207 /***********************************************************************
208 * SetDeskWallPaper (USER32.@)
210 * FIXME: is there a unicode version?
212 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
214 HBITMAP hbitmap;
215 HDC hdc;
216 char buffer[256];
217 WND *wndPtr = WIN_GetDesktop();
218 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
220 if (filename == (LPSTR)-1)
222 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
223 filename = buffer;
225 hdc = GetDC( 0 );
226 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
227 ReleaseDC( 0, hdc );
228 if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
229 desktopPtr->hbitmapWallPaper = hbitmap;
230 desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
231 if (hbitmap)
233 BITMAP bmp;
234 GetObjectA( hbitmap, sizeof(bmp), &bmp );
235 desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
236 desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
238 WIN_ReleaseDesktop();
239 return TRUE;
243 /***********************************************************************
244 * DESKTOP_SetPattern
246 * Set the desktop pattern.
248 BOOL DESKTOP_SetPattern( LPCSTR pattern )
250 WND *wndPtr = WIN_GetDesktop();
251 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
252 int pat[8];
254 if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
255 memset( pat, 0, sizeof(pat) );
256 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
257 &pat[0], &pat[1], &pat[2], &pat[3],
258 &pat[4], &pat[5], &pat[6], &pat[7] ))
260 WORD pattern[8];
261 HBITMAP hbitmap;
262 int i;
264 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
265 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
266 desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
267 DeleteObject( hbitmap );
269 else desktopPtr->hbrushPattern = 0;
270 WIN_ReleaseDesktop();
271 return TRUE;