Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / sdk / samples / basic / windows / plugin.cpp
blob8443ce501f066d6095e49a9b89d3323d86cb8fc5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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.
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.
22 * Contributor(s):
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 MPL, 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 MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include <windows.h>
40 #include <windowsx.h>
42 #include "plugin.h"
44 //////////////////////////////////////
46 // general initialization and shutdown
48 NPError NS_PluginInitialize()
50 return NPERR_NO_ERROR;
53 void NS_PluginShutdown()
57 /////////////////////////////////////////////////////////////
59 // construction and destruction of our plugin instance object
61 nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
63 if(!aCreateDataStruct)
64 return NULL;
66 nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
67 return plugin;
70 void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
72 if(aPlugin)
73 delete (nsPluginInstance *)aPlugin;
76 ////////////////////////////////////////
78 // nsPluginInstance class implementation
80 nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
81 mInstance(aInstance),
82 mInitialized(FALSE)
84 mhWnd = NULL;
87 nsPluginInstance::~nsPluginInstance()
91 static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
92 static WNDPROC lpOldProc = NULL;
94 NPBool nsPluginInstance::init(NPWindow* aWindow)
96 if(aWindow == NULL)
97 return FALSE;
99 mhWnd = (HWND)aWindow->window;
100 if(mhWnd == NULL)
101 return FALSE;
103 // subclass window so we can intercept window messages and
104 // do our drawing to it
105 lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
107 // associate window with our nsPluginInstance object so we can access
108 // it in the window procedure
109 SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
111 mInitialized = TRUE;
112 return TRUE;
115 void nsPluginInstance::shut()
117 // subclass it back
118 SubclassWindow(mhWnd, lpOldProc);
119 mhWnd = NULL;
120 mInitialized = FALSE;
123 NPBool nsPluginInstance::isInitialized()
125 return mInitialized;
128 const char * nsPluginInstance::getVersion()
130 return NPN_UserAgent(mInstance);
133 static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
135 switch (msg) {
136 case WM_PAINT:
138 // draw a frame and display the string
139 PAINTSTRUCT ps;
140 HDC hdc = BeginPaint(hWnd, &ps);
141 RECT rc;
142 GetClientRect(hWnd, &rc);
143 FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
145 // get our plugin instance object and ask it for the version string
146 nsPluginInstance *plugin = (nsPluginInstance *)GetWindowLong(hWnd, GWL_USERDATA);
147 if (plugin) {
148 const char * string = plugin->getVersion();
149 DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
151 else {
152 char string[] = "Error occured";
153 DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
156 EndPaint(hWnd, &ps);
158 break;
159 default:
160 break;
163 return DefWindowProc(hWnd, msg, wParam, lParam);