1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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
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.
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 NPL, 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 NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
52
extern HINSTANCE hInst;
53
extern CLogger * pLogger;
55
/***********************************************/
57
/* CPlugin class implementation */
59
/***********************************************/
61
CPlugin::CPlugin(NPP pNPInstance, WORD wMode) :
62
CPluginBase(pNPInstance, wMode),
68
m_hWndStandAloneLogWindow(NULL),
69
m_bPluginReady(FALSE),
70
m_hWndLastEditFocus(NULL),
75
pLogger->setLogToFileFlag(m_Pref_bToFile);
76
pLogger->blockDumpToFile(FALSE);
84
static char szSection[] = SECTION_PREFERENCES;
85
static char szYes[] = ENTRY_YES;
86
static char szNo[] = ENTRY_NO;
88
void CPlugin::restorePreferences()
90
char szFileName[_MAX_PATH];
91
GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
94
XP_GetPrivateProfileString(szSection, KEY_AUTO_MODE, ENTRY_NO, sz, sizeof(sz), szFileName);
95
m_Pref_ShowGUI = (strcmpi(sz, ENTRY_YES) == 0) ? sg_auto : sg_manual;
97
XP_GetPrivateProfileString(szSection, KEY_LOG_FILE, "Test.log", sz, sizeof(sz), szFileName);
98
strcpy(m_Pref_szLogFile, sz);
100
XP_GetPrivateProfileString(szSection, KEY_SCRIPT_FILE, "Test.pts", sz, sizeof(sz), szFileName);
101
strcpy(m_Pref_szScriptFile, sz);
103
XP_GetPrivateProfileString(szSection, KEY_TO_FILE, ENTRY_NO, sz, sizeof(sz), szFileName);
104
m_Pref_bToFile = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
106
XP_GetPrivateProfileString(szSection, KEY_TO_FRAME, ENTRY_YES, sz, sizeof(sz), szFileName);
107
m_Pref_bToFrame = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
109
XP_GetPrivateProfileString(szSection, KEY_FLUSH_NOW, ENTRY_YES, sz, sizeof(sz), szFileName);
110
m_Pref_bFlushNow = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
112
XP_GetPrivateProfileString(szSection, KEY_REMEMBER_LAST_API_CALL, ENTRY_YES, sz, sizeof(sz), szFileName);
113
m_Pref_bRememberLastCall = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
115
XP_GetPrivateProfileString(szSection, KEY_STAND_ALONE, ENTRY_NO, sz, sizeof(sz), szFileName);
116
m_Pref_bStandAlone = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
118
XP_GetPrivateProfileString(szSection, KEY_AUTOSTART_SCRIPT, ENTRY_NO, sz, sizeof(sz), szFileName);
119
m_Pref_bAutoStartScript = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
122
void CPlugin::savePreferences()
124
char szFileName[_MAX_PATH];
125
GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
127
XP_WritePrivateProfileString(szSection, KEY_AUTO_MODE, (m_Pref_ShowGUI == sg_auto) ? szYes : szNo, szFileName);
128
XP_WritePrivateProfileString(szSection, KEY_LOG_FILE, m_Pref_szLogFile, szFileName);
129
XP_WritePrivateProfileString(szSection, KEY_SCRIPT_FILE, m_Pref_szScriptFile, szFileName);
130
XP_WritePrivateProfileString(szSection, KEY_TO_FILE, m_Pref_bToFile ? szYes : szNo, szFileName);
131
XP_WritePrivateProfileString(szSection, KEY_TO_FRAME, m_Pref_bToFrame ? szYes : szNo, szFileName);
132
XP_WritePrivateProfileString(szSection, KEY_FLUSH_NOW, m_Pref_bFlushNow ? szYes : szNo, szFileName);
133
XP_WritePrivateProfileString(szSection, KEY_REMEMBER_LAST_API_CALL, m_Pref_bRememberLastCall ? szYes : szNo, szFileName);
134
XP_WritePrivateProfileString(szSection, KEY_STAND_ALONE, m_Pref_bStandAlone ? szYes : szNo, szFileName);
135
XP_WritePrivateProfileString(szSection, KEY_AUTOSTART_SCRIPT, m_Pref_bAutoStartScript ? szYes : szNo, szFileName);
138
void CPlugin::updatePrefs(GUIPrefs prefs, int iValue, char * szValue)
143
m_Pref_ShowGUI = (ShowGUI)iValue;
146
if(szValue && (strlen(szValue) < sizeof(m_Pref_szLogFile)))
147
strcpy(m_Pref_szLogFile, szValue);
150
if(szValue && (strlen(szValue) < sizeof(m_Pref_szScriptFile)))
151
strcpy(m_Pref_szScriptFile, szValue);
154
m_Pref_bToFile = (BOOL)iValue;
157
m_Pref_bToFrame = (BOOL)iValue;
160
m_Pref_bFlushNow = (BOOL)iValue;
162
case gp_rememberlast:
163
m_Pref_bRememberLastCall = (BOOL)iValue;
166
m_Pref_bStandAlone = (BOOL)iValue;
168
case gp_autostartscript:
169
m_Pref_bAutoStartScript = (BOOL)iValue;
176
void CPlugin::getModulePath(LPSTR szPath, int iSize)
178
GetModulePath(m_hInst, szPath, iSize);
181
void CPlugin::getLogFileName(LPSTR szLogFileName, int iSize)
183
if(getMode() == NP_EMBED)
186
getModulePath(szLogFileName, iSize);
187
Edit_GetText(GetDlgItem(m_hWnd, IDC_EDIT_LOG_FILE_NAME), sz, sizeof(sz));
189
strcpy(sz, m_Pref_szLogFile);
190
strcat(szLogFileName, sz);
193
CPluginBase::getLogFileName(szLogFileName, iSize);
196
BOOL CALLBACK NP_LOADDS TesterDlgProc(HWND, UINT, WPARAM, LPARAM);
198
static char szStandAlonePluginWindowClassName[] = "StandAloneWindowClass";
200
BOOL CPlugin::initStandAlone()
202
HWND hWndParent = NULL;
204
// ensure window class
207
wc.lpfnWndProc = DefWindowProc;
210
wc.hInstance = hInst;
211
wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON_APP));
212
wc.hCursor = LoadCursor(0, IDC_ARROW);
213
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
214
wc.lpszMenuName = NULL;
215
wc.lpszClassName = szStandAlonePluginWindowClassName;
217
// just register the window class, if class already exists GetLastError()
218
// will return ERROR_CLASS_ALREADY_EXISTS, let's not care about it
221
hWndParent = CreateWindow(szStandAlonePluginWindowClassName,
223
WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX,
225
GetDesktopWindow(), NULL, m_hInst, NULL);
227
// m_hWndStandAloneLogWindow = CreateWindow("LISTBOX", "",
228
// WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL | LBS_NOINTEGRALHEIGHT,
230
// hWndParent, NULL, m_hInst, NULL);
232
m_hWndStandAloneLogWindow = CreateWindow("EDIT", "",
233
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_READONLY,
235
hWndParent, NULL, m_hInst, NULL);
236
if(!IsWindow(hWndParent))
239
m_hWndParent = hWndParent;
241
HFONT hFont = GetStockFont(DEFAULT_GUI_FONT);
242
SetWindowFont(m_hWndStandAloneLogWindow, hFont, FALSE);
244
CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_DIALOG_TESTER), m_hWndParent, (DLGPROC)TesterDlgProc, (LPARAM)this);
246
m_bPluginReady = (m_hWnd != NULL);
248
return m_bPluginReady;
251
void CPlugin::shutStandAlone()
253
// we don't care about unregistering window class, let it be in the system
255
if (isStandAlone() && m_hWndParent)
256
DestroyWindow(m_hWndParent);
258
m_bPluginReady = FALSE;
261
BOOL CPlugin::initEmbed(DWORD dwInitData)
266
HWND hWndParent = (HWND)dwInitData;
268
if(!IsWindow(hWndParent))
271
m_hWndParent = hWndParent;
273
CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_DIALOG_TESTER), m_hWndParent, (DLGPROC)TesterDlgProc, (LPARAM)this);
275
m_bPluginReady = (m_hWnd != NULL);
277
return m_bPluginReady;
280
BOOL CPlugin::initFull(DWORD dwInitData)
282
m_bPluginReady = CPluginBase::initFull(dwInitData);
283
return m_bPluginReady;
286
void CPlugin::shutEmbed()
290
DestroyWindow(m_hWnd);
294
m_bPluginReady = FALSE;
297
void CPlugin::shutFull()
299
CPluginBase::shutFull();
300
m_bPluginReady = FALSE;
303
BOOL CPlugin::isInitialized()
305
return m_bPluginReady;
308
void CPlugin::onInit(HWND hWnd, HWND hWndManual, HWND hWndAuto)
310
assert(hWnd != NULL);
311
assert(hWndManual != NULL);
312
assert(hWndAuto != NULL);
315
m_hWndManual = hWndManual;
316
m_hWndAuto = hWndAuto;
318
pLogger->setShowImmediatelyFlag(IsDlgButtonChecked(m_hWnd, IDC_CHECK_SHOW_LOG) == BST_CHECKED);
319
pLogger->setLogToFrameFlag(IsDlgButtonChecked(m_hWnd, IDC_CHECK_LOG_TO_FRAME) == BST_CHECKED);
320
pLogger->setLogToFileFlag(IsDlgButtonChecked(m_hWnd, IDC_CHECK_LOG_TO_FILE) == BST_CHECKED);
323
BOOL CPlugin::isStandAlone()
325
return m_Pref_bStandAlone;
328
void CPlugin::outputToNativeWindow(LPSTR szString)
330
if (!m_hWndStandAloneLogWindow)
333
// VERSION FOR EDIT BOX
335
static char text[16384];
337
if (strlen(szString)) {
338
int length = Edit_GetTextLength(m_hWndStandAloneLogWindow);
339
if ((length + strlen(szString)) > sizeof(text))
340
strcpy(text, szString);
342
Edit_GetText(m_hWndStandAloneLogWindow, text, sizeof(text));
343
strcat(text, szString);
346
Edit_SetText(m_hWndStandAloneLogWindow, text);
347
int lines = Edit_GetLineCount(m_hWndStandAloneLogWindow);
348
Edit_Scroll(m_hWndStandAloneLogWindow, lines, 0);
351
Edit_SetText(m_hWndStandAloneLogWindow, ""); // clear the window
354
// VERSION FOR LISTBOX
356
// parse the string and add lines to the listbox
358
char *newline = szString;
361
// could be either \r\n or \n, we don't need them at all for the listbox
362
p = strchr(newline, '\n');
366
if (*(p - 1) == '\r')
372
strcpy(line, newline);
374
ListBox_AddString(m_hWndStandAloneLogWindow, line);
375
int count = ListBox_GetCount(m_hWndStandAloneLogWindow);
377
ListBox_ResetContent(m_hWndStandAloneLogWindow);
378
ListBox_SetCaretIndex(m_hWndStandAloneLogWindow, count - 1);
379
UpdateWindow(m_hWndStandAloneLogWindow);
381
// restore the original
392
int CPlugin::messageBox(LPSTR szMessage, LPSTR szTitle, UINT uStyle)
394
return MessageBox(m_hWnd, szMessage, szTitle, uStyle);
397
void CPlugin::onDestroy()
402
void CPlugin::onLogToFile(BOOL bLogToFile)
404
pLogger->setLogToFileFlag(bLogToFile);
406
pLogger->closeLogToFile();
409
const HINSTANCE CPlugin::getInstance()
414
const HWND CPlugin::getWindow()
419
const HWND CPlugin::getParent()
424
void CPlugin::showGUI(ShowGUI sg)
429
ShowWindow(m_hWndManual, SW_SHOW);
430
ShowWindow(m_hWndAuto, SW_HIDE);
433
ShowWindow(m_hWndManual, SW_HIDE);
434
ShowWindow(m_hWndAuto, SW_SHOW);
442
DWORD CPlugin::makeNPNCall(NPAPI_Action action, DWORD dw1, DWORD dw2, DWORD dw3,
443
DWORD dw4, DWORD dw5, DWORD dw6, DWORD dw7)
445
DWORD dwRet = CPluginBase::makeNPNCall(action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
447
// update GUI for Manual mode action
448
if((getMode() == NP_EMBED) && (IsWindowVisible(m_hWndManual)))
452
case action_npn_new_stream:
453
case action_npn_destroy_stream:
454
case action_npn_mem_alloc:
455
case action_npn_mem_free:
456
updateUI(m_hWndManual);
466
void CPlugin::autoStartScriptIfNeeded()
468
if (!m_Pref_bAutoStartScript)
472
scripter.associate(this);
474
char szFileName[_MAX_PATH];
475
getModulePath(szFileName, sizeof(szFileName));
476
strcat(szFileName, m_Pref_szScriptFile);
478
if(scripter.createScriptFromFile(szFileName))
480
int iRepetitions = scripter.getCycleRepetitions();
481
int iDelay = scripter.getCycleDelay();
485
assert(pLogger != NULL);
486
pLogger->resetStartTime();
488
for(int i = 0; i < iRepetitions; i++)
490
scripter.executeScript();
497
MessageBox(NULL, "Script file not found or invalid", "", MB_OK | MB_ICONERROR);
501
// Creation and destruction
502
CPluginBase * CreatePlugin(NPP instance, uint16 mode)
504
CPlugin * pPlugin = new CPlugin(instance, mode);
505
return (CPluginBase *)pPlugin;
508
void DestroyPlugin(CPluginBase * pPlugin)
511
delete (CPlugin *)pPlugin;