Added ctrl-c support.
[wine.git] / controls / desktop.c
blobf3df7b00250b195fa0050682294fa65e1b6f909f
1 /*
2 * Desktop window class.
4 * Copyright 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "user.h"
28 #include "controls.h"
29 #include "wine/winuser16.h"
31 static HBRUSH hbrushPattern;
32 static HBITMAP hbitmapWallPaper;
33 static SIZE bitmapSize;
34 static BOOL fTileWallPaper;
36 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
39 /*********************************************************************
40 * desktop class descriptor
42 const struct builtin_class_descr DESKTOP_builtin_class =
44 DESKTOP_CLASS_ATOM, /* name */
45 CS_GLOBALCLASS, /* style */
46 NULL, /* procA (winproc is Unicode only) */
47 DesktopWndProc, /* procW */
48 0, /* extra */
49 IDC_ARROWA, /* cursor */
50 COLOR_BACKGROUND+1 /* brush */
54 /***********************************************************************
55 * DESKTOP_LoadBitmap
57 * Load a bitmap from a file. Used by SetDeskWallPaper().
59 static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename )
61 BITMAPFILEHEADER *fileHeader;
62 BITMAPINFO *bitmapInfo;
63 HBITMAP hbitmap;
64 HFILE file;
65 LPSTR buffer;
66 LONG size;
68 /* Read all the file into memory */
70 if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR)
72 UINT len = GetWindowsDirectoryA( NULL, 0 );
73 if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
74 len + strlen(filename) + 2 )))
75 return 0;
76 GetWindowsDirectoryA( buffer, len + 1 );
77 strcat( buffer, "\\" );
78 strcat( buffer, filename );
79 file = _lopen( buffer, OF_READ );
80 HeapFree( GetProcessHeap(), 0, buffer );
82 if (file == HFILE_ERROR) return 0;
83 size = _llseek( file, 0, 2 );
84 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
86 _lclose( file );
87 return 0;
89 _llseek( file, 0, 0 );
90 size = _lread( file, buffer, size );
91 _lclose( file );
92 fileHeader = (BITMAPFILEHEADER *)buffer;
93 bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER));
95 /* Check header content */
96 if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize))
98 HeapFree( GetProcessHeap(), 0, buffer );
99 return 0;
101 hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT,
102 buffer + fileHeader->bfOffBits,
103 bitmapInfo, DIB_RGB_COLORS );
104 HeapFree( GetProcessHeap(), 0, buffer );
105 return hbitmap;
110 /***********************************************************************
111 * DesktopWndProc
113 static LRESULT WINAPI DesktopWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
115 LRESULT retvalue = 0;
117 if (message == WM_NCCREATE)
119 hbrushPattern = 0;
120 hbitmapWallPaper = 0;
121 SetDeskPattern();
122 SetDeskWallPaper( (LPSTR)-1 );
123 retvalue = 1;
125 /* all other messages are ignored */
126 return retvalue;
129 /***********************************************************************
130 * PaintDesktop (USER32.@)
133 BOOL WINAPI PaintDesktop(HDC hdc)
135 HWND hwnd = GetDesktopWindow();
137 /* check for an owning thread; otherwise don't paint anything (non-desktop mode) */
138 if (GetWindowThreadProcessId( hwnd, NULL ))
140 RECT rect;
142 GetClientRect( hwnd, &rect );
144 /* Paint desktop pattern (only if wall paper does not cover everything) */
146 if (!hbitmapWallPaper ||
147 (!fTileWallPaper && ((bitmapSize.cx < rect.right) || (bitmapSize.cy < rect.bottom))))
149 HBRUSH brush = hbrushPattern;
150 if (!brush) brush = GetClassLongA( hwnd, GCL_HBRBACKGROUND );
151 /* Set colors in case pattern is a monochrome bitmap */
152 SetBkColor( hdc, RGB(0,0,0) );
153 SetTextColor( hdc, GetSysColor(COLOR_BACKGROUND) );
154 FillRect( hdc, &rect, brush );
157 /* Paint wall paper */
159 if (hbitmapWallPaper)
161 INT x, y;
162 HDC hMemDC = CreateCompatibleDC( hdc );
164 SelectObject( hMemDC, hbitmapWallPaper );
166 if (fTileWallPaper)
168 for (y = 0; y < rect.bottom; y += bitmapSize.cy)
169 for (x = 0; x < rect.right; x += bitmapSize.cx)
170 BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
172 else
174 x = (rect.left + rect.right - bitmapSize.cx) / 2;
175 y = (rect.top + rect.bottom - bitmapSize.cy) / 2;
176 if (x < 0) x = 0;
177 if (y < 0) y = 0;
178 BitBlt( hdc, x, y, bitmapSize.cx, bitmapSize.cy, hMemDC, 0, 0, SRCCOPY );
180 DeleteDC( hMemDC );
183 return TRUE;
186 /***********************************************************************
187 * OldSetDeskPattern (USER.279)
189 BOOL16 WINAPI SetDeskPattern(void)
191 char buffer[100];
192 GetProfileStringA( "desktop", "Pattern", "(None)", buffer, 100 );
193 return DESKTOP_SetPattern( buffer );
197 /***********************************************************************
198 * SetDeskWallPaper (USER.285)
200 BOOL16 WINAPI SetDeskWallPaper16( LPCSTR filename )
202 return SetDeskWallPaper( filename );
206 /***********************************************************************
207 * SetDeskWallPaper (USER32.@)
209 * FIXME: is there a unicode version?
211 BOOL WINAPI SetDeskWallPaper( LPCSTR filename )
213 HBITMAP hbitmap;
214 HDC hdc;
215 char buffer[256];
217 if (filename == (LPSTR)-1)
219 GetProfileStringA( "desktop", "WallPaper", "(None)", buffer, 256 );
220 filename = buffer;
222 hdc = GetDC( 0 );
223 hbitmap = DESKTOP_LoadBitmap( hdc, filename );
224 ReleaseDC( 0, hdc );
225 if (hbitmapWallPaper) DeleteObject( hbitmapWallPaper );
226 hbitmapWallPaper = hbitmap;
227 fTileWallPaper = GetProfileIntA( "desktop", "TileWallPaper", 0 );
228 if (hbitmap)
230 BITMAP bmp;
231 GetObjectA( hbitmap, sizeof(bmp), &bmp );
232 bitmapSize.cx = (bmp.bmWidth != 0) ? bmp.bmWidth : 1;
233 bitmapSize.cy = (bmp.bmHeight != 0) ? bmp.bmHeight : 1;
235 return TRUE;
239 /***********************************************************************
240 * DESKTOP_SetPattern
242 * Set the desktop pattern.
244 BOOL DESKTOP_SetPattern( LPCSTR pattern )
246 int pat[8];
248 if (hbrushPattern) DeleteObject( hbrushPattern );
249 memset( pat, 0, sizeof(pat) );
250 if (pattern && sscanf( pattern, " %d %d %d %d %d %d %d %d",
251 &pat[0], &pat[1], &pat[2], &pat[3],
252 &pat[4], &pat[5], &pat[6], &pat[7] ))
254 WORD pattern[8];
255 HBITMAP hbitmap;
256 int i;
258 for (i = 0; i < 8; i++) pattern[i] = pat[i] & 0xffff;
259 hbitmap = CreateBitmap( 8, 8, 1, 1, (LPSTR)pattern );
260 hbrushPattern = CreatePatternBrush( hbitmap );
261 DeleteObject( hbitmap );
263 else hbrushPattern = 0;
264 return TRUE;