home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / activex / inetsdk / samples / urlpad / superpad.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-25  |  8.4 KB  |  331 lines

  1. //=------------------------------------------------------------------------=
  2. // SuperPad.Cpp
  3. //=------------------------------------------------------------------------=
  4. // Copyright 1992-1996 Microsoft Corporation.  All Rights Reserved.
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //=--------------------------------------------------------------------------=
  11. //
  12. // Defines the class behaviors for the application.
  13. //
  14.  
  15. #include "stdafx.h"
  16. #include "superpad.h"
  17. #include "mainfrm.h"
  18. #include "padview.h"
  19. #include "paddoc.h"
  20. #include "padframe.h"
  21. #include "ipframe.h"
  22.  
  23. #include <locale.h>
  24.  
  25. #include "download.h"
  26. #include "urlmon.h"
  27. #include "Fileopen.h"
  28.  
  29. BEGIN_MESSAGE_MAP(CTheApp, CWinApp)
  30.     //{{AFX_MSG_MAP(CTheApp)
  31.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  32.     ON_COMMAND(ID_PAGE_SETUP, OnPageSetup)
  33.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  34.     //}}AFX_MSG_MAP
  35.     // Standard file based document commands
  36.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  37.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  38.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // Superpad global data
  43.  
  44. CTheApp NEAR theApp;
  45.  
  46. // this is the GUID for OCLIENT documents
  47. static const GUID BASED_CODE clsid =
  48.     { 0x00021844, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CTheApp initialization
  52.  
  53. CTheApp::CTheApp()
  54. {
  55.     // initialize locale to system default
  56.     _tsetlocale(LC_ALL, _T(""));
  57. }
  58.  
  59. BOOL CTheApp::InitInstance()
  60. {
  61.     // initialized OLE 2.0 libraries
  62.     if (!AfxOleInit())
  63.     {
  64.         AfxMessageBox(IDP_OLE_INIT_FAILED);
  65.         return FALSE;
  66.     }
  67.  
  68.     // initialization that must be done before splash dialog is shown
  69.     Enable3dControls();
  70.     CPadFrame::Initialize();
  71.  
  72.     // add doc template
  73.     CDocTemplate* pDocTemplate;
  74.     pDocTemplate = new CMultiDocTemplate(IDR_TEXTTYPE,
  75.         RUNTIME_CLASS(CPadDoc),
  76.         RUNTIME_CLASS(CPadFrame),
  77.         RUNTIME_CLASS(CPadView));
  78.     pDocTemplate->SetServerInfo(
  79.         IDR_TEXTTYPE_EMBEDDED, IDR_TEXTTYPE_INPLACE,
  80.         RUNTIME_CLASS(CInPlaceFrame));
  81.     AddDocTemplate(pDocTemplate);
  82.  
  83.     // connect the server to the document template
  84.     m_server.ConnectTemplate(clsid, pDocTemplate, FALSE);
  85.  
  86.     // create main window
  87.     CMainFrame* pMainFrame = new CMainFrame;
  88.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  89.         return FALSE;
  90.     m_pMainWnd = pMainFrame;
  91.     int nCmdShow = m_nCmdShow;
  92.  
  93.     // Parse command line for standard shell commands, DDE, file open
  94.     CCommandLineInfo cmdInfo;
  95.     ParseCommandLine(cmdInfo);
  96.  
  97.     // setup main window
  98.     nCmdShow = !cmdInfo.m_bRunEmbedded ? m_nCmdShow : SW_HIDE;
  99.     m_nCmdShow = SW_HIDE;
  100.     ((CMainFrame*)m_pMainWnd)->InitialShowWindow(nCmdShow);
  101.  
  102.     if (!cmdInfo.m_bRunEmbedded)
  103.     {
  104.         m_pMainWnd->UpdateWindow();
  105.  
  106.         if (!m_pMainWnd->IsIconic() && cmdInfo.m_bShowSplash &&
  107.             m_splash.Create(m_pMainWnd))
  108.         {
  109.             m_splash.ShowWindow(SW_SHOW);
  110.             m_splash.UpdateWindow();
  111.             m_splash.SetTimer(1, 500, NULL);
  112.         }
  113.         m_dwSplashTime = ::GetCurrentTime();
  114.  
  115.         // update the registry now that the splash dialog is visible
  116.         m_server.UpdateRegistry();
  117.         RegisterShellFileTypes(TRUE);
  118.     }
  119.  
  120.     // do other initialization after (possibly) creating the splash window
  121.     EnableShellOpen();
  122.     m_pMainWnd->DragAcceptFiles();
  123.  
  124.     LoadStdProfileSettings();
  125.     CPadView::Initialize();
  126.     dlgPageSetup.Initialize();
  127.  
  128.     // register any class objects we may have (for automation & OLE)
  129.     COleTemplateServer::RegisterAll();
  130.  
  131.     // open default file if command line doesn't have /Embedding
  132.     if (!cmdInfo.m_bRunEmbedded)
  133.     {
  134.         // Dispatch commands specified on the command line
  135.         if (!ProcessShellCommand(cmdInfo))
  136.             return FALSE;
  137.     
  138.         m_nCmdShow = nCmdShow;
  139.         m_pMainWnd->UpdateWindow();
  140.     }
  141.  
  142.     return TRUE;
  143. }
  144.  
  145. BOOL CTheApp::OnIdle(LONG lCount)
  146. {
  147.     // call base class idle first
  148.     BOOL bResult = CWinApp::OnIdle(lCount);
  149.  
  150.     // then do our work
  151.     if (m_splash.m_hWnd != NULL)
  152.     {
  153.         if (::GetCurrentTime() - m_dwSplashTime > 2500)
  154.         {
  155.             // timeout expired, destroy the splash window
  156.             m_splash.DestroyWindow();
  157.             m_pMainWnd->UpdateWindow();
  158.  
  159.             // NOTE: don't set bResult to FALSE,
  160.             //  CWinApp::OnIdle may have returned TRUE
  161.         }
  162.         else
  163.         {
  164.             // check again later...
  165.             bResult = TRUE;
  166.         }
  167.     }
  168.  
  169.     return bResult;
  170. }
  171.  
  172. BOOL CTheApp::PreTranslateMessage(MSG* pMsg)
  173. {
  174.     BOOL bResult = CWinApp::PreTranslateMessage(pMsg);
  175.  
  176.     if (m_splash.m_hWnd != NULL &&
  177.         (pMsg->message == WM_KEYDOWN ||
  178.          pMsg->message == WM_SYSKEYDOWN ||
  179.          pMsg->message == WM_LBUTTONDOWN ||
  180.          pMsg->message == WM_RBUTTONDOWN ||
  181.          pMsg->message == WM_MBUTTONDOWN ||
  182.          pMsg->message == WM_NCLBUTTONDOWN ||
  183.          pMsg->message == WM_NCRBUTTONDOWN ||
  184.          pMsg->message == WM_NCMBUTTONDOWN))
  185.     {
  186.         m_splash.DestroyWindow();
  187.         m_pMainWnd->UpdateWindow();
  188.     }
  189.  
  190.     return bResult;
  191. }
  192.  
  193. int CTheApp::ExitInstance()
  194. {
  195.     dlgPageSetup.Terminate();
  196.     CPadView::Terminate();
  197.     CPadFrame::Terminate();
  198.  
  199.     return CWinApp::ExitInstance();
  200. }
  201.  
  202. CTheApp::~CTheApp()
  203. {
  204. }
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207. // CTheApp message handlers
  208.  
  209. void CTheApp::OnAppAbout()
  210. {
  211.     CAboutBox about;
  212.     about.DoModal();
  213. }
  214.  
  215. CPageSetupDlg NEAR dlgPageSetup;
  216. void CTheApp::OnPageSetup()
  217. {
  218.     dlgPageSetup.DoModal();
  219. }
  220.  
  221. void CTheApp::OnFileOpen() 
  222. {
  223.     // Prompt the user (with all document templates)
  224.     CString newName;
  225.     UINT retval;
  226.  
  227.     retval=PromptForFileName(newName, AFX_IDS_OPENFILE, 
  228.                            OFN_HIDEREADONLY|OFN_ENABLETEMPLATE|OFN_NOVALIDATE,
  229.                            TRUE);
  230.  
  231.     if (retval==0)
  232.         return; // open cancelled
  233.     else
  234.         if (retval==1)  // If this an ordinary file then just open
  235.         {
  236.             OpenDocumentFile(newName);
  237.             return;
  238.     }
  239.  
  240.     CWinApp* pApp = AfxGetApp() ;
  241.     CMainFrame* pFrame = (CMainFrame*)pApp->m_pMainWnd;
  242.     CBindStatusCallback *dtfcb= new CBindStatusCallback(pFrame); 
  243.     if( !dtfcb )
  244.     {
  245.         // Error to be handled
  246.         return;
  247.     }
  248.     
  249.     // File to be downloaded to temporary directory
  250.     static TCHAR count = '1';
  251.     CString localfile;
  252.  
  253.     LPTSTR tempPath= new CHAR[MAX_PATH];
  254.     if (tempPath)
  255.     {
  256.         ::GetTempPath(MAX_PATH,tempPath);
  257.         localfile+=tempPath;
  258.         delete [] tempPath;
  259.     }
  260.  
  261.     // If we couldn't allocate memory for tempPath then lets create the file locally.
  262.     // Lets go ahead with downloading of file.
  263.     // The file is downloaded to [temporary directory]urltmpX.htm [where X is the no of view]
  264.     localfile+="urltmp";
  265.     localfile+=count;
  266.     localfile+=".htm";
  267.     count++;
  268.  
  269.     // Do file downlaod
  270.      HRESULT hr = URLDownloadToFile(0,newName,localfile,0,dtfcb);
  271.  
  272.     // Open the local file.
  273.     CPadDoc *PPadDoc = (CPadDoc *)OpenDocumentFile(localfile);    
  274.     if (PPadDoc)
  275.     {
  276.         PPadDoc->m_urlflag=TRUE;
  277.  
  278.         CHAR seps[]   = "/\t\n";
  279.         CHAR *tokens = strtok((LPTSTR)newName.GetBuffer(_MAX_PATH), seps);
  280.         CHAR oldtoken[_MAX_PATH]; 
  281.         while (( tokens != NULL ))
  282.         {
  283.             lstrcpy(oldtoken,tokens);
  284.             tokens = strtok(NULL, seps);
  285.         }
  286.         PPadDoc->m_urlfile=oldtoken;
  287.         PPadDoc->SetTitle(oldtoken); // Sort of fooling but better than some cryptic temporary file name.
  288.     }
  289.     if (dtfcb)
  290.         delete dtfcb;
  291. }
  292.  
  293. //////////////////////////////////////////////////////////////////////////////
  294. // %%Function: CTheApp::PromptForFileName
  295. // Puts custom Template and calls the FileOpen Dialog
  296. // Return Type specifies whether
  297. // 0 : IDCANCEL pressed
  298. // 1 : Local File
  299. // 2 : URL file
  300. // ---------------------------------------------------------------------------
  301.  
  302. UINT
  303. CTheApp::PromptForFileName
  304. (
  305.     CString& fileName,        //    Buffer to return the filename
  306.     UINT nIDSTitle,            //    Title String
  307.     DWORD dwFlags,            //    Flags for FileOpen Dialog
  308.     BOOL bOpenFileDialog    // Flag specifying whether File Open or File Save
  309. )
  310. {
  311.  
  312.     CModFileDialog dlgFile(bOpenFileDialog);
  313.     CString title;
  314.     VERIFY(title.LoadString(nIDSTitle));
  315.     dlgFile.m_ofn.Flags |= dwFlags;
  316.     dlgFile.m_ofn.lpstrTitle = title;
  317.     dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
  318.     dlgFile.m_ofn.lpTemplateName = MAKEINTRESOURCE(MYFILEOPEN);
  319.     dlgFile.m_ofn.Flags &= ~OFN_EXPLORER;
  320.     dlgFile.m_ofn.hInstance=AfxGetInstanceHandle();
  321.     dlgFile.m_ofn.lpstrFilter = "*.*";
  322.     dlgFile.m_ofn.lpstrDefExt = "*.*";
  323.  
  324.  
  325.     UINT bRet =(dlgFile.DoModal() == IDOK) ? 1 : 0;
  326.     if ((dlgFile.m_urlflag==TRUE)&&(bRet!=0))
  327.         bRet=2;
  328.     fileName.ReleaseBuffer();
  329.     return bRet;
  330. }
  331.