home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / FIDO / MSGD2SRC.ZIP / OS2.ZIP / SCRNOS2.C < prev   
Encoding:
C/C++ Source or Header  |  1990-05-29  |  8.8 KB  |  495 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. static unsigned int pascal lgetkey(void);
  62.  
  63. static char s[TEXTLEN];
  64.  
  65. static unsigned int *screen = NULL;
  66. static unsigned int cx = 1;
  67. static unsigned int cy = 1;
  68. static int desqview = 0;
  69.  
  70. static unsigned char scroll_fill [2];
  71. static VIOMODEINFO fossil_data;
  72.  
  73.  
  74. void pascal video_init()
  75. {
  76.     KBDINFO ki;
  77.     fossil_data.cb = 2 * sizeof (fossil_data);
  78.     VioGetMode(&fossil_data,0);    /* Get mode info      */
  79.     maxx = fossil_data.col;        /* Maximum 'X' value  */
  80.     maxy = fossil_data.row;        /* Maximum 'Y' value  */
  81.     ki.cb = sizeof(KBDINFO);       /* Set binary keyboard mode */
  82.     ki.fsMask = KBD_BINARY;
  83.     KbdSetStatus (&ki, 0);
  84. }
  85.  
  86. void pascal video_end()
  87. {
  88. }
  89.  
  90. void pascal video_update()
  91. {
  92.     VioSetCurPos (cy - 1, cx - 1, 0);
  93. }
  94.  
  95. void pascal scrollup(x1, y1, x2, y2, lines)
  96.     int     x1, y1, x2, y2, lines;
  97.  
  98. {
  99.     y2 = min(y2,maxy) - 1;
  100.     y1 = min(y1,maxy) - 1;
  101.     x1 = min(x1,maxx) - 1;
  102.     x2 = min(x2,maxx) - 1;
  103.     scroll_fill [0] = ' ';
  104.     scroll_fill [1] = current_color;
  105.     if (lines == 0)
  106.         lines = -1;
  107.     VioScrollUp (y1,x1,y2,x2,lines,(unsigned char *) scroll_fill,0);
  108. }
  109.  
  110. void pascal scrolldown(x1, y1, x2, y2, lines)
  111.     int     x1, y1, x2, y2, lines;
  112.  
  113. {
  114.     y2 = min(y2,maxy) - 1;
  115.     y1 = min(y1,maxy) - 1;
  116.     x1 = min(x1,maxx) - 1;
  117.     x2 = min(x2,maxx) - 1;
  118.     scroll_fill [0] = ' ';
  119.     scroll_fill [1] = current_color;
  120.     VioScrollDn (y1,x1,y2,x2,lines,(unsigned char *)scroll_fill,0);
  121. }
  122.  
  123. void pascal bputc(int c)
  124. {
  125.     unsigned int d = 0;
  126.  
  127.     d = ((unsigned) c & 0xff) | (current_color << 8);
  128.     VioWrtCellStr ((PCH)&d, 2, cy - 1, cx - 1, 0);
  129.     if (++cx > maxx) {
  130.         cx = 1;
  131.         if (++cy > maxy)
  132.             cy = 1;
  133.     }
  134. }
  135.  
  136. void pascal gotoxy(int x, int y)
  137. {
  138.     cx = min(x,maxx);
  139.     cy = min(y,maxy);
  140. }
  141.  
  142. int pascal wherex()
  143. {
  144.     return (cx);
  145. }
  146.  
  147. int pascal wherey()
  148. {
  149.     return(cy);
  150. }
  151.  
  152. unsigned int pascal keyhit()
  153.  
  154. {
  155.     if (macro)        /* always return no keypress during macros */
  156.         return(0);
  157.  
  158.     if (!keybuf)
  159.         keybuf = lgetkey();
  160.  
  161.     return(keybuf);
  162. }
  163.  
  164.  
  165. unsigned int pascal getkey()
  166.  
  167. {
  168.     unsigned int k = keybuf;
  169.  
  170.     keybuf = 0;
  171.     while (!k)
  172.         k = lgetkey();
  173.     return(k);
  174. }
  175.  
  176.  
  177. static unsigned int pascal lgetkey()
  178.  
  179. {
  180.     KBDKEYINFO ki;
  181.  
  182.     if ((macros[0] != (void *) NULL) && !autostart) {
  183.         autostart = 1;
  184.         macro = macros[0];
  185.     }
  186.  
  187.     if (macro != (void *) NULL) {
  188.         if (*(++macro))
  189.             return(*macro);
  190.  
  191.         macro = (void *) NULL;
  192.     }
  193.     ki.chChar = ki.chScan = 0;
  194.     KbdCharIn (&ki, IO_WAIT, 0);
  195.     if (ki.chChar == 0xe0) {
  196.         if (ki.chScan) {
  197.             ki.chChar = 0;                        /* Force Scan return */
  198.         } else {                                /* Get next block     */
  199.             ki.chChar = 0;
  200.             KbdCharIn (&ki, IO_WAIT, 0);
  201.             if (!ki.chScan) {                    /* Still no scan?     */
  202.                 ki.chScan = ki.chChar;            /* Move new char over*/
  203.                 ki.chChar = 0;                    /* Force its return  */
  204.             } else {
  205.                 ki.chChar = 0;                    /* Force new scan     */
  206.             }
  207.         }
  208.     }
  209.     if (ki.chScan == 0xe0) {
  210.         if (!ki.chChar) {
  211.             ki.chScan = 0;
  212.             KbdCharIn (&ki, IO_WAIT, 0);
  213.             if (!ki.chScan) {                    /* Still no scan?     */
  214.                 ki.chScan = ki.chChar;            /* Move new char over*/
  215.                 ki.chChar = 0;                    /* Force its return  */
  216.             } else {
  217.                 ki.chChar = 0;                    /* Force new scan     */
  218.             }
  219.         } else {
  220.             ki.chScan = 0;                        /* Handle 0xe00d case*/
  221.         }
  222.     }
  223.  
  224.     if (ki.chChar)
  225.         ki.chScan = 0;
  226.     if ((ki.chScan >= 0x3b) && (ki.chScan <= 0x44))
  227.         macro = macros[ki.chScan - 0x3a];
  228.     else if ((ki.chScan >= 0x54) && (ki.chScan <= 0x71))
  229.         macro = macros[ki.chScan - 0x49];
  230.  
  231.     if (macro != (void *) NULL) {
  232.         if (*macro)
  233.             return(*macro);
  234.  
  235.         macro = (void *) NULL;
  236.     }
  237.  
  238.     return((unsigned int)((ki.chScan<<8)+(ki.chChar)));
  239. }
  240.  
  241. void pascal bputs(char *s)
  242.  
  243. {
  244.     unsigned int *linebase = screen + ((((cy - 1) * maxx) + (cx - 1)));
  245. /*    unsigned int d; */
  246.     char *t = s;
  247.     char ch;
  248.     unsigned int a = current_color;
  249.     unsigned int l, l1 = 0;
  250.     unsigned int limit = maxx - 1;
  251.  
  252.     if (s == NULL)
  253.         return;
  254.  
  255. /*    d = current_color << 8; */
  256.  
  257.     if ((t = strchr(s,'\n')) != NULL) {
  258.         ch = *t;
  259.         *t = '\0';
  260.     }
  261.  
  262.     if ((strlen(s) + cx) > maxx) {
  263.         if (t)
  264.             *t = ch;
  265.         ch = *(t = s + maxx - cx + 1);
  266.         *t = '\0';
  267.     }
  268.  
  269.     if (strlen(s)) {
  270.         l1 = l = (((strlen(s) + cx) < maxx)?strlen(s):maxx - cx + 1);
  271. /*        cx += l; */
  272. /*        VioWrtCharStrAtt(s,l,cy - 1,cx - l - 1, (PBYTE)&a,0); */
  273.         VioWrtCharStrAtt((char *) s,l,cy - 1,cx - 1, (PBYTE) &a,0);
  274.     }
  275.     if (t)
  276.         *t = ch;
  277.     cx += l1;
  278. }
  279.  
  280. void pascal cls()
  281.  
  282. {
  283.     scrollup(1, 1, maxx, maxy, 0);
  284.     gotoxy(1, 1);
  285. }
  286.  
  287. void pascal clrwnd(x1, y1, x2, y2)
  288.     int     x1, y1, x2, y2;
  289.  
  290. {
  291.     scrollup(x1, y1, x2, y2, 0);
  292.     gotoxy(x1,y1);
  293. }
  294.  
  295. void pascal clreol()
  296. {
  297.     int x = cx;
  298. /*    clrwnd(cx,cy,maxx,cy); */
  299.     clrwnd(x,cy,maxx,cy);
  300.     cx = x;
  301.  
  302. }
  303.  
  304. int pascal bgets(char *s1, int c, int w)
  305.  
  306. {
  307.     int     ch;
  308.     int     x1;
  309.     char   *t;
  310.     static    insert = ON;
  311.     int     y = wherey();
  312.     int     x = wherex();
  313.     int     ofs = 0;
  314.     char    *o;
  315.  
  316.     o = strdup(s1);
  317.     w = (w+x)>maxx?maxx-x:w;
  318.  
  319.     memset(s, 0, sizeof s);
  320.     strcpy(s, s1);
  321.     t = s + strlen(s);
  322.     *t = '\0';
  323.     bputs(s+ofs);
  324.  
  325.     while (TRUE) {            /* endless loop */
  326.         video_update();
  327.         switch (ch = getkey()) {
  328.  
  329.         case UP:
  330.         case DOWN:
  331.         case PGUP:
  332.         case PGDN:
  333.         case ENTER:
  334.             free(o);
  335.             strcpy(s1, s);
  336.             return (ch);
  337.  
  338.         case ABORT:
  339.             strcpy(s1, o);
  340.             free(o);
  341.             gotoxy(x,y);
  342.             clreol();
  343.             bputs(s1);
  344.             return(ch);
  345.  
  346.         case WORDRT:
  347.             break;
  348.  
  349.         case WORDLT:
  350.             break;
  351.  
  352.         case CANCEL:
  353.             memset(s, 0, sizeof s);
  354.             t = s;
  355.             gotoxy(x, y);
  356.             clreol();
  357.             break;
  358.  
  359.         case GOBOL:
  360.             t = s;
  361.             ofs = 0;
  362.             gotoxy(x, y);
  363.             break;
  364.  
  365.         case GOEOL:
  366.             t = s + strlen(s);
  367.             gotoxy(x + strlen(s) - ofs, y);
  368.             break;
  369.  
  370.         case RUBOUT:
  371.         case BKSPC:
  372.             if (x == wherex())
  373.                 break;
  374.             t--;
  375.             memmove(t, (t + 1), strlen(t) + 1);
  376.             gotoxy(wherex() - 1, y);
  377.             x1 = wherex();
  378.             bputs(t);
  379.             bputc(' ');
  380.             gotoxy(x1, y);
  381.             break;
  382.  
  383.         case LEFT:
  384.             if (t == s)
  385.                 break;
  386.             t--;
  387.             gotoxy(wherex() - 1, y);
  388.             break;
  389.  
  390.         case RIGHT:
  391.             if (t >= (s + strlen(s)))
  392.                 break;
  393.             t++;
  394.             gotoxy(wherex() + 1, y);
  395.             break;
  396.  
  397.         case DELCHR:
  398.             memmove(t, t + 1, strlen(t) + 1);
  399.             x1 = wherex();
  400.             bputs(t);
  401.             bputc(' ');
  402.             gotoxy(x1, y);
  403.             break;
  404.  
  405.         case INSERT:
  406.             insert = !insert;
  407.             break;
  408.  
  409.         default:
  410.             if (!(ch & 0xff) || (strlen(s) == c))
  411.                 break;
  412.             if (insert) {
  413.                 x1 = wherex();
  414.                 t++;
  415.                 strins(s, (char) (ch & 0xff), (x1 - x + 1));
  416.                 gotoxy(x, y);
  417.                 bputs(s+ofs);
  418.                 if (ofs)
  419.                     gotoxy(x1, y);
  420.                 else
  421.                     gotoxy(x1 + 1, y);
  422.             }
  423.             else {
  424.                 *t++ = (char) (ch & 0xFF);
  425.                 bputc((char) (ch & 0xff));
  426.             }
  427.             video_update();
  428.             break;
  429.         }
  430.  
  431.         if (ofs) {
  432.             gotoxy(x,y);
  433.             bputs(s+ofs);
  434.         }
  435.     }
  436. }
  437.  
  438. int pascal getnum(int l, int h, int value)
  439.  
  440. {
  441.     int     i, x;
  442.     char    s[TEXTLEN];
  443.  
  444.     i = value;
  445.     x = wherex();
  446.     do {
  447.         clreol();
  448.         memset(s, 0, sizeof(s));
  449.         itoa(i, s, 10);
  450.         gotoxy(x, wherey());
  451.         bgets(s, TEXTLEN/2, TEXTLEN/2);
  452.         i = atoi(s);
  453.     } while ((i < l) || (i > h));
  454.     return (i);
  455. }
  456.  
  457. void pascal set_color(unsigned int attr)
  458.  
  459. {
  460.     current_color = (unsigned char) attr;
  461. }
  462.  
  463. unsigned pascal get_color()
  464.  
  465. {
  466.     return(current_color);
  467. }
  468.  
  469. int cdecl bprintf(char *f,...)
  470.  
  471. {
  472.     va_list params;
  473.     int     i;
  474.  
  475.     va_start(params, f);
  476.     i = vsprintf(s, f, params);
  477.     bputs(s);
  478.     return (i);
  479. }
  480.  
  481.  
  482. CURSOR _cursor;
  483.  
  484. static void pascal vfossil_cursor (int st)
  485. {
  486.     CURSOR *q;
  487.  
  488.     q = (CURSOR *) &_cursor;
  489.     /* We can make the cursor go away */
  490.     VioGetCurType (q, 0);
  491.     _cursor.cur_attr = st ? 0 : -1;
  492.     VioSetCurType (q, 0);
  493. }
  494.  
  495.