jcs
/textview
/amendments
/25
Minor cleanup
jcs made amendment 25 over 2 years ago
--- textview.c Thu Oct 17 23:03:19 2024
+++ textview.c Fri Oct 18 08:33:32 2024
@@ -21,11 +21,6 @@
//#define DEBUG_DRAWING 1
-#define TEXT_SLAB_SIZE (16 * 1024)
-#define CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50)
-#define LINE_GROUP_NLINES 1000
-#define LINE_GROUP_SIZE (sizeof(struct TVLine) * LINE_GROUP_NLINES)
-
#define ARE_STYLES_EQUAL(one, two) ((one)->font == (two)->font && \
(one)->style == (two)->style && (one)->size == (two)->size)
#define IS_STYLE_EMPTY(_style) ((_style)->font == 0 && \
@@ -249,14 +244,16 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller
* redraw again, which can cause a jump if we're trying to keep the
* scrollbar position in the same place (like at the bottom).
*/
- HLock(scroller);
- if ((*scroller)->nextControl != 0)
- Debugger();
- (*scroller)->contrlMin = 0;
max = tv->bounds.bottom - tv->bounds.top -
(tv->view.bottom - tv->view.top);
- if (max > SHRT_MAX)
+ if (max > SHRT_MAX) {
+ /* TODO: shift to /10 scale? */
+ TVUnlock(tv);
Debugger();
+ return;
+ }
+ HLock(scroller);
+ (*scroller)->contrlMin = 0;
(*scroller)->contrlMax = (max > 0 ? max : 0);
SetCtlValue(scroller, -(tv->scrolled.top));
HUnlock(scroller);
@@ -470,7 +467,7 @@ TVClear(TVHandle tvh)
tv->line_groups[n] = NULL;
}
HLock(tv->line_groups[0]);
- memset((char *)*(tv->line_groups[0]), 0, LINE_GROUP_SIZE);
+ memset((char *)*(tv->line_groups[0]), 0, TV_LINE_GROUP_SIZE);
HUnlock(tv->line_groups[0]);
tv->bounds.bottom = tv->padding.top + tv->padding.bottom;
@@ -569,19 +566,15 @@ TVAllocateTextSlab(struct TVRec *tv)
else
slot = tv->last_text_slab + 1;
- if (slot > sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0])) {
+ if (slot > sizeof(tv->text_slabs) / sizeof(tv->text_slabs[0]))
/* no free slots */
- Debugger();
return NULL;
- }
- size = TEXT_SLAB_SIZE;
+ size = TV_TEXT_SLAB_SIZE;
tv->text_slabs[slot].data = NewHandleClear(size);
- if (tv->text_slabs[slot].data == NULL) {
+ if (tv->text_slabs[slot].data == NULL)
/* TODO: try smaller allocations */
- Debugger();
return NULL;
- }
tv->text_slabs[slot].size = size;
tv->text_slabs[slot].pos = 0;
if (slot != 0)
@@ -608,19 +601,15 @@ TVAllocateChunkSlab(struct TVRec *tv)
else
slot = tv->last_chunk_slab + 1;
- if (slot > sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0])) {
+ if (slot > sizeof(tv->chunk_slabs) / sizeof(tv->chunk_slabs[0]))
/* no free slots */
- Debugger();
return NULL;
- }
- size = CHUNK_SLAB_SIZE;
+ size = TV_CHUNK_SLAB_SIZE;
tv->chunk_slabs[slot].data = NewHandleClear(size);
- if (tv->chunk_slabs[slot].data == NULL) {
+ if (tv->chunk_slabs[slot].data == NULL)
/* TODO: try smaller allocations */
- Debugger();
return NULL;
- }
tv->chunk_slabs[slot].size = size;
tv->chunk_slabs[slot].pos = 0;
tv->last_chunk_slab = slot;
@@ -637,7 +626,7 @@ TVAllocateLineGroup(struct TVRec *tv, short groupn)
return NULL;
if (tv->line_groups[groupn] == NULL)
- tv->line_groups[groupn] = NewHandleClear(LINE_GROUP_SIZE);
+ tv->line_groups[groupn] = NewHandleClear(TV_LINE_GROUP_SIZE);
return tv->line_groups[groupn];
}
@@ -657,7 +646,6 @@ TVGetTextSlab(struct TVRec *tv, unsigned long addr)
return slab;
}
- Debugger();
return NULL;
}
@@ -678,8 +666,10 @@ TVGetChunk(struct TVRec *tv, TVChunkPtr ptr)
slab = &tv->chunk_slabs[slabn];
HLock(slab->data);
addr = *(slab->data) + CHUNKPTR_OFFSET(ptr);
- if ((unsigned long)addr % 2 != 0)
+ if ((unsigned long)addr % 2 != 0) {
Debugger();
+ return NULL;
+ }
/* keep slab locked */
@@ -727,11 +717,11 @@ TVGetLine(struct TVRec *tv, unsigned long line)
Handle group;
unsigned short groupn, idx;
- groupn = (line - 1) / LINE_GROUP_NLINES;
+ groupn = (line - 1) / TV_LINE_GROUP_NLINES;
if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0])))
return NULL;
- idx = line - 1 - (groupn * LINE_GROUP_NLINES);
+ idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES);
group = tv->line_groups[groupn];
if (group == NULL) {
@@ -772,21 +762,21 @@ TVFindStyle(struct TVRec *tv, struct TVStyle *style)
/* allocate a new one */
for (n = 0; n < sizeof(tv->styles) / sizeof(tv->styles[0]); n++) {
- if (IS_STYLE_EMPTY(&tv->styles[n])) {
- /* only copy over the important fields, rest will stay 0 */
- tv->styles[n].font = style->font;
- tv->styles[n].style = style->style;
- tv->styles[n].size = style->size;
- cur_style = &tv->styles[n];
- TVApplyStyle(cur_style);
- GetFontInfo(&cur_style->font_info);
- tv->cur_style = n;
- return cur_style;
- }
+ if (!IS_STYLE_EMPTY(&tv->styles[n]))
+ continue;
+
+ /* only copy over the important fields, rest will stay 0 */
+ tv->styles[n].font = style->font;
+ tv->styles[n].style = style->style;
+ tv->styles[n].size = style->size;
+ cur_style = &tv->styles[n];
+ TVApplyStyle(cur_style);
+ GetFontInfo(&cur_style->font_info);
+ tv->cur_style = n;
+ return cur_style;
}
/* no slots left */
- Debugger();
return NULL;
}
@@ -871,7 +861,6 @@ TVCalcLines(struct TVRec *tv)
height = style->font_info.ascent + style->font_info.descent;
for (n = start; n < chunk->end; n++) {
- if (n == 38736) Debugger();
c = text_slab_data[n - text_slab->base];
if (c == '\r') {
--- textview.h Thu Oct 17 17:42:52 2024
+++ textview.h Fri Oct 18 08:28:07 2024
@@ -103,15 +103,19 @@ struct TVRec {
unsigned char cur_style;
struct TVSlab text_slabs[64];
+#define TV_TEXT_SLAB_SIZE (16 * 1024)
unsigned char last_text_slab;
unsigned long text_length;
struct TVSlab chunk_slabs[64];
+#define TV_CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50)
unsigned char last_chunk_slab;
TVChunkPtr first_chunk;
TVChunkPtr last_chunk;
Handle line_groups[16];
+#define TV_LINE_GROUP_NLINES 1000
+#define TV_LINE_GROUP_SIZE (sizeof(struct TVLine) * TV_LINE_GROUP_NLINES)
unsigned short nlines;
unsigned short top_line;
unsigned short bottom_line;