Release 960717
[wine.git] / controls / desktop.c
blobb9fa9dded290b21c3fc51aed3e79551f4e20001a
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "win.h"
11 #include "desktop.h"
12 #include "directory.h"
13 #include "file.h"
14 #include "graphics.h"
15 #include "heap.h"
18 /***********************************************************************
19 * DESKTOP_LoadBitmap
21 * Load a bitmap from a file. Used by SetDeskWallPaper().
23 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
25 BITMAPFILEHEADER *fileHeader;
26 BITMAPINFO *bitmapInfo;
27 HBITMAP hbitmap;
28 HFILE file;
29 LPSTR buffer;
30 LONG size;
32 /* Read all the file into memory */
34 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
36 UINT32 len = GetWindowsDirectory( NULL, 0 );
37 if (!(buffer = HeapAlloc( SystemHeap, 0, len + strlen(filename) + 2 )))
38 return 0;
39 GetWindowsDirectory( buffer, len + 1 );
40 strcat( buffer, "\\" );
41 strcat( buffer, filename );
42 file = _lopen( buffer, OF_READ );
43 HeapFree( SystemHeap, 0, buffer );
45 if (file == HFILE_ERROR) return 0;
46 size = _llseek( file, 0, 2 );
47 if (!(buffer = HeapAlloc( SystemHeap, 0, size )))
49 _lclose( file );
50 return 0;
52 _llseek( file, 0, 0 );
53 size = FILE_Read( file, buffer, size );
54 _lclose( file );
55 fileHeader = (BITMAPFILEHEADER *)buffer;
56 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
58 /* Check header content */
59 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
61 HeapFree( SystemHeap, 0, buffer );
62 return 0;
64 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
65 buffer + fileHeader->bfOffBits,
66 bitmapInfo, DIB_RGB_COLORS );
67 HeapFree( SystemHeap, 0, buffer );
68 return hbitmap;
72 /***********************************************************************
73 * DESKTOP_DoEraseBkgnd
75 * Handle the WM_ERASEBKGND message.
77 static LONG DESKTOP_DoEraseBkgnd( HWND hwnd, HDC hdc, DESKTOPINFO *infoPtr )
79 RECT16 rect;
80 GetClientRect16( hwnd, &rect );
82 /* Paint desktop pattern (only if wall paper does not cover everything) */
84 if (!infoPtr->hbitmapWallPaper ||
85 (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
86 (infoPtr->bitmapSize.cy < rect.bottom))))
88 /* Set colors in case pattern is a monochrome bitmap */
89 SetBkColor( hdc, RGB(0,0,0) );
90 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
91 FillRect16( hdc, &rect, infoPtr->hbrushPattern );
94 /* Paint wall paper */
96 if (infoPtr->hbitmapWallPaper)
98 int x, y;
100 if (infoPtr->fTileWallPaper)
102 for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
103 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
104 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
105 x, y, 0, 0,
106 infoPtr->bitmapSize.cx,
107 infoPtr->bitmapSize.cy );
109 else
111 x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
112 y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
113 if (x < 0) x = 0;
114 if (y < 0) y = 0;
115 GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0,
116 infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy );
120 return 1;
124 /***********************************************************************
125 * DesktopWndProc
127 * Window procedure for the desktop window.
129 LRESULT DesktopWndProc( HWND32 hwnd, UINT32 message,
130 WPARAM32 wParam, LPARAM lParam )
132 WND *wndPtr = WIN_FindWndPtr( hwnd );
133 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
135 /* Most messages are ignored (we DON'T call DefWindowProc) */
137 switch(message)
139 /* Warning: this message is sent directly by */
140 /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
141 case WM_NCCREATE:
142 infoPtr->hbrushPattern = 0;
143 infoPtr->hbitmapWallPaper = 0;
144 SetDeskPattern();
145 SetDeskWallPaper( (LPSTR)-1 );
146 break;
148 case WM_ERASEBKGND:
149 if (rootWindow == DefaultRootWindow(display)) return 1;
150 return DESKTOP_DoEraseBkgnd( hwnd, (HDC)wParam, infoPtr );
152 case WM_SYSCOMMAND:
153 if ((wParam & 0xfff0) != SC_CLOSE) return 0;
154 ExitWindows( 0, 0 );
156 case WM_SETCURSOR:
157 return (LRESULT)SetCursor( LoadCursor16( 0, IDC_ARROW ) );
160 return 0;
164 /***********************************************************************
165 * SetDeskPattern (USER.279)
167 BOOL SetDeskPattern(void)
169 char buffer[100];
170 GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
171 return DESKTOP_SetPattern( buffer );
175 /***********************************************************************
176 * SetDeskWallPaper (USER.285)
178 BOOL SetDeskWallPaper( LPCSTR filename )
180 HBITMAP hbitmap;
181 HDC hdc;
182 char buffer[256];
183 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
184 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
186 if (filename == (LPSTR)-1)
188 GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
189 filename = buffer;
191 hdc = GetDC( 0 );
192 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
193 ReleaseDC( 0, hdc );
194 if (infoPtr->hbitmapWallPaper) DeleteObject( infoPtr->hbitmapWallPaper );
195 infoPtr->hbitmapWallPaper = hbitmap;
196 infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
197 if (hbitmap)
199 BITMAP16 bmp;
200 GetObject16( hbitmap, sizeof(bmp), &bmp );
201 infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
202 infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
204 return TRUE;
208 /***********************************************************************
209 * DESKTOP_SetPattern
211 * Set the desktop pattern.
213 BOOL DESKTOP_SetPattern(char *pattern )
215 WND *wndPtr = WIN_FindWndPtr( GetDesktopWindow() );
216 DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
217 int pat[8];
219 if (infoPtr->hbrushPattern) DeleteObject( infoPtr->hbrushPattern );
220 memset( pat, 0, sizeof(pat) );
221 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
222 &pat[0], &pat[1], &pat[2], &pat[3],
223 &pat[4], &pat[5], &pat[6], &pat[7] ))
225 WORD pattern[8];
226 HBITMAP hbitmap;
227 int i;
229 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
230 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
231 infoPtr->hbrushPattern = CreatePatternBrush( hbitmap );
232 DeleteObject( hbitmap );
234 else infoPtr->hbrushPattern = CreateSolidBrush( GetSysColor(COLOR_BACKGROUND) );
235 return TRUE;