home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
- #include "view.h"
-
- char HexDigit[16]={'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F' };
-
-
- void GetElementsInRow( HWND hWnd, int *iElements, int *iRows )
- {
- HDC hDC;
- HFONT hPrintFont, hOldFont;
- RECT r;
- SIZE s;
-
- hDC = GetDC( hWnd );
- hPrintFont = CreateFont( -MulDiv( 10, GetDeviceCaps( hDC, LOGPIXELSY ), 72L ),
- 0, // logical average character width
- 0, // angle of escapement
- 0, // base-line orientation angle
- FW_NORMAL, // font weight
- FALSE, // italic attribute flag
- FALSE, // underline attribute flag
- FALSE, // strikeout attribute flag
- ANSI_CHARSET, // character set identifier
- OUT_DEFAULT_PRECIS, // output precision
- CLIP_DEFAULT_PRECIS, // clipping precision
- DEFAULT_QUALITY, // output quality
- FF_MODERN, // pitch and family
- "Courier New" // pointer to typeface name string
- );
- hOldFont = SelectObject( hDC, hPrintFont );
- GetClientRect( hWnd, &r );
-
- GetTextExtentPoint( hDC, "012345678901 ", 14, &s );
- // Zwei Spaces als Trenner
- s.cx += 5;
- *iElements = (r.right - r.left) - s.cx;
-
- GetTextExtentPoint( hDC, "00 a", 4, &s );
- if( s.cx )
- *iElements /= s.cx;
- else
- *iElements = 1;
- if( *iElements == 0 ) *iElements = 1;
-
- *iRows = (r.bottom-r.top)/s.cy;
- (*iRows)--;
- if( (*iRows) < 0 ) *iRows=1;
-
- SelectObject( hDC, hOldFont );
- DeleteObject( hPrintFont );
- ReleaseDC( hWnd, hDC );
- }
-
- LRESULT WINAPI GrepView( HWND hWnd, UINT wMsg, WPARAM wp, LPARAM lp )
- {
- switch( wMsg )
- {
- case WM_CREATE:
- {
- LPCREATESTRUCT lpcs;
- LPVIEWPARAMS lpV;
- SCROLLINFO si;
- int e,r;
- char szFileName[512];
-
- GetElementsInRow( hWnd, &e, &r );
-
- lpcs = (LPCREATESTRUCT)lp;
- SetWindowLong( hWnd, 0, (long)lpcs->lpCreateParams );
- lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
-
- lpV->lMarkStart = lpV->lCurPos;
- lpV->lMarkEnd = lpV->lCurPos + 25;
- lpV->lCurPos /= e;
- lpV->lCurPos *= e;
-
- lpV->lFileSize = GetFileSize( lpV->hFile, NULL );
-
- si.cbSize = sizeof( SCROLLINFO);
- si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
- si.nMin = 0;
- si.nMax = (lpV->lFileSize/e)*e;
- si.nPage = r * e;
- si.nPos = 0;
- si.nTrackPos = 0;
-
- ShowScrollBar( hWnd, SB_VERT, TRUE );
- SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
-
- wsprintf( szFileName, "%s (%ld)", lpV->szFileName, lpV->lFileSize );
- SetWindowText( hWnd, szFileName );
- }
- break;
- case WM_VSCROLL:
- {
- SCROLLINFO si;
- int e,r;
- LPVIEWPARAMS lpV;
-
- lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
- GetElementsInRow( hWnd, &e, &r );
-
- si.cbSize = sizeof( SCROLLINFO );
- si.fMask = SIF_POS|SIF_TRACKPOS;
- GetScrollInfo( hWnd, SB_VERT, &si );
-
- switch( LOWORD( wp ) )
- {
- case SB_BOTTOM:
- lpV->lCurPos = lpV->lFileSize;
- break;
- case SB_ENDSCROLL:
- MessageBeep(0);
- break;
- case SB_LINEDOWN:
- lpV->lCurPos += e;
- break;
- case SB_LINEUP:
- lpV->lCurPos -= e;
- break;
- case SB_PAGEDOWN:
- lpV->lCurPos += e*r;
- break;
- case SB_PAGEUP:
- lpV->lCurPos -= e*r;
- break;
- case SB_THUMBPOSITION:
- lpV->lCurPos = si.nTrackPos;
- break;
- case SB_THUMBTRACK:
- lpV->lCurPos = si.nTrackPos;
- break;
- case SB_TOP:
- lpV->lCurPos = 0;
- break;
- }
-
- if( ( lpV->lCurPos + e*r ) > lpV->lFileSize )
- lpV->lCurPos = lpV->lFileSize - (e*r);
- if( lpV->lCurPos < 0 )
- lpV->lCurPos = 0;
- lpV->lCurPos = (lpV->lCurPos / e ) * e;
-
- si.cbSize = sizeof( SCROLLINFO );
- si.nPos = lpV->lCurPos;
- si.nTrackPos = lpV->lCurPos;
- e = SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
- InvalidateRect( hWnd, NULL, FALSE );
- }
- break;
- case WM_SIZE:
- {
- int e,r;
- LPVIEWPARAMS lpV;
- SCROLLINFO si;
-
- GetElementsInRow( hWnd, &e, &r );
- lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
-
- lpV->lCurPos = ( lpV->lCurPos / e ) * e;
-
- if( ( lpV->lCurPos + e * r ) > lpV->lFileSize )
- lpV->lCurPos = lpV->lFileSize - (e*r);
- if( lpV->lCurPos < 0 )
- lpV->lCurPos = 0;
- lpV->lCurPos = (lpV->lCurPos / e ) * e;
-
- si.cbSize = sizeof( SCROLLINFO);
- si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
- si.nMin = 0;
- si.nMax = (lpV->lFileSize/e)*e;
- si.nPage = r * e;
- si.nPos = lpV->lCurPos;
- si.nTrackPos = 0;
- SetScrollInfo( hWnd, SB_VERT, &si, TRUE );
- }
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hDC;
- RECT r, rOut1, rOut2;
- char szOutPuffer[4096];
- char cBuffer[2048];
- int iElementsPerRow;
- HFONT hPrintFont, hOldFont;
- char iVal;
- long lPos, l, lCount;
- int i,j,x;
- SIZE s;
- DWORD dwLen;
- BOOL bMark;
-
- LPVIEWPARAMS lpV;
- lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
- hDC = BeginPaint( hWnd, &ps );
- if( lpV )
- {
- hPrintFont = CreateFont( -MulDiv( 10, GetDeviceCaps( hDC, LOGPIXELSY ), 72L ),
- 0, // logical average character width
- 0, // angle of escapement
- 0, // base-line orientation angle
- FW_NORMAL, // font weight
- FALSE, // italic attribute flag
- FALSE, // underline attribute flag
- FALSE, // strikeout attribute flag
- ANSI_CHARSET, // character set identifier
- OUT_DEFAULT_PRECIS, // output precision
- CLIP_DEFAULT_PRECIS, // clipping precision
- DEFAULT_QUALITY, // output quality
- FF_MODERN, // pitch and family
- "Courier New" // pointer to typeface name string
- );
- hOldFont = SelectObject( hDC, hPrintFont );
- GetClientRect( hWnd, &r );
-
- // Anzahl der Elemente pro Zeile
-
- GetTextExtentPoint( hDC, "012345678901 ", 14, &s );
- // Zwei Spaces als Trenner
- s.cx += 5;
- iElementsPerRow = (r.right - r.left) - s.cx;
- x = r.left;
- // Jedes Element ben÷tigt mindestens eine Hex-DIgit und eine Ascii-Digit
-
- GetTextExtentPoint( hDC, "00 a", 4, &s );
- if( s.cx )
- iElementsPerRow /= s.cx;
- else
- iElementsPerRow = 1;
-
- SetFilePointer( lpV->hFile, lpV->lCurPos, NULL, FILE_BEGIN );
- ReadFile( lpV->hFile, cBuffer, sizeof( cBuffer ), &dwLen, NULL );
- lPos = 0;
- lCount = lpV->lCurPos;
-
- rOut1 = r;
- rOut2 = r;
-
- do
- {
- x = r.left;
- wsprintf( szOutPuffer, "%012ld ", lCount );
-
- GetTextExtentPoint( hDC, szOutPuffer, 14, &s );
- rOut1.left = x;
- rOut1.right = rOut1.left + s.cx;
- rOut1.bottom = rOut1.top + s.cy;
-
- SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT) );
- SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
- ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, 14, NULL );
- x = rOut1.right;
-
- bMark = (lpV->lMarkStart <= lCount ) && (lCount < lpV->lMarkEnd ) ||
- (lpV->lMarkStart <= (lCount+iElementsPerRow)) && ((lCount+iElementsPerRow) < lpV->lMarkEnd );
- if( bMark )
- {
- SetTextColor( hDC, GetSysColor(COLOR_HIGHLIGHTTEXT) );
- SetBkColor( hDC, GetSysColor(COLOR_HIGHLIGHT) );
- }
- j=0;
-
- l = lPos;
- if( ( l + iElementsPerRow) > dwLen )
- {
- SetFilePointer( lpV->hFile, lCount, NULL, FILE_BEGIN );
- ReadFile( lpV->hFile, cBuffer, sizeof( cBuffer ), &dwLen, NULL );
- lPos = 0;
- l = 0;
- }
- lCount += iElementsPerRow;
- for( i = 0; i < iElementsPerRow; i++ ) // MaxCount
- {
- if( l == dwLen )
- {
- if( lCount < lpV->lFileSize )
- {
- MessageBox( hWnd, "Schriftgr÷▀e zu klein. Ausgabe wird abgebrochen", "Fehler", MB_ICONSTOP );
- lCount = lpV->lFileSize + 1;
- }
- break;
- }
-
- iVal = cBuffer[l++];
- szOutPuffer[j++]= HexDigit[(iVal>>4)];
- szOutPuffer[j++]= HexDigit[(iVal&0x0F)];
- szOutPuffer[j++]= ' ';
- }
-
- for( ; i < iElementsPerRow; i++ ) // MaxCount
- {
- szOutPuffer[j++]= ' ';
- szOutPuffer[j++]= ' ';
- szOutPuffer[j++]= ' ';
- }
- szOutPuffer[j++]= ' ';
-
- GetTextExtentPoint( hDC, szOutPuffer, j, &s );
- rOut1.left = x;
- rOut1.right = rOut1.left + s.cx;
-
- ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, j, NULL );
- rOut2.left = rOut1.right;
-
- j = 0;
- // Zeichen Ausgeben
- l = lPos;
- for( i = 0; i < iElementsPerRow; i++ ) // MaxCount
- {
- if( l == dwLen )
- break;
-
- iVal = cBuffer[l++];
- szOutPuffer[j++] = iVal;
- }
- for( ; i < iElementsPerRow; i++ ) // MaxCount
- {
- szOutPuffer[j++]=' ';
- }
-
- GetTextExtentPoint( hDC, szOutPuffer, j, &s );
- s.cx += 5;
- if( !s.cy ) s.cy = 1;
- rOut1.left = r.right - s.cx;
- rOut1.right = r.right;
- ExtTextOut( hDC, rOut1.left, rOut1.top, ETO_OPAQUE, &rOut1, szOutPuffer, j, NULL );
-
- rOut2.right = rOut1.left;
- rOut2.top = rOut1.top;
- rOut2.bottom = rOut1.bottom;
- ExtTextOut( hDC, rOut2.left, rOut2.top, ETO_OPAQUE, &rOut2, "",0, NULL );
-
- rOut1.bottom = rOut1.top + s.cy;
- rOut1.top = rOut1.bottom;
- rOut2.bottom = rOut2.top + s.cy;
- rOut2.top = rOut2.bottom;
-
- lPos += iElementsPerRow;
-
- if( lCount > lpV->lFileSize )
- break;
- }
- while( rOut1.top < r.bottom );
-
- SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT) );
- SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
-
- rOut1.left = r.left;
- rOut1.right = r.right;
- rOut1.bottom = r.bottom;
-
- ExtTextOut( hDC, 0, 0, ETO_OPAQUE, &rOut1, "", 0, NULL );
-
- SelectObject( hDC, hOldFont );
-
- DeleteObject( hPrintFont );
- }
- EndPaint( hWnd, &ps );
- }
- break;
- case WM_DESTROY:
- {
- LPVIEWPARAMS lpV;
- lpV = (LPVIEWPARAMS) GetWindowLong( hWnd, 0 );
- CloseHandle( lpV->hFile );
- free( lpV );
- SetWindowLong( hWnd, 0, 0 );
- }
- break;
-
- }
- return DefWindowProc( hWnd, wMsg, wp, lp );
- }
-
- BOOL RegisterViewer( HINSTANCE hInst )
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof(wc);
- wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
- wc.lpfnWndProc = GrepView;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = sizeof( HANDLE); // Platz fⁿr Dateihandle
- wc.hInstance = hInst;
- wc.hIcon = NULL;
- wc.hCursor = NULL;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL; //(LPSTR)MAKEINTRESOURCE(IDR_MAINMENU);
- wc.lpszClassName = "VIEW";
- wc.hIconSm = NULL;
- return RegisterClassEx( &wc );
- }
-
- BOOL UnregisterViewer( HINSTANCE hInst )
- {
- return UnregisterClass( "VIEW", hInst );
- }
-
-
- BOOL CreateViewer( HINSTANCE hInst, HWND *hwndView, LPCSTR lpFileName, long lPos )
- {
- LPVIEWPARAMS lpV;
- HANDLE hFile;
-
- hFile = CreateFile( lpFileName, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, 0, NULL );
- if( hFile == INVALID_HANDLE_VALUE )
- return FALSE;
-
- lpV = (LPVIEWPARAMS)malloc( sizeof(VIEWPARAMS) );
- if( !lpV )
- {
- CloseHandle( hFile );
- return FALSE;
- }
-
- lstrcpy( lpV->szFileName, lpFileName );
- lpV->hFile = hFile ;
- lpV->lCurPos = lPos;
-
- *hwndView = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_CONTROLPARENT | WS_EX_WINDOWEDGE | WS_VSCROLL,
- "VIEW",
- "Window",
- WS_THICKFRAME|WS_CAPTION|WS_SYSMENU,
- 200,0,600,600,
- NULL,
- NULL,
- hInst,
- lpV );
- if( *hwndView ) return TRUE;
- return FALSE;
- }
-