home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / c / samples / bidi / edit.c__ / edit.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  12.4 KB  |  356 lines

  1. /*static char *SCCSID = "@(#)edit.c    6.1 92/02/19";*/
  2. /*************************************************************************
  3.  * File Name     : EDIT.C
  4.  *
  5.  * Description   : This sample program demonstrates how to use the API
  6.  *                 NlsEditShape. This Bidirectional (Bidi) API is an
  7.  *                 Arabic specific one, and it is use to shape
  8.  *                 (according to the value of the CSD byte in the
  9.  *                 Bidi Attributes) one character knowing its surronding
  10.  *                 characters (wich may get changed as well)
  11.  *
  12.  * Concepts      : When using this API in the Edit shape mode (effect =1),
  13.  *                 the current charater is read, as well as the three
  14.  *                 preceeding characters and the two succeeding ones.
  15.  *                 Based on these readings, the shape of the current
  16.  *                 character is determined, also the surrounding characters
  17.  *                 may change in shape to have a homogenous string of
  18.  *                 6 characters. the output is then written to the output
  19.  *                 buffer.
  20.  *                 If the surrounding characters could not be read or
  21.  *                 written by the API, probably because the current
  22.  *                 character is so close to the buffer boundary, the
  23.  *                 the missing characters are assumed to be blanks,
  24.  *                 and shaping contimues. However, if the buffer is
  25.  *                 too small, then the expected output may not be correct,
  26.  *                 that is why a warning error (NO_ERROR_BIDI_RW_INCOMPLETE)
  27.  *                 is returned, but the API does not exit.
  28.  *                 Note that the surrounding characters should have the
  29.  *                 correct shape in order that the current is shaped
  30.  *                 correctly.
  31.  *
  32.  *                 When using this API in the Input shape mode (effect=0),
  33.  *                 the current character is read, as well as its two `
  34.  *                 preceeding characters.Based on this readings and based
  35.  *                 on the value of the CSDState parameter, the current
  36.  *                 character is shaped and its two preceeding characters
  37.  *                 may change.
  38.  *                 To shape an buffer, this API will be called more than
  39.  *                 once to shape each character. On its first invocation,
  40.  *                 the value of CSDState should be Zero. This value will
  41.  *                 change after the API finishes, to the proper value
  42.  *                 needed to shape the next character in the buffer, and
  43.  *                 which will be used by the API the next time it is
  44.  *                 invoked. That is why the value of the CSDState should
  45.  *                 be preserved between calls until shaping the whole
  46.  *                 buffer is complete.
  47.  *
  48.  * API's         : NlsEditShape
  49.  *                 NlsQueryBidiAtt
  50.  *                 NlsSetBidiAtt
  51.  *                 VioWrtCharStr
  52.  *                 VioScrollUp
  53.  *                 VioSetCurType
  54.  *
  55.  * Required files: To build and run this sample code the following files
  56.  *                 are needed:
  57.  *                 - EDIT.C
  58.  *                 - EDIT.DEF
  59.  *                 - EDIT.MAK
  60.  *                 - EDIT.LNK
  61.  *
  62.  *  Copyright (C) 1991 IBM Corporation
  63.  *
  64.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  65.  *      sample code created by IBM Corporation. This sample code is not
  66.  *      part of any standard or IBM product and is provided to you solely
  67.  *      for  the purpose of assisting you in the development of your
  68.  *      applications.  The code is provided "AS IS", without
  69.  *      warranty of any kind.  IBM shall not be liable for any damages
  70.  *      arising out of your use of the sample code, even if they have been
  71.  *      advised of the possibility of such damages.                                                    *
  72.  ************************************************************************/
  73. #define INCL_BASE
  74. /* To include the neccessary header files for Bidi */
  75. #define INCL_DOSERRORS
  76. #define INCL_BDCALLS
  77.  
  78. #include <string.h>
  79. #include <os2.h>
  80. #include <conio.h>
  81.  
  82. VOID Initialize(VOID);
  83. VOID ShapeAndDisplay(VOID);
  84. VOID SetCur(INT);
  85.  
  86. /* Define a variable to save the session Bidi Attributes in it */
  87. ULONG ulOriginalBidiAttr;
  88.  
  89. /* Define the area where we will get the result
  90.  * of what we want to query from the session   */
  91. BDKVCB bdkvcbSetAttr;
  92.  
  93. /* Define the parameter to Hide and Restore the cursor */
  94. #define HIDE   -1
  95. #define RESTORE 0
  96.  
  97. /* Define the buffer for the string to be displayed */
  98.  
  99. CHAR cString[50];
  100.  
  101. /*************************************************************************
  102.  * Name          : Main
  103.  *
  104.  * Description   : This is the main part of the program, it first clear
  105.  *                 the screen and then hide the cursor. The character in
  106.  *                 the string to be shaped will be displayed before and
  107.                    after the NlsEditShape is called.
  108.  *
  109.  * API's         : NlsSetBidiAtt
  110.  *                 VioScrollUp
  111.  *
  112.  * Parameters    : This program does not take any command line parameters
  113.  *
  114.  * Returns       : None
  115.  *
  116.  ************************************************************************/
  117.  
  118. INT main(SHORT sArgc,CHAR **ppArgv)
  119. {
  120.    APIRET RC;
  121.  
  122.    /* clear the screen  */
  123.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  124.  
  125.    /* hide the cursor */
  126.    SetCur(HIDE);
  127.  
  128.    /* Write the static text to the screen and the string that
  129.     * is needed to be shaped */
  130.    Initialize();
  131.  
  132.    /* Call the API to shape the string and wirte it to the screen */
  133.    ShapeAndDisplay();
  134.  
  135.    /* Wait for Enter key to end the program */
  136.  
  137.    VioWrtCharStr("Press Enter to end the program", 32, 23, 23, 0l);
  138.    getchar();
  139.  
  140.    /* Restore the session Bidi attributes  */
  141.    bdkvcbSetAttr.BDAtts = ulOriginalBidiAttr ;
  142.  
  143.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  144.    if( RC != 0)
  145.    {
  146.      printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  147.      exit(1);
  148.    }
  149.  
  150.    /* clear the screen before exit */
  151.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  152.  
  153.    /* restore the cursor position */
  154.    SetCur(RESTORE);
  155.  
  156.    return (0);
  157. }
  158. /*************************************************************************
  159.  * Name          : INITIALIZE
  160.  *
  161.  * Description   : This function does save the current Bidi attributes of
  162.  *                 the session in a global variable, and set session with
  163.  *                 its own Bidi attributes to protect the displayed shaped
  164.  *                 string from been processed again according to the session
  165.  *                 Bidi attributes.
  166.  *                 Then the function displays some static text and also the
  167.  *                 String to be shaped before any shaping processing.
  168.  *
  169.  * API's         : NlsQueryBidiAtt
  170.  *                 NlsSetBidiAtt
  171.  *                 VioWrtCharStr
  172.  *
  173.  * Parameters    : This program does not take any command line parameters
  174.  *
  175.  * Returns       : None
  176.  *
  177.  ************************************************************************/
  178. VOID Initialize(VOID)
  179. {
  180.    INT iI, iRow, iCol;
  181.  
  182.    APIRET  RC;
  183.  
  184.    /* define structures to be array of strings to contain the static
  185.     * text to be wirtten on the screen  */
  186.    struct {
  187.           CHAR cScreen[40];
  188.           }mssg1[4];
  189.  
  190.    struct {
  191.            CHAR cScreen[15];
  192.            }mssg2[3];
  193.  
  194.    /* First we need to set the session Bidi attributes to have the
  195.     * shaping mode as Passthru. This will let the Bidi support
  196.     * to display the characters on the screen as they are.
  197.     * i.e without any additional shape processing.
  198.     * This way, when the application does its own shaping processing
  199.     * the output characters to the screen will not be corrupted. */
  200.  
  201.    /* The length here is 8 to query only on the Bidi attribute   */
  202.  
  203.    bdkvcbSetAttr.BDLength = 8;
  204.  
  205.    RC = NlsQueryBidiAtt( 0L,(PRETINFO)&bdkvcbSetAttr);
  206.  
  207.    if(RC != 0)
  208.    {
  209.       printf("\n\nError in query the Bidi Attributes RC=%ld\n",RC);
  210.       exit(1);
  211.    }
  212.  
  213.    /* Now we save the initial value of the Bidi attributes so that
  214.     * so that we set back the session with it before the program terminates*/
  215.  
  216.    ulOriginalBidiAttr = bdkvcbSetAttr.BDAtts;
  217.  
  218.   /* Here we set the shaping byte value to passthru */
  219.  
  220.    bdkvcbSetAttr.BDAtts = BD_LEVEL |
  221.                           BD_SUPPORT |
  222.                           BDORIENT_LTR |
  223.                           BDNUM_PASSTHRU |
  224.                           BDCSD_PASSTHRU;
  225.  
  226.   /* Now let's set the session Bidi attributes with the new value  */
  227.  
  228.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  229.    if( RC != 0)
  230.    {
  231.        printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  232.        exit(1);
  233.    }
  234.  
  235.    /* Initialize the structures with the characters to be displayed */
  236.    strcpy(mssg1[0].cScreen, "The original incorrect string is :");
  237.    strcpy(mssg1[1].cScreen, "Let's insert a space in the middle.");
  238.    strcpy(mssg1[2].cScreen, "Now, to correct the output, we use");
  239.    strcpy(mssg1[3].cScreen, "         NlsEditShape.            ");
  240.  
  241.    /* In this for loop, we increment the iRow after the display of each
  242.     * string in the structure to jump to another line, keeping the value
  243.     * of the iCol value fixed to have the strings indented */
  244.    for(iI = 0, iRow = 4, iCol = 23; iI < 3; iI++, iRow=iRow+5)
  245.       VioWrtCharStr(mssg1[iI].cScreen, strlen(mssg1[iI].cScreen),
  246.                     iRow, iCol, 0l);
  247.    /* write the last message */
  248.    VioWrtCharStr(mssg1[3].cScreen, strlen(mssg1[3].cScreen),
  249.                  iRow-4, iCol, 0l);
  250.  
  251.  
  252.    /* Initialize the second structure with the characters to be displayed */
  253.    strcpy(mssg2[0].cScreen, "÷╚╤∞╠σ¿µ╤╚  ");
  254.    strcpy(mssg2[1].cScreen, "÷╚╤∞ ╠σ¿µ╤╚ ");
  255.  
  256.    /* This is the string that is going to be passed to NlsEditShape */
  257.    strcpy(cString,"÷╚╤∞ ╠σ¿µ╤╚ ");
  258.  
  259.    for(iI = 0, iRow = 7, iCol = 32; iI <= 1; iI++, iRow = iRow+5)
  260.  
  261.    VioWrtCharStr(mssg2[iI].cScreen, strlen(mssg2[iI].cScreen), iRow, iCol, 0l);
  262.  
  263. }
  264. /*************************************************************************
  265.  * Name          : SHAPEANDDISPLAY
  266.  *
  267.  * Description   : This function will show how to use the Bidi API
  268.  *                 NslEditShape, and how to fill the needed structures.
  269.  *                 The function will display the string after it is shaped.
  270.  *
  271.  * API's         : NlsEditShape
  272.  *                 VioWrtCharStr
  273.  *
  274.  * Parameters    : This program does not take any command line parameters
  275.  *
  276.  * Returns       : None
  277.  *
  278.  ************************************************************************/
  279. VOID ShapeAndDisplay(VOID)
  280. {
  281.   APIRET RC;
  282.   ULONG ulBidiAttr;
  283.   ULONG ulEffect=1L;
  284.  
  285.  
  286.   /* Defining the input/output structure*/
  287.   CSDRec  PCSDSourceAndTarget;
  288.  
  289.   ULONG  ulCsdState=0;
  290.   ULONG  ulIncrement =1L;
  291.  
  292.   ulBidiAttr =  BD_LEVEL
  293.               | BD_LEVEL
  294.               | BDORIENT_LTR
  295.               | BDNUM_PASSTHRU
  296.               | BDCSD_AUTOMATIC;
  297.  
  298.   /* This value is reserved to be always 16 */
  299.   PCSDSourceAndTarget.RecLength = 16L;
  300.  
  301.   /* Here we point to the character were we want to shape, in
  302.    * our case is the character that is newly introduced in
  303.    * the string .. i.e the blank.  */
  304.   PCSDSourceAndTarget.BufferIndex = 4L;
  305.  
  306.   /* The length of the whole buffer */
  307.   PCSDSourceAndTarget.BufferLength = strlen(cString);
  308.  
  309.   /* assigne the address of the string */
  310.   PCSDSourceAndTarget.Buffer = (PCHAR)cString;
  311.  
  312.   RC = NlsEditShape(ulBidiAttr,
  313.                     ulEffect,
  314.                     &PCSDSourceAndTarget,
  315.                     &PCSDSourceAndTarget,
  316.                     &ulCsdState,
  317.                     ulIncrement);
  318.  
  319.   if(RC != 0)
  320.   {
  321.     printf ("RC1 =%ld\n",RC);
  322.     exit(1);
  323.   }
  324.  
  325.     VioWrtCharStr(cString, strlen(cString), 18, 32, 0l);
  326. }
  327.  
  328. /*************************************************************************
  329.  * Name          : SET_CUR
  330.  *
  331.  * Description   : This function hides or restores the cursor position.
  332.  *
  333.  * API's         : VioSetCurType
  334.  *
  335.  * Parameters    : (INT flag)
  336.  *                 flag indicates whether we want ot hide or restore
  337.  *                 the cursor.
  338.  *
  339.  * Returns       : None
  340.  *
  341.  ************************************************************************/
  342.  
  343. VOID SetCur(INT iFlag)
  344. {
  345.    HVIO HVIOhandle = 0;
  346.    VIOCURSORINFO cursor;
  347.  
  348.    cursor.yStart  = 3;
  349.    cursor.cEnd    = 5;
  350.    cursor.cx      = 0;
  351.    cursor.attr    = iFlag;
  352.  
  353.    VioSetCurType(&cursor, HVIOhandle);
  354. }
  355.  
  356.