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

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/tools/sdk/samples/scriptable/windows/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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
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 <windows.h>
 
39
#include <windowsx.h>
 
40
 
 
41
#include "plugin.h"
 
42
#include "nsIServiceManager.h"
 
43
#include "nsISupportsUtils.h" // some usefule macros are defined here
 
44
 
 
45
//////////////////////////////////////
 
46
//
 
47
// general initialization and shutdown
 
48
//
 
49
NPError NS_PluginInitialize()
 
50
{
 
51
  return NPERR_NO_ERROR;
 
52
}
 
53
 
 
54
void NS_PluginShutdown()
 
55
{
 
56
}
 
57
 
 
58
/////////////////////////////////////////////////////////////
 
59
//
 
60
// construction and destruction of our plugin instance object
 
61
//
 
62
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
 
63
{
 
64
  if(!aCreateDataStruct)
 
65
    return NULL;
 
66
 
 
67
  nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
 
68
  return plugin;
 
69
}
 
70
 
 
71
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
 
72
{
 
73
  if(aPlugin)
 
74
    delete (nsPluginInstance *)aPlugin;
 
75
}
 
76
 
 
77
////////////////////////////////////////
 
78
//
 
79
// nsPluginInstance class implementation
 
80
//
 
81
nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
 
82
  mInstance(aInstance),
 
83
  mInitialized(FALSE),
 
84
  mScriptablePeer(NULL)
 
85
{
 
86
  mhWnd = NULL;
 
87
  mString[0] = '\0';
 
88
}
 
89
 
 
90
nsPluginInstance::~nsPluginInstance()
 
91
{
 
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);
 
98
}
 
99
 
 
100
static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
 
101
static WNDPROC lpOldProc = NULL;
 
102
 
 
103
NPBool nsPluginInstance::init(NPWindow* aWindow)
 
104
{
 
105
  if(aWindow == NULL)
 
106
    return FALSE;
 
107
 
 
108
  mhWnd = (HWND)aWindow->window;
 
109
  if(mhWnd == NULL)
 
110
    return FALSE;
 
111
 
 
112
  // subclass window so we can intercept window messages and
 
113
  // do our drawing to it
 
114
  lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
 
115
 
 
116
  // associate window with our nsPluginInstance object so we can access 
 
117
  // it in the window procedure
 
118
  SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
 
119
 
 
120
  mInitialized = TRUE;
 
121
  return TRUE;
 
122
}
 
123
 
 
124
void nsPluginInstance::shut()
 
125
{
 
126
  // subclass it back
 
127
  SubclassWindow(mhWnd, lpOldProc);
 
128
  mhWnd = NULL;
 
129
  mInitialized = FALSE;
 
130
}
 
131
 
 
132
NPBool nsPluginInstance::isInitialized()
 
133
{
 
134
  return mInitialized;
 
135
}
 
136
 
 
137
// this will force to draw a version string in the plugin window
 
138
void nsPluginInstance::showVersion()
 
139
{
 
140
  const char *ua = NPN_UserAgent(mInstance);
 
141
  strcpy(mString, ua);
 
142
  InvalidateRect(mhWnd, NULL, TRUE);
 
143
  UpdateWindow(mhWnd);
 
144
}
 
145
 
 
146
// this will clean the plugin window
 
147
void nsPluginInstance::clear()
 
148
{
 
149
  strcpy(mString, "");
 
150
  InvalidateRect(mhWnd, NULL, TRUE);
 
151
  UpdateWindow(mhWnd);
 
152
}
 
153
 
 
154
// ==============================
 
155
// ! Scriptability related code !
 
156
// ==============================
 
157
//
 
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)
 
164
{
 
165
  NPError rv = NPERR_NO_ERROR;
 
166
 
 
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;
 
172
    } else
 
173
      rv = NPERR_OUT_OF_MEMORY_ERROR;
 
174
  }
 
175
  else if (aVariable == NPPVpluginScriptableIID) {
 
176
    static nsIID scriptableIID = NS_ISCRIPTABLEPLUGINSAMPLE_IID;
 
177
    nsIID* ptr = (nsIID *)NPN_MemAlloc(sizeof(nsIID));
 
178
    if (ptr) {
 
179
        *ptr = scriptableIID;
 
180
        *(nsIID **)aValue = ptr;
 
181
    } else
 
182
      rv = NPERR_OUT_OF_MEMORY_ERROR;
 
183
  }
 
184
 
 
185
  return rv;
 
186
}
 
187
 
 
188
// ==============================
 
189
// ! Scriptability related code !
 
190
// ==============================
 
191
//
 
192
// this method will return the scriptable object (and create it if necessary)
 
193
nsScriptablePeer* nsPluginInstance::getScriptablePeer()
 
194
{
 
195
  if (!mScriptablePeer) {
 
196
    mScriptablePeer = new nsScriptablePeer(this);
 
197
    if(!mScriptablePeer)
 
198
      return NULL;
 
199
 
 
200
    NS_ADDREF(mScriptablePeer);
 
201
  }
 
202
 
 
203
  // add reference for the caller requesting the object
 
204
  NS_ADDREF(mScriptablePeer);
 
205
  return mScriptablePeer;
 
206
}
 
207
 
 
208
static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
209
{
 
210
  switch (msg) {
 
211
    case WM_PAINT:
 
212
      {
 
213
        // draw a frame and display the string
 
214
        PAINTSTRUCT ps;
 
215
        HDC hdc = BeginPaint(hWnd, &ps);
 
216
        RECT rc;
 
217
        GetClientRect(hWnd, &rc);
 
218
        FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
 
219
 
 
220
        // get our plugin instance object and ask it for the version string
 
221
        nsPluginInstance *plugin = (nsPluginInstance *)GetWindowLong(hWnd, GWL_USERDATA);
 
222
        if (plugin)
 
223
          DrawText(hdc, plugin->mString, strlen(plugin->mString), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
 
224
        else {
 
225
          char string[] = "Error occured";
 
226
          DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
 
227
        }
 
228
 
 
229
        EndPaint(hWnd, &ps);
 
230
      }
 
231
      break;
 
232
    default:
 
233
      break;
 
234
  }
 
235
 
 
236
  return DefWindowProc(hWnd, msg, wParam, lParam);
 
237
}