-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKPCTEXT.cpp
431 lines (393 loc) · 13.1 KB
/
PKPCTEXT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "PKIN.H"
#include "PKINDLOG.h"
static long laststrlen;
/****getmactextselect()******************************************************/
void getmactextselect(void) {;}
/*___getmactextselect()_____________________________________________________*/
/****pkintextreplace()*******************************************************/
void pkintextreplace(char textstr[256])
{
if (!Lquiet) /* only do this when run in GUI mode */
{
inserttexts(textstr,1); /* 1 tells inserttexts() to replace text */
RedrawWindow(PrekinWindow,NULL,NULL,RDW_ERASE|RDW_UPDATENOW);
}
}
/*___pkintextreplace()______________________________________________________*/
/****pkintextinsert()********************************************************/
void pkintextinsert(char textstr[256])
{
if (!Lquiet) /* only do this when run in GUI mode */
{
inserttexts(textstr,0); /* 0 tells inserttexts() to add to text */
InvalidateRect(PrekinWindow,NULL,TRUE);
}
}
/*___pkintextinsert()_______________________________________________________*/
/****inserttexts()***********************************************************/
void inserttexts(char textstr[256], int replace) /*<-GetTextFromFile(),MAGETEXT*/
{
LPSTR textbuf;
int j;
long textend,maxtextchar;
if (Lquiet == 0)
{
textbuf = (LPSTR)GetWindowLong(PrekinWindow,TEXTHANDLE);
maxtextchar = GetWindowLong(PrekinWindow,TEXTLENGTH);
if(!Limittext)
{
textend = maxtextchar;
/* maxtextchar needs to be adjusted depending on if we */
/* are inserting or replacing the text on the screen */
if (replace)
maxtextchar = maxtextchar - laststrlen + (long)strlen(textstr);
else maxtextchar = maxtextchar + (long)strlen(textstr);
if (maxtextchar >= 32767L)
{
Limittext = 1;
sprintf(alertstr,"Text about to exceed 32767 characters,\n"
"no more text will be put into Text Window!\n");
DoReportDialog();
}
else
{
/* again, if replacing text adjust the ending */
if (replace) textend = textend - laststrlen;
laststrlen = (long)strlen(textstr);/*update*/
for (j=0; textstr[j] != '\0'; j++)
{
textbuf[textend] = textstr[j];
textend++;
}
textbuf[textend] = '\0';
}
maxtextchar = textend;
SetWindowLong(PrekinWindow,TEXTLENGTH,maxtextchar); /*update actual length*/
}
}
}
/*___inserttexts()_________________________________________________________*/
/****adjusttext()************************************************************/
void adjusttext(int iforce) /*see PKMCTEXT.C*/
/*must call this with iforce==0 before iforce==1 to get calc right*/
{
SCROLLINFO si;
if (Lquiet == 0)
{
makelinestarts((HWND)PrekinWindow);
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nPage = linesonascreen(PrekinWindow);
si.nMin = 0;
si.nMax = GetWindowLong(PrekinWindow,TEXTLINESTOTAL)-1;
if ((unsigned)si.nMax <= si.nPage) si.nPos = 0;
else si.nPos = si.nMax-si.nPage+1;
SetWindowLong(PrekinWindow,TEXTLINECURRENT,si.nPos);
SetScrollInfo(PrekinWindow, SB_VERT, &si, TRUE);
}
}
/*___adjusttext()___________________________________________________________*/
/****SetTextCaption()********************************************************/
/* Set the caption of a "PrekinTextWindow" */
void SetTextCaption(HWND hWnd, LPSTR text)
{
if (Lquiet == 0)
{
SendMessage(hWnd,WM_SETTEXT,0,(DWORD)text);
}
}
/*___SetTextCaption()_______________________________________________________*/
/****SystemFontSize()*******************************************************/
void SystemFontSize(void)
{
/* find the system font size */
HFONT hFont = (HFONT)GetStockObject(SYSTEM_FONT);
HFONT oldFont;
TEXTMETRIC tm;
HDC hDC;
hDC = GetDC(PrekinWindow);
oldFont = (HFONT)SelectObject(hDC,hFont);
GetTextMetrics(hDC,&tm);
SelectObject(hDC,oldFont);
ReleaseDC(PrekinWindow,hDC);
fontheight = tm.tmHeight;
fontwidth = tm.tmAveCharWidth;
fontmaxwidth = tm.tmMaxCharWidth;
fontverticalspace = tm.tmHeight + tm.tmExternalLeading;
}
/*___SystemFontSize()______________________________________________________*/
/****totalbufferlines()******************************************************/
/* count lines in the buffer for the current window size */
long totalbufferlines(HWND hWnd, HDC hDC, LPSTR s)
{
LPSTR s0;
LPSTR e;
int nlines = 0;
int nb;
int i;
RECT r;
int windowwidth;
e = s + GetWindowLong(hWnd,TEXTLENGTH);
GetClientRect(hWnd,&r);
windowwidth = (r.right - r.left);
if (s)
{
while (*s && s < e)
{
nb = nlinebreaks(s,&s0);
if (nb == 1) s = s0;
if (s >= e || *s == '\0') break;
i = sizeoftextline(hDC, windowwidth, s);
/* putnchars(s,i); */
s += i;
while (*s == ' ') ++s;
++nlines;
}
}
return(nlines);
}
/*___totalbufferlines()_____________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****linefromoffset()********************************************************/
/* For a given offset in the text buffer, what line is it on? */
/* This is used so that the top of the window is roughly the */
/* same after a resize. */
long linefromoffset(HWND hWnd, unsigned long *lsbuf, long offset)
{
long nlines = GetWindowLong(hWnd,TEXTLINESTOTAL);
long i;
unsigned long end;
for (i = 0; i < nlines; ++i)
{
end = lsbuf[i+1];
if (end > (unsigned)offset) return(i);
}
return(0);
}
/*___linefromoffset()_______________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****makelinestarts()********************************************************/
/* Build the linestart array, freeing and allocating memory as necessary. */
/* Unlock the result and store the handle in text window extra bytes. */
long makelinestarts(HWND hWnd)
{
LPSTR s00,s0,e;
int nlines = 0;
int nb,i,windowwidth;
RECT r;
HDC hDC = GetDC(hWnd);
LPSTR s = (LPSTR)GetWindowLong(hWnd,TEXTHANDLE);
long* lsbuf = (long*)GetWindowLong(hWnd,TEXTLINESTARTHANDLE);
long topline = GetWindowLong(hWnd,TEXTLINECURRENT);
unsigned long currentoffset = 0;
long nlinestotal;
nlinestotal = totalbufferlines(hWnd,hDC,s)+1;
GetClientRect(hWnd,&r);
windowwidth = (r.right - r.left);
if (lsbuf != 0)
{/* there is an existing buffer; kill it */
currentoffset = lsbuf[topline];
free(lsbuf);
SetWindowLong(hWnd,TEXTLINESTARTHANDLE,0);
}
SetWindowLong(hWnd,TEXTLINESTOTAL,nlinestotal);
/* allocate the new array */
lsbuf = (long *)calloc(nlinestotal+1,sizeof(nlinestotal));
SetWindowLong(hWnd,TEXTLINESTARTHANDLE,(long)lsbuf);
/* fill the new array */
s00 = s;
e = s + GetWindowLong(hWnd,TEXTLENGTH);
if (s)
{
while (*s && s < e)
{
nb = nlinebreaks(s,&s0);
if (nb == 1) s = s0;
if (s >= e || *s == '\0') break;
i = sizeoftextline(hDC, windowwidth, s);
if (nlines > nlinestotal)
{
fprintf(stderr,"Nlines exceeded 2\n");
exit(10);
}
lsbuf[nlines] = s-s00;
s += i;
while (*s == ' ') ++s;
++nlines;
}
}
if (nlines > nlinestotal)
{
fprintf(stderr,"Nlines exceeded 3\n");
exit(10);
}
lsbuf[nlines] = s-s00;
/* make sure we have the same top line as before */
topline = linefromoffset(hWnd,(unsigned long *)lsbuf,currentoffset);
SetWindowLong(hWnd,TEXTLINECURRENT,topline);
ReleaseDC(hWnd,hDC);
return(nlines);
}
/*___makelinestarts()_______________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****linesonascreen()********************************************************/
int linesonascreen(HWND hWnd)
{
RECT r;
int n=0;
if (Lquiet == 0)
{
GetClientRect(hWnd,&r);
if (fontverticalspace <= 0) return(0);
n = (r.bottom - r.top) / fontverticalspace;
}
return(n);
}
/*___linesonascreen()_______________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****resetscrollbarvalues()**************************************************/
void resetscrollbarvalues(HWND hWnd)
{
SCROLLINFO si;
if (Lquiet == 0)
{
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nPage = linesonascreen(hWnd);
si.nMin = 0;
si.nMax = GetWindowLong(hWnd,TEXTLINESTOTAL)-1;
if ((unsigned)si.nMax <= si.nPage-1)
{
si.nPos = 0;
SetWindowLong(hWnd,TEXTLINECURRENT,si.nPos);
}
else if ((((unsigned)si.nMax)-
(unsigned int)GetWindowLong(hWnd,TEXTLINECURRENT)) < si.nPage)
{
si.nPos = si.nMax-si.nPage+1;
SetWindowLong(hWnd,TEXTLINECURRENT,si.nPos);
}
else si.nPos = GetWindowLong(hWnd,TEXTLINECURRENT);
SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
}
}
/*___resetscrollbarvalues()_________________________________________________*/
/****RePaint()***************************************************************/
/* repaint the text window */
/* this is intended to turn into the response to a WM_PAINT message */
int RePaint(HWND hWnd, HDC hDC)
{
if (Lquiet == 0)
{
long topline = GetWindowLong(hWnd,TEXTLINECURRENT);
long totallines = GetWindowLong(hWnd,TEXTLINESTOTAL);
LPSTR textbuf = (LPSTR)GetWindowLong(hWnd,TEXTHANDLE);
long* lsbuf = (long*)GetWindowLong(hWnd,TEXTLINESTARTHANDLE);
int i,nchars;
char *textstart;
long start,end;
int screenline = 0;
int linesperscreen = linesonascreen(hWnd);
for (i = topline; i < totallines && screenline < linesperscreen; ++i)
{
start = lsbuf[i];
if (i == totallines) end = GetWindowLong(hWnd,TEXTLENGTH);
else end = lsbuf[i+1];
nchars = end-start;
textstart = textbuf+start;
if (nchars > 0 && (textstart[nchars-1] == EOL)) nchars--;
if (nchars > 0) TabbedTextOut(hDC,0,screenline*fontverticalspace,
(LPSTR)textstart,nchars,0,NULL,0);
++screenline;
}
}
return(0);
}
/*___RePaint()______________________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****nlinebreaks()************************************************************/
/* count empty lines (if any); record character following last line break */
int nlinebreaks(LPSTR s, LPSTR *t)
{
int breaks = 0;
LPSTR s0 = s;
if (*s == EOL)
{
breaks = 1;
s0 = s + 1;
}
*t = s0;
return(breaks);
}
/*___nlinebreaks()__________________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****sizeoftextline()********************************************************/
/* size in characters of longest well-broken text line that */
/* can fit within windowwidth */
int sizeoftextline(HDC hDC, int windowwidth, char *p)
{
char *t = p;
char *t0 = p;
DWORD extent;
for (;;)
{
while (!isspace(*t) && *t != '\0') ++t;
extent = GetTabbedTextExtent(hDC,p,(t-p),0,NULL);
if (LOWORD(extent) > windowwidth)
{
if (t0 == p)
{/* panic: can't fit even one word on line */
for ( ; t0 < t; ++t0)
if(LOWORD(GetTabbedTextExtent(hDC,p,t0-p,0,NULL))>windowwidth)
return((t0-p)-1);
}
else return(t0-p);
}
else
{
t0 = t; /* set last good break point to current point */
if (*t == '\0' || *t == EOL) return(t-p);
++t;
}
}
}
/*___sizeoftextline()_______________________________________________________*/
/****cleartexts()************************************************************/
void cleartexts()
{
HANDLE buf;
if (Lquiet == 0)
{
/*setup window text buffer*/
SetWindowLong(PrekinWindow,TEXTLINECURRENT,0);
/* give up previous buffer, if any */
buf = (HANDLE)GetWindowLong(PrekinWindow,TEXTHANDLE);
if (buf != 0) free(buf);
/* allocate new buffer */
buf = calloc(32768,sizeof(char));
SetWindowLong(PrekinWindow,TEXTHANDLE,(long)buf);
SetWindowLong(PrekinWindow,TEXTLENGTH,0);
if (buf == 0)
{
sprintf(alertstr,"Out of memory in cleartexts");
DoReportDialog();
}
}
}
/*___cleartexts()___________________________________________________________*/
/****completetext()***920614 dcr*********************************************/
void completetext() /*<-GetTextFromFile(),MAGETEXT*/
{
if (Lquiet == 0)
{
makelinestarts((HWND)PrekinWindow);
resetscrollbarvalues(PrekinWindow);
}
}
/*___completetext()___920614 dcr____________________________________________*/
/****cleanuptextwindow()*****************************************************/
void cleanuptextwindow()
{
;
}
/*___cleanuptextwindow()____________________________________________________*/