AmendHub

Download

jcs

/

textview

/

tv_layout.c

 

(View History)

jcs   TVOffsetFor*: Add caret flag, add TVScrollToOffset Latest amendment: 94 on 2026-11-11

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 Boolean
23 TVResize(TVHandle tvh)
24 {
25 struct TVRec *tv;
26 long n;
27 struct TVLine *line;
28 struct TVChunk *chunk;
29 unsigned long top_start;
30
31 tv = TVLock(tvh);
32
33 SetRect(&tv->frame, 0, 0, tv->view.right - tv->view.left,
34 tv->view.bottom - tv->view.top);
35
36 TVUseCanvas(tv);
37 EraseRect(&tv->frame);
38 TVRevealCanvas(tv);
39
40 /* find text position of first visible line to try to stay there */
41 top_start = 0;
42 line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top)));
43 if (line != NULL)
44 top_start = line->start;
45
46 #ifdef USE_CANVAS
47 if (!TVSetupCanvas(tv)) {
48 TVUnlock(tv);
49 return false;
50 }
51 #endif
52 tv->bounds.right = tv->frame.right;
53 tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
54 tv->scrolled = tv->bounds;
55
56 /* clear line groups except first one */
57 for (n = 1; n < sizeof(tv->line_groups) / sizeof(tv->line_groups[0]);
58 n++) {
59 if (tv->line_groups[n] == NULL)
60 continue;
61 DisposHandle(tv->line_groups[n]);
62 tv->line_groups[n] = NULL;
63 }
64 HLock(tv->line_groups[0]);
65 memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE);
66 HUnlock(tv->line_groups[0]);
67
68 tv->nlines = 0;
69 tv->last_space = 0;
70 tv->space_width = 0;
71 tv->pending_redraw = false;
72
73 for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
74 chunk = TVGetChunk(tv, chunk->next_chunk)) {
75 chunk->start_line = 0;
76 chunk->end_line = 0;
77 chunk->start_left = 0;
78 chunk->end_right = 0;
79 chunk->need_recalc = true;
80 }
81
82 if (!TVCalcLinesLocked(tv, true)) {
83 /* XXX: if this fails, what are we supposed to do? */
84 TVUnlock(tv);
85 return false;
86 }
87
88 if (tv->bounds.bottom > tv->frame.bottom) {
89 if (tv->autoscroll) {
90 tv->scrolled.bottom = tv->frame.bottom;
91 tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
92 } else {
93 /* keep the line now holding that text at the top */
94 line = TVGetLine(tv, TVLineForOffset(tv, top_start));
95 if (line != NULL && line->start <= top_start &&
96 top_start < line->end) {
97 tv->scrolled.top = -(line->rect.top);
98 tv->scrolled.bottom = tv->scrolled.top +
99 tv->bounds.bottom;
100 }
101
102 if (tv->scrolled.bottom < tv->frame.bottom) {
103 tv->scrolled.bottom = tv->frame.bottom;
104 tv->scrolled.top = tv->scrolled.bottom -
105 tv->bounds.bottom;
106 }
107 }
108 }
109
110 TVRedrawRect(tv, &tv->frame, 0, true);
111 TVUnlock(tv);
112
113 return true;
114 }
115
116 long
117 TVGetTextOffset(TVHandle tvh, Point p)
118 {
119 struct TVRec *tv;
120 long off;
121
122 tv = TVLock(tvh);
123 off = TVOffsetForPoint(tv,
124 (long)p.h - tv->scrolled.left - tv->view.left,
125 (long)p.v - tv->scrolled.top - tv->view.top, false);
126 TVUnlock(tv);
127 return off;
128 }
129
130 Boolean
131 TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty)
132 {
133 struct TVSlab *text_slab;
134 struct TVChunk *chunk, *last_chunk, *prev_chunk;
135 struct TVStyle *style;
136 struct TVLine *line, *prev_line, *next_line;
137 unsigned long start, nline;
138 long n;
139 short width, height, max_right;
140 char *text_slab_data;
141 register unsigned char *p, c;
142 Boolean new_line;
143
144 if (tv->first_chunk == 0) {
145 tv->nlines = 0;
146 return true;
147 }
148
149 if (ignore_dirty)
150 chunk = TVGetChunk(tv, tv->first_chunk);
151 else {
152 /* find first dirty chunk but search from the end */
153 last_chunk = TVGetChunk(tv, tv->last_chunk);
154 chunk = last_chunk;
155 while (chunk != NULL) {
156 prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
157 if (!prev_chunk || !prev_chunk->need_recalc)
158 break;
159 chunk = prev_chunk;
160 }
161 }
162
163 max_right = tv->view.right - tv->view.left - tv->padding.left -
164 tv->padding.right;
165
166 for (; chunk != NULL; chunk = TVGetChunk(tv, chunk->next_chunk)) {
167 new_line = false;
168
169 reload_chunk:
170 style = TVGetStyle(tv, chunk->style);
171 if (style == NULL)
172 return false;
173 TVApplyStyle(style);
174
175 if (chunk->end_line == 0) {
176 /* working with a new text chunk */
177 if (chunk->prev_chunk == 0) {
178 prev_chunk = NULL;
179 chunk->start_left = tv->padding.left;
180 chunk->start_line = 1;
181 chunk->end_right = chunk->start_left;
182 chunk->end_line = 1;
183 } else {
184 prev_chunk = TVGetChunk(tv, chunk->prev_chunk);
185 if (prev_chunk == NULL)
186 return false;
187 chunk->start_left = prev_chunk->end_right;
188 chunk->start_line = prev_chunk->end_line;
189 chunk->end_right = chunk->start_left;
190 chunk->end_line = chunk->start_line;
191 }
192 }
193
194 /* setup text slab */
195 text_slab = TVGetTextSlab(tv, chunk->start);
196 if (text_slab == NULL)
197 return false;
198 text_slab_data = text_slab->data;
199
200 /* find our current line */
201 nline = chunk->end_line;
202 line = TVGetLine(tv, nline);
203 if (line == NULL)
204 return false;
205 if (nline == 1) {
206 line->rect.left = tv->padding.left;
207 line->rect.top = tv->padding.top;
208 line->start = 0;
209 line->chunk = tv->first_chunk;
210 start = line->end;
211 } else {
212 prev_line = TVGetLine(tv, nline - 1);
213 if (prev_line == NULL)
214 return false;
215 line->rect.top = prev_line->rect.bottom;
216 if (line->end)
217 start = line->end;
218 else
219 start = prev_line->end;
220 }
221
222 line->nline = nline;
223 height = style->font_info.ascent + style->font_info.descent +
224 style->font_info.leading;
225
226 /*
227 * Walk each character and find its width, possibly wrapping and
228 * starting a new line.
229 */
230 p = (unsigned char *)text_slab_data + (start - text_slab->base);
231 for (n = start; n < chunk->end; n++, p++) {
232 c = *p;
233
234 if (c == '\r') {
235 new_line = true;
236 width = 0;
237 } else {
238 if (c == '\t' || !(width = style->width_cache[c]))
239 width = TVCharWidth(tv, style, line, chunk->end_right,
240 c);
241
242 if (c == ' ' && !style->bitmap_data_size) {
243 tv->last_space = n;
244 tv->space_width = 0;
245 } else if (!new_line && tv->linewrap &&
246 chunk->end_right + width > max_right + tv->padding.left) {
247 new_line = true;
248 if (tv->last_space) {
249 /* break after the last space */
250 n = tv->last_space + 1;
251 chunk->end_right -= tv->space_width;
252 line->end = n;
253 line->rect.right = chunk->end_right;
254
255 if (n >= chunk->start) {
256 n = tv->last_space;
257 p = (unsigned char *)text_slab_data +
258 (n - text_slab->base);
259 continue;
260 }
261
262 while (n < chunk->start) {
263 if (!chunk->prev_chunk) {
264 /* how did we get here? */
265 Debugger();
266 return false;
267 }
268 chunk->end_line = 0;
269 chunk = TVGetChunk(tv, chunk->prev_chunk);
270 if (chunk == NULL)
271 return false;
272 }
273
274 chunk->end_right = line->rect.right;
275
276 /* setup chunk with new_line set */
277 goto reload_chunk;
278 }
279 /* otherwise, hard-break this long word */
280 } else
281 tv->space_width += width;
282 }
283
284 if (line->start == line->end) {
285 /* start of new line disregards previous line's style */
286 line->baseline = style->font_info.descent;
287 line->leading = style->font_info.leading;
288 line->rect.bottom = line->rect.top + height;
289 }
290
291 if (new_line) {
292 new_line = false;
293 tv->space_width = 0;
294 tv->last_space = 0;
295
296 line->end = n;
297 line->rect.right = chunk->end_right;
298
299 if (style->align == TV_ALIGN_CENTER)
300 line->indent = (max_right - (line->rect.right -
301 line->rect.left)) / 2;
302 else if (style->align == TV_ALIGN_RIGHT)
303 line->indent = max_right - (line->rect.right -
304 line->rect.left);
305 else
306 line->indent = 0;
307
308 if (n == chunk->start) {
309 /* no text on previous line, start on next line */
310 chunk->start_left = tv->padding.left;
311 chunk->end_right = chunk->start_left;
312 chunk->start_line++;
313 } else if (height > line->rect.bottom - line->rect.top)
314 line->rect.bottom = line->rect.top + height;
315
316 nline++;
317 chunk->end_line = nline;
318 chunk->end_right = tv->padding.left;
319 next_line = TVGetLine(tv, nline);
320 if (next_line == NULL)
321 return false;
322 next_line->nline = nline;
323 next_line->start = n;
324 if (c == '\r')
325 next_line->start++;
326 next_line->chunk = chunk->loc;
327 next_line->leading = style->font_info.leading;
328 next_line->rect.top = line->rect.bottom;
329 next_line->rect.left = tv->padding.left;
330 next_line->rect.right = chunk->end_right;
331 next_line->rect.bottom = next_line->rect.top;
332 if (n + 1 < chunk->end)
333 next_line->rect.bottom += height;
334
335 line = next_line;
336 }
337
338 chunk->end_right += width;
339
340 if (line->start != line->end) {
341 /* if style changes mid-line, previous chunks may grow */
342 if (style->font_info.descent > line->baseline) {
343 if (line->baseline)
344 line->grew = true;
345 line->baseline = style->font_info.descent;
346 }
347 if (style->font_info.leading > line->leading) {
348 if (line->leading)
349 line->grew = true;
350 line->leading = style->font_info.leading;
351 }
352 if (line->rect.bottom == 0 ||
353 (line->rect.bottom - line->rect.top < height)) {
354 if (line->rect.bottom != line->rect.top &&
355 line->rect.bottom != 0)
356 line->grew = true;
357 line->rect.bottom = line->rect.top + height;
358 }
359 }
360 }
361
362 line->end = chunk->end;
363 line->rect.right = chunk->end_right;
364 chunk->end_line = nline;
365 chunk->need_recalc = false;
366
367 if (style->align == TV_ALIGN_CENTER)
368 line->indent = (max_right - (line->rect.right -
369 line->rect.left)) / 2;
370 else if (style->align == TV_ALIGN_RIGHT)
371 line->indent = max_right - (line->rect.right -
372 line->rect.left);
373 else
374 line->indent = 0;
375 }
376
377 tv->nlines = nline;
378
379 line = TVGetLine(tv, nline);
380 if (line == NULL)
381 return false;
382
383 if (line->rect.bottom > tv->bounds.bottom - tv->padding.bottom) {
384 tv->bounds.bottom = line->rect.bottom + tv->padding.bottom;
385 tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
386 }
387
388 return true;
389 }
390
391 Boolean
392 TVFindInRectLocked(struct TVRec *tv, BigRect *rect, long startline,
393 TVFindInRectCallback callback, void *cookie)
394 {
395 struct TVFindInRectMatch match;
396 unsigned long nline;
397 Boolean ret;
398
399 TVUseCanvas(tv);
400
401 match.findrect = *rect;
402 match.chunk = NULL;
403 ret = false;
404
405 if (startline == 0)
406 startline = TVLineForY(tv, rect->top);
407
408 if (startline == 0)
409 startline = 1;
410
411 for (nline = startline; nline <= tv->nlines; nline++) {
412 match.line = TVGetLine(tv, nline);
413 if (match.line == NULL)
414 goto draw_done;
415
416 if (match.line->rect.bottom < rect->top)
417 /* is not in the view at all yet */
418 continue;
419
420 if (match.line->rect.top >= rect->bottom)
421 /* is further down than the view, we've done enough lines */
422 break;
423
424 /* advance chunk if we need to catch up to this line */
425 if (match.chunk == NULL || match.chunk->end_line < nline) {
426 if (match.chunk == NULL) {
427 /* start at this line's chunk rather than the first one */
428 match.chunk = TVGetChunk(tv, match.line->chunk ?
429 match.line->chunk : tv->first_chunk);
430 if (match.chunk == NULL)
431 goto draw_done;
432 }
433
434 while (match.chunk->end_line < nline ||
435 match.chunk->end < match.line->start) {
436 match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
437 if (!match.chunk)
438 goto draw_done;
439 }
440
441 prep_chunk:
442 match.text_slab = TVGetTextSlab(tv, match.chunk->start);
443 if (match.text_slab == NULL)
444 goto draw_done;
445
446 match.style = TVGetStyle(tv, match.chunk->style);
447 if (match.style == NULL)
448 goto draw_done;
449 if (!match.style->bitmap_data_size)
450 TVApplyStyle(match.style);
451 }
452
453 match.rect.top = match.line->rect.top;
454 match.rect.bottom = match.line->rect.bottom;
455
456 if (match.line->start > match.chunk->start) {
457 match.text_start = match.line->start;
458 match.rect.left = match.line->rect.left + match.line->indent;
459 } else {
460 match.text_start = match.chunk->start;
461 match.rect.left = match.chunk->start_left + match.line->indent;
462 }
463
464 if (match.line->end < match.chunk->end) {
465 match.text_end = match.line->end;
466 match.rect.right = match.line->rect.right + match.line->indent;
467 } else {
468 match.text_end = match.chunk->end;
469 match.rect.right = match.chunk->end_right + match.line->indent;
470 }
471
472 ret = true;
473
474 if (callback(tv, &match, cookie) == false)
475 /* callback requested we stop matching */
476 goto draw_done;
477
478 if (match.chunk->end_line == nline &&
479 match.chunk->end < match.line->end) {
480 /* more chunks on this line */
481 match.chunk = TVGetChunk(tv, match.chunk->next_chunk);
482 if (match.chunk != NULL)
483 goto prep_chunk;
484 }
485 }
486
487 draw_done:
488 TVRevealCanvas(tv);
489 return ret;
490 }
491
492 short
493 TVCharWidth(struct TVRec *tv, struct TVStyle *style, struct TVLine *line,
494 long x, unsigned char c)
495 {
496 long next_tabstop;
497 short width;
498
499 if (c == '\r')
500 return 0;
501
502 if (style->bitmap_data_size)
503 return style->bitmap.bounds.right - style->bitmap.bounds.left;
504
505 if (c == '\t' && tv->tabstop) {
506 next_tabstop = line->rect.left + (long)tv->tabstop *
507 (((x - line->rect.left) / tv->tabstop) + 1);
508 return next_tabstop - x;
509 }
510
511 if (!(width = style->width_cache[c]))
512 width = style->width_cache[c] = CharWidth(c);
513
514 return width;
515 }
516
517 unsigned long
518 TVLineForY(struct TVRec *tv, long y)
519 {
520 struct TVLine *line;
521 unsigned long lo, hi, mid;
522
523 if (tv->nlines == 0)
524 return 0;
525
526 /* find the last line whose top is at or above y */
527 lo = 1;
528 hi = tv->nlines;
529 while (lo < hi) {
530 mid = (lo + hi + 1) / 2;
531 line = TVGetLine(tv, mid);
532 if (line == NULL)
533 return 0;
534 if (line->rect.top <= y)
535 lo = mid;
536 else
537 hi = mid - 1;
538 }
539
540 return lo;
541 }
542
543 unsigned long
544 TVLineForOffset(struct TVRec *tv, unsigned long off)
545 {
546 struct TVLine *line;
547 unsigned long lo, hi, mid;
548
549 if (tv->nlines == 0)
550 return 0;
551
552 /* find the last line starting at or before off */
553 lo = 1;
554 hi = tv->nlines;
555 while (lo < hi) {
556 mid = (lo + hi + 1) / 2;
557 line = TVGetLine(tv, mid);
558 if (line == NULL)
559 return 0;
560 if (line->start <= off)
561 lo = mid;
562 else
563 hi = mid - 1;
564 }
565
566 return lo;
567 }
568
569 struct TVChunk *
570 TVLineChunk(struct TVRec *tv, struct TVLine *line)
571 {
572 struct TVChunk *chunk, *tchunk;
573
574 chunk = TVGetChunk(tv, line->chunk ? line->chunk : tv->first_chunk);
575
576 /* the hint should be right, but don't trust it */
577 while (chunk != NULL && chunk->start > line->start &&
578 (tchunk = TVGetChunk(tv, chunk->prev_chunk)) != NULL)
579 chunk = tchunk;
580 while (chunk != NULL && chunk->end <= line->start &&
581 (tchunk = TVGetChunk(tv, chunk->next_chunk)) != NULL)
582 chunk = tchunk;
583
584 return chunk;
585 }
586
587 long
588 TVXForOffset(struct TVRec *tv, struct TVLine *line, unsigned long off)
589 {
590 struct TVChunk *chunk;
591 struct TVStyle *style;
592 struct TVSlab *slab;
593 unsigned long n, end;
594 long x;
595
596 if (off > line->end)
597 off = line->end;
598
599 x = line->rect.left;
600
601 for (chunk = TVLineChunk(tv, line); chunk != NULL;
602 chunk = TVGetChunk(tv, chunk->next_chunk)) {
603 if (chunk->start > off)
604 break;
605
606 /* same as TVFindInRectLocked, so we measure where it drew */
607 if (chunk->start >= line->start)
608 x = chunk->start_left;
609
610 n = (chunk->start > line->start ? chunk->start : line->start);
611 end = (chunk->end < off ? chunk->end : off);
612 if (n < end) {
613 style = TVGetStyle(tv, chunk->style);
614 slab = TVGetTextSlab(tv, chunk->start);
615 if (style == NULL || slab == NULL)
616 break;
617 if (!style->bitmap_data_size)
618 TVApplyStyle(style);
619
620 for (; n < end; n++)
621 x += TVCharWidth(tv, style, line, x,
622 slab->data[n - slab->base]);
623 }
624
625 if (chunk->end >= off)
626 break;
627 }
628
629 return x + line->indent;
630 }
631
632 long
633 TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x, Boolean caret)
634 {
635 struct TVChunk *chunk;
636 struct TVStyle *style;
637 struct TVSlab *slab;
638 unsigned long n, end;
639 long lx;
640 short width;
641
642 x -= line->indent;
643 lx = line->rect.left;
644
645 for (chunk = TVLineChunk(tv, line); chunk != NULL;
646 chunk = TVGetChunk(tv, chunk->next_chunk)) {
647 if (chunk->start > line->end)
648 break;
649
650 if (chunk->start >= line->start)
651 lx = chunk->start_left;
652
653 n = (chunk->start > line->start ? chunk->start : line->start);
654 end = (chunk->end < line->end ? chunk->end : line->end);
655 if (n < end) {
656 style = TVGetStyle(tv, chunk->style);
657 slab = TVGetTextSlab(tv, chunk->start);
658 if (style == NULL || slab == NULL)
659 break;
660 if (!style->bitmap_data_size)
661 TVApplyStyle(style);
662
663 for (; n < end; n++) {
664 width = TVCharWidth(tv, style, line, lx,
665 slab->data[n - slab->base]);
666 if (caret) {
667 if (x < lx + (width / 2))
668 return n;
669 } else if (x < lx)
670 return -1;
671 else if (x < lx + width)
672 return n;
673 lx += width;
674 }
675 }
676
677 if (chunk->end >= line->end)
678 break;
679 }
680
681 return (caret ? line->end : -1);
682 }
683
684 long
685 TVOffsetForPoint(struct TVRec *tv, long h, long v, Boolean caret)
686 {
687 struct TVLine *line;
688 unsigned long nline;
689
690 if (tv->nlines == 0)
691 return (caret ? 0 : -1);
692
693 line = TVGetLine(tv, 1);
694 if (line == NULL || v < line->rect.top)
695 return (caret ? 0 : -1);
696
697 nline = TVLineForY(tv, v);
698 line = TVGetLine(tv, nline);
699 if (line == NULL)
700 return (caret ? 0 : -1);
701 if (nline == tv->nlines && v >= line->rect.bottom)
702 return (caret ? tv->text_length : -1);
703
704 return TVOffsetForX(tv, line, h, caret);
705 }