KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Image scroll utility
PRODUCT: 4D | VERSION: 13.4 | PLATFORM: Mac & Win
Published On: January 16, 2014

The need can arise to be able to programmatically scroll an image on a 4D form picture object. Programmatically scrolling an image can be done using the commands OBJECT GET SCROLL POSITION and OBJECT SET SCROLL POSITION. The challenge in programmatic scrolling an image is not to "over scroll" or scroll beyond the limits of the image.

There is no single command in 4D that reveals the scrolling limit of an image in a 4D form picture object. The task to prevent over scrolling an image it to dynamically compare the size of the form picture object combined with the current scroll position and the size of the image in memory.

The utility method below accomplishes this task for vertical scrolling of an image presented in a 4D form picture object assuming it is using the Truncated display format. Only the Truncated display formate displays the image without modification of its aspect ratio. In addition to ensuring that the limits of the image are not over scrolled, when the top or bottom limit is reached it properly enables and disables the buttons used for scrolling.

If (True)
   If (False)
      Begin SQL
         /*
         Name: SCROLL_Image
         Path: SCROLL_Image

         Purpose: Scroll and image and keep it within picture object height
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
      //===================== Declare Variables ==================================
      //method_parameters_declarations
   C_TEXT($ObjName_T;$1)
   C_LONGINT($ImgHight_L;$2)
   C_BOOLEAN($ScrollDown_B;$3)
   C_TEXT($ScrollUpBtnName_T;$4)
   C_TEXT($ScrollDnBtnName_T;$5)
      //----------------------------------------------------------------------------
      //method_wide_constants_declarations
      //----------------------------------------------------------------------------
      //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L;\
   $OBJ_H_L;$OBJ_Left_L;$OBJ_Top_L;$OBJ_Right_L;$OBJ_Bottom_L)

End if
   //====================== Initialize and Setup ================================

$Params_L:=Count parameters
If (Asserted($Params_L>=5;"Bad parameter count!"))

   $ObjName_T:=$1
   $ImgHight_L:=$2
   $ScrollDown_B:=$3
   $ScrollUpBtnName_T:=$4
   $ScrollDnBtnName_T:=$5

   //======================== Method Actions ==================================

    If ($ScrollDown_B)
      OBJECT GET COORDINATES(*;$ObjName_T;\
         $OBJ_Left_L;$OBJ_Top_L;$OBJ_Right_L;$OBJ_Bottom_L)
      $OBJ_H_L:=$OBJ_Bottom_L-$OBJ_Top_L

      OBJECT GET SCROLL POSITION(*;$ObjName_T;$Ndx)
      If (($Ndx+$OBJ_H_L)<$ImgHight_L)
         If (($Ndx+20+$OBJ_H_L)<$ImgHight_L)
            OBJECT SET SCROLL POSITION(*;$ObjName_T;$Ndx+20;*)
         Else
            $Ndx:=$Ndx+($ImgHight_L-($OBJ_H_L+$Ndx))
            OBJECT SET SCROLL POSITION(*;$ObjName_T;$Ndx;*)
         End if
         OBJECT SET ENABLED(*;$ScrollUpBtnName_T;True)
      End if

      OBJECT GET SCROLL POSITION(*;$ObjName_T;$Ndx)
      OBJECT SET ENABLED(*;$ScrollDnBtnName_T;($Ndx+$OBJ_H_L)<$ImgHight_L)
    Else
      OBJECT GET SCROLL POSITION(*;$ObjName_T;$Ndx)
      If ($Ndx>0)
         If ($Ndx>=20)
            OBJECT SET SCROLL POSITION(*;$ObjName_T;$Ndx-20;*)
         Else
            OBJECT SET SCROLL POSITION(*;$ObjName_T;-$Ndx;*)
            OBJECT SET ENABLED(*;$ScrollUpBtnName_T;False)
         End if

          OBJECT SET ENABLED(*;$ScrollDnBtnName_T;True)
      End if
    End if

    //======================== Clean up and Exit =================================

End if


On a form that has two button objects named "BTN_ScrollUp" and "BTN_ScrollDown" the method would be called as shown in the code snippet below.

// True is scroll down, False is scroll up.
//
SCROLL_Image ("MyPic_G";ImageH_L;True;"BTN_ScrollUp";"BTN_ScrollDown")