~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/tools/tester/windows/plugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include <windows.h>
 
39
#include <windowsx.h>
 
40
#include <assert.h>
 
41
 
 
42
#include "resource.h"
 
43
 
 
44
#include "plugin.h"
 
45
#include "helpers.h"
 
46
#include "guihlp.h"
 
47
#include "logger.h"
 
48
#include "scripter.h"
 
49
#include "guiprefs.h"
 
50
#include "winutils.h"
 
51
 
 
52
extern HINSTANCE hInst;
 
53
extern CLogger * pLogger;
 
54
 
 
55
/***********************************************/
 
56
/*                                             */
 
57
/*       CPlugin class implementation          */
 
58
/*                                             */
 
59
/***********************************************/
 
60
 
 
61
CPlugin::CPlugin(NPP pNPInstance, WORD wMode) :
 
62
  CPluginBase(pNPInstance, wMode),
 
63
  m_hInst(hInst),
 
64
  m_hWnd(NULL),
 
65
  m_hWndManual(NULL),
 
66
  m_hWndAuto(NULL),
 
67
  m_hWndParent(NULL),
 
68
  m_hWndStandAloneLogWindow(NULL),
 
69
  m_bPluginReady(FALSE),
 
70
  m_hWndLastEditFocus(NULL),
 
71
  m_iWidth(200),
 
72
  m_iHeight(500)
 
73
{
 
74
  restorePreferences();
 
75
  pLogger->setLogToFileFlag(m_Pref_bToFile);
 
76
  pLogger->blockDumpToFile(FALSE);
 
77
}
 
78
 
 
79
CPlugin::~CPlugin()
 
80
{
 
81
  savePreferences();
 
82
}
 
83
 
 
84
static char szSection[] = SECTION_PREFERENCES;
 
85
static char szYes[] = ENTRY_YES;
 
86
static char szNo[] = ENTRY_NO;
 
87
 
 
88
void CPlugin::restorePreferences()
 
89
{
 
90
  char szFileName[_MAX_PATH];
 
91
  GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
 
92
 
 
93
  char sz[256];
 
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;
 
96
 
 
97
  XP_GetPrivateProfileString(szSection, KEY_LOG_FILE, "Test.log", sz, sizeof(sz), szFileName);
 
98
  strcpy(m_Pref_szLogFile, sz);
 
99
 
 
100
  XP_GetPrivateProfileString(szSection, KEY_SCRIPT_FILE, "Test.pts", sz, sizeof(sz), szFileName);
 
101
  strcpy(m_Pref_szScriptFile, sz);
 
102
 
 
103
  XP_GetPrivateProfileString(szSection, KEY_TO_FILE, ENTRY_NO, sz, sizeof(sz), szFileName);
 
104
  m_Pref_bToFile = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
 
105
 
 
106
  XP_GetPrivateProfileString(szSection, KEY_TO_FRAME, ENTRY_YES, sz, sizeof(sz), szFileName);
 
107
  m_Pref_bToFrame = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
 
108
 
 
109
  XP_GetPrivateProfileString(szSection, KEY_FLUSH_NOW, ENTRY_YES, sz, sizeof(sz), szFileName);
 
110
  m_Pref_bFlushNow = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
 
111
 
 
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;
 
114
 
 
115
  XP_GetPrivateProfileString(szSection, KEY_STAND_ALONE, ENTRY_NO, sz, sizeof(sz), szFileName);
 
116
  m_Pref_bStandAlone = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
 
117
 
 
118
  XP_GetPrivateProfileString(szSection, KEY_AUTOSTART_SCRIPT, ENTRY_NO, sz, sizeof(sz), szFileName);
 
119
  m_Pref_bAutoStartScript = (strcmpi(sz, ENTRY_YES) == 0) ? TRUE : FALSE;
 
120
}
 
121
 
 
122
void CPlugin::savePreferences()
 
123
{
 
124
  char szFileName[_MAX_PATH];
 
125
  GetINIFileName(m_hInst, szFileName, sizeof(szFileName));
 
126
 
 
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);
 
136
}
 
137
 
 
138
void CPlugin::updatePrefs(GUIPrefs prefs, int iValue, char * szValue)
 
