View Raw SPL
/*****************************************************************************
*                                                                            *
*   PROTECTWIN.SPL Copyright (C) 2009-2010 DSP Development Corporation       *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:         Randy Race                                               *
*                                                                            *
*   Synopsis:       Protects a window so it can be unprotected               *
*                                                                            *
*   Revisions:      23 Jun 2009  RRR  Creation - from MISC.MAC               *
*                   15 Feb 2010  RRR  multiple windows                       *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_PROTECTWIN

    PROTECTWIN

    Purpose: Protects a Window from updating so it can be unproteced.

    Syntax:  PROTECTWIN(win)

                win - Optional, one or more Windows. The Windows to protect. 
                      Defaults to the current Window.

    Returns: Nothing, the Windows areprotected and no longer automatically
             update.

    Example:
             W1: gnorm(1000, 1)
             W2: integ(W1)
             protectwin(W2)

             W1 contains 1000 samples of random noise and W2 performs an
             integration of the noise. W2 is protected such that if W1
             changes, W2 no longer updates. The formula for W2 becomes:

             Protected: integ(w1)

    Example:
             unprotectwin(w2)

             Re-establishes the original formula and allows W2 to update
             when W1 changes.

    Remarks:
             A protected Window is removed from the dependency list so that
             it is no longer dependent on any Window.

             Unlike PROTECT, PROTECTWIN protects a window such that
             UNPROTECTWIN will unprotect the Window with the original
             formula.

             Use UNPROTECTWIN to re-establish the original Window formula
             and dependencies.

    See Also:
             Protect
             Unprotectwin
             Winlock
#endif



/* protect windows so they do not update */
protectwin(argv)
{
        local i, win;

        if (argc < 1)
        {
                /* protect current window */
                protectwin_protect(refwin(w0));
        }
        else
        {
                /* protect list of windows */
                loop (i = 1..argc)
                {
                        win = getargv(i);
                        protectwin_protect(win);
                }
        }
}


/* performs protection */
protectwin_protect(win)
{
        local f, p, cmd, wcmd;

        if (iswindow(win))
        {
                /* get current formula */
                f = getwform(win);

                /* see if we are already protected */
                p = strextract(f, 1, 11);
                
                if (p != "Protected: ")
                {
                        /* ok to protect */
                        cmd = sprintf("Protected: %s", f);
                        wcmd = sprintf("protect(w%d, cmd)", getwnum(win));
                        eval(wcmd);
                }                
        }
}