home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Samples / vfHex / VHexStatic.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-10  |  5.9 KB  |  200 lines

  1. // -------------------------------------------------------------------------
  2. // Copyright @ 1997 TCK Software, Incorporated
  3. // All Rights Reserved
  4. // -------------------------------------------------------------------------
  5. #include "stdafx.h"
  6. #include "VHexStatic.h"
  7. #include "VForm.h"
  8.  
  9. // -------------------------------------------------------------------------
  10. // VHexStatic Class
  11. // -------------------------------------------------------------------------
  12. VHexStatic::VHexStatic()
  13. {
  14.     m_text.Format("Ctl %d", m_nId);
  15.     SetBorder(TRUE);
  16.     VertCenterText(TRUE);
  17.     SetTransparent(FALSE);
  18.     m_nLeftMargin    = 1;
  19.     m_nLineHeight    = 18;
  20.     m_bHexOn        = FALSE;
  21. }
  22.  
  23. // -------------------------------------------------------------------------
  24. // VHexStatic Constructor with arguments
  25. // -------------------------------------------------------------------------
  26. VHexStatic::VHexStatic(VRow *pRow, int nId, LPCSTR szText, 
  27.              int x, int y, int cx, int cy) :
  28.     VCtl(pRow, nId, szText, x, y, cx, cy)
  29. {
  30.     SetBorder(TRUE);
  31.     VertCenterText(TRUE);
  32.     SetTransparent(FALSE);
  33.     m_nLeftMargin    = 1;
  34.     m_nLineHeight    = 18;
  35.     m_bHexOn        = FALSE;
  36. }
  37.  
  38. // -------------------------------------------------------------------------
  39. // VHexStatic Assignment Operator (=)
  40. // -------------------------------------------------------------------------
  41. VHexStatic& VHexStatic::operator =(VHexStatic& x)
  42. {
  43.     (VCtl&)*this    = (VCtl&)x;
  44.     m_bHexOn        = x.m_bHexOn;
  45.     m_nLineHeight    = x.m_nLineHeight;
  46.  
  47.     return *this;
  48. }
  49.  
  50. // -------------------------------------------------------------------------
  51. // VHexStatic Init
  52. // -------------------------------------------------------------------------
  53. void VHexStatic::Init(VRow *pRow, int nId, LPCSTR szText, int x, int y, int cx, int cy)
  54. {
  55.     VCtl::Init(pRow, nId, szText, x, y, cx, cy);
  56. }
  57.  
  58. // -------------------------------------------------------------------------
  59. // VHexStatic SetHexText
  60. // -------------------------------------------------------------------------
  61. void VHexStatic::SetHexText(LPCSTR szBuf, int nLength)
  62. {
  63.     char c;
  64.     char *pAscii = m_text.GetBuffer(nLength+1);
  65.     char *pHex1 = m_hex1.GetBuffer(nLength+1);
  66.     char *pHex2 = m_hex2.GetBuffer(nLength+1);
  67.  
  68.     // Logic to cdisplay hex information
  69.     for(int i=0; i < nLength; i++)
  70.     {
  71.         c = szBuf[i];
  72.         pAscii[i] = (c == 0) ? '.' : c;        // Convert nulls to periods
  73.         pHex1[i] = HexHi(c);
  74.         pHex2[i] = HexLo(c);
  75.     }
  76.  
  77.     pAscii[nLength] = 0;    // Null Terminate 
  78.     pHex1[nLength] = 0;        // Null Terminate 
  79.     pHex2[nLength] = 0;        // Null Terminate 
  80.  
  81.     m_text.ReleaseBuffer();
  82.     m_hex1.ReleaseBuffer();
  83.     m_hex2.ReleaseBuffer();
  84. }
  85.  
  86. // -------------------------------------------------------------------------
  87. // Returns Hex Char for byte
  88. // -------------------------------------------------------------------------
  89. char VHexStatic::HexLo(char c)
  90. {
  91.      char x = c & 0x0F;                // Mask off the high bits
  92.     static char hexArray[] = "0123456789ABCDEFXXXX";
  93.     
  94.     return hexArray[x];
  95. }
  96.  
  97. #define HIHALFBYTE(b)        ((BYTE)(((BYTE)(b) >> 4) & 0x0F))
  98. char VHexStatic::HexHi(char c)
  99. {
  100.      char x = HIHALFBYTE(c);            // Shift off low bits and mask
  101.     static char hexArray[] = "0123456789ABCDEFXXXX";
  102.  
  103.     return hexArray[x];
  104. }
  105.  
  106. // -------------------------------------------------------------------------
  107. // VHexStatic OnDraw Routine
  108. // -------------------------------------------------------------------------
  109. void VHexStatic::OnDraw(CDC *pDC, LPRECT pRect, BOOL bGridMode)
  110. {
  111.     CRect rcOrig = pRect ? *pRect : m_rectWin;
  112.     CRect rect = rcOrig;
  113.     CRect rectText;
  114.     BOOL bSkipBkgrnd = FALSE;
  115.     int nLineStart = rcOrig.top;
  116.     VClipObj myClipObj;
  117.  
  118.     if(bGridMode)
  119.     {
  120.         myClipObj.Set(pDC, rcOrig);
  121.          // DrawTextEx(pDC, rect, TRUE, bGridMode);    // Background already drawn
  122.          // return;
  123.     }
  124.  
  125.     rect.bottom = rect.top + m_nLineHeight;            // First do ascii text
  126.     if(rect.bottom > rcOrig.bottom)
  127.         rect.bottom = rcOrig.bottom;
  128.     rectText = rect;
  129.  
  130.     if(HasBorder())
  131.     {
  132.         if(NoInactiveBorder() && !HasFocus())
  133.             bSkipBkgrnd = TRUE;
  134.         else
  135.             DrawEffect(pDC, &rect, m_nEffect, m_nEffectDepth);
  136.  
  137.         rectText = EffectInterior(&rect, m_nEffect, m_nEffectDepth);
  138.     }
  139.  
  140.     if(!IsTransparent() && !bSkipBkgrnd)
  141.     {
  142.         CBrush brBack(m_vclrBack.GetRGB());
  143.         pDC->FillRect(&rectText, &brBack);
  144.     }
  145.  
  146.     rectText.left += m_nLeftMargin;
  147.     // DrawCtlText(pDC, m_text, rectText, DT_EDITCONTROL | DT_WORDBREAK);
  148.  
  149.     // --------------------------
  150.     // Draw the Text
  151.     // --------------------------
  152.     CFont*    pOldFont=0;
  153.     COLORREF clrOldText, clrOldBk;
  154.     int  nOldBkMode, nOldTextAlign;
  155.     BOOL bVal;
  156.     
  157.     // --- Setup Text Attributes ---
  158.     if(m_pFont)    pOldFont = pDC->SelectObject(m_pFont);    // Set Custom Font
  159.     clrOldText = pDC->SetTextColor(ForeColor());
  160.     clrOldBk = pDC->SetBkColor(BackColor());
  161.     nOldBkMode = pDC->SetBkMode(TRANSPARENT);
  162.     nOldTextAlign = pDC->SetTextAlign(m_nTextAlign);
  163.  
  164.     bVal = pDC->ExtTextOut(rectText.left, rectText.top, ETO_CLIPPED, 
  165.             rectText, m_text, NULL );
  166.  
  167.     // Showing extra 2 rows
  168.     if(HexOn())
  169.     {
  170.         nLineStart += m_nLineHeight + 4;
  171.         rectText.top = nLineStart;
  172.         rectText.bottom = rectText.top + m_nLineHeight;
  173.         if(rectText.bottom > rcOrig.bottom)
  174.             rectText.bottom = rcOrig.bottom;
  175.  
  176.         if(rectText.bottom > rectText.top)        // Only draw if room
  177.             bVal = pDC->ExtTextOut(rectText.left, rectText.top, ETO_CLIPPED, 
  178.                     rectText, m_hex1, NULL );
  179.  
  180.         // Since we arent using borders, we will cheat a little...
  181.         nLineStart += m_nLineHeight - 3;
  182.         rectText.top = nLineStart;
  183.         rectText.bottom = rectText.top + m_nLineHeight;
  184.         if(rectText.bottom > rcOrig.bottom)
  185.             rectText.bottom = rcOrig.bottom;
  186.  
  187.         if(rectText.bottom > rectText.top)        // Only draw if room
  188.             bVal = pDC->ExtTextOut(rectText.left, rectText.top, ETO_CLIPPED, 
  189.                     rectText, m_hex2, NULL );
  190.     }
  191.  
  192.     // --- Reset Text Attributes back to originals ---
  193.     pDC->SetTextColor(clrOldText);
  194.     pDC->SetBkColor(clrOldBk);
  195.     pDC->SetBkMode(nOldBkMode);
  196.     pDC->SetTextAlign(nOldTextAlign);
  197.     if(pOldFont)    pDC->SelectObject(pOldFont);    // Reset orig Font
  198. }
  199.  
  200.