Release 950109
[wine/multimedia.git] / controls / desktop.c
blob7a7d54545be7256f1b475cf73644c7580ae8b248
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1994";
7 */
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 "dos_fs.h"
17 #include "graphics.h"
19 /***********************************************************************
20 * DESKTOP_LoadBitmap
22 * Load a bitmap from a file. Used by SetDeskWallPaper().
24 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, char *filename )
26 BITMAPFILEHEADER *fileHeader;
27 BITMAPINFO *bitmapInfo;
28 HBITMAP hbitmap;
29 char *unixFileName, *buffer;
30 int file;
31 long size;
33 /* Read all the file into memory */
35 if (!(unixFileName = DOS_GetUnixFileName( filename ))) return 0;
36 if ((file = open( unixFileName, O_RDONLY )) == -1) return 0;
37 size = lseek( file, 0, SEEK_END );
38 if (!(buffer = (char *)malloc( size )))
40 close( file );
41 return 0;
43 lseek( file, 0, SEEK_SET );
44 size = read( file, buffer, size );
45 close( file );
46 fileHeader = (BITMAPFILEHEADER *)buffer;
47 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
49 /* Check header content */
50 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
52 free( buffer );
53 return 0;
55 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
56 buffer + fileHeader->bfOffBits,
57 bitmapInfo, DIB_RGB_COLORS );
58 free( buffer );
59 return hbitmap;
63 /***********************************************************************
64 * DESKTOP_DoEraseBkgnd
66 * Handle the WM_ERASEBKGND message.
68 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
70 RECT rect;
71 GetClientRect( hwnd, &rect );
73 /* Paint desktop pattern (only if wall paper does not cover everything) */
75 if (!infoPtr->hbitmapWallPaper ||
76 (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
77 (infoPtr->bitmapSize.cy < rect.bottom))))
79 /* Set colors in case pattern is a monochrome bitmap */
80 SetBkColor( hdc, RGB(0,0,0) );
81 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
82 FillRect( hdc, &rect, infoPtr->hbrushPattern );
85 /* Paint wall paper */
87 if (infoPtr->hbitmapWallPaper)
89 int x, y;
91 if (infoPtr->fTileWallPaper)
93 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
94 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
95 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
96 x, y, 0, 0,
97 infoPtr->bitmapSize.cx,
98 infoPtr->bitmapSize.cy );
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 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
107 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy );
111 return 1;
115 /***********************************************************************
116 * DesktopWndProc
118 * Window procedure for the desktop window.
120 LONG DesktopWndProc ( HWND hwnd, WORD message, WORD wParam, LONG lParam )
122 WND *wndPtr = WIN_FindWndPtr( hwnd );
123 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
125 /* Most messages are ignored (we DON'T call DefWindowProc) */
127 switch(message)
129 /* Warning: this message is sent directly by */
130 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
131 case WM_NCCREATE:
132 infoPtr->hbrushPattern = 0;
133 infoPtr->hbitmapWallPaper = 0;
134 SetDeskPattern();
135 SetDeskWallPaper( (LPSTR)-1 );
136 break;
138 case WM_ERASEBKGND:
139 if (rootWindow == DefaultRootWindow(display)) return 1;
140 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
143 return 0;
147 /***********************************************************************
148 * SetDeskPattern (USER.279)
150 BOOL SetDeskPattern(void)
152 char buffer[100];
153 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
154 return DESKTOP_SetPattern( buffer );
158 /***********************************************************************
159 * SetDeskWallPaper (USER.285)
161 BOOL SetDeskWallPaper( LPSTR filename )
163 HBITMAP hbitmap;
164 HDC hdc;
165 char buffer[256];
166 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
167 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
169 if (filename == (LPSTR)-1)
171 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
172 filename = buffer;
174 hdc = GetDC( 0 );
175 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
176 ReleaseDC( 0, hdc );
177 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
178 infoPtr->hbitmapWallPaper = hbitmap;
179 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
180 if (hbitmap)
182 BITMAP bmp;
183 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
184 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
185 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
187 return TRUE;
191 /***********************************************************************
192 * DESKTOP_SetPattern
194 * Set the desktop pattern.
196 BOOL DESKTOP_SetPattern(char *pattern )
198 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
199 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
200 int pat[8];
202 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
203 memset( pat, 0, sizeof(pat) );
204 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
205 &pat[0], &pat[1], &pat[2], &pat[3],
206 &pat[4], &pat[5], &pat[6], &pat[7] ))
208 WORD pattern[8];
209 HBITMAP hbitmap;
210 int i;
212 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
213 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
214 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
215 DeleteObject( hbitmap );
217 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
218 return TRUE;