Back to index

lightning-sunbird  0.9+nobinonly
plugin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "plugin.h"
00039 
00040 #define MIME_TYPES_HANDLED  "application/basic-plugin"
00041 #define PLUGIN_NAME         "Basic Example Plug-in for Mozilla"
00042 #define MIME_TYPES_DESCRIPTION  MIME_TYPES_HANDLED":bsc:"PLUGIN_NAME
00043 #define PLUGIN_DESCRIPTION  PLUGIN_NAME " (Plug-ins SDK sample)"
00044 
00045 
00046 char* NPP_GetMIMEDescription(void)
00047 {
00048   return(MIME_TYPES_DESCRIPTION);
00049 }
00050 
00052 // general initialization and shutdown
00053 //
00054 NPError NS_PluginInitialize()
00055 {
00056   return NPERR_NO_ERROR;
00057 }
00058 
00059 void NS_PluginShutdown()
00060 {
00061 }
00062 
00063 // get values per plugin
00064 NPError NS_PluginGetValue(NPPVariable aVariable, void *aValue)
00065 {
00066   NPError err = NPERR_NO_ERROR;
00067   switch (aVariable) {
00068     case NPPVpluginNameString:
00069       *((char **)aValue) = PLUGIN_NAME;
00070       break;
00071     case NPPVpluginDescriptionString:
00072       *((char **)aValue) = PLUGIN_DESCRIPTION;
00073       break;
00074     default:
00075       err = NPERR_INVALID_PARAM;
00076       break;
00077   }
00078   return err;
00079 }
00080 
00082 //
00083 // construction and destruction of our plugin instance object
00084 //
00085 nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
00086 {
00087   if(!aCreateDataStruct)
00088     return NULL;
00089 
00090   nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
00091   return plugin;
00092 }
00093 
00094 void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
00095 {
00096   if(aPlugin)
00097     delete (nsPluginInstance *)aPlugin;
00098 }
00099 
00101 //
00102 // nsPluginInstance class implementation
00103 //
00104 nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
00105   mInstance(aInstance),
00106   mInitialized(FALSE),
00107   mWindow(0),
00108   mXtwidget(0),
00109   mFontInfo(0)
00110 {
00111 }
00112 
00113 nsPluginInstance::~nsPluginInstance()
00114 {
00115 }
00116 
00117 static void
00118 xt_event_handler(Widget xtwidget, nsPluginInstance *plugin, XEvent *xevent, Boolean *b)
00119 {
00120   switch (xevent->type) {
00121     case Expose:
00122       // get rid of all other exposure events
00123       if (plugin) {
00124         //while(XCheckTypedWindowEvent(plugin->Display(), plugin->Window(), Expose, xevent));
00125         plugin->draw();
00126       }
00127       default:
00128         break;
00129   }
00130 }
00131 
00132 void nsPluginInstance::draw()
00133 {
00134   unsigned int h = mHeight/2;
00135   unsigned int w = 3 * mWidth/4;
00136   int x = (mWidth - w)/2; // center
00137   int y = h/2;
00138   if (x >= 0 && y >= 0) {
00139     GC gc = XCreateGC(mDisplay, mWindow, 0, NULL);
00140     if (!gc) 
00141       return;
00142     XDrawRectangle(mDisplay, mWindow, gc, x, y, w, h);
00143   const char *string = getVersion();
00144   if (string && *string) {
00145     int l = strlen(string);
00146     int fmba = mFontInfo->max_bounds.ascent;
00147     int fmbd = mFontInfo->max_bounds.descent;
00148     int fh = fmba + fmbd;
00149     y += fh;
00150     x += 32;
00151       XDrawString(mDisplay, mWindow, gc, x, y, string, l); 
00152     }
00153     XFreeGC(mDisplay, gc);
00154   }
00155 }
00156 
00157 NPBool nsPluginInstance::init(NPWindow* aWindow)
00158 {
00159   if(aWindow == NULL)
00160     return FALSE;
00161   
00162   if (SetWindow(aWindow))
00163   mInitialized = TRUE;
00164        
00165   return mInitialized;
00166 }
00167 
00168 void nsPluginInstance::shut()
00169 {
00170   mInitialized = FALSE;
00171 }
00172 
00173 const char * nsPluginInstance::getVersion()
00174 {
00175   return NPN_UserAgent(mInstance);
00176 }
00177 
00178 NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
00179 {
00180   NPError err = NPERR_NO_ERROR;
00181   switch (aVariable) {
00182     case NPPVpluginNameString:
00183     case NPPVpluginDescriptionString:
00184       return NS_PluginGetValue(aVariable, aValue) ;
00185       break;
00186     default:
00187       err = NPERR_INVALID_PARAM;
00188       break;
00189   }
00190   return err;
00191 
00192 }
00193 
00194 NPError nsPluginInstance::SetWindow(NPWindow* aWindow)
00195 {
00196   if(aWindow == NULL)
00197     return FALSE;
00198 
00199   mX = aWindow->x;
00200   mY = aWindow->y;
00201   mWidth = aWindow->width;
00202   mHeight = aWindow->height;
00203   if (mWindow == (Window) aWindow->window) {
00204     // The page with the plugin is being resized.
00205     // Save any UI information because the next time
00206     // around expect a SetWindow with a new window id.
00207   } else {
00208     mWindow = (Window) aWindow->window;
00209     NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *)aWindow->ws_info;
00210     mDisplay = ws_info->display;
00211     mVisual = ws_info->visual;
00212     mDepth = ws_info->depth;
00213     mColormap = ws_info->colormap;
00214 
00215     if (!mFontInfo) {
00216       if (!(mFontInfo = XLoadQueryFont(mDisplay, "9x15")))
00217         printf("Cannot open 9X15 font\n");
00218     }
00219     // add xt event handler
00220     Widget xtwidget = XtWindowToWidget(mDisplay, mWindow);
00221     if (xtwidget && mXtwidget != xtwidget) {
00222       mXtwidget = xtwidget;
00223       long event_mask = ExposureMask;
00224       XSelectInput(mDisplay, mWindow, event_mask);
00225       XtAddEventHandler(xtwidget, event_mask, False, (XtEventHandler)xt_event_handler, this);
00226     }
00227   }
00228   draw();
00229   return TRUE;
00230 }