home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c11 / lab02 / baseline / ic_2view.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  8.1 KB  |  326 lines

  1. // IC_2View.cpp : implementation of the CIC_2View class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "IC_2.h"
  6.  
  7. #include "IC_2Doc.h"
  8. #include "IC_2View.h"
  9.  
  10. #include "UrlDialog.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CIC_2View
  20.  
  21. IMPLEMENT_DYNCREATE(CIC_2View, CScrollView)
  22.  
  23. BEGIN_MESSAGE_MAP(CIC_2View, CScrollView)
  24.     //{{AFX_MSG_MAP(CIC_2View)
  25.     ON_COMMAND(ID_INTERNET_CREATESESSION, OnInternetCreateSession)
  26.     ON_UPDATE_COMMAND_UI(ID_INTERNET_CREATESESSION, OnUpdateInternetCreateSession)
  27.     ON_COMMAND(ID_INTERNET_GETCONNECTION, OnInternetGetConnection)
  28.     ON_UPDATE_COMMAND_UI(ID_INTERNET_GETCONNECTION, OnUpdateInternetGetConnection)
  29.     ON_COMMAND(ID_INTERNET_CLOSESESSION, OnInternetCloseSession)
  30.     ON_UPDATE_COMMAND_UI(ID_INTERNET_CLOSESESSION, OnUpdateInternetCloseSession)
  31.     ON_COMMAND(ID_INTERNET_SENDREQUEST, OnInternetSendRequest)
  32.     ON_COMMAND(ID_INTERNET_OPENREQUEST, OnInternetOpenRequest)
  33.     ON_COMMAND(ID_INTERNET_READINFORMATION, OnInternetReadInformation)
  34.     ON_UPDATE_COMMAND_UI(ID_INTERNET_OPENREQUEST, OnUpdateInternetOpenRequest)
  35.     ON_UPDATE_COMMAND_UI(ID_INTERNET_SENDREQUEST, OnUpdateInternetSendRequest)
  36.     ON_UPDATE_COMMAND_UI(ID_INTERNET_READINFORMATION, OnUpdateInternetReadInformation)
  37.     ON_COMMAND(ID_INTERNET_OBTAINURL, OnInternetObtainUrl)
  38.     ON_COMMAND(ID_INTERNET_ALLTHEABOVE, OnInternetAllTheAbove)
  39.     ON_UPDATE_COMMAND_UI(ID_INTERNET_ALLTHEABOVE, OnUpdateInternetAllTheAbove)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CIC_2View construction/destruction
  45.  
  46. CIC_2View::CIC_2View()
  47. {
  48. }
  49.  
  50. CIC_2View::~CIC_2View()
  51. {
  52.     CloseSession();
  53. }
  54.  
  55. BOOL CIC_2View::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     return CScrollView::PreCreateWindow(cs);
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CIC_2View drawing
  62.  
  63. void CIC_2View::OnDraw(CDC* pDC)
  64. {
  65.     CIC_2Doc* pDoc = GetDocument();
  66.     ASSERT_VALID(pDoc);
  67.  
  68.     // Do nothing if there's nothing to display.
  69.     if (pDoc->m_WebPage.IsEmpty() == TRUE)
  70.         return;
  71.  
  72.     int BeginningRow, EndingRow, i, x, y;
  73.     TEXTMETRIC tm;
  74.     CPoint pt;
  75.     CRect rect;
  76.  
  77.     pDC->GetTextMetrics(&tm);
  78.     pt = GetScrollPosition();
  79.   
  80.     // In the interest of efficiency, only display as many rows of the
  81.     // retrieved document as the view's height will allow. This requires
  82.     // knowing 2 things: the current vertical scroll position...
  83.     BeginningRow = pt.y / tm.tmHeight;
  84.  
  85.     // and the current height of the view window, which determines
  86.     // how many 'rows' can be displayed.
  87.     GetClientRect(&rect);
  88.     EndingRow = BeginningRow + rect.Height() / tm.tmHeight;
  89.     
  90.     // However, don't attempt to display more lines than exist
  91.     // in the document.
  92.     int linecount = pDoc->m_WebPage.GetCount();
  93.     if (EndingRow > linecount)
  94.         EndingRow = linecount;
  95.         
  96.     x = 0;
  97.     y = BeginningRow * tm.tmHeight;
  98.     POSITION pos = pDoc->m_WebPage.FindIndex(BeginningRow);
  99.     for (i = BeginningRow; i <= EndingRow; i++, y += tm.tmHeight)
  100.     {
  101.         if (!pos) break;
  102.         CString & s = pDoc->m_WebPage.GetNext(pos);
  103.         pDC->TabbedTextOut( x, y, s, 0, NULL, 0);
  104.     }
  105.     
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CIC_2View diagnostics
  110.  
  111. #ifdef _DEBUG
  112. void CIC_2View::AssertValid() const
  113. {
  114.     CScrollView::AssertValid();
  115. }
  116.  
  117. void CIC_2View::Dump(CDumpContext& dc) const
  118. {
  119.     CScrollView::Dump(dc);
  120. }
  121.  
  122. CIC_2Doc* CIC_2View::GetDocument() // non-debug version is inline
  123. {
  124.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CIC_2Doc)));
  125.     return (CIC_2Doc*)m_pDocument;
  126. }
  127. #endif //_DEBUG
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130. // CIC_2View message handlers
  131. void CIC_2View::OnInternetObtainUrl() 
  132. {
  133.     CIC_2Doc* pDoc = GetDocument();
  134.  
  135.     CUrlDialog dlg;
  136.     
  137.     if (IDOK == dlg.DoModal())
  138.     {
  139.         pDoc->m_Object = dlg.m_Object;
  140.         pDoc->m_Server = dlg.m_Server;
  141.         pDoc->m_Port = dlg.m_Port;
  142.         pDoc->m_ServerType = dlg.m_ServerType;
  143.         m_processState = 0;
  144.     }
  145. }
  146.  
  147. void CIC_2View::OnInternetCreateSession() 
  148. {
  149. }
  150.  
  151. void CIC_2View::OnInternetGetConnection() 
  152. {
  153. }
  154.  
  155. void CIC_2View::OnInternetOpenRequest() 
  156. {
  157. }
  158.  
  159. void CIC_2View::OnInternetSendRequest() 
  160. {
  161. }
  162.  
  163. void CIC_2View::OnInternetReadInformation() 
  164. {
  165. }
  166.  
  167. // This is a shortcut way of calling all the 5 menu items.
  168. void CIC_2View::OnInternetAllTheAbove() 
  169. {
  170.     this->SendMessage(WM_COMMAND, ID_INTERNET_CREATESESSION);
  171.     this->SendMessage(WM_COMMAND, ID_INTERNET_GETCONNECTION);
  172.     this->SendMessage(WM_COMMAND, ID_INTERNET_OPENREQUEST);
  173.     this->SendMessage(WM_COMMAND, ID_INTERNET_SENDREQUEST);
  174.     this->SendMessage(WM_COMMAND, ID_INTERNET_READINFORMATION);
  175. }
  176.  
  177. void CIC_2View::OnInternetCloseSession() 
  178. {
  179.     // clean up any objects that are still lying around
  180.     CloseSession();
  181.  
  182.     CIC_2Doc* pDoc = GetDocument();
  183.     pDoc->UpdateAllViews(NULL);
  184.  
  185.     m_processState = -1;
  186. }
  187.  
  188. void CIC_2View::OnUpdateInternetCreateSession(CCmdUI* pCmdUI) 
  189. {
  190.     pCmdUI->Enable(m_processState == 0);
  191. }
  192.  
  193. void CIC_2View::OnUpdateInternetGetConnection(CCmdUI* pCmdUI) 
  194. {
  195.     pCmdUI->Enable(m_processState == 1);
  196. }
  197.  
  198. void CIC_2View::OnUpdateInternetOpenRequest(CCmdUI* pCmdUI) 
  199. {
  200.     pCmdUI->Enable(m_processState == 2);
  201. }
  202.  
  203. void CIC_2View::OnUpdateInternetSendRequest(CCmdUI* pCmdUI) 
  204. {
  205.     pCmdUI->Enable(m_processState == 3);
  206. }
  207.  
  208. void CIC_2View::OnUpdateInternetReadInformation(CCmdUI* pCmdUI) 
  209. {
  210.     pCmdUI->Enable(m_processState == 4);
  211. }
  212.  
  213. void CIC_2View::OnUpdateInternetAllTheAbove(CCmdUI* pCmdUI) 
  214. {
  215.     pCmdUI->Enable(m_processState == 0);
  216. }
  217.  
  218. void CIC_2View::OnUpdateInternetCloseSession(CCmdUI* pCmdUI) 
  219. {
  220.     pCmdUI->Enable(m_processState > 0);
  221. }
  222.  
  223. void CIC_2View::OnInitialUpdate() 
  224. {
  225.     CScrollView::OnInitialUpdate();
  226.  
  227.     m_pInternetSession = NULL;
  228.     m_pHttpConnection = NULL;
  229.     m_pHttpFile = NULL;
  230.     m_processState = -1;
  231. }
  232.  
  233. BOOL CIC_2View::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll) 
  234. {
  235.     // If we're scrolling in the vertical direction by use of the
  236.     // thumbtack, we only want to scroll in units equal to a character's
  237.     // height. nPos is adjusted so it's always equal to some multiple
  238.     // of a character's height.
  239.     if (SB_THUMBTRACK == HIBYTE(nScrollCode))
  240.     {
  241.         TEXTMETRIC tm;
  242.         CClientDC dc(this);
  243.         dc.GetTextMetrics(&tm);
  244.         while ((nPos % tm.tmHeight) != 0)
  245.             nPos--;
  246.     }
  247.  
  248.     return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
  249. }
  250.  
  251. void CIC_2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  252. {
  253.     CView::OnUpdate(pSender, lHint, pHint);
  254.     SetScrollingAttributes();
  255. }
  256.  
  257. void CIC_2View::SetScrollingAttributes()
  258. {
  259.     CIC_2Doc* pDoc = GetDocument();
  260.  
  261.     CSize sizeTotal, sizePage, sizeLine;
  262.  
  263.     CClientDC dc(this);
  264.     TEXTMETRIC tm;
  265.     dc.GetTextMetrics(&tm);
  266.  
  267.     // Calculate the total size of this view
  268.     sizeTotal.cx = 80 * tm.tmMaxCharWidth;
  269.     
  270.     // Just for grins, if there are no lines to be displayed,
  271.     // I'll use the classic 25x80 display size.
  272.     if (pDoc->m_WebPage.GetCount() == 0)
  273.         sizeTotal.cy = 25 * tm.tmHeight;     
  274.     else
  275.         sizeTotal.cy = pDoc->m_WebPage.GetCount() * tm.tmHeight;     
  276.     // Note: this value cannot exceed 32767
  277.  
  278.     // Scroll one letter in either direction when
  279.     // clicking on a scroll bar's arrows.
  280.     // Current page length when clicking in the vertical shaft.
  281.     // 5 characters when clicking in the horizontal shaft.
  282.     sizeLine.cx = tm.tmAveCharWidth;
  283.     sizeLine.cy = tm.tmHeight;
  284.     sizePage.cx = 5 * tm.tmAveCharWidth;
  285.     sizePage.cy = 25 * tm.tmHeight;
  286.  
  287.     SetScrollSizes(MM_TEXT, sizeTotal, sizePage, sizeLine);
  288.  
  289.     GetParentFrame()->RecalcLayout();
  290.     ResizeParentToFit();
  291. }
  292. void CIC_2View::CloseSession()
  293. {
  294.     if (m_pInternetSession != NULL)
  295.     {
  296.         m_pInternetSession->Close();
  297.         delete m_pInternetSession;
  298.         m_pInternetSession = NULL;
  299.     }
  300.     
  301.     if (m_pHttpConnection != NULL)
  302.     {
  303.         m_pHttpConnection->Close();
  304.         delete m_pHttpConnection;
  305.         m_pHttpConnection = NULL;
  306.     }
  307.  
  308.     if (m_pHttpFile != NULL)
  309.     {
  310.         m_pHttpFile->Close();
  311.         delete m_pHttpFile;
  312.         m_pHttpFile = NULL;
  313.     }
  314.     
  315.     CIC_2Doc* pDoc = GetDocument();
  316.     while (!pDoc->m_WebPage.IsEmpty()) 
  317.     {
  318.         CString String= pDoc->m_WebPage.RemoveHead();
  319.     }
  320.  
  321. //    pDoc->m_WebPage.RemoveAll();
  322.  
  323. }
  324.  
  325.  
  326.