~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/tools/sdk/samples/scriptable/mac/plugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include "plugin.h"
 
39
#include "nsISupportsUtils.h" // some usefule macros are defined here
 
40
 
 
41
#include <string.h>
 
42
 
 
43
#if !TARGET_API_MAC_CARBON
 
44
extern QDGlobals*       gQDPtr;
 
45
#endif
 
46
 
 
47
//////////////////////////////////////
 
48
//
 
49
// general initialization and shutdown
 
50
//
 
51
NPError NS_PluginInitialize()
 
52
{
 
53
  return NPERR_NO_ERROR;
 
54
}
 
55
 
 
56
void NS_PluginShutdown()
 
57
{
 
58
}
 
59
 
 
60
/////////////////////////////////////////////////////////////
 
61
//
 
62
// construction and destruction of our plugin instance object
 
63
//
 
64
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
 
65
{
 
66
  if(!aCreateDataStruct)
 
67
    return NULL;
 
68
 
 
69
  nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
 
70
  return plugin;
 
71
}
 
72
 
 
73
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
 
74
{
 
75
  if(aPlugin)
 
76
    delete (nsPluginInstance *)aPlugin;
 
77
}
 
78
 
 
79
////////////////////////////////////////
 
80
//
 
81
// nsPluginInstance class implementation
 
82
//
 
83
nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
 
84
  mInstance(aInstance),
 
85
  mInitialized(FALSE),
 
86
  mScriptablePeer(NULL)
 
87
{
 
88
  mWindow = NULL;
 
89
  mString[0] = '\0';
 
90
}
 
91
 
 
92
nsPluginInstance::~nsPluginInstance()
 
93
{
 
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);
 
100
}
 
101
 
 
102
NPBool nsPluginInstance::init(NPWindow* aWindow)
 
103
{
 
104
  if(aWindow == NULL)
 
105
    return FALSE;
 
106
  
 
107
  mWindow = aWindow;
 
108
  mInitialized = TRUE;
 
109
  mSaveClip = NewRgn();
 
110
  return TRUE;
 
111
}
 
112
 
 
113
void nsPluginInstance::shut()
 
114
{
 
115
  mWindow = NULL;
 
116
  mInitialized = FALSE;
 
117
}
 
118
 
 
119
NPBool nsPluginInstance::isInitialized()
 
120
{
 
121
  return mInitialized;
 
122
}
 
123
 
 
124
/////////////////////////////////////////////////////////////
 
125
//
 
126
// DrawString
 
127
//
 
128
void
 
129
nsPluginInstance::DrawString(const unsigned char* text, 
 
130
                             short width, 
 
131
                             short height, 
 
132
                             short centerX, 
 
133
                             Rect drawRect)
 
134
{
 
135
        short length, textHeight, textWidth;
 
136
 
 
137
        if(text == NULL)
 
138
                return;
 
139
        
 
140
        length = strlen((char*)text);
 
141
        TextFont(1);
 
142
        TextFace(bold);
 
143
        TextMode(srcCopy);
 
144
        TextSize(12);
 
145
        
 
146
        FontInfo fontInfo;
 
147
        GetFontInfo(&fontInfo);
 
148
 
 
149
        textHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
 
150
        textWidth = TextWidth(text, 0, length);
 
151
                
 
152
        if (width > textWidth && height > textHeight)
 
153
        {
 
154
                MoveTo(centerX - (textWidth >> 1), height >> 1);
 
155
                DrawText(text, 0, length);
 
156
        }               
 
157
}
 
158
 
 
159
/////////////////////////////////////////////////////////////
 
160
//
 
161
// DoDraw - paint
 
162
//
 
163
void 
 
164
nsPluginInstance::DoDraw(void)
 
165
{
 
166
        Rect drawRect;
 
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;
 
173
 
 
174
        UInt8           *pTheText = (unsigned char*) mString;
 
175
 
 
176
        drawRect.top = 0;
 
177
        drawRect.left = 0;
 
178
        drawRect.bottom = drawRect.top + height;
 
179
        drawRect.right = drawRect.left + width;
 
180
 
 
181
  PenNormal();
 
182
  RGBForeColor(&black);
 
183
  RGBBackColor(&white);
 
184
 
 
185
#if !TARGET_API_MAC_CARBON
 
186
  FillRect(&drawRect, &(gQDPtr->white));
 
187
#else
 
188
  Pattern qdWhite;
 
189
  FillRect(&drawRect, GetQDGlobalsWhite(&qdWhite));
 
190
#endif
 
191
 
 
192
        FrameRect(&drawRect);
 
193
  DrawString(pTheText, width, height, centerX, drawRect);
 
194
}
 
195
 
 
196
/////////////////////////////////////////////////////////////
 
197
//
 
198
// SetWindow
 
199
//
 
200
NPError
 
201
nsPluginInstance::SetWindow(NPWindow* window)
 
202
{
 
203
        mWindow = window;
 
204
        if( StartDraw(window) ) {
 
205
                DoDraw();
 
206
                EndDraw(window);
 
207
        }
 
208
        return NPERR_NO_ERROR;
 
209
}
 
