View Raw SPL
/*****************************************************************************
*                                                                            *
*   BGSCROLL.SPL Copyright (C) 2024 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Scrolls one or more windows in the background               *
*                                                                            *
*   Revisions:   12 Dec 2024  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_BGSCROLL

    BGSCROLL

    Purpose: Scrolls one or more windows in the background.

    Syntax:  BGSCROLL(win1, win2, ..., winN, factor)

                winN - Optional. A list of windows to scroll. Defaults to
                       the current window.

              factor - Optional. A real, the scroll factor. Defaults to the
                       normal scroll interval for the windows. If FACTOR is
                       0.0, the scrolling is terminated. If FACTOR is 
                       negative, the windows are scrolled left to right.

    Returns: Nothing, the specified windows are scrolled in the background
             until the scroll limit is reached. 

    Example:
             W1: integ(gnorm(100000, 1/10000))

             bgscroll(); 

             The series in W1 is scrolled right to left until it is no
             longer within the plotting area.

    Example:
             W1: integ(gnorm(100000, 1/10000))
             W2: w1 * w1
             W3: movavg(w1, 200)

             bgscroll(w1, w3); 

             The series in W1 and W3 are scrolled right to left until both
             series exit the plotting area.

    Example:
             W1: integ(gnorm(100000, 1/10000))
             W2: w1 * w1
             W3: movavg(w1, 200)

             bgscroll(w1, w3, 1/10); 

             Same as above except the series in W1 and W3 are scrolled right
             to left with a smaller scroll factor.

    Example:
             W1: integ(gnorm(100000, 1/10000))
             W2: w1 * w1
             W3: movavg(w1, 200)

             bgscroll(w1, w3, -1/10); 

             Same as above except the series in W1 and W3 are scrolled left
             to right (reversed).

    Remarks:
             BGSCROLL scrolls one or more windows in the background. The
             windows scroll until all windows leave the plotting viewport.
             Normal processing can occur while the windows scroll.

             If specified, FACTOR is used to scale the default plotting
             scroll factor with:

               scrollfactor = FACTOR * 0.1 * plotwidth

             where PLOTWIDTH is the current X axis plot range:

               plotwidth = getxr() - getxl()

             FACTOR == 1 is the default scrolling factor.

             If FACTOR == 0, the scrolling terminates.

             if FACTOR < 0, the windows scroll left to right.
             
    See Also:
             SCROLLL
             SCROLLR
#endif


static s_form = "", s_fac, s_id;

/* experimental background scroller */
bgscroll(argv)
{
        local j, wlist = "", factor = {}, scrollfun;

        if (argc > 0)
        {
                /* build window list */
                loop (j = 1..argc)
                {
                        if (iswindow(getargv(j)))
                        {
                                wlist = sprintf("%s,%s", wlist, strwin(getargv(j)));
                        }
                        else if (isscalar(getargv(j)))
                        {
                                factor = getargv(j);
                        }
                }
        
                if (strlen(wlist) > 0)
                {
                        /* remove initial comma */
                        wlist = strextract(wlist, 2, -1);
                }
                else
                {
                        /* default to current window */
                        wlist = strwin(w0);
                }
        }
        else
        {
                /* default to current window */
                wlist = strwin(w0);
        }

        /* end any current scrolling */
        bgscrollstop();

        /* factor == 0 stops scrolling */
        if (not(factor == 0))
        {
                if (isempty(factor))
                {
                        /* use window default scrolling, tables scroll up */
                        s_form = sprintf("scrolllu(%s)", wlist);
                }
                else
                {
                        /* negative factor scrolls right to left, tables scroll down */
                        s_form = sprintf("scrolllu(%s, %g)", wlist, factor);
                }

                /* add to background tasks */
                s_id = rttinit("bgscroll_scroll()");
        }
        
        if (outargc > 0)
        {
                return(s_id);
        }
}


/* real time background task */
bgscroll_scroll()
{
        if (strlen(s_form) > 0)
        {
                if (eval(s_form) < 0)
                {
                        /* at end point */
                        bgscrollstop();
                }
        }
        else
        {
                bgscrollstop();
        }
}


/* end scrolling */        
bgscrollstop()
{
        if (s_id > 0)
        {
                /* terminate task */
                rttterm(s_id);

                s_id   = 0;
                s_form = "";
        }
}