139
{
 
140
  switch(prefs)
 
141
  {
 
142
    case gp_mode:
 
143
      m_Pref_ShowGUI = (ShowGUI)iValue;
 
144
      break;
 
145
    case gp_logfile:
 
146
      if(szValue && (strlen(szValue) < sizeof(m_Pref_szLogFile)))
 
147
        strcpy(m_Pref_szLogFile, szValue);
 
148
      break;
 
149
    case gp_scriptfile:
 
150
      if(szValue && (strlen(szValue) < sizeof(m_Pref_szScriptFile)))
 
151
        strcpy(m_Pref_szScriptFile, szValue);
 
152
      break;
 
153
    case gp_tofile:
 
154
      m_Pref_bToFile = (BOOL)iValue;
 
155
      break;
 
156
    case gp_toframe:
 
157
      m_Pref_bToFrame = (BOOL)iValue;
 
158
      break;
 
159
    case gp_flush:
 
160
      m_Pref_bFlushNow = (BOOL)iValue;
 
161
      break;
 
162
    case gp_rememberlast:
 
163
      m_Pref_bRememberLastCall = (BOOL)iValue;
 
164
      break;
 
165
    case gp_standalone:
 
166
      m_Pref_bStandAlone = (BOOL)iValue;
 
167
      break;
 
168
    case gp_autostartscript:
 
169
      m_Pref_bAutoStartScript = (BOOL)iValue;
 
170
      break;
 
171
    default:
 
172
      break;
 
173
  }
 
174
}
 
175
 
 
176
void CPlugin::getModulePath(LPSTR szPath, int iSize)
 
177
{
 
178
  GetModulePath(m_hInst, szPath, iSize);
 
179
}
 
180
 
 
181
void CPlugin::getLogFileName(LPSTR szLogFileName, int iSize)
 
182
{
 
183
  if(getMode() == NP_EMBED)
 
184
  {
 
185
    char sz[256];
 
186
    getModulePath(szLogFileName, iSize);
 
187
    Edit_GetText(GetDlgItem(m_hWnd, IDC_EDIT_LOG_FILE_NAME), sz, sizeof(sz));
 
188
    if(!strlen(sz))
 
189
      strcpy(sz, m_Pref_szLogFile);
 
190
    strcat(szLogFileName, sz);
 
191
  }
 
192
  else
 
193
    CPluginBase::getLogFileName(szLogFileName, iSize);
 
194
}
 
195
 
 
196
BOOL CALLBACK NP_LOADDS TesterDlgProc(HWND, UINT, WPARAM, LPARAM);
 
197
 
 
198
static char szStandAlonePluginWindowClassName[] = "StandAloneWindowClass";
 
199
 
 
200
BOOL CPlugin::initStandAlone()
 
201
{
 
202
  HWND hWndParent = NULL;
 
203
 
 
204
  // ensure window class
 
205
  WNDCLASS wc;
 
206
  wc.style         = 0; 
 
207
  wc.lpfnWndProc   = DefWindowProc; 
 
208
  wc.cbClsExtra    = 0; 
 
209
  wc.cbWndExtra    = 0; 
 
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;
 
216
 
 
217
  // just register the window class, if class already exists GetLastError() 
 
218
  // will return ERROR_CLASS_ALREADY_EXISTS, let's not care about it
 
219
  RegisterClass(&wc);
 
220
 
 
221
  hWndParent = CreateWindow(szStandAlonePluginWindowClassName, 
 
222
                            "The Tester Plugin", 
 
223
                            WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX, 
 
224
                            0, 0, 800, 600, 
 
225
                            GetDesktopWindow(), NULL, m_hInst, NULL);
 
226
 
 
227
//  m_hWndStandAloneLogWindow = CreateWindow("LISTBOX", "", 
 
228
  //                                          WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL | LBS_NOINTEGRALHEIGHT, 
 
229
    //                                        200, 3, 590, 562, 
 
230
      //                                      hWndParent, NULL, m_hInst, NULL);
 
231
 
 
232
  m_hWndStandAloneLogWindow = CreateWindow("EDIT", "", 
 
233
                                            WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_READONLY,
 
234
                                            200, 3, 590, 562, 
 
235
                                            hWndParent, NULL, m_hInst, NULL);
 
236
  if(!IsWindow(hWndParent))
 
237
    return FALSE;
 
238
 
 
239
  m_hWndParent = hWndParent;
 
240
 
 
241
  HFONT hFont = GetStockFont(DEFAULT_GUI_FONT);
 
242
  SetWindowFont(m_hWndStandAloneLogWindow, hFont, FALSE);
 
243
 
 
244
  CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_DIALOG_TESTER), m_hWndParent, (DLGPROC)TesterDlgProc, (LPARAM)this);
 
245
 
 
246
  m_bPluginReady = (m_hWnd != NULL);
 
247
 
 
248
  return m_bPluginReady;
 
249
}
 
250
 
 
251
void CPlugin::shutStandAlone()
 
