Debian Codegraph @

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