Release 961222
[wine/gsoc-2012-control.git] / controls / desktop.c
blob8fcde45a499952869f0d6dd1f8db843d1da384b5
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include "win.h"
12 #include "desktop.h"
13 #include "directory.h"
14 #include "graphics.h"
15 #include "heap.h"
18 /***********************************************************************
19 * DESKTOP_LoadBitmap
21 * Load a bitmap from a file. Used by SetDeskWallPaper().
23 static HBITMAP32 DESKTOP_LoadBitmap( HDC32 hdc, const char *filename )
25 BITMAPFILEHEADER *fileHeader;
26 BITMAPINFO *bitmapInfo;
27 HBITMAP32 hbitmap;
28 HFILE file;
29 LPSTR buffer;
30 LONG size;
32 /* Read all the file into memory */
34 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
36 UINT32 len = GetWindowsDirectory32A( NULL, 0 );
37 if (!(buffer = HeapAlloc( SystemHeap, 0, len + strlen(filename) + 2 )))
38 return 0;
39 GetWindowsDirectory32A( buffer, len + 1 );
40 strcat( buffer, "\\" );
41 strcat( buffer, filename );
42 file = _lopen( buffer, OF_READ );
43 HeapFree( SystemHeap, 0, buffer );
45 if (file == HFILE_ERROR) return 0;
46 size = _llseek( file, 0, 2 );
47 if (!(buffer = HeapAlloc( SystemHeap, 0, size )))
49 _lclose( file );
50 return 0;
52 _llseek( file, 0, 0 );
53 size = _lread32( file, buffer, size );
54 _lclose( file );
55 fileHeader = (BITMAPFILEHEADER *)buffer;
56 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
58 /* Check header content */
59 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
61 HeapFree( SystemHeap, 0, buffer );
62 return 0;
64 hbitmap = CreateDIBitmap32( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
65 buffer + fileHeader->bfOffBits,
66 bitmapInfo, DIB_RGB_COLORS );
67 HeapFree( SystemHeap, 0, buffer );
68 return hbitmap;
72 /***********************************************************************
73 * DESKTOP_DoEraseBkgnd
75 * Handle the WM_ERASEBKGND message.
77 static LRESULT DESKTOP_DoEraseBkgnd( HWND32 hwnd, HDC32 hdc,
78 DESKTOPINFO *infoPtr )
80 RECT32 rect;
81 WND* Wnd = WIN_FindWndPtr( hwnd );
83 if (Wnd->hrgnUpdate > 1) DeleteObject32( Wnd->hrgnUpdate );
84 Wnd->hrgnUpdate = 0;
86 GetClientRect32( hwnd, &rect );
88 /* Paint desktop pattern (only if wall paper does not cover everything) */
90 if (!infoPtr->hbitmapWallPaper ||
91 (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
92 (infoPtr->bitmapSize.cy < rect.bottom))))
94 /* Set colors in case pattern is a monochrome bitmap */
95 SetBkColor( hdc, RGB(0,0,0) );
96 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
97 FillRect32( hdc, &rect, infoPtr->hbrushPattern );
100 /* Paint wall paper */
102 if (infoPtr->hbitmapWallPaper)
104 INT32 x, y;
106 if (infoPtr->fTileWallPaper)
108 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
109 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
110 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
111 x, y, 0, 0,
112 infoPtr->bitmapSize.cx,
113 infoPtr->bitmapSize.cy );
115 else
117 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
118 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
119 if (x < 0) x = 0;
120 if (y < 0) y = 0;
121 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
122 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy );
126 return 1;
130 /***********************************************************************
131 * DesktopWndProc
133 * Window procedure for the desktop window.
135 LRESULT DesktopWndProc( HWND32 hwnd, UINT32 message,
136 WPARAM32 wParam, LPARAM lParam )
138 WND *wndPtr = WIN_FindWndPtr( hwnd );
139 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
141 /* Most messages are ignored (we DON'T call DefWindowProc) */
143 switch(message)
145 /* Warning: this message is sent directly by */
146 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
147 case WM_NCCREATE:
148 infoPtr->hbrushPattern = 0;
149 infoPtr->hbitmapWallPaper = 0;
150 SetDeskPattern();
151 SetDeskWallPaper32( (LPSTR)-1 );
152 break;
154 case WM_ERASEBKGND:
155 if (rootWindow == DefaultRootWindow(display)) return 1;
156 return DESKTOP_DoEraseBkgnd( hwnd, (HDC32)wParam, infoPtr );
158 case WM_SYSCOMMAND:
159 if ((wParam & 0xfff0) != SC_CLOSE) return 0;
160 ExitWindows( 0, 0 );
162 case WM_SETCURSOR:
163 return (LRESULT)SetCursor( LoadCursor16( 0, IDC_ARROW ) );
166 return 0;
170 /***********************************************************************
171 * SetDeskPattern (USER.279)
173 BOOL16 SetDeskPattern(void)
175 char buffer[100];
176 GetProfileString32A( "desktop", "Pattern", "(None)", buffer, 100 );
177 return DESKTOP_SetPattern( buffer );
181 /***********************************************************************
182 * SetDeskWallPaper16 (USER.285)
184 BOOL16 SetDeskWallPaper16( LPCSTR filename )
186 return SetDeskWallPaper32( filename );
190 /***********************************************************************
191 * SetDeskWallPaper32 (USER32.475)
193 * FIXME: is there a unicode version?
195 BOOL32 SetDeskWallPaper32( LPCSTR filename )
197 HBITMAP32 hbitmap;
198 HDC32 hdc;
199 char buffer[256];
200 WND *wndPtr = WIN_GetDesktop();
201 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
203 if (filename == (LPSTR)-1)
205 GetProfileString32A( "desktop", "WallPaper", "(None)", buffer, 256 );
206 filename = buffer;
208 hdc = GetDC32( 0 );
209 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
210 ReleaseDC32( 0, hdc );
211 if (infoPtr->hbitmapWallPaper) DeleteObject32( infoPtr->hbitmapWallPaper );
212 infoPtr->hbitmapWallPaper = hbitmap;
213 infoPtr->fTileWallPaper = GetProfileInt32A( "desktop", "TileWallPaper", 0 );
214 if (hbitmap)
216 BITMAP32 bmp;
217 GetObject32A( hbitmap, sizeof(bmp), &bmp );
218 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
219 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
221 return TRUE;
225 /***********************************************************************
226 * DESKTOP_SetPattern
228 * Set the desktop pattern.
230 BOOL32 DESKTOP_SetPattern( LPCSTR pattern )
232 WND *wndPtr = WIN_GetDesktop();
233 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
234 int pat[8];
236 if (infoPtr->hbrushPattern) DeleteObject32( infoPtr->hbrushPattern );
237 memset( pat, 0, sizeof(pat) );
238 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
239 &pat[0], &pat[1], &pat[2], &pat[3],
240 &pat[4], &pat[5], &pat[6], &pat[7] ))
242 WORD pattern[8];
243 HBITMAP32 hbitmap;
244 int i;
246 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
247 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
248 infoPtr->hbrushPattern = CreatePatternBrush32( hbitmap );
249 DeleteObject32( hbitmap );
251 else infoPtr->hbrushPattern = CreateSolidBrush32( GetSysColor(COLOR_BACKGROUND) );
252 return TRUE;