252
{
 
253
  // we don't care  about unregistering window class, let it be in the system
 
254
 
 
255
  if (isStandAlone() && m_hWndParent) 
 
256
    DestroyWindow(m_hWndParent);
 
257
 
 
258
  m_bPluginReady = FALSE;
 
259
}
 
260
 
 
261
BOOL CPlugin::initEmbed(DWORD dwInitData)
 
262
{
 
263
  if (m_bPluginReady)
 
264
    return TRUE;
 
265
 
 
266
  HWND hWndParent = (HWND)dwInitData;
 
267
 
 
268
  if(!IsWindow(hWndParent))
 
269
    return FALSE;
 
270
 
 
271
  m_hWndParent = hWndParent;
 
272
 
 
273
  CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_DIALOG_TESTER), m_hWndParent, (DLGPROC)TesterDlgProc, (LPARAM)this);
 
274
 
 
275
  m_bPluginReady = (m_hWnd != NULL);
 
276
 
 
277
  return m_bPluginReady;
 
278
}
 
279
 
 
280
BOOL CPlugin::initFull(DWORD dwInitData)
 
281
{
 
282
  m_bPluginReady = CPluginBase::initFull(dwInitData);
 
283
  return m_bPluginReady;
 
284
}
 
285
 
 
286
void CPlugin::shutEmbed()
 
287
{
 
288
  if(m_hWnd != NULL)
 
289
  {
 
290
    DestroyWindow(m_hWnd);
 
291
    m_hWnd = NULL;
 
292
  }
 
293
 
 
294
  m_bPluginReady = FALSE;
 
295
}
 
296
 
 
297
void CPlugin::shutFull()
 
298
{
 
299
  CPluginBase::shutFull();
 
300
  m_bPluginReady = FALSE;
 
301
}
 
302
 
 
303
BOOL CPlugin::isInitialized()
 
304
{
 
305
  return m_bPluginReady;
 
306
}
 
307
 
 
308
void CPlugin::onInit(HWND hWnd, HWND hWndManual, HWND hWndAuto)
 
309
{
 
310
  assert(hWnd != NULL);
 
311
  assert(hWndManual != NULL);
 
312
  assert(hWndAuto != NULL);
 
313
 
 
314
  m_hWnd = hWnd;
 
315
  m_hWndManual = hWndManual;
 
316
  m_hWndAuto = hWndAuto;
 
317
 
 
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);
 
321
}
 
322
 
 
323
BOOL CPlugin::isStandAlone()
 
324
{
 
325
  return m_Pref_bStandAlone;
 
326
}
 
327
 
 
328
void CPlugin::outputToNativeWindow(LPSTR szString)
 
329
{
 
330
  if (!m_hWndStandAloneLogWindow)
 
331
    return;
 
332
 
 
333
  // VERSION FOR EDIT BOX
 
334
 
 
335
  static char text[16384];
 
336
 
 
337
  if (strlen(szString)) {
 
338
    int length = Edit_GetTextLength(m_hWndStandAloneLogWindow);
 
339
    if ((length + strlen(szString)) > sizeof(text))
 
340
      strcpy(text, szString);
 
341
    else {
 
342
      Edit_GetText(m_hWndStandAloneLogWindow, text, sizeof(text));
 
343
      strcat(text, szString);
 
344
    }
 
345
 
 
346
    Edit_SetText(m_hWndStandAloneLogWindow, text);
 
347
    int lines = Edit_GetLineCount(m_hWndStandAloneLogWindow);
 
348
    Edit_Scroll(m_hWndStandAloneLogWindow, lines, 0);
 
349
  }
 
350
  else
 
351
    Edit_SetText(m_hWndStandAloneLogWindow, ""); // clear the window
 
352
 
 
353
/*
 
354
  // VERSION FOR LISTBOX
 
355
 
 
356
  // parse the string and add lines to the listbox
 
357
  char *p = 0;
 
358
  char *newline = szString;
 
359
 
 
360
  for (;;) {
 
361
    // could be either \r\n or \n, we don't need them at all for the listbox
 
362
    p = strchr(newline, '\n');
 
363
    if (!p)
 
364
      break;
 
365
 
 
366
    if (*(p - 1) == '\r')
 
367
      *(p - 1) = '\0';
 
368
    else
 
369
      *p = '\0';
 
370
 
 
371
    char line[512];
 
372
    strcpy(line, newline);
 
373
 
 
374
    ListBox_AddString(m_hWndStandAloneLogWindow, line);
 
375
    int count = ListBox_GetCount(m_hWndStandAloneLogWindow);
 
376
    if(count == 32767)
 
377
      ListBox_ResetContent(m_hWndStandAloneLogWindow);
 
378
    ListBox_SetCaretIndex(m_hWndStandAloneLogWindow, count - 1);
 
379
    UpdateWindow(m_hWndStandAloneLogWindow);
 
380
 
 
381
    // restore the original
 
382
    if (*p == '\n')
 
383
      *(p - 1) = '\r';
 
384
    else
 
385
      *p = '\n';
 
386
 
 
387
    newline = ++p;
 
388
  }
 
389
*/
 
390
}
 
