Release 961102
[wine] / controls / desktop.c
1 /*
2  * Desktop window class.
3  *
4  * Copyright 1994 Alexandre Julliard
5  */
6
7 #define NO_TRANSITION_TYPES  /* This file is Win32-clean */
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include "win.h"
12 #include "desktop.h"
13 #include "directory.h"
14 #include "graphics.h"
15 #include "heap.h"
16
17
18 /***********************************************************************
19  *           DESKTOP_LoadBitmap
20  *
21  * Load a bitmap from a file. Used by SetDeskWallPaper().
22  */
23 static HBITMAP32 DESKTOP_LoadBitmap( HDC32 hdc, const char *filename )
24 {
25     BITMAPFILEHEADER *fileHeader;
26     BITMAPINFO *bitmapInfo;
27     HBITMAP32 hbitmap;
28     HFILE file;
29     LPSTR buffer;
30     LONG size;
31
32     /* Read all the file into memory */
33
34     if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
35     {
36         UINT32 len = GetWindowsDirectory32A( NULL, 0 );
37         if (!(buffer = HeapAlloc( SystemHeap, 0, len + strlen(filename) + 2 )))
38             return 0;
39         GetWindowsDirectory32A( buffer, len + 1 );
40         strcat( buffer, "\\" );
41         strcat( buffer, filename );
42         file = _lopen( buffer, OF_READ );
43         HeapFree( SystemHeap, 0, buffer );
44     }
45     if (file == HFILE_ERROR) return 0;
46     size = _llseek( file, 0, 2 );
47     if (!(buffer = HeapAlloc( SystemHeap, 0, size )))
48     {
49         _lclose( file );
50         return 0;
51     }
52     _llseek( file, 0, 0 );
53     size = _lread32( file, buffer, size );
54     _lclose( file );
55     fileHeader = (BITMAPFILEHEADER *)buffer;
56     bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
57     
58       /* Check header content */
59     if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
60     {
61         HeapFree( SystemHeap, 0, buffer );
62         return 0;
63     }
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;
69 }
70
71
72 /***********************************************************************
73  *           DESKTOP_DoEraseBkgnd
74  *
75  * Handle the WM_ERASEBKGND message.
76  */
77 static LRESULT DESKTOP_DoEraseBkgnd( HWND32 hwnd, HDC32 hdc,
78                                      DESKTOPINFO *infoPtr )
79 {
80     RECT32 rect;
81     WND*   Wnd = WIN_FindWndPtr( hwnd );
82
83     if (Wnd->hrgnUpdate > 1) DeleteObject32( Wnd->hrgnUpdate );
84     Wnd->hrgnUpdate = 0;
85
86     GetClientRect32( hwnd, &rect );    
87
88     /* Paint desktop pattern (only if wall paper does not cover everything) */
89
90     if (!infoPtr->hbitmapWallPaper || 
91         (!infoPtr->fTileWallPaper && ((infoPtr->bitmapSize.cx < rect.right) ||
92          (infoPtr->bitmapSize.cy < rect.bottom))))
93     {
94           /* Set colors in case pattern is a monochrome bitmap */
95         SetBkColor( hdc, RGB(0,0,0) );
96         SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
97         FillRect32( hdc, &rect, infoPtr->hbrushPattern );
98     }
99
100       /* Paint wall paper */
101
102     if (infoPtr->hbitmapWallPaper)
103     {
104         INT32 x, y;
105
106         if (infoPtr->fTileWallPaper)
107         {
108             for (y = 0; y < rect.bottom; y += infoPtr->bitmapSize.cy)
109                 for (x = 0; x < rect.right; x += infoPtr->bitmapSize.cx)
110                     GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper,
111                                       x, y, 0, 0, 
112                                       infoPtr->bitmapSize.cx,
113                                       infoPtr->bitmapSize.cy );
114         }
115         else
116         {
117             x = (rect.left + rect.right - infoPtr->bitmapSize.cx) / 2;
118             y = (rect.top + rect.bottom - infoPtr->bitmapSize.cy) / 2;
119             if (x < 0) x = 0;
120             if (y < 0) y = 0;
121             GRAPH_DrawBitmap( hdc, infoPtr->hbitmapWallPaper, x, y, 0, 0, 
122                               infoPtr->bitmapSize.cx, infoPtr->bitmapSize.cy );
123         }
124     }
125
126     return 1;
127 }
128
129
130 /***********************************************************************
131  *           DesktopWndProc
132  *
133  * Window procedure for the desktop window.
134  */
135 LRESULT DesktopWndProc( HWND32 hwnd, UINT32 message,
136                         WPARAM32 wParam, LPARAM lParam )
137 {
138     WND *wndPtr = WIN_FindWndPtr( hwnd );
139     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
140
141       /* Most messages are ignored (we DON'T call DefWindowProc) */
142
143     switch(message)
144     {
145         /* Warning: this message is sent directly by                     */
146         /* WIN_CreateDesktopWindow() and does not contain a valid lParam */
147     case WM_NCCREATE:
148         infoPtr->hbrushPattern = 0;
149         infoPtr->hbitmapWallPaper = 0;
150         SetDeskPattern();
151         SetDeskWallPaper32( (LPSTR)-1 );
152         break;
153         
154     case WM_ERASEBKGND:
155         if (rootWindow == DefaultRootWindow(display)) return 1;
156         return DESKTOP_DoEraseBkgnd( hwnd, (HDC32)wParam, infoPtr );
157
158     case WM_SYSCOMMAND:
159         if ((wParam & 0xfff0) != SC_CLOSE) return 0;
160         ExitWindows( 0, 0 ); 
161
162     case WM_SETCURSOR:
163         return (LRESULT)SetCursor( LoadCursor16( 0, IDC_ARROW ) );
164     }
165     
166     return 0;
167 }
168
169
170 /***********************************************************************
171  *           SetDeskPattern   (USER.279)
172  */
173 BOOL16 SetDeskPattern(void)
174 {
175     char buffer[100];
176     GetProfileString( "desktop", "Pattern", "(None)", buffer, 100 );
177     return DESKTOP_SetPattern( buffer );
178 }
179
180
181 /***********************************************************************
182  *           SetDeskWallPaper16   (USER.285)
183  */
184 BOOL16 SetDeskWallPaper16( LPCSTR filename )
185 {
186     return SetDeskWallPaper32( filename );
187 }
188
189
190 /***********************************************************************
191  *           SetDeskWallPaper32   (USER32.475)
192  *
193  * FIXME: is there a unicode version?
194  */
195 BOOL32 SetDeskWallPaper32( LPCSTR filename )
196 {
197     HBITMAP32 hbitmap;
198     HDC32 hdc;
199     char buffer[256];
200     WND *wndPtr = WIN_GetDesktop();
201     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
202
203     if (filename == (LPSTR)-1)
204     {
205         GetProfileString( "desktop", "WallPaper", "(None)", buffer, 256 );
206         filename = buffer;
207     }
208     hdc = GetDC32( 0 );
209     hbitmap = DESKTOP_LoadBitmap( hdc, filename );
210     ReleaseDC32( 0, hdc );
211     if (infoPtr->hbitmapWallPaper) DeleteObject32( infoPtr->hbitmapWallPaper );
212     infoPtr->hbitmapWallPaper = hbitmap;
213     infoPtr->fTileWallPaper = GetProfileInt( "desktop", "TileWallPaper", 0 );
214     if (hbitmap)
215     {
216         BITMAP32 bmp;
217         GetObject32A( hbitmap, sizeof(bmp), &bmp );
218         infoPtr->bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
219         infoPtr->bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
220     }
221     return TRUE;
222 }
223
224
225 /***********************************************************************
226  *           DESKTOP_SetPattern
227  *
228  * Set the desktop pattern.
229  */
230 BOOL32 DESKTOP_SetPattern( LPCSTR pattern )
231 {
232     WND *wndPtr = WIN_GetDesktop();
233     DESKTOPINFO *infoPtr = (DESKTOPINFO *)wndPtr->wExtra;
234     int pat[8];
235
236     if (infoPtr->hbrushPattern) DeleteObject32( infoPtr->hbrushPattern );
237     memset( pat, 0, sizeof(pat) );
238     if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
239                            &pat[0], &pat[1], &pat[2], &pat[3],
240                            &pat[4], &pat[5], &pat[6], &pat[7] ))
241     {
242         WORD pattern[8];
243         HBITMAP32 hbitmap;
244         int i;
245
246         for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
247         hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
248         infoPtr->hbrushPattern = CreatePatternBrush32( hbitmap );
249         DeleteObject32( hbitmap );
250     }
251     else infoPtr->hbrushPattern = CreateSolidBrush32( GetSysColor(COLOR_BACKGROUND) );
252     return TRUE;
253 }
254