| 1 |
/* |
| 2 |
* Copyright (c) 2023-2024 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 |
#ifndef __TEXTVIEW_H__ |
| 18 |
#define __TEXTVIEW_H__ |
| 19 |
|
| 20 |
#include <stddef.h> |
| 21 |
#include <limits.h> |
| 22 |
|
| 23 |
struct BigRect { |
| 24 |
long left; |
| 25 |
long top; |
| 26 |
long right; |
| 27 |
long bottom; |
| 28 |
}; |
| 29 |
typedef struct BigRect BigRect; |
| 30 |
#define BigRectToRect(br, r) SetRect(&(r), (br).left, (br).top, (br).right, (br).bottom) |
| 31 |
#define RectToBigRect(r, br) { \ |
| 32 |
(br).left = (r).left; \ |
| 33 |
(br).top = (r).top; \ |
| 34 |
(br).right = (r).right; \ |
| 35 |
(br).bottom = (r).bottom; } |
| 36 |
|
| 37 |
/* when storing data in slabs, TVSlabLocator stores slab id and offset */ |
| 38 |
typedef unsigned long TVSlabLocator; |
| 39 |
|
| 40 |
/* high 2 bytes are slab id, low 2 bytes are offset */ |
| 41 |
#define SLABLOC_SLABID(x) (((x) >> 16) & 0xffff) |
| 42 |
#define SLABLOC_OFFSET(x) ((x) & 0x0000ffff) |
| 43 |
|
| 44 |
#define SET_SLABLOC_SLABID(loc, x) \ |
| 45 |
{ (loc) = ((unsigned long)(x) << 16) | ((loc) & 0xffff); } |
| 46 |
#define SET_SLABLOC_OFFSET(loc, x) \ |
| 47 |
{ (loc) = ((loc) & 0xffff0000) | ((x) & 0x0000ffff); } |
| 48 |
|
| 49 |
enum { |
| 50 |
TV_ALIGN_LEFT, |
| 51 |
TV_ALIGN_CENTER, |
| 52 |
TV_ALIGN_RIGHT |
| 53 |
}; |
| 54 |
|
| 55 |
struct TVStyle { |
| 56 |
unsigned char font; |
| 57 |
unsigned char style; |
| 58 |
unsigned char size; |
| 59 |
unsigned char align; |
| 60 |
unsigned long tag; |
| 61 |
|
| 62 |
/* these are private and ignored when fed into TVAppend() */ |
| 63 |
BitMap bitmap; |
| 64 |
struct FontInfo font_info; |
| 65 |
unsigned char width_cache[256]; |
| 66 |
TVSlabLocator loc; |
| 67 |
unsigned long bitmap_data_size; |
| 68 |
unsigned char bitmap_data[]; |
| 69 |
}; |
| 70 |
|
| 71 |
struct TVChunk { |
| 72 |
TVSlabLocator loc; |
| 73 |
TVSlabLocator prev_chunk; |
| 74 |
TVSlabLocator next_chunk; |
| 75 |
unsigned long start; |
| 76 |
unsigned long start_line; |
| 77 |
unsigned long end; |
| 78 |
unsigned long end_line; |
| 79 |
unsigned short start_left; |
| 80 |
unsigned short end_right; |
| 81 |
TVSlabLocator style; |
| 82 |
Boolean need_recalc; |
| 83 |
}; |
| 84 |
|
| 85 |
struct TVLine { |
| 86 |
unsigned long start; |
| 87 |
unsigned long end; |
| 88 |
TVSlabLocator chunk; |
| 89 |
BigRect rect; |
| 90 |
unsigned long indent; |
| 91 |
unsigned long nline; |
| 92 |
unsigned short leading; |
| 93 |
unsigned char baseline; |
| 94 |
Boolean grew; |
| 95 |
}; |
| 96 |
|
| 97 |
struct TVSlab { |
| 98 |
unsigned long base; |
| 99 |
unsigned long size; |
| 100 |
unsigned long pos; |
| 101 |
Handle prev_slab; |
| 102 |
Handle next_slab; |
| 103 |
unsigned short id; |
| 104 |
char data[]; |
| 105 |
}; |
| 106 |
|
| 107 |
struct TVRec { |
| 108 |
/* bounds of where we live in window's port */ |
| 109 |
Rect view; |
| 110 |
RgnHandle view_clip; |
| 111 |
|
| 112 |
/* visible rect relative to view */ |
| 113 |
Rect frame; |
| 114 |
|
| 115 |
/* full size of all text, bottom grows as more text is added */ |
| 116 |
BigRect bounds; |
| 117 |
|
| 118 |
/* padding into bounds where we should start text */ |
| 119 |
Rect padding; |
| 120 |
|
| 121 |
/* off-screen canvas where we draw text that is visible */ |
| 122 |
GrafPtr canvas; |
| 123 |
Handle canvas_baseaddr; |
| 124 |
short canvas_refcnt; |
| 125 |
GrafPtr canvas_old_port; |
| 126 |
Boolean canvas_dirty; |
| 127 |
|
| 128 |
/* bounds as it is scrolled */ |
| 129 |
BigRect scrolled; |
| 130 |
|
| 131 |
Handle first_style_slab; |
| 132 |
Handle last_style_slab; |
| 133 |
Handle style_slab_hint; |
| 134 |
#define TV_STYLE_SLAB_SIZE (sizeof(struct TVStyle) * 16) |
| 135 |
TVSlabLocator cur_style; |
| 136 |
|
| 137 |
Handle first_text_slab; |
| 138 |
Handle last_text_slab; |
| 139 |
Handle text_slab_hint; |
| 140 |
#define TV_TEXT_SLAB_SIZE (8 * 1024) |
| 141 |
unsigned long text_length; |
| 142 |
unsigned long last_space; |
| 143 |
short space_width; |
| 144 |
|
| 145 |
Handle first_chunk_slab; |
| 146 |
Handle last_chunk_slab; |
| 147 |
Handle chunk_slab_hint; |
| 148 |
#define TV_CHUNK_SLAB_SIZE (sizeof(struct TVChunk) * 50) |
| 149 |
TVSlabLocator first_chunk; |
| 150 |
TVSlabLocator last_chunk; |
| 151 |
|
| 152 |
Handle line_groups[32]; |
| 153 |
#define TV_LINE_GROUP_NLINES 512 |
| 154 |
#define TV_LINE_GROUP_SIZE (sizeof(struct TVLine) * TV_LINE_GROUP_NLINES) |
| 155 |
unsigned long nlines; |
| 156 |
|
| 157 |
unsigned long sel_start; |
| 158 |
unsigned long sel_end; |
| 159 |
unsigned long last_click_ticks; |
| 160 |
Point last_click_point; |
| 161 |
short click_count; |
| 162 |
Boolean active; |
| 163 |
ControlHandle scroller; |
| 164 |
|
| 165 |
/* number of pixels for each tabstop, usually CharWidth(' ')*4 or 8 */ |
| 166 |
unsigned short tabstop; |
| 167 |
Pattern tab_pat; |
| 168 |
|
| 169 |
Boolean autocalc; |
| 170 |
Boolean pending_redraw; |
| 171 |
unsigned long pending_from_line; |
| 172 |
unsigned long pending_from_end; |
| 173 |
Boolean autoscroll; |
| 174 |
Boolean linewrap; |
| 175 |
char unlock_refcnt; |
| 176 |
}; |
| 177 |
typedef struct TVRec ** TVHandle; |
| 178 |
|
| 179 |
struct TVFindInRectMatch { |
| 180 |
BigRect rect; |
| 181 |
struct TVChunk *chunk; |
| 182 |
struct TVLine *line; |
| 183 |
struct TVStyle *style; |
| 184 |
struct TVSlab *text_slab; |
| 185 |
unsigned long text_start; |
| 186 |
unsigned long text_end; |
| 187 |
BigRect findrect; |
| 188 |
}; |
| 189 |
|
| 190 |
typedef Boolean (*TVFindInRectCallback)(struct TVRec *tv, |
| 191 |
struct TVFindInRectMatch *match, void *cookie); |
| 192 |
|
| 193 |
TVHandle TVNew(Rect *view, Rect *padding); |
| 194 |
void TVDispose(TVHandle tvh); |
| 195 |
Boolean TVAppend(TVHandle tvh, struct TVStyle *style, char *text, |
| 196 |
unsigned long length); |
| 197 |
Boolean TVAppendLine(TVHandle tvh, struct TVStyle *style, char *text, |
| 198 |
unsigned long length); |
| 199 |
Boolean TVAppendBitMap(TVHandle tvh, BitMap *bitmap, |
| 200 |
struct TVStyle *bstyle); |
| 201 |
void TVAutoCalc(TVHandle tvh, Boolean calc); |
| 202 |
void TVAutoScroll(TVHandle tvh, Boolean scroll); |
| 203 |
Boolean TVFindInRect(TVHandle tvh, BigRect *findrect, long startline, |
| 204 |
TVFindInRectCallback callback, void *cookie); |
| 205 |
void TVLineWrap(TVHandle tvh, Boolean wrap); |
| 206 |
void TVTabStop(TVHandle tvh, unsigned short tabstop); |
| 207 |
Boolean TVResize(TVHandle tvh); |
| 208 |
void TVUpdate(TVHandle tvh); |
| 209 |
void TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller); |
| 210 |
void TVClick(TVHandle tvh, Point p, Boolean extend); |
| 211 |
unsigned long TVGetText(TVHandle tvh, char *dst, unsigned long start, |
| 212 |
unsigned long end); |
| 213 |
long TVGetTextOffset(TVHandle tvh, Point p); |
| 214 |
void TVBigRectToLocalRect(TVHandle tvh, BigRect *bigrect, Rect *rect); |
| 215 |
void TVScroll(TVHandle tvh, long horiz, long vert); |
| 216 |
void TVScrollPart(TVHandle tvh, short part, Boolean fine); |
| 217 |
void TVScrollToOffset(TVHandle tvh, unsigned long off); |
| 218 |
Boolean TVCopy(TVHandle tvh); |
| 219 |
void TVSetSelect(TVHandle tvh, unsigned long start, unsigned long end); |
| 220 |
void TVGetSelect(TVHandle tvh, unsigned long *start, unsigned long *end); |
| 221 |
void TVSelectAll(TVHandle tvh); |
| 222 |
void TVActivate(TVHandle tvh, Boolean active); |
| 223 |
void TVSetScrollbar(TVHandle tvh, ControlHandle scroller); |
| 224 |
void TVClear(TVHandle tvh); |
| 225 |
Boolean TVCalcLines(TVHandle tvh); |
| 226 |
|
| 227 |
void TVSetTrackScrollControl(TVHandle tvh); |
| 228 |
pascal void TVTrackScrollControl(ControlHandle control, short part); |
| 229 |
|
| 230 |
/* internal, shared between textview source files */ |
| 231 |
|
| 232 |
//#define USE_CANVAS 1 |
| 233 |
|
| 234 |
/* NOTE: update TVFindStyle when additional fields are added */ |
| 235 |
#define ARE_STYLES_EQUAL(one, two) \ |
| 236 |
((one)->tag == (two)->tag && \ |
| 237 |
(one)->font == (two)->font && \ |
| 238 |
(one)->style == (two)->style && \ |
| 239 |
(one)->size == (two)->size && \ |
| 240 |
(one)->align == (two)->align && \ |
| 241 |
(one)->bitmap_data_size == (two)->bitmap_data_size) |
| 242 |
|
| 243 |
struct TVRec * TVLock(TVHandle tvh); |
| 244 |
void TVUnlock(struct TVRec *); |
| 245 |
Boolean TVSetupCanvas(struct TVRec *tv); |
| 246 |
Handle TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first, |
| 247 |
Handle *last); |
| 248 |
Handle TVAllocateLineGroup(struct TVRec *tv, short groupn); |
| 249 |
struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsigned long addr); |
| 250 |
struct TVChunk * TVGetChunk(struct TVRec *tv, TVSlabLocator loc); |
| 251 |
struct TVChunk * TVCreateChunk(struct TVRec *tv); |
| 252 |
struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line); |
| 253 |
Boolean TVRedrawAppendLocked(struct TVRec *tv, unsigned long fromnline, |
| 254 |
unsigned long fromend); |
| 255 |
Boolean TVAppendLocked(struct TVRec *tv, struct TVStyle *style, char *text, |
| 256 |
unsigned long length, char terminator); |
| 257 |
short TVScrollDivisor(struct TVRec *tv); |
| 258 |
Boolean TVCalcLinesLocked(struct TVRec *tv, Boolean ignore_dirty); |
| 259 |
void TVApplyStyle(struct TVStyle *style); |
| 260 |
struct TVStyle * TVGetStyle(struct TVRec *tv, TVSlabLocator loc); |
| 261 |
struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style); |
| 262 |
void TVScrollLocked(struct TVRec *tv, long horiz, long vert); |
| 263 |
Boolean TVFindInRectLocked(struct TVRec *tv, BigRect *findrect, |
| 264 |
long startline, TVFindInRectCallback callback, void *cookie); |
| 265 |
Boolean TVDrawMatch(struct TVRec *tv, struct TVFindInRectMatch *match, |
| 266 |
void *cookie); |
| 267 |
void TVUseCanvas(struct TVRec *tv); |
| 268 |
void TVRevealCanvas(struct TVRec *tv); |
| 269 |
unsigned char TVCharAt(struct TVRec *tv, unsigned long off); |
| 270 |
short TVCharWidth(struct TVRec *tv, struct TVStyle *style, |
| 271 |
struct TVLine *line, long x, unsigned char c); |
| 272 |
unsigned long TVLineForY(struct TVRec *tv, long y); |
| 273 |
unsigned long TVLineForOffset(struct TVRec *tv, unsigned long off); |
| 274 |
struct TVChunk * TVLineChunk(struct TVRec *tv, struct TVLine *line); |
| 275 |
long TVXForOffset(struct TVRec *tv, struct TVLine *line, unsigned long off); |
| 276 |
long TVOffsetForX(struct TVRec *tv, struct TVLine *line, long x, |
| 277 |
Boolean caret); |
| 278 |
long TVOffsetForPoint(struct TVRec *tv, long h, long v, Boolean caret); |
| 279 |
void TVRedrawRect(struct TVRec *tv, Rect *r, long startline, Boolean erase); |
| 280 |
void TVInvertRange(struct TVRec *tv, unsigned long a, unsigned long b, |
| 281 |
Rect *clip); |
| 282 |
void TVSetSelectLocked(struct TVRec *tv, unsigned long start, |
| 283 |
unsigned long end); |
| 284 |
void TVSelectUnit(struct TVRec *tv, unsigned long off, short unit, |
| 285 |
unsigned long *start, unsigned long *end); |
| 286 |
|
| 287 |
#endif /* __TEXTVIEW__ */ |