home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / os2tk20 / c / samples / bidi / constr.c__ / CONSTR.C
Encoding:
C/C++ Source or Header  |  1992-07-15  |  12.1 KB  |  355 lines

  1. /*static char *SCCSID = "@(#)constr.c    6.1 92/02/19";*/
  2. /*************************************************************************
  3.  * File Name     : CONSTR.C
  4.  *
  5.  * Description   : This sample program demonstrates how to use the API
  6.  *                 NlsConvertBidiString. This Bidirectional (Bidi) API
  7.  *                 is use to convert the bidi string from one set of
  8.  *                 attributes (FromAttr, which describe the Bidi
  9.  *                 attributes of the input buffer) to another set of
  10.  *                 attributes (ToAttr, wich describes the Bidi attibutes
  11.  *                 of the output buffer).
  12.  *
  13.  * Concepts      : When there is a change between the FromAttr and ToAttr
  14.  *                 the API does the necessary to convert the input buffer
  15.  *                 to the ToAttr format, and put it in the output buffer.
  16.  *                 If FromAttr equals to ToAttr, no Bidi conversion will
  17.  *                 take place, and the input Buffer will be copied into
  18.  *                 the output buffer.
  19.  *
  20.  * API's         : NlsSetBidiAtt
  21.  *                 NlsQueryBidiAtt
  22.  *                 NlsConvertBidiString
  23.  *                 VioWrtCharStr
  24.  *                 VioScrollUp
  25.  *                 VioSetCurType
  26.  *
  27.  * Required files: To build and run this sample code the following files
  28.  *                 are needed:
  29.  *                 - constr.C
  30.  *                 - constr.DEF
  31.  *                 - constr.MAK
  32.  *                 - constr.LNK
  33.  *
  34.  *  Copyright (C) 1991 IBM Corporation
  35.  *
  36.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  37.  *      sample code created by IBM Corporation. This sample code is not
  38.  *      part of any standard or IBM product and is provided to you solely
  39.  *      for  the purpose of assisting you in the development of your
  40.  *      applications.  The code is provided "AS IS", without
  41.  *      warranty of any kind.  IBM shall not be liable for any damages
  42.  *      arising out of your use of the sample code, even if they have been
  43.  *      advised of the possibility of such damages.                                                    *
  44.  ************************************************************************/
  45. #define INCL_BASE
  46. /* To include the neccessary header files for Bidi */
  47. #define INCL_DOSERRORS
  48. #define INCL_BDCALLS
  49.  
  50. #include <string.h>
  51. #include <os2.h>
  52. #include <conio.h>
  53.  
  54. VOID Initialize(VOID);
  55. VOID ConvertAndDisplay(VOID);
  56. VOID SetCur(INT);
  57.  
  58. /* Define a variable to save the session Bidi Attributes in it */
  59. ULONG ulOriginalBidiAttr;
  60.  
  61. /* Define the area where we will get the result
  62.  * of what we want to query from the session   */
  63. BDKVCB bdkvcbSetAttr;
  64.  
  65. /* Define the parameter to Hide and Restore the cursor */
  66. #define HIDE   -1
  67. #define RESTORE 0
  68.  
  69. /* Define the buffer for the string to be displayed */
  70. CHAR cString[20];
  71.  
  72. /*************************************************************************
  73.  * Name          : Main
  74.  *
  75.  * Description   : This is the main part of the program, it first clear
  76.  *                 the screen and then hide the cursor. The string to be
  77.  *                 converted will be displayed before and after the API
  78.  *                 NlsConvertBidiString is called.
  79.  *
  80.  * API's         : NlsSetBidiAtt
  81.  *                 VioScrollUp
  82.  *
  83.  * Parameters    : This program does not take any command line parameters
  84.  *
  85.  * Returns       : None
  86.  *
  87.  ************************************************************************/
  88.  
  89. INT main(SHORT sArgc,CHAR **ppArgv)
  90. {
  91.    APIRET RC;
  92.  
  93.    /* clear the screen  */
  94.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  95.  
  96.    /* hide the cursor */
  97.    SetCur(HIDE);
  98.  
  99.    /* Write the static text to the screen and the string that
  100.     * is needed to be shaped */
  101.    Initialize();
  102.  
  103.    /* Call the API to shape the string and write it to the screen */
  104.    ConvertAndDisplay();
  105.  
  106.    /* Wait for Enter key to end the program */
  107.  
  108.    VioWrtCharStr("Press Enter to end the program", 32, 23, 26, 0l);
  109.    getchar();
  110.  
  111.    /* Restore the session Bidi attributes  */
  112.    bdkvcbSetAttr.BDAtts = ulOriginalBidiAttr ;
  113.  
  114.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  115.    if( RC != 0)
  116.    {
  117.      printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  118.      exit(1);
  119.    }
  120.  
  121.    /* clear the screen before exit */
  122.    VioScrollUp(0, 0, 24, 79, -1," \x07", 0);
  123.  
  124.    /* restore the cursor position */
  125.    SetCur(RESTORE);
  126.  
  127.    return (0);
  128. }
  129. /*************************************************************************
  130.  * Name          : INITIALIZE
  131.  *
  132.  * Description   : This function does save the current Bidi attributes of
  133.  *                 the session in a global variable, and set session with
  134.  *                 its own Bidi attributes to protect the displayed shaped
  135.  *                 string from been processed again according to the session
  136.  *                 Bidi attributes.
  137.  *                 Then the function displays some static text and also the
  138.  *                 String to be converted before calling the API.
  139.  *
  140.  * API's         : NlsQueryBidiAtt
  141.  *                 NlsSetBidiAtt
  142.  *                 VioWrtCharStr
  143.  *
  144.  * Parameters    : This program does not take any command line parameters
  145.  *
  146.  * Returns       : None
  147.  *
  148.  ************************************************************************/
  149. VOID Initialize(VOID)
  150. {
  151.    INT iI, iRow, iCol;
  152.  
  153.    APIRET  RC;
  154.  
  155.    /* define structures to be array of strings to contain the static
  156.     * text to be wirtten on the screen  */
  157.    struct {
  158.           CHAR cScreen[40];
  159.           }mssg1[3];
  160.  
  161.    struct {
  162.            CHAR cScreen[45];
  163.            }mssg2[3];
  164.  
  165.    /* First we need to set the session Bidi attributes to have the
  166.     * shaping mode as Passthru. This will let the Bidi support
  167.     * to display the characters on the screen as they are.
  168.     * i.e without any additional shape processing.
  169.     * This way, when the application does its own shaping processing
  170.     * the output characters to the screen will not be corrupted. */
  171.  
  172.    /* The length here is 8 to query only on the Bidi attribute   */
  173.  
  174.    bdkvcbSetAttr.BDLength = 8;
  175.  
  176.    RC = NlsQueryBidiAtt( 0L,(PRETINFO)&bdkvcbSetAttr);
  177.  
  178.    if(RC != 0)
  179.    {
  180.       printf("\n\nError in query the Bidi Attributes RC=%ld\n",RC);
  181.       exit(1);
  182.    }
  183.  
  184.    /* Now we save the initial value of the Bidi attributes so that
  185.     * so that we set back the session with it before the program terminates*/
  186.  
  187.    ulOriginalBidiAttr = bdkvcbSetAttr.BDAtts;
  188.  
  189.   /* Here we set the shaping byte value to passthru */
  190.  
  191.    bdkvcbSetAttr.BDAtts = BD_LEVEL |
  192.                           BD_SUPPORT |
  193.                           BDORIENT_LTR |
  194.                           BDNUM_PASSTHRU |
  195.                           BDCSD_PASSTHRU;
  196.  
  197.   /* Now let's set the session Bidi attributes with the new value  */
  198.  
  199.    RC = NlsSetBidiAtt( 0L, (PSETINFO)&bdkvcbSetAttr);
  200.    if( RC != 0)
  201.    {
  202.        printf("\n\nCannot set the session Bidi Attributes RC=%ld\n",RC);
  203.        exit(1);
  204.    }
  205.  
  206.    /* Initialize the structures with the characters to be displayed */
  207.    strcpy(mssg1[0].cScreen, "Here we will display the Bidi string,");
  208.    strcpy(mssg1[1].cScreen, " its Bidi attr are RTL, Arabic Num,");
  209.    strcpy(mssg1[2].cScreen, "    and Base shapes characters.   ");
  210.  
  211.    /* In this for loop, we increment the iRow after the display of each
  212.     * string in the structure to jump to another line, keeping the value
  213.     * of the iCol value fixed to have the strings indented */
  214.    for(iI = 0, iRow = 2, iCol = 23; iI < 3; iI++, iRow++)
  215.      VioWrtCharStr(mssg1[iI].cScreen, strlen(mssg1[iI].cScreen),
  216.                    iRow, iCol, 0l);
  217.  
  218.    /* Display the string before it is shaped.
  219.     * Note that this is a Right To Left string, because the logical
  220.     * order of the characters stored is from the begining of the Buffer
  221.     * to the end, i.e from low to high memory.
  222.     * Because the screen attributes is LTR, then this RTL string will
  223.     * not be displayed in a readable form, however if we would have
  224.     * set the screen bidi attibutes to RTL before displaying the
  225.     * string, it would be readable.
  226.     * In this example we will keep the screen Bidi attributes to be
  227.     * LTR and we will use the API to convert the string to LTR, so
  228.     * that it can be readable. */
  229.  
  230.    VioWrtCharStr("NOTE: If the current code page is not 864,   ",
  231.                   47, 7, 21, 0L);
  232.    VioWrtCharStr("the following strings will not be readable.  ",
  233.                  47,  8, 21, 0l);
  234.  
  235.    strcpy(cString,"╚╤µ╟σ╠ ┘╤╚Ω ╤Γσ: 1");
  236.    VioWrtCharStr(cString, strlen(cString), 11, 31, 0l);
  237.  
  238.    /* Initialize the second structure with the characters to be displayed */
  239.    strcpy(mssg2[0].cScreen, "Now lets call the  NlsConvertBdiString, to");
  240.    strcpy(mssg2[1].cScreen, "convert the  above string to be  displayed");
  241.    strcpy(mssg2[2].cScreen, "correctly on the current screen attributes");
  242.  
  243.    for(iI = 0, iRow = 14, iCol = 21; iI <= 2; iI++, iRow++)
  244.      VioWrtCharStr(mssg2[iI].cScreen, strlen(mssg2[iI].cScreen),
  245.                    iRow, iCol, 0l);
  246.  
  247. }
  248. /*************************************************************************
  249.  * Name          : ConvertAndDisplay
  250.  *
  251.  * Description   : This function will show how to use the Bidi API
  252.  *                 NslConvertBidiString.
  253.  *                 The function will also displays the string after
  254.  *                 the conversion.
  255.  *
  256.  * API's         : NlsConvertBidiString
  257.  *                 VioWrtCharStr
  258.  *
  259.  * Parameters    : This program does not take any command line parameters
  260.  *
  261.  * Returns       : None
  262.  *
  263.  ************************************************************************/
  264. VOID ConvertAndDisplay(VOID)
  265. {
  266.    APIRET RC;
  267.    INT   iRow, iCol;
  268.    ULONG ulFromAttr,ulToAttr;
  269.    ULONG ulLength;
  270.    ULONG ulIncrement ;
  271.    ULONG ulReserved = 0L;
  272.  
  273.    ulLength = strlen(cString);
  274.  
  275.    /* The string contains successive characters, i.e the cell here is
  276.     * only one character, i.e Increment =1 */
  277.    ulIncrement = 1;
  278.  
  279.    iCol = 31;
  280.  
  281.    /* using the OR operator, we set the bits in FromAttr, to describe
  282.     * the input string attributes, we know that it is a RTL, contains
  283.     * Arabic numeral, and it is in Base shapes */
  284.  
  285.    ulFromAttr= BD_LEVEL |
  286.                BD_SUPPORT |
  287.                BDORIENT_RTL |
  288.                BDNUM_ARABIC |
  289.                BDCSD_BASE ;
  290.  
  291.  
  292.    /* Now let's compose the ToAttr, we want the output string to be
  293.     * LTR, Hindu numeral, and Shaped (Automatic Shape Determination) */
  294.  
  295.    ulToAttr= BD_LEVEL |
  296.              BD_SUPPORT |
  297.              BDORIENT_LTR |
  298.              BDNUM_HINDU |
  299.              BDCSD_AUTOMATIC ;
  300.  
  301.  
  302.   /* calling the API.. to convert the string */
  303.   RC = NlsConvertBidiString (cString,
  304.                              cString,
  305.                              ulLength,
  306.                              ulIncrement,
  307.                              ulFromAttr,
  308.                              ulToAttr,
  309.                              ulReserved);
  310.  
  311.   if (RC!=0)
  312.      printf("RC=%ld\n",RC);
  313.  
  314.   /* NOTE: If the screen attributes were equal Hindi and Automatic, we
  315.    * will obtain the same result on the screen if we called the API
  316.    * NlsInverseString. In that case, the Bidi Screen Handler will
  317.    * do the numeral and the shaping processing.
  318.    * In addition, if the screen attributes (direction) was also RTL,
  319.    * then we do not need to do any processing because the Bidi Screen
  320.    *  Handler will do the string inversion.  */
  321.  
  322.   /* display the target buffer */
  323.     VioWrtCharStr(cString, strlen(cString), 19, iCol, 0l);
  324.  
  325. }
  326.  
  327. /*************************************************************************
  328.  * Name          : SET_CUR
  329.  *
  330.  * Description   : This function hides or restores the cursor position.
  331.  *
  332.  * API's         : VioSetCurType
  333.  *
  334.  * Parameters    : (INT flag)
  335.  *                 flag indicates whether we want ot hide or restore
  336.  *                 the cursor.
  337.  *
  338.  * Returns       : None
  339.  *
  340.  ************************************************************************/
  341.  
  342. VOID SetCur(INT iFlag)
  343. {
  344.    HVIO HVIOhandle = 0;
  345.    VIOCURSORINFO cursor;
  346.  
  347.    cursor.yStart  = 3;
  348.    cursor.cEnd    = 5;
  349.    cursor.cx      = 0;
  350.    cursor.attr    = iFlag;
  351.  
  352.    VioSetCurType(&cursor, HVIOhandle);
  353. }
  354.  
  355.