home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Windows 95 Special 1 / WINDOWS95_1.bin / internet / vogon / vogonvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-29  |  10.5 KB  |  347 lines

  1. // vogonvw.cpp : implementation of the CVogonView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "vogon.h"
  6.  
  7. #include "CntrInfo.h"
  8. #include "CntrItem.h"
  9. #include "vogondoc.h"
  10. #include "vogonvw.h"
  11.  
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CVogonView
  19.  
  20. IMPLEMENT_DYNCREATE(CVogonView, CView)
  21.  
  22. BEGIN_MESSAGE_MAP(CVogonView, CView)
  23.    //{{AFX_MSG_MAP(CVogonView)
  24.    ON_WM_SETFOCUS()
  25.    ON_WM_SIZE()
  26.    ON_COMMAND(ID_VIEW_WEBSTER, OnInsertWebster)
  27.    ON_COMMAND(ID_VIEW_ABOUTWEBSTER, OnAboutWebster)
  28.     ON_COMMAND(ID_SHOW_PROPERTY_INSPECTOR, OnShowPropertyInspector)
  29.     ON_UPDATE_COMMAND_UI(ID_SHOW_PROPERTY_INSPECTOR, OnUpdateShowPropertyInspector)
  30.     ON_UPDATE_COMMAND_UI(ID_VIEW_ABOUTWEBSTER, OnUpdateViewAboutwebster)
  31.     ON_COMMAND(ID_VIEW_ACTIVATE, OnActivateWebster)
  32.     ON_UPDATE_COMMAND_UI(ID_VIEW_SLIDESHOW, OnUpdateSlideShow)
  33.     ON_COMMAND(ID_VIEW_SLIDESHOW, OnSlideShow)
  34.     ON_WM_TIMER()
  35.     ON_WM_DESTROY()
  36.     //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CVogonView construction/destruction
  41.  
  42. CVogonView::CVogonView()
  43. {
  44.    // TODO: add construction code here
  45.    m_pSelection    = NULL;
  46.    m_idTimerSlides = 0;
  47. }
  48.  
  49. CVogonView::~CVogonView()
  50. {
  51. }
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CVogonView drawing
  55.  
  56. void CVogonView::OnDraw(CDC* pDC)
  57. {
  58.    CVogonDoc* pDoc = GetDocument();
  59.    ASSERT_VALID(pDoc);
  60.  
  61.    // TODO: add draw code for native data here
  62.    // TODO: also draw all OLE items in the document
  63.  
  64.    // Draw the selection at an arbitrary position.  This code should be
  65.    //  removed once your real drawing code is implemented.  This position
  66.    //  corresponds exactly to the rectangle returned by CVogonCntrItem,
  67.    //  to give the effect of in-place editing.
  68.  
  69.    // TODO: remove this code when final draw code is complete.
  70. #ifdef NOTNOW
  71.    if (m_pSelection == NULL)
  72.    {
  73.       POSITION pos = pDoc->GetStartPosition();
  74.       m_pSelection = (CVogonCntrItem*)pDoc->GetNextClientItem(pos);
  75.    }
  76.    if (m_pSelection != NULL)
  77.       m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));
  78. #endif // NOTNOW
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CVogonView drawing
  83. void CVogonView::OnInitialUpdate()
  84. {
  85.    POSITION pos = GetDocument()->GetStartPosition();
  86.    if (pos)
  87.    {  // Have at least one item:
  88.       CVogonCntrItem* pItem = (CVogonCntrItem*) GetDocument()->GetNextItem(pos);
  89.       // Select the lone item
  90.       m_pSelection = pItem;
  91.       // Cannot activate here due to hook problems
  92.       PostMessage(WM_COMMAND, ID_VIEW_ACTIVATE, 0);
  93.    }
  94.    else
  95.    {  // Need to create the control:
  96.       m_pSelection = NULL;    // initialize selection
  97.       // Cannot call OnInsertWebster() from here, it messes up CMainFrame menu handling:
  98.       PostMessage(WM_COMMAND, ID_VIEW_WEBSTER, 0);
  99.    }
  100.  
  101.    CView::OnInitialUpdate();
  102. }
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // OLE Client support and commands
  106.  
  107. BOOL CVogonView::IsSelected(const CObject* pDocItem) const
  108. {
  109.    // The implementation below is adequate if your selection consists of
  110.    //  only CVogonCntrItem objects.  To handle different selection
  111.    //  mechanisms, the implementation here should be replaced.
  112.  
  113.    // TODO: implement this function that tests for a selected OLE client item
  114.  
  115.    return pDocItem == m_pSelection;
  116. }
  117.  
  118. // Special handling of OnSetFocus and OnSize are required for a container
  119. //  when an object is being edited in-place.
  120. void CVogonView::OnSetFocus(CWnd* pOldWnd)
  121. {
  122.    COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  123.    if (pActiveItem != NULL &&
  124.       pActiveItem->GetItemState() == COleClientItem::activeUIState)
  125.    {
  126.       // need to set focus to this item if it is in the same view
  127.       CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  128.       if (pWnd != NULL)
  129.       {
  130.          pWnd->SetFocus();   // don't call the base class
  131.          return;
  132.       }
  133.    }
  134.  
  135.    // CView::OnSetFocus(pOldWnd);
  136. }
  137.  
  138. void CVogonView::OnSize(UINT nType, int cx, int cy)
  139. {
  140.    // Autosize to the view
  141.    if (m_pSelection && m_pSelection->IsInPlaceActive())
  142.    {  // We have a control
  143.       m_pSelection->SetItemRects(CRect(0, 0, cx, cy));
  144.    }
  145. }
  146.  
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CVogonView diagnostics
  149.  
  150. #ifdef _DEBUG
  151. void CVogonView::AssertValid() const
  152. {
  153.    CView::AssertValid();
  154. }
  155.  
  156. void CVogonView::Dump(CDumpContext& dc) const
  157. {
  158.    CView::Dump(dc);
  159. }
  160.  
  161. CVogonDoc* CVogonView::GetDocument() // non-debug version is inline
  162. {
  163.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVogonDoc)));
  164.    return (CVogonDoc*)m_pDocument;
  165. }
  166. #endif //_DEBUG
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CVogonView message handlers
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. //
  173. void CVogonView::OnInsertWebster() 
  174. {
  175.    if (GetDocument()->GetStartPosition() == NULL)
  176.    {  // Insert the webster control
  177.       BeginWaitCursor();
  178.  
  179.       CVogonCntrItem* pItem = NULL;
  180.       TRY
  181.       {  // Create new item connected to our document.
  182.          CVogonDoc* pDoc = GetDocument();
  183.          ASSERT_VALID(pDoc);
  184.          pItem = new CVogonCntrItem(pDoc);
  185.          ASSERT_VALID(pItem);
  186.          // Translate the registry key to an OLE class ID
  187.          CLSID classID;
  188.          HRESULT hRes = CLSIDFromString((LPSTR) (LPCSTR) _T("WEBSTER.WebsterCtrl.1"), &classID);
  189.          TRACE("CLSIDFromString status = %d\n", hRes);
  190.          // Create the new control item:
  191.          if (!pItem->CreateNewItem(classID))
  192.          {  // "Any exception will do"
  193.             AfxThrowMemoryException();  
  194.          }
  195.          ASSERT_VALID(pItem);
  196.          // Allow the server to refresh and size itself:
  197.          pItem->UpdateLink();
  198.          // As an arbitrary user interface design, this sets the selection
  199.          //  to the last item inserted.
  200.          m_pSelection = pItem;
  201.          // Have a window - activate
  202.          OnActivateWebster();
  203.          // Refresh
  204.          Invalidate();
  205.          // Tell Webster who we are - this will become an HTTP header
  206.          CString stringWhoWeAre;
  207.          stringWhoWeAre.LoadString(IDP_VOGON_VERSION);
  208.          m_pSelection->m_dWebster.SetBrowserName(stringWhoWeAre);
  209.          // Hide the URL window - we show our own status
  210.          m_pSelection->m_dWebster.SetUrlWindowStyle(0);
  211.       }
  212.       CATCH(CException, e)
  213.       {  // Failure has occured:
  214.          if (pItem != NULL)
  215.          {
  216.             ASSERT_VALID(pItem);
  217.             pItem->Delete();
  218.          }
  219.          m_pSelection = NULL;
  220.          AfxMessageBox(IDP_FAILED_TO_CREATE);
  221.       }
  222.       END_CATCH
  223.       // Done
  224.       EndWaitCursor();
  225.    }
  226. }
  227.  
  228. /////////////////////////////////////////////////////////////////////////////
  229. // CVogonView message handlers
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232. // 
  233. void CVogonView::OnDestroy() 
  234. {  // We need to kill any timers before the window is gone ..
  235.    if (m_idTimerSlides)
  236.    {  // Had a timer
  237.       KillTimer(m_idTimerSlides);
  238.       // Be polite, clean up ..
  239.       m_idTimerSlides = 0;
  240.       TRACE("OnDestroy found active timer, shutting down\n");
  241.    }
  242.    // Allow base class to do it's thing
  243.    CView::OnDestroy();
  244. }
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247. // 
  248. void CVogonView::OnAboutWebster() 
  249. {
  250.    m_pSelection->m_dWebster.AboutBox();
  251. }
  252.  
  253. /////////////////////////////////////////////////////////////////////////////
  254. // 
  255. void CVogonView::OnUpdateViewAboutwebster(CCmdUI* pCmdUI) 
  256. {
  257.    pCmdUI->Enable(m_pSelection != NULL);
  258. }
  259.  
  260. /////////////////////////////////////////////////////////////////////////////
  261. // 
  262. void CVogonView::OnShowPropertyInspector() 
  263. {  // Bring up the property pages
  264.    m_pSelection->DoVerb(OLEIVERB_PRIMARY, this);
  265. }
  266.  
  267. /////////////////////////////////////////////////////////////////////////////
  268. // 
  269. void CVogonView::OnUpdateShowPropertyInspector(CCmdUI* pCmdUI) 
  270. {
  271.    pCmdUI->Enable(m_pSelection != NULL);
  272. }
  273.  
  274. /////////////////////////////////////////////////////////////////////////////
  275. // 
  276. void CVogonView::OnActivateWebster() 
  277. {  // Activate-in-place
  278.    doActiveSelection();
  279.    // Size to fit the view
  280.    CRect rectClient;
  281.    GetClientRect(rectClient);
  282.    m_pSelection->SetItemRects(rectClient);
  283. }
  284.  
  285. /////////////////////////////////////////////////////////////////////////////
  286. // 
  287. void CVogonView::OnUpdateSlideShow(CCmdUI* pCmdUI) 
  288. {
  289.    pCmdUI->SetCheck(m_idTimerSlides);
  290. }
  291.  
  292. /////////////////////////////////////////////////////////////////////////////
  293. // 
  294. void CVogonView::OnSlideShow() 
  295. {  // Toggle the slide show
  296.    if (m_idTimerSlides)
  297.    {  // Was on - switch off
  298.       KillTimer(m_idTimerSlides);
  299.       m_idTimerSlides = 0;
  300.    }
  301.    else
  302.    {  // Was off - turn on a one-second timer, if any slides present:
  303.       CString stringSlide(GetDocument()->GetFirstSlideName());
  304.       if (!stringSlide.IsEmpty())
  305.       {  // Have slides: set the timer
  306.          m_idTimerSlides = SetTimer(1, 1000, NULL);
  307.          // Remember the start time
  308.          m_timeLastSlide = time(NULL);
  309.          // Set the first slide
  310.          m_pSelection->m_dWebster.SetPageURL(stringSlide);
  311.       }
  312.    }
  313. }
  314.  
  315. /////////////////////////////////////////////////////////////////////////////
  316. // 
  317. void CVogonView::OnTimer(UINT nIDEvent) 
  318. {
  319.    if (nIDEvent == m_idTimerSlides)
  320.    {  // Our timer: is the time up ?
  321.       CTimeSpan timeSpan = CTime(time(NULL)) - m_timeLastSlide;
  322.       if (timeSpan.GetTotalSeconds() >= GetDocument()->GetSlideInterval())
  323.       {  // Time for the next URL:
  324.          CString stringSlide(GetDocument()->GetNextSlideName());
  325.          // At the end of the list, if no repeat, string will be blank
  326.          if (!stringSlide.IsEmpty())
  327.          {  // Cycle to the next page
  328.             TRACE("Slide (%d sec) is \"%s\"\n", 
  329.                  GetDocument()->GetSlideInterval(), (LPCTSTR) stringSlide);
  330.             m_pSelection->m_dWebster.SetPageURL(stringSlide);
  331.             // Remember the start time
  332.             m_timeLastSlide = time(NULL);
  333.          }
  334.          else
  335.          {  // Toggle off
  336.             KillTimer(m_idTimerSlides);
  337.             m_idTimerSlides = 0;
  338.          }
  339.       }
  340.    }
  341.    else
  342.    {  // Must be for the base class
  343.       CView::OnTimer(nIDEvent);
  344.    }
  345. }
  346.  
  347.