AmendHub

Download

jcs

/

textview

/

textview.c

 

(View History)

jcs   TVDispose: Don't use TVClear, it redraws Latest amendment: 92 on 2026-11-05

1 /*
2 * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdio.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include "textview.h"
21
22 void TVDisposeSlabs(Handle slabh);
23
24 TVHandle
25 TVNew(Rect *view, Rect *padding)
26 {
27 TVHandle tvh;
28 struct TVRec *tv;
29 struct TVLine *line;
30
31 tvh = (TVHandle)NewHandleClear(sizeof(struct TVRec));
32 if (tvh == NULL)
33 return NULL;
34
35 HLock(tvh);
36 tv = *tvh;
37
38 tv->view = *view;
39 tv->padding = *padding;
40 tv->padding.left++;
41
42 SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
43 tv->view.bottom - tv->view.top);
44
45 tv->linewrap = true;
46 tv->autocalc = true;
47 tv->active = true;
48
49 memset(&tv->tab_pat, 0x88, sizeof(tv->tab_pat));
50
51 #ifdef USE_CANVAS
52 if (!TVSetupCanvas(tv))
53 goto fail;
54 #endif
55 tv->bounds.right = tv->frame.right;
56 tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
57 tv->scrolled = tv->bounds;
58
59 EraseRect(&tv->view);
60
61 if (TVAllocateSlab(tv, TV_STYLE_SLAB_SIZE, &tv->first_style_slab,
62 &tv->last_style_slab) == NULL)
63 goto fail;
64
65 if (TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE, &tv->first_text_slab,
66 &tv->last_text_slab) == NULL)
67 goto fail;
68
69 if (TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE, &tv->first_chunk_slab,
70 &tv->last_chunk_slab) == NULL)
71 goto fail;
72
73 if (TVAllocateLineGroup(tv, 0) == NULL)
74 goto fail;
75
76 HUnlock(tvh);
77 return tvh;
78
79 fail:
80 if (tv->first_chunk_slab)
81 DisposHandle(tv->first_chunk_slab);
82 if (tv->first_text_slab)
83 DisposHandle(tv->first_text_slab);
84 if (tv->first_style_slab)
85 DisposHandle(tv->first_style_slab);
86 if (tv->canvas_baseaddr)
87 DisposHandle(tv->canvas_baseaddr);
88 if (tv->canvas) {
89 ClosePort(tv->canvas);
90 DisposPtr(tv->canvas);
91 }
92 DisposHandle(tvh);
93 return NULL;
94 }
95
96 Boolean
97 TVAppend(TVHandle tvh, struct TVStyle *style, char *text,
98 unsigned long length)
99 {
100 struct TVRec *tv;
101 Boolean ret;
102
103 tv = TVLock(tvh);
104 ret = TVAppendLocked(tv, style, text, length, 0);
105 TVUnlock(tv);
106
107 return ret;
108 }
109
110 Boolean
111 TVAppendLine(TVHandle tvh, struct TVStyle *style, char *text,
112 unsigned long length)
113 {
114 struct TVRec *tv;
115 Boolean ret;
116
117 tv = TVLock(tvh);
118 ret = TVAppendLocked(tv, style, text, length, '\r');
119 TVUnlock(tv);
120
121 return ret;
122 }
123
124 Boolean
125 TVAppendBitMap(TVHandle tvh, BitMap *bitmap, struct TVStyle *bstyle)
126 {
127 struct TVRec *tv;
128 struct TVStyle *style;
129 long bitmap_size, width, height;
130 Boolean ret;
131
132 width = bitmap->bounds.right - bitmap->bounds.left;
133 height = bitmap->bounds.bottom - bitmap->bounds.top;
134 bitmap_size = (long)bitmap->rowBytes * height;
135
136 style = (struct TVStyle *)NewPtrClear(sizeof(struct TVStyle) +
137 bitmap_size);
138 if (style == NULL)
139 return false;
140
141 style->bitmap_data_size = bitmap_size;
142 style->bitmap = *bitmap;
143 memcpy(style->bitmap_data, bitmap->baseAddr, bitmap_size);
144 style->bitmap.baseAddr = (Ptr)style->bitmap_data;
145
146 tv = TVLock(tvh);
147
148 if (!bstyle)
149 bstyle = TVGetStyle(tv, tv->cur_style);
150 if (bstyle) {
151 style->font = bstyle->font;
152 style->style = bstyle->style;
153 style->size = bstyle->size;
154 style->font_info = bstyle->font_info;
155 style->tag = bstyle->tag;
156 }
157
158 ret = TVAppendLocked(tv, style, " ", 1, 0);
159 TVUnlock(tv);
160
161 DisposPtr(style);
162 return ret;
163 }
164
165 Boolean
166 TVCalcLines(TVHandle tvh)
167 {
168 struct TVRec *tv;
169 Boolean ret;
170
171 tv = TVLock(tvh);
172 ret = TVCalcLinesLocked(tv, false);
173 TVUnlock(tv);
174
175 return ret;
176 }
177
178 Boolean
179 TVFindInRect(TVHandle tvh, BigRect *findrect, long startline,
180 TVFindInRectCallback callback, void *cookie)
181 {
182 struct TVRec *tv;
183 Boolean ret;
184
185 tv = TVLock(tvh);
186 ret = TVFindInRectLocked(tv, findrect, startline, callback, cookie);
187 TVUnlock(tv);
188
189 return ret;
190 }
191
192 void
193 TVAutoCalc(TVHandle tvh, Boolean calc)
194 {
195 struct TVRec *tv;
196
197 tv = TVLock(tvh);
198 tv->autocalc = calc;
199 if (calc && tv->pending_redraw) {
200 tv->pending_redraw = false;
201 TVRedrawAppendLocked(tv, tv->pending_from_line,
202 tv->pending_from_end);
203 }
204 TVUnlock(tv);
205 }
206
207 void
208 TVAutoScroll(TVHandle tvh, Boolean scroll)
209 {
210 struct TVRec *tv;
211
212 tv = TVLock(tvh);
213 tv->autoscroll = scroll;
214 TVUnlock(tv);
215 }
216
217 void
218 TVLineWrap(TVHandle tvh, Boolean wrap)
219 {
220 struct TVRec *tv;
221
222 tv = TVLock(tvh);
223 tv->linewrap = wrap;
224 TVUnlock(tv);
225 }
226
227 void
228 TVTabStop(TVHandle tvh, unsigned short tabstop)
229 {
230 struct TVRec *tv;
231
232 tv = TVLock(tvh);
233 tv->tabstop = tabstop;
234 TVUnlock(tv);
235 }
236
237 void
238 TVSetScrollbar(TVHandle tvh, ControlHandle scroller)
239 {
240 struct TVRec *tv;
241
242 tv = TVLock(tvh);
243 tv->scroller = scroller;
244 TVUnlock(tv);
245 }
246
247 unsigned long
248 TVGetText(TVHandle tvh, char *dst, unsigned long start, unsigned long end)
249 {
250 struct TVRec *tv;
251 struct TVSlab *slab;
252 unsigned long slabstart, slabend, read, size;
253
254 tv = TVLock(tvh);
255
256 if (end > tv->text_length)
257 end = tv->text_length;
258 read = 0;
259 while (start + read < end) {
260 slab = TVGetTextSlab(tv, start + read);
261 if (slab == NULL) {
262 read = 0;
263 break;
264 }
265 slabstart = slab->base;
266 slabend = slab->base + slab->size;
267
268 if (start > slabstart)
269 slabstart = start;
270 if (end < slabend)
271 slabend = end;
272
273 size = slabend - slabstart;
274 if (size == 0)
275 Debugger();
276 memcpy(dst, slab->data + (slabstart - slab->base), size);
277
278 dst += size;
279 read += size;
280 }
281
282 TVUnlock(tv);
283 return read;
284 }
285
286 void
287 TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, Rect *rect)
288 {
289 struct TVRec *tv;
290
291 tv = TVLock(tvh);
292
293 rect->left = bigrect->left + tv->scrolled.left + tv->view.left;
294 rect->top = bigrect->top + tv->scrolled.top + tv->view.top;
295 rect->right = bigrect->right + tv->scrolled.left + tv->view.left;
296 rect->bottom = bigrect->bottom + tv->scrolled.top + tv->view.top;
297
298 TVUnlock(tv);
299 }
300
301 void
302 TVClear(TVHandle tvh)
303 {
304 Handle slabh;
305 struct TVRec *tv;
306 struct TVChunk *tchunk, *cur_chunk;
307 struct TVSlab *slab;
308 short n;
309
310 tv = TVLock(tvh);
311
312 /* free style slabs except first one */
313 while (tv->last_style_slab != tv->first_style_slab) {
314 slabh = tv->last_style_slab;
315 HLock(slabh);
316 slab = (struct TVSlab *)*slabh;
317 tv->last_style_slab = slab->prev_slab;
318 DisposeHandle(slabh);
319 }
320
321 HLock(tv->first_style_slab);
322 slab = (struct TVSlab *)*(tv->first_style_slab);
323 memset(slab->data, 0, slab->size);
324 slab->pos = 0;
325 slab->next_slab = NULL;
326 HUnlock(tv->first_style_slab);
327
328 tv->cur_style = 0;
329
330 /* free chunk slabs except first one */
331 while (tv->last_chunk_slab != tv->first_chunk_slab) {
332 slabh = tv->last_chunk_slab;
333 HLock(slabh);
334 slab = (struct TVSlab *)*slabh;
335 tv->last_chunk_slab = slab->prev_slab;
336 DisposeHandle(slabh);
337 }
338
339 HLock(tv->first_chunk_slab);
340 slab = (struct TVSlab *)*(tv->first_chunk_slab);
341 memset(slab->data, 0, slab->size);
342 slab->pos = 0;
343 slab->next_slab = NULL;
344 HUnlock(tv->first_chunk_slab);
345
346 tv->first_chunk = 0;
347 tv->last_chunk = 0;
348
349 /* free text slabs except first one */
350 while (tv->last_text_slab != tv->first_text_slab) {
351 slabh = tv->last_text_slab;
352 HLock(slabh);
353 slab = (struct TVSlab *)*slabh;
354 tv->last_text_slab = slab->prev_slab;
355 DisposeHandle(slabh);
356 }
357
358 HLock(tv->first_text_slab);
359 slab = (struct TVSlab *)*(tv->first_text_slab);
360 memset(slab->data, 0, slab->size);
361 slab->pos = 0;
362 slab->next_slab = NULL;
363 HUnlock(tv->first_text_slab);
364
365 tv->text_slab_hint = NULL;
366 tv->style_slab_hint = NULL;
367 tv->chunk_slab_hint = NULL;
368 tv->text_length = 0;
369 tv->nlines = 0;
370 tv->last_space = 0;
371 tv->space_width = 0;
372 tv->sel_start = 0;
373 tv->sel_end = 0;
374 tv->click_count = 0;
375 tv->pending_redraw = false;
376
377 /* clear line groups except first one */
378 for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
379 n++) {
380 if (tv->line_groups[n] == NULL)
381 continue;
382 DisposeHandle(tv->line_groups[n]);
383 tv->line_groups[n] = NULL;
384 }
385 HLock(tv->line_groups[0]);
386 memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE);
387 HUnlock(tv->line_groups[0]);
388
389 tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
390 tv->scrolled = tv->bounds;
391
392 TVUseCanvas(tv);
393 EraseRect(&tv->frame);
394 tv->canvas_dirty = true;
395 TVRevealCanvas(tv);
396 TVUnlock(tv);
397 }
398
399 void
400 TVDisposeSlabs(Handle slabh)
401 {
402 Handle nexth;
403
404 while (slabh != NULL) {
405 nexth = ((struct TVSlab *)*slabh)->next_slab;
406 DisposeHandle(slabh);
407 slabh = nexth;
408 }
409 }
410
411 void
412 TVDispose(TVHandle tvh)
413 {
414 struct TVRec *tv;
415 short n;
416
417 /* do no drawing in here */
418
419 HLock(tvh);
420 tv = *tvh;
421
422 TVDisposeSlabs(tv->first_style_slab);
423 TVDisposeSlabs(tv->first_chunk_slab);
424 TVDisposeSlabs(tv->first_text_slab);
425
426 for (n = 0; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
427 n++) {
428 if (tv->line_groups[n] != NULL)
429 DisposeHandle(tv->line_groups[n]);
430 }
431
432 #ifdef USE_CANVAS
433 ClosePort(tv->canvas);
434 DisposPtr(tv->canvas);
435 DisposeHandle(tv->canvas_baseaddr);
436 #endif
437
438 DisposeHandle(tvh);
439 }
440
441 /*
442 * internal functions
443 * anything taking a TVRec assumes everything is locked for dereferencing
444 */
445
446 struct TVRec *
447 TVLock(TVHandle tvh)
448 {
449 struct TVRec *tv;
450
451 HLock(tvh);
452 tv = *tvh;
453 tv->unlock_refcnt++;
454
455 return tv;
456 }
457
458 void
459 TVUnlock(struct TVRec *tv)
460 {
461 Handle slabh, tslabh;
462 struct TVSlab *slab;
463 short n;
464
465 if (--tv->unlock_refcnt != 0) {
466 if (tv->unlock_refcnt < 0)
467 Debugger();
468 return;
469 }
470
471 slabh = tv->first_style_slab;
472 while (slabh) {
473 slab = (struct TVSlab *)*slabh;
474 tslabh = slabh;
475 slabh = slab->next_slab;
476 HUnlock(tslabh);
477 }
478
479 slabh = tv->first_text_slab;
480 while (slabh) {
481 HLock(slabh);
482 slab = (struct TVSlab *)*slabh;
483 tslabh = slabh;
484 slabh = slab->next_slab;
485 HUnlock(tslabh);
486 }
487
488 slabh = tv->first_chunk_slab;
489 while (slabh) {
490 HLock(slabh);
491 slab = (struct TVSlab *)*slabh;
492 tslabh = slabh;
493 slabh = slab->next_slab;
494 HUnlock(tslabh);
495 }
496
497 for (n = 0; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]); n++) {
498 if (tv->line_groups[n])
499 HUnlock(tv->line_groups[n]);
500 }
501
502 HUnlock(RecoverHandle(tv));
503 }
504
505 Boolean
506 TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text,
507 unsigned long length, char terminator)
508 {
509 struct TVSlab *text_slab;
510 struct TVChunk *chunk, *prev_chunk;
511 struct TVStyle *cur_style;
512 struct TVLine *fromline;
513 Handle text_slabh;
514 unsigned long avail, fromnline, fromend;
515 TVSlabLocator prev_style, prev_chunk_loc;
516 char *text_slab_data;
517 Boolean new_chunk;
518
519 if (terminator)
520 length++;
521
522 if (length == 0)
523 return false;
524
525 new_chunk = false;
526 fromnline = tv->nlines;
527 fromline = TVGetLine(tv, fromnline);
528 fromend = fromline ? fromline->end : 0;
529
530 /* setup style */
531 prev_style = tv->cur_style;
532 cur_style = TVFindStyle(tv, style);
533 if (cur_style == NULL)
534 return false;
535 tv->cur_style = cur_style->loc;
536 if (tv->cur_style != prev_style)
537 new_chunk = true;
538
539 /* setup text slab */
540 HLock(tv->last_text_slab);
541 text_slab = (struct TVSlab *)*(tv->last_text_slab);
542
543 /* setup chunk */
544 prev_chunk = NULL;
545 chunk = TVGetChunk(tv, tv->last_chunk);
546
547 while (length) {
548 avail = text_slab->size - text_slab->pos;
549 if (avail == 0) {
550 /* new text slab, new chunk */
551 if ((text_slabh = TVAllocateSlab(tv, TV_TEXT_SLAB_SIZE,
552 &tv->first_text_slab, &tv->last_text_slab)) == NULL)
553 return false;
554
555 text_slab = (struct TVSlab *)*text_slabh;
556 avail = text_slab->size;
557 new_chunk = true;
558 }
559
560 if (chunk == NULL || new_chunk) {
561 new_chunk = false;
562 prev_chunk_loc = chunk ? chunk->loc : 0;
563 chunk = TVCreateChunk(tv);
564 if (chunk == NULL)
565 return false;
566 prev_chunk = TVGetChunk(tv, prev_chunk_loc);
567
568 chunk->start = text_slab->base + text_slab->pos;
569 chunk->end = chunk->start;
570 chunk->style = tv->cur_style;
571 if (prev_chunk) {
572 chunk->prev_chunk = prev_chunk->loc;
573 prev_chunk->next_chunk = chunk->loc;
574 }
575 }
576
577 text_slab_data = text_slab->data + text_slab->pos;
578
579 if (length < avail)
580 avail = length;
581 if (terminator && avail == length) {
582 memcpy(text_slab_data, text, avail - 1);
583 text_slab_data[avail - 1] = terminator;
584 } else
585 memcpy(text_slab_data, text, avail);
586 text += avail;
587 text_slab->pos += avail;
588 length -= avail;
589 tv->text_length += avail;
590 chunk->end += avail;
591 chunk->need_recalc = true;
592 }
593
594 if (!tv->autocalc) {
595 if (!tv->pending_redraw) {
596 tv->pending_redraw = true;
597 tv->pending_from_line = fromnline;
598 tv->pending_from_end = fromend;
599 }
600 return true;
601 }
602
603 if (tv->pending_redraw) {
604 tv->pending_redraw = false;
605 fromnline = tv->pending_from_line;
606 fromend = tv->pending_from_end;
607 }
608
609 return TVRedrawAppendLocked(tv, fromnline, fromend);
610 }
611
612 Boolean
613 TVRedrawAppendLocked(struct TVRec *tv, unsigned long fromnline,
614 unsigned long fromend)
615 {
616 struct TVLine *fromline, *toline;
617 RgnHandle vacated;
618 BigRect redraw;
619 Rect r;
620 unsigned long wastop;
621
622 if (!TVCalcLinesLocked(tv, false))
623 return false;
624
625 fromline = TVGetLine(tv, fromnline);
626
627 /* keep scrolling and redrawing in one CopyBits */
628 TVUseCanvas(tv);
629
630 if (tv->autoscroll && tv->scrolled.bottom > tv->frame.bottom) {
631 wastop = tv->scrolled.top;
632 tv->scrolled.bottom = tv->frame.bottom;
633 tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
634 vacated = NewRgn();
635 ScrollRect(&tv->frame, 0, tv->scrolled.top - wastop, vacated);
636 DisposeRgn(vacated);
637 tv->canvas_dirty = true;
638 }
639
640 if (fromline && fromline->nline == tv->nlines)
641 toline = fromline;
642 else
643 toline = TVGetLine(tv, tv->nlines);
644
645 redraw.top = fromline ? fromline->rect.top : tv->padding.top;
646 redraw.bottom = toline->rect.bottom;
647 redraw.left = 0;
648 redraw.right = tv->frame.right;
649
650 if (tv->scrolled.top + redraw.bottom > 0L &&
651 tv->scrolled.top + redraw.top < tv->frame.bottom) {
652 r.left = redraw.left + tv->scrolled.left;
653 r.right = redraw.right + tv->scrolled.left;
654 if (tv->scrolled.top + redraw.top < 0L)
655 r.top = 0;
656 else
657 r.top = tv->scrolled.top + redraw.top;
658 if (tv->scrolled.top + redraw.bottom > tv->frame.bottom)
659 r.bottom = tv->frame.bottom;
660 else
661 r.bottom = tv->scrolled.top + redraw.bottom;
662
663 TVRedrawRect(tv, &r, fromline ? fromline->nline : 0,
664 (fromline != NULL && fromline->end < fromend) ||
665 (tv->active && tv->sel_start != tv->sel_end &&
666 fromline != NULL && tv->sel_end > fromline->start));
667 }
668
669 TVRevealCanvas(tv);
670 return true;
671 }