AmendHub

Download

jcs

/

textview

/

tv_select.c

 

(View History)

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

1 /*
2 * Copyright (c) 2026 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 /* number of clicks to select each thing */
23 #define TV_UNIT_CHAR 1
24 #define TV_UNIT_WORD 2
25 #define TV_UNIT_LINE 3
26
27 /* max clicks this far apart (in pixels) count as a double click */
28 #define TV_DOUBLE_CLICK_SLOP 2
29
30 /* ticks between scrolls while dragging past the top or bottom */
31 #define TV_DRAG_SCROLL_TICKS 2
32
33 #define TV_IS_WORD_CHAR(c) ((c) >= 128 || (c) == '_' || \
34 ((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'Z') || \
35 ((c) >= 'a' && (c) <= 'z'))
36
37 void
38 TVClick(TVHandle tvh, Point p, Boolean extend)
39 {
40 struct TVRec *tv;
41 struct TVLine *line;
42 unsigned long off, anchor_start, anchor_end, ustart, uend;
43 unsigned long last_scroll;
44 long v, amount;
45 short dh, dv;
46 Point mp;
47
48 tv = TVLock(tvh);
49
50 off = TVOffsetForPoint(tv,
51 (long)p.h - tv->view.left - tv->scrolled.left,
52 (long)p.v - tv->view.top - tv->scrolled.top, true);
53
54 dh = p.h - tv->last_click_point.h;
55 dv = p.v - tv->last_click_point.v;
56 if (!extend &&
57 (unsigned long)TickCount() - tv->last_click_ticks <= GetDblTime() &&
58 dh >= -TV_DOUBLE_CLICK_SLOP && dh <= TV_DOUBLE_CLICK_SLOP &&
59 dv >= -TV_DOUBLE_CLICK_SLOP && dv <= TV_DOUBLE_CLICK_SLOP)
60 tv->click_count = (tv->click_count % TV_UNIT_LINE) + 1;
61 else
62 tv->click_count = 1;
63 tv->last_click_point = p;
64
65 if (extend) {
66 /* keep the end of the selection furthest from the click */
67 if (off < tv->sel_start)
68 anchor_start = tv->sel_end;
69 else if (off > tv->sel_end)
70 anchor_start = tv->sel_start;
71 else if (off - tv->sel_start < tv->sel_end - off)
72 anchor_start = tv->sel_end;
73 else
74 anchor_start = tv->sel_start;
75 anchor_end = anchor_start;
76 } else
77 TVSelectUnit(tv, off, tv->click_count, &anchor_start, &anchor_end);
78
79 last_scroll = 0;
80
81 for (;;) {
82 /* select from the anchor to the unit under the mouse */
83 TVSelectUnit(tv, off, tv->click_count, &ustart, &uend);
84 TVSetSelectLocked(tv, (ustart < anchor_start ? ustart : anchor_start),
85 (uend > anchor_end ? uend : anchor_end));
86
87 if (!StillDown())
88 break;
89
90 /* this must not be called while using the canvas */
91 GetMouse(&mp);
92
93 if (((mp.v < tv->view.top && tv->scrolled.top < 0) ||
94 (mp.v >= tv->view.bottom &&
95 tv->scrolled.bottom > tv->frame.bottom)) &&
96 (unsigned long)TickCount() - last_scroll >= TV_DRAG_SCROLL_TICKS) {
97 /* scroll the next line above or below fully into view */
98 if (mp.v < tv->view.top) {
99 line = TVGetLine(tv, TVLineForY(tv, -tv->scrolled.top - 1));
100 amount = (line == NULL ? 0 : -tv->scrolled.top -
101 line->rect.top);
102 if (amount < 1)
103 amount = 1;
104 } else {
105 line = TVGetLine(tv, TVLineForY(tv, -tv->scrolled.top +
106 tv->frame.bottom));
107 amount = (line == NULL ? 0 : -(line->rect.bottom -
108 (-tv->scrolled.top + tv->frame.bottom)));
109 if (amount > -1)
110 amount = -1;
111 }
112 TVScrollLocked(tv, 0, amount);
113 if (tv->scroller)
114 TVUpdateScrollbar(tvh, tv->scroller);
115 last_scroll = TickCount();
116 }
117
118 /* keep the mouse inside the view to find the text under it */
119 v = mp.v;
120 if (v < tv->view.top)
121 v = tv->view.top;
122 else if (v >= tv->view.bottom)
123 v = tv->view.bottom - 1;
124
125 off = TVOffsetForPoint(tv,
126 (long)mp.h - tv->view.left - tv->scrolled.left,
127 v - tv->view.top - tv->scrolled.top, true);
128 }
129
130 tv->last_click_ticks = TickCount();
131
132 TVUnlock(tv);
133 }
134
135 /* find the start and end of the unit (char, word, line) at offset off */
136 void
137 TVSelectUnit(struct TVRec *tv, unsigned long off, short unit,
138 unsigned long *start, unsigned long *end)
139 {
140 unsigned char c;
141
142 *start = off;
143 *end = off;
144
145 switch (unit) {
146 case TV_UNIT_WORD:
147 c = TVCharAt(tv, off);
148 if (!TV_IS_WORD_CHAR(c)) {
149 /* clicking just past the end of a word selects it */
150 if (off > 0) {
151 c = TVCharAt(tv, off - 1);
152 if (TV_IS_WORD_CHAR(c))
153 off--;
154 }
155 if (off == *start) {
156 /* not in a word, select this one character */
157 c = TVCharAt(tv, off);
158 if (off < tv->text_length && c != '\r')
159 *end = off + 1;
160 return;
161 }
162 }
163
164 *start = off;
165 while (*start > 0) {
166 c = TVCharAt(tv, *start - 1);
167 if (!TV_IS_WORD_CHAR(c))
168 break;
169 (*start)--;
170 }
171 *end = off;
172 while (*end < tv->text_length) {
173 c = TVCharAt(tv, *end);
174 if (!TV_IS_WORD_CHAR(c))
175 break;
176 (*end)++;
177 }
178 break;
179 case TV_UNIT_LINE:
180 /* a whole line of text including its return, not a wrapped line */
181 while (*start > 0) {
182 c = TVCharAt(tv, *start - 1);
183 if (c == '\r')
184 break;
185 (*start)--;
186 }
187 while (*end < tv->text_length) {
188 c = TVCharAt(tv, *end);
189 if (c == '\r') {
190 (*end)++;
191 break;
192 }
193 (*end)++;
194 }
195 break;
196 }
197 }
198
199 void
200 TVSetSelectLocked(struct TVRec *tv, unsigned long start, unsigned long end)
201 {
202 unsigned long t;
203
204 if (start > end) {
205 t = start;
206 start = end;
207 end = t;
208 }
209 if (end > tv->text_length)
210 end = tv->text_length;
211 if (start > end)
212 start = end;
213
214 if (start == tv->sel_start && end == tv->sel_end)
215 return;
216
217 if (tv->active) {
218 /* only invert what changed, see TVInvertRange */
219 if (tv->sel_start == tv->sel_end)
220 TVInvertRange(tv, start, end, &tv->frame);
221 else if (start == end)
222 TVInvertRange(tv, tv->sel_start, tv->sel_end, &tv->frame);
223 else {
224 TVInvertRange(tv, tv->sel_start, start, &tv->frame);
225 TVInvertRange(tv, tv->sel_end, end, &tv->frame);
226 }
227 }
228
229 tv->sel_start = start;
230 tv->sel_end = end;
231 }
232
233 void
234 TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end)
235 {
236 struct TVRec *tv;
237
238 tv = TVLock(tvh);
239 TVSetSelectLocked(tv, start, end);
240 TVUnlock(tv);
241 }
242
243 void
244 TVGetSelect(TVHandle tvh, unsigned long *start, unsigned long *end)
245 {
246 struct TVRec *tv;
247
248 tv = TVLock(tvh);
249 *start = tv->sel_start;
250 *end = tv->sel_end;
251 TVUnlock(tv);
252 }
253
254 void
255 TVSelectAll(TVHandle tvh)
256 {
257 struct TVRec *tv;
258
259 tv = TVLock(tvh);
260 TVSetSelectLocked(tv, 0, tv->text_length);
261 TVUnlock(tv);
262 }
263
264 void
265 TVActivate(TVHandle tvh, Boolean active)
266 {
267 struct TVRec *tv;
268
269 active = (active != 0);
270
271 tv = TVLock(tvh);
272 if (tv->active != active) {
273 tv->active = active;
274 /* show or hide the selection */
275 if (tv->sel_start != tv->sel_end)
276 TVInvertRange(tv, tv->sel_start, tv->sel_end, &tv->frame);
277 }
278 TVUnlock(tv);
279 }
280
281 Boolean
282 TVCopy(TVHandle tvh)
283 {
284 struct TVRec *tv;
285 Handle scrap;
286 unsigned long start, end, len;
287 Boolean ret;
288
289 tv = TVLock(tvh);
290 start = tv->sel_start;
291 end = tv->sel_end;
292 TVUnlock(tv);
293
294 len = end - start;
295 if (len == 0)
296 return false;
297
298 /* tvh is unlocked so this can move it if it needs to */
299 scrap = NewHandle(len);
300 if (scrap == NULL)
301 return false;
302 HLock(scrap);
303
304 ret = false;
305 if (TVGetText(tvh, *scrap, start, end) == len) {
306 ZeroScrap();
307 ret = (PutScrap(len, 'TEXT', *scrap) == noErr);
308 }
309
310 DisposHandle(scrap);
311 return ret;
312 }