1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 ***** */
39
#include "nsISupportsUtils.h" // some usefule macros are defined here
43
#if !TARGET_API_MAC_CARBON
44
extern QDGlobals* gQDPtr;
47
//////////////////////////////////////
49
// general initialization and shutdown
51
NPError NS_PluginInitialize()
53
return NPERR_NO_ERROR;
56
void NS_PluginShutdown()
60
/////////////////////////////////////////////////////////////
62
// construction and destruction of our plugin instance object
64
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
66
if(!aCreateDataStruct)
69
nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
73
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
76
delete (nsPluginInstance *)aPlugin;
79
////////////////////////////////////////
81
// nsPluginInstance class implementation
83
nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
92
nsPluginInstance::~nsPluginInstance()
94
// mScriptablePeer may be also held by the browser
95
// so releasing it here does not guarantee that it is over
96
// we should take precaution in case it will be called later
97
// and zero its mPlugin member
98
mScriptablePeer->SetInstance(NULL);
99
NS_IF_RELEASE(mScriptablePeer);
102
NPBool nsPluginInstance::init(NPWindow* aWindow)
109
mSaveClip = NewRgn();
113
void nsPluginInstance::shut()
116
mInitialized = FALSE;
119
NPBool nsPluginInstance::isInitialized()
124
/////////////////////////////////////////////////////////////
129
nsPluginInstance::DrawString(const unsigned char* text,
135
short length, textHeight, textWidth;
140
length = strlen((char*)text);
147
GetFontInfo(&fontInfo);
149
textHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
150
textWidth = TextWidth(text, 0, length);
152
if (width > textWidth && height > textHeight)
154
MoveTo(centerX - (textWidth >> 1), height >> 1);
155
DrawText(text, 0, length);
159
/////////////////////////////////////////////////////////////
164
nsPluginInstance::DoDraw(void)
167
RGBColor black = { 0x0000, 0x0000, 0x0000 };
168
RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF };
169
SInt32 height = mWindow->height;
170
SInt32 width = mWindow->width;
171
SInt32 centerX = (width) >> 1;
172
SInt32 centerY = (height) >> 1;
174
UInt8 *pTheText = (unsigned char*) mString;
178
drawRect.bottom = drawRect.top + height;
179
drawRect.right = drawRect.left + width;
182
RGBForeColor(&black);
183
RGBBackColor(&white);
185
#if !TARGET_API_MAC_CARBON
186
FillRect(&drawRect, &(gQDPtr->white));
189
FillRect(&drawRect, GetQDGlobalsWhite(&qdWhite));
192
FrameRect(&drawRect);
193
DrawString(pTheText, width, height, centerX, drawRect);
196
/////////////////////////////////////////////////////////////
201
nsPluginInstance::SetWindow(NPWindow* window)
204
if( StartDraw(window) ) {
208
return NPERR_NO_ERROR;
211
/////////////////////////////////////////////////////////////
216
nsPluginInstance::HandleEvent(void* event)
218
int16 eventHandled = FALSE;
220
EventRecord* ev = (EventRecord*) event;
226
* Draw ourselves on update events
229
if( StartDraw(mWindow) ) {
243
/////////////////////////////////////////////////////////////
245
// StartDraw - setup port state
248
nsPluginInstance::StartDraw(NPWindow* window)
253
NP_Port* npport = (NP_Port*) mWindow->window;
254
CGrafPtr ourPort = npport->port;
256
if (mWindow->clipRect.left < mWindow->clipRect.right)
259
SetPort((GrafPtr) ourPort);
261
#if !TARGET_API_MAC_CARBON
262
portRect = ourPort->portRect;
264
GetPortBounds(ourPort, &portRect);
266
mSavePortTop = portRect.top;
267
mSavePortLeft = portRect.left;
270
mRevealedRect.top = mWindow->clipRect.top + npport->porty;
271
mRevealedRect.left = mWindow->clipRect.left + npport->portx;
272
mRevealedRect.bottom = mWindow->clipRect.bottom + npport->porty;
273
mRevealedRect.right = mWindow->clipRect.right + npport->portx;
274
SetOrigin(npport->portx, npport->porty);
275
ClipRect(&mRevealedRect);
283
/////////////////////////////////////////////////////////////
285
// EndDraw - restore port state
288
nsPluginInstance::EndDraw(NPWindow* window)
290
SetOrigin(mSavePortLeft, mSavePortTop);
296
// this will force to draw a version string in the plugin window
297
void nsPluginInstance::showVersion()
299
const char *ua = NPN_UserAgent(mInstance);
307
// this will clean the plugin window
308
void nsPluginInstance::clear()
317
// ==============================
318
// ! Scriptability related code !
319
// ==============================
321
// here the plugin is asked by Mozilla to tell if it is scriptable
322
// we should return a valid interface id and a pointer to
323
// nsScriptablePeer interface which we should have implemented
324
// and which should be defined in the corressponding *.xpt file
325
// in the bin/components folder
326
NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
328
NPError rv = NPERR_NO_ERROR;
331
case NPPVpluginScriptableInstance: {
332
// addref happens in getter, so we don't addref here
333
nsIScriptablePluginSample * scriptablePeer = getScriptablePeer();
334
if (scriptablePeer) {
335
*(nsISupports **)aValue = scriptablePeer;
337
rv = NPERR_OUT_OF_MEMORY_ERROR;
341
case NPPVpluginScriptableIID: {
342
static nsIID scriptableIID = NS_ISCRIPTABLEPLUGINSAMPLE_IID;
343
nsIID* ptr = (nsIID *)NPN_MemAlloc(sizeof(nsIID));
345
*ptr = scriptableIID;
346
*(nsIID **)aValue = ptr;
348
rv = NPERR_OUT_OF_MEMORY_ERROR;
359
// ==============================
360
// ! Scriptability related code !
361
// ==============================
363
// this method will return the scriptable object (and create it if necessary)
364
nsScriptablePeer* nsPluginInstance::getScriptablePeer()
366
if (!mScriptablePeer) {
367
mScriptablePeer = new nsScriptablePeer(this);
371
NS_ADDREF(mScriptablePeer);
374
// add reference for the caller requesting the object
375
NS_ADDREF(mScriptablePeer);
376
return mScriptablePeer;