// This effect Copyright (C) 2026 and later Cockos Incorporated
// License: LGPL - http://www.gnu.org/licenses/lgpl.html

desc: TCP Notes Display (Cockos)
//tags: utility track notes
//author: Cockos

options:no_meter

@init

has_cap = 0;

@block

@sample

@gfx 400 400

gfx_clear = $xFFFFFF;
gfx_setfont(1, "Arial", 14);
gfx_set(0, 0, 0);

cw = ch = 0;
gfx_measurestr("0", cw, ch);
lh = ch*5/4;
xo = cw*3;
yo = lh;
gfx_x = xo;
gfx_y = yo;

str = flags = 0;
get_track_notes(str, flags);
!strcmp(str, "") ? strcpy(str, " "); // force frame draw

i = 0;
len = strlen(str);

function is_newline(c)
(
  c == 10 || c == 13;
);
function is_whitespace(c)
(
  c == 32 || c == 9;
);
function charwid(c)
(
  w = h = 0;
  gfx_measurechar(c, w, h);
  w;
);

function draw_str(str, start, end)
(
  while (start < end)
  (
    gfx_drawchar(str_getchar(str, start));
    start = start+1;
  );
);


start = ws = 0;
ex = xo;
while (i < len)
(
  c = str_getchar(str, i);

  draw_line = 0;

  is_newline(c) ?
  (
    draw_line = 1;
    i = i+1;
  ) :
  (
    ex = ex+charwid(c);
    ex >= gfx_w-xo*2 && !is_whitespace(c) ?
    (
      draw_line = 1;
      (flags&8) && ws > start ? i = ws+1;
    );
  );

  draw_line ?
  (
    draw_str(str, start, i);
    start = ws = i;
    ex = xo;
    yo = yo+lh;
    gfx_x = xo;
    gfx_y = yo;
  ) : is_whitespace(c) ?
  (
    ws = i;
  );

  i = i+1;
);

draw_str(str, start, len);

(mouse_cap&1) ?
(
  has_cap = 1;
) : has_cap ?
(
  has_cap = 0;
  edit_track_notes();
);

