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

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

options:no_meter

@init

str = 0;
laststr = 1;
flags = 0;
lastflags = 0;
has_cap = 0;
lastw = 0;
lasth = 0;

@block

@sample

@gfx 400 400

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;
  );
);

get_track_notes(str, flags);
strcmp(str, laststr) || flags != lastflags ||
gfx_w != lastw || gfx_h != lasth ?
(
  strcpy(laststr, str);
  lastflags = flags;
  lastw = gfx_w;
  lasth = gfx_h;

  !strcmp(str, "") ? strcpy(str, " "); // force frame draw

  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;
  yo = ch/2;
  gfx_x = xo;
  gfx_y = yo/2;

  i = 0;
  len = strlen(str);

  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();
);

