AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

66

select: Add text selection module


jcs made amendment 66 about 1 month ago
--- tv_select.c Sat Sep 26 22:49:55 2026 +++ tv_select.c Sat Sep 26 22:49:55 2026 @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2026 joshua stein <jcs@jcs.org> + * + * 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 <stdio.h> +#include <stddef.h> +#include <string.h> +#include "textview.h" + +/* number of clicks to select each thing */ +#define TV_UNIT_CHAR 1 +#define TV_UNIT_WORD 2 +#define TV_UNIT_LINE 3 + +/* max clicks this far apart (in pixels) count as a double click */ +#define TV_DOUBLE_CLICK_SLOP 2 + +/* ticks between scrolls while dragging past the top or bottom */ +#define TV_DRAG_SCROLL_TICKS 2 + +#define TV_IS_WORD_CHAR(c) ((c) >= 128 || (c) == '_' || \ + ((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'Z') || \ + ((c) >= 'a' && (c) <= 'z')) + +void +TVClick(TVHandle tvh, Point p, Boolean extend) +{ + struct TVRec *tv; + struct TVLine *line; + unsigned long off, anchor_start, anchor_end, ustart, uend; + unsigned long last_scroll; + long v, amount; + short dh, dv; + Point mp; + + tv = TVLock(tvh); + + off = TVOffsetForPoint(tv, (long)p.h - tv->view.left - tv->scrolled.left, + (long)p.v - tv->view.top - tv->scrolled.top); + + dh = p.h - tv->last_click_point.h; + dv = p.v - tv->last_click_point.v; + if (!extend && + (unsigned long)TickCount() - tv->last_click_ticks <= GetDblTime() && + dh >= -TV_DOUBLE_CLICK_SLOP && dh <= TV_DOUBLE_CLICK_SLOP && + dv >= -TV_DOUBLE_CLICK_SLOP && dv <= TV_DOUBLE_CLICK_SLOP) + tv->click_count = (tv->click_count % 3) + 1; + else + tv->click_count = 1; + tv->last_click_point = p; + + if (extend) { + /* keep the end of the selection furthest from the click */ + if (off < tv->sel_start) + anchor_start = tv->sel_end; + else if (off > tv->sel_end) + anchor_start = tv->sel_start; + else if (off - tv->sel_start < tv->sel_end - off) + anchor_start = tv->sel_end; + else + anchor_start = tv->sel_start; + anchor_end = anchor_start; + } else + TVSelectUnit(tv, off, tv->click_count, &anchor_start, &anchor_end); + + last_scroll = 0; + + for (;;) { + /* select from the anchor to the unit under the mouse */ + TVSelectUnit(tv, off, tv->click_count, &ustart, &uend); + TVSetSelectLocked(tv, (ustart < anchor_start ? ustart : anchor_start), + (uend > anchor_end ? uend : anchor_end)); + + if (!StillDown()) + break; + + /* this must not be called while using the canvas */ + GetMouse(&mp); + + if (((mp.v < tv->view.top && tv->scrolled.top < 0) || + (mp.v >= tv->view.bottom && + tv->scrolled.bottom > tv->frame.bottom)) && + (unsigned long)TickCount() - last_scroll >= TV_DRAG_SCROLL_TICKS) { + /* scroll the next line above or below fully into view */ + if (mp.v < tv->view.top) { + line = TVGetLine(tv, TVLineForY(tv, -tv->scrolled.top - 1)); + amount = (line == NULL ? 0 : -tv->scrolled.top - + line->rect.top); + if (amount < 1) + amount = 1; + } else { + line = TVGetLine(tv, TVLineForY(tv, -tv->scrolled.top + + tv->frame.bottom)); + amount = (line == NULL ? 0 : -(line->rect.bottom - + (-tv->scrolled.top + tv->frame.bottom))); + if (amount > -1) + amount = -1; + } + TVScrollUnlocked(tv, 0, amount); + if (tv->scroller) + TVUpdateScrollbar(tvh, tv->scroller); + last_scroll = TickCount(); + } + + /* keep the mouse inside the view to find the text under it */ + v = mp.v; + if (v < tv->view.top) + v = tv->view.top; + else if (v >= tv->view.bottom) + v = tv->view.bottom - 1; + + off = TVOffsetForPoint(tv, + (long)mp.h - tv->view.left - tv->scrolled.left, + v - tv->view.top - tv->scrolled.top); + } + + tv->last_click_ticks = TickCount(); + + TVUnlock(tv); +} + +/* find the start and end of the unit (char, word, line) at offset off */ +void +TVSelectUnit(struct TVRec *tv, unsigned long off, short unit, + unsigned long *start, unsigned long *end) +{ + unsigned char c; + + *start = off; + *end = off; + + switch (unit) { + case TV_UNIT_WORD: + c = TVCharAt(tv, off); + if (!TV_IS_WORD_CHAR(c)) { + /* clicking just past the end of a word selects it */ + if (off > 0) { + c = TVCharAt(tv, off - 1); + if (TV_IS_WORD_CHAR(c)) + off--; + } + if (off == *start) { + /* not in a word, select this one character */ + c = TVCharAt(tv, off); + if (off < tv->text_length && c != '\r') + *end = off + 1; + return; + } + } + + *start = off; + while (*start > 0) { + c = TVCharAt(tv, *start - 1); + if (!TV_IS_WORD_CHAR(c)) + break; + (*start)--; + } + *end = off; + while (*end < tv->text_length) { + c = TVCharAt(tv, *end); + if (!TV_IS_WORD_CHAR(c)) + break; + (*end)++; + } + break; + case TV_UNIT_LINE: + /* a whole line of text including its return, not a wrapped line */ + while (*start > 0) { + c = TVCharAt(tv, *start - 1); + if (c == '\r') + break; + (*start)--; + } + while (*end < tv->text_length) { + c = TVCharAt(tv, *end); + if (c == '\r') { + (*end)++; + break; + } + (*end)++; + } + break; + } +} + +void +TVSetSelectLocked(struct TVRec *tv, unsigned long start, unsigned long end) +{ + unsigned long t; + + if (start > end) { + t = start; + start = end; + end = t; + } + if (end > tv->text_length) + end = tv->text_length; + if (start > end) + start = end; + + if (start == tv->sel_start && end == tv->sel_end) + return; + + if (tv->active) { + /* only invert what changed, see TVInvertRange */ + if (tv->sel_start == tv->sel_end) + TVInvertRange(tv, start, end, &tv->frame); + else if (start == end) + TVInvertRange(tv, tv->sel_start, tv->sel_end, &tv->frame); + else { + TVInvertRange(tv, tv->sel_start, start, &tv->frame); + TVInvertRange(tv, tv->sel_end, end, &tv->frame); + } + } + + tv->sel_start = start; + tv->sel_end = end; +} + +void +TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end) +{ + struct TVRec *tv; + + tv = TVLock(tvh); + TVSetSelectLocked(tv, start, end); + TVUnlock(tv); +} + +void +TVGetSelect(TVHandle tvh, unsigned long *start, unsigned long *end) +{ + struct TVRec *tv; + + tv = TVLock(tvh); + *start = tv->sel_start; + *end = tv->sel_end; + TVUnlock(tv); +} + +void +TVSelectAll(TVHandle tvh) +{ + struct TVRec *tv; + + tv = TVLock(tvh); + TVSetSelectLocked(tv, 0, tv->text_length); + TVUnlock(tv); +} + +void +TVActivate(TVHandle tvh, Boolean active) +{ + struct TVRec *tv; + + active = (active != 0); + + tv = TVLock(tvh); + if (tv->active != active) { + tv->active = active; + /* show or hide the selection */ + if (tv->sel_start != tv->sel_end) + TVInvertRange(tv, tv->sel_start, tv->sel_end, &tv->frame); + } + TVUnlock(tv); +} + +Boolean +TVCopy(TVHandle tvh) +{ + struct TVRec *tv; + Handle scrap; + unsigned long start, end, len; + Boolean ret; + + tv = TVLock(tvh); + start = tv->sel_start; + end = tv->sel_end; + TVUnlock(tv); + + len = end - start; + if (len == 0) + return false; + + /* tvh is unlocked so this can move it if it needs to */ + scrap = NewHandle(len); + if (scrap == NULL) + return false; + HLock(scrap); + + ret = false; + if (TVGetText(tvh, *scrap, start, end) == len) { + ZeroScrap(); + ret = (PutScrap(len, 'TEXT', *scrap) == noErr); + } + + DisposHandle(scrap); + return ret; +}