update for HEAD-2003091401
[reactos.git] / subsys / win32k / eng / misc.c
1 /*
2  *  ReactOS W32 Subsystem
3  *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$ */
20 #include <ddk/winddi.h>
21 #include <include/dib.h>
22 #include <include/object.h>
23 #include <include/surface.h>
24 #include "misc.h"
25 #include "objects.h"
26
27 BOOL STDCALL
28 IntEngEnter(PINTENG_ENTER_LEAVE EnterLeave,
29             SURFOBJ *DestObj,
30             RECTL *DestRect,
31             BOOL ReadOnly,
32             POINTL *Translate,
33             SURFOBJ **OutputObj)
34 {
35   LONG Exchange;
36   SIZEL BitmapSize;
37   POINTL SrcPoint;
38   LONG Width;
39
40   /* Normalize */
41   if (DestRect->right < DestRect->left)
42     {
43     Exchange = DestRect->left;
44     DestRect->left = DestRect->right;
45     DestRect->right = Exchange;
46     }
47   if (DestRect->bottom < DestRect->top)
48     {
49     Exchange = DestRect->top;
50     DestRect->top = DestRect->bottom;
51     DestRect->bottom = Exchange;
52     }
53
54   if (NULL != DestObj && STYPE_BITMAP != DestObj->iType &&
55       (NULL == DestObj->pvScan0 || 0 == DestObj->lDelta))
56     {
57     EnterLeave->DestGDI = (SURFGDI*)AccessInternalObjectFromUserObject(DestObj);
58     /* Driver needs to support DrvCopyBits, else we can't do anything */
59     if (NULL == EnterLeave->DestGDI->CopyBits)
60       {
61       return FALSE;
62       }
63
64     /* Allocate a temporary bitmap */
65     BitmapSize.cx = DestRect->right - DestRect->left;
66     BitmapSize.cy = DestRect->bottom - DestRect->top;
67     Width = DIB_GetDIBWidthBytes(BitmapSize.cx, BitsPerFormat(DestObj->iBitmapFormat));
68     EnterLeave->OutputBitmap = EngCreateBitmap(BitmapSize, Width,
69                                                DestObj->iBitmapFormat,
70                                                BMF_NOZEROINIT, NULL);
71     *OutputObj = (SURFOBJ *) AccessUserObject((ULONG) EnterLeave->OutputBitmap);
72
73     EnterLeave->DestRect.left = 0;
74     EnterLeave->DestRect.top = 0;
75     EnterLeave->DestRect.right = BitmapSize.cx;
76     EnterLeave->DestRect.bottom = BitmapSize.cy;
77     SrcPoint.x = DestRect->left;
78     SrcPoint.y = DestRect->top;
79     EnterLeave->TrivialClipObj = EngCreateClip();
80     EnterLeave->TrivialClipObj->iDComplexity = DC_TRIVIAL;
81     if (! EnterLeave->DestGDI->CopyBits(*OutputObj, DestObj,
82                                         EnterLeave->TrivialClipObj, NULL,
83                                         &EnterLeave->DestRect, &SrcPoint))
84       {
85       EngDeleteClip(EnterLeave->TrivialClipObj);
86       EngFreeMem((*OutputObj)->pvBits);
87       EngDeleteSurface(EnterLeave->OutputBitmap);
88       return FALSE;
89       }
90     EnterLeave->DestRect.left = DestRect->left;
91     EnterLeave->DestRect.top = DestRect->top;
92     EnterLeave->DestRect.right = DestRect->right;
93     EnterLeave->DestRect.bottom = DestRect->bottom;
94     Translate->x = - DestRect->left;
95     Translate->y = - DestRect->top;
96     }
97   else
98     {
99     Translate->x = 0;
100     Translate->y = 0;
101     *OutputObj = DestObj;
102     }
103
104   EnterLeave->DestObj = DestObj;
105   EnterLeave->OutputObj = *OutputObj;
106   EnterLeave->ReadOnly = ReadOnly;
107
108   return TRUE;
109 }
110
111 BOOL STDCALL
112 IntEngLeave(PINTENG_ENTER_LEAVE EnterLeave)
113 {
114   POINTL SrcPoint;
115   BOOL Result;
116
117   if (EnterLeave->OutputObj != EnterLeave->DestObj && NULL != EnterLeave->OutputObj)
118     {
119     if (! EnterLeave->ReadOnly)
120       {
121       SrcPoint.x = 0;
122       SrcPoint.y = 0;
123       Result = EnterLeave->DestGDI->CopyBits(EnterLeave->DestObj,
124                                              EnterLeave->OutputObj,
125                                              EnterLeave->TrivialClipObj, NULL,
126                                              &EnterLeave->DestRect, &SrcPoint);
127       }
128     EngFreeMem(EnterLeave->OutputObj->pvBits);
129     EngDeleteSurface(EnterLeave->OutputBitmap);
130     EngDeleteClip(EnterLeave->TrivialClipObj);
131     }
132   else
133     {
134     Result = TRUE;
135     }
136
137   return Result;
138 }
139 /* EOF */