Release 960521
[wine.git] / controls / desktop.c
blobe70d85a396a09e2a1834af6d5c40503a53407403
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include "win.h"
13 #include "desktop.h"
14 #include "directory.h"
15 #include "dos_fs.h"
16 #include "graphics.h"
19 /***********************************************************************
20 * DESKTOP_LoadBitmap
22 * Load a bitmap from a file. Used by SetDeskWallPaper().
24 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
26 BITMAPFILEHEADER *fileHeader;
27 BITMAPINFO *bitmapInfo;
28 HBITMAP hbitmap;
29 char *buffer;
30 const char *unixFileName;
31 int file;
32 long size;
34 /* Read all the file into memory */
36 if (!(unixFileName = DOSFS_GetUnixFileName( filename, TRUE )))
38 int len = DIR_GetWindowsUnixDir( NULL, 0 );
39 if (!(buffer = malloc( len + strlen(filename) + 2 ))) return 0;
40 DIR_GetWindowsUnixDir( buffer, len + 1 );
41 strcat( buffer, "/" );
42 strcat( buffer, filename );
43 unixFileName = DOSFS_GetUnixFileName( buffer, TRUE );
44 free( buffer );
45 if (!unixFileName) return 0;
47 if ((file = open( unixFileName, O_RDONLY )) == -1) return 0;
48 size = lseek( file, 0, SEEK_END );
49 if (!(buffer = (char *)malloc( size )))
51 close( file );
52 return 0;
54 lseek( file, 0, SEEK_SET );
55 size = read( file, buffer, size );
56 close( file );
57 fileHeader = (BITMAPFILEHEADER *)buffer;
58 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
60 /* Check header content */
61 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
63 free( buffer );
64 return 0;
66 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
67 buffer + fileHeader->bfOffBits,
68 bitmapInfo, DIB_RGB_COLORS );
69 free( buffer );
70 return hbitmap;
74 /***********************************************************************
75 * DESKTOP_DoEraseBkgnd
77 * Handle the WM_ERASEBKGND message.
79 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
81 RECT16 rect;
82 GetClientRect16( hwnd, &rect );
84 /* Paint desktop pattern (only if wall paper does not cover everything) */
86 if (!infoPtr->hbitmapWallPaper ||
87 (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
88 (infoPtr->bitmapSize.cy < rect.bottom))))
90 /* Set colors in case pattern is a monochrome bitmap */
91 SetBkColor( hdc, RGB(0,0,0) );
92 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
93 FillRect16( hdc, &rect, infoPtr->hbrushPattern );
96 /* Paint wall paper */
98 if (infoPtr->hbitmapWallPaper)
100 int x, y;
102 if (infoPtr->fTileWallPaper)
104 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
105 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
106 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
107 x, y, 0, 0,
108 infoPtr->bitmapSize.cx,
109 infoPtr->bitmapSize.cy );
111 else
113 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
114 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
115 if (x < 0) x = 0;
116 if (y < 0) y = 0;
117 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
118 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy );
122 return 1;
126 /***********************************************************************
127 * DesktopWndProc
129 * Window procedure for the desktop window.
131 LRESULT DesktopWndProc( HWND32 hwnd, UINT32 message,
132 WPARAM32 wParam, LPARAM lParam )
134 WND *wndPtr = WIN_FindWndPtr( hwnd );
135 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
137 /* Most messages are ignored (we DON'T call DefWindowProc) */
139 switch(message)
141 /* Warning: this message is sent directly by */
142 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
143 case WM_NCCREATE:
144 infoPtr->hbrushPattern = 0;
145 infoPtr->hbitmapWallPaper = 0;
146 SetDeskPattern();
147 SetDeskWallPaper( (LPSTR)-1 );
148 break;
150 case WM_ERASEBKGND:
151 if (rootWindow == DefaultRootWindow(display)) return 1;
152 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
154 case WM_SYSCOMMAND:
155 if ((wParam & 0xfff0) != SC_CLOSE) return 0;
156 ExitWindows( 0, 0 );
158 case WM_SETCURSOR:
159 return (LRESULT)SetCursor( LoadCursor( 0, IDC_ARROW ) );
162 return 0;
166 /***********************************************************************
167 * SetDeskPattern (USER.279)
169 BOOL SetDeskPattern(void)
171 char buffer[100];
172 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
173 return DESKTOP_SetPattern( buffer );
177 /***********************************************************************
178 * SetDeskWallPaper (USER.285)
180 BOOL SetDeskWallPaper( LPCSTR filename )
182 HBITMAP hbitmap;
183 HDC hdc;
184 char buffer[256];
185 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
186 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
188 if (filename == (LPSTR)-1)
190 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
191 filename = buffer;
193 hdc = GetDC( 0 );
194 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
195 ReleaseDC( 0, hdc );
196 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
197 infoPtr->hbitmapWallPaper = hbitmap;
198 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
199 if (hbitmap)
201 BITMAP bmp;
202 GetObject( hbitmap, sizeof(bmp), (LPSTR)&bmp );
203 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
204 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
206 return TRUE;
210 /***********************************************************************
211 * DESKTOP_SetPattern
213 * Set the desktop pattern.
215 BOOL DESKTOP_SetPattern(char *pattern )
217 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
218 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
219 int pat[8];
221 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
222 memset( pat, 0, sizeof(pat) );
223 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
224 &pat[0], &pat[1], &pat[2], &pat[3],
225 &pat[4], &pat[5], &pat[6], &pat[7] ))
227 WORD pattern[8];
228 HBITMAP hbitmap;
229 int i;
231 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
232 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
233 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
234 DeleteObject( hbitmap );
236 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
237 return TRUE;