/* * Copyright (c) 2023-2024 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "textview.h" void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller) { struct TVRec *tv; long max; RgnHandle rgn; short wasval, wasmax, val, div; tv = TVLock(tvh); div = TVScrollDivisor(tv); /* * Avoid SetCtlMin/Max because it will redraw and then SetCtlValue will * redraw again, which can cause a jump if we're trying to keep the * scrollbar position in the same place (like at the bottom). */ max = (tv->bounds.bottom - tv->bounds.top - (tv->view.bottom - tv->view.top)) / div; if (max < 0) max = 0; wasval = GetCtlValue(scroller); HLock(scroller); wasmax = (*scroller)->contrlMax; (*scroller)->contrlMin = 0; val = -(tv->scrolled.top) / div; (*scroller)->contrlMax = max; SetCtlValue(scroller, val); HUnlock(scroller); if ((wasmax == 0 && max != 0) || (wasmax != 0 && max == 0)) goto redraw; if (wasval == 0 && val == 0) goto done; if (wasval == wasmax && val == max && tv->autoscroll) goto done; redraw: rgn = NewRgn(); HLock(scroller); RectRgn(rgn, &(*scroller)->contrlRect); UpdtControl((*(scroller))->contrlOwner, rgn); HUnlock(scroller); DisposeRgn(rgn); done: TVUnlock(tv); } static TVHandle tv_track_control = NULL; void TVSetTrackScrollControl(TVHandle tvh) { tv_track_control = tvh; } pascal void TVTrackScrollControl(ControlHandle control, short part) { struct TVRec *tv; KeyMap km; Boolean cmd_down; if (tv_track_control == NULL) { /* TVTrackScrollControl without TVSetTrackScrollControl */ Debugger(); return; } GetKeys(&km); cmd_down = (km[1] & (1L << 15)) != 0; TVScrollPart(tv_track_control, part, cmd_down); tv = TVLock(tv_track_control); if (control != tv->scroller) SetCtlValue(control, -(tv->scrolled.top) / TVScrollDivisor(tv)); TVUnlock(tv); } void TVScrollPart(TVHandle tvh, short part, Boolean fine) { struct TVRec *tv; struct TVLine *top_line, *bottom_line, *line, *oline; tv = TVLock(tvh); top_line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top))); bottom_line = TVGetLine(tv, TVLineForY(tv, -(tv->scrolled.top) + tv->frame.bottom - 1)); if (top_line == NULL || bottom_line == NULL) { TVUnlock(tv); return; } switch (part) { case inUpButton: if (fine) { TVScrollLocked(tv, 0, 1); break; } /* scroll to the line before the top-most line */ if (top_line->nline > 1) { oline = TVGetLine(tv, top_line->nline - 1); if (oline == NULL) break; TVScrollLocked(tv, 0, top_line->rect.top - oline->rect.top + 1); break; } /* nothing found, just scroll to the top */ TVScrollLocked(tv, 0, tv->bounds.bottom); break; case inPageUp: /* scroll to put the top mostly-visible line at the bottom */ TVScrollLocked(tv, 0, (long)tv->frame.bottom - (tv->scrolled.top + top_line->rect.bottom) - 1); break; case inDownButton: if (fine) { TVScrollLocked(tv, 0, -1); break; } /* scroll to the line after the bottom-most line */ if (bottom_line->nline < tv->nlines) { line = TVGetLine(tv, bottom_line->nline + 1); if (line == NULL) break; TVScrollLocked(tv, 0, -(tv->scrolled.top - tv->frame.bottom + line->rect.bottom)); break; } /* nothing found, just scroll to the bottom */ TVScrollLocked(tv, 0, -(tv->bounds.bottom)); break; case inPageDown: /* scroll to put the bottom mostly-visible line at the top */ if (bottom_line->nline < tv->nlines) { line = TVGetLine(tv, bottom_line->nline + 1); if (line == NULL) break; TVScrollLocked(tv, 0, -(line->rect.top + tv->scrolled.top) + (line->rect.bottom - line->rect.top) + 1); break; } /* nothing found, just scroll to the bottom */ TVScrollLocked(tv, 0, -(tv->bounds.bottom)); break; } if (tv->scroller) SetCtlValue(tv->scroller, -(tv->scrolled.top) / TVScrollDivisor(tv)); TVUnlock(tv); } void TVScroll(TVHandle tvh, long horiz, long vert) { struct TVRec *tv; short div; tv = TVLock(tvh); div = TVScrollDivisor(tv); TVScrollLocked(tv, horiz * div, vert * div); TVUnlock(tv); } void TVScrollToOffset(TVHandle tvh, unsigned long off) { struct TVRec *tv; struct TVLine *line; short div; tv = TVLock(tvh); div = TVScrollDivisor(tv); line = TVGetLine(tv, TVLineForOffset(tv, off)); if (line != NULL) TVScrollLocked(tv, 0, -((line->rect.top / div) * div) - tv->scrolled.top); TVUnlock(tv); } void TVUpdate(TVHandle tvh) { struct TVRec *tv; tv = TVLock(tvh); TVRedrawRect(tv, &tv->frame, 0, true); TVUnlock(tv); } Boolean TVSetupCanvas(struct TVRec *tv) { long width, height; GrafPtr old_port, canvas = NULL; Handle canvas_baseaddr = NULL; /* * TODO: make canvas 3 times vis portal height so we can pre-render * pages above and below to make scrolling seem faster? */ width = tv->view.right - tv->view.left; height = tv->view.bottom - tv->view.top; /* this must be a Ptr because it cannot move once opened */ canvas = (GrafPtr)NewPtrClear(sizeof(GrafPort)); if (canvas == NULL) goto fail; canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2; canvas_baseaddr = NewHandleClear((long)canvas->portBits.rowBytes * height); if (canvas_baseaddr == NULL) goto fail; /* everything allocated, commit to new canvas */ if (tv->canvas) { ClosePort(tv->canvas); DisposPtr(tv->canvas); } if (tv->canvas_baseaddr) DisposHandle(tv->canvas_baseaddr); tv->canvas = canvas; /* opening the port resets portRect/portBits */ GetPort(&old_port); OpenPort(tv->canvas); RectRgn(tv->canvas->clipRgn, &tv->canvas->portRect); RectRgn(tv->canvas->visRgn, &tv->canvas->portRect); SetPort(old_port); tv->canvas->portRect.right = width; tv->canvas->portRect.bottom = height; tv->canvas->portBits.bounds = tv->canvas->portRect; tv->canvas->portBits.rowBytes = (((width - 1) / 16) + 1) * 2; tv->canvas_baseaddr = canvas_baseaddr; HLock(tv->canvas_baseaddr); tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr); return true; fail: if (canvas) DisposPtr(canvas); if (canvas_baseaddr) DisposHandle(canvas_baseaddr); return false; } short TVScrollDivisor(struct TVRec *tv) { short div; long max; div = 1; max = tv->bounds.bottom - tv->bounds.top - (tv->view.bottom - tv->view.top); while (max > SHRT_MAX) { div *= 10; max /= 10; } if (div == 0) Debugger(); return div; } Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie) { unsigned long n; long next_tabstop; unsigned char c; char *text, *spanstart; Rect r; Point pen; if (match->text_end == match->text_start) return true; if (match->rect.bottom < -(tv->scrolled.top)) /* no sense drawing what we can't see yet */ return true; if (match->rect.top >= -(tv->scrolled.top) + tv->frame.bottom) /* further down than the scrolled view, we've drawn enough lines */ return false; if (match->line->grew) { r.left = tv->scrolled.left + match->line->rect.left; r.top = tv->scrolled.top + match->line->rect.top; r.right = tv->scrolled.left + match->line->rect.right; r.bottom = tv->scrolled.top + match->line->rect.bottom; EraseRect(&r); match->line->grew = false; } else if (match->style->align == TV_ALIGN_CENTER || match->style->align == TV_ALIGN_RIGHT) { /* erase from this chunk to the right of the view */ r.left = tv->scrolled.left + match->rect.left; r.top = tv->scrolled.top + match->line->rect.top; r.right = tv->scrolled.left + tv->view.right - tv->padding.right; r.bottom = tv->scrolled.top + match->line->rect.bottom; EraseRect(&r); } MoveTo(tv->scrolled.left + match->rect.left, tv->scrolled.top + match->rect.bottom - match->line->baseline); if (match->style->bitmap_data_size) { r = match->style->bitmap.bounds; r.left = tv->scrolled.left + match->rect.left; r.right = r.left + (match->style->bitmap.bounds.right - match->style->bitmap.bounds.left); r.bottom = tv->scrolled.top + match->line->rect.bottom; r.top = r.bottom - (match->style->bitmap.bounds.bottom - match->style->bitmap.bounds.top); match->style->bitmap.baseAddr = (QDPtr)match->style->bitmap_data; CopyBits(&match->style->bitmap, &thePort->portBits, &match->style->bitmap.bounds, &r, srcOr, NULL); } else if (tv->tabstop) { text = match->text_slab->data + (match->text_start - match->text_slab->base); spanstart = NULL; for (n = 0; n < match->text_end - match->text_start; n++) { c = *text; if (c == '\t') { if (spanstart != NULL) DrawText(spanstart, 0, text - spanstart); GetPen(&pen); next_tabstop = pen.h + TVCharWidth(tv, match->style, match->line, pen.h - tv->scrolled.left - match->line->indent, '\t'); #if 0 /* draw dotted lines for tabs */ /* XXX: disabled for now but might be useful as an option */ PenPat(&tv->tab_pat); MoveTo(pen.h, tv->scrolled.top + match->line->rect.top + ((match->line->rect.bottom - match->line->rect.top) / 2)); GetPen(&pen); LineTo(next_tabstop - 1, pen.v); PenPat(black); MoveTo(next_tabstop, tv->scrolled.top + match->line->rect.bottom - match->line->baseline); #else MoveTo(next_tabstop, pen.v); #endif spanstart = NULL; } else if (spanstart == NULL) spanstart = text; text++; } if (spanstart != NULL) DrawText(spanstart, 0, text - spanstart); } else DrawText(match->text_slab->data + (match->text_start - match->text_slab->base), 0, match->text_end - match->text_start); tv->canvas_dirty = true; return true; } void TVScrollLocked(struct TVRec *tv, long horiz, long vert) { long wastop, top, wasleft, left; Rect r; RgnHandle vacated; if (!horiz && !vert) return; TVUseCanvas(tv); wastop = tv->scrolled.top; wasleft = tv->scrolled.left; tv->scrolled.bottom += vert; tv->scrolled.top += vert; if (tv->scrolled.bottom < tv->frame.bottom) { /* can never scroll down lower than last line */ tv->scrolled.bottom = tv->frame.bottom; tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom; } if (tv->scrolled.top > 0) { /* can never scroll up higher than first line */ tv->scrolled.top = 0; tv->scrolled.bottom = tv->bounds.bottom; } tv->scrolled.left += horiz; tv->scrolled.right += horiz; /* TODO: bound */ left = tv->scrolled.left - wasleft; top = tv->scrolled.top - wastop; if ((top < 0 ? -top : top) > tv->frame.bottom) { /* moving a lot so nothing to ScrollRect */ TVRedrawRect(tv, &tv->frame, 0, true); TVRevealCanvas(tv); return; } vacated = NewRgn(); ScrollRect(&tv->frame, left, top, vacated); tv->canvas_dirty = true; HLock(vacated); r = (**vacated).rgnBBox; HUnlock(vacated); DisposeRgn(vacated); /* only redraw what was vacated, the rest moved with its highlighting */ TVRedrawRect(tv, &r, 0, true); TVRevealCanvas(tv); } void TVUseCanvas(struct TVRec *tv) { if (++tv->canvas_refcnt != 1) return; if (tv->canvas_refcnt == 0) Debugger(); #ifdef USE_CANVAS GetPort(&tv->canvas_old_port); HLock(tv->canvas_baseaddr); tv->canvas->portBits.baseAddr = *(tv->canvas_baseaddr); SetPort(tv->canvas); #else GetClip(tv->view_clip = NewRgn()); SetOrigin(-tv->view.left, -tv->view.top); ClipRect(&tv->frame); #endif } void TVRevealCanvas(struct TVRec *tv) { if (tv->canvas_refcnt <= 0) Debugger(); if (--tv->canvas_refcnt != 0) return; #ifdef USE_CANVAS SetPort(tv->canvas_old_port); if (tv->canvas_dirty) CopyBits(&tv->canvas->portBits, &tv->canvas_old_port->portBits, &tv->canvas->portRect, &tv->view, srcCopy, NULL); #else SetOrigin(0, 0); SetClip(tv->view_clip); DisposeRgn(tv->view_clip); #endif ValidRect(&tv->view); tv->canvas_old_port = NULL; tv->canvas_dirty = false; #ifdef USE_CANVAS HUnlock(tv->canvas_baseaddr); #endif } void TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase) { BigRect find; Rect clip; if (!SectRect(r, &tv->frame, &clip)) return; TVUseCanvas(tv); ClipRect(&clip); if (erase) EraseRect(&clip); find.left = clip.left - tv->scrolled.left; find.top = clip.top - tv->scrolled.top; find.right = clip.right - tv->scrolled.left; find.bottom = clip.bottom - tv->scrolled.top; TVFindInRectLocked(tv, &find, startline, TVDrawMatch, NULL); if (tv->active && tv->sel_start != tv->sel_end) TVInvertRange(tv, tv->sel_start, tv->sel_end, &clip); ClipRect(&tv->frame); tv->canvas_dirty = true; TVRevealCanvas(tv); } void TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b, Rect *clip) { struct TVLine *line; unsigned long nline, cline, t; long left, right, top, bottom, edge; Boolean chars, trail; Rect r; if (a > b) { t = a; a = b; b = t; } if (a == b || tv->nlines == 0) return; nline = TVLineForOffset(tv, a); if (nline > 1) { /* a wrapped line ends where the next begins, include its trail */ line = TVGetLine(tv, nline - 1); if (line != NULL && line->end >= a) nline--; } /* skip lines above the clip */ cline = TVLineForY(tv, (long)clip->top - tv->scrolled.top); if (cline > nline) nline = cline; edge = tv->frame.right - tv->padding.right; TVUseCanvas(tv); for (; nline <= tv->nlines; nline++) { line = TVGetLine(tv, nline); if (line == NULL || line->start >= b) break; top = tv->scrolled.top + line->rect.top; bottom = tv->scrolled.top + line->rect.bottom; if (top >= clip->bottom) break; if (bottom <= clip->top) continue; /* any of this line's characters, then the area past its end */ chars = (a < line->end && b > line->start); trail = (a <= line->end && b > line->end); if (!chars && !trail) continue; if (a <= line->start) left = line->rect.left + line->indent; else left = TVXForOffset(tv, line, a); if (b >= line->end) right = line->rect.right + line->indent; else right = TVXForOffset(tv, line, b); if (trail && right < edge) right = edge; if (right <= left) continue; if (top < clip->top) top = clip->top; if (bottom > clip->bottom) bottom = clip->bottom; r.left = tv->scrolled.left + left; r.top = top; r.right = tv->scrolled.left + right; r.bottom = bottom; if (SectRect(&r, clip, &r)) InvertRect(&r); } tv->canvas_dirty = true; TVRevealCanvas(tv); }