home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tvos200.zip / TVISION / TVOS2.CPP < prev    next >
C/C++ Source or Header  |  1995-04-25  |  13KB  |  362 lines

  1. /*
  2.  *  tvos2.cpp:  Turbo Vision OS/2 functions
  3.  *  =======================================
  4.  *  (C)1995 by F.Jalvingh; All rights reserved.
  5.  *
  6.  *  This module contains C++ versions of original assembler code.
  7.  *
  8.  *  $Header$
  9.  *
  10.  *  $Log$
  11.  */
  12. #ifdef  __OS2__
  13.  
  14. #define INCL_OS2
  15. #define INCL_BASE
  16. #define INCL_SUB
  17. #define INCL_DOSFILEMGR
  18. #define INCL_DOSMISC
  19.  
  20. #define Uses_TView
  21. #define Uses_TGroup
  22. #define Uses_TFrame
  23. #define Uses_TDrawBuffer
  24. #define Uses_THardwareInfo
  25. #define Uses_TKeys
  26. #define Uses_TEventQueue
  27. #define Uses_TScreen
  28. #define Uses_TTerminal
  29.  
  30. #include    <tvision/tv.h>
  31. #include    <string.h>
  32.  
  33.  
  34.  
  35.  
  36. /*
  37.  *  resetCursor() displays the current cursor IF NEEDED in the view spec'd.
  38.  */
  39. void TView::resetCursor()
  40. {
  41.     TView           *v, *target;
  42.     TGroup          *o;
  43.     short           x, y;                   // Current cursor position.
  44.  
  45.     x           = cursor.x;
  46.     y           = cursor.y;
  47.     target      = this;
  48.  
  49.     if( ((sfVisible|sfCursorVis|sfFocused) & state) == (sfVisible|sfCursorVis|sfFocused))
  50.     {
  51.         /*
  52.          *  Loop 1: From the current view, then to it's OWNER until the topmost
  53.          *  owner is reached.. (for o = v; o != NULL; o = o->owner)
  54.          */
  55.         for(;;)
  56.         {
  57.             /*
  58.              *  View IS the visible active focused view && has a cursor ON. Is
  59.              *  the cursor within this-view's bounds?
  60.              */
  61.             if(x < 0 || y < 0 || x >= target->size.x || y >= target->size.y)
  62.                 break;                          // Exit: cursor is off bounds.
  63.  
  64.             x   += target->origin.x;
  65.             y   += target->origin.y;            // Make pos within owner.
  66.             o   = target->owner;                // Get owner,
  67.             if(o == NULL)                       // No more owners?
  68.             {
  69.                 /*
  70.                  *  Cursor is VISIBLE!! Set cursor type depending on the INS
  71.                  *  mode: if T, make a BLOCK cursor, else use the original
  72.                  *  cursor from TScreen::cursorLines..
  73.                  */
  74.                 if(state & sfCursorIns)         // Insert-mode cursor?
  75.                     THardwareInfo::setCaretSize(0x0700);
  76.                 else
  77.                     THardwareInfo::setCaretSize(TScreen::cursorLines);
  78.                 THardwareInfo::setCaretPosition(x, y);
  79.                 return;                         // Done!!!
  80.             }
  81.  
  82.             /**** Handle all views in the current GROUP ****/
  83.             v   = o->last;                      // Get 1st view in GROUP,
  84.             for(;;)
  85.             {
  86.                 v   = v->next;                  // Move to next (1st) view
  87.                 if(v == target) break;          // Exit if all done,
  88.  
  89.                 if(sfVisible & v->state)        // Is view visible?
  90.                 {
  91.                     /**** If the cursor is obscured by this view disable it, ****/
  92.                     if(x >= v->origin.x && x < v->origin.x+v->size.x &&
  93.                        y >= v->origin.y && y < v->origin.y+v->size.y)
  94.                         goto nocursor;
  95.                 }
  96.             }
  97.  
  98.             /*
  99.              *  Cursor is NOT obscured by one of this-group's views.. Move to
  100.              *  the OWNER...
  101.              */
  102.             target  = (TView *)o;               // Make target the owner,
  103.         }
  104.     }
  105.  
  106.     /**** The cursor is OFF!! ****/
  107. nocursor:
  108.     THardwareInfo::setCaretSize(0);             // Hide cursor.
  109. }
  110.  
  111.  
  112. /****************************************************************************/
  113. /*                                                                          */
  114. /*  CODING: writeView() is the main view writing agent.                     */
  115. /*                                                                          */
  116. /****************************************************************************/
  117. static void wvGroup(ushort *buf, short off, TView *target, short bx, short ex, short y);
  118.  
  119.  
  120. /*
  121.  *  wvViews() checks which parts of the region passed are obscured
  122.  *  by TViews within the master group. Each part is checked to see if it
  123.  *  reaches the screen. If so, the owner for this group is asked to display
  124.  *  the area.
  125.  *
  126.  *  It works as follows: each view, starting at <first>, is checked against the
  127.  *  area. If the area is obscured by ANY view, then it's obscured and we
  128.  *  return immediately (nothing is visible).
  129.  *  If the area is NOT obscured the next view is checked for intersection with
  130.  *  the area. This continues till the <target> view is reached: views after
  131.  *  the target view have a lower Zorder and are obscured by the target.
  132.  *
  133.  *  If the area is partially obscured each part is checked independently.
  134.  */
  135. static void wvViews(ushort *buf, short off, TView *v, TView *target, short bx, short ex, short y)
  136. {
  137.     TGroup  *o  = target->owner;
  138.     short   vex, vbx;
  139.     ushort  *p;
  140.  
  141.     for(;;)
  142.     {
  143.         v   = v->next;                      // Move to NEXT view (is 1st view 1st time),
  144.         if(v == target)                     // Last one reached?
  145.             break;
  146.  
  147.         /**** Check: does the current view intersect with the display item?? ****/
  148.         if(v->state & sfVisible)            // If visible,
  149.         {
  150.             if(y >= v->origin.y && y < v->origin.y + v->size.y) // In Y region?
  151.             {
  152.                 /*
  153.                  *  Y coordinates fit: this view's Y falls within the owner's Y.
  154.                  *  Now check if the X coordinates fall within the view..
  155.                  */
  156.                 vex = (vbx = v->origin.x) + v->size.x;
  157.                 if(bx >= vbx && ex <= vex) return;  // Completely obscured -> not exposed!
  158.  
  159.                 /*
  160.                  *  Does the current item intersect the current view? If not,
  161.                  *  there's nothing obscured...
  162.                  */
  163.                 if(ex >= vbx && bx < vex)       // Overlaps current view?
  164.                 {
  165.                     /**** Something is visible here. Check both sides, ****/
  166.                     if(bx < vbx)                    // Something visible at LHS?
  167.                     {
  168.                         /**** LHS is exposed. Is the RHS exposed too? ****/
  169.                         if(ex > vex)                // RHS also visible.
  170.                         {
  171.                             /*
  172.                              *  Auch! Check the RHS recursively!!!!
  173.                              */
  174.                             wvViews(buf, off, v, target, vex, ex, y);
  175.                         }
  176.  
  177.                         /**** The RHS was obscured somewhere. Make the LHS current. ****/
  178.                         ex  = vbx;
  179.                     }
  180.                     else if(ex > vex)               // Something visible at RHS?
  181.                     {
  182.                         /*
  183.                          *  Already certain that this is the ONLY part: the other
  184.                          *  part is obscured. So just continue to check this part.
  185.                          */
  186.                         bx  = vex;                  // Check area AFTER this view.
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.     }
  192.  
  193.     /*
  194.      *  The current part IS visible: display it in the group's buffer, the
  195.      *  screen oid. Then ask the OWNER of this to display the data.
  196.      */
  197.     p   = buf + bx - off;
  198.     if(o->options & ofPhysical)                 // This is the SCREEN group?
  199.     {
  200.         /**** Display whatever's left! ****/
  201.         THardwareInfo::cursorOff();
  202.         VioWrtCellStr((PCH) p, (ex-bx) * 2, y, bx, 0);
  203.         THardwareInfo::cursorOn();
  204.         return;
  205.     }
  206.     else if(o->buffer != 0)                     // Owner has BUFFER associated?
  207.     {
  208.         /**** Move to View's buffer, ****/
  209.         ushort  *d = (ushort *) o->buffer;
  210.         d   += y * o->size.x + bx;
  211.         memcpy(d, p, (ex-bx)*2);
  212.     }
  213.  
  214.     if(! o->lockFlag)                           // Not locked for redraw?
  215.     {
  216.         /**** Check whatever's left of the line in the OWNER'S owner.. ****/
  217.         wvGroup(buf, off, o, bx, ex, y);        // Ask OWNER to display area,
  218.         return;
  219.     }
  220. }
  221.  
  222.  
  223. /*
  224.  *  wvGroup() checks if the part of the line (bx to ex) is exposed on
  225.  *  the group.
  226.  *
  227.  *  To check, it starts to clip the part according to the part's owner TGroup,
  228.  *  then it traverses the owner's TView list. For each TView it checks to see
  229.  *  if that view obscures part(s) of the line. If part of the line is NOT
  230.  *  obscured that part is checked: a check is made if it's obscured
  231.  */
  232. static void wvGroup(ushort *buf, short off, TView *target, short bx, short ex, short y)
  233. {
  234.     TGroup  *o;
  235.  
  236.     if( (o = target->owner) == NULL) return;    // No owner-> exit.
  237.  
  238.     /**** Make view's position within owner, ****/
  239.     y   += target->origin.y;
  240.     bx  += target->origin.x;
  241.     ex  += target->origin.x;
  242.     off += target->origin.x;
  243.  
  244.     /**** Clip view area according to it's owner specification.. ****/
  245.     if(y < o->clip.a.y || y >= o->clip.b.y) return; // Not exposed (y out of Group)
  246.     if(bx < o->clip.a.x) bx = o->clip.a.x;          // Move x to start of region,
  247.     if(ex > o->clip.b.x) ex = o->clip.b.x;          // And idem for end,
  248.     if(bx >= ex) return;                            // Exit if x clipped out,
  249.  
  250.     /*
  251.      *  Ok, the part (bx-ex, y) is within the owner's clipping region. Now
  252.      *  check the children: for each child view of this group that's ABOVE
  253.      *  the <current> view check if it obscures part of the current line
  254.      *  part...
  255.      */
  256.     wvViews(buf, off, o->last, target, bx, ex, y);
  257. }
  258.  
  259.  
  260.  
  261. /*
  262.  *  TView::writeView() writes a line buffer to a view, taking care of the
  263.  *  clipping regions e.a. of the owner view. This function does the actual
  264.  *  screen write. It's hideously complex so take care when modifying it.
  265.  */
  266. void TView::writeView(short x, short y, short count, ushort *buf)
  267. {
  268.     short       ex;
  269.  
  270.     /**** 1. Check positions with regard to the view's boundaries ****/
  271.     if(y < 0 || y >= size.y) return;        // Out of Y boundaries.
  272.     if(x < 0) x = 0;                        // Make sure X starts within view
  273.  
  274.     ex      = x + count;                    // End x position + 1!!
  275.     if(ex >= size.x) ex = size.x;           // Truncate size to end,
  276.     if(x >= ex) return;                     // Exit if nothing to print,
  277.  
  278.     wvGroup(buf, x, this, x, ex, y);
  279. }
  280.  
  281.  
  282. /****************************************************************************/
  283. /*                                                                          */
  284. /*  CODING: TView main writing entrypoints, using writeView()..             */
  285. /*                                                                          */
  286. /****************************************************************************/
  287. /*
  288.  *  writeLine() writes the specified buffer to the line at (x,y). Only <w>
  289.  *  chars are written. The buffer's contents is repeated for <h> lines.
  290.  */
  291. void TView::writeLine(  short x, short y, short w, short h, const void *b)
  292. {
  293.     /**** Repeat the display of <b> from line <y>, <h> times.. ****/
  294.     while(h-- > 0)
  295.     {
  296.         writeView(x, y, w, (ushort *)b);            // Write these.
  297.         y++;
  298.     }
  299. }
  300.  
  301.  
  302. /*
  303.  *  writeBuf() writes the specified buffer to the line at (x,y). Only <w>
  304.  *  chars are written. The next <w> chars are written on the NEXT line, if <h>
  305.  *  is > 1.
  306.  */
  307. void TView::writeBuf(  short x, short y, short w, short h, const void *b)
  308. {
  309.     ushort  *p = (ushort *) b;
  310.  
  311.     /**** Repeat the display of <b> from line <y>, <h> times.. ****/
  312.     while(h-- > 0)
  313.     {
  314.         writeView(x, y, w, p);                      // Write these.
  315.         y++;
  316.         p   += w;
  317.     }
  318. }
  319.  
  320. /*
  321.  *  writeChar() writes the char in c to the screen N times.
  322.  */
  323. void TView::writeChar( short x, short y, char c, uchar color, short count )
  324. {
  325.     ushort  buf[256], *p, v, ct;
  326.  
  327.     if(count <= 0) return;
  328.     if(count > 256) count = 256;
  329.  
  330.     ct  = count;
  331.     v   = mapColor(color);                          // Get color to write in,
  332.     v   = (v << 8) | (ushort)(uchar)c;              // Make actual charcode,
  333.     p   = buf;
  334.     while(ct-- > 0)
  335.         *p++ = v;
  336.     writeView(x, y, count, buf);
  337. }
  338.  
  339.  
  340. /*
  341.  *  writeStr()
  342.  */
  343. void TView::writeStr(short x, short y, const char *str, uchar color)
  344. {
  345.     ushort  len, buf[256], *p, v, ct;
  346.  
  347.     len = strlen(str);                      // Get #chars in string,
  348.     if(len == 0) return;
  349.     if(len > 256) len = 256;                // Truncit,
  350.  
  351.     v   = mapColor(color);                  // Get color mapping
  352.     v   <<= 8;                              // Shift to attr position,
  353.     p   = buf;
  354.     ct  = len;
  355.     while(ct-- > 0)
  356.         *p++ = v | (ushort) (uchar)*str++;
  357.     writeView(x, y, len, buf);
  358. }
  359.  
  360. #endif
  361.  
  362.