home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / vrac / odoors50.zip / EX_CHAT.C < prev    next >
C/C++ Source or Header  |  1994-09-24  |  12KB  |  264 lines

  1. /* EX_CHAT.C - Example of a multi-window full-screen chat door written with
  2.  *             OpenDoors. Demonstrates the ease of using sophisticated ANSI /
  3.  *             AVATAR terminal features within OpenDoors programs. See manual
  4.  *             for instructions on how to compile this program or any other
  5.  *             OpenDoors program.
  6.  *
  7.  *             This program create two windows on the screen, seperated by
  8.  *             a bar with user name / sysop name information. This program
  9.  *             permits communication between the local sysop and remote user
  10.  *             by displaying the text typed by the user in one window, and
  11.  *             the text typed by the sysop in the other window. When either
  12.  *             user reaches the bottom of the window, the contents of the
  13.  *             window is scrolled up to provide more room for typing. Words
  14.  *             are also wrapped when either typist reaches the end of a
  15.  *             line. The advantage of a split-screen chat program is that
  16.  *             it permits both sysop and user to type at the same time
  17.  *             without difficulty. The chat function automatically invokes
  18.  *             OpenDoor's internal chat mode if ANSI or AVATAR modes are not
  19.  *             available. The display colours, window sizes and locations,
  20.  *             and distance to scroll a window's contents are configurable
  21.  *             by setting the appropriate variables, below. When the Sysop
  22.  *             invokes a DOS shell, a pop-up window is displayed to indicate
  23.  *             to the user that the door program has been suspended.
  24.  */
  25.  
  26. #include "opendoor.h"                        /* Include required header files */
  27. #include <string.h>
  28.  
  29.  
  30. void fullscreen_chat(void);           /* Full-screen chat function prototypes */
  31. void chat_new_line(void);
  32. void display_shell_window(void);
  33. void remove_shell_window(void);
  34.  
  35.  
  36.  
  37. main()                                     /* PROGRAM'S EXECUTION BEGINS HERE */
  38.    {
  39.    od_init();                                         /* Initialize OpenDoors */
  40.  
  41.    fullscreen_chat();                     /* Invoke full-screen chat function */
  42.  
  43.    return(0);                                                    /* Exit door */
  44.    }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.                                     /* FULL-SCREEN CHAT CUSTOMIZATION OPTIONS */
  51.  
  52. char window_colour[2]={0x0b,0x0c};        /* Text colour used for each person */
  53. char bar_colour=0x70;                      /* Colour of window seperation bar */
  54. char top_line[2]={1,13};       /* Specifies location of each window on screen */
  55. char bottom_line[2]={11,23};          /* Line number of bottom of each window */
  56. char bar_line=12;                     /* Line number of window seperation bar */
  57. char scroll_distance=2;            /* Distance to scroll window when required */
  58. char shell_window_title=0x1a;   /* Colour of title of DOS shell notice window */
  59. char shell_window_boarder=0x1f;         /* Colour of DOS shell window boarder */
  60. char shell_window_text=0x1b;            /* Colour of text in DOS shell window */
  61.  
  62.  
  63.  
  64. char cursor_window;                     /* FULL-SCREEN CHAT INTERNAL VARIABLES */
  65. char current_word[2][81];
  66. int word_length[2];
  67. int cursor_col[2];
  68. int cursor_line[2];
  69. unsigned char key;
  70. int old_chat_key;
  71. void *shell_window;
  72. char *before_shell_text;
  73. char *after_shell_text;
  74.                                                  /* FULL-SCREEN CHAT FUNCTION */
  75. void fullscreen_chat(void)
  76.    {
  77.    cursor_window=0;                                /* Reset working variables */
  78.    word_length[0]=word_length[1]=0;
  79.    cursor_col[0]=cursor_col[1]=1;
  80.    cursor_line[0]=top_line[0];
  81.    cursor_line[1]=top_line[1];
  82.  
  83.  
  84.                           /* If ANSI or AVATAR graphics mode is not available */
  85.    if(!od_control.user_ansi && !od_control.user_avatar)
  86.       {
  87.       od_chat();                /* Then use OpenDoor's line chat mode instead */
  88.       return;
  89.       }
  90.  
  91.    od_control.od_cbefore_shell=display_shell_window;/* Set DOS shell settings */
  92.    od_control.od_cafter_shell=remove_shell_window;
  93.    before_shell_text=od_control.od_before_shell;
  94.    after_shell_text=od_control.od_after_shell;
  95.    od_control.od_before_shell=NULL;
  96.    od_control.od_after_shell=NULL;
  97.  
  98.    old_chat_key=od_control.key_chat;       /* Prevent internal chat mode from */
  99.    od_control.key_chat=0;                                    /* being invoked */
  100.  
  101.                                                       /* DRAW THE CHAT SCREEN */
  102.    od_set_attrib(window_colour[0]);
  103.    od_clr_scr();                                          /* Clear the screen */
  104.  
  105.    od_set_cursor(bar_line,1);                   /* Draw window seperation bar */
  106.    od_set_attrib(bar_colour);
  107.    od_clr_line();
  108.    od_printf(" Top : %s    Bottom : %s",od_control.user_name,
  109.                                         od_control.sysop_name);
  110.  
  111.    od_set_cursor(top_line[0],1);     /* Locate cursor where typing will begin */
  112.    od_set_attrib(window_colour[0]);            /* Set appropriate text colour */
  113.  
  114.                                                             /* MAIN CHAT LOOP */
  115.    for(;;)                              /* (Repeats for each character typed) */
  116.       {
  117.       key=(char)od_get_key(TRUE);         /* Get next keystroke from keyboard */
  118.  
  119.  
  120.                                                      /* CHECK FOR SYSOP ABORT */
  121.       if(key==27 && od_control.od_last_input==1)    /* If sysop pressed [ESC] */
  122.          {
  123.          od_set_attrib(0x07);                         /* Reset display colour */
  124.          od_clr_scr();                                    /* Clear the screen */
  125.          od_control.key_chat=old_chat_key;    /* Re-enable internal chat mode */
  126.  
  127.          od_control.od_cbefore_shell=NULL;      /* Restore DOS shell settings */
  128.          od_control.od_cafter_shell=NULL;
  129.          od_control.od_before_shell=before_shell_text;
  130.          od_control.od_after_shell=after_shell_text;
  131.  
  132.          return;                                     /* Exit full-screen chat */
  133.          }
  134.  
  135.                                                       /* CHECK FOR NEW TYPIST */
  136.       if(od_control.od_last_input!=cursor_window) /* If new person typing now */
  137.          {                             /* Switch cursor to appropriate window */
  138.          cursor_window=od_control.od_last_input;        /* Set current typist */
  139.  
  140.                                                  /* Move cursor to new window */
  141.          od_set_cursor(cursor_line[cursor_window],cursor_col[cursor_window]);
  142.  
  143.          od_set_attrib(window_colour[cursor_window]);   /* Change text colour */
  144.          }
  145.  
  146.  
  147.       if(key==13 || key==10)            /* IF USER PRESSED [ENTER] / [RETURN] */
  148.          {
  149.          word_length[cursor_window]=0;       /* Enter constitutes end of word */
  150.  
  151.          chat_new_line();                                /* Move to next line */
  152.          }
  153.  
  154.  
  155.       else if(key==8)                            /* IF USER PRESS [BACKSPACE] */
  156.          {
  157.          if(cursor_col[cursor_window] > 1)        /* If not at left of screen */
  158.             {
  159.             --cursor_col[cursor_window];     /* Move cursor back on character */
  160.             if(word_length[cursor_window] > 0) --word_length[cursor_window];
  161.             od_printf("\b \b");           /* Erase last character from screen */
  162.             }
  163.          }
  164.  
  165.  
  166.       else if(key==32)                             /* IF USER PRESSED [SPACE] */
  167.          {
  168.          word_length[cursor_window]=0;     /* [Space] constitutes end of word */
  169.  
  170.          if(cursor_col[cursor_window]==79)               /* If at end of line */
  171.             chat_new_line();                      /* Move cursor to next line */
  172.          else                                        /* If not at end of line */
  173.             {
  174.             ++cursor_col[cursor_window];         /* Increment cursor position */
  175.             od_putch(32);                                  /* Display a space */
  176.             }
  177.          }
  178.  
  179.  
  180.       else if(key>32)                  /* IF USER TYPED A PRINTABLE CHARACTER */
  181.          {                                  /* PERFORM WORD WRAP IF NECESSARY */
  182.          if(cursor_col[cursor_window]==79)     /* If cursor is at end of line */
  183.             {
  184.                                                 /* If there is a word to wrap */
  185.             if(word_length[cursor_window]>0 && word_length[cursor_window]<78)
  186.                {
  187.                                           /* Move cursor to beginning of word */
  188.                od_set_cursor(cursor_line[cursor_window],
  189.                           cursor_col[cursor_window]-word_length[cursor_window]);
  190.  
  191.                od_clr_line();                 /* Erase word from current line */
  192.  
  193.                chat_new_line();                   /* Move cursor to next line */
  194.  
  195.                                                             /* Redisplay word */
  196.                od_disp(current_word[cursor_window],word_length[cursor_window],
  197.                                                                           TRUE);
  198.                cursor_col[cursor_window]+=word_length[cursor_window];
  199.                }
  200.  
  201.             else                             /* If there is no word to "wrap" */
  202.                {
  203.                chat_new_line();                   /* Move cursor to next line */
  204.                word_length[cursor_window]=0;              /* Start a new word */
  205.                }
  206.             }
  207.  
  208.                                              /* ADD CHARACTER TO CURRENT WORD */
  209.                                /* If there is room for more character in word */
  210.          if(strlen(current_word[cursor_window])<79)      /* Add new character */
  211.             current_word[cursor_window][word_length[cursor_window]++]=key;
  212.  
  213.                                              /* DISPLAY NEWLY TYPED CHARACTER */
  214.          ++cursor_col[cursor_window];
  215.          od_putch(key);
  216.          }
  217.       }
  218.    }
  219.  
  220.  
  221.  
  222.                /* FUNCTION USED BY FULL-SCREEN CHAT TO START A NEW INPUT LINE */
  223. void chat_new_line(void)
  224.    {                                      /* If cursor is at bottom of window */
  225.    if(cursor_line[cursor_window]==bottom_line[cursor_window])
  226.       {                                /* Scroll window up one line on screen */
  227.       od_scroll(1,top_line[cursor_window],79, bottom_line[cursor_window],
  228.                 scroll_distance, 0);
  229.       cursor_line[cursor_window]-=(scroll_distance - 1);
  230.       }
  231.  
  232.    else                               /* If cursor is not at bottom of window */
  233.       {
  234.       ++cursor_line[cursor_window];              /* Move cursor down one line */
  235.       }
  236.  
  237.                                           /* Move cursor's position on screen */
  238.    od_set_cursor(cursor_line[cursor_window],cursor_col[cursor_window]=1);
  239.  
  240.    od_set_attrib(window_colour[cursor_window]);         /* Change text colour */
  241.    }
  242.  
  243.  
  244. void display_shell_window(void)
  245.    {
  246.    if((shell_window=od_window_create(17,9,63,15,"DOS Shell",
  247.                                      shell_window_boarder, shell_window_title, 
  248.                                      shell_window_text, 0))==NULL) return;
  249.  
  250.    od_set_attrib(shell_window_text);
  251.    od_set_cursor(11,26);
  252.    od_printf("The Sysop has shelled to DOS");
  253.    od_set_cursor(13,21);
  254.    od_printf("He/She will return in a few moments...");
  255.    }
  256.  
  257.  
  258. void remove_shell_window(void)
  259.    {
  260.    od_window_remove(shell_window);
  261.    od_set_cursor(cursor_line[cursor_window],cursor_col[cursor_window]);
  262.    od_set_attrib(window_colour[cursor_window]);
  263.    }
  264.