jcs
/textview
/amendments
/26
Fix some bugs, use Boolean instead of bool to avoid carrying stdint.h
Fix SET_CHUNKPTR_OFFSET mask
Properly draw multiple chunks on one line
Update cur_style properly
canvas is a pointer now so pass it to ClosePort properly
Don't modify view on creation to match bounds like TextEdit does
jcs made amendment 26 over 2 years ago
--- textview.c Fri Oct 18 08:33:32 2024
+++ textview.c Fri Oct 18 15:22:49 2024
@@ -36,11 +36,11 @@ struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsign
struct TVChunk * TVGetChunk(struct TVRec *tv, TVChunkPtr ptr);
struct TVChunk * TVCreateChunk(struct TVRec *tv);
struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line);
-bool TVCalcLines(struct TVRec *tv);
+Boolean 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 TVDrawVisibleLines(struct TVRec *tv);
+Boolean TVDrawVisibleLines(struct TVRec *tv);
void TVUseCanvas(struct TVRec *tv);
void TVRevealCanvas(struct TVRec *tv);
@@ -61,10 +61,6 @@ TVNew(Rect *view, Rect *padding)
tv = *tvh;
tv->view = *view;
- tv->view.left++;
- tv->view.top++;
- tv->view.right -= 1;
- tv->view.bottom -= 1;
tv->padding = *padding;
tv->padding.left++;
@@ -117,6 +113,10 @@ TVNew(Rect *view, Rect *padding)
tv->cur_style = NO_STYLE;
+ TVUseCanvas(tv);
+ EraseRect(&tv->canvas->portRect);
+ TVRevealCanvas(tv);
+
HUnlock(tvh);
return tvh;
@@ -135,7 +135,7 @@ fail:
return NULL;
}
-bool
+Boolean
TVAppend(TVHandle tvh, struct TVStyle *style, char *text,
unsigned long length)
{
@@ -146,7 +146,7 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
unsigned long avail;
short width, prev_style_idx;
char *text_slab_data;
- bool new_chunk, ret;
+ Boolean new_chunk, ret;
tv = TVLock(tvh);
@@ -221,7 +221,7 @@ append_done:
}
void
-TVAutoScroll(TVHandle tvh, bool scroll)
+TVAutoScroll(TVHandle tvh, Boolean scroll)
{
struct TVRec *tv;
@@ -270,7 +270,7 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller
}
void
-TVClick(TVHandle tvh, Point p, bool extend)
+TVClick(TVHandle tvh, Point p, Boolean extend)
{
struct TVRec *tv;
struct TVChunk *chunk;
@@ -315,7 +315,7 @@ TVTrackScrollControl(ControlHandle control, short part
KeyMap km;
long n, top;
unsigned short linen;
- bool horiz, cmd_down;
+ Boolean horiz, cmd_down;
if (tv_track_control == NULL) {
/* TVTrackScrollControl without TVSetTrackScrollControl */
@@ -507,7 +507,7 @@ TVDispose(TVHandle tvh)
DisposeHandle(tv->text_slabs[0].data);
DisposeHandle(tv->line_groups[0]);
- ClosePort(&tv->canvas);
+ ClosePort(tv->canvas);
DisposeHandle(tv->canvas_baseaddr);
DisposeHandle(tvh);
}
@@ -756,9 +756,12 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
return cur_style;
}
- for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++)
- if (ARE_STYLES_EQUAL(&tv->styles[n], style))
+ for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++) {
+ if (ARE_STYLES_EQUAL(&tv->styles[n], style)) {
+ tv->cur_style = n;
return &tv->styles[n];
+ }
+ }
/* allocate a new one */
for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++) {
@@ -780,7 +783,7 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
return NULL;
}
-bool
+Boolean
TVCalcLines(struct TVRec *tv)
{
struct TVSlab *text_slab;
@@ -792,7 +795,7 @@ TVCalcLines(struct TVRec *tv)
short width, space_width, height;
char *text_slab_data;
unsigned char c;
- bool new_line;
+ Boolean new_line;
/* find first dirty chunk */
last_chunk = TVGetChunk(tv, tv->last_chunk);
@@ -887,6 +890,7 @@ TVCalcLines(struct TVRec *tv)
if (new_line) {
new_line = false;
space_width = 0;
+ last_space = 0;
line->end = n;
line->rect.right = chunk->end_right;
@@ -953,7 +957,7 @@ done_chunks:
return true;
}
-bool
+Boolean
TVDrawVisibleLines(struct TVRec *tv)
{
struct TVSlab *text_slab;
@@ -965,7 +969,7 @@ TVDrawVisibleLines(struct TVRec *tv)
unsigned long bottom, start, end, len;
unsigned short chars, linen;
long left;
- bool ret;
+ Boolean ret;
TVUseCanvas(tv);
EraseRect(&tv->canvas->portRect);
@@ -1002,6 +1006,7 @@ TVDrawVisibleLines(struct TVRec *tv)
}
}
+prep_chunk:
text_slab = TVGetTextSlab(tv, chunk->start);
if (text_slab == NULL)
goto draw_done;
@@ -1036,12 +1041,19 @@ TVDrawVisibleLines(struct TVRec *tv)
/* this line is more than half visible at the bottom */
tv->bottom_line = linen;
- if (end == start)
- continue;
-
- MoveTo(left, tv->scrolled.top + line->rect.bottom - line->baseline);
- DrawText(text_slab_data + (start - text_slab->base), 0,
- end - start);
+ if (end != start) {
+ MoveTo(left, tv->scrolled.top + line->rect.bottom -
+ line->baseline);
+ DrawText(text_slab_data + (start - text_slab->base), 0,
+ end - start);
+ }
+
+ if (chunk->end_line == linen && chunk->end < line->end) {
+ /* more chunks on this line */
+ chunk = TVGetChunk(tv, chunk->next_chunk);
+ if (chunk != NULL)
+ goto prep_chunk;
+ }
}
ret = true;
--- textview.h Fri Oct 18 08:28:07 2024
+++ textview.h Fri Oct 18 15:17:34 2024
@@ -16,7 +16,6 @@
#include <stddef.h>
#include <limits.h>
-#include "stdint.h"
struct BigRect {
long top;
@@ -46,12 +45,12 @@ typedef unsigned long TVChunkPtr;
/* high 2 are slab index, low 6 byte offset */
#define CHUNKPTR_SLAB(x) (((x) >> 24) & 0xff)
-#define CHUNKPTR_OFFSET(x) ((x) & 0xffffff)
+#define CHUNKPTR_OFFSET(x) ((x) & 0x00ffffff)
#define SET_CHUNKPTR_SLAB(ptr, x) \
- { (ptr) = ((unsigned long)(x) << 24) | (ptr) & 0xff; }
+ { (ptr) = ((unsigned long)(x) << 24) | ((ptr) & 0xff); }
#define SET_CHUNKPTR_OFFSET(ptr, x) \
- { (ptr) = (ptr) & 0xff000000 | (x) & 0xff; }
+ { (ptr) = ((ptr) & 0xff000000) | ((x) & 0x00ffffff); }
struct TVChunk {
TVChunkPtr ptr;
@@ -64,7 +63,7 @@ struct TVChunk {
unsigned short start_left;
unsigned short end_right;
unsigned char style;
- bool dirty;
+ Boolean dirty;
};
struct TVLine {
@@ -121,18 +120,18 @@ struct TVRec {
unsigned short bottom_line;
char unlock_refcnt;
- bool autoscroll;
+ Boolean autoscroll;
};
typedef struct TVRec ** TVHandle;
TVHandle TVNew(Rect *view, Rect *padding);
void TVDispose(TVHandle tvh);
-void TVAutoScroll(TVHandle tvh, bool scroll);
-bool TVAppend(TVHandle tvh, struct TVStyle *style, char *text,
+void TVAutoScroll(TVHandle tvh, Boolean scroll);
+Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text,
unsigned long length);
void TVUpdate(TVHandle tvh);
void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller);
-void TVClick(TVHandle tvh, Point p, bool extend);
+void TVClick(TVHandle tvh, Point p, Boolean extend);
void TVScroll(TVHandle tvh, short horiz, short vert);
void TVClear(TVHandle tvh);