Release 940912
[wine/multimedia.git] / controls / desktop.c
blob4045177740310f299b26f93ee4e8fede81cb7504
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 "win.h"
14 #include "desktop.h"
15 #include "prototypes.h"
17 extern BOOL GRAPH_DrawBitmap( HDC hdc, HBITMAP hbitmap, int xdest, int ydest,
18 int xsrc, int ysrc, int width, int height,
19 int rop ); /* graphics.c */
21 /***********************************************************************
22 * DESKTOP_LoadBitmap
24 * Load a bitmap from a file. Used by SetDeskWallPaper().
26 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, char *filename )
28 BITMAPFILEHEADER *fileHeader;
29 BITMAPINFO *bitmapInfo;
30 HBITMAP hbitmap;
31 char *unixFileName, *buffer;
32 int file;
33 long size;
35 /* Read all the file into memory */
37 if (!(unixFileName = GetUnixFileName( filename ))) return 0;
38 if ((file = open( unixFileName, O_RDONLY )) == -1) return 0;
39 size = lseek( file, 0, SEEK_END );
40 if (!(buffer = (char *)malloc( size )))
42 close( file );
43 return 0;
45 lseek( file, 0, SEEK_SET );
46 size = read( file, buffer, size );
47 close( file );
48 fileHeader = (BITMAPFILEHEADER *)buffer;
49 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
51 /* Check header content */
52 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
54 free( buffer );
55 return 0;
57 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
58 buffer + fileHeader->bfOffBits,
59 bitmapInfo, DIB_RGB_COLORS );
60 free( buffer );
61 return hbitmap;
65 /***********************************************************************
66 * DESKTOP_DoEraseBkgnd
68 * Handle the WM_ERASEBKGND message.
70 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
72 RECT rect;
73 GetClientRect( hwnd, &rect );
75 /* Paint desktop pattern (only if wall paper does not cover everything) */
77 if (!infoPtr->hbitmapWallPaper ||
78 (!infoPtr->fTileWallPaper && (infoPtr->bitmapSize.cx < rect.right) &&
79 (infoPtr->bitmapSize.cy < rect.bottom)))
81 /* Set colors in case pattern is a monochrome bitmap */
82 SetBkColor( hdc, RGB(0,0,0) );
83 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
84 FillRect( hdc, &rect, infoPtr->hbrushPattern );
87 /* Paint wall paper */
89 if (infoPtr->hbitmapWallPaper)
91 int x, y;
93 if (infoPtr->fTileWallPaper)
95 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
96 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
97 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
98 x, y, 0, 0,
99 infoPtr->bitmapSize.cx,
100 infoPtr->bitmapSize.cy, SRCCOPY );
102 else
104 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
105 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
106 if (x < 0) x = 0;
107 if (y < 0) y = 0;
108 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
109 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy,
110 SRCCOPY );
114 return 1;
118 /***********************************************************************
119 * DesktopWndProc
121 * Window procedure for the desktop window.
123 LONG DesktopWndProc ( HWND hwnd, WORD message, WORD wParam, LONG lParam )
125 WND *wndPtr = WIN_FindWndPtr( hwnd );
126 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
128 /* Most messages are ignored (we DON'T call DefWindowProc) */
130 switch(message)
132 /* Warning: this message is sent directly by */
133 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
134 case WM_NCCREATE:
135 infoPtr->hbrushPattern = 0;
136 infoPtr->hbitmapWallPaper = 0;
137 SetDeskPattern();
138 SetDeskWallPaper( (LPSTR)-1 );
139 break;
141 case WM_ERASEBKGND:
142 if (rootWindow == DefaultRootWindow(display)) return 1;
143 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
146 return 0;
150 /***********************************************************************
151 * SetDeskPattern (USER.279)
153 BOOL SetDeskPattern()
155 char buffer[100];
156 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
157 return DESKTOP_SetPattern( buffer );
161 /***********************************************************************
162 * SetDeskWallPaper (USER.285)
164 BOOL SetDeskWallPaper( LPSTR filename )
166 HBITMAP hbitmap;
167 HDC hdc;
168 char buffer[256];
169 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
170 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
172 if (filename == (LPSTR)-1)
174 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
175 filename = buffer;
177 hdc = GetDC( 0 );
178 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
179 ReleaseDC( 0, hdc );
180 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
181 infoPtr->hbitmapWallPaper = hbitmap;
182 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
183 if (hbitmap)
185 BITMAP bmp;
186 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
187 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
188 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
190 return TRUE;
194 /***********************************************************************
195 * DESKTOP_SetPattern
197 * Set the desktop pattern.
199 BOOL DESKTOP_SetPattern(char *pattern )
201 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
202 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
203 int pat[8];
205 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
206 memset( pat, 0, sizeof(pat) );
207 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
208 &pat[0], &pat[1], &pat[2], &pat[3],
209 &pat[4], &pat[5], &pat[6], &pat[7] ))
211 WORD pattern[8];
212 HBITMAP hbitmap;
213 int i;
215 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
216 hbitmap = CreateBitmap( 8, 8, 1, 1, pattern );
217 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
218 DeleteObject( hbitmap );
220 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
221 return TRUE;