home *** CD-ROM | disk | FTP | other *** search
- /*++
-
- (C) Copyright 1995 - 1999 Microsoft Corporation. All rights reserved.
-
- Module Name:
-
- AppletHoster.cpp
-
- Abstract:
-
- Demonstrates the use of the ActiveX Browser Applet Control. This is the
- same control used by Microsoft Internet Explorer to handle the <applet>
- tag. This control was added in the SDK 2.0 / Internet Explorer 4.0
- Microsoft virtual machine.
-
- --*/
-
- #include <windows.h>
- #include <olectl.h>
- #ifndef __IPropertyBag2_INTERFACE_DEFINED__
- // propbag2.h is automatically included in the latest ocidl.h from the
- // Microsoft Platform SDK.
- #include <propbag2.h>
- #endif
- #include <initguid.h>
- #include <javaax.h>
-
- #if _MSC_VER >= 1100
- #define IID_IPropertyBag2 __uuidof(IPropertyBag2)
- #define IID_IPersistPropertyBag2 __uuidof(IPersistPropertyBag2)
- #endif
-
- #define ARRAY_ELEMENTS(rg) (sizeof(rg) / sizeof((rg)[0]))
-
- HINSTANCE g_hInstance = NULL;
-
- HRESULT CoDuplicateString(LPCOLESTR pwszSource, LPOLESTR *ppwszDestination)
- {
- HRESULT hr;
- int length;
- LPOLESTR pwszDestination;
-
- length = wcslen(pwszSource);
- pwszDestination = (LPOLESTR) CoTaskMemAlloc((length + 1) * sizeof(OLECHAR));
-
- if (pwszDestination != NULL) {
- CopyMemory(pwszDestination, pwszSource, length * sizeof(OLECHAR));
- pwszDestination[length] = L'\0';
- hr = S_OK;
- } else {
- hr = E_OUTOFMEMORY;
- }
-
- *ppwszDestination = pwszDestination;
-
- return hr;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // CPropertyBag2
-
- struct NAME_VALUE_PAIR {
- LPOLESTR pwszName;
- LPOLESTR pwszValue;
- };
-
- class CPropertyBag2: public IPropertyBag2
- {
- private:
- static NAME_VALUE_PAIR s_HardcodedParameters[];
-
- ULONG m_cRef;
-
- public:
- CPropertyBag2() {
- m_cRef = 1;
- }
-
- // IUnknown methods
-
- STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppvObject);
- STDMETHODIMP_(ULONG) AddRef(void);
- STDMETHODIMP_(ULONG) Release(void);
-
- // IPropertyBag2 methods
-
- STDMETHODIMP Read(ULONG cProperties, PROPBAG2 *pPropBag, IErrorLog *pErrLog,
- VARIANT *pvarValue, HRESULT *phrError);
- STDMETHODIMP Write(ULONG cProperties, PROPBAG2 *pPropBag, VARIANT *pvarValue);
- STDMETHODIMP CountProperties(ULONG *pcProperties);
- STDMETHODIMP GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2
- *pPropBag, ULONG *pcProperties);
- STDMETHODIMP LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown
- *pUnkObject, IErrorLog *pErrLog);
- };
-
- NAME_VALUE_PAIR CPropertyBag2::s_HardcodedParameters[] = {
- { L"codebase", L"." },
- { L"code", L"SampleApplet" },
- { L"SomeParameter", L"Some Random Text To Paint" },
- };
-
- STDMETHODIMP CPropertyBag2::QueryInterface(REFIID riid, LPVOID *ppvObject)
- {
- HRESULT hr;
-
- if (riid == IID_IUnknown || riid == IID_IPropertyBag2) {
- *ppvObject = (IPropertyBag2 *) this;
- AddRef();
- hr = S_OK;
- } else {
- *ppvObject = NULL;
- hr = E_NOINTERFACE;
- }
-
- return hr;
- }
-
- STDMETHODIMP_(ULONG) CPropertyBag2::AddRef(void)
- {
- m_cRef++;
-
- return m_cRef;
- }
-
- STDMETHODIMP_(ULONG) CPropertyBag2::Release(void)
- {
- m_cRef--;
-
- if (m_cRef == 0) {
- delete this;
- return 0;
- }
-
- return m_cRef;
- }
-
- STDMETHODIMP CPropertyBag2::Read(ULONG cProperties, PROPBAG2 *pPropBag,
- IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
- {
- HRESULT hr;
- BOOL fFail;
- ULONG current;
- ULONG match;
- VARIANT varLocal;
-
- fFail = FALSE;
-
- for (current = 0; current < cProperties; current++) {
-
- hr = E_FAIL;
-
- VariantClear(pvarValue);
-
- for (match = 0; match < ARRAY_ELEMENTS(s_HardcodedParameters); match++) {
-
- if (_wcsicmp(pPropBag->pstrName,
- s_HardcodedParameters[match].pwszName) == 0) {
-
- varLocal.vt = VT_BSTR;
- varLocal.bstrVal =
- ::SysAllocString(s_HardcodedParameters[match].pwszValue);
-
- if (varLocal.bstrVal != NULL) {
-
- // If the caller wanted a BSTR, then just transfer
- // ownership of the string to them (saves duplicating the
- // string).
- if (pPropBag->vt == VT_BSTR) {
- *pvarValue = varLocal;
- hr = S_OK;
- } else {
- hr = VariantChangeTypeEx(pvarValue, &varLocal,
- LOCALE_USER_DEFAULT, 0, pPropBag->vt);
- VariantClear(&varLocal);
- }
-
- } else {
- hr = E_OUTOFMEMORY;
- }
-
- break;
- }
- }
-
- if (hr != S_OK)
- fFail = TRUE;
-
- if (phrError != NULL) {
- *phrError = hr;
- phrError++;
- }
- }
-
- return fFail ? E_FAIL : S_OK;
- }
-
- STDMETHODIMP CPropertyBag2::Write(ULONG cProperties, PROPBAG2 *pPropBag, VARIANT
- *pvarValue)
- {
- // The applet control does support IPersistPropertyBag2::Save in which case
- // it will invoke this method, but since we aren't saving anything, we'll
- // skip the implementation.
- //
- // If you were to use IPersistPropertyBag2::Save, then the applet control
- // calls this method with:
- // pPropBag: .dwType = PROPBAG2_TYPE_DATA, .vt = VT_BSTR,
- // .pstrName = the parameter name.
- // pvarValue: vt = VT_BSTR, bstrVal = the parameter value.
- //
- // The BSTRs are owned by the applet control and are only valid during this
- // method execution.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CPropertyBag2::CountProperties(ULONG *pcProperties)
- {
- // The applet control will invoke this method to find out how many
- // properties to read from the list.
- *pcProperties = ARRAY_ELEMENTS(s_HardcodedParameters);
-
- return S_OK;
- }
-
- STDMETHODIMP CPropertyBag2::GetPropertyInfo(ULONG iProperty, ULONG cProperties,
- PROPBAG2 *pPropBag, ULONG *pcProperties)
- {
- HRESULT hr;
- ULONG current;
-
- hr = S_OK;
-
- for (current = iProperty; current < ARRAY_ELEMENTS(s_HardcodedParameters) &&
- current < iProperty + cProperties; current++) {
-
- // We return a data element of type VT_BSTR. The hint can be used in
- // IPropertyBag2::Read to find the requested name more quickly, but is
- // optional.
- pPropBag->dwType = PROPBAG2_TYPE_DATA;
- pPropBag->vt = VT_BSTR;
- pPropBag->cfType = CF_TEXT;
- pPropBag->dwHint = current;
-
- hr = CoDuplicateString(s_HardcodedParameters[current].pwszName,
- &(pPropBag->pstrName));
-
- if (hr != S_OK)
- break;
-
- pPropBag++;
- }
-
- if (pcProperties != NULL)
- *pcProperties = current - iProperty;
-
- return hr;
- }
-
- STDMETHODIMP CPropertyBag2::LoadObject(LPCOLESTR pstrName, DWORD dwHint,
- IUnknown *pUnkObject, IErrorLog *pErrLog)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- // CAppletHostFrame
-
- #define DISPID_BASEHREF 0x00000100
-
- class CAppletHostFrame: public IOleClientSite,
- public IOleControlSite,
- public IDispatch,
- public IOleInPlaceSite,
- public IOleInPlaceFrame,
- public IPropertyNotifySink
- {
- private:
- static const LPCSTR s_pszWndClassName;
-
- ULONG m_cRef;
- HWND m_hwnd;
- IOleObject *m_pOleObject;
- IConnectionPoint *m_pPropNotifyConnectionPoint;
- DWORD m_dwPropNotifyConnectionCookie;
-
- BSTR GetDocumentBase();
- void ResizeApplet();
- void CloseApplet();
-
- public:
- static HRESULT InitializeClass(void);
-
- static LRESULT WINAPI WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
- lParam);
-
- CAppletHostFrame() {
- m_cRef = 1;
- m_hwnd = NULL;
- m_pOleObject = NULL;
- m_pPropNotifyConnectionPoint = NULL;
- }
-
- ~CAppletHostFrame() {
- if (m_pPropNotifyConnectionPoint != NULL)
- m_pPropNotifyConnectionPoint->Release();
- if (m_pOleObject != NULL)
- m_pOleObject->Release();
- }
-
- HRESULT Initialize(int nShowCmd);
- void DemonstrateIDispatch(void);
-
- // IUnknown methods
-
- STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppvObject);
- STDMETHODIMP_(ULONG) AddRef(void);
- STDMETHODIMP_(ULONG) Release(void);
-
- // IOleClientSite methods
-
- STDMETHODIMP SaveObject(void);
- STDMETHODIMP GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker
- **ppmk);
- STDMETHODIMP GetContainer(IOleContainer **ppContainer);
- STDMETHODIMP ShowObject(void);
- STDMETHODIMP OnShowWindow(BOOL fShow);
- STDMETHODIMP RequestNewObjectLayout(void);
-
- // IOleControlSite methods
-
- STDMETHODIMP OnControlInfoChanged(void);
- STDMETHODIMP LockInPlaceActive(BOOL fLock);
- STDMETHODIMP GetExtendedControl(IDispatch **ppDisp);
- STDMETHODIMP TransformCoords(POINTL *pPtlHimetric, POINTF *pPtfContainer,
- DWORD dwFlags);
- STDMETHODIMP TranslateAccelerator(MSG *pMsg, DWORD grfModifiers);
- STDMETHODIMP OnFocus(BOOL fGotFocus);
- STDMETHODIMP ShowPropertyFrame(void);
-
- // IDispatch methods
-
- STDMETHODIMP GetTypeInfoCount(UINT *pctinfo);
- STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
- STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames,
- LCID lcid, DISPID *rgDispId);
- STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD
- wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO
- *pExcepInfo, UINT *puArgErr);
-
- // IOleInPlaceSite methods
-
- STDMETHODIMP CanInPlaceActivate(void);
- STDMETHODIMP OnInPlaceActivate(void);
- STDMETHODIMP OnUIActivate(void);
- STDMETHODIMP GetWindowContext(IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow
- **ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO
- lpFrameInfo);
- STDMETHODIMP Scroll(SIZE scrollExtant);
- STDMETHODIMP OnUIDeactivate(BOOL fUndoable);
- STDMETHODIMP OnInPlaceDeactivate(void);
- STDMETHODIMP DiscardUndoState(void);
- STDMETHODIMP DeactivateAndUndo(void);
- STDMETHODIMP OnPosRectChange(LPCRECT lprcPosRect);
-
- // IOleWindow methods
-
- STDMETHODIMP GetWindow(HWND *phwnd);
- STDMETHODIMP ContextSensitiveHelp(BOOL fEnterMode);
-
- // IOleInPlaceUIWindow methods
-
- STDMETHODIMP GetBorder(LPRECT lprectBorder);
- STDMETHODIMP RequestBorderSpace(LPCBORDERWIDTHS pborderwidths);
- STDMETHODIMP SetBorderSpace(LPCBORDERWIDTHS pborderwidths);
- STDMETHODIMP SetActiveObject(IOleInPlaceActiveObject *pActiveObject,
- LPCOLESTR pszObjName);
-
- // IOleInPlaceFrame methods
-
- STDMETHODIMP InsertMenus(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS
- lpMenuWidths);
- STDMETHODIMP SetMenu(HMENU hmenuShared, HOLEMENU holemenu, HWND
- hwndActiveObject);
- STDMETHODIMP RemoveMenus(HMENU hmenuShared);
- STDMETHODIMP SetStatusText(LPCOLESTR pszStatusText);
- STDMETHODIMP EnableModeless(BOOL fEnable);
- STDMETHODIMP TranslateAccelerator(LPMSG lpmsg, WORD wID);
-
- // IPropertyNotifySink methods
-
- STDMETHODIMP OnChanged(DISPID dispid);
- STDMETHODIMP OnRequestEdit(DISPID dispid);
- };
-
- const LPCSTR CAppletHostFrame::s_pszWndClassName = "CAppletHostFrame";
-
- //
- // CAppletHostFrame::InitializeClass
- //
- // Performs any initialization required by this class type.
- //
-
- HRESULT CAppletHostFrame::InitializeClass(void)
- {
- WNDCLASS wc;
-
- ZeroMemory(&wc, sizeof(wc));
- wc.lpfnWndProc = CAppletHostFrame::WndProc;
- wc.hInstance = g_hInstance;
- wc.lpszClassName = s_pszWndClassName;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
-
- return RegisterClass(&wc) != 0 ? S_OK : HRESULT_FROM_WIN32(GetLastError());
- }
-
- //
- // CAppletHostFrame::WndProc
- //
-
- LRESULT WINAPI CAppletHostFrame::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam,
- LPARAM lParam)
- {
- CAppletHostFrame *pFrame;
-
- if (uMsg == WM_NCCREATE) {
- pFrame = (CAppletHostFrame *) ((LPCREATESTRUCT) lParam)->
- lpCreateParams;
- ::SetWindowLong(hwnd, GWL_USERDATA, (LONG) pFrame);
- } else {
- pFrame = (CAppletHostFrame *) ::GetWindowLong(hwnd, GWL_USERDATA);
- }
-
- switch (uMsg) {
-
- case WM_SIZE:
- pFrame->ResizeApplet();
- break;
-
- case WM_DESTROY:
- pFrame->CloseApplet();
- ::PostQuitMessage(0);
- break;
-
- default:
- return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
-
- return 0;
- }
-
- HRESULT CAppletHostFrame::Initialize(int nShowCmd)
- {
- HRESULT hr;
- CPropertyBag2 *pParameters;
- IConnectionPointContainer *pcpc;
- IPersistPropertyBag2 *ppbag;
- RECT rcClient;
-
- hr = ::CoCreateInstance(CLSID_BrowserAppletControl, NULL,
- CLSCTX_INPROC_SERVER, IID_IOleObject, (LPVOID *) &m_pOleObject);
-
- if (hr == S_OK) {
-
- m_hwnd = ::CreateWindowEx(0, s_pszWndClassName, "", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
- NULL, g_hInstance, (LPVOID) this);
-
- if (m_hwnd != NULL) {
-
- hr = m_pOleObject->SetClientSite(this);
-
- if (hr == S_OK) {
-
- // Connect to the applet's IPropertyNotifySink events. The
- // applet will fire a DISPID_READYSTATE property change
- // notification when the applet has fully loaded and is
- // available for use.
- if (m_pOleObject->QueryInterface(IID_IConnectionPointContainer,
- (LPVOID *) &pcpc) == S_OK) {
-
- if (pcpc->FindConnectionPoint(IID_IPropertyNotifySink,
- &m_pPropNotifyConnectionPoint) == S_OK) {
-
- if (m_pPropNotifyConnectionPoint->Advise(
- (IPropertyNotifySink *) this,
- &m_dwPropNotifyConnectionCookie) != S_OK) {
- m_pPropNotifyConnectionPoint->Release();
- m_pPropNotifyConnectionPoint = NULL;
- }
- }
-
- pcpc->Release();
- }
-
- hr = m_pOleObject->QueryInterface(IID_IPersistPropertyBag2,
- (LPVOID *) &ppbag);
-
- if (hr == S_OK) {
-
- pParameters = new CPropertyBag2;
-
- if (pParameters != NULL) {
-
- hr = ppbag->Load(pParameters, NULL);
- pParameters->Release();
-
- if (hr == S_OK) {
-
- ::GetClientRect(m_hwnd, &rcClient);
-
- hr = m_pOleObject->DoVerb(OLEIVERB_UIACTIVATE, NULL,
- this, 0, m_hwnd, &rcClient);
- }
-
- } else {
- hr = E_OUTOFMEMORY;
- }
-
- ppbag->Release();
- }
- }
-
- ::ShowWindow(m_hwnd, nShowCmd);
- ::UpdateWindow(m_hwnd);
-
- } else {
- hr = HRESULT_FROM_WIN32(GetLastError());
- }
-
- if (hr != S_OK)
- CloseApplet();
- }
-
- return hr;
- }
-
- //
- // CAppletHostFrame::DemonstrateIDispatch
- //
- // Demonstrates the use of IDispatch to access the scripting interface of the
- // hosted applet.
- //
-
- void CAppletHostFrame::DemonstrateIDispatch(void)
- {
- IDispatch *pDispatch;
- LPOLESTR rgszNames[1];
- DISPID dispid;
- DISPPARAMS dispparams;
- VARIANT var;
- DISPID dispidPropertyPut;
- VARIANT varResult;
-
- if (m_pOleObject != NULL) {
-
- if (m_pOleObject->QueryInterface(IID_IDispatch, (LPVOID *)
- &pDispatch) == S_OK) {
-
- VariantInit(&var);
- var.vt = VT_BSTR;
- var.bstrVal = SysAllocString(L"Some More Random Text");
-
- if (var.bstrVal != NULL) {
-
- rgszNames[0] = L"somestring";
-
- // An example of doing a "property put".
- if (pDispatch->GetIDsOfNames(IID_NULL, rgszNames,
- ARRAY_ELEMENTS(rgszNames), LOCALE_USER_DEFAULT, &dispid) ==
- S_OK) {
-
- dispidPropertyPut = DISPID_PROPERTYPUT;
-
- dispparams.rgvarg = &var;
- dispparams.rgdispidNamedArgs = &dispidPropertyPut;
- dispparams.cArgs = 1;
- dispparams.cNamedArgs = 1;
-
- pDispatch->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
- DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
- }
-
- rgszNames[0] = L"somemethod";
-
- // An example of doing an "invoke".
- if (pDispatch->GetIDsOfNames(IID_NULL, rgszNames,
- ARRAY_ELEMENTS(rgszNames), LOCALE_USER_DEFAULT, &dispid) ==
- S_OK) {
-
- dispparams.rgvarg = &var;
- dispparams.rgdispidNamedArgs = NULL;
- dispparams.cArgs = 1;
- dispparams.cNamedArgs = 0;
-
- pDispatch->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
- DISPATCH_METHOD, &dispparams, NULL, NULL, NULL);
- }
- }
-
- VariantClear(&var);
-
- pDispatch->Release();
- }
- }
- }
-
- //
- // CAppletHostFrame::GetDocumentBase
- //
- // Returns the "document base" for the applet. We'll use the current working
- // directory.
- //
-
- BSTR CAppletHostFrame::GetDocumentBase(void)
- {
- CHAR szCurrentDirectory[MAX_PATH];
- CHAR szDocumentBaseUrl[MAX_PATH+30];
- WCHAR wszDocumentBaseUrl[MAX_PATH+30];
-
- GetCurrentDirectory(ARRAY_ELEMENTS(szCurrentDirectory), szCurrentDirectory);
- lstrcpy(szDocumentBaseUrl, "file://");
- lstrcat(szDocumentBaseUrl, szCurrentDirectory);
- lstrcat(szDocumentBaseUrl, "/");
-
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szDocumentBaseUrl, -1,
- wszDocumentBaseUrl, ARRAY_ELEMENTS(wszDocumentBaseUrl));
-
- return SysAllocString(wszDocumentBaseUrl);
- }
-
- //
- // CAppletHostFrame::ResizeApplet
- //
- // Notifies the contained applet control that its bounding rectangle has
- // changed.
- //
-
- void CAppletHostFrame::ResizeApplet(void)
- {
- RECT rcClient;
- IOleInPlaceObject *pOleInPlaceObject;
-
- if (m_pOleObject != NULL) {
-
- if (m_pOleObject->QueryInterface(IID_IOleInPlaceObject, (LPVOID *)
- &pOleInPlaceObject) == S_OK) {
-
- ::GetClientRect(m_hwnd, &rcClient);
- pOleInPlaceObject->SetObjectRects(&rcClient, &rcClient);
- pOleInPlaceObject->Release();
- }
- }
- }
-
- //
- // CAppletHostFrame::CloseApplet
- //
- // Deactivates and close the contained applet control.
- //
-
- void CAppletHostFrame::CloseApplet(void)
- {
- if (m_pPropNotifyConnectionPoint != NULL) {
-
- m_pPropNotifyConnectionPoint->Unadvise(m_dwPropNotifyConnectionCookie);
- m_pPropNotifyConnectionPoint->Release();
- m_pPropNotifyConnectionPoint = NULL;
- }
-
- if (m_pOleObject != NULL) {
-
- m_pOleObject->Close(OLECLOSE_NOSAVE);
- m_pOleObject->Release();
- m_pOleObject = NULL;
- }
- }
-
- STDMETHODIMP CAppletHostFrame::QueryInterface(REFIID riid, LPVOID *ppvObject)
- {
- HRESULT hr;
-
- if (riid == IID_IUnknown || riid == IID_IOleClientSite) {
- *ppvObject = (LPVOID) (IOleClientSite *) this;
- hr = S_OK;
- } else if (riid == IID_IOleControlSite) {
- *ppvObject = (LPVOID) (IOleControlSite *) this;
- hr = S_OK;
- } else if (riid == IID_IDispatch) {
- *ppvObject = (LPVOID) (IDispatch *) this;
- hr = S_OK;
- } else if (riid == IID_IOleInPlaceSite) {
- *ppvObject = (LPVOID) (IOleInPlaceSite *) this;
- hr = S_OK;
- } else if (riid == IID_IOleWindow || riid == IID_IOleInPlaceUIWindow ||
- riid == IID_IOleInPlaceFrame) {
- *ppvObject = (LPVOID) (IOleInPlaceFrame *) this;
- hr = S_OK;
- } else if (riid == IID_IPropertyNotifySink) {
- *ppvObject = (LPVOID) (IPropertyNotifySink *) this;
- hr = S_OK;
- } else {
- *ppvObject = NULL;
- hr = E_NOINTERFACE;
- }
-
- if (hr == S_OK)
- AddRef();
-
- return hr;
- }
-
- STDMETHODIMP_(ULONG) CAppletHostFrame::AddRef(void)
- {
- m_cRef++;
-
- return m_cRef;
- }
-
- STDMETHODIMP_(ULONG) CAppletHostFrame::Release(void)
- {
- m_cRef--;
-
- if (m_cRef == 0) {
- delete this;
- return 0;
- }
-
- return m_cRef;
- }
-
- STDMETHODIMP CAppletHostFrame::SaveObject(void)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker
- **ppmk)
- {
- // The applet control doesn't currently invoke this method.
- *ppmk = NULL;
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetContainer(IOleContainer **ppContainer)
- {
- // The applet control doesn't currently invoke this method.
- *ppContainer = NULL;
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::ShowObject(void)
- {
- // The applet control doesn't currently invoke this method.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::OnShowWindow(BOOL fShow)
- {
- // The applet control doesn't currently invoke this method.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::RequestNewObjectLayout(void)
- {
- // The applet control will invoke this method when running as an <OBJECT>
- // tag which uses a different binding mechanism not covered in this
- // example.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::OnControlInfoChanged(void)
- {
- // Ignore the notification.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::LockInPlaceActive(BOOL fLock)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetExtendedControl(IDispatch **ppDisp)
- {
- // The extended control is implemented directly on this object.
- return this->QueryInterface(IID_IDispatch, (LPVOID *) ppDisp);
- }
-
- STDMETHODIMP CAppletHostFrame::TransformCoords(POINTL *pPtlHimetric, POINTF
- *pPtfContainer, DWORD dwFlags)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::TranslateAccelerator(MSG *pMsg, DWORD grfModifiers)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::OnFocus(BOOL fGotFocus)
- {
- // Ignore the notification.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::ShowPropertyFrame(void)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetTypeInfoCount(UINT *pctinfo)
- {
- // The applet control doesn't currently invoke this method.
- *pctinfo = 0;
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo
- **ppTInfo)
- {
- // The applet control doesn't currently invoke this method.
- *ppTInfo = NULL;
- return E_FAIL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames,
- UINT cNames, LCID lcid, DISPID *rgDispId)
- {
- HRESULT hr;
- UINT i;
-
- if (riid == IID_NULL) {
-
- for (i = 0; i < cNames; i++)
- rgDispId[i] = DISPID_UNKNOWN;
-
- // The applet control will ask for "BaseHref" in order to determine the
- // URL of the document that's hosting the applet control. The control
- // will call IDispatch::Invoke (below) with this dispid.
- if (_wcsicmp(rgszNames[0], L"BaseHref") == 0) {
- rgDispId[0] = DISPID_BASEHREF;
- hr = S_OK;
- } else {
- hr = DISP_E_UNKNOWNNAME;
- }
-
- // If we were asked for parameter names, too, then these are definitely
- // not known.
- if (cNames > 1)
- hr = DISP_E_UNKNOWNNAME;
-
- } else {
- hr = DISP_E_UNKNOWNINTERFACE;
- }
-
- return hr;
- }
-
- STDMETHODIMP CAppletHostFrame::Invoke(DISPID dispIdMember, REFIID riid, LCID
- lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO
- *pExcepInfo, UINT *puArgErr)
- {
- HRESULT hr;
-
- if (riid == IID_NULL) {
-
- switch (dispIdMember) {
-
- // Return the document URL to the applet control. It's valid to
- // not support this dispid if a document URL doesn't make any sense
- // in the context of the container. This document URL is used as
- // the default codebase if an explicit codebase is not specified
- // in the property bag. Also, the document URL is returned to the
- // applet through getDocumentBase().
- case DISPID_BASEHREF:
- if (pVarResult == NULL || pDispParams == NULL) {
- hr = E_INVALIDARG;
- } else if (!(wFlags & DISPATCH_PROPERTYGET)) {
- hr = DISP_E_MEMBERNOTFOUND;
- } else {
- pVarResult->vt = VT_BSTR;
- pVarResult->bstrVal = GetDocumentBase();
- hr = S_OK;
- }
- break;
-
- // The applet control will do a property get for the locale id.
- // The locale id controls the locale of the applet (as returned
- // by Component.getLocale). This dispid is optional: if the site
- // does not support this property, then the system default locale
- // id is used.
- #if 0
- case DISPID_AMBIENT_LOCALEID:
- if (pVarResult == NULL || pDispParams == NULL) {
- hr = E_INVALIDARG;
- } else if (!(wFlags & DISPATCH_PROPERTYGET)) {
- hr = DISP_E_MEMBERNOTFOUND;
- } else {
- pVarResult->vt = VT_I4;
- pVarResult->lVal = ::GetSystemDefaultLCID();
- hr = S_OK;
- }
- break;
- #endif
-
- default:
- hr = DISP_E_MEMBERNOTFOUND;
- break;
- }
-
- } else {
- hr = DISP_E_UNKNOWNINTERFACE;
- }
-
- return hr;
- }
-
- STDMETHODIMP CAppletHostFrame::CanInPlaceActivate(void)
- {
- // The applet control will invoke this method before moving into the
- // "inplace active" state to check if the ActiveX site will allow
- // activation. We'll allow activation.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::OnInPlaceActivate(void)
- {
- // The applet control will invoke this method after the control has
- // formally transitioned to the "inplace active" state (after this site has
- // called IOleObject::DoVerb(OLEIVERB_INPLACEACTIVATE)).
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::OnUIActivate(void)
- {
- // The applet control will invoke this method after the control has
- // formally transitioned to the "UI active" state (after this site has
- // called IOleObject::DoVerb(OLEIVERB_UIACTIVATE)).
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::GetWindowContext(IOleInPlaceFrame **ppFrame,
- IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect,
- LPOLEINPLACEFRAMEINFO lpFrameInfo)
- {
- RECT rcClient;
-
- // For simplicity, the frame, document, and site windows are all
- // implemented on the same object. These could be different objects. For
- // example, a single frame and window object may contain several site
- // objects each of which is hosting a single instance of the applet
- // control. Such would be the case in something like Internet Explorer.
- if (ppFrame != NULL) {
- *ppFrame = this;
- AddRef();
- }
-
- if (ppDoc != NULL) {
- *ppDoc = this;
- AddRef();
- }
-
- ::GetClientRect(m_hwnd, &rcClient);
-
- if (lprcPosRect != NULL)
- ::CopyRect(lprcPosRect, &rcClient);
-
- if (lprcClipRect != NULL)
- ::CopyRect(lprcClipRect, &rcClient);
-
- // The applet control doesn't currently use any of the values in this
- // structure, but fill it in for possible future use.
- lpFrameInfo->fMDIApp = FALSE;
- lpFrameInfo->hwndFrame = m_hwnd;
- lpFrameInfo->haccel = NULL;
- lpFrameInfo->cAccelEntries = 0;
-
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::Scroll(SIZE scrollExtant)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::OnUIDeactivate(BOOL fUndoable)
- {
- // Ignore the notification.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::OnInPlaceDeactivate(void)
- {
- // Ignore the notification.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::DiscardUndoState(void)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::DeactivateAndUndo(void)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::OnPosRectChange(LPCRECT lprcPosRect)
- {
- // Ignore the notification.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::GetWindow(HWND *phwnd)
- {
- // Return our window to the applet control. The applet control's window
- // will be a child window of this window.
- *phwnd = m_hwnd;
-
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::ContextSensitiveHelp(BOOL fEnterMode)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::GetBorder(LPRECT lprectBorder)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::RequestBorderSpace(LPCBORDERWIDTHS pborderwidths)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::SetBorderSpace(LPCBORDERWIDTHS pborderwidths)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::SetActiveObject(IOleInPlaceActiveObject
- *pActiveObject, LPCOLESTR pszObjName)
- {
- // The applet control will invoke this when it goes into the UI active
- // state. An ActiveX contianer would typically save this object in order
- // to forward any accelerator keystrokes or to track tab order.
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::InsertMenus(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS
- lpMenuWidths)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::SetMenu(HMENU hmenuShared, HOLEMENU holemenu,
- HWND hwndActiveObject)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::RemoveMenus(HMENU hmenuShared)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::SetStatusText(LPCOLESTR pszStatusText)
- {
- CHAR szBuffer[512];
-
- // The applet control calls this method whenever the applet calls
- // showStatus().
- if (::WideCharToMultiByte(CP_ACP, 0, pszStatusText, -1, szBuffer,
- ARRAY_ELEMENTS(szBuffer), NULL, NULL) > 0)
- ::SetWindowText(m_hwnd, szBuffer);
-
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::EnableModeless(BOOL fEnable)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::TranslateAccelerator(LPMSG lpmsg, WORD wID)
- {
- // The applet control doesn't currently invoke this method.
- return E_NOTIMPL;
- }
-
- STDMETHODIMP CAppletHostFrame::OnChanged(DISPID dispid)
- {
- IDispatch *pDispatch;
- DISPPARAMS dispparams;
- VARIANT varResult;
-
- // The applet control will call IPropertyNotifySink::OnChanged with
- // DISPID_READYSTATE after the loaded Java object has been created. The
- // applet's init() method has not been called yet.
- if (dispid == DISPID_READYSTATE) {
-
- if (m_pOleObject != NULL && m_pOleObject->QueryInterface(IID_IDispatch,
- (LPVOID *) &pDispatch) == S_OK) {
-
- dispparams.rgvarg = NULL;
- dispparams.rgdispidNamedArgs = NULL;
- dispparams.cArgs = 0;
- dispparams.cNamedArgs = 0;
-
- VariantInit(&varResult);
-
- if (pDispatch->Invoke(DISPID_READYSTATE, IID_NULL,
- LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispparams,
- &varResult, NULL, NULL) == S_OK) {
-
- // Check if the READYSTATE is now READYSTATE_COMPLETE. If so,
- // we can now safely use IDispatch to access the public methods
- // and properties of the applet and use
- // IConnectionPointContainer to attach to the events of the
- // applet.
- if (varResult.vt == VT_I4 && varResult.lVal ==
- READYSTATE_COMPLETE) {
-
- // The applet is now ready to go. The applet can now be
- // scripted against and it's now possible to connect to the
- // applet's events.
- DemonstrateIDispatch();
- }
-
- VariantClear(&varResult);
- }
-
- pDispatch->Release();
- }
- }
-
- return S_OK;
- }
-
- STDMETHODIMP CAppletHostFrame::OnRequestEdit(DISPID dispid)
- {
- // Ignore the notification.
- return S_OK;
- }
-
-
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
- lpszCmdLine, int nShowCmd)
- {
- CAppletHostFrame *pFrame;
- MSG msg;
-
- g_hInstance = hInstance;
-
- if (SUCCEEDED(CoInitialize(NULL))) {
-
- if (SUCCEEDED(CAppletHostFrame::InitializeClass())) {
-
- pFrame = new CAppletHostFrame();
-
- if (pFrame != NULL) {
-
- if (SUCCEEDED(pFrame->Initialize(nShowCmd))) {
-
- while (GetMessage(&msg, NULL, 0, 0)) {
- DispatchMessage(&msg);
- TranslateMessage(&msg);
- }
- }
-
- pFrame->Release();
- }
- }
-
- CoUninitialize();
- }
-
- return 0;
- }
-