home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / prefs / edpref / src / edpref.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 KB  |  335 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "pch.h"
  20. #include "edpref.h"
  21. #include "pages.h"
  22. #include "edprefid.h"
  23. #include "prefuiid.h"
  24. #include "isppageo.h"
  25. #include <assert.h>
  26. #define NUM_EDITOR_PAGES 3
  27.  
  28. // Create a new instance of our derived class and return it.
  29. CComDll *
  30. DLL_ConsumerCreateInstance()
  31. {
  32.     return new CEditorPrefsDll;
  33. }
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // Global new/delete operators
  37.  
  38. // Override the global new/delete operators to allocate memory owned by the
  39. // application and not by the DLL; _fmalloc calls GlobalAlloc with GMEM_SHARE
  40. // for DLLs and that means the memory will not be freed until the DLL is
  41. // unloaded. _fmalloc only maintains one heap for a DLL which is shared by
  42. // all applications that use the DLL
  43. #ifndef _WIN32
  44. void *
  45. operator new (size_t size)
  46. {
  47.     return CoTaskMemAlloc(size);
  48. }
  49.  
  50. void
  51. operator delete (void *lpMem)
  52. {
  53.     CoTaskMemFree(lpMem);
  54. }
  55. #endif
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CSpecifyPropertyPageObjects
  59.  
  60. class CSpecifyPropertyPageObjects : public ISpecifyPropertyPageObjects {
  61.     public:
  62.         CSpecifyPropertyPageObjects();
  63.  
  64.         // IUnknown methods
  65.         STDMETHODIMP QueryInterface(REFIID riid, LPVOID FAR* ppvObj);
  66.         STDMETHODIMP_(ULONG) AddRef();
  67.         STDMETHODIMP_(ULONG) Release();
  68.  
  69.     private:
  70.         ULONG    m_uRef;
  71. };
  72.  
  73. CSpecifyPropertyPageObjects::CSpecifyPropertyPageObjects()
  74. {
  75.     m_uRef = 0;
  76. }
  77.  
  78. STDMETHODIMP
  79. CSpecifyPropertyPageObjects::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  80. {
  81.     *ppvObj = NULL;
  82.  
  83.     if (riid == IID_IUnknown || riid == IID_ISpecifyPropertyPageObjects)
  84.         *ppvObj = (LPVOID)this;
  85.  
  86.     if (*ppvObj) {
  87.         AddRef();
  88.         return NOERROR;
  89.     }
  90.  
  91.     return ResultFromScode(E_NOINTERFACE);
  92. }
  93.  
  94. STDMETHODIMP_(ULONG)
  95. CSpecifyPropertyPageObjects::AddRef()
  96. {
  97.     return ++m_uRef;
  98. }
  99.  
  100. STDMETHODIMP_(ULONG)
  101. CSpecifyPropertyPageObjects::Release()
  102. {
  103.     if (--m_uRef == 0) {
  104. #ifdef _DEBUG
  105.         OutputDebugString("Destroying CSpecifyPropertyPageObjects object.\n");
  106. #endif
  107.         delete this;
  108.         return 0;
  109.     }
  110.  
  111.     return m_uRef;
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CEditorCategory
  116.  
  117. class CEditorCategory : public CSpecifyPropertyPageObjects {
  118.     public:
  119.         // ISpecifyPropertyPageObjects methods
  120.         STDMETHODIMP GetPageObjects(CAPPAGE *pPages);
  121. };
  122.  
  123. STDMETHODIMP
  124. CEditorCategory::GetPageObjects(CAPPAGE *pPages)
  125. {
  126.     if (!pPages)
  127.         return ResultFromScode(E_POINTER);
  128.  
  129.     pPages->cElems = NUM_EDITOR_PAGES;
  130.     pPages->pElems = (LPPROPERTYPAGE *)CoTaskMemAlloc(pPages->cElems * sizeof(LPPROPERTYPAGE));
  131.     if (!pPages->pElems)
  132.         return ResultFromScode(E_OUTOFMEMORY);
  133.  
  134.     pPages->pElems[0] = new CEditorPrefs;
  135.     pPages->pElems[1] = new CPublishPrefs;
  136.     pPages->pElems[2] = new CEditorPrefs2;
  137.     
  138.     for (ULONG i = 0; i < pPages->cElems; i++)
  139.         pPages->pElems[i]->AddRef();
  140.     
  141.     return NOERROR;
  142. }
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. // Class CPropertyPageFactory
  146.  
  147. // Class factory for our property pages. We use the same C++ class
  148. // to handle all of our CLSIDs
  149. class CPropertyPageFactory : public IClassFactory {
  150.     public:
  151.         CPropertyPageFactory(REFCLSID rClsid);
  152.  
  153.         // *** IUnknown methods ***
  154.         STDMETHODIMP             QueryInterface(REFIID, LPVOID FAR*);
  155.         STDMETHODIMP_(ULONG)     AddRef();
  156.         STDMETHODIMP_(ULONG)     Release();
  157.      
  158.         // *** IClassFactory methods ***
  159.         STDMETHODIMP             CreateInstance(LPUNKNOWN, REFIID, LPVOID FAR*);
  160.         STDMETHODIMP             LockServer(BOOL bLock);
  161.  
  162.     private:
  163.         CRefDll    m_refDll;
  164.         ULONG   m_uRef;
  165.         CLSID    m_clsid;
  166. };
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CPropertyPageFactory implementation
  170.  
  171. CPropertyPageFactory::CPropertyPageFactory(REFCLSID rClsid)
  172. {
  173.     m_uRef = 0;
  174.     m_clsid = rClsid;
  175. }
  176.  
  177. // *** IUnknown methods ***
  178. STDMETHODIMP CPropertyPageFactory::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  179. {
  180.     *ppvObj = NULL;
  181.  
  182.     if (riid == IID_IUnknown || riid == IID_IClassFactory)
  183.         *ppvObj = (LPVOID)this;
  184.  
  185.     if (*ppvObj) {
  186.         AddRef();
  187.         return NOERROR;
  188.     }
  189.  
  190.     return ResultFromScode(E_NOINTERFACE);
  191. }
  192.  
  193.  
  194. STDMETHODIMP_(ULONG) CPropertyPageFactory::AddRef()
  195. {
  196.     return ++m_uRef;
  197. }
  198.  
  199.  
  200. STDMETHODIMP_(ULONG) CPropertyPageFactory::Release(void)
  201. {
  202.     if (--m_uRef == 0) {
  203. #ifdef _DEBUG
  204.         OutputDebugString("Destroying CPropertyPageFactory class object.\n");
  205. #endif
  206.            delete this;
  207.         return 0;
  208.     }
  209.  
  210.     return m_uRef;
  211. }
  212.  
  213.  
  214. // *** IClassFactory methods ***
  215. STDMETHODIMP CPropertyPageFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID FAR* ppvObj)
  216. {
  217.     // We do not support aggregation
  218.     if (pUnkOuter)
  219.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  220.  
  221. #ifdef _DEBUG
  222.     OutputDebugString("CPropertyPageFactory::CreateInstance() called.\n");
  223. #endif
  224.     LPSPECIFYPROPERTYPAGEOBJECTS pCategory;
  225.      
  226.     if (m_clsid == CLSID_EditorPrefs)
  227.         pCategory = new CEditorCategory;
  228.  
  229.     if (!pCategory)
  230.         return ResultFromScode(E_OUTOFMEMORY);
  231.  
  232.     pCategory->AddRef();
  233.     HRESULT    hRes = pCategory->QueryInterface(riid, ppvObj);
  234.     pCategory->Release();
  235.     return hRes;
  236. }
  237.  
  238.  
  239. STDMETHODIMP CPropertyPageFactory::LockServer(BOOL bLock)
  240. {
  241.     CComDll    *pDll = CProcess::GetProcessDll();
  242.     HRESULT     hres;
  243.  
  244.     assert(pDll);
  245.     hres = CoLockObjectExternal(pDll, bLock, TRUE);
  246.     pDll->Release();
  247.     return hres;
  248. }
  249.  
  250. /////////////////////////////////////////////////////////////////////////////
  251. // CEditorPrefsDll implementation
  252.  
  253. HRESULT
  254. CEditorPrefsDll::GetClassObject(REFCLSID rClsid, REFIID riid, LPVOID *ppObj)
  255. {
  256.     HRESULT hres = ResultFromScode(E_UNEXPECTED);
  257.     *ppObj = NULL;
  258.  
  259. #ifdef _DEBUG
  260.     OutputDebugString("CEditorPrefsDll::GetClassObject() called.\n");
  261. #endif
  262.  
  263.     // See if we have that particular class object.
  264.     if (rClsid == CLSID_EditorPrefs) {
  265.         // Create a class object
  266.         CPropertyPageFactory *pFactory = new CPropertyPageFactory(rClsid);
  267.  
  268.         if (!pFactory)
  269.             return ResultFromScode(E_OUTOFMEMORY);
  270.             
  271.         // Get the desired interface. Note if the QueryInterface fails, the Release
  272.         // will delete the class object
  273.         pFactory->AddRef();
  274.         hres = pFactory->QueryInterface(riid, ppObj);
  275.         pFactory->Release(); 
  276.  
  277.     } else {
  278.         hres = ResultFromScode(CLASS_E_CLASSNOTAVAILABLE);
  279.     }
  280.  
  281.     return hres;
  282. }
  283.  
  284. // Return array of implemented CLSIDs by this DLL. Allocated
  285. // memory freed by caller.
  286. const CLSID **
  287. CEditorPrefsDll::GetCLSIDs()
  288. {
  289.     const CLSID **ppRetval = (const CLSID **)CoTaskMemAlloc(sizeof(CLSID *) * 2);
  290.  
  291.     if (ppRetval) {
  292.         ppRetval[0] = &CLSID_EditorPrefs;
  293.         ppRetval[1] = NULL;
  294.     }
  295.  
  296.     return ppRetval;
  297. }
  298.  
  299. #ifdef _WIN32
  300. BOOL WINAPI
  301. DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
  302. {
  303.     switch (fdwReason) {
  304.         case DLL_PROCESS_ATTACH:
  305.             // The DLL is being loaded for the first time by a given process
  306.             CComDll::m_hInstance = hInstance;
  307.             break;
  308.  
  309.         case DLL_PROCESS_DETACH:
  310.             // The DLL is being unloaded by a given process
  311.             break;
  312.  
  313.         case DLL_THREAD_ATTACH:
  314.             // A thread is being created in a process that has already loaded
  315.             // this DLL
  316.             break;
  317.  
  318.         case DLL_THREAD_DETACH:
  319.             // A thread is exiting cleanly in a process that has already
  320.             // loaded this DLL
  321.             break;
  322.     }
  323.  
  324.     return TRUE;
  325. }
  326. #else
  327. extern "C" int CALLBACK
  328. LibMain(HINSTANCE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR)
  329. {
  330.     CComDll::m_hInstance = hInstance;
  331.     return TRUE;
  332. }
  333. #endif
  334.  
  335.