1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
42
#include "nsIServiceManager.h"
43
#include "nsISupportsUtils.h" // some usefule macros are defined here
45
//////////////////////////////////////
47
// general initialization and shutdown
49
NPError NS_PluginInitialize()
51
return NPERR_NO_ERROR;
54
void NS_PluginShutdown()
58
/////////////////////////////////////////////////////////////
60
// construction and destruction of our plugin instance object
62
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
64
if(!aCreateDataStruct)
67
nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
71
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
74
delete (nsPluginInstance *)aPlugin;
77
////////////////////////////////////////
79
// nsPluginInstance class implementation
81
nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
90
nsPluginInstance::~nsPluginInstance()
92
// mScriptablePeer may be also held by the browser
93
// so releasing it here does not guarantee that it is over
94
// we should take precaution in case it will be called later
95
// and zero its mPlugin member
96
mScriptablePeer->SetInstance(NULL);
97
NS_IF_RELEASE(mScriptablePeer);
100
static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
101
static WNDPROC lpOldProc = NULL;
103
NPBool nsPluginInstance::init(NPWindow* aWindow)
108
mhWnd = (HWND)aWindow->window;
112
// subclass window so we can intercept window messages and
113
// do our drawing to it
114
lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
116
// associate window with our nsPluginInstance object so we can access
117
// it in the window procedure
118
SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
124
void nsPluginInstance::shut()
127
SubclassWindow(mhWnd, lpOldProc);
129
mInitialized = FALSE;
132
NPBool nsPluginInstance::isInitialized()
137
// this will force to draw a version string in the plugin window
138
void nsPluginInstance::showVersion()
140
const char *ua = NPN_UserAgent(mInstance);
142
InvalidateRect(mhWnd, NULL, TRUE);
146
// this will clean the plugin window
147
void nsPluginInstance::clear()
150
InvalidateRect(mhWnd, NULL, TRUE);
154
// ==============================
155
// ! Scriptability related code !
156
// ==============================
158
// here the plugin is asked by Mozilla to tell if it is scriptable
159
// we should return a valid interface id and a pointer to
160
// nsScriptablePeer interface which we should have implemented
161
// and which should be defined in the corressponding *.xpt file
162
// in the bin/components folder
163
NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
165
NPError rv = NPERR_NO_ERROR;
167
if (aVariable == NPPVpluginScriptableInstance) {
168
// addref happens in getter, so we don't addref here
169
nsIScriptablePluginSample * scriptablePeer = getScriptablePeer();
170
if (scriptablePeer) {
171
*(nsISupports **)aValue = scriptablePeer;
173
rv = NPERR_OUT_OF_MEMORY_ERROR;
175
else if (aVariable == NPPVpluginScriptableIID) {
176
static nsIID scriptableIID = NS_ISCRIPTABLEPLUGINSAMPLE_IID;
177
nsIID* ptr = (nsIID *)NPN_MemAlloc(sizeof(nsIID));
179
*ptr = scriptableIID;
180
*(nsIID **)aValue = ptr;
182
rv = NPERR_OUT_OF_MEMORY_ERROR;
188
// ==============================
189
// ! Scriptability related code !
190
// ==============================
192
// this method will return the scriptable object (and create it if necessary)
193
nsScriptablePeer* nsPluginInstance::getScriptablePeer()
195
if (!mScriptablePeer) {
196
mScriptablePeer = new nsScriptablePeer(this);
200
NS_ADDREF(mScriptablePeer);
203
// add reference for the caller requesting the object
204
NS_ADDREF(mScriptablePeer);
205
return mScriptablePeer;
208
static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
213
// draw a frame and display the string
215
HDC hdc = BeginPaint(hWnd, &ps);
217
GetClientRect(hWnd, &rc);
218
FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
220
// get our plugin instance object and ask it for the version string
221
nsPluginInstance *plugin = (nsPluginInstance *)GetWindowLong(hWnd, GWL_USERDATA);
223
DrawText(hdc, plugin->mString, strlen(plugin->mString), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
225
char string[] = "Error occured";
226
DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
236
return DefWindowProc(hWnd, msg, wParam, lParam);