Release 940804
[wine/gsoc-2012-control.git] / controls / desktop.c
blobe1f5d7f16c2db8121d0486fc50a8db027cb6010c
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1994";
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "win.h"
13 #include "desktop.h"
14 #include "prototypes.h"
17 /***********************************************************************
18 * DESKTOP_LoadBitmap
20 * Load a bitmap from a file. Used by SetDeskWallPaper().
22 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, char *filename )
24 BITMAPFILEHEADER *fileHeader;
25 BITMAPINFO *bitmapInfo;
26 HBITMAP hbitmap;
27 char *unixFileName, *buffer;
28 int file;
29 long size;
31 /* Read all the file into memory */
33 if (!(unixFileName = GetUnixFileName( filename ))) return 0;
34 if ((file = open( unixFileName, O_RDONLY )) == -1) return 0;
35 size = lseek( file, 0, SEEK_END );
36 if (!(buffer = (char *)malloc( size )))
38 close( file );
39 return 0;
41 lseek( file, 0, SEEK_SET );
42 size = read( file, buffer, size );
43 close( file );
44 fileHeader = (BITMAPFILEHEADER *)buffer;
45 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
47 /* Check header content */
48 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
50 free( buffer );
51 return 0;
53 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
54 buffer + fileHeader->bfOffBits,
55 bitmapInfo, DIB_RGB_COLORS );
56 free( buffer );
57 return hbitmap;
61 /***********************************************************************
62 * DESKTOP_DoEraseBkgnd
64 * Handle the WM_ERASEBKGND message.
66 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
68 RECT rect;
69 GetClientRect( hwnd, &rect );
71 /* Paint desktop pattern (only if wall paper does not cover everything) */
73 if (!infoPtr->hbitmapWallPaper ||
74 (!infoPtr->fTileWallPaper && (infoPtr->bitmapSize.cx < rect.right) &&
75 (infoPtr->bitmapSize.cy < rect.bottom)))
77 /* Set colors in case pattern is a monochrome bitmap */
78 SetBkColor( hdc, RGB(0,0,0) );
79 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
80 FillRect( hdc, &rect, infoPtr->hbrushPattern );
83 /* Paint wall paper */
85 if (infoPtr->hbitmapWallPaper)
87 int x, y;
88 HDC hdcmem;
90 hdcmem = CreateCompatibleDC( hdc );
91 SelectObject( hdcmem, infoPtr->hbitmapWallPaper );
92 if (infoPtr->fTileWallPaper)
94 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
95 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
96 BitBlt( hdc, x, y,
97 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy,
98 hdcmem, 0, 0, SRCCOPY );
100 else
102 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
103 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
104 if (x < 0) x = 0;
105 if (y < 0) y = 0;
106 BitBlt( hdc, x, y, infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy,
107 hdcmem, 0, 0, SRCCOPY );
109 DeleteDC( hdcmem );
112 return 1;
116 /***********************************************************************
117 * DesktopWndProc
119 * Window procedure for the desktop window.
121 LONG DesktopWndProc ( HWND hwnd, WORD message, WORD wParam, LONG lParam )
123 WND *wndPtr = WIN_FindWndPtr( hwnd );
124 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
126 /* Most messages are ignored (we DON'T call DefWindowProc) */
128 switch(message)
130 /* Warning: this message is sent directly by */
131 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
132 case WM_NCCREATE:
133 infoPtr->hbrushPattern = 0;
134 infoPtr->hbitmapWallPaper = 0;
135 SetDeskPattern();
136 SetDeskWallPaper( (LPSTR)-1 );
137 break;
139 case WM_ERASEBKGND:
140 if (rootWindow == DefaultRootWindow(display)) return 1;
141 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
144 return 0;
148 /***********************************************************************
149 * SetDeskPattern (USER.279)
151 BOOL SetDeskPattern()
153 char buffer[100];
154 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
155 return DESKTOP_SetPattern( buffer );
159 /***********************************************************************
160 * SetDeskWallPaper (USER.285)
162 BOOL SetDeskWallPaper( LPSTR filename )
164 HBITMAP hbitmap;
165 HDC hdc;
166 char buffer[256];
167 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
168 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
170 if (filename == (LPSTR)-1)
172 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
173 filename = buffer;
175 hdc = GetDC( 0 );
176 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
177 ReleaseDC( 0, hdc );
178 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
179 infoPtr->hbitmapWallPaper = hbitmap;
180 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
181 if (hbitmap)
183 BITMAP bmp;
184 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
185 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
186 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
188 return TRUE;
192 /***********************************************************************
193 * DESKTOP_SetPattern
195 * Set the desktop pattern.
197 BOOL DESKTOP_SetPattern(char *pattern )
199 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
200 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
201 int pat[8];
203 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
204 memset( pat, 0, sizeof(pat) );
205 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
206 &pat[0], &pat[1], &pat[2], &pat[3],
207 &pat[4], &pat[5], &pat[6], &pat[7] ))
209 WORD pattern[8];
210 HBITMAP hbitmap;
211 int i;
213 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
214 hbitmap = CreateBitmap( 8, 8, 1, 1, pattern );
215 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
216 DeleteObject( hbitmap );
218 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
219 return TRUE;