home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 1999 October / PCpro_1999_10.ISO / Tools / wingrep / Source / View.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-22  |  13.4 KB  |  435 lines

  1. #include <windows.h>
  2. #include "view.h"
  3.  
  4. char HexDigit[16]={'0','1','2','3',
  5.                    '4','5','6','7',
  6.                    '8','9','A','B',
  7.                    'C','D','E','F' };
  8.  
  9.  
  10. void GetElementsInRow( HWND hWnd, int *iElements, int *iRows )
  11. {
  12.   HDC hDC;
  13.   HFONT hPrintFont, hOldFont;
  14.   RECT r;
  15.   SIZE s;
  16.  
  17.   hDC = GetDC( hWnd );
  18.   hPrintFont = CreateFont( -MulDiv( 10, GetDeviceCaps( hDC, LOGPIXELSY ), 72L ),
  19.                            0,                      // logical average character width
  20.                            0,                      // angle of escapement
  21.                            0,                      // base-line orientation angle
  22.                            FW_NORMAL,              // font weight
  23.                            FALSE,                  // italic attribute flag
  24.                            FALSE,                  // underline attribute flag
  25.                            FALSE,                  // strikeout attribute flag
  26.                            ANSI_CHARSET,           // character set identifier
  27.                            OUT_DEFAULT_PRECIS,     // output precision
  28.                            CLIP_DEFAULT_PRECIS,    // clipping precision
  29.                            DEFAULT_QUALITY,        // output quality
  30.                            FF_MODERN,              // pitch and family
  31.                            "Courier New"           // pointer to typeface name string
  32.                         );
  33.   hOldFont = SelectObject( hDC, hPrintFont );
  34.   GetClientRect( hWnd, &r );
  35.  
  36.   GetTextExtentPoint( hDC, "012345678901  ", 14, &s );
  37.   // Zwei Spaces als Trenner
  38.   s.cx += 5;
  39.   *iElements = (r.right - r.left) - s.cx;
  40.       
  41.   GetTextExtentPoint( hDC, "00 a", 4, &s );
  42.   if( s.cx )
  43.     *iElements /= s.cx;
  44.   else
  45.     *iElements = 1;
  46.   if( *iElements == 0 ) *iElements = 1;
  47.   
  48.   *iRows = (r.bottom-r.top)/s.cy;
  49.   (*iRows)--;
  50.   if( (*iRows) < 0 ) *iRows=1;
  51.  
  52.   SelectObject( hDC, hOldFont );
  53.   DeleteObject( hPrintFont );
  54.   ReleaseDC( hWnd, hDC );
  55. }
  56.  
  57. LRESULT WINAPI GrepView( HWND hWnd, UINT wMsg, WPARAM wp, LPARAM lp )
  58. {
  59.   switch( wMsg )
  60.   {
  61.     case WM_CREATE:
  62.     {
  63.       LPCREATESTRUCT lpcs;
  64.       LPVIEWPARAMS lpV;
  65.       SCROLLINFO si;
  66.       int e,r;
  67.       char szFileName[512];
  68.  
  69.       GetElementsInRow( hWnd, &e, &r );
  70.  
  71.       lpcs = (LPCREATESTRUCT)lp;
  72.       SetWindowLong( hWnd, 0, (long)lpcs->lpCreateParams );
  73.       lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
  74.  
  75.       lpV->lMarkStart = lpV->lCurPos;
  76.       lpV->lMarkEnd = lpV->lCurPos + 25;
  77.       lpV->lCurPos /= e;
  78.       lpV->lCurPos *= e;
  79.  
  80.       lpV->lFileSize = GetFileSize( lpV->hFile, NULL );
  81.  
  82.       si.cbSize = sizeof( SCROLLINFO);
  83.       si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  84.       si.nMin = 0;
  85.       si.nMax = (lpV->lFileSize/e)*e;
  86.       si.nPage = r * e;
  87.       si.nPos = 0;
  88.       si.nTrackPos = 0;
  89.  
  90.       ShowScrollBar( hWnd, SB_VERT, TRUE );
  91.       SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  92.  
  93.       wsprintf( szFileName, "%s (%ld)", lpV->szFileName, lpV->lFileSize );
  94.       SetWindowText( hWnd, szFileName );
  95.     }
  96.     break;
  97.     case WM_VSCROLL:
  98.     {
  99.       SCROLLINFO si;
  100.       int e,r;
  101.       LPVIEWPARAMS lpV;
  102.  
  103.       lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
  104.       GetElementsInRow( hWnd, &e, &r );
  105.       
  106.       si.cbSize = sizeof( SCROLLINFO );
  107.       si.fMask = SIF_POS|SIF_TRACKPOS;
  108.       GetScrollInfo( hWnd, SB_VERT, &si );
  109.  
  110.       switch( LOWORD( wp ) )
  111.       {
  112.         case SB_BOTTOM:
  113.           lpV->lCurPos = lpV->lFileSize;
  114.         break;
  115.         case SB_ENDSCROLL:
  116.           MessageBeep(0);
  117.         break;
  118.         case SB_LINEDOWN:
  119.           lpV->lCurPos += e;
  120.         break;
  121.         case SB_LINEUP:
  122.           lpV->lCurPos -= e;
  123.         break;
  124.         case SB_PAGEDOWN:
  125.           lpV->lCurPos += e*r;
  126.         break;
  127.         case SB_PAGEUP:
  128.           lpV->lCurPos -= e*r;
  129.         break;
  130.         case SB_THUMBPOSITION:
  131.           lpV->lCurPos = si.nTrackPos;
  132.         break;
  133.         case SB_THUMBTRACK:
  134.           lpV->lCurPos = si.nTrackPos;
  135.         break;
  136.         case SB_TOP:
  137.           lpV->lCurPos = 0;
  138.         break;
  139.       }
  140.  
  141.       if( ( lpV->lCurPos + e*r ) > lpV->lFileSize )
  142.         lpV->lCurPos = lpV->lFileSize - (e*r);
  143.       if( lpV->lCurPos < 0 )
  144.         lpV->lCurPos = 0;
  145.       lpV->lCurPos = (lpV->lCurPos / e ) * e;
  146.  
  147.       si.cbSize = sizeof( SCROLLINFO );
  148.       si.nPos = lpV->lCurPos;
  149.       si.nTrackPos = lpV->lCurPos;
  150.       e = SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  151.       InvalidateRect( hWnd, NULL, FALSE );
  152.     }
  153.     break;
  154.     case WM_SIZE:
  155.     {
  156.       int e,r;
  157.       LPVIEWPARAMS lpV;
  158.       SCROLLINFO si;
  159.  
  160.       GetElementsInRow( hWnd, &e, &r );
  161.       lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
  162.  
  163.       lpV->lCurPos = ( lpV->lCurPos / e ) * e;
  164.  
  165.       if( ( lpV->lCurPos + e * r ) > lpV->lFileSize )
  166.         lpV->lCurPos = lpV->lFileSize - (e*r);
  167.       if( lpV->lCurPos < 0 )
  168.         lpV->lCurPos = 0;
  169.       lpV->lCurPos = (lpV->lCurPos / e ) * e;
  170.  
  171.       si.cbSize = sizeof( SCROLLINFO);
  172.       si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
  173.       si.nMin = 0;
  174.       si.nMax = (lpV->lFileSize/e)*e;
  175.       si.nPage = r * e;
  176.       si.nPos = lpV->lCurPos;
  177.       si.nTrackPos = 0;
  178.       SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
  179.     }
  180.     break;
  181.     case WM_PAINT:
  182.     {
  183.       PAINTSTRUCT ps;
  184.       HDC hDC;
  185.       RECT r, rOut1, rOut2;
  186.       char szOutPuffer[4096];
  187.       char cBuffer[2048];
  188.       int iElementsPerRow;
  189.       HFONT hPrintFont, hOldFont;
  190.       char iVal;
  191.       long lPos, l, lCount;
  192.       int i,j,x;
  193.       SIZE s;
  194.       DWORD dwLen;
  195.       BOOL bMark;
  196.       
  197.       LPVIEWPARAMS lpV;
  198.       lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
  199.       hDC = BeginPaint( hWnd, &ps );
  200.       if( lpV )
  201.       {
  202.         hPrintFont = CreateFont( -MulDiv( 10, GetDeviceCaps( hDC, LOGPIXELSY ), 72L ),
  203.                            0,                      // logical average character width
  204.                            0,                      // angle of escapement
  205.                            0,                      // base-line orientation angle
  206.                            FW_NORMAL,              // font weight
  207.                            FALSE,                  // italic attribute flag
  208.                            FALSE,                  // underline attribute flag
  209.                            FALSE,                  // strikeout attribute flag
  210.                            ANSI_CHARSET,           // character set identifier
  211.                            OUT_DEFAULT_PRECIS,     // output precision
  212.                            CLIP_DEFAULT_PRECIS,    // clipping precision
  213.                            DEFAULT_QUALITY,        // output quality
  214.                            FF_MODERN,              // pitch and family
  215.                            "Courier New"           // pointer to typeface name string
  216.                         );
  217.         hOldFont = SelectObject( hDC, hPrintFont );
  218.         GetClientRect( hWnd, &r );
  219.  
  220.         // Anzahl der Elemente pro Zeile
  221.       
  222.         GetTextExtentPoint( hDC, "012345678901  ", 14, &s );
  223.         // Zwei Spaces als Trenner
  224.         s.cx += 5;
  225.         iElementsPerRow = (r.right - r.left) - s.cx;
  226.         x = r.left;
  227.         // Jedes Element ben÷tigt mindestens eine Hex-DIgit und eine Ascii-Digit
  228.       
  229.         GetTextExtentPoint( hDC, "00 a", 4, &s );
  230.         if( s.cx )
  231.           iElementsPerRow /= s.cx;
  232.         else
  233.           iElementsPerRow = 1;
  234.  
  235.         SetFilePointer( lpV->hFile, lpV->lCurPos, NULL, FILE_BEGIN );
  236.         ReadFile( lpV->hFile, cBuffer, sizeof( cBuffer ), &dwLen, NULL );
  237.         lPos = 0;
  238.         lCount = lpV->lCurPos;
  239.  
  240.         rOut1 = r;
  241.         rOut2 = r;
  242.  
  243.         do
  244.         {
  245.           x = r.left;
  246.           wsprintf( szOutPuffer, "%012ld  ", lCount );
  247.  
  248.           GetTextExtentPoint( hDC, szOutPuffer, 14, &s );
  249.           rOut1.left = x;
  250.           rOut1.right = rOut1.left + s.cx;
  251.           rOut1.bottom = rOut1.top + s.cy;
  252.           
  253.           SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT) );
  254.           SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
  255.           ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, 14, NULL );
  256.           x = rOut1.right;
  257.           
  258.           bMark = (lpV->lMarkStart <= lCount ) && (lCount < lpV->lMarkEnd ) ||
  259.                   (lpV->lMarkStart <= (lCount+iElementsPerRow)) && ((lCount+iElementsPerRow) < lpV->lMarkEnd );
  260.           if( bMark )
  261.           {
  262.             SetTextColor( hDC, GetSysColor(COLOR_HIGHLIGHTTEXT) );
  263.             SetBkColor( hDC, GetSysColor(COLOR_HIGHLIGHT) );
  264.           }
  265.           j=0;
  266.           
  267.           l = lPos;
  268.           if( ( l + iElementsPerRow) > dwLen )
  269.           {
  270.               SetFilePointer( lpV->hFile, lCount, NULL, FILE_BEGIN );
  271.               ReadFile( lpV->hFile, cBuffer, sizeof( cBuffer ), &dwLen, NULL );
  272.               lPos = 0;
  273.               l = 0;
  274.           }
  275.           lCount += iElementsPerRow;
  276.           for( i = 0; i < iElementsPerRow; i++ ) // MaxCount
  277.           {
  278.             if( l == dwLen )
  279.             {
  280.               if( lCount < lpV->lFileSize )
  281.               {
  282.                 MessageBox( hWnd, "Schriftgr÷▀e zu klein. Ausgabe wird abgebrochen", "Fehler", MB_ICONSTOP );
  283.                 lCount = lpV->lFileSize + 1;
  284.               }
  285.               break;
  286.             }
  287.  
  288.             iVal = cBuffer[l++];
  289.             szOutPuffer[j++]= HexDigit[(iVal>>4)];
  290.             szOutPuffer[j++]= HexDigit[(iVal&0x0F)];
  291.             szOutPuffer[j++]= ' ';
  292.           }
  293.  
  294.           for( ; i < iElementsPerRow; i++ ) // MaxCount
  295.           {
  296.             szOutPuffer[j++]= ' ';
  297.             szOutPuffer[j++]= ' ';
  298.             szOutPuffer[j++]= ' ';
  299.           }
  300.           szOutPuffer[j++]= ' ';
  301.  
  302.           GetTextExtentPoint( hDC, szOutPuffer, j, &s );
  303.           rOut1.left = x;
  304.           rOut1.right = rOut1.left + s.cx;
  305.  
  306.           ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, j, NULL );
  307.           rOut2.left = rOut1.right;
  308.  
  309.           j = 0;
  310.           // Zeichen Ausgeben
  311.           l = lPos;
  312.           for( i = 0; i < iElementsPerRow; i++ ) // MaxCount
  313.           {
  314.             if( l == dwLen )
  315.               break;
  316.  
  317.             iVal = cBuffer[l++];
  318.             szOutPuffer[j++] = iVal;
  319.           }
  320.           for( ; i < iElementsPerRow; i++ ) // MaxCount
  321.           {
  322.             szOutPuffer[j++]=' ';
  323.           }
  324.  
  325.           GetTextExtentPoint( hDC, szOutPuffer, j, &s );
  326.           s.cx += 5;
  327.           if( !s.cy ) s.cy = 1;
  328.           rOut1.left = r.right - s.cx;
  329.           rOut1.right = r.right;
  330.           ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, j, NULL );
  331.         
  332.           rOut2.right = rOut1.left;
  333.           rOut2.top = rOut1.top;
  334.           rOut2.bottom = rOut1.bottom;
  335.           ExtTextOut( hDC, rOut2.left, rOut2.top, ETO_OPAQUE, &rOut2, "",0, NULL );
  336.         
  337.           rOut1.bottom = rOut1.top + s.cy;
  338.           rOut1.top = rOut1.bottom;
  339.           rOut2.bottom = rOut2.top + s.cy;
  340.           rOut2.top = rOut2.bottom;
  341.  
  342.           lPos += iElementsPerRow;
  343.  
  344.           if( lCount > lpV->lFileSize )
  345.             break;
  346.         } 
  347.         while( rOut1.top < r.bottom );
  348.  
  349.         SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT) );
  350.         SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
  351.         
  352.         rOut1.left = r.left;
  353.         rOut1.right = r.right;
  354.         rOut1.bottom = r.bottom;
  355.  
  356.         ExtTextOut( hDC, 0, 0, ETO_OPAQUE, &rOut1, "", 0, NULL );
  357.  
  358.         SelectObject( hDC, hOldFont );
  359.  
  360.         DeleteObject( hPrintFont );
  361.       }
  362.       EndPaint( hWnd, &ps );
  363.     }
  364.     break;
  365.     case WM_DESTROY:
  366.     {
  367.       LPVIEWPARAMS lpV;
  368.       lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
  369.       CloseHandle( lpV->hFile );
  370.       free( lpV );
  371.       SetWindowLong( hWnd, 0, 0 );
  372.     }
  373.     break;
  374.  
  375.   }
  376.   return DefWindowProc( hWnd, wMsg, wp, lp );
  377. }
  378.  
  379. BOOL RegisterViewer( HINSTANCE hInst )
  380. {
  381.   WNDCLASSEX wc;
  382.   wc.cbSize = sizeof(wc); 
  383.   wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW; 
  384.   wc.lpfnWndProc = GrepView; 
  385.   wc.cbClsExtra = 0; 
  386.   wc.cbWndExtra = sizeof( HANDLE);  // Platz fⁿr Dateihandle
  387.   wc.hInstance = hInst; 
  388.   wc.hIcon = NULL; 
  389.   wc.hCursor = NULL; 
  390.   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
  391.   wc.lpszMenuName = NULL; //(LPSTR)MAKEINTRESOURCE(IDR_MAINMENU);
  392.   wc.lpszClassName = "VIEW"; 
  393.   wc.hIconSm = NULL; 
  394.   return RegisterClassEx( &wc );
  395. }
  396.  
  397. BOOL UnregisterViewer( HINSTANCE hInst )
  398. {
  399.   return UnregisterClass( "VIEW", hInst );
  400. }
  401.  
  402.  
  403. BOOL CreateViewer( HINSTANCE hInst, HWND *hwndView, LPCSTR lpFileName, long lPos )
  404. {
  405.   LPVIEWPARAMS lpV;
  406.   HANDLE hFile;
  407.  
  408.   hFile = CreateFile( lpFileName, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, 0, NULL );
  409.   if( hFile == INVALID_HANDLE_VALUE ) 
  410.     return FALSE;
  411.  
  412.   lpV = (LPVIEWPARAMS)malloc( sizeof(VIEWPARAMS) );
  413.   if( !lpV )
  414.   {
  415.     CloseHandle( hFile );
  416.     return FALSE;
  417.   }
  418.  
  419.   lstrcpy( lpV->szFileName, lpFileName );
  420.   lpV->hFile = hFile ;
  421.   lpV->lCurPos = lPos;
  422.  
  423.   *hwndView = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_CONTROLPARENT | WS_EX_WINDOWEDGE | WS_VSCROLL,
  424.                               "VIEW",
  425.                               "Window",
  426.                               WS_THICKFRAME|WS_CAPTION|WS_SYSMENU,
  427.                               200,0,600,600,
  428.                               NULL,
  429.                               NULL,
  430.                               hInst,
  431.                               lpV );
  432.   if( *hwndView ) return TRUE;
  433.   return FALSE;
  434. }
  435.