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 
00038 #define INCL_WIN
00039 #include <os2.h>
00040 #include <assert.h>
00041 
00042 #include "resource.h"
00043 
00044 #include "plugin.h"
00045 #include "helpers.h"
00046 #include "guihlp.h"
00047 #include "logger.h"
00048 #include "guiprefs.h"
00049 #include "os2utils.h"
00050 
00051 extern HMODULE hInst;
00052 extern CLogger * pLogger;
00053 
00054 /***********************************************/
00055 /*                                             */
00056 /*       CPlugin class implementation          */
00057 /*                                             */
00058 /***********************************************/
00059 
00060 CPlugin::CPlugin(NPP pNPInstance, USHORT wMode) :
00061   CPluginBase(pNPInstance, wMode),
00062   m_hInst(hInst),
00063   m_hWnd(NULL),
00064   m_hWndManual(NULL),
00065   m_hWndAuto(NULL),
00066   m_hWndParent(NULL),
00067   m_bPluginReady(FALSE),
00068   m_hWndLastEditFocus(NULL),
00069   m_iWidth(200),
00070   m_iHeight(500)
00071 {
00072 }
00073 
00074 CPlugin::~CPlugin()
00075 {
00076 }
00077 
00078 static char szSection[] = SECTION_PREFERENCES;
00079 static char szYes[] = ENTRY_YES;
00080 static char szNo[] = ENTRY_NO;
00081 
00082 void CPlugin::restorePreferences()
00083 {
00084   char szFileName[_MAX_PATH];
00085   GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
00086 
00087   char sz[256];
00088   XP_GetPrivateProfileString(szSection, KEY_AUTO_MODE, ENTRY_NO, sz, sizeof(sz), szFileName);
00089   m_Pref_ShowGUI = (stricmp(sz, ENTRY_YES) == 0) ? sg_auto : sg_manual;
00090 
00091   XP_GetPrivateProfileString(szSection, KEY_LOG_FILE, "Test.log", sz, sizeof(sz), szFileName);
00092   strcpy(m_Pref_szLogFile, sz);
00093 
00094   XP_GetPrivateProfileString(szSection, KEY_SCRIPT_FILE, "Test.pts", sz, sizeof(sz), szFileName);
00095   strcpy(m_Pref_szScriptFile, sz);
00096 
00097   XP_GetPrivateProfileString(szSection, KEY_TO_FILE, ENTRY_NO, sz, sizeof(sz), szFileName);
00098   m_Pref_bToFile = (stricmp(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
00099 
00100   XP_GetPrivateProfileString(szSection, KEY_TO_FRAME, ENTRY_YES, sz, sizeof(sz), szFileName);
00101   m_Pref_bToFrame = (stricmp(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
00102 
00103   XP_GetPrivateProfileString(szSection, KEY_FLUSH_NOW, ENTRY_YES, sz, sizeof(sz), szFileName);
00104   m_Pref_bFlushNow = (stricmp(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
00105 
00106   XP_GetPrivateProfileString(szSection, KEY_REMEMBER_LAST_API_CALL, ENTRY_YES, sz, sizeof(sz), szFileName);
00107   m_Pref_bRememberLastCall = (stricmp(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
00108 
00109   XP_GetPrivateProfileString(szSection, KEY_STAND_ALONE, ENTRY_NO, sz, sizeof(sz), szFileName);
00110   m_Pref_bStandAlone = (stricmp(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
00111 }
00112 
00113 void CPlugin::savePreferences()
00114 {
00115   char szFileName[_MAX_PATH];
00116   GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
00117 
00118   XP_WritePrivateProfileString(szSection, KEY_AUTO_MODE, (m_Pref_ShowGUI == sg_auto) ? szYes : szNo, szFileName);
00119   XP_WritePrivateProfileString(szSection, KEY_LOG_FILE, m_Pref_szLogFile, szFileName);
00120   XP_WritePrivateProfileString(szSection, KEY_SCRIPT_FILE, m_Pref_szScriptFile, szFileName);
00121   XP_WritePrivateProfileString(szSection, KEY_TO_FILE, m_Pref_bToFile ? szYes : szNo, szFileName);
00122   XP_WritePrivateProfileString(szSection, KEY_TO_FRAME, m_Pref_bToFrame ? szYes : szNo, szFileName);
00123   XP_WritePrivateProfileString(szSection, KEY_FLUSH_NOW, m_Pref_bFlushNow ? szYes : szNo, szFileName);
00124   XP_WritePrivateProfileString(szSection, KEY_REMEMBER_LAST_API_CALL, m_Pref_bRememberLastCall ? szYes : szNo, szFileName);
00125   XP_WritePrivateProfileString(szSection, KEY_STAND_ALONE, m_Pref_bStandAlone ? szYes : szNo, szFileName);
00126 }
00127 
00128 void CPlugin::updatePrefs(GUIPrefs prefs, int iValue, char * szValue)
00129 {
00130   switch(prefs)
00131   {
00132     case gp_mode:
00133       m_Pref_ShowGUI = (ShowGUI)iValue;
00134       break;
00135     case gp_logfile:
00136       if(szValue && (strlen(szValue) < sizeof(m_Pref_szLogFile)))
00137         strcpy(m_Pref_szLogFile, szValue);
00138       break;
00139     case gp_scriptfile:
00140       if(szValue && (strlen(szValue) < sizeof(m_Pref_szScriptFile)))
00141         strcpy(m_Pref_szScriptFile, szValue);
00142       break;
00143     case gp_tofile:
00144       m_Pref_bToFile = (BOOL)iValue;
00145       break;
00146     case gp_toframe:
00147       m_Pref_bToFrame = (BOOL)iValue;
00148       break;
00149     case gp_flush:
00150       m_Pref_bFlushNow = (BOOL)iValue;
00151       break;
00152     case gp_rememberlast:
00153       m_Pref_bRememberLastCall = (BOOL)iValue;
00154       break;
00155     case gp_standalone:
00156       m_Pref_bStandAlone = (BOOL)iValue;
00157       break;
00158     default:
00159       break;
00160   }
00161 }
00162 
00163 void CPlugin::getModulePath(PSZ szPath, int iSize)
00164 {
00165   GetModulePath(m_hInst, szPath, iSize);
00166 }
00167 
00168 void CPlugin::getLogFileName(PSZ szLogFileName, int iSize)
00169 {
00170   if(getMode() == NP_EMBED)
00171   {
00172     char sz[256];
00173     getModulePath(szLogFileName, iSize);
00174     WinQueryWindowText(WinWindowFromID(m_hWnd, IDC_EDIT_LOG_FILE_NAME), sizeof(sz), sz);
00175     strcat(szLogFileName, sz);
00176   }
00177   else
00178     CPluginBase::getLogFileName(szLogFileName, iSize);
00179 }
00180 
00181 MRESULT EXPENTRY NP_LOADDS TesterDlgProc(HWND, ULONG, MPARAM, MPARAM);
00182 
00183 BOOL CPlugin::initEmbed(ULONG dwInitData)
00184 {
00185   restorePreferences();
00186 
00187   HWND hWndParent = (HWND)dwInitData;
00188 
00189   if(WinIsWindow((HAB)0, hWndParent))
00190     m_hWndParent = hWndParent;
00191 
00192   WinLoadDlg(m_hWndParent, m_hWndParent, (PFNWP)TesterDlgProc, m_hInst, IDD_DIALOG_TESTER, (PVOID)this);
00193 
00194   m_bPluginReady = (m_hWnd != NULL);
00195 
00196   return m_bPluginReady;
00197 }
00198 
00199 BOOL CPlugin::initFull(ULONG dwInitData)
00200 {
00201   m_bPluginReady = CPluginBase::initFull(dwInitData);
00202   return m_bPluginReady;
00203 }
00204 
00205 void CPlugin::shutEmbed()
00206 {
00207   savePreferences();
00208 
00209   if(m_hWnd != NULL)
00210   {
00211     WinDismissDlg(m_hWnd, IDD_DIALOG_TESTER); //WinDestroyWindow(m_hWnd);
00212     m_hWnd = NULL;
00213   }
00214 
00215   m_bPluginReady = FALSE;
00216 }
00217 
00218 void CPlugin::shutFull()
00219 {
00220   CPluginBase::shutFull();
00221   m_bPluginReady = FALSE;
00222 }
00223 
00224 BOOL CPlugin::isInitialized()
00225 {
00226   return m_bPluginReady;
00227 }
00228 
00229 void CPlugin::onInit(HWND hWnd, HWND hWndManual, HWND hWndAuto)
00230 {
00231   assert(hWnd != NULL);
00232   assert(hWndManual != NULL);
00233   assert(hWndAuto != NULL);
00234 
00235   m_hWnd = hWnd;
00236   m_hWndManual = hWndManual;
00237   m_hWndAuto = hWndAuto;
00238 
00239   pLogger->setShowImmediatelyFlag(WinQueryButtonCheckstate(m_hWnd, IDC_CHECK_SHOW_LOG) == BST_CHECKED);
00240   pLogger->setLogToFrameFlag(WinQueryButtonCheckstate(m_hWnd, IDC_CHECK_LOG_TO_FRAME) == BST_CHECKED);
00241   pLogger->setLogToFileFlag(WinQueryButtonCheckstate(m_hWnd, IDC_CHECK_LOG_TO_FILE) == BST_CHECKED);
00242 }
00243 
00244 int CPlugin::messageBox(LPSTR szMessage, LPSTR szTitle, UINT uStyle)
00245 {
00246   return WinMessageBox(HWND_DESKTOP, m_hWnd, szMessage, szTitle, 0, uStyle);
00247 }
00248 
00249 BOOL CPlugin::isStandAlone()
00250 {
00251   return FALSE;
00252 }
00253 
00254 BOOL CPlugin::initStandAlone()
00255 {
00256   return FALSE;
00257 }
00258 
00259 void CPlugin::shutStandAlone()
00260 {
00261 }
00262 
00263 void CPlugin::outputToNativeWindow(LPSTR szString)
00264 {
00265 }
00266 
00267 void CPlugin::onDestroy()
00268 {
00269   m_hWnd = NULL;
00270 }
00271 
00272 void CPlugin::onLogToFile(BOOL bLogToFile)
00273 {
00274   pLogger->setLogToFileFlag(bLogToFile);
00275   if(!bLogToFile)
00276     pLogger->closeLogToFile();
00277 }
00278 
00279 const HMODULE CPlugin::getInstance()
00280 {
00281   return m_hInst;
00282 }
00283 
00284 const HWND CPlugin::getWindow()
00285 {
00286   return m_hWnd;
00287 }
00288 
00289 const HWND CPlugin::getParent()
00290 {
00291   return m_hWndParent;
00292 }
00293 
00294 void CPlugin::showGUI(ShowGUI sg)
00295 {
00296   switch (sg)
00297   {
00298     case sg_manual:
00299       WinShowWindow(m_hWndManual, TRUE);
00300       WinShowWindow(m_hWndAuto, FALSE);
00301       break;
00302     case sg_auto:
00303       WinShowWindow(m_hWndManual, FALSE);
00304       WinShowWindow(m_hWndAuto, TRUE);
00305       break;
00306     default:
00307       assert(0);
00308       break;
00309   }
00310 }
00311 
00312 ULONG CPlugin::makeNPNCall(NPAPI_Action action, ULONG dw1, ULONG dw2, ULONG dw3, 
00313                            ULONG dw4, ULONG dw5, ULONG dw6, ULONG dw7)
00314 {
00315   ULONG dwRet = CPluginBase::makeNPNCall(action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
00316 
00317     // update GUI for Manual mode action
00318   if((getMode() == NP_EMBED) && (WinIsWindowVisible(m_hWndManual)))
00319   {
00320     switch (action)
00321     {
00322       case action_npn_new_stream:
00323       case action_npn_destroy_stream:
00324       case action_npn_mem_alloc:
00325       case action_npn_mem_free:
00326         updateUI(m_hWndManual);
00327         break;
00328       default:
00329         break;
00330     }
00331   }
00332 
00333   return dwRet;
00334 }
00335 
00336 // Creation and destruction
00337 CPluginBase * CreatePlugin(NPP instance, uint16 mode) 
00338 {
00339   CPlugin * pPlugin = new CPlugin(instance, mode);
00340   return (CPluginBase *)pPlugin;
00341 }
00342 
00343 void DestroyPlugin(CPluginBase * pPlugin)
00344 {
00345   if(pPlugin != NULL)
00346     delete (CPlugin *)pPlugin;
00347 }