This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / lib / cards / cards.c
1 /*
2  *  ReactOS Cards
3  *
4  *  Copyright (C) 2003  Filip Navara <xnavara@volny.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "cards.h"
28
29 HBITMAP g_CardBitmaps[MAX_CARD_BITMAPS];
30 HINSTANCE g_hModule = 0;
31
32 /*
33  * Redundant function from 16-bit Windows time
34  */
35 BOOL WINAPI WEP(DWORD Unknown)
36 {
37         return TRUE;
38 }
39
40 /*
41  * Initialize card library and return cards width and height
42  */
43 BOOL WINAPI cdtInit(INT *Width, INT *Height)
44 {       
45     DWORD dwIndex;
46
47     /* Report card width and height to user */
48         *Width = CARD_WIDTH;
49         *Height = CARD_HEIGHT;
50
51         /* Load images */
52         for (dwIndex = 0; dwIndex < MAX_CARD_BITMAPS; ++dwIndex)
53                 g_CardBitmaps[dwIndex] =
54                         (HBITMAP)LoadBitmapA(g_hModule, MAKEINTRESOURCEA(dwIndex + 1));
55
56         return TRUE;
57 }
58
59 /*
60  * Terminate card library
61  */
62 VOID WINAPI cdtTerm(VOID)
63 {
64     DWORD dwIndex;
65
66     /* Unload images */
67         for (dwIndex = 0; dwIndex < MAX_CARD_BITMAPS; dwIndex++)
68                 DeleteObject(g_CardBitmaps[dwIndex]);
69 }
70
71 /*
72  * Render card with no stretching
73  */
74 BOOL WINAPI cdtDraw(HDC hdc, INT x, INT y, INT card, INT type, COLORREF color)
75 {
76         return cdtDrawExt(hdc, x, y, CARD_WIDTH, CARD_HEIGHT, card, type, color);
77 }
78
79 /*
80  * Render card
81  *
82  * Parameters:
83  *    hdc - Handle of destination device context
84  *    x - Position left
85  *    y - Position right
86  *    dx - Destination width
87  *    dy - Destination height
88  *    card - Image id (meaning depend on type)
89  *    type - One of edt* constants
90  *    color - Background color (?)
91  */
92 BOOL WINAPI cdtDrawExt(HDC hdc, INT x, INT y, INT dx, INT dy, INT card, INT type, COLORREF color)
93 {
94         HDC hdcCard;
95         COLORREF SavedPixels[12];
96         DWORD dwRasterOp = SRCCOPY, OldBkColor;
97         BOOL bSaveEdges = TRUE;
98         BOOL bStretch = FALSE;
99
100         if (type & ectSAVEEDGESMASK)
101         {
102                 type &= ~ectSAVEEDGESMASK;
103                 bSaveEdges = FALSE;
104         }
105
106         if (dx != CARD_WIDTH || dy != CARD_HEIGHT)
107         {
108                 bStretch = TRUE;
109                 bSaveEdges = FALSE;
110         }
111
112         switch (type)
113         {
114                 case ectINVERTED:
115                         dwRasterOp = NOTSRCCOPY;
116                 case ectFACES:
117                         card = (card % 4) * 13 + (card / 4);
118                         break;
119                 case ectBACKS:
120                         --card;
121                         break;
122                 case ectEMPTYNOBG:
123                         dwRasterOp = SRCAND;
124                 case ectEMPTY:
125                         card = 52;
126                         break;
127                 case ectERASE:
128                         break;
129                 case ectREDX:
130                         card = 66;
131                         break;
132                 case ectGREENO:
133                         card = 67;
134                         break;
135                 default:
136                         return FALSE;
137         }
138
139         if (type == ectEMPTY || type == ectERASE)
140         {
141             POINT pPoint;
142             HBRUSH hBrush;
143
144             hBrush = CreateSolidBrush(color);
145                 GetDCOrgEx(hdc, &pPoint);
146                 SetBrushOrgEx(hdc, pPoint.x, pPoint.y, 0);
147                 SelectObject(hdc, hBrush);
148                 PatBlt(hdc, x, y, dx, dy, PATCOPY);
149         }
150         if (type != ectERASE)
151         {
152                 hdcCard = CreateCompatibleDC(hdc);
153                 SelectObject(hdcCard, g_CardBitmaps[card]);
154                 OldBkColor = SetBkColor(hdc, (type == ectFACES) ? 0xFFFFFF : color);
155                 if (bSaveEdges)
156                 {
157                         SavedPixels[0] = GetPixel(hdc, x, y);
158                         SavedPixels[1] = GetPixel(hdc, x + 1, y);
159                         SavedPixels[2] = GetPixel(hdc, x, y + 1);
160                         SavedPixels[3] = GetPixel(hdc, x + dx - 1, y);
161                         SavedPixels[4] = GetPixel(hdc, x + dx - 2, y);
162                         SavedPixels[5] = GetPixel(hdc, x + dx - 1, y + 1);
163                         SavedPixels[6] = GetPixel(hdc, x, y + dy - 1);
164                         SavedPixels[7] = GetPixel(hdc, x + 1, y + dy - 1);
165                         SavedPixels[8] = GetPixel(hdc, x, y + dy - 2);
166                         SavedPixels[9] = GetPixel(hdc, x + dx - 1, y + dy - 1);
167                         SavedPixels[10] = GetPixel(hdc, x + dx - 2, y + dy - 1);
168                         SavedPixels[11] = GetPixel(hdc, x + dx - 1, y + dy - 2);
169                 }
170                 if (bStretch)
171                 {
172                         StretchBlt(hdc, x, y, dx, dy, hdcCard, 0, 0, CARD_WIDTH, CARD_HEIGHT, dwRasterOp);
173                 } else
174                 {
175                         BitBlt(hdc, x, y, dx, dy, hdcCard, 0, 0, dwRasterOp);
176 /*
177  * This is need when using Microsoft images, because they use two-color red/white images for
178  * red cards and thus needs fix-up of the edge to black color.
179  */
180 #if 0
181                         if (ISREDCARD(card))
182                         {
183                                 PatBlt(hdc, x, y + 2, 1, dy - 4, BLACKNESS);
184                                 PatBlt(hdc, x + dx - 1, y + 2, 1, dy - 4, BLACKNESS);
185                                 PatBlt(hdc, x + 2, y, dx - 4, 1, BLACKNESS);
186                                 PatBlt(hdc, x + 2, y + dy - 1, dx - 4, 1, BLACKNESS);
187                                 SetPixel(hdc, x + 1, y + 1, 0);
188                                 SetPixel(hdc, x + dx - 2, y + 1, 0);
189                                 SetPixel(hdc, x + 1, y + dy - 2, 0);
190                                 SetPixel(hdc, x + dx - 2, y + dy - 2, 0);
191                         }
192 #endif
193                 }
194                 if (bSaveEdges)
195                 {
196                         SetPixel(hdc, x, y, SavedPixels[0]);
197                         SetPixel(hdc, x + 1, y, SavedPixels[1]);
198                         SetPixel(hdc, x, y + 1, SavedPixels[2]);
199                         SetPixel(hdc, x + dx - 1, y, SavedPixels[3]);
200                         SetPixel(hdc, x + dx - 2, y, SavedPixels[4]);
201                         SetPixel(hdc, x + dx - 1, y + 1, SavedPixels[5]);
202                         SetPixel(hdc, x, y + dy - 1, SavedPixels[6]);
203                         SetPixel(hdc, x + 1, y + dy - 1, SavedPixels[7]);
204                         SetPixel(hdc, x, y + dy - 2, SavedPixels[8]);
205                         SetPixel(hdc, x + dx - 1, y + dy - 1, SavedPixels[9]);
206                         SetPixel(hdc, x + dx - 2, y + dy - 1, SavedPixels[10]);
207                         SetPixel(hdc, x + dx - 1, y + dy - 2, SavedPixels[11]);
208                 }
209                 SetBkColor(hdc, OldBkColor);
210                 DeleteDC(hdcCard);
211         }
212
213         return TRUE;
214 }
215
216
217 /***********************************************************************
218  *             cdtAnimate   (CARDS.@)
219  *
220  * Animate card background, we don't use it
221  */
222 BOOL WINAPI cdtAnimate(HDC hdc, int cardback, int x, int y, int frame)
223 {
224         return TRUE;
225 }
226
227 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
228 {
229     if (fdwReason == DLL_PROCESS_ATTACH)
230                 g_hModule = hinstDLL;
231
232     return TRUE;
233 }