05ff746992aeb9705eded6eaa46c6735e12445bc
[reactos.git] / reactos / dll / win32 / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <wingdi.h>
24 #include <winuser.h>
25
26 #include "cards.h"
27
28 HBITMAP g_CardBitmaps[MAX_CARD_BITMAPS];
29 HINSTANCE g_hModule = 0;
30
31 /*
32 * Redundant function from 16-bit Windows time
33 */
34 BOOL WINAPI WEP(DWORD Unknown)
35 {
36 UNREFERENCED_PARAMETER(Unknown);
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 * internal
81 */
82 static __inline VOID BltCard(HDC hdc, INT x, INT y, INT dx, INT dy, HDC hdcCard, DWORD dwRasterOp, BOOL bStretch)
83 {
84 if (bStretch)
85 {
86 StretchBlt(hdc, x, y, dx, dy, hdcCard, 0, 0, CARD_WIDTH, CARD_HEIGHT, dwRasterOp);
87 }
88 else
89 {
90 BitBlt(hdc, x, y, dx, dy, hdcCard, 0, 0, dwRasterOp);
91 /*
92 * This is need when using Microsoft images, because they use two-color red/white images for
93 * red cards and thus needs fix-up of the edge to black color.
94 */
95 #if 0
96 if (ISREDCARD(card))
97 {
98 PatBlt(hdc, x, y + 2, 1, dy - 4, BLACKNESS);
99 PatBlt(hdc, x + dx - 1, y + 2, 1, dy - 4, BLACKNESS);
100 PatBlt(hdc, x + 2, y, dx - 4, 1, BLACKNESS);
101 PatBlt(hdc, x + 2, y + dy - 1, dx - 4, 1, BLACKNESS);
102 SetPixel(hdc, x + 1, y + 1, 0);
103 SetPixel(hdc, x + dx - 2, y + 1, 0);
104 SetPixel(hdc, x + 1, y + dy - 2, 0);
105 SetPixel(hdc, x + dx - 2, y + dy - 2, 0);
106 }
107 #endif
108 }
109 }
110
111 /*
112 * Render card
113 *
114 * Parameters:
115 * hdc - Handle of destination device context
116 * x - Position left
117 * y - Position right
118 * dx - Destination width
119 * dy - Destination height
120 * card - Image id (meaning depend on type)
121 * type - One of edt* constants
122 * color - Background color (?)
123 */
124 BOOL WINAPI cdtDrawExt(HDC hdc, INT x, INT y, INT dx, INT dy, INT card, INT type, COLORREF color)
125 {
126 HDC hdcCard;
127 DWORD dwRasterOp = SRCCOPY, OldBkColor;
128 BOOL bSaveEdges = TRUE;
129 BOOL bStretch = FALSE;
130
131 if (type & ectSAVEEDGESMASK)
132 {
133 type &= ~ectSAVEEDGESMASK;
134 bSaveEdges = FALSE;
135 }
136
137 if (dx != CARD_WIDTH || dy != CARD_HEIGHT)
138 {
139 bStretch = TRUE;
140 bSaveEdges = FALSE;
141 }
142
143 switch (type)
144 {
145 case ectINVERTED:
146 dwRasterOp = NOTSRCCOPY;
147 case ectFACES:
148 card = (card % 4) * 13 + (card / 4);
149 break;
150 case ectBACKS:
151 --card;
152 break;
153 case ectEMPTYNOBG:
154 dwRasterOp = SRCAND;
155 case ectEMPTY:
156 card = 52;
157 break;
158 case ectERASE:
159 break;
160 case ectREDX:
161 card = 66;
162 break;
163 case ectGREENO:
164 card = 67;
165 break;
166 default:
167 return FALSE;
168 }
169
170 if (type == ectEMPTY || type == ectERASE)
171 {
172 POINT pPoint;
173 HBRUSH hBrush;
174
175 hBrush = CreateSolidBrush(color);
176 GetDCOrgEx(hdc, &pPoint);
177 SetBrushOrgEx(hdc, pPoint.x, pPoint.y, 0);
178 SelectObject(hdc, hBrush);
179 PatBlt(hdc, x, y, dx, dy, PATCOPY);
180 }
181 if (type != ectERASE)
182 {
183 hdcCard = CreateCompatibleDC(hdc);
184 SelectObject(hdcCard, g_CardBitmaps[card]);
185 OldBkColor = SetBkColor(hdc, (type == ectFACES) ? 0xFFFFFF : color);
186 if (bSaveEdges)
187 {
188 COLORREF SavedPixels[12];
189 SavedPixels[0] = GetPixel(hdc, x, y);
190 SavedPixels[1] = GetPixel(hdc, x + 1, y);
191 SavedPixels[2] = GetPixel(hdc, x, y + 1);
192 SavedPixels[3] = GetPixel(hdc, x + dx - 1, y);
193 SavedPixels[4] = GetPixel(hdc, x + dx - 2, y);
194 SavedPixels[5] = GetPixel(hdc, x + dx - 1, y + 1);
195 SavedPixels[6] = GetPixel(hdc, x, y + dy - 1);
196 SavedPixels[7] = GetPixel(hdc, x + 1, y + dy - 1);
197 SavedPixels[8] = GetPixel(hdc, x, y + dy - 2);
198 SavedPixels[9] = GetPixel(hdc, x + dx - 1, y + dy - 1);
199 SavedPixels[10] = GetPixel(hdc, x + dx - 2, y + dy - 1);
200 SavedPixels[11] = GetPixel(hdc, x + dx - 1, y + dy - 2);
201
202 BltCard(hdc, x, y, dx, dy, hdcCard, dwRasterOp, bStretch);
203
204 SetPixel(hdc, x, y, SavedPixels[0]);
205 SetPixel(hdc, x + 1, y, SavedPixels[1]);
206 SetPixel(hdc, x, y + 1, SavedPixels[2]);
207 SetPixel(hdc, x + dx - 1, y, SavedPixels[3]);
208 SetPixel(hdc, x + dx - 2, y, SavedPixels[4]);
209 SetPixel(hdc, x + dx - 1, y + 1, SavedPixels[5]);
210 SetPixel(hdc, x, y + dy - 1, SavedPixels[6]);
211 SetPixel(hdc, x + 1, y + dy - 1, SavedPixels[7]);
212 SetPixel(hdc, x, y + dy - 2, SavedPixels[8]);
213 SetPixel(hdc, x + dx - 1, y + dy - 1, SavedPixels[9]);
214 SetPixel(hdc, x + dx - 2, y + dy - 1, SavedPixels[10]);
215 SetPixel(hdc, x + dx - 1, y + dy - 2, SavedPixels[11]);
216 }
217 else
218 {
219 BltCard(hdc, x, y, dx, dy, hdcCard, dwRasterOp, bStretch);
220 }
221 SetBkColor(hdc, OldBkColor);
222 DeleteDC(hdcCard);
223 }
224
225 return TRUE;
226 }
227
228
229 /***********************************************************************
230 * cdtAnimate (CARDS.@)
231 *
232 * Animate card background, we don't use it
233 */
234 BOOL WINAPI cdtAnimate(HDC hdc, int cardback, int x, int y, int frame)
235 {
236 UNREFERENCED_PARAMETER(frame);
237 UNREFERENCED_PARAMETER(y);
238 UNREFERENCED_PARAMETER(x);
239 UNREFERENCED_PARAMETER(cardback);
240 UNREFERENCED_PARAMETER(hdc);
241 return TRUE;
242 }
243
244 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
245 {
246 UNREFERENCED_PARAMETER(lpvReserved);
247 if (fdwReason == DLL_PROCESS_ATTACH)
248 g_hModule = hinstDLL;
249
250 return TRUE;
251 }