jcs
/textview
/amendments
/22
Bring scrolling up to date
jcs made amendment 22 over 2 years ago
--- textview.c Wed Oct 16 22:25:38 2024
+++ textview.c Thu Oct 17 15:01:50 2024
@@ -45,7 +45,7 @@ bool TVCalcLines(struct TVRec *tv);
void TVApplyStyle(struct TVStyle *style);
struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style);
void TVScrollUnlocked(struct TVRec *tv, short horiz, short vert);
-bool TVDrawVisibleChunks(struct TVRec *tv);
+bool TVDrawVisibleLines(struct TVRec *tv);
void TVUseCanvas(struct TVRec *tv);
void TVRevealCanvas(struct TVRec *tv);
@@ -68,8 +68,8 @@ TVNew(Rect *view, Rect *padding)
tv->view = *view;
tv->view.left++;
tv->view.top++;
- tv->view.right -= 2;
- tv->view.bottom -= 2;
+ tv->view.right -= 1;
+ tv->view.bottom -= 1;
tv->padding = *padding;
tv->padding.left++;
@@ -190,7 +190,6 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
avail = text_slab->size - text_slab->pos;
if (avail == 0) {
/* new text slab, new chunk */
- Debugger();
chunk->end = text_slab->base + text_slab->pos;
if ((text_slab = TVAllocateTextSlab(tv)) == NULL) {
@@ -216,7 +215,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
}
if (TVCalcLines(tv))
- TVDrawVisibleChunks(tv);
+ TVDrawVisibleLines(tv);
else
ret = false;
@@ -311,9 +310,12 @@ TVTrackScrollControl(ControlHandle control, short part
{
struct TVRec *tv;
struct TVChunk *chunk, *top_chunk = NULL;
+ struct TVLine *line, *oline;
+ KeyMap km;
long n, top;
- bool horiz;
-#if 0
+ unsigned short linen;
+ bool horiz, cmd_down;
+
if (tv_track_control == NULL) {
/* TVTrackScrollControl without TVSetTrackScrollControl */
Debugger();
@@ -323,40 +325,34 @@ TVTrackScrollControl(ControlHandle control, short part
horiz = (((*control)->contrlRect.bottom - (*control)->contrlRect.top) <
((*control)->contrlRect.right - (*control)->contrlRect.left));
+
+ GetKeys(&km);
+ cmd_down = (km[1] & (1 << 15)) != 0;
switch (part) {
case inUpButton:
- /* scroll to the previous line that is not yet mostly-visible */
- for (chunk = TVGetChunk(tv, tv->last_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->prev_chunk)) {
- if ((chunk->rect.top + (long)(chunk->baseline / 2) +
- tv->scrolled.top) > 0)
- continue;
-
- if (!TVGetChunk(tv, chunk->prev_chunk))
- break;
-
- TVScrollUnlocked(tv, 0, -(tv->scrolled.top) - chunk->rect.top);
+ if (cmd_down) {
+ TVScrollUnlocked(tv, 0, 1);
goto update_value;
}
+ /* scroll to the line before the top-most line */
+ if (tv->top_line > 1) {
+ line = TVGetLine(tv, tv->top_line);
+ oline = TVGetLine(tv, tv->top_line - 1);
+ TVScrollUnlocked(tv, 0, line->rect.top - oline->rect.top + 1);
+ goto update_value;
+ }
+
/* nothing found, just scroll to the top */
TVScrollUnlocked(tv, 0, -(tv->scrolled.top));
break;
case inPageUp:
/* scroll to put the top mostly-visible line at the bottom */
- for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- if ((chunk->rect.top + (long)(chunk->baseline / 2) +
- tv->scrolled.top) < 0)
- continue;
-
- top = tv->scrolled.top + chunk->rect.top + tv->view_height -
- (chunk->rect.bottom - chunk->rect.top);
- if (tv->scrolled.top + top > 0)
- break;
-
- TVScrollUnlocked(tv, 0, top);
+ if (tv->top_line) {
+ line = TVGetLine(tv, tv->top_line);
+ TVScrollUnlocked(tv, 0, tv->view_height -
+ (tv->scrolled.top + line->rect.bottom) - 1);
goto update_value;
}
@@ -364,32 +360,28 @@ TVTrackScrollControl(ControlHandle control, short part
TVScrollUnlocked(tv, 0, -(tv->scrolled.top));
break;
case inDownButton:
+ if (cmd_down) {
+ TVScrollUnlocked(tv, 0, -1);
+ goto update_value;
+ }
+
/* scroll to the line after the bottom-most line */
- for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- top = chunk->rect.top + (chunk->baseline / 2) +
- tv->scrolled.top;
- if (top < tv->view_height)
- continue;
-
+ if (tv->bottom_line < tv->nlines) {
+ line = TVGetLine(tv, tv->bottom_line + 1);
TVScrollUnlocked(tv, 0,
- -((chunk->rect.bottom - tv->view_height) +
- tv->scrolled.top));
+ -(tv->scrolled.top - tv->view_height + line->rect.bottom));
goto update_value;
}
/* nothing found, just scroll to the bottom */
- TVScrollUnlocked(tv, 0, tv->view_height - tv->scrolled.bottom);
+ TVScrollUnlocked(tv, 0, -(tv->bounds.bottom));
break;
case inPageDown:
/* scroll to put the bottom mostly-visible line at the top */
- for (chunk = TVGetChunk(tv, tv->last_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->prev_chunk)) {
- if (chunk->rect.bottom - (long)(chunk->baseline / 2) +
- tv->scrolled.top > tv->view_height)
- continue;
-
- TVScrollUnlocked(tv, 0, -(tv->scrolled.top) - chunk->rect.top);
+ if (tv->bottom_line < tv->nlines) {
+ line = TVGetLine(tv, tv->bottom_line + 1);
+ TVScrollUnlocked(tv, 0, -(line->rect.top + tv->scrolled.top) +
+ (line->rect.bottom - line->rect.top) + 1);
goto update_value;
}
@@ -401,7 +393,6 @@ TVTrackScrollControl(ControlHandle control, short part
update_value:
SetCtlValue(control, -(tv->scrolled.top));
TVUnlock(tv);
-#endif
}
void
@@ -486,23 +477,11 @@ void
TVUpdate(TVHandle tvh)
{
struct TVRec *tv;
- struct TVChunk *chunk;
-#if 0
+
tv = TVLock(tvh);
TVUseCanvas(tv);
-
- for (chunk = TVGetChunk(tv, tv->first_visible_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- if (chunk->rect.bottom < -(tv->scrolled.top))
- continue;
- if (chunk->rect.top + tv->scrolled.top > tv->view_height)
- break;
- TVDrawText(tv, chunk, 0);
- }
-
TVRevealCanvas(tv);
TVUnlock(tv);
-#endif
}
void
@@ -908,7 +887,6 @@ TVCalcLines(struct TVRec *tv)
if (n == chunk->start) {
/* no text on previous line, start on next line */
- Debugger();
chunk->start_left = tv->padding.left;
chunk->end_right = chunk->start_left;
chunk->start_line++;
@@ -955,7 +933,7 @@ done_chunks:
tv->scrolled.bottom = tv->scrolled.top + tv->bounds.bottom;
}
- if (tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
+ if (0 && tv->autoscroll && (tv->scrolled.bottom > tv->view_height)) {
tv->scrolled.bottom = tv->view_height;
tv->scrolled.top = tv->scrolled.bottom - tv->bounds.bottom;
}
@@ -964,7 +942,7 @@ done_chunks:
}
bool
-TVDrawVisibleChunks(struct TVRec *tv)
+TVDrawVisibleLines(struct TVRec *tv)
{
struct TVSlab *text_slab;
struct TVChunk *chunk;
@@ -983,15 +961,19 @@ TVDrawVisibleChunks(struct TVRec *tv)
text_slab = TVGetTextSlab(tv, chunk->start);
text_slab_data = *(text_slab->data);
+ tv->top_line = 0;
+ tv->bottom_line = 0;
+
for (linen = 1; linen <= tv->nlines; linen++) {
line = TVGetLine(tv, linen);
- if (tv->scrolled.top + line->rect.bottom < 0)
+ if (tv->scrolled.top + line->rect.bottom < 0L)
+ /* is not in the view at all yet */
continue;
-#if 0
- if (tv->scrolled.bottom < line->rect.top)
+ if (tv->scrolled.top + line->rect.top >= tv->view_height)
+ /* is further down than the view */
break;
-#endif
+
while (chunk && chunk->end_line < linen) {
chunk = TVGetChunk(tv, chunk->next_chunk);
if (!chunk)
@@ -1018,12 +1000,38 @@ TVDrawVisibleChunks(struct TVRec *tv)
end = line->end;
else
end = chunk->end;
+
+ if (tv->top_line == 0 && (tv->scrolled.top + line->rect.top +
+ ((line->rect.bottom - line->rect.top) / 2) > 0))
+ /* this line is more than half visible at the top */
+ tv->top_line = linen;
+ if ((tv->scrolled.top + line->rect.top +
+ ((line->rect.bottom - line->rect.top) / 2) < tv->view_height))
+ /* this line is more than half visible at the bottom */
+ tv->bottom_line = linen;
+
top = tv->scrolled.top + line->rect.bottom - line->baseline;
MoveTo(left, top);
+
+#ifdef DEBUG_DRAWING
+ {
+ Rect r;
+ struct TVStyle ls;
+ char l[10];
+ sprintf(l, "%d", linen);
+ ls.font = geneva;
+ ls.size = 10;
+ ls.style = 0;
+ TVApplyStyle(&ls);
+ DrawText(l, 0, strlen(l));
+ TVApplyStyle(style);
+ MoveTo(left + 20, top);
+ }
+#endif
DrawText(text_slab_data + (start - text_slab->base), 0,
end - start);
-
+
if (end < chunk->end)
/* span to next line */
break;
@@ -1054,10 +1062,8 @@ void
TVScrollUnlocked(struct TVRec *tv, short horiz, short vert)
{
short top, left;
- BigRect rvacated;
Rect was, sect;
RgnHandle vacated;
- struct TVChunk *chunk, *start, *last = NULL;
if (!horiz && !vert)
return;
@@ -1083,27 +1089,16 @@ TVScrollUnlocked(struct TVRec *tv, short horiz, short
tv->scrolled.right += horiz;
/* TODO: bound */
+#if 0
+ /* TODO: scrollrect, mark lines in vacated space dirty, draw those */
vacated = NewRgn();
left = tv->scrolled.left - was.left;
top = tv->scrolled.top - was.top;
ScrollRect(&tv->canvas.portRect, left, top, vacated);
-
- rvacated = tv->scrolled;
- rvacated.top = tv->scrolled.bottom;
- rvacated.bottom = rvacated.top - top;
-
-#if 0
- for (chunk = TVGetChunk(tv, tv->first_chunk); chunk != NULL;
- chunk = TVGetChunk(tv, chunk->next_chunk)) {
- if (chunk->rect.bottom < rvacated.top)
- continue;
- if (chunk->rect.top > rvacated.bottom)
- break;
- //TVDrawChunk(tv, chunk);
- }
-#endif
-
DisposeRgn(vacated);
+#endif
+
+ TVDrawVisibleLines(tv);
TVRevealCanvas(tv);
}
--- textview.h Wed Oct 16 11:35:11 2024
+++ textview.h Thu Oct 17 10:17:51 2024
@@ -84,7 +84,7 @@ struct TVSlab {
struct TVRec {
/* bounds of where we live in window's port */
Rect view;
- unsigned long view_height;
+ long view_height;
/* full size of all text, bottom grows as more text is added */
BigRect bounds;
/* padding into bounds where we should start text */
@@ -102,7 +102,7 @@ struct TVRec {
struct TVStyle styles[32];
unsigned char cur_style;
- struct TVSlab text_slabs[32];
+ struct TVSlab text_slabs[64];
unsigned char last_text_slab;
unsigned long text_length;
@@ -114,6 +114,8 @@ struct TVRec {
/* each lines item holds 1000 TVLine records */
Handle lines[16];
unsigned short nlines;
+ unsigned short top_line;
+ unsigned short bottom_line;
char unlock_refcnt;
bool autoscroll;