home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / fileview / fvinit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  9.6 KB  |  350 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1996  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //      PROGRAM: FVINIT.CPP             
  9. //
  10. //      PURPOSE:  All initialization functions and some miscellany for the CFileViewer implementation.
  11. //
  12. //      PLATFORMS:      Windows 95
  13. //
  14. //      FUNCTIONS:      
  15. //   
  16. //
  17. //      SPECIAL INSTRUCTIONS: N/A
  18. //
  19. #include "fileview.h"
  20.  
  21. //
  22. //   FUNCTION: CFileViewer::Init        
  23. //
  24. //   PURPOSE: Performs any intiailization of a CFileViewer that's prone to
  25. //  failure that we also use internally before exposing the object
  26. //  outside.  
  27. //
  28. //   RETURN VALUE:
  29. //   HRESULT         NOERROR if successful, error code otherwise.
  30. //
  31. HRESULT CFileViewer::Init(void)
  32.     {
  33.     LPUNKNOWN       pIUnknown=(LPUNKNOWN)this;
  34.     WNDCLASS        wc;
  35.     HRESULT         hr;
  36.     HDC             hDC;
  37.  
  38.     //Default error code
  39.     hr=ResultFromScode(E_OUTOFMEMORY);
  40.  
  41.     // Make pIUnknown point to outer unknown if we get one.
  42.     // The interfaces allocated below are always given an IUnknown
  43.     // to which they delegate at all times.  This will be either
  44.     // the CFileViewer object's IUnknown itself or the outer
  45.     // unknown if this object is aggregated.  The interfaces
  46.     // need not know which case is being used.
  47.     if (NULL!=m_pUnkOuter)
  48.     pIUnknown=m_pUnkOuter;
  49.  
  50.     //Allocate contained interfaces.
  51.     m_pIPersistFile=new CImpIPersistFile(this, pIUnknown);
  52.  
  53.     if (NULL==m_pIPersistFile)
  54.     return hr;
  55.  
  56.     m_pIFileViewer=new CImpIFileViewer(this, pIUnknown);
  57.  
  58.     if (NULL==m_pIFileViewer)
  59.     return hr;
  60.  
  61.     m_pST=new CStringTable(m_hInst);
  62.  
  63.     if (NULL==m_pST)
  64.     return hr;
  65.  
  66.     // Go load the strings we need in CFileViewer::String.  
  67.     if (!m_pST->FInit(IDS_MIN, IDS_MAX, CCHSTRINGMAX))
  68.     return hr;
  69.  
  70.     //Register window classes that we'll need to display the file
  71.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  72.     wc.lpfnWndProc   = FileViewerFrameProc;
  73.     wc.cbClsExtra    = 0;
  74.     wc.cbWndExtra    = CBWNDEXTRAFRAME;
  75.     wc.hInstance     = m_hInst;
  76.     wc.hIcon         = LoadIcon(m_hInst, "Icon");
  77.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  78.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  79.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  80.     wc.lpszClassName = String(IDS_CLASSFRAME);
  81.  
  82.     if (!RegisterClass(&wc))
  83.     return hr;
  84.  
  85.     // MODIFY:  This is the viewport window that does the work
  86.     // of displaying the data.  Modify it to your needs.
  87.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  88.     wc.lpfnWndProc   = ViewportWndProc;
  89.     wc.cbClsExtra    = 0;
  90.     wc.cbWndExtra    = CBWNDEXTRAVIEWPORT;
  91.     wc.hInstance     = m_hInst;
  92.     wc.hIcon         = NULL;
  93.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  94.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  95.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  96.     wc.lpszClassName = String(IDS_CLASSVIEWPORT);
  97.  
  98.     if (!RegisterClass(&wc))
  99.     return hr;
  100.  
  101.     m_fClassReg=TRUE;
  102.  
  103.     // Create a default fixed pitch font for the Viewport.  We
  104.     // use Courier New here instead of say, SYSTEM_FIXED_FONT
  105.     // because we want the font sizing buttons to work normally
  106.      // when this window appears, and the system fonts do not
  107.      // scale, but Courier New does.  Default is 10 point calculated
  108.      // from 10*PIXELSPERINCH/72.
  109.     hDC=GetDC(NULL);
  110.     m_cyPPI=GetDeviceCaps(hDC, LOGPIXELSY);
  111.     ReleaseDC(NULL, hDC);
  112.  
  113.     m_hFont=CreateFont(MulDiv(-10, m_cyPPI, 72)
  114.     , 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  115.     , ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS
  116.     , DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "Courier New");
  117.  
  118.     return NOERROR;
  119.     }
  120. //
  121. //   FUNCTION: CFileViewer::FInitFrameControls  
  122. //
  123. //   PURPOSE:  Creates and initializes the toolbar and status bar for the frame window.
  124. //
  125. //   PARAMETERS: 
  126. //
  127. //   RETURN VALUE:
  128. //  BOOL            TRUE if the function worked, FALSE otherwise.
  129. //
  130. BOOL CFileViewer::FInitFrameControls(void)
  131.     {
  132.     BOOL        fRet;
  133.     RECT        rc;
  134.     TBBUTTON    rgtb[CTBBUTTONS];
  135.  
  136.      // Initialize the toolbar button array.  This uses the TOOLS.BMP
  137.      // resource which has three buttons and one separator.  The
  138.      // buttons are tied to File/Open As and Increase/Decrease Font
  139.      // size.
  140.     rgtb[0].iBitmap=IDBT_OPENAS;
  141.     rgtb[0].idCommand=IDM_FILEOPENAS;
  142.     rgtb[0].fsState=TBSTATE_ENABLED;
  143.     rgtb[0].fsStyle=TBSTYLE_BUTTON;
  144.     rgtb[0].dwData=0L;
  145.     rgtb[0].iString=0;
  146.  
  147.     rgtb[1].iBitmap=0;
  148.     rgtb[1].idCommand=0;
  149.     rgtb[1].fsState=TBSTATE_ENABLED;
  150.     rgtb[1].fsStyle=TBSTYLE_SEP;
  151.     rgtb[1].dwData=0L;
  152.     rgtb[1].iString=0;
  153.  
  154.     rgtb[2].iBitmap=IDBT_FONTSIZEINCREASE;
  155.     rgtb[2].idCommand=IDM_VIEWFONTINCREASE;
  156.     rgtb[2].fsState=TBSTATE_ENABLED;
  157.     rgtb[2].fsStyle=TBSTYLE_BUTTON;
  158.     rgtb[2].dwData=0L;
  159.     rgtb[2].iString=0;
  160.  
  161.     rgtb[3].iBitmap=IDBT_FONTSIZEDECREASE;
  162.     rgtb[3].idCommand=IDM_VIEWFONTDECREASE;
  163.     rgtb[3].fsState=TBSTATE_ENABLED;
  164.     rgtb[3].fsStyle=TBSTYLE_BUTTON;
  165.     rgtb[3].dwData=0L;
  166.     rgtb[3].iString=0;
  167.  
  168.      // BUGBUG:  Open as button:  call SHGetIconOfFile to retrieve
  169.      // the image, then overwrite it in the toolbar bitmap we send
  170.      // to CreateToolbarEx.  If SHGetIconOfFile fails, then just
  171.      // use the one in the toolbar bitmap already.
  172.      //
  173.     m_hWndToolbar=CreateToolbarEx(m_hWnd, TBSTYLE_TOOLTIPS
  174.     | WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, ID_TOOLBAR
  175.     , CTBBITMAPS, m_hInst, IDB_TOOLS, rgtb, CTBBUTTONS
  176.     , 0, 0, 0, 0, sizeof(TBBUTTON));
  177.  
  178.     if (NULL==m_hWndToolbar)
  179.     {
  180.     ODS("CFileViewer::FInitFrameTools failed to create toolbar");
  181.     return FALSE;
  182.     }
  183.  
  184.      // Remember the height for resizing this and the viewport.
  185.      // Use window rectangles for toolbars.
  186.     GetWindowRect(m_hWndToolbar, &rc);
  187.     m_cyTools=rc.bottom-rc.top;
  188.  
  189.      // Status line
  190.     m_hWndStatus=CreateStatusWindow(WS_CHILD | WS_VISIBLE
  191.     , String(IDS_MSGREADY), m_hWnd, ID_STATUSBAR);
  192.  
  193.     if (NULL==m_hWndStatus)
  194.     {
  195.     ODS("CFileViewer::FInitFrameTools failed to create status bar");
  196.     return FALSE;
  197.     }
  198.  
  199.      // Remember the height for resizing this and the viewport.
  200.      // Use client rectangle for status lines.
  201.     GetClientRect(m_hWndStatus, &rc);
  202.     m_cyStatus=rc.bottom-rc.top;
  203.  
  204.      // Create a status bar helper and tell it what messages
  205.      // to use.  This object will be called from WM_MENUSELECT
  206.      // in FileViewerFrameProc in FVPROC.CPP.
  207.     m_pSH=new CStatusHelper(m_hWndStatus, m_hInst);
  208.  
  209.     if (NULL==m_pSH)
  210.     return FALSE;
  211.  
  212.     fRet=m_pSH->MessageMap(m_hWnd, IDR_STATMESSAGEMAP, IDS_STATUSMSGMIN
  213.     , IDS_STATUSMSGMAX, CCHSTATUSMSGMAX, ID_MENUFILE, ID_MENUHELP
  214.     , ID_MSGREADY, ID_MSGEMPTY, ID_MENUSYS);
  215.  
  216.     return fRet;
  217.     }
  218.  
  219. //
  220. //   FUNCTION:  CFileViewer::String
  221. //
  222. //   PURPOSE:    string lookup function for access to stringtable. 
  223. //
  224. LPSTR CFileViewer::String(UINT uID)
  225.     {
  226.     return (*m_pST)[uID];
  227.     }
  228.  
  229. //
  230. //   FUNCTION:  CFileViewer::MemAlloc
  231. //
  232. //   PURPOSE:Central allocation function using IMalloc.   
  233. //
  234. LPVOID CFileViewer::MemAlloc(ULONG cb)
  235.     {
  236.     LPMALLOC    pIMalloc;
  237.     LPVOID      pv;
  238.     HRESULT     hr;
  239.  
  240.     if (FAILED(hr = CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  241.     {
  242.     ODSu("CFileViewer::MemAlloc CoGetMalloc failed %ld", hr);
  243.  
  244.     if (hr == CO_E_NOTINITIALIZED)
  245.         {
  246.         if ((FAILED(hr=CoInitialize(NULL))) ||
  247.             FAILED(hr=CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  248.         {
  249.         ODSu("CFileViewer::MemAlloc CoInitialize/CoGetMalloc failed %x", hr);
  250.         return NULL;
  251.         }
  252.  
  253.         }
  254.     else
  255.         return NULL;
  256.     }
  257.  
  258.     pv=pIMalloc->Alloc(cb);
  259.     pIMalloc->Release();
  260.     return pv;
  261.     }
  262. //
  263. //   FUNCTION:CFileViewer::MemFree      
  264. //
  265. //   PURPOSE:  Central free function using IMalloc. 
  266. //
  267. void CFileViewer::MemFree(LPVOID pv)
  268.     {
  269.     LPMALLOC    pIMalloc;
  270.  
  271.     if (NULL==pv)
  272.     {
  273.     ODS("CFileViewer::MemFree passed NULL pointer");
  274.     return;
  275.     }
  276.  
  277.     if (FAILED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  278.     {
  279.     ODS("CFileViewer::MemFree CoGetMalloc failed");
  280.     return;
  281.     }
  282.  
  283.     pIMalloc->Free(pv);
  284.     pIMalloc->Release();
  285.     return;
  286.     }
  287. //
  288. //   FUNCTION: CFileViewer::QueryInterface      
  289. //
  290. STDMETHODIMP CFileViewer::QueryInterface(REFIID riid, PPVOID ppv)
  291.     {
  292.     *ppv=NULL;
  293.  
  294.      //The only calls for IUnknown are either in a nonaggregated
  295.      // case or when created in an aggregation, so in either case
  296.      // always return our IUnknown for IID_IUnknown.
  297.     if (IsEqualIID(riid, IID_IUnknown))
  298.     *ppv=(LPVOID)this;
  299.  
  300.     //IPersist is base of IPersistFile
  301.     if (IsEqualIID(riid, IID_IPersist)
  302.     || IsEqualIID(riid, IID_IPersistFile))
  303.     *ppv=(LPVOID)m_pIPersistFile;
  304.  
  305.     if (IsEqualIID(riid, IID_IFileViewer))
  306.     *ppv=(LPVOID)m_pIFileViewer;
  307.  
  308.     //AddRef any interface we'll return.
  309.     if (NULL!=*ppv)
  310.     {
  311.     ((LPUNKNOWN)*ppv)->AddRef();
  312.     return NOERROR;
  313.     }
  314.  
  315.     return ResultFromScode(E_NOINTERFACE);
  316.     }
  317.  
  318. //
  319. //   FUNCTION:  CFileViewer::AddRef
  320. //
  321. STDMETHODIMP_(ULONG) CFileViewer::AddRef(void)
  322.     {
  323.     return ++m_cRef;
  324.     }
  325.  
  326. //
  327. //   FUNCTION: CFileViewer::Release
  328. //
  329. STDMETHODIMP_(ULONG) CFileViewer::Release(void)
  330.     {
  331.     ULONG       cRefT;
  332.  
  333.     cRefT=--m_cRef;
  334.  
  335.     ODSu("CFileViewer::Release Count=%d", cRefT);
  336.  
  337.     if (0L==m_cRef)
  338.     {
  339.      // Tell the server that an object is going away
  340.      // so it can shut down if appropriate.  (See FILEVIEW.CPP
  341.      // for the ObjectDestroyed function).
  342.     if (NULL!=m_pfnDestroy)
  343.         (*m_pfnDestroy)();
  344.  
  345.     delete this;
  346.     }
  347.  
  348.     return cRefT;
  349.     }
  350.