Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / sdk / samples / basic / unix / plugin.cpp
blob5ab58d0b8e34c879738e8c14f7ff0eda95649314
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 ***** */
38 #include "plugin.h"
40 #define MIME_TYPES_HANDLED "application/basic-plugin"
41 #define PLUGIN_NAME "Basic Example Plug-in for Mozilla"
42 #define MIME_TYPES_DESCRIPTION MIME_TYPES_HANDLED":bsc:"PLUGIN_NAME
43 #define PLUGIN_DESCRIPTION PLUGIN_NAME " (Plug-ins SDK sample)"
46 char* NPP_GetMIMEDescription(void)
48 return(MIME_TYPES_DESCRIPTION);
51 /////////////////////////////////////
52 // general initialization and shutdown
54 NPError NS_PluginInitialize()
56 return NPERR_NO_ERROR;
59 void NS_PluginShutdown()
63 // get values per plugin
64 NPError NS_PluginGetValue(NPPVariable aVariable, void *aValue)
66 NPError err = NPERR_NO_ERROR;
67 switch (aVariable) {
68 case NPPVpluginNameString:
69 *((char **)aValue) = PLUGIN_NAME;
70 break;
71 case NPPVpluginDescriptionString:
72 *((char **)aValue) = PLUGIN_DESCRIPTION;
73 break;
74 default:
75 err = NPERR_INVALID_PARAM;
76 break;
78 return err;
81 /////////////////////////////////////////////////////////////
83 // construction and destruction of our plugin instance object
85 nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
87 if(!aCreateDataStruct)
88 return NULL;
90 nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
91 return plugin;
94 void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
96 if(aPlugin)
97 delete (nsPluginInstance *)aPlugin;
100 ////////////////////////////////////////
102 // nsPluginInstance class implementation
104 nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
105 mInstance(aInstance),
106 mInitialized(FALSE),
107 mWindow(0),
108 mXtwidget(0),
109 mFontInfo(0)
113 nsPluginInstance::~nsPluginInstance()
117 static void
118 xt_event_handler(Widget xtwidget, nsPluginInstance *plugin, XEvent *xevent, Boolean *b)
120 switch (xevent->type) {
121 case Expose:
122 // get rid of all other exposure events
123 if (plugin) {
124 //while(XCheckTypedWindowEvent(plugin->Display(), plugin->Window(), Expose, xevent));
125 plugin->draw();
127 default:
128 break;
132 void nsPluginInstance::draw()
134 unsigned int h = mHeight/2;
135 unsigned int w = 3 * mWidth/4;
136 int x = (mWidth - w)/2; // center
137 int y = h/2;
138 if (x >= 0 && y >= 0) {
139 GC gc = XCreateGC(mDisplay, mWindow, 0, NULL);
140 if (!gc)
141 return;
142 XDrawRectangle(mDisplay, mWindow, gc, x, y, w, h);
143 const char *string = getVersion();
144 if (string && *string) {
145 int l = strlen(string);
146 int fmba = mFontInfo->max_bounds.ascent;
147 int fmbd = mFontInfo->max_bounds.descent;
148 int fh = fmba + fmbd;
149 y += fh;
150 x += 32;
151 XDrawString(mDisplay, mWindow, gc, x, y, string, l);
153 XFreeGC(mDisplay, gc);
157 NPBool nsPluginInstance::init(NPWindow* aWindow)
159 if(aWindow == NULL)
160 return FALSE;
162 if (SetWindow(aWindow))
163 mInitialized = TRUE;
165 return mInitialized;
168 void nsPluginInstance::shut()
170 mInitialized = FALSE;
173 const char * nsPluginInstance::getVersion()
175 return NPN_UserAgent(mInstance);
178 NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
180 NPError err = NPERR_NO_ERROR;
181 switch (aVariable) {
182 case NPPVpluginNameString:
183 case NPPVpluginDescriptionString:
184 return NS_PluginGetValue(aVariable, aValue) ;
185 break;
186 default:
187 err = NPERR_INVALID_PARAM;
188 break;
190 return err;
194 NPError nsPluginInstance::SetWindow(NPWindow* aWindow)
196 if(aWindow == NULL)
197 return FALSE;
199 mX = aWindow->x;
200 mY = aWindow->y;
201 mWidth = aWindow->width;
202 mHeight = aWindow->height;
203 if (mWindow == (Window) aWindow->window) {
204 // The page with the plugin is being resized.
205 // Save any UI information because the next time
206 // around expect a SetWindow with a new window id.
207 } else {
208 mWindow = (Window) aWindow->window;
209 NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *)aWindow->ws_info;
210 mDisplay = ws_info->display;
211 mVisual = ws_info->visual;
212 mDepth = ws_info->depth;
213 mColormap = ws_info->colormap;
215 if (!mFontInfo) {
216 if (!(mFontInfo = XLoadQueryFont(mDisplay, "9x15")))
217 printf("Cannot open 9X15 font\n");
219 // add xt event handler
220 Widget xtwidget = XtWindowToWidget(mDisplay, mWindow);
221 if (xtwidget && mXtwidget != xtwidget) {
222 mXtwidget = xtwidget;
223 long event_mask = ExposureMask;
224 XSelectInput(mDisplay, mWindow, event_mask);
225 XtAddEventHandler(xtwidget, event_mask, False, (XtEventHandler)xt_event_handler, this);
228 draw();
229 return TRUE;