My mistake
[reactos.git] / rosapps / hcalc / hcalc.c
1 /* Copyright 1998 DJ Delorie <dj@delorie.com>
2 Distributed under the terms of the GNU GPL
3 http://www.delorie.com/store/hcalc/
4 */
5 #include <stdio.h>
6 #include <stdarg.h>
7
8 #define STRICT
9 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
11
12 #include "hcalc.h"
13
14 #define WIDTH 125
15 #define HEIGHT 147
16
17 /*
18 ** local vars
19 */
20 static char szAppName[16];
21 static char szTitle[80];
22 static HINSTANCE hInst;
23 static HBITMAP face, chars, bits;
24
25 static int r=0, g=0, b=0;
26 static HWND window;
27
28 static char shown_offsets[15];
29 static int shown_bitmask;
30 static int show_bits;
31
32 #define CHARS_LEFT 6
33 #define CHARS_TOP 6
34
35 #define BITS_RIGHT 92
36 #define BITS_TOP 6
37
38 char charmap[] = " 0123456789ABCDEF-x,.ro+";
39 int char_to_x[256];
40
41 void
42 paint_bits(HDC dc, HDC bdc)
43 {
44 int i;
45 SelectObject(bdc, bits);
46 for (i=0; i<32; i++)
47 {
48 int b = (shown_bitmask >> i) & 1;
49 BitBlt(dc, BITS_RIGHT-2*i-3*(i/4), BITS_TOP, 1, 7,
50 bdc, b, 0, SRCCOPY);
51 }
52
53 }
54
55 void
56 paint_chars(HDC dc, HDC bdc)
57 {
58 int i;
59 SelectObject(bdc, chars);
60 for (i=0; i<15; i++)
61 {
62 BitBlt(dc, CHARS_LEFT+6*i, CHARS_TOP, 5, 7,
63 bdc, shown_offsets[i], 0, SRCCOPY);
64 }
65 }
66
67 int
68 paint()
69 {
70 PAINTSTRUCT paintstruct;
71 HDC dc = BeginPaint(window, &paintstruct);
72 HDC bdc = CreateCompatibleDC(dc);
73 SelectObject(bdc, face);
74 BitBlt(dc, 0, 0, WIDTH, HEIGHT, bdc, 0, 0, SRCCOPY);
75
76 if (show_bits)
77 paint_bits(dc, bdc);
78 else
79 paint_chars(dc, bdc);
80
81 DeleteDC(bdc);
82 EndPaint(window, &paintstruct);
83 return 0;
84 }
85
86 void
87 redraw()
88 {
89 RECT r;
90 r.left = 0;
91 r.right = WIDTH-1;
92 r.top = 0;
93 r.bottom = HEIGHT-1;
94 InvalidateRect(window, &r, FALSE);
95 }
96
97 void
98 set_bits(int b)
99 {
100 shown_bitmask = b;
101 show_bits = 1;
102 redraw();
103 }
104
105 void
106 set_string(char *s)
107 {
108 char tmp[16];
109 int i;
110 sprintf(tmp, "%15.15s", s);
111 for (i=0; i<15; i++)
112 shown_offsets[i] = char_to_x[tmp[i]];
113 show_bits = 0;
114 redraw();
115 }
116
117 static int count=0;
118 static char tmp[100];
119 static char ctmp[20] = " ";
120
121 void
122 do_exit(int ec)
123 {
124 PostQuitMessage(ec);
125 }
126
127 /*
128 ** Main Windows Proc
129 */
130 LRESULT CALLBACK
131 WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
132 {
133 // int i;
134 // int lw = LOWORD(wParam);
135 // int hw = HIWORD(wParam);
136 // HWND w = (HWND)lParam;
137
138 window = hWnd;
139
140 switch (message)
141 {
142 case WM_DESTROY:
143 PostQuitMessage(0);
144 break;
145
146 case WM_PAINT:
147 return paint();
148
149 case WM_LBUTTONDOWN:
150 #if 0
151 count++;
152 wsprintf(tmp, "%3d %3d", LOWORD(lParam), HIWORD(lParam));
153 set_string(tmp);
154 #else
155 button(1, LOWORD(lParam), HIWORD(lParam));
156 #endif
157 break;
158
159 case WM_RBUTTONDOWN:
160 #if 0
161 count++;
162 set_bits(count);
163 #else
164 button(2, LOWORD(lParam), HIWORD(lParam));
165 #endif
166 break;
167
168 case WM_CHAR:
169 #if 0
170 for (i=0; i<20; i++)
171 ctmp[i] = ctmp[i+1];
172 ctmp[14] = wParam;
173 ctmp[15] = 0;
174 set_string(ctmp);
175 #else
176 key(wParam);
177 #endif
178 break;
179
180 default:
181 break;
182
183 } /* switch message */
184
185 return DefWindowProc (hWnd, message, wParam, lParam);
186 }
187
188 /*
189 ** register class
190 */
191 static BOOL
192 InitApplication(HINSTANCE hInstance, int nCmdShow)
193 {
194 int i, style;
195 WNDCLASS wc;
196 HWND hWnd;
197 RECT size;
198
199 LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
200 LoadString(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle));
201
202 hInst = hInstance;
203
204 wc.style = CS_HREDRAW | CS_VREDRAW;
205 wc.lpfnWndProc = (WNDPROC)WndProc;
206 wc.cbClsExtra = 0;
207 wc.cbWndExtra = 0;
208 wc.hInstance = hInstance;
209 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
210 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
211 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
212 wc.lpszMenuName = NULL;
213 wc.lpszClassName = szAppName;
214
215 if (RegisterClass(&wc) == 0)
216 return FALSE;
217
218 style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
219
220 size.left = 0;
221 size.top = 0;
222 size.right = WIDTH-3;
223 size.bottom = HEIGHT-3;
224 AdjustWindowRect(&size, style, 0);
225
226 hWnd = CreateWindowEx(WS_EX_TOPMOST,
227 szAppName,
228 szTitle,
229 style,
230 CW_USEDEFAULT, 0,
231 size.right-size.left, size.bottom-size.top,
232 NULL,
233 NULL,
234 hInstance,
235 NULL
236 );
237
238 if (hWnd == NULL)
239 return FALSE;
240
241 face = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_FACE));
242 chars = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CHARS));
243 bits = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITS));
244
245 for (i=0; i<256; i++)
246 char_to_x[i] = 0;
247 for (i=0; charmap[i]; i++)
248 char_to_x[charmap[i]] = i*6;
249
250 window = hWnd;
251
252 ShowWindow(hWnd, nCmdShow);
253 UpdateWindow(hWnd);
254
255 return TRUE;
256 }
257
258 /*
259 ** Main entry
260 */
261 int WINAPI
262 WinMain(HINSTANCE hInstance,
263 HINSTANCE hPrevInstance,
264 LPSTR lpszCmdLine,
265 int nCmdShow)
266 {
267 MSG msg;
268 HANDLE hAccelTable;
269
270 if (!InitApplication(hInstance, nCmdShow))
271 return FALSE;
272
273 hAccelTable = LoadAccelerators(hInstance, szAppName);
274
275 while( GetMessage(&msg, NULL, 0, 0))
276 if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg))
277 {
278 TranslateMessage(&msg);
279 DispatchMessage(&msg);
280 }
281 return msg.wParam;
282 }