Release 941017
[wine/gsoc-2012-control.git] / controls / desktop.c
blob920ac2328220d6e3da375285529de9bce2b08c00
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 <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include "win.h"
15 #include "desktop.h"
16 #include "prototypes.h"
18 extern BOOL GRAPH_DrawBitmap( HDC hdc, HBITMAP hbitmap, int xdest, int ydest,
19 int xsrc, int ysrc, int width, int height,
20 int rop ); /* graphics.c */
22 /***********************************************************************
23 * DESKTOP_LoadBitmap
25 * Load a bitmap from a file. Used by SetDeskWallPaper().
27 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, char *filename )
29 BITMAPFILEHEADER *fileHeader;
30 BITMAPINFO *bitmapInfo;
31 HBITMAP hbitmap;
32 char *unixFileName, *buffer;
33 int file;
34 long size;
36 /* Read all the file into memory */
38 if (!(unixFileName = GetUnixFileName( filename ))) return 0;
39 if ((file = open( unixFileName, O_RDONLY )) == -1) return 0;
40 size = lseek( file, 0, SEEK_END );
41 if (!(buffer = (char *)malloc( size )))
43 close( file );
44 return 0;
46 lseek( file, 0, SEEK_SET );
47 size = read( file, buffer, size );
48 close( file );
49 fileHeader = (BITMAPFILEHEADER *)buffer;
50 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
52 /* Check header content */
53 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
55 free( buffer );
56 return 0;
58 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
59 buffer + fileHeader->bfOffBits,
60 bitmapInfo, DIB_RGB_COLORS );
61 free( buffer );
62 return hbitmap;
66 /***********************************************************************
67 * DESKTOP_DoEraseBkgnd
69 * Handle the WM_ERASEBKGND message.
71 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
73 RECT rect;
74 GetClientRect( hwnd, &rect );
76 /* Paint desktop pattern (only if wall paper does not cover everything) */
78 if (!infoPtr->hbitmapWallPaper ||
79 (!infoPtr->fTileWallPaper && (infoPtr->bitmapSize.cx < rect.right) &&
80 (infoPtr->bitmapSize.cy < rect.bottom)))
82 /* Set colors in case pattern is a monochrome bitmap */
83 SetBkColor( hdc, RGB(0,0,0) );
84 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
85 FillRect( hdc, &rect, infoPtr->hbrushPattern );
88 /* Paint wall paper */
90 if (infoPtr->hbitmapWallPaper)
92 int x, y;
94 if (infoPtr->fTileWallPaper)
96 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
97 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
98 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
99 x, y, 0, 0,
100 infoPtr->bitmapSize.cx,
101 infoPtr->bitmapSize.cy, SRCCOPY );
103 else
105 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
106 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
107 if (x < 0) x = 0;
108 if (y < 0) y = 0;
109 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
110 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy,
111 SRCCOPY );
115 return 1;
119 /***********************************************************************
120 * DesktopWndProc
122 * Window procedure for the desktop window.
124 LONG DesktopWndProc ( HWND hwnd, WORD message, WORD wParam, LONG lParam )
126 WND *wndPtr = WIN_FindWndPtr( hwnd );
127 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
129 /* Most messages are ignored (we DON'T call DefWindowProc) */
131 switch(message)
133 /* Warning: this message is sent directly by */
134 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
135 case WM_NCCREATE:
136 infoPtr->hbrushPattern = 0;
137 infoPtr->hbitmapWallPaper = 0;
138 SetDeskPattern();
139 SetDeskWallPaper( (LPSTR)-1 );
140 break;
142 case WM_ERASEBKGND:
143 if (rootWindow == DefaultRootWindow(display)) return 1;
144 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
147 return 0;
151 /***********************************************************************
152 * SetDeskPattern (USER.279)
154 BOOL SetDeskPattern()
156 char buffer[100];
157 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
158 return DESKTOP_SetPattern( buffer );
162 /***********************************************************************
163 * SetDeskWallPaper (USER.285)
165 BOOL SetDeskWallPaper( LPSTR filename )
167 HBITMAP hbitmap;
168 HDC hdc;
169 char buffer[256];
170 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
171 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
173 if (filename == (LPSTR)-1)
175 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
176 filename = buffer;
178 hdc = GetDC( 0 );
179 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
180 ReleaseDC( 0, hdc );
181 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
182 infoPtr->hbitmapWallPaper = hbitmap;
183 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
184 if (hbitmap)
186 BITMAP bmp;
187 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
188 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
189 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
191 return TRUE;
195 /***********************************************************************
196 * DESKTOP_SetPattern
198 * Set the desktop pattern.
200 BOOL DESKTOP_SetPattern(char *pattern )
202 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
203 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
204 int pat[8];
206 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
207 memset( pat, 0, sizeof(pat) );
208 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
209 &pat[0], &pat[1], &pat[2], &pat[3],
210 &pat[4], &pat[5], &pat[6], &pat[7] ))
212 WORD pattern[8];
213 HBITMAP hbitmap;
214 int i;
216 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
217 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
218 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
219 DeleteObject( hbitmap );
221 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
222 return TRUE;