AmendHub

Download (as of this amendment):

jcs

/

textview

/

amendments

/

41

TVGetOffset: Return a long to be able to return -1, fix TVClear

We weren't clearing first_chunk and last_chunk when clearing so the
next drawing might have gone into lala land.
 
Also use an initial slab id of 1 to avoid the not-0-offset hackery.

jcs made amendment 41 over 2 years ago
--- textview.c Fri Oct 25 16:20:30 2024 +++ textview.c Tue Oct 29 08:58:59 2024 @@ -467,11 +467,12 @@ Boolean TVGetOffsetCallback(struct TVRec *tv, struct TVFindInRectMatch *match, void *cookie) { - unsigned long n, left, *off; + unsigned long n, left; + long *off; short width; unsigned char c; - off = (unsigned long *)cookie; + off = (long *)cookie; left = match->rect.left; for (n = match->text_start; n < match->text_end; n++) { @@ -489,12 +490,12 @@ TVGetOffsetCallback(struct TVRec *tv, struct TVFindInR return true; } -unsigned long +long TVGetOffset(TVHandle tvh, Point p) { struct TVRec *tv; BigRect prect; - unsigned long off; + long off; tv = TVLock(tvh); TVUseCanvas(tv); @@ -505,7 +506,8 @@ TVGetOffset(TVHandle tvh, Point p) prect.bottom = prect.top; off = 0; - TVFindInRect(tv, &prect, 1, TVGetOffsetCallback, &off); + if (!TVFindInRect(tv, &prect, 1, TVGetOffsetCallback, &off)) + off = -1; TVRevealCanvas(tv); TVUnlock(tv); @@ -665,13 +667,13 @@ TVClear(TVHandle tvh) struct TVChunk *tchunk, *cur_chunk; struct TVSlab *slab; short n; - + tv = TVLock(tvh); /* free chunk slabs except first one */ while (tv->last_chunk_slab != tv->first_chunk_slab) { - HLock(tv->last_chunk_slab); slabh = tv->last_chunk_slab; + HLock(slabh); slab = (struct TVSlab *)*slabh; tv->last_chunk_slab = slab->prev_slab; DisposeHandle(slabh); @@ -682,11 +684,14 @@ TVClear(TVHandle tvh) memset(slab->data, 0, slab->size); slab->pos = 0; HUnlock(tv->first_chunk_slab); + + tv->first_chunk = 0; + tv->last_chunk = 0; /* free text slabs except first one */ while (tv->last_text_slab != tv->first_text_slab) { - HLock(tv->last_text_slab); slabh = tv->last_text_slab; + HLock(slabh); slab = (struct TVSlab *)*slabh; tv->last_text_slab = slab->prev_slab; DisposeHandle(slabh); @@ -904,10 +909,10 @@ TVAllocateSlab(struct TVRec *tv, unsigned long size, H unsigned short id; if (last == NULL || *last == NULL) - id = 0; + id = 1; else { HLock(*last); - slab = (struct TVSlab *)*last; + slab = (struct TVSlab *)**last; if (slab->id == USHRT_MAX) { Debugger(); HUnlock(*last); @@ -1028,10 +1033,6 @@ TVCreateChunk(struct TVRec *tv) slab = (struct TVSlab *)*slabh; } - if (slab->id == 0 && slab->pos == 0 && !tv->first_chunk) - /* for first chunk, offset pos so it's not confused with NULL */ - slab->pos = 2; - SET_CHUNKPTR_SLABID(cptr, slab->id); SET_CHUNKPTR_OFFSET(cptr, slab->pos); @@ -1053,6 +1054,9 @@ TVGetLine(struct TVRec *tv, unsigned long line) Handle group; unsigned short groupn, idx; + if (line == 0) + return NULL; + groupn = (line - 1) / TV_LINE_GROUP_NLINES; if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))) return NULL; @@ -1451,9 +1455,12 @@ prep_chunk: match.rect.right = match.chunk->end_right; } - if (!callback(tv, &match, cookie)) + ret = true; + + if (callback(tv, &match, cookie) == false) + /* callback requested we stop matching */ goto draw_done; - + if (match.chunk->end_line == nline && match.chunk->end < match.line->end) { /* more chunks on this line */ @@ -1462,7 +1469,6 @@ prep_chunk: goto prep_chunk; } } - ret = true; draw_done: TVRevealCanvas(tv); --- textview.h Fri Oct 25 16:28:27 2024 +++ textview.h Mon Oct 28 12:38:08 2024 @@ -153,7 +153,7 @@ Boolean TVResize(TVHandle tvh); void TVUpdate(TVHandle tvh); void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller); void TVClick(TVHandle tvh, Point p, Boolean extend); -unsigned long TVGetOffset(TVHandle tvh, Point p); +long TVGetOffset(TVHandle tvh, Point p); void TVScroll(TVHandle tvh, short horiz, short vert); void TVCopy(TVHandle tvh); void TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end);