210
 
 
211
/////////////////////////////////////////////////////////////
 
212
//
 
213
// HandleEvent
 
214
//
 
215
uint16
 
216
nsPluginInstance::HandleEvent(void* event)
 
217
{
 
218
        int16 eventHandled = FALSE;
 
219
        
 
220
        EventRecord* ev = (EventRecord*) event;
 
221
        if (event != NULL)
 
222
        {
 
223
                switch (ev->what)
 
224
                {
 
225
                        /*
 
226
                         * Draw ourselves on update events
 
227
                         */
 
228
                        case updateEvt:
 
229
                                if( StartDraw(mWindow) ) {
 
230
                                        DoDraw();
 
231
                                        EndDraw(mWindow);
 
232
                                }
 
233
                                eventHandled = true;
 
234
                                break;
 
235
 
 
236
                        default:
 
237
                                break;
 
238
                }
 
239
        }
 
240
        return eventHandled;
 
241
}
 
242
 
 
243
/////////////////////////////////////////////////////////////
 
244
//
 
245
// StartDraw - setup port state
 
246
//
 
247
NPBool
 
248
nsPluginInstance::StartDraw(NPWindow* window)
 
249
{
 
250
        if (mWindow == NULL)
 
251
                return false;
 
252
                
 
253
        NP_Port* npport = (NP_Port*) mWindow->window;
 
254
        CGrafPtr ourPort = npport->port;
 
255
        
 
256
        if (mWindow->clipRect.left < mWindow->clipRect.right)
 
257
        {
 
258
                GetPort(&mSavePort);
 
259
                SetPort((GrafPtr) ourPort);
 
260
    Rect portRect;
 
261
#if !TARGET_API_MAC_CARBON
 
262
    portRect = ourPort->portRect;
 
263
#else
 
264
    GetPortBounds(ourPort, &portRect);
 
265
#endif
 
266
                mSavePortTop = portRect.top;
 
267
                mSavePortLeft = portRect.left;
 
268
                GetClip(mSaveClip);
 
269
                
 
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);
 
276
 
 
277
                return true;
 
278
        }
 
279
        else
 
280
                return false;
 
281
}
 
282
 
 
283
/////////////////////////////////////////////////////////////
 
284
//
 
285
// EndDraw - restore port state
 
286
//
 
287
void
 
288
nsPluginInstance::EndDraw(NPWindow* window)
 
289
{
 
290
        SetOrigin(mSavePortLeft, mSavePortTop);
 
291
        SetClip(mSaveClip);
 
292
        SetPort(mSavePort);
 
293
}
 
294
 
 
295
 
 
296
// this will force to draw a version string in the plugin window
 
297
void nsPluginInstance::showVersion()
 
298
{
 
299
  const char *ua = NPN_UserAgent(mInstance);
 
300
  strcpy(mString, ua);
 
301
 
 
302
  StartDraw(mWindow);
 
303
  DoDraw();
 
304
  EndDraw(mWindow);
 
305
}
 
306
 
 
307
// this will clean the plugin window
 
308
void nsPluginInstance::clear()
 
309
{
 
310
  strcpy(mString, "");
 
311
 
 
312
  StartDraw(mWindow);
 
313
  DoDraw();
 
314
  EndDraw(mWindow);
 
315
}
 
316
 
 
317
// ==============================
 
318
// ! Scriptability related code !
 
319
// ==============================
 
320
//
 
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)
 
327
{
 
328
  NPError rv = NPERR_NO_ERROR;
 
329
 
 
330
  switch (aVariable) {
 
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;
 
336
      } else
 
337
        rv = NPERR_OUT_OF_MEMORY_ERROR;
 
338
    }
 
339
    break;
 
340
 
 
341
    case NPPVpluginScriptableIID: {
 
342
      static nsIID scriptableIID = NS_ISCRIPTABLEPLUGINSAMPLE_IID;
 
343
      nsIID* ptr = (nsIID *)NPN_MemAlloc(sizeof(nsIID));
 
344
      if (ptr) {
 
345
          *ptr = scriptableIID;
 
346
          *(nsIID **)aValue = ptr;
 
347
      } else
 
348
        rv = NPERR_OUT_OF_MEMORY_ERROR;
 
349
    }
 
350
    break;
 
351
 
 
352
    default:
 
353
      break;
 
354
  }
 
355
 
 
356
  return rv;
 
357
}
 
358
 
 
359
// ==============================
 
360
// ! Scriptability related code !
 
361
// ==============================
 
362
//
 
363
// this method will return the scriptable object (and create it if necessary)
 
364
nsScriptablePeer* nsPluginInstance::getScriptablePeer()
 
365
{
 
366
  if (!mScriptablePeer) {
 
367
    mScriptablePeer = new nsScriptablePeer(this);
 
368
    if(!mScriptablePeer)
 
369
      return NULL;
 
370
 
 
371
    NS_ADDREF(mScriptablePeer);
 
372
  }
 
373
 
 
374
  // add reference for the caller requesting the object
 
375
  NS_ADDREF(mScriptablePeer);
 
376
  return mScriptablePeer;
 
377
}