Back to index

lightning-sunbird  0.9+nobinonly
plugin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 
00039 //
00040 // CPlugin class implementation
00041 //
00042 #ifdef XP_WIN
00043 #include <windows.h>
00044 #include <windowsx.h>
00045 #endif
00046 
00047 #ifdef XP_MAC
00048 #include <TextEdit.h>
00049 #endif
00050 
00051 #include "plugin.h"
00052 
00053 CPlugin::CPlugin(NPP pNPInstance) :
00054   m_pNPInstance(pNPInstance),
00055   m_pNPStream(NULL),
00056   m_bInitialized(FALSE),
00057   m_pScriptablePeer(NULL)
00058 {
00059 #ifdef XP_WIN
00060   m_hWnd = NULL;
00061 #endif
00062 
00063   const char *ua = NPN_UserAgent(m_pNPInstance);
00064   strcpy(m_String, ua);
00065 }
00066 
00067 CPlugin::~CPlugin()
00068 {
00069   NS_IF_RELEASE(m_pScriptablePeer);
00070 }
00071 
00072 #ifdef XP_WIN
00073 static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
00074 static WNDPROC lpOldProc = NULL;
00075 #endif
00076 
00077 NPBool CPlugin::init(NPWindow* pNPWindow)
00078 {
00079   if(pNPWindow == NULL)
00080     return FALSE;
00081 
00082 #ifdef XP_WIN
00083   m_hWnd = (HWND)pNPWindow->window;
00084   if(m_hWnd == NULL)
00085     return FALSE;
00086 
00087   // subclass window so we can intercept window messages and
00088   // do our drawing to it
00089   lpOldProc = SubclassWindow(m_hWnd, (WNDPROC)PluginWinProc);
00090 
00091   // associate window with our CPlugin object so we can access 
00092   // it in the window procedure
00093   SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);
00094 #endif
00095 
00096   m_Window = pNPWindow;
00097 
00098   m_bInitialized = TRUE;
00099   return TRUE;
00100 }
00101 
00102 void CPlugin::shut()
00103 {
00104 #ifdef XP_WIN
00105   // subclass it back
00106   SubclassWindow(m_hWnd, lpOldProc);
00107   m_hWnd = NULL;
00108 #endif
00109 
00110   m_bInitialized = FALSE;
00111 }
00112 
00113 NPBool CPlugin::isInitialized()
00114 {
00115   return m_bInitialized;
00116 }
00117 
00118 int16 CPlugin::handleEvent(void* event)
00119 {
00120 #ifdef XP_MAC
00121     NPEvent* ev = (NPEvent*)event;
00122     if (m_Window) {
00123         Rect box = { m_Window->y, m_Window->x,
00124                      m_Window->y + m_Window->height, m_Window->x + m_Window->width };
00125         if (ev->what == updateEvt) {
00126             ::TETextBox(m_String, strlen(m_String), &box, teJustCenter);
00127         }
00128     }
00129 #endif
00130     return 0;
00131 }
00132 
00133 // this will force to draw a version string in the plugin window
00134 void CPlugin::showVersion()
00135 {
00136   const char *ua = NPN_UserAgent(m_pNPInstance);
00137   strcpy(m_String, ua);
00138 
00139 #ifdef XP_WIN
00140   InvalidateRect(m_hWnd, NULL, TRUE);
00141   UpdateWindow(m_hWnd);
00142 #endif
00143 
00144   if (m_Window) {
00145     NPRect r = { m_Window->y, m_Window->x,
00146                  m_Window->y + m_Window->height, m_Window->x + m_Window->width };
00147     NPN_InvalidateRect(m_pNPInstance, &r);
00148   }
00149 }
00150 
00151 // this will clean the plugin window
00152 void CPlugin::clear()
00153 {
00154   strcpy(m_String, "");
00155 
00156 #ifdef XP_WIN
00157   InvalidateRect(m_hWnd, NULL, TRUE);
00158   UpdateWindow(m_hWnd);
00159 #endif
00160 }
00161 
00162 void CPlugin::getVersion(char* *aVersion)
00163 {
00164   const char *ua = NPN_UserAgent(m_pNPInstance);
00165   char*& version = *aVersion;
00166   version = (char*)NPN_MemAlloc(1 + strlen(ua));
00167   if (version)
00168     strcpy(version, ua);
00169 }
00170 
00171 // ==============================
00172 // ! Scriptability related code !
00173 // ==============================
00174 //
00175 // this method will return the scriptable object (and create it if necessary)
00176 nsI4xScriptablePlugin* CPlugin::getScriptablePeer()
00177 {
00178   if (!m_pScriptablePeer) {
00179     m_pScriptablePeer = new nsScriptablePeer(this);
00180     if(!m_pScriptablePeer)
00181       return NULL;
00182 
00183     NS_ADDREF(m_pScriptablePeer);
00184   }
00185 
00186   // add reference for the caller requesting the object
00187   NS_ADDREF(m_pScriptablePeer);
00188   return m_pScriptablePeer;
00189 }
00190 
00191 #ifdef XP_WIN
00192 static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00193 {
00194   switch (msg) {
00195     case WM_PAINT:
00196       {
00197         // draw a frame and display the string
00198         PAINTSTRUCT ps;
00199         HDC hdc = BeginPaint(hWnd, &ps);
00200         RECT rc;
00201         GetClientRect(hWnd, &rc);
00202         FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
00203         CPlugin * p = (CPlugin *)GetWindowLong(hWnd, GWL_USERDATA);
00204         if(p)
00205           DrawText(hdc, p->m_String, strlen(p->m_String), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00206         EndPaint(hWnd, &ps);
00207       }
00208       break;
00209     default:
00210       break;
00211   }
00212 
00213   return DefWindowProc(hWnd, msg, wParam, lParam);
00214 }
00215 #endif