home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MSGDP206.SZH / SCRNOS2.C < prev    next >
Text File  |  1991-01-07  |  11KB  |  481 lines

  1. /*
  2.  
  3. Title:  MsgEd
  4.  
  5. File:   Scrnos2.c
  6.  
  7. Author: Jim Nutt
  8.  
  9. Copr:   1987 by Jim Nutt
  10.  
  11. Description:
  12.  
  13.     Screen handling functions for MsgEd.  also a few string handlers.
  14.  
  15.     if you need to port this... remember, msged uses a 1 based coordinate
  16.     system (i.e. top left is (1,1) NOT (0,0)).  anything that is dependent
  17.     on zortech c, quick c or turbo c will complain when you try to compile
  18.     this thing under any other compiler...
  19.  
  20. */
  21.  
  22. #define USEFOSSIL 1
  23.  
  24. #include <dos.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #ifndef MK_FP
  30. #include "mkfp.h"
  31. #endif
  32. #include "screen.h"
  33. #include "vfossil2.h"
  34. #include "screen2.h"
  35.  
  36. static void pascal vfossil_cursor (int st);
  37. static void pascal vfossil_init (void);
  38. static void pascal vfossil_open (void);
  39. static void pascal vfossil_close (void);
  40.  
  41. void pascal strins(char *l, char c, int x);
  42. void pascal strdel(char *l, int x);
  43.  
  44. #define FALSE 0
  45. #define TRUE !FALSE
  46. #define TEXTLEN 256
  47.  
  48. unsigned int *macros[41];    /* function key macros + 1 for autostart */
  49.  
  50. int maxx;               /* maximum screen columns */
  51. int maxy;               /* maximum screen rows */
  52. int videomethod;        /* how to write to the screen */
  53. unsigned int vbase;     /* where screen memory is located */
  54. unsigned char current_color;     /* current screen attribute */
  55.  
  56. /* keyboard control values */
  57.  
  58. static int autostart = 0;
  59. static unsigned int * macro = (void *) NULL;
  60. static unsigned int keybuf = 0;
  61.  
  62. static char s[TEXTLEN];
  63.  
  64. static unsigned int *screen = NULL;
  65. static unsigned int cx = 1;
  66. static unsigned int cy = 1;
  67. static int desqview = 0;
  68.  
  69. static unsigned char scroll_fill [2];
  70. static VIOMODEINFO fossil_data;
  71.  
  72.  
  73. void pascal video_init()
  74. {
  75.     KBDINFO ki;
  76.     fossil_data.cb = 2 * sizeof (fossil_data);
  77.     VioGetMode(&fossil_data,0);    /* Get mode info      */
  78.     maxx = fossil_data.col;        /* Maximum 'X' value  */
  79.     maxy = fossil_data.row;        /* Maximum 'Y' value  */
  80.     ki.cb = sizeof(KBDINFO);       /* Set binary keyboard mode */
  81.     ki.fsMask = KBD_BINARY;
  82.     KbdSetStatus (&ki, 0);
  83. }
  84.  
  85. void pascal video_end()
  86. {
  87. }
  88.  
  89. void pascal video_update()
  90. {
  91.     VioSetCurPos (cy - 1, cx - 1, 0);
  92. }
  93.  
  94. void pascal scrollup(int x1,int y1,int x2,int y2,int lines)
  95.  
  96. {
  97.     y2 = min(y2,maxy) - 1;
  98.     y1 = min(y1,maxy) - 1;
  99.     x1 = min(x1,maxx) - 1;
  100.     x2 = min(x2,maxx) - 1;
  101.     scroll_fill [0] = ' ';
  102.     scroll_fill [1] = current_color;
  103.     if (lines == 0)
  104.         lines = -1;
  105.     VioScrollUp (y1,x1,y2,x2,lines,(unsigned char *) scroll_fill,0);
  106. }
  107.  
  108. void pascal scrolldown(int x1,int y1,int x2,int y2,int lines)
  109.  
  110. {
  111.     y2 = min(y2,maxy) - 1;
  112.     y1 = min(y1,maxy) - 1;
  113.     x1 = min(x1,maxx) - 1;
  114.     x2 = min(x2,maxx) - 1;
  115.     scroll_fill [0] = ' ';
  116.     scroll_fill [1] = current_color;
  117.     VioScrollDn (y1,x1,y2,x2,lines,(unsigned char *)scroll_fill,0);
  118. }
  119.  
  120. void pascal bputc(int c)
  121. {
  122.     unsigned int d = 0;
  123.  
  124.     d = ((unsigned) c & 0xff) | (current_color << 8);
  125.     VioWrtCellStr ((PCH)&d, 2, cy - 1, cx - 1, 0);
  126.     if (++cx > maxx) {
  127.         cx = 1;
  128.         if (++cy > maxy)
  129.             cy = 1;
  130.     }
  131. }
  132.  
  133. void pascal gotoxy(int x, int y)
  134. {
  135.     cx = min(x,maxx);
  136.     cy = min(y,maxy);
  137. }
  138.  
  139. int pascal wherex()
  140. {
  141.     return (cx);
  142. }
  143.  
  144. int pascal wherey()
  145. {
  146.     return(cy);
  147. }
  148.  
  149. unsigned int pascal keyhit()
  150.  
  151. {
  152.     KBDKEYINFO ki;
  153.  
  154.     if (macro)      /* always return no keypress during macros */
  155.         return(0);
  156.  
  157.     ki.chChar = ki.chScan = ki.fbStatus = 0;
  158.     KbdPeek (&ki, 0);
  159.  
  160.     return(ki.fbStatus == 0x40);
  161. }
  162.  
  163.  
  164. unsigned int pascal getkey()
  165.  
  166. {
  167.     KBDKEYINFO ki;
  168.  
  169.     if ((macros[0] != (void *) NULL) && !autostart) {
  170.         autostart = 1;
  171.         macro = macros[0];
  172.     }
  173.  
  174.     if (macro != (void *) NULL) {
  175.         if (*(++macro))
  176.             return(*macro);
  177.  
  178.         macro = (void *) NULL;
  179.     }
  180.     ki.chChar = ki.chScan = 0;
  181.     KbdCharIn (&ki, IO_WAIT, 0);
  182.     if (ki.chChar == 0xe0) {
  183.         if (ki.chScan) {
  184.             ki.chChar = 0;                      /* Force Scan return */
  185.         } else {                                /* Get next block    */
  186.             ki.chChar = 0;
  187.             KbdCharIn (&ki, IO_WAIT, 0);
  188.             if (!ki.chScan) {                   /* Still no scan?    */
  189.                 ki.chScan = ki.chChar;          /* Move new char over*/
  190.                 ki.chChar = 0;                  /* Force its return  */
  191.             } else {
  192.                 ki.chChar = 0;                  /* Force new scan    */
  193.             }
  194.         }
  195.     }
  196.     if (ki.chScan == 0xe0) {
  197.         if (!ki.chChar) {
  198.             ki.chScan = 0;
  199.             KbdCharIn (&ki, IO_WAIT, 0);
  200.             if (!ki.chScan) {                   /* Still no scan?    */
  201.                 ki.chScan = ki.chChar;          /* Move new char over*/
  202.                 ki.chChar = 0;                  /* Force its return  */
  203.             } else {
  204.                 ki.chChar = 0;                  /* Force new scan    */
  205.             }
  206.         } else {
  207.             ki.chScan = 0;                      /* Handle 0xe00d case*/
  208.         }
  209.     }
  210.  
  211.     if (ki.chChar)
  212.         ki.chScan = 0;
  213.     if ((ki.chScan >= 0x3b) && (ki.chScan <= 0x44))
  214.         macro = macros[ki.chScan - 0x3a];
  215.     else if ((ki.chScan >= 0x54) && (ki.chScan <= 0x71))
  216.         macro = macros[ki.chScan - 0x49];
  217.  
  218.     if (macro != (void *) NULL) {
  219.         if (*macro)
  220.             return(*macro);
  221.  
  222.         macro = (void *) NULL;
  223.     }
  224.  
  225.     return((unsigned int)((ki.chScan<<8)+(ki.chChar)));
  226. }
  227.  
  228. void pascal bputs(char *s)
  229.  
  230. {
  231.     unsigned int *linebase = screen + ((((cy - 1) * maxx) + (cx - 1)));
  232. /*  unsigned int d; */
  233.     char *t = s;
  234.     char ch;
  235.     unsigned int a = current_color;
  236.     unsigned int l, l1 = 0;
  237.     unsigned int limit = maxx - 1;
  238.  
  239.     if (s == NULL)
  240.         return;
  241.  
  242. /*  d = current_color << 8; */
  243.  
  244.     if ((t = strchr(s,'\n')) != NULL) {
  245.         ch = *t;
  246.         *t = '\0';
  247.     }
  248.  
  249.     if ((strlen(s) + cx) > maxx) {
  250.         if (t)
  251.             *t = ch;
  252.         ch = *(t = s + maxx - cx + 1);
  253.         *t = '\0';
  254.     }
  255.  
  256.     if (strlen(s)) {
  257.         l1 = l = (((strlen(s) + cx) < maxx)?strlen(s):maxx - cx + 1);
  258. /*      cx += l; */
  259. /*      VioWrtCharStrAtt(s,l,cy - 1,cx - l - 1, (PBYTE)&a,0); */
  260.         VioWrtCharStrAtt((char *) s,l,cy - 1,cx - 1, (PBYTE) &a,0);
  261.     }
  262.     if (t)
  263.         *t = ch;
  264.     cx += l1;
  265. }
  266.  
  267. void pascal cls()
  268.  
  269. {
  270.     scrollup(1, 1, maxx, maxy, 0);
  271.     gotoxy(1, 1);
  272. }
  273.  
  274. void pascal clrwnd(int x1,int y1,int x2,int y2)
  275.  
  276. {
  277.     scrollup(x1, y1, x2, y2, 0);
  278.     gotoxy(x1,y1);
  279. }
  280.  
  281. void pascal clreol()
  282. {
  283.     int x = cx;
  284. /*  clrwnd(cx,cy,maxx,cy); */
  285.     clrwnd(x,cy,maxx,cy);
  286.     cx = x;
  287.  
  288. }
  289.  
  290. int pascal bgets(char *s1, int c, int w)
  291.  
  292. {
  293.     int     ch;
  294.     int     x1;
  295.     char   *t;
  296.     static  insert = ON;
  297.     int     y = wherey();
  298.     int     x = wherex();
  299.     int     ofs = 0;
  300.     char    *o;
  301.  
  302.     o = strdup(s1);
  303.     w = (w+x)>maxx?maxx-x:w;
  304.  
  305.     memset(s, 0, sizeof s);
  306.     strcpy(s, s1);
  307.     t = s + strlen(s);
  308.     *t = '\0';
  309.     bputs(s+ofs);
  310.  
  311.     while (TRUE) {          /* endless loop */
  312.         video_update();
  313.         switch (ch = getkey()) {
  314.  
  315.         case UP:
  316.         case DOWN:
  317.         case PGUP:
  318.         case PGDN:
  319.         case ENTER:
  320.             free(o);
  321.             strcpy(s1, s);
  322.             return (ch);
  323.  
  324.         case ABORT:
  325.             strcpy(s1, o);
  326.             free(o);
  327.             gotoxy(x,y);
  328.             clreol();
  329.             bputs(s1);
  330.             return(ch);
  331.  
  332.         case WORDRT:
  333.             break;
  334.  
  335.         case WORDLT:
  336.             break;
  337.  
  338.         case CANCEL:
  339.             memset(s, 0, sizeof s);
  340.             t = s;
  341.             gotoxy(x, y);
  342.             clreol();
  343.             break;
  344.  
  345.         case GOBOL:
  346.             t = s;
  347.             ofs = 0;
  348.             gotoxy(x, y);
  349.             break;
  350.  
  351.         case GOEOL:
  352.             t = s + strlen(s);
  353.             gotoxy(x + strlen(s) - ofs, y);
  354.             break;
  355.  
  356.         case RUBOUT:
  357.         case BKSPC:
  358.             if (x == wherex())
  359.                 break;
  360.             t--;
  361.             memmove(t, (t + 1), strlen(t) + 1);
  362.             gotoxy(wherex() - 1, y);
  363.             x1 = wherex();
  364.             bputs(t);
  365.             bputc(' ');
  366.             gotoxy(x1, y);
  367.             break;
  368.  
  369.         case LEFT:
  370.             if (t == s)
  371.                 break;
  372.             t--;
  373.             gotoxy(wherex() - 1, y);
  374.             break;
  375.  
  376.         case RIGHT:
  377.             if (t >= (s + strlen(s)))
  378.                 break;
  379.             t++;
  380.             gotoxy(wherex() + 1, y);
  381.             break;
  382.  
  383.         case DELCHR:
  384.             memmove(t, t + 1, strlen(t) + 1);
  385.             x1 = wherex();
  386.             bputs(t);
  387.             bputc(' ');
  388.             gotoxy(x1, y);
  389.             break;
  390.  
  391.         case INSERT:
  392.             insert = !insert;
  393.             break;
  394.  
  395.         default:
  396.             if (!(ch & 0xff) || (strlen(s) == (unsigned) c))
  397.                 break;
  398.             if (insert) {
  399.                 x1 = wherex();
  400.                 t++;
  401.                 strins(s, (char) (ch & 0xff), (x1 - x + 1));
  402.                 gotoxy(x, y);
  403.                 bputs(s+ofs);
  404.                 if (ofs)
  405.                     gotoxy(x1, y);
  406.                 else
  407.                     gotoxy(x1 + 1, y);
  408.             }
  409.             else {
  410.                 *t++ = (char) (ch & 0xFF);
  411.                 bputc((char) (ch & 0xff));
  412.             }
  413.             video_update();
  414.             break;
  415.         }
  416.  
  417.         if (ofs) {
  418.             gotoxy(x,y);
  419.             bputs(s+ofs);
  420.         }
  421.     }
  422. }
  423.  
  424. int pascal getnum(int l, int h, int value)
  425.  
  426. {
  427.     int     i, x;
  428.     char    s[TEXTLEN];
  429.  
  430.     i = value;
  431.     x = wherex();
  432.     do {
  433.         clreol();
  434.         memset(s, 0, sizeof(s));
  435.         itoa(i, s, 10);
  436.         gotoxy(x, wherey());
  437.         bgets(s, TEXTLEN/2, TEXTLEN/2);
  438.         i = atoi(s);
  439.     } while ((i < l) || (i > h));
  440.     return (i);
  441. }
  442.  
  443. void pascal set_color(unsigned int attr)
  444.  
  445. {
  446.     current_color = (unsigned char) attr;
  447. }
  448.  
  449. unsigned pascal get_color()
  450.  
  451. {
  452.     return(current_color);
  453. }
  454.  
  455. int cdecl bprintf(char *f,...)
  456.  
  457. {
  458.     va_list params;
  459.     int     i;
  460.  
  461.     va_start(params, f);
  462.     i = vsprintf(s, f, params);
  463.     bputs(s);
  464.     return (i);
  465. }
  466.  
  467.  
  468. CURSOR _cursor;
  469.  
  470. static void pascal vfossil_cursor (int st)
  471. {
  472.     CURSOR *q;
  473.  
  474.     q = (CURSOR *) &_cursor;
  475.     /* We can make the cursor go away */
  476.     VioGetCurType (q, 0);
  477.     _cursor.cur_attr = st ? 0 : -1;
  478.     VioSetCurType (q, 0);
  479. }
  480.  
  481.