Removed import of x11drv from ddraw.
[wine/wine-kai.git] / controls / desktop.c
blob3400ddebab68b74d0613e4fd28696c3719a4bfee
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 "user.h"
14 #include "win.h"
15 #include "controls.h"
16 #include "wine/winuser16.h"
18 typedef struct
20 HBRUSH hbrushPattern;
21 HBITMAP hbitmapWallPaper;
22 SIZE bitmapSize;
23 BOOL fTileWallPaper;
24 } DESKTOP;
26 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
29 /*********************************************************************
30 * desktop class descriptor
32 const struct builtin_class_descr DESKTOP_builtin_class =
34 DESKTOP_CLASS_ATOM, /* name */
35 CS_GLOBALCLASS, /* style */
36 NULL, /* procA (winproc is Unicode only) */
37 DesktopWndProc, /* procW */
38 sizeof(DESKTOP), /* extra */
39 IDC_ARROWA, /* cursor */
40 COLOR_BACKGROUND+1 /* brush */
44 /***********************************************************************
45 * DESKTOP_LoadBitmap
47 * Load a bitmap from a file. Used by SetDeskWallPaper().
49 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
51 BITMAPFILEHEADER *fileHeader;
52 BITMAPINFO *bitmapInfo;
53 HBITMAP hbitmap;
54 HFILE file;
55 LPSTR buffer;
56 LONG size;
58 /* Read all the file into memory */
60 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
62 UINT len = GetWindowsDirectoryA( NULL, 0 );
63 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
64 len + strlen(filename) + 2 )))
65 return 0;
66 GetWindowsDirectoryA( buffer, len + 1 );
67 strcat( buffer, "\\" );
68 strcat( buffer, filename );
69 file = _lopen( buffer, OF_READ );
70 HeapFree( GetProcessHeap(), 0, buffer );
72 if (file == HFILE_ERROR) return 0;
73 size = _llseek( file, 0, 2 );
74 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
76 _lclose( file );
77 return 0;
79 _llseek( file, 0, 0 );
80 size = _lread( file, buffer, size );
81 _lclose( file );
82 fileHeader = (BITMAPFILEHEADER *)buffer;
83 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
85 /* Check header content */
86 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
88 HeapFree( GetProcessHeap(), 0, buffer );
89 return 0;
91 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
92 buffer + fileHeader->bfOffBits,
93 bitmapInfo, DIB_RGB_COLORS );
94 HeapFree( GetProcessHeap(), 0, buffer );
95 return hbitmap;
100 /***********************************************************************
101 * DesktopWndProc
103 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
105 LRESULT retvalue = 0;
106 WND *wndPtr = WIN_FindWndPtr( hwnd );
107 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
109 if (message == WM_NCCREATE)
111 desktopPtr->hbrushPattern = 0;
112 desktopPtr->hbitmapWallPaper = 0;
113 SetDeskPattern();
114 SetDeskWallPaper( (LPSTR)-1 );
115 retvalue = 1;
117 /* all other messages are ignored */
119 WIN_ReleaseWndPtr(wndPtr);
120 return retvalue;
123 /***********************************************************************
124 * PaintDesktop (USER32.@)
127 BOOL WINAPI PaintDesktop(HDC hdc)
129 HWND hwnd = GetDesktopWindow();
130 WND *wndPtr = WIN_FindWndPtr( hwnd );
131 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
133 /* check for a queue; otherwise don't paint anything (non-desktop mode) */
134 if (wndPtr->hmemTaskQ)
136 RECT rect;
138 GetClientRect( hwnd, &rect );
140 /* Paint desktop pattern (only if wall paper does not cover everything) */
142 if (!desktopPtr->hbitmapWallPaper ||
143 (!desktopPtr->fTileWallPaper && ((desktopPtr->bitmapSize.cx < rect.right) ||
144 (desktopPtr->bitmapSize.cy < rect.bottom))))
146 HBRUSH brush = desktopPtr->hbrushPattern;
147 if (!brush) brush = GetClassLongA( hwnd, GCL_HBRBACKGROUND );
148 /* Set colors in case pattern is a monochrome bitmap */
149 SetBkColor( hdc, RGB(0,0,0) );
150 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
151 FillRect( hdc, &rect, brush );
154 /* Paint wall paper */
156 if (desktopPtr->hbitmapWallPaper)
158 INT x, y;
159 HDC hMemDC = CreateCompatibleDC( hdc );
161 SelectObject( hMemDC, desktopPtr->hbitmapWallPaper );
163 if (desktopPtr->fTileWallPaper)
165 for (y = 0; y < rect.bottom; y += desktopPtr->bitmapSize.cy)
166 for (x = 0; x < rect.right; x += desktopPtr->bitmapSize.cx)
167 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
168 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
170 else
172 x = (rect.left + rect.right - desktopPtr->bitmapSize.cx) / 2;
173 y = (rect.top + rect.bottom - desktopPtr->bitmapSize.cy) / 2;
174 if (x < 0) x = 0;
175 if (y < 0) y = 0;
176 BitBlt( hdc, x, y, desktopPtr->bitmapSize.cx,
177 desktopPtr->bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
179 DeleteDC( hMemDC );
182 WIN_ReleaseWndPtr(wndPtr);
183 return TRUE;
186 /***********************************************************************
187 * OldSetDeskPattern (USER.279)
189 BOOL16 WINAPI SetDeskPattern(void)
191 char buffer[100];
192 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
193 return DESKTOP_SetPattern( buffer );
197 /***********************************************************************
198 * SetDeskWallPaper (USER.285)
200 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
202 return SetDeskWallPaper( filename );
206 /***********************************************************************
207 * SetDeskWallPaper (USER32.@)
209 * FIXME: is there a unicode version?
211 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
213 HBITMAP hbitmap;
214 HDC hdc;
215 char buffer[256];
216 WND *wndPtr = WIN_GetDesktop();
217 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
219 if (filename == (LPSTR)-1)
221 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
222 filename = buffer;
224 hdc = GetDC( 0 );
225 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
226 ReleaseDC( 0, hdc );
227 if (desktopPtr->hbitmapWallPaper) DeleteObject( desktopPtr->hbitmapWallPaper );
228 desktopPtr->hbitmapWallPaper = hbitmap;
229 desktopPtr->fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
230 if (hbitmap)
232 BITMAP bmp;
233 GetObjectA( hbitmap, sizeof(bmp), &bmp );
234 desktopPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
235 desktopPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
237 WIN_ReleaseDesktop();
238 return TRUE;
242 /***********************************************************************
243 * DESKTOP_SetPattern
245 * Set the desktop pattern.
247 BOOL DESKTOP_SetPattern( LPCSTR pattern )
249 WND *wndPtr = WIN_GetDesktop();
250 DESKTOP *desktopPtr = (DESKTOP *)wndPtr->wExtra;
251 int pat[8];
253 if (desktopPtr->hbrushPattern) DeleteObject( desktopPtr->hbrushPattern );
254 memset( pat, 0, sizeof(pat) );
255 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
256 &pat[0], &pat[1], &pat[2], &pat[3],
257 &pat[4], &pat[5], &pat[6], &pat[7] ))
259 WORD pattern[8];
260 HBITMAP hbitmap;
261 int i;
263 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
264 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
265 desktopPtr->hbrushPattern = CreatePatternBrush( hbitmap );
266 DeleteObject( hbitmap );
268 else desktopPtr->hbrushPattern = 0;
269 WIN_ReleaseDesktop();
270 return TRUE;