391
 
 
392
int CPlugin::messageBox(LPSTR szMessage, LPSTR szTitle, UINT uStyle)
 
393
{
 
394
  return MessageBox(m_hWnd, szMessage, szTitle, uStyle);
 
395
}
 
396
 
 
397
void CPlugin::onDestroy()
 
398
{
 
399
  m_hWnd = NULL;
 
400
}
 
401
 
 
402
void CPlugin::onLogToFile(BOOL bLogToFile)
 
403
{
 
404
  pLogger->setLogToFileFlag(bLogToFile);
 
405
  if(!bLogToFile)
 
406
    pLogger->closeLogToFile();
 
407
}
 
408
 
 
409
const HINSTANCE CPlugin::getInstance()
 
410
{
 
411
  return m_hInst;
 
412
}
 
413
 
 
414
const HWND CPlugin::getWindow()
 
415
{
 
416
  return m_hWnd;
 
417
}
 
418
 
 
419
const HWND CPlugin::getParent()
 
420
{
 
421
  return m_hWndParent;
 
422
}
 
423
 
 
424
void CPlugin::showGUI(ShowGUI sg)
 
425
{
 
426
  switch (sg)
 
427
  {
 
428
    case sg_manual:
 
429
      ShowWindow(m_hWndManual, SW_SHOW);
 
430
      ShowWindow(m_hWndAuto, SW_HIDE);
 
431
      break;
 
432
    case sg_auto:
 
433
      ShowWindow(m_hWndManual, SW_HIDE);
 
434
      ShowWindow(m_hWndAuto, SW_SHOW);
 
435
      break;
 
436
    default:
 
437
      assert(0);
 
438
      break;
 
439
  }
 
440
}
 
441
 
 
442
DWORD CPlugin::makeNPNCall(NPAPI_Action action, DWORD dw1, DWORD dw2, DWORD dw3, 
 
443
                           DWORD dw4, DWORD dw5, DWORD dw6, DWORD dw7)
 
444
{
 
445
  DWORD dwRet = CPluginBase::makeNPNCall(action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
 
446
 
 
447
    // update GUI for Manual mode action
 
448
  if((getMode() == NP_EMBED) && (IsWindowVisible(m_hWndManual)))
 
449
  {
 
450
    switch (action)
 
451
    {
 
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);
 
457
        break;
 
458
      default:
 
459
        break;
 
460
    }
 
461
  }
 
462
 
 
463
  return dwRet;
 
464
}
 
465
 
 
466
void CPlugin::autoStartScriptIfNeeded()
 
467
{
 
468
  if (!m_Pref_bAutoStartScript)
 
469
    return;
 
470
 
 
471
  CScripter scripter;
 
472
  scripter.associate(this);
 
473
 
 
474
  char szFileName[_MAX_PATH];
 
475
  getModulePath(szFileName, sizeof(szFileName));
 
476
  strcat(szFileName, m_Pref_szScriptFile);
 
477
 
 
478
  if(scripter.createScriptFromFile(szFileName))
 
479
  {
 
480
    int iRepetitions = scripter.getCycleRepetitions();
 
481
    int iDelay = scripter.getCycleDelay();
 
482
    if(iDelay < 0)
 
483
      iDelay = 0;
 
484
 
 
485
    assert(pLogger != NULL);
 
486
    pLogger->resetStartTime();
 
487
 
 
488
    for(int i = 0; i < iRepetitions; i++)
 
489
    {
 
490
      scripter.executeScript();
 
491
      if(iDelay != 0)
 
492
        XP_Sleep(iDelay);
 
493
    }
 
494
  }
 
495
  else
 
496
  {
 
497
    MessageBox(NULL, "Script file not found or invalid", "", MB_OK | MB_ICONERROR);
 
498
  }
 
499
}
 
500
 
 
501
// Creation and destruction
 
502
CPluginBase * CreatePlugin(NPP instance, uint16 mode)
 
503
{
 
504
  CPlugin * pPlugin = new CPlugin(instance, mode);
 
505
  return (CPluginBase *)pPlugin;
 
506
}
 
507
 
 
508
void DestroyPlugin(CPluginBase * pPlugin)
 
509
{
 
510
  if(pPlugin != NULL)
 
511
    delete (CPlugin *)pPlugin;
 
512
}