Debian Codegraph @

gc/cord/tests/de_win.c

raw
1 /*
2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /*
15 * The Windows specific part of de.
16 * This started as the generic Windows application template
17 * but significant parts didn't survive to the final version.
18 */
19 #if defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) \
20 || defined(__NT__) || defined(_WIN32) || defined(WIN32)
21
22 #ifndef WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN 1
24 #endif
25 #define NOSERVICE
26 #include <windows.h>
27
28 #include <ctype.h>
29
30 #include "gc.h"
31 #include "cord.h"
32 #include "de_cmds.h"
33 #include "de_win.h"
34
35 int LINES = 0;
36 int COLS = 0;
37
38 #define szAppName TEXT("DE")
39
40 HWND hwnd;
41
42 void de_error(const char *s)
43 {
44 (void)MessageBoxA(hwnd, s, "Demonstration Editor",
45 MB_ICONINFORMATION | MB_OK);
46 InvalidateRect(hwnd, NULL, TRUE);
47 }
48
49 int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
50 LPSTR command_line, int nCmdShow)
51 {
52 MSG msg;
53 WNDCLASS wndclass;
54 HACCEL hAccel;
55
56 GC_set_find_leak(0);
57 GC_INIT();
58 # ifndef NO_INCREMENTAL
59 GC_enable_incremental();
60 # endif
61 # if defined(CPPCHECK)
62 GC_noop1((GC_word)&WinMain);
63 # endif
64
65 if (!hPrevInstance)
66 {
67 wndclass.style = CS_HREDRAW | CS_VREDRAW;
68 wndclass.lpfnWndProc = WndProc;
69 wndclass.cbClsExtra = 0;
70 wndclass.cbWndExtra = DLGWINDOWEXTRA;
71 wndclass.hInstance = hInstance;
72 wndclass.hIcon = LoadIcon (hInstance, szAppName);
73 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
74 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
75 wndclass.lpszMenuName = TEXT("DE");
76 wndclass.lpszClassName = szAppName;
77
78 if (RegisterClass (&wndclass) == 0) {
79 de_error("RegisterClass error");
80 return(0);
81 }
82 }
83
84 /* Empirically, the command line does not include the command name ...
85 if (command_line != 0) {
86 while (isspace(*command_line)) command_line++;
87 while (*command_line != 0 && !isspace(*command_line)) command_line++;
88 while (isspace(*command_line)) command_line++;
89 } */
90
91 if (command_line == 0 || *command_line == 0) {
92 de_error("File name argument required");
93 return( 0 );
94 } else {
95 char *p = command_line;
96
97 while (*p != 0 && !isspace(*(unsigned char *)p))
98 p++;
99 arg_file_name = CORD_to_char_star(
100 CORD_substr(command_line, 0, p - command_line));
101 }
102
103 hwnd = CreateWindow (szAppName,
104 TEXT("Demonstration Editor"),
105 WS_OVERLAPPEDWINDOW | WS_CAPTION, /* Window style */
106 CW_USEDEFAULT, 0, /* default pos. */
107 CW_USEDEFAULT, 0, /* default width, height */
108 NULL, /* No parent */
109 NULL, /* Window class menu */
110 hInstance, NULL);
111 if (hwnd == NULL) {
112 de_error("CreateWindow error");
113 return(0);
114 }
115
116 ShowWindow (hwnd, nCmdShow);
117
118 hAccel = LoadAccelerators( hInstance, szAppName );
119
120 while (GetMessage (&msg, NULL, 0, 0))
121 {
122 if( !TranslateAccelerator( hwnd, hAccel, &msg ) )
123 {
124 TranslateMessage (&msg);
125 DispatchMessage (&msg);
126 }
127 }
128 return (int)msg.wParam;
129 }
130
131 /* Return the argument with all control characters replaced by blanks. */
132 char * plain_chars(char * text, size_t len)
133 {
134 char * result = (char *)GC_MALLOC_ATOMIC(len + 1);
135 size_t i;
136
137 if (NULL == result) return NULL;
138 for (i = 0; i < len; i++) {
139 if (iscntrl(((unsigned char *)text)[i])) {
140 result[i] = ' ';
141 } else {
142 result[i] = text[i];
143 }
144 }
145 result[len] = '\0';
146 return(result);
147 }
148
149 /* Return the argument with all non-control-characters replaced by */
150 /* blank, and all control characters c replaced by c + 64. */
151 char * control_chars(char * text, size_t len)
152 {
153 char * result = (char *)GC_MALLOC_ATOMIC(len + 1);
154 size_t i;
155
156 if (NULL == result) return NULL;
157 for (i = 0; i < len; i++) {
158 if (iscntrl(((unsigned char *)text)[i])) {
159 result[i] = (char)(text[i] + 0x40);
160 } else {
161 result[i] = ' ';
162 }
163 }
164 result[len] = '\0';
165 return(result);
166 }
167
168 int char_width;
169 int char_height;
170
171 void get_line_rect(int line_arg, int win_width, RECT * rectp)
172 {
173 rectp -> top = line_arg * (LONG)char_height;
174 rectp -> bottom = rectp->top + char_height;
175 rectp -> left = 0;
176 rectp -> right = win_width;
177 }
178
179 int caret_visible = 0; /* Caret is currently visible. */
180
181 int screen_was_painted = 0;/* Screen has been painted at least once. */
182
183 void update_cursor(void);
184
185 INT_PTR CALLBACK AboutBoxCallback( HWND hDlg, UINT message,
186 WPARAM wParam, LPARAM lParam )
187 {
188 (void)lParam;
189 switch( message )
190 {
191 case WM_INITDIALOG:
192 SetFocus( GetDlgItem( hDlg, IDOK ) );
193 break;
194
195 case WM_COMMAND:
196 switch( wParam )
197 {
198 case IDOK:
199 EndDialog( hDlg, TRUE );
200 break;
201 }
202 break;
203
204 case WM_CLOSE:
205 EndDialog( hDlg, TRUE );
206 return TRUE;
207
208 }
209 return FALSE;
210 }
211
212 LRESULT CALLBACK WndProc (HWND hwnd_arg, UINT message,
213 WPARAM wParam, LPARAM lParam)
214 {
215 static HINSTANCE hInstance;
216 HDC dc;
217 PAINTSTRUCT ps;
218 RECT client_area;
219 RECT this_line;
220 RECT dummy;
221 TEXTMETRIC tm;
222 int i;
223 int id;
224
225 switch (message)
226 {
227 case WM_CREATE:
228 hInstance = ( (LPCREATESTRUCT) lParam)->hInstance;
229 dc = GetDC(hwnd_arg);
230 SelectObject(dc, GetStockObject(SYSTEM_FIXED_FONT));
231 GetTextMetrics(dc, &tm);
232 ReleaseDC(hwnd_arg, dc);
233 char_width = tm.tmAveCharWidth;
234 char_height = tm.tmHeight + tm.tmExternalLeading;
235 GetClientRect(hwnd_arg, &client_area);
236 COLS = (client_area.right - client_area.left)/char_width;
237 LINES = (client_area.bottom - client_area.top)/char_height;
238 generic_init();
239 return(0);
240
241 case WM_CHAR:
242 if (wParam == QUIT) {
243 SendMessage(hwnd_arg, WM_CLOSE, 0, 0L);
244 } else {
245 do_command((int)wParam);
246 }
247 return(0);
248
249 case WM_SETFOCUS:
250 CreateCaret(hwnd_arg, NULL, char_width, char_height);
251 ShowCaret(hwnd_arg);
252 caret_visible = 1;
253 update_cursor();
254 return(0);
255
256 case WM_KILLFOCUS:
257 HideCaret(hwnd_arg);
258 DestroyCaret();
259 caret_visible = 0;
260 return(0);
261
262 case WM_LBUTTONUP:
263 {
264 unsigned xpos = LOWORD(lParam); /* From left */
265 unsigned ypos = HIWORD(lParam); /* from top */
266
267 set_position(xpos / (unsigned)char_width,
268 ypos / (unsigned)char_height);
269 return(0);
270 }
271
272 case WM_COMMAND:
273 id = LOWORD(wParam);
274 if (id & EDIT_CMD_FLAG) {
275 if (id & REPEAT_FLAG) do_command(REPEAT);
276 do_command(CHAR_CMD(id));
277 return( 0 );
278 } else {
279 switch(id) {
280 case IDM_FILEEXIT:
281 SendMessage(hwnd_arg, WM_CLOSE, 0, 0L);
282 return( 0 );
283
284 case IDM_HELPABOUT:
285 if( DialogBox( hInstance, TEXT("ABOUTBOX"),
286 hwnd_arg, AboutBoxCallback ) )
287 InvalidateRect(hwnd_arg, NULL, TRUE);
288 return( 0 );
289 case IDM_HELPCONTENTS:
290 de_error(
291 "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n"
292 "Undo: ^U Write: ^W Quit:^D Repeat count: ^R[n]\n"
293 "Top: ^T Locate (search, find): ^L text ^L\n");
294 return( 0 );
295 }
296 }
297 break;
298
299 case WM_CLOSE:
300 DestroyWindow(hwnd_arg);
301 return 0;
302
303 case WM_DESTROY:
304 PostQuitMessage (0);
305 GC_win32_free_heap();
306 return 0;
307
308 case WM_PAINT:
309 dc = BeginPaint(hwnd_arg, &ps);
310 GetClientRect(hwnd_arg, &client_area);
311 COLS = (client_area.right - client_area.left)/char_width;
312 LINES = (client_area.bottom - client_area.top)/char_height;
313 SelectObject(dc, GetStockObject(SYSTEM_FIXED_FONT));
314 for (i = 0; i < LINES; i++) {
315 get_line_rect(i, client_area.right, &this_line);
316 if (IntersectRect(&dummy, &this_line, &ps.rcPaint)) {
317 CORD raw_line = retrieve_screen_line(i);
318 size_t len = CORD_len(raw_line);
319 char * text = CORD_to_char_star(raw_line);
320 /* May contain embedded NULLs */
321 char * plain = plain_chars(text, len);
322 char * blanks = CORD_to_char_star(CORD_chars(' ',
323 COLS - len));
324 char * control = control_chars(text, len);
325 if (NULL == plain || NULL == control)
326 de_error("Out of memory!");
327
328 # define RED RGB(255,0,0)
329
330 SetBkMode(dc, OPAQUE);
331 SetTextColor(dc, GetSysColor(COLOR_WINDOWTEXT));
332
333 if (plain != NULL)
334 TextOutA(dc, this_line.left, this_line.top,
335 plain, (int)len);
336 TextOutA(dc, this_line.left + (int)len * char_width,
337 this_line.top,
338 blanks, (int)(COLS - len));
339 SetBkMode(dc, TRANSPARENT);
340 SetTextColor(dc, RED);
341 if (control != NULL)
342 TextOutA(dc, this_line.left, this_line.top,
343 control, (int)strlen(control));
344 }
345 }
346 EndPaint(hwnd_arg, &ps);
347 screen_was_painted = 1;
348 return 0;
349 }
350 return DefWindowProc(hwnd_arg, message, wParam, lParam);
351 }
352
353 int last_col;
354 int last_line;
355
356 void move_cursor(int c, int l)
357 {
358 last_col = c;
359 last_line = l;
360
361 if (caret_visible) update_cursor();
362 }
363
364 void update_cursor(void)
365 {
366 SetCaretPos(last_col * char_width, last_line * char_height);
367 ShowCaret(hwnd);
368 }
369
370 void invalidate_line(int i)
371 {
372 RECT line_r;
373
374 if (!screen_was_painted) return;
375 /* Invalidating a rectangle before painting seems result in a */
376 /* major performance problem. */
377 get_line_rect(i, COLS*char_width, &line_r);
378 InvalidateRect(hwnd, &line_r, FALSE);
379 }
380
381 #else
382
383 extern int GC_quiet;
384 /* ANSI C doesn't allow translation units to be empty. */
385 /* So we guarantee this one is nonempty. */
386
387 #endif /* !WIN32 */