home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / drivers.cpp < prev    next >
C/C++ Source or Header  |  1998-05-03  |  11KB  |  250 lines

  1. /*
  2.  * drivers.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. /*
  13.  * SS: Some non-portable code changed.
  14.  */
  15.  
  16. #define Uses_TKeys
  17. #define Uses_TDrawBuffer
  18. #include <tvision/tv.h>
  19.  
  20. /*------------------------------------------------------------------------*/
  21. /*                                                                        */
  22. /*  TDrawBuffer::moveBuf                                                  */
  23. /*                                                                        */
  24. /*  arguments:                                                            */
  25. /*                                                                        */
  26. /*      indent - character position within the buffer where the data      */
  27. /*               is to go                                                 */
  28. /*                                                                        */
  29. /*      source - far pointer to an array of character/attribute pairs     */
  30. /*                                                                        */
  31. /*      attr   - attribute to be used for all characters (0 to retain     */
  32. /*               the attribute from 'source')                             */
  33. /*                                                                        */
  34. /*      count   - number of character/attribute pairs to move             */
  35. /*                                                                        */
  36. /*------------------------------------------------------------------------*/
  37.  
  38. void TDrawBuffer::moveBuf( ushort indent, const void *source,
  39.                            ushort attr, ushort count )
  40.  
  41. {
  42.     register ushort *dest = &data[indent];
  43.     register uchar *s = (uchar *)source;
  44.  
  45.     if (attr != 0)
  46.         for (; count; --count, ++s, ++dest)
  47.         {
  48.         *dest = *s | ((attr & 0xff) << 8);
  49. //            ((uchar*)dest)[0] = *s;
  50. //            ((uchar*)dest)[1] = (uchar)attr;
  51.         }
  52.     else
  53.         while (count--)
  54.             *(uchar *)dest++ = *s++;
  55. }
  56.  
  57. /*------------------------------------------------------------------------*/
  58. /*                                                                        */
  59. /*  TDrawBuffer::moveChar                                                 */
  60. /*                                                                        */
  61. /*  arguments:                                                            */
  62. /*                                                                        */
  63. /*      indent  - character position within the buffer where the data     */
  64. /*                is to go                                                */
  65. /*                                                                        */
  66. /*      c       - character to be put into the buffer                     */
  67. /*                                                                        */
  68. /*      attr    - attribute to be put into the buffer                     */
  69. /*                                                                        */
  70. /*      count   - number of character/attribute pairs to put into the     */
  71. /*                buffer                                                  */
  72. /*                                                                        */
  73. /*------------------------------------------------------------------------*/
  74.  
  75. void TDrawBuffer::moveChar( ushort indent, char c, ushort attr, ushort count )
  76. {
  77.     register ushort *dest = &data[indent];
  78.  
  79.     if (attr != 0)
  80.         for (; count; --count, ++dest)
  81.             {
  82.         if (c != 0) *dest = (*dest & 0xff00) | c;
  83.         *dest = (*dest & 0x00ff) | ((attr & 0xff) << 8);
  84. //            if (c) ((uchar*)dest)[0] = c;
  85. //            ((uchar*)dest)[1] = (uchar)attr;
  86.             }
  87.     else
  88.         while (count--)
  89.             *(uchar *)dest++ = c;
  90. }
  91.  
  92. /*------------------------------------------------------------------------*/
  93. /*                                                                        */
  94. /*  TDrawBuffer::moveCStr                                                 */
  95. /*                                                                        */
  96. /*  arguments:                                                            */
  97. /*                                                                        */
  98. /*      indent  - character position within the buffer where the data     */
  99. /*                is to go                                                */
  100. /*                                                                        */
  101. /*      str     - pointer to a 0-terminated string of characters to       */
  102. /*                be moved into the buffer                                */
  103. /*                                                                        */
  104. /*      attrs   - pair of text attributes to be put into the buffer       */
  105. /*                with each character in the string.  Initially the       */
  106. /*                low byte is used, and a '~' in the string toggles       */
  107. /*                between the low byte and the high byte.                 */
  108. /*                                                                        */
  109. /*------------------------------------------------------------------------*/
  110.  
  111. void TDrawBuffer::moveCStr( ushort indent, const char *str, ushort attrs)
  112. {
  113.     register ushort *dest = &data[indent];
  114.     int toggle;
  115.     uchar c, curAttr;
  116.  
  117.     for (curAttr= attrs & 0xff, toggle=1; (c=*str) != 0; str++)
  118. //    for (curAttr=((uchar *)&attrs)[0], toggle=1; (c=*str) != 0; str++)
  119.         {
  120.         if (c == '~')
  121.             {
  122.         if (toggle == 0) curAttr = attrs & 0xff;
  123.         else curAttr = (attrs & 0xff00) >> 8;
  124. //            curAttr = ((uchar *)&attrs)[toggle];
  125.             toggle = 1-toggle;
  126.             }
  127.         else
  128.             {
  129.         *dest = (curAttr << 8) | c;
  130. //            ((uchar*)dest)[0] = c;
  131. //            ((uchar*)dest)[1] = curAttr;
  132.             dest++;
  133.             }
  134.         }
  135. }
  136.  
  137. /*------------------------------------------------------------------------*/
  138. /*                                                                        */
  139. /*  TDrawBuffer::moveStr                                                  */
  140. /*                                                                        */
  141. /*  arguments:                                                            */
  142. /*                                                                        */
  143. /*      indent  - character position within the buffer where the data     */
  144. /*                is to go                                                */
  145. /*                                                                        */
  146. /*      str     - pointer to a 0-terminated string of characters to       */
  147. /*                be moved into the buffer                                */
  148. /*                                                                        */
  149. /*      attr    - text attribute to be put into the buffer with each      */
  150. /*                character in the string.                                */
  151. /*                                                                        */
  152. /*------------------------------------------------------------------------*/
  153.  
  154. void TDrawBuffer::moveStr( ushort indent, const char *str, ushort attr )
  155. {
  156.     register ushort *dest = &data[indent];
  157.     uchar c;
  158.  
  159.     if (attr != 0)
  160.         for (;(c=*str) != 0; ++str, ++dest)
  161.             {
  162.         *dest = ((attr & 0xff) << 8) | c;
  163. //            ((uchar*)dest)[0] = c;
  164. //            ((uchar*)dest)[1] = (uchar)attr;
  165.             }
  166.         else
  167.             while (*str)
  168.                 *(uchar *)dest++ = *str++;
  169. }
  170.  
  171. /*------------------------------------------------------------------------*/
  172. /*                                                                        */
  173. /*  ctrlToArrow                                                           */
  174. /*                                                                        */
  175. /*  argument:                                                             */
  176. /*                                                                        */
  177. /*      keyCode - scan code to be mapped to keypad arrow code             */
  178. /*                                                                        */
  179. /*  returns:                                                              */
  180. /*                                                                        */
  181. /*      scan code for arrow key corresponding to Wordstar key,            */
  182. /*      or original key code if no correspondence exists                  */
  183. /*                                                                        */
  184. /*------------------------------------------------------------------------*/
  185. ushort ctrlToArrow(ushort keyCode)
  186. {
  187.  
  188. #ifndef __UNPATCHED
  189. static const uchar ctrlCodes[] =
  190.         {
  191.     kbCtrlS, kbCtrlD, kbCtrlE, kbCtrlX, kbCtrlA,
  192.     kbCtrlF, kbCtrlG, kbCtrlV, kbCtrlR, kbCtrlC, kbCtrlH
  193.     };
  194.  
  195. static const ushort arrowCodes[] =
  196.     {
  197.     kbLeft, kbRight, kbUp, kbDown, kbHome,
  198.     kbEnd,  kbDel,   kbIns,kbPgUp, kbPgDn, kbBack
  199.     };
  200. #else
  201. const uchar ctrlCodes[] =
  202.     {
  203.     kbCtrlS, kbCtrlD, kbCtrlE, kbCtrlX, kbCtrlA,
  204.     kbCtrlF, kbCtrlG, kbCtrlV, kbCtrlR, kbCtrlC, kbCtrlH
  205.     };
  206.  
  207. const ushort arrowCodes[] =
  208.     {
  209.     kbLeft, kbRight, kbUp, kbDown, kbHome,
  210.     kbEnd,  kbDel,   kbIns,kbPgUp, kbPgDn, kbBack
  211.     };
  212. #endif
  213.  
  214. //    for( int i = 0; i < sizeof(ctrlCodes); i++ ) /* XXX */
  215.     for( int i = 0; i < (int)sizeof(ctrlCodes); i++ ) /* XXX */
  216.         if( (keyCode & 0x00ff) == ctrlCodes[i] )
  217.             return arrowCodes[i];
  218.     return keyCode;
  219. }
  220.  
  221. /*------------------------------------------------------------------------*/
  222. /*                                                                        */
  223. /*  cstrlen                                                               */
  224. /*                                                                        */
  225. /*  argument:                                                             */
  226. /*                                                                        */
  227. /*      s       - pointer to 0-terminated string                          */
  228. /*                                                                        */
  229. /*  returns                                                               */
  230. /*                                                                        */
  231. /*      length of string, ignoring '~' characters.                        */
  232. /*                                                                        */
  233. /*  Comments:                                                             */
  234. /*                                                                        */
  235. /*      Used in determining the displayed length of command strings,      */
  236. /*      which use '~' to toggle between display attributes                */
  237. /*                                                                        */
  238. /*------------------------------------------------------------------------*/
  239.  
  240. int cstrlen( const char *s )
  241. {
  242.     int len = 0;
  243.     while( *s != EOS )
  244.         {
  245.         if( *s++ != '~' )
  246.             len++;
  247.         }
  248.     return len;
  249. }
  250.