home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / OS2DISP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-05  |  11.5 KB  |  423 lines

  1. /*
  2.     os2disp.c
  3.  
  4.     % OS/2 VIO text mode device interface functions.
  5.  
  6.     11/11/88  by Ted.
  7.  
  8.     OWL 1.2
  9.     Copyright (c) 1988, by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      1/14/89 ted    Removed redundant disp clips.
  15.      1/18/89 Ted    changed call to PlotText for simplified interface.
  16.      4/09/89 ted    converted for static digdata.
  17.      4/22/89 ted    added digpriv's.
  18.      9/11/89 gam    added some casts for Version 1.1
  19.  
  20.      2/28/90 ted    fixed invalid os2data->h references in CACHE/FLUSH methods.
  21.      3/29/90 ted    added DC_HIDE/SHOWMOUSE msgs.
  22.      3/29/90 ted    added vio_readattr to tweak clear color for scrolling.
  23.      4/05/90 jmd    ansi-fied
  24.      9/15/90 ted    added DC_PV... cases to Control function.
  25.     10/04/90 ted    added casts to eliminate MC6.0 signed/unsigned warnings.
  26. */
  27.  
  28. #include "os2priv.h"
  29.  
  30. #ifdef OAK_OS2            /* Only compile this for OS2 */
  31.  
  32. #define MAXPLOTLEN    132        /* the maximum length string PlotText can do */
  33.  
  34. OSTATIC void DIGPRIV vio_setfuncs(dig_struct *digp);
  35. OSTATIC void DIGPRIV vio_cellcolor(opixval color, byte *cell);
  36.  
  37. OSTATIC dig_dControl_func        (vio_dControl);
  38. OSTATIC dig_pPlotText_func        (vio_PlotText);
  39. OSTATIC dig_pDrawCursor_func    (vio_DrawCursor);
  40. OSTATIC dig_pClear_func            (vio_Clear);
  41. OSTATIC dig_pScrollBoxVt_func    (vio_ScrollBoxVt);
  42. OSTATIC dig_pScrollBoxHz_func    (vio_ScrollBoxHz);
  43.  
  44. OSTATIC void DIGPRIV vio_ReadCharAttrbuf(ptd_struct *ptd, opcoord x, opcoord y, char *charbuf, byte *attrbuf, int slen);
  45. OSTATIC byte DIGPRIV vio_readattr(opcoord x, opcoord y);
  46. OSTATIC unsigned os2_dDummy(void);
  47.  
  48. /* -------------------------------------------------------------------------- */
  49.  
  50. boolean os2_ModeText(dig_struct *digp)
  51. {
  52.     if (!os2_OpenDIG(digp, NULL)) {
  53.         return(FALSE);
  54.     }
  55.     vio_setfuncs(digp);
  56.     return(TRUE);
  57. }
  58. /* -------------------------------------------------------------------------- */
  59.  
  60. static void DIGPRIV vio_setfuncs(dig_struct *digp)
  61. {
  62.     digp->CloseDIG = os2_CloseDIG;
  63.     digp->dControl      = vio_dControl;
  64.  
  65.     digp->pPlotText     = vio_PlotText;
  66.     digp->pDrawCursor   = vio_DrawCursor;
  67.     digp->pClear        = vio_Clear;
  68.     digp->pScrollBoxVt  = vio_ScrollBoxVt;
  69.     digp->pScrollBoxHz  = vio_ScrollBoxHz;
  70.  
  71.     /* in hardware cursor text modes, no pixmap funcs are needed */
  72.     digp->pDrawPixmap   = (dig_pDrawPixmap_func ((*))) os2_dDummy;
  73.     digp->pReadPixmap   = (dig_pReadPixmap_func ((*))) os2_dDummy;
  74.     digp->pControl      = (dig_pControl_func ((*))) os2_dDummy;
  75.  
  76.     digp->vtscroll = TRUE;
  77.     digp->hzscroll = TRUE;
  78.     digp->textfree = FALSE;
  79.     digp->evcheck  = TRUE;
  80.     digp->type = 0;
  81.     digp->version = 0;
  82. }
  83. /* -------------------------------------------------------------------------- */
  84.  
  85. static void DIGPRIV vio_cellcolor(opixval color, byte *cell)
  86. /*
  87.     Return a 16 bit value which can be repeated to clear a region to the
  88.     given color.
  89. */
  90. {
  91.     cell[0] = ' ';
  92.     /* put color val in bgcolor slot, and invert it for fgcolor slot */
  93.     cell[1] = (byte) color & (byte) 0x07;
  94.     cell[1] = (cell[1] ^ (byte) 0x07) | (byte)(cell[1] << 4);
  95. }
  96. /* -------------------------------------------------------------------------- */
  97.  
  98. static int vio_dControl(dig_dcmsg msg, VOID *indata, VOID *outdata)
  99. {
  100.     setattr_struct *pvs;
  101.  
  102.     switch (msg) {
  103.     case DC_GETINFO:
  104.         *((dispinfo_struct **)(outdata)) = &os2data->info;
  105.         break;
  106.     case DC_INITMODE:
  107.         os2_initmode();
  108.         break;
  109.     case DC_RESTOREMODE:
  110.         VioSetCurPos(os2data->oldcursy, os2data->oldcursx, os2data->hvio);
  111.         os2_vsetcursortype(CURSOR_NORMAL);
  112.         break;
  113.     case DC_SETATTRCOLS:
  114.         pvs = (setattr_struct *)indata;
  115.  
  116.         os2data->attrmap[pvs->attr] =  (byte) pvs->fg & (byte) 0x0F;
  117.         os2data->attrmap[pvs->attr] |= (byte) pvs->bg << 4;
  118.         break;
  119.     case DC_GETATTRFG:
  120.         *((opixval *) outdata) = (opixval)
  121.             (os2data->attrmap[*((byte *)indata)] & 0x0F);
  122.         break;
  123.     case DC_GETATTRBG:
  124.         *((opixval *) outdata) = (opixval)
  125.             (os2data->attrmap[*((byte *)indata)] >> 4);
  126.         break;
  127.     case DC_SETCOLMAP:
  128.     case DC_GETCOLMAP:
  129.         break;
  130.  
  131.     case DC_PVFINDRGB:    /* indata is (findcol_struct *), outdata is (opixval *), ret is int dist or -1 for failure */
  132.         return(-1);
  133.     case DC_PVALLOC:    /* indata is void, outdata is (opixval *), ret is TRUE/FALSE */
  134.     case DC_PVMAKERO:    /* indata is (opixval *), outdata is void, ret is void */
  135.     case DC_PVFREE:        /* indata is (opixval *), outdata is void, ret is void */
  136.         return(FALSE);
  137.  
  138.     case DC_OPENFONT:
  139.           memmove((VOID *)(ofont_type)outdata, (VOID *)&os2data->info.def_font,
  140.                 sizeof(struct ofont_struct));
  141.           memmove((VOID *)(&((ofont_type)outdata)->req), (VOID *)indata,
  142.                 sizeof(fontdesc_struct));
  143.         break;
  144.     case DC_READCHARATTR:
  145.         {    ptarg_struct *pta = (ptarg_struct *)indata;
  146.             vio_ReadCharAttrbuf(pta->ptd, pta->x, pta->y,
  147.                                     pta->charbuf, pta->attrbuf, pta->slen);
  148.         }
  149.         break;
  150. #ifdef OAK_OS2FAPI
  151.     case DC_HIDEMOUSE:
  152.     case DC_CACHE:
  153.         if (os2data->dosbox) {
  154.             if (pchdata->ismouse) {
  155.                 OREGS regs;
  156.                 regs.x.ax = BMOU_HIDE;
  157.                 oakint86(BIOS_MOUSEINT, ®s);
  158.             }
  159.         }
  160.         else {
  161.             if (os2hdata->ismouse) {
  162.                 os2_dohide();
  163.             }
  164.         }
  165.         break;
  166.     case DC_SHOWMOUSE:
  167.     case DC_FLUSH:
  168.         if (os2data->dosbox) {
  169.             if (pchdata->ismouse) {
  170.                 OREGS regs;
  171.                 regs.x.ax = BMOU_SHOW;
  172.                 oakint86(BIOS_MOUSEINT, ®s);
  173.             }
  174.         }
  175.         else {
  176.             if (os2hdata->ismouse) {
  177.                 os2_doshow();
  178.             }
  179.         }
  180.         break;
  181. #else
  182.     case DC_HIDEMOUSE:
  183.     case DC_CACHE:
  184.         if (os2hdata->ismouse) {
  185.             os2_dohide();
  186.         }
  187.         break;
  188.     case DC_SHOWMOUSE:
  189.     case DC_FLUSH:
  190.         if (os2hdata->ismouse) {
  191.             os2_doshow();
  192.         }
  193.         break;
  194. #endif
  195.     }
  196.     return(TRUE);
  197. }
  198. /* -------------------------------------------------------------------------- */
  199.  
  200. static void vio_PlotText(ptd_struct *ptd, opcoord x, opcoord y, char *charbuf, char rchar, byte rattr, int slen)
  201. {
  202.     win_type win;
  203.     opbox clipbox;
  204.     int delta;
  205.     byte tbuf[2*MAXPLOTLEN];
  206.  
  207.     win = ptd->win;
  208.  
  209.     opbox_copy(&clipbox, ptd->relboxp);
  210.     opbox_trans(&clipbox, win_GetXmin(win), win_GetYmin(win));
  211.     x += win_GetXmin(win);
  212.     y += win_GetYmin(win);
  213.  
  214.     delta = opbox_clipstring(&clipbox, &x, &y, &slen, win_GetFont(win));
  215.     if (slen <= 0) {
  216.         return;
  217.     }
  218.     if (charbuf != NULL) charbuf += delta;
  219.  
  220.     y -= 1;        /* Move y up to top of char box for vio cell address */
  221.  
  222.     rattr = os2data->attrmap[rattr];
  223.  
  224. /* Index into buf for clipping. */
  225.     if (charbuf != NULL) {
  226.     /* Draw the string, interleaving chars with rattr */
  227.         VioWrtCharStrAtt(charbuf, slen, y, x, &rattr, os2data->hvio);
  228.     }
  229.     else {    /* no charbuf */
  230.     /* Draw a string of rchar/rattr pairs */
  231.         tbuf[0] = rchar;
  232.         tbuf[1] = rattr;
  233.         VioWrtNCell(tbuf, slen, y, x, os2data->hvio);
  234.     }
  235. }
  236. /* -------------------------------------------------------------------------- */
  237.  
  238. static void vio_DrawCursor(ptd_struct *ptd, cursortype ctype)
  239. {
  240.     win_type win;
  241.     opbox clipbox;
  242.     int row, col;
  243.     ofont_type font;
  244.     opcoord x;
  245.     opcoord y;
  246.     int slen;
  247.  
  248.     win = ptd->win;
  249.     font = win_GetFont(win);
  250.  
  251.     opbox_copy(&clipbox, ptd->relboxp);
  252.     opbox_trans(&clipbox, win_GetXmin(win), win_GetYmin(win));
  253.     x = win_GetXmin(win) + win_GetCursx(win);
  254.     y = win_GetYmin(win) + win_GetCursy(win);
  255.  
  256.     col = opcoord_GetXCol(x + ofont_GetWidth(font) - 1, font);    /* round in */
  257.     row = opcoord_GetYRow(y - 1, font);
  258.     x = col * ofont_GetWidth(font);
  259.     y = (row + 1) * ofont_GetHeight(font);
  260.  
  261.     slen = 1;
  262.     opbox_clipstring(&clipbox, &x, &y, &slen, font);
  263.     if (slen <= 0) {
  264.         return;
  265.     }
  266.     if (ctype != CURSOR_NONE) {
  267.         VioSetCurPos(row, col, os2data->hvio);
  268.     }
  269.     if (os2data->curctype != ctype) {
  270.         os2_vsetcursortype(ctype);
  271.     }
  272. }
  273. /* -------------------------------------------------------------------------- */
  274.  
  275. static void vio_Clear(ptd_struct *ptd, opixval color)
  276. {
  277.     win_type win;
  278.     opbox scrbox;
  279.     byte cell[2];
  280.  
  281.     win = ptd->win;
  282.  
  283.     opbox_copy(&scrbox, ptd->relboxp);
  284.     opbox_trans(&scrbox, win_GetXmin(win), win_GetYmin(win));
  285.  
  286.     vio_cellcolor(color, cell);
  287.  
  288.     VioScrollUp(scrbox.ymin, scrbox.xmin,
  289.                 scrbox.ymax-1, scrbox.xmax-1,
  290.                 opbox_GetHeight(&scrbox), cell, os2data->hvio);
  291. }
  292. /* -------------------------------------------------------------------------- */
  293. static byte blank[2] = {0x20, 0x07};
  294. /* -------------------------------------------------------------------------- */
  295.  
  296. static void vio_ScrollBoxVt(ptd_struct *ptd, opcoord nrows)
  297. {
  298.     win_type win;
  299.     opbox scrbox;
  300.  
  301.     if (nrows == 0) return;
  302.  
  303.     win = ptd->win;
  304.  
  305.     opbox_copy(&scrbox, ptd->relboxp);
  306.     opbox_trans(&scrbox, win_GetXmin(win), win_GetYmin(win));
  307.  
  308.     if (nrows > 0) {
  309.         if ((odim) nrows >= opbox_GetHeight(&scrbox)) return;    /* quit if scroll all */
  310.  
  311.         blank[1] = vio_readattr(scrbox.xmin, scrbox.ymax - 1);
  312.         VioScrollUp(scrbox.ymin, scrbox.xmin,
  313.                     scrbox.ymax-1, scrbox.xmax-1,
  314.                     nrows, blank, os2data->hvio);
  315.     }
  316.     else if (nrows < 0) {
  317.         nrows = -nrows;
  318.         if ((odim) nrows >= opbox_GetHeight(&scrbox)) return;    /* quit if scroll all */
  319.  
  320.         blank[1] = vio_readattr(scrbox.xmin, scrbox.ymin);
  321.         VioScrollDn(scrbox.ymin, scrbox.xmin,
  322.                     scrbox.ymax-1, scrbox.xmax-1,
  323.                     nrows, blank, os2data->hvio);
  324.     }
  325. }
  326. /* -------------------------------------------------------------------------- */
  327.  
  328. static void vio_ScrollBoxHz(ptd_struct *ptd, opcoord ncols)
  329. {
  330.     win_type win;
  331.     opbox scrbox;
  332.  
  333.     if (ncols == 0) return;
  334.  
  335.     win = ptd->win;
  336.  
  337.     opbox_copy(&scrbox, ptd->relboxp);
  338.     opbox_trans(&scrbox, win_GetXmin(win), win_GetYmin(win));
  339.  
  340.     if (ncols > 0) {
  341.         if ((odim) ncols >= opbox_GetWidth(&scrbox)) return;    /* quit if scroll all */
  342.  
  343.         blank[1] = vio_readattr(scrbox.xmax - 1, scrbox.ymin);
  344.         VioScrollLf(scrbox.ymin, scrbox.xmin,
  345.                     scrbox.ymax-1, scrbox.xmax-1,
  346.                     ncols, blank, os2data->hvio);
  347.     }
  348.     else if (ncols < 0) {
  349.         ncols = -ncols;
  350.         if ((odim) ncols >= opbox_GetWidth(&scrbox)) return;    /* quit if scroll all */
  351.  
  352.         blank[1] = vio_readattr(scrbox.xmin, scrbox.ymin);
  353.         VioScrollRt(scrbox.ymin, scrbox.xmin,
  354.                     scrbox.ymax-1, scrbox.xmax-1,
  355.                     ncols, blank, os2data->hvio);
  356.     }
  357. }
  358. /* -------------------------------------------------------------------------- */
  359.  
  360. static void DIGPRIV vio_ReadCharAttrbuf(ptd_struct *ptd, opcoord x, opcoord y, char *charbuf, byte *attrbuf, int slen)
  361. {
  362.     win_type win;
  363.     opbox clipbox;
  364.     int delta;
  365.     byte tbuf[2*MAXPLOTLEN];
  366.  
  367.     win = ptd->win;
  368.  
  369.     opbox_copy(&clipbox, ptd->relboxp);
  370.     opbox_trans(&clipbox, win_GetXmin(win), win_GetYmin(win));
  371.     x += win_GetXmin(win);
  372.     y += win_GetYmin(win);
  373.  
  374.     delta = opbox_clipstring(&clipbox, &x, &y, &slen, win_GetFont(win));
  375.     if (slen <= 0) {
  376.         return;
  377.     }
  378.     if (charbuf != NULL) charbuf += delta;
  379.     if (attrbuf != NULL) attrbuf += delta;
  380.  
  381.     if (attrbuf == NULL || charbuf == NULL) {
  382.         return;
  383.     }
  384.  
  385.     /* Read the string in from display in char/attr form */
  386.     if (slen > MAXPLOTLEN) slen = MAXPLOTLEN;
  387.     slen *= 2;
  388.     VioReadCellStr(tbuf, (PUSHORT)&slen, (USHORT)y-1, (USHORT)x, os2data->hvio);
  389.     slen /= 2;
  390.  
  391.     /* Separate chars and attrs */
  392.     for (delta = 0; slen > 0; slen--) {
  393.         *charbuf++ = (char)tbuf[delta++];
  394.         *attrbuf++ = tbuf[delta++];
  395.     }
  396. }
  397. /* -------------------------------------------------------------------------- */
  398.  
  399. static byte DIGPRIV vio_readattr(opcoord x, opcoord y)
  400. /* Return the text attribute found at the screen character position x,y. */
  401. {
  402.     byte cell[2];
  403.     unsigned slen;
  404.  
  405.     slen = 1;
  406.     VioReadCellStr(cell, (PUSHORT)&slen, (USHORT)y, (USHORT)x, os2data->hvio);
  407.     return(cell[1]);
  408. }
  409. /* -------------------------------------------------------------------------- */
  410.  
  411. static unsigned os2_dDummy()
  412. {
  413.     return(0);
  414. }
  415. /* -------------------------------------------------------------------------- */
  416.  
  417. #else    /* ifndef OAK_OS2 */
  418. /* A dummy so compilers won't freak if everything else is ifdef'ed out. */
  419.     int os2textdummy(void);
  420.     int os2textdummy() { return(0); }
  421. #endif
  422.  
  423.