#include "stdafx.h" #include #include #include #include "mplugin.h" #include "npupp.h" #include "SampleMozCtl.h" class ScriptablePluginObject : public NPObject { public: ScriptablePluginObject(NPP npp) { m_MPlugin = NULL; m_Npp = npp; } CMPlugin *m_MPlugin; NPP m_Npp; }; static NPObject *SPO_Allocate(NPP npp,NPClass *aClass) { return new ScriptablePluginObject(npp); } static void SPO_Deallocate(NPObject *obj) { delete (ScriptablePluginObject *)obj; } static void SPO_Invalidate(NPObject *obj) { } static bool SPO_HasMethod(NPObject *obj, NPIdentifier name) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozHasMethod( name ); } static bool SPO_Invoke(NPObject *obj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozInvoke( name,args,argCount,result ); } static bool SPO_SetProperty(NPObject *obj, NPIdentifier name, const NPVariant *value) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozSetProperty( name,value ); } static bool SPO_HasProperty(NPObject * obj, NPIdentifier name) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozHasProperty( name ); } static bool SPO_RemoveProperty(NPObject *obj, NPIdentifier name ) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozRemoveProperty( name ); } static bool SPO_GetProperty(NPObject *obj, NPIdentifier name, NPVariant *result) { return ((CSampleMozCtrl*)((ScriptablePluginObject*)obj)->m_MPlugin->m_Wnd)->MozGetProperty( name,result ); } static bool SPO_InvokeDefault( NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result) { return FALSE; } NPClass sSPO_Class = { NP_CLASS_STRUCT_VERSION, SPO_Allocate, SPO_Deallocate, SPO_Invalidate, SPO_HasMethod, SPO_Invoke, SPO_InvokeDefault, SPO_HasProperty, SPO_GetProperty, SPO_SetProperty, SPO_RemoveProperty }; static NPIdentifier sFoo_id; static NPIdentifier sBar_id; static NPIdentifier sDocument_id; static NPIdentifier sBody_id; static NPIdentifier sCreateElement_id; static NPIdentifier sCreateTextNode_id; static NPIdentifier sAppendChild_id; static NPObject *sWindowObj; CMPlugin::CMPlugin(NPP pNPInstance) : m_pNPInstance(pNPInstance), m_IsPluginInitialized(FALSE), m_pScriptableObject(NULL), m_lpOldProc(NULL) { m_Wnd = NULL; } CMPlugin::~CMPlugin() { if (m_pScriptableObject) NPN_ReleaseObject(m_pScriptableObject); } static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM); NPBool CMPlugin::InitPlugin(NPWindow* pNPWindow) { CSampleMozCtrl *bec = new CSampleMozCtrl; m_Wnd = bec; //TODO add parameters you saved from page to your plugin CRect rect; rect.left = 0; rect.top = 0; rect.right = pNPWindow->width; rect.bottom = pNPWindow->height; CWnd *wnd = CWnd::FromHandle( (HWND)pNPWindow->window ); bec->Create( _T("winclass"), _T("SampleMoz"), WS_CHILD | WS_VISIBLE, rect,wnd , 11001); //TODO m_hWnd = (HWND)pNPWindow->window; // subclass window so we can intercept window messages and // do our drawing to it m_lpOldProc = SubclassWindow( m_hWnd, (WNDPROC)PluginWinProc ); // associate window with our CMPlugin object so we can access // it in the window procedure SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this); m_IsPluginInitialized = TRUE; return TRUE; } void CMPlugin::ClosePlugin() { #ifdef XP_WIN //subclass it back SubclassWindow(m_hWnd, m_lpOldProc); m_hWnd = NULL; #endif if( m_Wnd ) { delete m_Wnd; } m_IsPluginInitialized = FALSE; } int16 CMPlugin::handleEvent(void* event) { return 0; } NPObject * CMPlugin::GetScriptableObject() { if (!m_pScriptableObject) { m_pScriptableObject = NPN_CreateObject(m_pNPInstance, &sSPO_Class); ((ScriptablePluginObject*)m_pScriptableObject)->m_MPlugin = this; } if (m_pScriptableObject) { NPN_RetainObject(m_pScriptableObject); } return m_pScriptableObject; } static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { if( msg == WM_SIZE ) { //prcoess browser resize message CMPlugin * p = (CMPlugin *)GetWindowLong(hWnd, GWL_USERDATA); if( p ) {//resize plugin CRect rect; ::GetWindowRect(hWnd,rect ); CPoint pt( rect.left, rect.top ); ::ScreenToClient(hWnd,&pt ); rect.left = pt.x; rect.top = pt.y; pt.x = rect.right; pt.y = rect.bottom; ::ScreenToClient(hWnd,&pt ); rect.right = pt.x; rect.bottom = pt.y; ((CSampleMozCtrl*)p->m_Wnd)->WindowResize( rect ); } return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); }