home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK3 / MFC / SAMPLES / FILEVIEW / FILEVIEW.CP$ / fileview
Encoding:
Text File  |  1992-03-14  |  9.7 KB  |  491 lines

  1. // fileview.cpp : Defines the class behaviors for the application.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "fileview.h"
  14.  
  15. #include "afxdlgs.h"
  16.  
  17. #define SIZESTRING  256        // max characters in string
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20.  
  21. // theApp:
  22. // Just creating this application object runs the whole application.
  23. //
  24. CTheApp theApp;
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27.  
  28. // CMainWindow constructor:
  29. // Create the window with the appropriate style, size, menu, etc.
  30. //
  31. CMainWindow::CMainWindow()
  32. {
  33.     VERIFY(LoadAccelTable( "MainAccelTable" ));
  34.     VERIFY(Create( NULL, "FileView Application",
  35.         WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  36.         rectDefault, NULL, "MainMenu" ));
  37. }
  38.  
  39. CMainWindow::~CMainWindow()
  40. {
  41.     if (m_pMyFile != NULL)
  42.     {
  43.         m_pMyFile->Close();
  44.         delete m_pMyFile;
  45.     }
  46. }
  47. // OnCreate
  48. //
  49.  
  50. int CMainWindow::OnCreate( LPCREATESTRUCT /* lpcs */)
  51. {
  52.     TEXTMETRIC tm;
  53.     CWindowDC dc( this );
  54.  
  55.     dc.SelectStockObject( SYSTEM_FIXED_FONT );
  56.     dc.GetTextMetrics( &tm );
  57.  
  58.     m_nCxChar = tm.tmAveCharWidth;
  59.     m_nCyChar = tm.tmHeight + tm.tmExternalLeading;
  60.  
  61.     m_nVScrollPos = 0;
  62.     m_nHScrollPos = 0;
  63.     m_lTopLine = 0L;
  64.     m_pMyFile = NULL;
  65.  
  66.     SetScrollRange( SB_VERT, 0, SCROLLMAX, FALSE );
  67.     SetScrollRange( SB_HORZ, 0, SIZESTRING, FALSE );
  68.  
  69.     return 0;
  70. }
  71.  
  72.  
  73. // OnPaint
  74. //
  75.  
  76. void CMainWindow::OnPaint()
  77. {
  78.     char        acBuf[ SIZESTRING ];
  79.     CPaintDC    dc( this );
  80.     CRect       rect, rect2;
  81.     int         y;
  82.     char*       pc;
  83.  
  84.     GetClientRect( &rect );
  85.     dc.SetTextAlign( TA_LEFT );
  86.     dc.SelectStockObject( SYSTEM_FIXED_FONT );
  87.     dc.SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  88.     dc.SetBkColor(::GetSysColor(COLOR_WINDOW));
  89.     if ( m_pMyFile != NULL )
  90.     {
  91.         TRY
  92.         {
  93.             m_pMyFile->Seek( m_lTopLine, CFile::begin );
  94.         }
  95.         CATCH( CFileException, e )
  96.         {
  97.             TRACE( "Bad Seek in OnPaint %ld\n", m_lTopLine );
  98.         }
  99.         END_CATCH
  100.  
  101.         rect2.left  = rect.left;
  102.         rect2.right = rect.right;
  103.  
  104.         m_nLinesPainted = 0;
  105.         for ( y = m_nCxChar/2; y < rect.bottom; y += m_nCyChar )
  106.         {
  107.             m_pMyFile->NextLine( acBuf, SIZESTRING );
  108.  
  109.             pc = acBuf;
  110.             if ( (int)strlen(acBuf) < m_nHScrollPos)
  111.             {
  112.                 *acBuf = 0;
  113.             }
  114.             else
  115.             {
  116.                 pc += m_nHScrollPos;
  117.             }
  118.  
  119.             rect2.top = y;
  120.             rect2.bottom = y + m_nCyChar;
  121.             dc.ExtTextOut( m_nCyChar/2, rect2.top, ETO_OPAQUE, &rect2,
  122.                            pc, strlen(pc), NULL );
  123.             m_nLinesPainted++;
  124.         }
  125.     }
  126. }
  127.  
  128. // OnAbout:
  129. //
  130.  
  131. void CMainWindow::OnAbout()
  132. {
  133.     CModalDialog about( "AboutBox", this );
  134.     about.DoModal();
  135. }
  136.  
  137. // OnExit:
  138. //
  139.  
  140. void CMainWindow::OnExit()
  141. {
  142.     DestroyWindow();
  143. }
  144.  
  145. // OnOpen:
  146. //
  147.  
  148. void CMainWindow::OnOpen()
  149. {
  150.     CString strFileName, strFileTitle;
  151.  
  152.     //
  153.     // First close any open file
  154.     //
  155.  
  156.     if ( m_pMyFile != NULL )
  157.     {
  158.         m_pMyFile->Close();
  159.         delete m_pMyFile;
  160.         m_pMyFile = NULL;
  161.         SetWindowText( "No File" );
  162.         Invalidate( TRUE );
  163.         m_lTopLine = 0L;
  164.         m_nVScrollPos = 0;
  165.         SetScrollPos( SB_VERT, m_nVScrollPos, TRUE );
  166.         m_nHScrollPos = 0;
  167.         SetScrollPos( SB_HORZ, m_nHScrollPos, TRUE );
  168.     }
  169.  
  170.     TRY
  171.     {
  172.         BOOL bValidFileName = FALSE;
  173.         BOOL bStringsReleased = FALSE;
  174.  
  175.         if (FileDlg(TRUE, SIZESTRING, strFileName.GetBuffer(SIZESTRING),
  176.                 SIZESTRING, strFileTitle.GetBuffer(SIZESTRING)))
  177.         {
  178.             strFileName.ReleaseBuffer();
  179.             strFileTitle.ReleaseBuffer();
  180.             bStringsReleased = TRUE;
  181.             bValidFileName = TRUE;
  182.         }
  183.  
  184.         if (!bStringsReleased)
  185.         {
  186.             strFileName.ReleaseBuffer();
  187.             strFileTitle.ReleaseBuffer();
  188.         }
  189.  
  190.         if (bValidFileName)
  191.         {
  192.             //
  193.             // try to open the file here
  194.             //
  195.             m_pMyFile = new CLineFile( strFileName,
  196.                                         CFile::modeRead | CFile::typeBinary);
  197.             m_lFileSize = m_pMyFile->GetLength();
  198.  
  199.             // check to make sure it is a text file
  200.             BYTE byBuf[128];
  201.             for (int iby = m_pMyFile->Read(&byBuf, 128); --iby >= 0;)
  202.                 if (byBuf[iby] > 128)
  203.                 {
  204.                     m_pMyFile->Close();
  205.                     delete m_pMyFile;
  206.                     m_pMyFile = NULL;
  207.                     m_lFileSize = 0L;
  208.                     MessageBox("File contains non-printable characters", 
  209.                         "Error", MB_OK);
  210.                     return;
  211.                 }
  212.             m_pMyFile->SeekToBegin();
  213.  
  214.             SetWindowText( strFileTitle );
  215.             Invalidate(TRUE);
  216.         }
  217.     }
  218.     CATCH( CFileException, e )
  219.     {
  220.         char ErrorMsg[ 80 ];
  221.         sprintf( ErrorMsg,"Opening %s returned a 0x%lx.",
  222.                 (const char*)strFileTitle, e->m_lOsError );
  223.         MessageBox( ErrorMsg, "File Open Error" );
  224.     }
  225.     END_CATCH
  226. }
  227.  
  228. //
  229. // OnVScroll:
  230.  
  231. void CMainWindow::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* /* pScrollBar */)
  232. {
  233.     char        acBuf[SIZESTRING];
  234.     LONG        oldLine = m_lTopLine;
  235.     LONG        l;
  236.  
  237.     if ( m_pMyFile == NULL )
  238.     {
  239.         return;
  240.     }
  241.  
  242.     TRY
  243.     {
  244.         switch ( nSBCode )
  245.         {
  246.         case SB_LINEUP:
  247.             if ( m_lTopLine == 0L )
  248.             {
  249.                 break;
  250.             }
  251.             m_pMyFile->SetBegin( m_lTopLine );
  252.             m_lTopLine = m_pMyFile->BackLines( acBuf, SIZESTRING, 1 );
  253.             break;
  254.  
  255.         case SB_LINEDOWN:
  256.             m_pMyFile->Seek( m_lTopLine, CFile::begin);
  257.             m_lTopLine = m_pMyFile->NextLine( acBuf, SIZESTRING );
  258.             break;
  259.  
  260.         case SB_PAGEUP:
  261.             if ( m_lTopLine == 0L )
  262.             {
  263.                 break;
  264.             }
  265.             m_pMyFile->SetBegin( m_lTopLine );
  266.             m_lTopLine = m_pMyFile->BackLines( acBuf, SIZESTRING,
  267.                                                m_nLinesPainted );
  268.             break;
  269.  
  270.         case SB_PAGEDOWN:
  271.             m_lTopLine = m_pMyFile->GetPosition();
  272.             break;
  273.  
  274.         case SB_BOTTOM:
  275.             nPos = 1000;
  276.             goto ThumbGoTo;
  277.  
  278.         case SB_TOP:
  279.             nPos = 0;
  280.             // fall thru
  281.  
  282.         case SB_THUMBPOSITION:
  283.     ThumbGoTo:
  284.  
  285.             m_nVScrollPos = nPos;
  286.             if ( m_lFileSize > 40000L )
  287.             {
  288.                 l = ( m_lFileSize / 1000L ) * nPos;
  289.             }
  290.             else
  291.             {
  292.                 l = ( m_lFileSize * nPos ) / 1000L;
  293.             }
  294.  
  295.             m_lTopLine = m_pMyFile->LineNear( acBuf, SIZESTRING, l );
  296.             break;
  297.  
  298.         default:
  299.             return;
  300.         }
  301.     }
  302.     CATCH( CFileException, e )
  303.     {
  304.         TRACE( "Bad Seek in OnVScroll\n" );
  305.         m_lTopLine = 0L;
  306.     }
  307.     END_CATCH
  308.  
  309.     if ( m_lTopLine < 0L )
  310.     {
  311.         m_lTopLine = 0L;
  312.     }
  313.  
  314.     if ( m_lFileSize > 40000 )
  315.     {
  316.         m_nVScrollPos = (short)(m_lTopLine  / ( m_lFileSize / 1000L ));
  317.     }
  318.     else
  319.     {
  320.         m_nVScrollPos = (short)(m_lTopLine * 1000L / m_lFileSize);
  321.     }
  322.  
  323.     if ( m_nVScrollPos < 0 )
  324.     {
  325.         m_nVScrollPos = 0;
  326.     }
  327.     if ( m_nVScrollPos > SCROLLMAX )
  328.     {
  329.         m_nVScrollPos = SCROLLMAX;
  330.     }
  331.  
  332.     SetScrollPos( SB_VERT, m_nVScrollPos, TRUE );
  333.  
  334.     if ( m_lTopLine != oldLine )
  335.     {
  336.         Invalidate( FALSE );
  337.     }
  338. }
  339.  
  340. // OnHScroll:
  341. //
  342.  
  343. void
  344. CMainWindow::OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* /* pScrollBar */)
  345. {
  346.     if ( m_pMyFile == NULL )
  347.     {
  348.         return;
  349.     }
  350.  
  351.     switch ( nSBCode )
  352.     {
  353.     case SB_LINEUP:
  354.         m_nHScrollPos -= 1;
  355.         break;
  356.  
  357.     case SB_LINEDOWN:
  358.         m_nHScrollPos += 1;
  359.         break;
  360.  
  361.     case SB_PAGEUP:
  362.         m_nHScrollPos -= 10;
  363.         break;
  364.  
  365.     case SB_PAGEDOWN:
  366.         m_nHScrollPos += 10;
  367.         break;
  368.  
  369.     case SB_TOP:
  370.         nPos = 0;
  371.         // fall thru
  372.     case SB_THUMBPOSITION:
  373.         m_nHScrollPos = nPos;
  374.         break;
  375.  
  376.     default:
  377.         return;
  378.     }
  379.  
  380.     if ( m_nHScrollPos < 0 )
  381.     {
  382.         m_nHScrollPos = 0;
  383.     }
  384.  
  385.     if ( m_nHScrollPos > SIZESTRING )
  386.     {
  387.         m_nHScrollPos = SIZESTRING;
  388.     }
  389.  
  390.     SetScrollPos( SB_HORZ, m_nHScrollPos, TRUE );
  391.     Invalidate( FALSE );
  392. }
  393.  
  394. //
  395. // catch arrow keys and simulate touching scroll bars
  396. //
  397. void
  398. CMainWindow::OnKeyDown( UINT wChar, UINT /* nRepCnt */, UINT /* wFlags */)
  399. {
  400.     switch( wChar )
  401.     {
  402.     case VK_HOME:
  403.         SendMessage( WM_VSCROLL, SB_TOP, 0L);
  404.         SendMessage( WM_HSCROLL, SB_TOP, 0L);
  405.         break;
  406.  
  407.     case VK_END:
  408.         SendMessage( WM_VSCROLL, SB_BOTTOM, 0L);
  409.         break;
  410.  
  411.     case VK_PRIOR:
  412.         SendMessage( WM_VSCROLL, SB_PAGEUP, 0L);
  413.         break;
  414.  
  415.     case VK_NEXT:
  416.         SendMessage( WM_VSCROLL, SB_PAGEDOWN, 0L);
  417.         break;
  418.  
  419.     case VK_UP:
  420.         SendMessage( WM_VSCROLL, SB_LINEUP, 0L);
  421.         break;
  422.  
  423.     case VK_DOWN:
  424.         SendMessage( WM_VSCROLL, SB_LINEDOWN, 0L);
  425.         break;
  426.  
  427.     case VK_RIGHT:
  428.         SendMessage( WM_HSCROLL, SB_LINEDOWN, 0L);
  429.         break;
  430.  
  431.     case VK_LEFT:
  432.         SendMessage( WM_HSCROLL, SB_LINEUP, 0L);
  433.         break;
  434.     }
  435. }
  436.  
  437. BOOL
  438. CMainWindow::FileDlg( BOOL bOpen, int nMaxFile, LPSTR szFile,
  439.         int nMaxFileTitle, LPSTR szFileTitle )
  440. {
  441.     char szFilter[] = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
  442.     CFileDialog dlg(bOpen, "txt", szFile, OFN_HIDEREADONLY, szFilter);
  443.  
  444.     // This example shows how to access the OPENFILENAME struct
  445.     // directly.
  446.     dlg.m_ofn.lpstrFile = szFile;
  447.     dlg.m_ofn.lpstrFileTitle = szFileTitle;
  448.     dlg.m_ofn.nMaxFileTitle = nMaxFileTitle;
  449.     
  450.     return dlg.DoModal() == IDOK ? TRUE : FALSE;
  451. }
  452.  
  453. // CMainWindow message map:
  454. // Associate messages with member functions.
  455. //
  456. // It is implied that the ON_WM_PAINT macro expects a member function
  457. // "void OnPaint()".
  458. //
  459. // It is implied that members connected with the ON_COMMAND macro
  460. // receive no arguments and are void of return type, e.g., "void OnAbout()".
  461. //
  462. BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
  463.     ON_WM_PAINT()
  464.     ON_COMMAND( IDM_ABOUT, OnAbout )
  465.     ON_COMMAND( IDM_OPEN,  OnOpen )
  466.     ON_COMMAND( IDM_EXIT,  OnExit )
  467.     ON_WM_HSCROLL()
  468.     ON_WM_VSCROLL()
  469.     ON_WM_CREATE()
  470.     ON_WM_KEYDOWN()
  471. END_MESSAGE_MAP()
  472.  
  473. /////////////////////////////////////////////////////////////////////////////
  474. // CTheApp
  475.  
  476. // InitInstance:
  477. // When any CTheApp object is created, this member function is automatically
  478. // called.  Any data may be set up at this point.
  479. //
  480. // Also, the main window of the application should be created and shown here.
  481. // Return TRUE if the initialization is successful.
  482. //
  483. BOOL CTheApp::InitInstance()
  484. {
  485.     m_pMainWnd = new CMainWindow();
  486.     m_pMainWnd->ShowWindow( m_nCmdShow );
  487.     m_pMainWnd->UpdateWindow();
  488.  
  489.     return TRUE;
  490. }
  491.