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