home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / GRAPHICS / WINDOWPR.ZIP / TUTOR4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-29  |  12.6 KB  |  419 lines

  1. #include "pro.h"
  2. #include "xglobals.h"
  3. #include "vs.h"
  4. #include "colors.h"
  5.  
  6. char string[2000];
  7.  
  8. int main()
  9. {
  10.     unsigned int window_handle[3], count, row, column, count1;
  11.     unsigned char tile_handle[10];
  12.     char key[10];
  13.  
  14.     /*
  15.      * See tutors 1-3 for an explanation of this section.
  16.     */
  17.     active_attr = WHITE + (BLACK << 4);
  18.     inactive_attr = LIGHTGRAY + (BLACK << 4);
  19.     active_tile_attr = LIGHTGREEN + (BLACK << 4);
  20.     inactive_tile_attr = RED + (BLACK << 4);
  21.     scroll_bars_on = FALSE;
  22.     method = DMA;
  23.     wn_init();
  24.  
  25.     window_handle[0] = wn_createw(25,80,2,3,1,1,20,70,FALSE,HEAD_ON,"Virtual Screen TUTORIAL",NULL);
  26.  
  27.     tile_handle[0] = 0;
  28.     wn_namet(window_handle[0], tile_handle[0], "tile number 1");
  29.  
  30.     tile_handle[1] = wn_createt(window_handle[0], "tile number 2", 80, 25, 1, 1);
  31.     wn_opent(window_handle[0], tile_handle[1]);
  32.     wn_openw(window_handle[0]);
  33.     getch();
  34.  
  35.  
  36.     /*
  37.      * Lesson 1: vs_printf, the logical cursor, and activating tiles
  38.      *
  39.      * vs_printf is similar to printf except that you specify a
  40.      * virtual screen and a foreground and background color.
  41.      * Printing begins at the virtual screen's logical cursor
  42.      * location and the cursor is advanced for each character output.
  43.      * TABS are expanded to 8, CR are ignored and LF puts the cursor
  44.      * at the beginning of the next row.  If the string extends beyond
  45.      * the right side of the virtual screen it wraps around, and if
  46.      * string extends beyond the bottom of the virtual screen the virtual
  47.      * screen is scrolled up.  Pretty much like using regular printf
  48.      * on the physical screen.
  49.      *
  50.      * Currently, only the BORLAND version has a vs_printf.  If
  51.      * you are using MICROSOFT C use their sprintf to get a formatted
  52.      * string and then use WINDOW PRO'S vs_format.  vs_format operates
  53.      * just like vs_printf except it won't except a variable argument
  54.      * list, i.e. all it does is the TAB, CR, and LF conversion and
  55.      * the cursor advance.
  56.      *
  57.      * Let's try counting to 5.
  58.      */
  59.  
  60.     for (count = 1; count < 6; count++) {
  61.  
  62.     #ifdef MSC
  63.  
  64.         sprintf(buf, "This is the number %d\n", count);
  65.         vs_format(window_handle[0], tile_handle[0], WHITE, BLACK, buf);
  66.  
  67.     #else
  68.         vs_printf(window_handle[0], tile_handle[0], WHITE, BLACK,
  69.             "This is the number %d\n", count);
  70.     #endif
  71.  
  72.     }
  73.     getch();
  74.  
  75.     /* and now in the other tile */
  76.  
  77.     for (count = 1; count < 6; count++) {
  78.  
  79.     #ifdef MSC
  80.  
  81.         sprintf(buf, "This is the number %d\n", count);
  82.         vs_format(window_handle[0], tile_handle[1], WHITE, BLACK, buf);
  83.  
  84.     #else
  85.         vs_printf(window_handle[0], tile_handle[1], WHITE, BLACK,
  86.             "This is the number %d\n", count);
  87.     #endif
  88.  
  89.     }
  90.     getch();
  91.  
  92.     /*
  93.      * Notice that the cursor doesn't move down to tile 2.  The logical
  94.      * was advanced, but the hardware cursor is only displayed in the
  95.      * active tile.  We can display the cursor by activating tile 2
  96.     */
  97.  
  98.     wn_actt(window_handle[0], tile_handle[1]);
  99.     getch();
  100.  
  101.  
  102.     /*
  103.      * Lesson 2: vs_puts, vs_putc, and vs_clrvs.
  104.      *
  105.      * vs_puts is similar to vs_format.  It doesn't except a variable
  106.      * argument list, just a single string.  But, it also does not
  107.      * do TAB expansion or CR/LF conversion (it outputs the IBM
  108.      * extended character in its place.)  In addition although
  109.      * it does do wrapping at the right edge of the virtual screen
  110.      * it will not automatically scroll the virtual screen if the
  111.      * string extends beyond the bottom of the virtual screen.  Instead
  112.      * it merrily continues writing to whatever memory is above the virtual
  113.      * screen.  Because this is not a good idea, you can set a maximum
  114.      * string length, which will clip the string output if it goes
  115.      * beyond the maximum length.  If the string is less than the maximum
  116.      * length it pads spaces. Lastly, it is not dependent on the logical
  117.      * cursor location, nor does it advance the logical cursor.
  118.      *
  119.      * But, before we do that lets clear tile 1.
  120.      *
  121.      */
  122.  
  123.     vs_clrvs(window_handle[0], tile_handle[0], WHITE, BLACK);
  124.     getch();
  125.  
  126.     /*
  127.      * There are a whole series of vs_clr?? functions for clearing
  128.      * the virtual screen from the logical cursor to beginning of a line
  129.      * to the end of a line, etc.  See the reference manual for more
  130.      * info.
  131.     */
  132.  
  133.     #define MAXLEN        60
  134.     #define ROW        1
  135.     #define COLUMN        1
  136.  
  137.     vs_puts(window_handle[0], tile_handle[0], COLUMN, ROW, MAXLEN, BLACK,
  138.         LIGHTGRAY, "vs_puts works \n <--- but newline looks funny");
  139.     getch();
  140.  
  141.     /*    
  142.      * vs_putc is identical to vs_puts except it outputs single characters.
  143.      * So, it doesn't need that maximum length stuff.
  144.      * Let's output the whole IBM character set
  145.     */
  146.  
  147.     row = column = 1;
  148.     vs_clrvs(window_handle[0], tile_handle[1], WHITE, BLACK);
  149.  
  150.     for (count = 0; count < 255; count++) {
  151.         vs_putc(window_handle[0], tile_handle[1], column++, row,
  152.             BLACK, LIGHTGRAY, (char) count);
  153.         if (column > window[window_handle[0]]->port_columns) {
  154.             column =1;
  155.             row++;
  156.         }
  157.     }
  158.     getch();
  159.  
  160.     /*
  161.      * Lesson 3: fill
  162.      *
  163.      * WINDOW PRO let's you define a rectangular region by its
  164.      * upper left and lower right coordinates.
  165.      *
  166.      * Then, with vs_fillchar you can fill in that region with both
  167.      * a character and an attribute, or with vs_fillattr you can keep
  168.      * the existing characters but fill in the region with a new
  169.      * attribute.
  170.      *
  171.      * Lets clear out the top tile and see how it works.
  172.      */
  173.  
  174.     vs_clrvs(window_handle[0], tile_handle[0], BLACK, LIGHTGRAY);
  175.     getch();
  176.     
  177.     #define UPPER_LEFT_COORD         15,3
  178.     #define LOWER_RIGHT_COORD        30,8
  179.  
  180.     vs_fillchar(window_handle[0], tile_handle[0],
  181.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, 'x', WHITE, BLACK);
  182.     getch();
  183.  
  184.     /* now we can change the attribute of that same region */
  185.  
  186.     vs_fillattr(window_handle[0], tile_handle[0],
  187.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, LIGHTGRAY, BLACK);
  188.     getch();
  189.  
  190.     /*
  191.      * Lesson 4: Inserting and deleting rows and columns
  192.      *
  193.      * These functions: vs_insrow, vs_inscolumn, vs_delrow, vs_delcolumn,
  194.      * let you define a rectangular region as discussed in the fill
  195.      * functions and then insert and delete rows and columns in that
  196.      * region.
  197.      *
  198.      * First, vs_insrow, it inserts rows at the top of the rectangular
  199.      * region and pushes all of the other rows down.  The new blank
  200.      * row at the top of the region has an attribute of your choosing.
  201.      *
  202.      * For example,
  203.     /*
  204.  
  205.     /* First lets file tile 2 with some text */
  206.     for (count1 = 1; count1 <= 25; count1++) {
  207.         for (count = 1; count <= 80; count++) {
  208.             vs_putc(window_handle[0], tile_handle[1], count,
  209.                 count1, WHITE, BLACK,
  210.                 (char) (count1 + count + 63));
  211.         }
  212.     }
  213.     getch();
  214.  
  215.     /*
  216.      * And now change the attribute of the region so you can see it more
  217.      * clearly
  218.     */
  219.  
  220.     vs_fillattr(window_handle[0], tile_handle[1],
  221.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, LIGHTGRAY, BLACK);
  222.     getch();
  223.  
  224.     /* Now let's insert 5 rows one at a time */
  225.  
  226.     #define NUMBER_OF_ROWS            1
  227.  
  228.     for (count = 0; count < 5; count++) {
  229.         vs_insrow(window_handle[0], tile_handle[1], UPPER_LEFT_COORD,
  230.             LOWER_RIGHT_COORD, NUMBER_OF_ROWS, LIGHTGRAY, BLACK);
  231.         getch();
  232.     }
  233.  
  234.     /*
  235.      * Deleting rows is inentical to inserting rows except that the
  236.      * rows at the top of the region are 'deleted' and the blank lines
  237.      * appear at the bottom of the region.
  238.      *
  239.      * Now lets setup tile 1 and delete 5 rows one at a time.
  240.     */
  241.  
  242.     for (count1 = 1; count1 <= 25; count1++) {
  243.         for (count = 1; count <= 80; count++) {
  244.             vs_putc(window_handle[0], tile_handle[0], count,
  245.                 count1, WHITE, BLACK,
  246.                 (char) (count1 + count + 63));
  247.         }
  248.     }
  249.     getch();
  250.  
  251.     /*
  252.      * And now change the attribute of the region so you can see it more
  253.      * clearly
  254.     */
  255.  
  256.     vs_fillattr(window_handle[0], tile_handle[0],
  257.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, LIGHTGRAY, BLACK);
  258.     getch();
  259.  
  260.     /* Now let's delete 5 rows one at a time */
  261.  
  262.     for (count = 0; count < 5; count++) {
  263.         vs_delrow(window_handle[0], tile_handle[0], UPPER_LEFT_COORD,
  264.             LOWER_RIGHT_COORD, NUMBER_OF_ROWS, LIGHTGRAY, BLACK);
  265.         getch();
  266.     }
  267.  
  268.     /*
  269.      * vs_inscolumn works the same as vs_insrow except that it inserts
  270.      * a blank column at the left side of the region and pushes the rest
  271.      * of the columns in the region to the right.
  272.      *
  273.      * We'll use the same technique as before to demonstrate.
  274.     */
  275.  
  276.     /* here's a trick to speed up a bunch of vs_putc functions */
  277.     wn_suspendt(window_handle[0], tile_handle[1]);
  278.  
  279.     for (count1 = 1; count1 <= 25; count1++) {
  280.         for (count = 1; count <= 80; count++) {
  281.             vs_putc(window_handle[0], tile_handle[1], count,
  282.                 count1, WHITE, BLACK,
  283.                 (char) (count1 + count + 63));
  284.         }
  285.     }
  286.     wn_suspendt(window_handle[0], tile_handle[1]);
  287.     getch();
  288.  
  289.     /*
  290.      * And now change the attribute of the region so you can see it more
  291.      * clearly
  292.     */
  293.  
  294.     vs_fillattr(window_handle[0], tile_handle[1],
  295.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, LIGHTGRAY, BLACK);
  296.     getch();
  297.  
  298.     /* Now let's insert 10 columns one at a time */
  299.  
  300.     for (count = 0; count < 10; count++) {
  301.         vs_inscolumn(window_handle[0], tile_handle[1], UPPER_LEFT_COORD,
  302.             LOWER_RIGHT_COORD, NUMBER_OF_ROWS, LIGHTGRAY, BLACK);
  303.         getch();
  304.     }
  305.  
  306.     /*
  307.      * And finally, the vs_delcolumn function (if you haven't already
  308.      * guessed) deletes the column on the left side of the region and
  309.      * leaves blank lines on the right side
  310.      *
  311.      * Let's see how that might work.
  312.     */
  313.  
  314.     wn_suspendt(window_handle[0], tile_handle[0]);
  315.  
  316.     for (count1 = 1; count1 <= 25; count1++) {
  317.         for (count = 1; count <= 80; count++) {
  318.             vs_putc(window_handle[0], tile_handle[0], count,
  319.                 count1, WHITE, BLACK,
  320.                 (char) (count1 + count + 63));
  321.         }
  322.     }
  323.     wn_suspendt(window_handle[0], tile_handle[0]);
  324.     getch();
  325.  
  326.     /*
  327.      * And now change the attribute of the region so you can see it more
  328.      * clearly
  329.     */
  330.  
  331.     vs_fillattr(window_handle[0], tile_handle[0],
  332.         UPPER_LEFT_COORD, LOWER_RIGHT_COORD, LIGHTGRAY, BLACK);
  333.     getch();
  334.  
  335.     /* Now let's delete 10 columns one at a time */
  336.  
  337.     for (count = 0; count < 10; count++) {
  338.         vs_delcolumn(window_handle[0], tile_handle[0], UPPER_LEFT_COORD,
  339.             LOWER_RIGHT_COORD, NUMBER_OF_ROWS, LIGHTGRAY, BLACK);
  340.         getch();
  341.     }
  342.  
  343.     /*
  344.      * Lesson 5: vs_gets
  345.      *
  346.      * vs_gets is a gets-like function for virtual screens.  It echoes
  347.      * characters typed at the keyboard to a virtual screen, starting at
  348.      * the virtual screen's logical cursor and for each character input
  349.      * outputs the character (in the attribute of your choice) and advances
  350.      * the logical cursor.  If the cursor is advanced to an area outside of
  351.      * the viewport you can elect to automatically readjust the viewport, or
  352.      * not.  If the string exceeds the length of the line it automatically
  353.      * wraps to the beginning of the next line.  If the string exceeds the
  354.      * number of rows in the virtual screen all of the text in the virtual
  355.      * screen scrolls up.  You must specify a pointer to the storage location
  356.      * and vs_gets does not determine if the pointer has sufficient
  357.      * memory allocated to it to hold the input string.  Instead you
  358.      * pass a maximum length parameter.
  359.      *
  360.      * For example:
  361.     */
  362.  
  363.     /* Lets set up the screen first */
  364.     vs_clrvs(window_handle[0], tile_handle[0], WHITE, BLACK);
  365.     wn_sizet(window_handle[0], tile_handle[0], -20, 0);
  366.     vs_locatecur(window_handle[0], tile_handle[0], 1,2);
  367.     vs_puts(window_handle[0], tile_handle[0], 1, 1, 50, WHITE, BLACK,
  368.         "Type in a few sentences. (Scroll is on.)");
  369.     
  370.     /* You don't have to do this, but you won't see the cursor otherwise */
  371.     wn_actt(window_handle[0], tile_handle[0]);
  372.  
  373.     #define SCROLL_OPTION_ON    1
  374.     #define SCROLL_OPTION_OFF    0
  375.     #define MAXIMUM_LENGTH        2000
  376.  
  377.     /* Here is vs_gets */
  378.     vs_gets(window_handle[0], tile_handle[0], SCROLL_OPTION_ON, string,
  379.         WHITE, BLACK, MAXIMUM_LENGTH);
  380.     getch();
  381.  
  382.     /* Now lets do the whole thing over again but with the scroll option off */
  383.  
  384.     /* Lets set up the screen first */
  385.     vs_clrvs(window_handle[0], tile_handle[0], WHITE, BLACK);
  386.     vs_locatecur(window_handle[0], tile_handle[0], 1,2);
  387.     vs_puts(window_handle[0], tile_handle[0], 1, 1, 50, WHITE, BLACK,
  388.         "Type in a few sentences. (Scroll is off.)");
  389.     
  390.     /* Here is vs_gets */
  391.     vs_gets(window_handle[0], tile_handle[0], SCROLL_OPTION_OFF, string,
  392.         WHITE, BLACK, MAXIMUM_LENGTH);
  393.     getch();
  394.  
  395.  
  396.     /* finish up with some pizzazzzzzzzzzzzzzzzz............ */
  397.  
  398.     wn_flybox(window[window_handle[0]]->physical_x,
  399.         window[window_handle[0]]->physical_y,
  400.         window[window_handle[0]]->port_rows,
  401.         window[window_handle[0]]->port_columns,
  402.         window[window_handle[0]]->physical_x +
  403.         window[window_handle[0]]->port_columns/2,
  404.         window[window_handle[0]]->physical_y +
  405.         window[window_handle[0]]->port_rows/2,
  406.         1, 1, 5, 5000, box4, WHITE, BLACK);
  407.     wn_delw(window_handle[0]);
  408.  
  409.     /* restore the characters to the original screen */
  410.         wn_restorescr();
  411.  
  412.     /* restore the cursor position */
  413.     v_gotoxy(oldx,oldy);
  414.  
  415.     /* restore the cursor shape */
  416.     v_curshape(oldb, olde);
  417.  
  418. }
  419.