gecko-dev/webshell/tests/viewer/winmain.cpp

347 lines
9.6 KiB
C++
Raw Normal View History

1998-04-13 20:24:54 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
// Win32 specific viewer driver
1998-04-13 20:24:54 +00:00
#include <windows.h>
1998-05-22 20:11:14 +00:00
#include <crtdbg.h>
1998-04-13 20:24:54 +00:00
#include "resources.h"
#include "jsconsres.h"
#include "JSConsole.h"
#include "nsViewer.h"
1998-04-13 20:24:54 +00:00
#include "nsGlobalVariables.h"
#include "nsIDocument.h"
#include "nsIURL.h"
1998-04-13 20:24:54 +00:00
#include "nsVoidArray.h"
#include "nsCRT.h"
1998-05-19 19:06:59 +00:00
#include "prenv.h"
1998-04-13 20:24:54 +00:00
#include "nsIScriptContext.h"
1998-05-19 19:06:59 +00:00
// Debug Robot options
static int gDebugRobotLoads = 5000;
static char gVerifyDir[_MAX_PATH];
static BOOL gVisualDebug = TRUE;
// DebugRobot call
extern "C" NS_EXPORT int DebugRobot(
nsVoidArray * workList, nsIWebWidget * ww, int imax, char * verify_dir, void (*yieldProc)(const char *));
1998-05-19 19:06:59 +00:00
1998-04-13 20:24:54 +00:00
// Temporary Netlib stuff...
/* XXX: Don't include net.h... */
extern "C" {
extern int NET_PollSockets();
};
#define DEBUG_EMPTY "(none)"
1998-04-13 20:24:54 +00:00
class nsWin32Viewer : public nsViewer {
// From nsViewer
public:
virtual void AddMenu(nsIWidget* aMainWindow);
virtual void ShowConsole(WindowData* aWindata);
virtual void DoDebugRobot(WindowData* aWindata);
virtual void CopySelection(WindowData* aWindata);
virtual void Destroy(WindowData* wd);
virtual void CloseConsole();
virtual void Stop();
virtual void CrtSetDebug(PRUint32 aNewFlags);
// Utilities
virtual void CopyTextContent(WindowData* wd, HWND aHWnd);
1998-04-13 20:24:54 +00:00
};
static HANDLE gInstance, gPrevInstance;
1998-04-13 20:24:54 +00:00
//-----------------------------------------------------------------
// JSConsole support
//-----------------------------------------------------------------
1998-04-13 20:24:54 +00:00
// JSConsole window
JSConsole *gConsole = NULL;
1998-04-13 20:24:54 +00:00
static char* class1Name = "Viewer";
1998-04-13 20:24:54 +00:00
void DestroyConsole()
1998-04-13 20:24:54 +00:00
{
if (gConsole) {
gConsole->SetNotification(NULL);
delete gConsole;
gConsole = NULL;
1998-05-08 15:06:41 +00:00
}
1998-04-13 20:24:54 +00:00
}
//-----------------------------------------------------------------
// CRT Debug
//-----------------------------------------------------------------
void nsWin32Viewer::CrtSetDebug(PRUint32 aNewFlags)
1998-04-13 20:24:54 +00:00
{
int oldFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
_CrtSetDbgFlag(aNewFlags);
printf("Note: crt flags: old=%x new=%x\n", oldFlags, aNewFlags);
1998-04-13 20:24:54 +00:00
}
//-----------------------------------------------------------------
// Debug Robot support
//-----------------------------------------------------------------
void yieldProc(const char * str)
{
// Process messages
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
GetMessage(&msg, NULL, 0, 0);
if (!JSConsole::sAccelTable ||
!gConsole ||
!gConsole->GetMainWindow() ||
!TranslateAccelerator(gConsole->GetMainWindow(), JSConsole::sAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
/* Pump Netlib... */
NET_PollSockets();
}
}
}
/* Debug Robot Dialog options */
BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam,LPARAM lParam)
{
BOOL translated = FALSE;
HWND hwnd;
switch (msg)
{
case WM_INITDIALOG:
{
SetDlgItemInt(hDlg,IDC_PAGE_LOADS,5000,FALSE);
char * text = PR_GetEnv("VERIFY_PARSER");
SetDlgItemText(hDlg,IDC_VERIFICATION_DIRECTORY,text ? text : DEBUG_EMPTY);
hwnd = GetDlgItem(hDlg,IDC_UPDATE_DISPLAY);
SendMessage(hwnd,BM_SETCHECK,TRUE,0);
}
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
gDebugRobotLoads = GetDlgItemInt(hDlg,IDC_PAGE_LOADS,&translated,FALSE);
GetDlgItemText(hDlg, IDC_VERIFICATION_DIRECTORY, gVerifyDir, sizeof(gVerifyDir));
if (!strcmp(gVerifyDir,DEBUG_EMPTY))
gVerifyDir[0] = '\0';
hwnd = GetDlgItem(hDlg,IDC_UPDATE_DISPLAY);
gVisualDebug = (BOOL)SendMessage(hwnd,BM_GETCHECK,0,0);
EndDialog(hDlg,IDOK);
break;
case IDCANCEL:
EndDialog(hDlg,IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
BOOL CreateRobotDialog(HWND hParent)
1998-04-13 20:24:54 +00:00
{
BOOL result = (DialogBox(gInstance,MAKEINTRESOURCE(IDD_DEBUGROBOT),hParent,(DLGPROC)DlgProc) == IDOK);
return result;
1998-04-13 20:24:54 +00:00
}
void AddViewerMenu(HINSTANCE hInstance, nsIWidget* aWidget, LPCTSTR lpMenuName)
1998-04-13 20:24:54 +00:00
{
HMENU menu = ::LoadMenu(hInstance,lpMenuName);
HWND hwnd = aWidget->GetNativeData(NS_NATIVE_WINDOW);
::SetMenu(hwnd, menu);
1998-04-13 20:24:54 +00:00
}
void nsWin32Viewer::AddMenu(nsIWidget* aMainWindow)
1998-04-13 20:24:54 +00:00
{
AddViewerMenu(gInstance, aMainWindow, class1Name);
1998-04-13 20:24:54 +00:00
}
//-----------------------------------------------------------------
// nsWin32Viewer Implementation
//-----------------------------------------------------------------
1998-04-13 20:24:54 +00:00
void nsWin32Viewer::ShowConsole(WindowData* aWinData)
1998-04-13 20:24:54 +00:00
{
HWND hWnd = aWinData->windowWidget->GetNativeData(NS_NATIVE_WINDOW);
if (!gConsole) {
// load the accelerator table for the console
if (!JSConsole::sAccelTable) {
JSConsole::sAccelTable = LoadAccelerators(gInstance,
MAKEINTRESOURCE(ACCELERATOR_TABLE));
1998-04-13 20:24:54 +00:00
}
nsIScriptContext *context = nsnull;
if (NS_OK == aWinData->ww->GetScriptContext(&context)) {
// create the console
gConsole = JSConsole::CreateConsole();
gConsole->SetContext(context);
// lifetime of the context is still unclear at this point.
// Anyway, as long as the web widget is alive the context is alive.
// Maybe the context shouldn't even be RefCounted
context->Release();
gConsole->SetNotification(DestroyConsole);
}
else {
MessageBox(hWnd, "Unable to load JavaScript", "Viewer Error", MB_ICONSTOP);
1998-04-13 20:24:54 +00:00
}
}
1998-04-13 20:24:54 +00:00
}
void nsWin32Viewer::CloseConsole()
1998-05-08 15:06:41 +00:00
{
DestroyConsole();
}
1998-05-08 15:06:41 +00:00
void nsWin32Viewer::DoDebugRobot(WindowData* aWindata)
{
if ((nsnull != aWindata) && (nsnull != aWindata->ww)) {
if (CreateRobotDialog(aWindata->windowWidget->GetNativeData(NS_NATIVE_WINDOW)))
{
nsIDocument* doc = aWindata->ww->GetDocument();
if (nsnull!=doc) {
const char * str = doc->GetDocumentURL()->GetSpec();
nsVoidArray * gWorkList = new nsVoidArray();
gWorkList->AppendElement(new nsString(str));
DebugRobot(
gWorkList,
gVisualDebug ? aWindata->ww : nsnull,
gDebugRobotLoads,
PL_strdup(gVerifyDir),
yieldProc);
}
}
}
1998-05-08 15:06:41 +00:00
}
1998-05-08 15:06:41 +00:00
// Selects all the Content
void nsWin32Viewer::CopyTextContent(WindowData* wd, HWND aHWnd)
1998-05-08 15:06:41 +00:00
{
HGLOBAL hGlobalMemory;
PSTR pGlobalMemory;
if (wd->ww != nsnull) {
nsIDocument* doc = wd->ww->GetDocument();
if (doc != nsnull) {
// Get Text from Selection
nsString text;
doc->GetSelectionText(text);
// Copy text to Global Memory Area
hGlobalMemory = (HGLOBAL)GlobalAlloc(GHND, text.Length()+1);
if (hGlobalMemory != NULL) {
pGlobalMemory = (PSTR) GlobalLock(hGlobalMemory);
char * str = text.ToNewCString();
char * s = str;
for (int i=0;i<text.Length();i++) {
*pGlobalMemory++ = *s++;
}
delete str;
// Put data on Clipboard
GlobalUnlock(hGlobalMemory);
OpenClipboard(aHWnd);
EmptyClipboard();
SetClipboardData(CF_TEXT, hGlobalMemory);
CloseClipboard();
}
NS_IF_RELEASE(doc);
}
}
}
void nsWin32Viewer::CopySelection(WindowData* aWindata)
1998-05-19 19:06:59 +00:00
{
CopyTextContent(aWindata, aWindata->windowWidget->GetNativeData(NS_NATIVE_WINDOW));
1998-04-13 20:24:54 +00:00
}
void nsWin32Viewer::Stop()
1998-04-13 20:24:54 +00:00
{
PostQuitMessage(0);
1998-04-13 20:24:54 +00:00
}
void nsWin32Viewer::Destroy(WindowData* wd)
1998-04-13 20:24:54 +00:00
{
CloseConsole();
nsViewer::Destroy(wd);
1998-04-13 20:24:54 +00:00
}
int PASCAL
RunViewer(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow, nsWin32Viewer* aViewer)
1998-04-13 20:24:54 +00:00
{
gInstance = instance;
gPrevInstance = prevInstance;
SetViewer(aViewer);
1998-04-13 20:24:54 +00:00
nsIWidget *mainWindow = nsnull;
nsDocLoader* dl = aViewer->SetupViewer(&mainWindow);
1998-04-13 20:24:54 +00:00
// Process messages
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (!JSConsole::sAccelTable ||
!gConsole ||
!gConsole->GetMainWindow() ||
!TranslateAccelerator(gConsole->GetMainWindow(), JSConsole::sAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
NET_PollSockets();
1998-04-13 20:24:54 +00:00
}
}
aViewer->CleanupViewer(dl);
1998-05-19 19:06:59 +00:00
return msg.wParam;
1998-05-19 19:06:59 +00:00
}
//------------------------------------------------------------------
// Win32 Main
//------------------------------------------------------------------
void main(int argc, char **argv)
{
nsWin32Viewer* viewer = new nsWin32Viewer();
viewer->ProcessArguments(argc, argv);
RunViewer(GetModuleHandle(NULL), NULL, 0, SW_SHOW, viewer);
}
int PASCAL
WinMain(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow)
1998-04-13 20:24:54 +00:00
{
nsWin32Viewer* viewer = new nsWin32Viewer();
return(RunViewer(instance, prevInstance, cmdParam, nCmdShow, viewer));
1998-04-13 20:24:54 +00:00
}
1998-05-19 19:06:59 +00:00