You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.2 KiB
C

#include "stdafx.h"
#include <stdio.h>
#include "mplugin.h"
#include "npupp.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 ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_HasMethod( name );
}
static bool SPO_Invoke(NPObject *obj, NPIdentifier name, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
return ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_Invoke( name,args,argCount,result );
}
static bool SPO_SetProperty(NPObject *obj, NPIdentifier name, const NPVariant *value)
{
return ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_SetProperty( name,value );
}
static bool SPO_HasProperty(NPObject * obj, NPIdentifier name)
{
return ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_HasProperty( name );
}
static bool SPO_RemoveProperty(NPObject *obj, NPIdentifier name )
{
return ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_RemoveProperty( name );
}
static bool SPO_GetProperty(NPObject *obj, NPIdentifier name, NPVariant *result)
{
return ((yourclass*)((ScriptablePluginObject*)obj)->m_Plugin->m_Wnd)->Mozilla_GetProperty( 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_pNPStream(NULL),
m_bInitialized(FALSE),
m_pScriptableObject(NULL),
m_lpOldProc(NULL)
{
m_Wnd = NULL;
}
CMPlugin::~CMPlugin()
{
if (m_pScriptableObject)
NPN_ReleaseObject(m_pScriptableObject);
}
#ifdef XP_WIN
static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
#endif
NPBool CMPlugin::InitPlugin(NPWindow* pNPWindow)
{
yourclass *bec = new yourclass;
m_Wnd = bec;
//TODO add parameters you saved from page to your plugin
CWnd *wnd = CWnd::FromHandle( (HWND)pNPWindow->window );
bec->Create( _T("winclass"), _T("yourapp"), WS_CHILD | WS_VISIBLE, rect,wnd , 9001); //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_bInitialized = 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_bInitialized = 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_Plugin = this;
}
if (m_pScriptableObject) {
NPN_RetainObject(m_pScriptableObject);
}
return m_pScriptableObject;
}
#ifdef XP_WIN
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
}
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
#endif