Release 960114
[wine/hacks.git] / controls / desktop.c
blob9f462c6e583a56df23baa95fc8af3f41aeb3a510
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"
20 /***********************************************************************
21 * DESKTOP_LoadBitmap
23 * Load a bitmap from a file. Used by SetDeskWallPaper().
25 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
27 BITMAPFILEHEADER *fileHeader;
28 BITMAPINFO *bitmapInfo;
29 HBITMAP hbitmap;
30 char *buffer;
31 const char *unixFileName;
32 int file;
33 long size;
35 /* Read all the file into memory */
37 if (!(unixFileName = DOSFS_GetUnixFileName( filename, TRUE ))) 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 );
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 );
113 return 1;
117 /***********************************************************************
118 * DesktopWndProc
120 * Window procedure for the desktop window.
122 LRESULT DesktopWndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
124 WND *wndPtr = WIN_FindWndPtr( hwnd );
125 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
127 /* Most messages are ignored (we DON'T call DefWindowProc) */
129 switch(message)
131 /* Warning: this message is sent directly by */
132 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
133 case WM_NCCREATE:
134 infoPtr->hbrushPattern = 0;
135 infoPtr->hbitmapWallPaper = 0;
136 SetDeskPattern();
137 SetDeskWallPaper( (LPSTR)-1 );
138 break;
140 case WM_ERASEBKGND:
141 if (rootWindow == DefaultRootWindow(display)) return 1;
142 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
145 return 0;
149 /***********************************************************************
150 * SetDeskPattern (USER.279)
152 BOOL SetDeskPattern(void)
154 char buffer[100];
155 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
156 return DESKTOP_SetPattern( buffer );
160 /***********************************************************************
161 * SetDeskWallPaper (USER.285)
163 BOOL SetDeskWallPaper( LPCSTR filename )
165 HBITMAP hbitmap;
166 HDC hdc;
167 char buffer[256];
168 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
169 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
171 if (filename == (LPSTR)-1)
173 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
174 filename = buffer;
176 hdc = GetDC( 0 );
177 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
178 ReleaseDC( 0, hdc );
179 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
180 infoPtr->hbitmapWallPaper = hbitmap;
181 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
182 if (hbitmap)
184 BITMAP bmp;
185 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
186 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
187 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
189 return TRUE;
193 /***********************************************************************
194 * DESKTOP_SetPattern
196 * Set the desktop pattern.
198 BOOL DESKTOP_SetPattern(char *pattern )
200 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
201 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
202 int pat[8];
204 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
205 memset( pat, 0, sizeof(pat) );
206 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
207 &pat[0], &pat[1], &pat[2], &pat[3],
208 &pat[4], &pat[5], &pat[6], &pat[7] ))
210 WORD pattern[8];
211 HBITMAP hbitmap;
212 int i;
214 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
215 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
216 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
217 DeleteObject( hbitmap );
219 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
220 return TRUE;