Progress
Programming
Handbook


Trigger Scope

The scope of a trigger is that part of an application during which the trigger can be executed. The scope of a definitional trigger is the same as the scope of the widget.

By default, the scope of a run-time trigger is the nearest containing procedure, subprocedure, or trigger block in which it is defined. You might want a run-time trigger to remain active beyond the procedure or trigger that defines it. You can do this by setting up a persistent trigger. Like a definitional trigger, a persistent trigger remains active as long as the widget exists. A persistent trigger allows you to create the equivalent of a definitional trigger for a dynamic widget.

Table 16–6 summarizes the scope for each type of trigger.

Table 16–6: Trigger Scopes 
Trigger Type
Scope
Definitional
The same as the widget for which it is defined.
Run-time, nonpersistent
The procedure, subprocedure, or trigger block in which it is defined.
Run-time, persistent
The same as the widget for which it is defined.

A persistent trigger must consist only of a single RUN statement. The RUN statement can invoke an internal or external procedure. The procedure can have one or more input parameters, but must not have any output or input/output parameters. You must ensure that the specified procedure is available whenever the trigger is invoked. For example, if you use an internal procedure, you must ensure that the trigger is not invoked from another external procedure.

NOTE: If you pass parameters to a persistent trigger procedure, the parameter values are evaluated once when the trigger is defined. They are not reevaluated each time the trigger executes. You cannot, for example, pass the SELF system handle as a parameter to the persistent trigger.

The following procedure creates a button within the CHOOSE trigger for make–butt. Normally, any trigger assigned to the new button remains active only until execution of the CHOOSE trigger ends. To maintain the new trigger beyond the CHOOSE trigger, you must use a persistent trigger:

p-pers1.p
DEFINE VARIABLE num-butts    AS INTEGER INITIAL 0.
DEFINE VARIABLE temp-hand    AS WIDGET-HANDLE.

DEFINE BUTTON make-butt      LABEL "Make new button".

DEFINE FRAME  butt-frame     WITH WIDTH 44.
 
ENABLE make-butt WITH FRAME make-frame.

ON CHOOSE OF make-butt
   DO:
      if num-butts >= 100
      THEN DO:
          MESSAGE "Too many buttons.".
          RETURN NO-APPLY.
      END.
      num-butts = num-butts + 1.
      ASSIGN FRAME butt-frame:HEIGHT-CHARS =
                     TRUNC(((num-butts - 1) / 10), 0) + 2.
      
      CREATE BUTTON temp-hand 
           ASSIGN FRAME = FRAME butt-frame:HANDLE
                  ROW = TRUNCATE((num-butts - 1) / 10, 0) + 1
                  COLUMN = (((num-butts - 1) * 4) MODULO 40) + 1
                  LABEL = STRING(num-butts)
                  SENSITIVE = TRUE
                  VISIBLE = TRUE 
           TRIGGERS:
              ON CHOOSE
                 PERSISTENT RUN but-mess.
           END TRIGGERS.            
   END.
 
VIEW FRAME butt-frame.

WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW.
 
 
PROCEDURE but-mess.  
   
   MESSAGE "You have selected button number" SELF:LABEL
           "of" num-butts.
   
END PROCEDURE. 

The example allows the user to create up to 100 dynamic buttons. Each time the user chooses make–butt, the value of num–butts is incremented, the height of the frame is recalculated to ensure the new button will fit, and the new button is created. When the user chooses one of the dynamic buttons, the procedure displays a message.

If after defining a persistent trigger for an event-widget pair you define a nonpersistent trigger for that same pair, the nonpersistent trigger overrides the persistent trigger. However, after the nonpersistent trigger goes out of scope (or is reverted), the persistent trigger becomes active again.

NOTE: Because persistent triggers remain active beyond the scope of the defining procedure, you must ensure that any variables, frames, or buffers referenced by the trigger are available whenever it is active.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095