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 
00039 #include <windows.h>
00040 #include <windowsx.h>
00041 
00042 #include "plugin.h"
00043 
00045 //
00046 // general initialization and shutdown
00047 //
00048 NPError NS_PluginInitialize()
00049 {
00050   return NPERR_NO_ERROR;
00051 }
00052 
00053 void NS_PluginShutdown()
00054 {
00055 }
00056 
00058 //
00059 // construction and destruction of our plugin instance object
00060 //
00061 nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
00062 {
00063   if(!aCreateDataStruct)
00064     return NULL;
00065 
00066   nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
00067   return plugin;
00068 }
00069 
00070 void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
00071 {
00072   if(aPlugin)
00073     delete (nsPluginInstance *)aPlugin;
00074 }
00075 
00077 //
00078 // nsPluginInstance class implementation
00079 //
00080 nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
00081   mInstance(aInstance),
00082   mInitialized(FALSE)
00083 {
00084   mhWnd = NULL;
00085 }
00086 
00087 nsPluginInstance::~nsPluginInstance()
00088 {
00089 }
00090 
00091 static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
00092 static WNDPROC lpOldProc = NULL;
00093 
00094 NPBool nsPluginInstance::init(NPWindow* aWindow)
00095 {
00096   if(aWindow == NULL)
00097     return FALSE;
00098 
00099   mhWnd = (HWND)aWindow->window;
00100   if(mhWnd == NULL)
00101     return FALSE;
00102 
00103   // subclass window so we can intercept window messages and
00104   // do our drawing to it
00105   lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
00106 
00107   // associate window with our nsPluginInstance object so we can access 
00108   // it in the window procedure
00109   SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
00110 
00111   mInitialized = TRUE;
00112   return TRUE;
00113 }
00114 
00115 void nsPluginInstance::shut()
00116 {
00117   // subclass it back
00118   SubclassWindow(mhWnd, lpOldProc);
00119   mhWnd = NULL;
00120   mInitialized = FALSE;
00121 }
00122 
00123 NPBool nsPluginInstance::isInitialized()
00124 {
00125   return mInitialized;
00126 }
00127 
00128 const char * nsPluginInstance::getVersion()
00129 {
00130   return NPN_UserAgent(mInstance);
00131 }
00132 
00133 static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00134 {
00135   switch (msg) {
00136     case WM_PAINT:
00137       {
00138         // draw a frame and display the string
00139         PAINTSTRUCT ps;
00140         HDC hdc = BeginPaint(hWnd, &ps);
00141         RECT rc;
00142         GetClientRect(hWnd, &rc);
00143         FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
00144 
00145         // get our plugin instance object and ask it for the version string
00146         nsPluginInstance *plugin = (nsPluginInstance *)GetWindowLong(hWnd, GWL_USERDATA);
00147         if (plugin) {
00148           const char * string = plugin->getVersion();
00149           DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00150         }
00151         else {
00152           char string[] = "Error occured";
00153           DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00154         }
00155 
00156         EndPaint(hWnd, &ps);
00157       }
00158       break;
00159     default:
00160       break;
00161   }
00162 
00163   return DefWindowProc(hWnd, msg, wParam, lParam);
00164 }