From f0ed4e7b765fa2eca9177726a7bf849dd8c05cc3 Mon Sep 17 00:00:00 2001 From: kmcclusk Date: Mon, 1 Jun 1998 17:14:40 +0000 Subject: [PATCH] Reorganized the viewer app to separate the platform specific code from the cross-platform code. Converted as much of the WIN32 specific code to cross-platform code as possible. git-svn-id: svn://10.0.0.236/trunk@2813 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/webshell/tests/viewer/main.cpp | 38 + mozilla/webshell/tests/viewer/makefile.win | 35 +- mozilla/webshell/tests/viewer/nsViewer.cpp | 778 +++++++++++++++ mozilla/webshell/tests/viewer/nsViewer.h | 113 +++ mozilla/webshell/tests/viewer/winmain.cpp | 1025 ++++---------------- 5 files changed, 1156 insertions(+), 833 deletions(-) create mode 100644 mozilla/webshell/tests/viewer/main.cpp create mode 100644 mozilla/webshell/tests/viewer/nsViewer.cpp create mode 100644 mozilla/webshell/tests/viewer/nsViewer.h diff --git a/mozilla/webshell/tests/viewer/main.cpp b/mozilla/webshell/tests/viewer/main.cpp new file mode 100644 index 00000000000..a45ce731ebc --- /dev/null +++ b/mozilla/webshell/tests/viewer/main.cpp @@ -0,0 +1,38 @@ +/* -*- 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. + */ + +#include "nsViewer.h" + +nsresult ExecuteViewer(nsViewer* aViewer) +{ + nsIWidget *mainWindow = nsnull; + nsDocLoader* dl = aViewer->SetupViewer(&mainWindow); + nsresult result = aViewer->Run(); + aViewer->CleanupViewer(dl); + + return result; +} + +void main(int argc, char **argv) +{ + nsViewer* viewer = new nsViewer(); + SetViewer(viewer); + viewer->ProcessArguments(argc, argv); + ExecuteViewer(viewer); +} + diff --git a/mozilla/webshell/tests/viewer/makefile.win b/mozilla/webshell/tests/viewer/makefile.win index b319621335c..790d5c35428 100644 --- a/mozilla/webshell/tests/viewer/makefile.win +++ b/mozilla/webshell/tests/viewer/makefile.win @@ -22,33 +22,34 @@ MAKE_OBJ_TYPE = EXE PROGRAM = .\$(OBJDIR)\viewer.exe RESFILE = viewer.res -MISCDEP= \ - $(DIST)\lib\DebugRobot.lib \ - $(DIST)\lib\raptorweb.lib \ - $(DIST)\lib\xpcom32.lib \ - $(LIBNSPR) \ +MISCDEP= \ + $(DIST)\lib\DebugRobot.lib \ + $(DIST)\lib\raptorweb.lib \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) \ $(DIST)\lib\libplc21.lib OBJS = \ .\$(OBJDIR)\winmain.obj \ + .\$(OBJDIR)\nsViewer.obj \ .\$(OBJDIR)\JSConsole.obj \ .\$(OBJDIR)\nsDocLoader.obj \ $(NULL) LINCS=-I$(PUBLIC)\raptor -I$(PUBLIC)\xpcom -I$(PUBLIC)\dom -I$(PUBLIC)\js -I$(PUBLIC)\netlib -MYLIBS= \ - $(DIST)\lib\raptorbase.lib \ - $(DIST)\lib\raptorgfx.lib \ - $(DIST)\lib\raptorhtml.lib \ - $(DIST)\lib\raptorweb.lib \ - $(DIST)\lib\DebugRobot.lib \ - $(DIST)\lib\xpcom32.lib \ - $(DIST)\lib\js3240.lib \ - $(LIBNSPR) \ - $(DIST)\lib\libplc21.lib \ - $(DIST)\lib\netlib.lib \ - comdlg32.lib \ +MYLIBS= \ + $(DIST)\lib\raptorbase.lib \ + $(DIST)\lib\raptorgfx.lib \ + $(DIST)\lib\raptorhtml.lib \ + $(DIST)\lib\raptorweb.lib \ + $(DIST)\lib\DebugRobot.lib \ + $(DIST)\lib\xpcom32.lib \ + $(DIST)\lib\js3240.lib \ + $(LIBNSPR) \ + $(DIST)\lib\libplc21.lib \ + $(DIST)\lib\netlib.lib \ + comdlg32.lib \ $(NULL) LLIBS= $(MYLIBS) \ diff --git a/mozilla/webshell/tests/viewer/nsViewer.cpp b/mozilla/webshell/tests/viewer/nsViewer.cpp new file mode 100644 index 00000000000..97d82513af3 --- /dev/null +++ b/mozilla/webshell/tests/viewer/nsViewer.cpp @@ -0,0 +1,778 @@ +/* -*- 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. + */ + +/* + * Only cross-platform code should reside in this file. + * winmain.cpp (Win32) and main.cpp (Unix) contain the platform specific code. + */ + +#include +#include + +#include "nsViewer.h" +#include "resources.h" // #defines ID's for menu items +#include "nsIWidget.h" +#include "nsGlobalVariables.h" +#include "nsIWebWidget.h" +#include "nsIPresContext.h" +#include "nsIDocument.h" +#include "nsIDocumentObserver.h" +#include "nsIURL.h" +#include "nsUnitConversion.h" +#include "nsVoidArray.h" +#include "nsCRT.h" +#include "prthread.h" +#include "prprf.h" +#include "nsRepository.h" +#include "nsWidgetsCID.h" +#include "nsGfxCIID.h" +#include "nsViewsCID.h" +#include "nsString.h" +#include "plevent.h" +#include "prenv.h" +#include "nsIScriptContext.h" +#include "nsDocLoader.h" +#include "nsIFileWidget.h" + + +static NS_DEFINE_IID(kCFileWidgetCID, NS_FILEWIDGET_CID); +static NS_DEFINE_IID(kIFileWidgetIID, NS_IFILEWIDGET_IID); +static NS_DEFINE_IID(kCWindowCID, NS_WINDOW_CID); +static NS_DEFINE_IID(kIWidgetIID, NS_IWIDGET_IID); +static NS_DEFINE_IID(kCAppShellCID, NS_APPSHELL_CID); +static NS_DEFINE_IID(kIAppShellIID, NS_IAPPSHELL_IID); +static NS_DEFINE_IID(kCWindowIID, NS_WINDOW_CID); +static NS_DEFINE_IID(kCScrollbarIID, NS_VERTSCROLLBAR_CID); +static NS_DEFINE_IID(kCHScrollbarIID, NS_HORZSCROLLBAR_CID); +static NS_DEFINE_IID(kCButtonIID, NS_BUTTON_CID); +static NS_DEFINE_IID(kCComboBoxCID, NS_COMBOBOX_CID); +static NS_DEFINE_IID(kCListBoxCID, NS_LISTBOX_CID); +static NS_DEFINE_IID(kCRadioButtonCID, NS_RADIOBUTTON_CID); +static NS_DEFINE_IID(kCTextAreaCID, NS_TEXTAREA_CID); +static NS_DEFINE_IID(kCTextFieldCID, NS_TEXTFIELD_CID); +static NS_DEFINE_IID(kCCheckButtonIID, NS_CHECKBUTTON_CID); +static NS_DEFINE_IID(kCChildIID, NS_CHILD_CID); +static NS_DEFINE_IID(kCRenderingContextIID, NS_RENDERING_CONTEXT_CID); +static NS_DEFINE_IID(kCDeviceContextIID, NS_DEVICE_CONTEXT_CID); +static NS_DEFINE_IID(kCFontMetricsIID, NS_FONT_METRICS_CID); +static NS_DEFINE_IID(kCImageIID, NS_IMAGE_CID); +static NS_DEFINE_IID(kCRegionIID, NS_REGION_CID); +static NS_DEFINE_IID(kCViewManagerCID, NS_VIEW_MANAGER_CID); +static NS_DEFINE_IID(kCViewCID, NS_VIEW_CID); +static NS_DEFINE_IID(kCScrollingViewCID, NS_SCROLLING_VIEW_CID); + + +#define SAMPLES_BASE_URL "resource:/res/samples" +#define START_URL SAMPLES_BASE_URL "/test0.html" + +nsViewer* gTheViewer = nsnull; +nsIAppShell *gAppShell= nsnull; +static char* startURL; +static nsVoidArray* gWindows; +static PRBool gDoPurify; // run in Purify auto mode +static PRBool gDoQuantify; // run in Quantify auto mode +static PRBool gLoadTestFromFile; // run in auto mode by pulling URLs from a file (gInputFileName) +static PRInt32 gDelay=1; // if running in an auto mode, this is the delay between URL loads +static PRInt32 gRepeatCount=1; // if running in an auto mode, this is the number of times to cycle through the input +static PRInt32 gNumSamples=9; // if running in an auto mode that uses the samples, this is the last sample to load +static char gInputFileName[_MAX_PATH+1]; + + +// Temporary Netlib stuff... +/* XXX: Don't include net.h... */ +extern "C" { +extern int NET_PollSockets(); +}; + +//---------------------------------------------------------------------- + +static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENTOBSERVER_IID); + +NS_IMPL_ISUPPORTS(DocObserver, kIDocumentObserverIID); + +// Pass title information through to all of the web widgets that +// belong to this document. +NS_IMETHODIMP DocObserver::SetTitle(const nsString& aTitle) +{ + PRInt32 i, n = gWindows->Count(); + for (i = 0; i < n; i++) { + WindowData* wd = (WindowData*) gWindows->ElementAt(i); + if (wd->ww == mWebWidget) { + wd->windowWidget->SetTitle(aTitle); + } + } + return NS_OK; +} + +static DocObserver* NewObserver(nsIWebWidget* ww) +{ + nsISupports* oldContainer; + nsresult rv = ww->GetContainer(&oldContainer); + if (NS_OK == rv) { + if (nsnull == oldContainer) { + DocObserver* it = new DocObserver(ww); + NS_ADDREF(it); + ww->SetContainer(it); + return it; + } + else { + NS_RELEASE(oldContainer); + } + } + return nsnull; +} + +nsEventStatus PR_CALLBACK HandleEvent(nsGUIEvent *aEvent) +{ + nsEventStatus result = nsEventStatus_eIgnore; + switch(aEvent->message) { + case NS_SIZE: + { + struct WindowData *wd = gTheViewer->FindWindowData(aEvent->widget); + nsSizeEvent* sizeEvent = (nsSizeEvent*)aEvent; + if (wd->ww) { + nsRect rr(0, 0, sizeEvent->windowSize->width, sizeEvent->windowSize->height); + wd->ww->SetBounds(rr); + } + } + return nsEventStatus_eConsumeNoDefault; + + case NS_DESTROY: + { + struct WindowData *wd = gTheViewer->FindWindowData(aEvent->widget); + gTheViewer->Destroy(wd); + + if (gWindows->Count() == 0) { + gTheViewer->Stop(); + + } + return nsEventStatus_eConsumeDoDefault; + } + + + default: + return(gTheViewer->DispatchMenuItem(aEvent)); + break; + } + + return(nsEventStatus_eIgnore); +} + +void ReleaseMemory() +{ + nsGlobalVariables::Release(); + delete gWindows; +} + +void PrintHelpInfo(char **argv) +{ + fprintf(stderr, "Usage: %s [-p][-q][-md #][-f filename][-d #] [starting url]\n", argv[0]); + fprintf(stderr, "\t-p[#] -- run purify, optionally with a # that says which sample to stop at. For example, -p2 says to run samples 0, 1, and 2.\n"); + fprintf(stderr, "\t-q -- run quantify\n"); + fprintf(stderr, "\t-md # -- set the crt debug flags to #\n"); + fprintf(stderr, "\t-d # -- set the delay between URL loads to # (in milliseconds)\n"); + fprintf(stderr, "\t-r # -- set the repeat count, which is the number of times the URLs will be loaded in batch mode.\n"); + fprintf(stderr, "\t-f filename -- read a list of URLs from \n"); +} + +void nsViewer::ProcessArguments(int argc, char **argv) +{ + for (int i = 1; i < argc; i++) { + if (argv[i][0] == '-') { + if (strncmp(argv[i], "-p", 2) == 0) { + gDoPurify = PR_TRUE; + char *optionalSampleStopIndex = &(argv[i][2]); + if (nsnull!=*optionalSampleStopIndex) + { + if (1!=sscanf(optionalSampleStopIndex, "%d", &gNumSamples)) + { + PrintHelpInfo(argv); + exit(-1); + } + } + } + else if (strcmp(argv[i], "-q") == 0) { + gDoQuantify = PR_TRUE; + } + else if (strcmp(argv[i], "-f") == 0) { + gLoadTestFromFile = PR_TRUE; + i++; + if (i>=argc || nsnull==argv[i] || nsnull==*(argv[i])) + { + PrintHelpInfo(argv); + exit(-1); + } + strcpy(gInputFileName, argv[i]); + } + else if (strcmp(argv[i], "-d") == 0) { + i++; + if (i>=argc || 1!=sscanf(argv[i], "%d", &gDelay)) + { + PrintHelpInfo(argv); + exit(-1); + } + } + else if (strcmp(argv[i], "-md") == 0) { + if (i == argc - 1) { + PrintHelpInfo(argv); + exit(-1); + } + PRUint32 newFlags = atoi(argv[++i]); + CrtSetDebug(newFlags); + } + else if (strcmp(argv[i], "-r") == 0) { + i++; + if (i>=argc || 1!=sscanf(argv[i], "%d", &gRepeatCount)) + { + PrintHelpInfo(argv); + exit(-1); + } + } + else { + PrintHelpInfo(argv); + exit(-1); + } + } + else + break; + } + if (i < argc) { + startURL = argv[i]; + } +} + +//---------------------------------------------------------------------- +// nsViewer Implementation +//---------------------------------------------------------------------- + +/* + * Purify methods + */ + +void nsViewer::AddTestDocs(nsDocLoader* aDocLoader) +{ + char url[500]; + for (int docnum = 0; docnum < gNumSamples; docnum++) + { + PR_snprintf(url, 500, "%s/test%d.html", SAMPLES_BASE_URL, docnum); + aDocLoader->AddURL(url); + } +} + +/* + * SelfTest methods + */ +void nsViewer::AddTestDocsFromFile(nsDocLoader* aDocLoader, char *aFileName) +{ + /* Steve's table test code. + Assumes you have a file in the current working directory called aFileName + that contains a list of URLs, one per line, of files to load. + */ + + PRFileDesc* input = PR_Open(aFileName, PR_RDONLY, 0); + if (nsnull==input) + { + printf("FAILED TO OPEN %s!", aFileName); + return; + } + // read one line of input and pass it in as a URL + char *inputString = new char[10000]; + if (nsnull==inputString) + { + printf("couldn't allocate buffer, insufficient memory\n"); + exit (-1); + } + nsCRT::memset(inputString, 0, 10000); + PR_Read(input, inputString, 10000); + PR_Close(input); + + char *nextInput = inputString; + while (nsnull!=nextInput && nsnull!=*nextInput) + { + char * endOfLine = PL_strchr(nextInput, '\n'); + if (nsnull!=nextInput) + { + if (nsnull!=endOfLine) + { + char save = *endOfLine; + *endOfLine = nsnull; + } + if ('#' != *nextInput) // use '#' as a comment character + { + aDocLoader->AddURL(nextInput); + } + if (nsnull!=endOfLine) + { + nextInput = endOfLine+1; + } + else + nextInput = nsnull; + } + } + if (nsnull!=inputString) + delete [] inputString; +} + +void nsViewer::DestroyAllWindows() +{ + if (nsnull != gWindows) { + PRInt32 n = gWindows->Count(); + for (PRInt32 i = 0; i < n; i++) { + WindowData* wd = (WindowData*) gWindows->ElementAt(i); + wd->windowWidget->Destroy(); + } + gWindows->Clear(); + } +} + +struct WindowData* nsViewer::FindWindowData(nsIWidget* aWidget) +{ + PRInt32 i, n = gWindows->Count(); + for (i = 0; i < n; i++) { + WindowData* wd = (WindowData*) gWindows->ElementAt(i); + if (wd->windowWidget == aWidget) { + return(wd); + } + } + return(nsnull); +} + +#define FILE_PROTOCOL "file://" + +// Displays the Open common dialog box and lets the user specify an HTML file +// to open. +PRBool nsViewer::GetFileNameFromFileSelector(nsIWidget* aParentWindow, nsString* aFileName) +{ + PRBool selectedFileName = PR_FALSE; + nsIFileWidget *fileWidget; + nsString title("Open HTML"); + + NSRepository::CreateInstance(kCFileWidgetCID, nsnull, kIFileWidgetIID, (void**)&fileWidget); + nsString titles[] = {"all files","html" }; + nsString filters[] = {"*.*", "*.html"}; + fileWidget->SetFilterList(2, titles, filters); + fileWidget->Create(aParentWindow, + title, + eMode_load, + nsnull, + nsnull); + + PRUint32 result = fileWidget->Show(); + if (result) { + fileWidget->GetFile(*aFileName); + selectedFileName = PR_TRUE; + } + + NS_RELEASE(fileWidget); + return(selectedFileName); +} + +// Displays the Open common dialog box and lets the user specify an HTML file +// to open. Then passes the filename along to the Web widget. +void nsViewer::OpenHTMLFile(WindowData* wd) +{ + nsString fileName; + char szFile[_MAX_PATH]; + + if (GetFileNameFromFileSelector(wd->windowWidget, &fileName)) { + + + fileName.ToCString(szFile, _MAX_PATH); + PRInt32 len = strlen(szFile); + char* lpszFileURL = (char*)malloc(len + sizeof(FILE_PROTOCOL)); + + // Translate '\' to '/' + for (PRInt32 i = 0; i < len; i++) { + if (szFile[i] == '\\') { + szFile[i] = '/'; + } + } + + // Build the file URL + PR_snprintf(lpszFileURL, _MAX_PATH, "%s%s", FILE_PROTOCOL, szFile); + + // Ask the Web widget to load the file URL + wd->ww->LoadURL(lpszFileURL); + free(lpszFileURL); + } +} + +// Selects all the Content +void nsViewer::SelectAll(WindowData* wd) +{ + if (wd->ww != nsnull) { + nsIDocument* doc = wd->ww->GetDocument(); + if (doc != nsnull) { + doc->SelectAll(); + wd->ww->ShowFrameBorders(PR_FALSE); + /*PRInt32 numShells = doc->GetNumberOfShells(); + for (PRInt32 i=0;iGetShellAt(i); + //nsIViewManager * viewMgr = shell->GetViewManager(); + nsIFrame * frame = shell->GetRootFrame(); + nsRect rect; + nsIView * view; + nsPoint pnt; + frame->GetOffsetFromView(pnt, view); + frame->GetRect(rect); + rect.x = pnt.x; + rect.y = pnt.y; + if (view != nsnull) { + nsIViewManager * viewMgr = view->GetViewManager(); + if (viewMgr != nsnull) { + viewMgr->UpdateView(view, rect, 0); + } + } + NS_IF_RELEASE(shell); + //NS_IF_RELEASE(frame); + }*/ + + NS_IF_RELEASE(doc); + } + } +} + +/** + * Create a top level window + */ + +WindowData* nsViewer::CreateTopLevel(const char* title, + int aWidth, int aHeight) +{ + WindowData* wd = new WindowData(); + nsIWidget* window = nsnull; + NSRepository::CreateInstance(kCWindowCID, nsnull, kIWidgetIID, (void**)&window); + nsRect rect(100, 100, aWidth, aHeight); + + window->Create((nsIWidget*)NULL, rect, HandleEvent, nsnull); + window->SetTitle(title); + wd->windowWidget = window; + gWindows->AppendElement(wd); + window->Show(PR_TRUE); + + return wd; +} + + +nsresult nsViewer::ShowPrintPreview(nsIWebWidget* web, PRIntn aColumns) +{ + if (nsnull != web) { + nsIDocument* doc = web->GetDocument(); + if (nsnull != doc) { + nsIPresContext* cx; + nsresult rv = NS_NewPrintPreviewContext(&cx); + if (NS_OK != rv) { + return rv; + } + + WindowData* wd = CreateTopLevel("Print Preview", 500, 300); + + nsRect bounds; + wd->windowWidget->GetBounds(bounds); + nsRect rr(0, 0, bounds.width, bounds.height); + + rv = NS_NewWebWidget(&wd->ww); + rv = wd->ww->Init(wd->windowWidget->GetNativeData(NS_NATIVE_WINDOW), rr, doc, cx); + wd->ww->Show(); + wd->observer = NewObserver(wd->ww); + + NS_RELEASE(cx); + NS_RELEASE(doc); + } + } + return NS_OK; +} + + +nsEventStatus nsViewer::DispatchMenuItem(nsGUIEvent *aEvent) +{ + nsEventStatus result = nsEventStatus_eIgnore; + + switch(aEvent->message) { + + case NS_MENU_SELECTED: + nsMenuEvent* menuEvent = (nsMenuEvent*)aEvent; + WindowData* wd = FindWindowData(aEvent->widget); + switch(menuEvent->menuItem) { + + case VIEWER_EXIT: + DestroyAllWindows(); + Stop(); + return nsEventStatus_eConsumeNoDefault; + + case PREVIEW_CLOSE: + aEvent->widget->Destroy(); + return nsEventStatus_eConsumeNoDefault; + + case VIEWER_FILE_OPEN: + OpenHTMLFile(wd); + break; + + case VIEWER_EDIT_CUT: + break; + + case VIEWER_EDIT_COPY: + CopySelection(wd); + break; + + case VIEWER_EDIT_PASTE: + break; + + case VIEWER_EDIT_SELECTALL: + SelectAll(wd); + break; + + case VIEWER_EDIT_FINDINPAGE: + break; + + case VIEWER_DEMO0: + case VIEWER_DEMO1: + case VIEWER_DEMO2: + case VIEWER_DEMO3: + case VIEWER_DEMO4: + case VIEWER_DEMO5: + case VIEWER_DEMO6: + case VIEWER_DEMO7: + case VIEWER_DEMO8: + if ((nsnull != wd) && (nsnull != wd->ww)) { + PRIntn ix = menuEvent->menuItem - VIEWER_DEMO0; + char* url = new char[500]; + PR_snprintf(url, 500, "%s/test%d.html", SAMPLES_BASE_URL, ix); + wd->ww->LoadURL(url); + delete url; + } + break; + + case VIEWER_VISUAL_DEBUGGING: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->ShowFrameBorders(PRBool(!wd->ww->GetShowFrameBorders())); + } + break; + + case VIEWER_DUMP_CONTENT: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->DumpContent(); + } + break; + case VIEWER_DUMP_FRAMES: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->DumpFrames(); + } + break; + case VIEWER_DUMP_VIEWS: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->DumpViews(); + } + break; + case VIEWER_DUMP_STYLE_SHEETS: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->DumpStyleSheets(); + } + break; + case VIEWER_DUMP_STYLE_CONTEXTS: + if ((nsnull != wd) && (nsnull != wd->ww)) { + wd->ww->DumpStyleContexts(); + } + break; + + case VIEWER_DEBUGROBOT: + DoDebugRobot(wd); + break; + + case VIEWER_ONE_COLUMN: + case VIEWER_TWO_COLUMN: + case VIEWER_THREE_COLUMN: + if ((nsnull != wd) && (nsnull != wd->ww)) { + ShowPrintPreview(wd->ww, menuEvent->menuItem - VIEWER_ONE_COLUMN + 1); + } + break; + + case JS_CONSOLE: + ShowConsole(wd); + break; + } + break; + } + + return(result); +} + +void nsViewer::CleanupViewer(nsDocLoader* aDl) +{ + if (aDl != nsnull) + delete aDl; + ReleaseMemory(); +} + +nsDocLoader* nsViewer::SetupViewer(nsIWidget **aMainWindow) +{ + PL_InitializeEventsLib(""); + + gWindows = new nsVoidArray(); + + NSRepository::RegisterFactory(kCWindowIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCScrollbarIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCHScrollbarIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCButtonIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCComboBoxCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCFileWidgetCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCListBoxCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCRadioButtonCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCTextAreaCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCTextFieldCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCCheckButtonIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCChildIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCAppShellCID, WIDGET_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCRenderingContextIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCDeviceContextIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCFontMetricsIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCImageIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCRegionIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCViewManagerCID, VIEW_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCViewCID, VIEW_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCScrollingViewCID, VIEW_DLL, PR_FALSE, PR_FALSE); + + // Create an application shell + NSRepository::CreateInstance(kCAppShellCID, nsnull, kIAppShellIID, (void**)&gAppShell); + gAppShell->Create(); + gAppShell->SetDispatchListener(this); + + // Create a top level window for the WebWidget + WindowData* wd = CreateTopLevel("Raptor HTML Viewer", 620, 400); + *aMainWindow = wd->windowWidget; + + // Attach a menu to the top level window + AddMenu(wd->windowWidget); + + // Now embed the web widget in it + nsresult rv = NS_NewWebWidget(&wd->ww); + nsRect bounds; + wd->windowWidget->GetBounds(bounds); + nsRect rr(0, 0, bounds.width, bounds.height); + rv = wd->ww->Init(wd->windowWidget->GetNativeData(NS_NATIVE_WINDOW), rr); + wd->ww->Show(); + wd->observer = NewObserver(wd->ww); + + + // Determine if we should run the purify test + nsDocLoader* dl = nsnull; + if (gDoPurify) { + dl = new nsDocLoader(wd->ww, gDelay); + + // Add the documents to the loader + for (PRInt32 i=0; iStartTimedLoading(); + } + else if (gLoadTestFromFile) { + dl = new nsDocLoader(wd->ww, gDelay); + for (PRInt32 i=0; iStartTimedLoading(); + } + else { + // Load the starting url if we have one + wd->ww->LoadURL(startURL ? startURL : START_URL); + if (gDoQuantify) { + // Synthesize 20 ResizeReflow commands (+/- 10 pixels) and then + // exit. +#define kNumReflows 20 + for (PRIntn i = 0; i < kNumReflows; i++) { + nsRect r = wd->ww->GetBounds(); + if (i & 1) { + r.width -= 10; + } + else { + r.width += 10; + } + wd->ww->SetBounds(r); + } + exit(0); + } + } + + return(dl); +} + +void nsViewer::AfterDispatch() +{ + NET_PollSockets(); +} + +void nsViewer::AddMenu(nsIWidget* aMainWindow) +{ + printf("Menu not implemented\n"); +} + +void nsViewer::ShowConsole(WindowData* aWindata) +{ + printf("ShowConsole not implemented\n"); +} + +void nsViewer::CloseConsole() +{ + printf("CloseConsole not implemented\n"); +} + +void nsViewer::DoDebugRobot(WindowData* aWinData) +{ + printf("DebugRobot not implemented\n"); +} + +void nsViewer::CopySelection(WindowData* aWindata) +{ + printf("CopySelection not implemented\n"); +} + +void nsViewer::CrtSetDebug(PRUint32 aNewFlags) +{ + printf("CrtSetDebug not implemented\n"); +} + +void nsViewer::Destroy(WindowData* aWinData) +{ + if (nsnull != aWinData) { + if (nsnull != aWinData->ww) { + aWinData->ww->SetContainer(nsnull); // release the doc observer + NS_RELEASE(aWinData->ww); + } + if (nsnull != aWinData->observer) { + NS_RELEASE(aWinData->observer); + } + gWindows->RemoveElement(aWinData); + delete aWinData; + } +} + +nsresult nsViewer::Run() +{ + return(gAppShell->Run()); +} + +void nsViewer::Stop() +{ + gAppShell->Exit(); +} + +void SetViewer(nsViewer* aViewer) +{ + gTheViewer = aViewer; +} + diff --git a/mozilla/webshell/tests/viewer/nsViewer.h b/mozilla/webshell/tests/viewer/nsViewer.h new file mode 100644 index 00000000000..c63bd17e1e4 --- /dev/null +++ b/mozilla/webshell/tests/viewer/nsViewer.h @@ -0,0 +1,113 @@ +/* -*- 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. + */ +#ifndef nsViewer_h___ +#define nsViewer_h___ + +#include "nsIWebWidget.h" +#include "nsIDocumentObserver.h" +#include "nsDocLoader.h" +#include "nsIAppShell.h" + +#define WIDGET_DLL "raptorwidget.dll" +#define GFXWIN_DLL "raptorgfxwin.dll" +#define VIEW_DLL "raptorview.dll" + +class DocObserver : public nsIDocumentObserver { +public: + DocObserver(nsIWebWidget* aWebWidget) { + NS_INIT_REFCNT(); + mWebWidget = aWebWidget; + NS_ADDREF(aWebWidget); + } + + NS_DECL_ISUPPORTS; + + NS_IMETHOD SetTitle(const nsString& aTitle); + + virtual void BeginUpdate() { } + virtual void EndUpdate() { } + virtual void ContentChanged(nsIContent* aContent, + nsISupports* aSubContent) {} + virtual void ContentAppended(nsIContent* aContainer) { } + virtual void ContentInserted(nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { } + virtual void ContentReplaced(nsIContent* aContainer, + nsIContent* aOldChild, + nsIContent* aNewChild, + PRInt32 aIndexInContainer) { } + virtual void ContentWillBeRemoved(nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { } + virtual void ContentHasBeenRemoved(nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { } + virtual void StyleSheetAdded(nsIStyleSheet* aStyleSheet) { } + + +protected: + ~DocObserver() { + NS_RELEASE(mWebWidget); + } + + nsIWebWidget* mWebWidget; +}; + +struct WindowData { + nsIWebWidget* ww; + DocObserver* observer; + nsIWidget *windowWidget; + + WindowData() { + ww = nsnull; + } +}; + +class nsViewer : public nsDispatchListener { + public: + virtual void AddMenu(nsIWidget* aMainWindow); + virtual void ShowConsole(WindowData* aWindata); + virtual void CloseConsole(); + virtual void DoDebugRobot(WindowData* aWindata); + virtual void CopySelection(WindowData* aWindata); + virtual nsresult Run(); + virtual void Destroy(WindowData* wd); + virtual void Stop(); + virtual void AfterDispatch(); + virtual void CrtSetDebug(PRUint32 aNewFlags); + + virtual nsDocLoader* SetupViewer(nsIWidget **aMainWindow); + virtual void CleanupViewer(nsDocLoader* aDl); + virtual nsEventStatus DispatchMenuItem(nsGUIEvent *aEvent); + virtual nsresult ShowPrintPreview(nsIWebWidget* web, PRIntn aColumns); + virtual WindowData* CreateTopLevel(const char* title, int aWidth, int aHeight); + virtual void AddTestDocs(nsDocLoader* aDocLoader); + virtual void AddTestDocsFromFile(nsDocLoader* aDocLoader, char *aFileName); + virtual void DestroyAllWindows(); + virtual struct WindowData* FindWindowData(nsIWidget* aWidget); + virtual PRBool GetFileNameFromFileSelector(nsIWidget* aParentWindow, nsString* aFileName); + virtual void OpenHTMLFile(WindowData* wd); + virtual void SelectAll(WindowData* wd); + virtual void ProcessArguments(int argc, char **argv); +}; + + // Set the single viewer. +extern void SetViewer(nsViewer* aViewer); + + +#endif \ No newline at end of file diff --git a/mozilla/webshell/tests/viewer/winmain.cpp b/mozilla/webshell/tests/viewer/winmain.cpp index c05e727df5f..09757697c9d 100644 --- a/mozilla/webshell/tests/viewer/winmain.cpp +++ b/mozilla/webshell/tests/viewer/winmain.cpp @@ -15,71 +15,31 @@ * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ + +// Win32 specific viewer driver + #include #include -#include - #include "resources.h" #include "jsconsres.h" #include "JSConsole.h" - +#include "nsViewer.h" #include "nsGlobalVariables.h" -#include "nsIWebWidget.h" -#include "nsIPresContext.h" #include "nsIDocument.h" -#include "nsIDocumentObserver.h" #include "nsIURL.h" -#include "nsUnitConversion.h" #include "nsVoidArray.h" #include "nsCRT.h" -#include "prthread.h" -#include "prprf.h" -#include "nsRepository.h" -#include "nsWidgetsCID.h" -#include "nsGfxCIID.h" -#include "nsViewsCID.h" -#include "nsString.h" -#include "plevent.h" #include "prenv.h" -#include #include "nsIScriptContext.h" -#include "nsDocLoader.h" - -// DebugRobot call -extern "C" NS_EXPORT int DebugRobot( - nsVoidArray * workList, nsIWebWidget * ww, int imax, char * verify_dir, void (*yieldProc)(const char *)); - -// Selection Repaint includes -#include "nsIPresShell.h" -#include "nsIFrame.h" -#include "nsIViewManager.h" - -// JSConsole window -JSConsole *gConsole = NULL; - -#define SAMPLES_BASE_URL "resource:/res/samples" -#define START_URL SAMPLES_BASE_URL "/test0.html" - -static char* class1Name = "Viewer"; -static char* class2Name = "PrintPreview"; - -static HANDLE gInstance, gPrevInstance; -static char* startURL; -static nsVoidArray* gWindows; -static PRBool gDoPurify; // run in Purify auto mode -static PRBool gDoQuantify; // run in Quantify auto mode -static PRBool gLoadTestFromFile; // run in auto mode by pulling URLs from a file (gInputFileName) -static PRInt32 gDelay=1; // if running in an auto mode, this is the delay between URL loads -static PRInt32 gRepeatCount=1; // if running in an auto mode, this is the number of times to cycle through the input -static PRInt32 gNumSamples=9; // if running in an auto mode that uses the samples, this is the last sample to load -static char gInputFileName[_MAX_PATH+1]; // Debug Robot options static int gDebugRobotLoads = 5000; static char gVerifyDir[_MAX_PATH]; static BOOL gVisualDebug = TRUE; -#define DEBUG_EMPTY "(none)" +// DebugRobot call +extern "C" NS_EXPORT int DebugRobot( + nsVoidArray * workList, nsIWebWidget * ww, int imax, char * verify_dir, void (*yieldProc)(const char *)); // Temporary Netlib stuff... /* XXX: Don't include net.h... */ @@ -87,331 +47,59 @@ extern "C" { extern int NET_PollSockets(); }; -//---------------------------------------------------------------------- - -class DocObserver : public nsIDocumentObserver { -public: - DocObserver(nsIWebWidget* aWebWidget) { - NS_INIT_REFCNT(); - mWebWidget = aWebWidget; - NS_ADDREF(aWebWidget); - } - - NS_DECL_ISUPPORTS; - - NS_IMETHOD SetTitle(const nsString& aTitle); - - virtual void BeginUpdate() { } - virtual void EndUpdate() { } - virtual void ContentChanged(nsIContent* aContent, - nsISupports* aSubContent) {} - virtual void ContentAppended(nsIContent* aContainer) { } - virtual void ContentInserted(nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { } - virtual void ContentReplaced(nsIContent* aContainer, - nsIContent* aOldChild, - nsIContent* aNewChild, - PRInt32 aIndexInContainer) { } - virtual void ContentWillBeRemoved(nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { } - virtual void ContentHasBeenRemoved(nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { } - virtual void StyleSheetAdded(nsIStyleSheet* aStyleSheet) { } +#define DEBUG_EMPTY "(none)" -protected: - ~DocObserver() { - NS_RELEASE(mWebWidget); - } - - nsIWebWidget* mWebWidget; +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); }; -struct WindowData { - HWND window; - nsIWebWidget* ww; - DocObserver* observer; +static HANDLE gInstance, gPrevInstance; - WindowData() { - window = nsnull; - ww = nsnull; - } -}; +//----------------------------------------------------------------- +// JSConsole support +//----------------------------------------------------------------- -static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENTOBSERVER_IID); +// JSConsole window +JSConsole *gConsole = NULL; -NS_IMPL_ISUPPORTS(DocObserver, kIDocumentObserverIID); - -// Pass title information through to all of the web widgets that -// belong to this document. -NS_IMETHODIMP DocObserver::SetTitle(const nsString& aTitle) -{ - PRInt32 i, n = gWindows->Count(); - for (i = 0; i < n; i++) { - WindowData* wd = (WindowData*) gWindows->ElementAt(i); - if (wd->ww == mWebWidget) { - char* cp = aTitle.ToNewCString(); - ::SetWindowText(wd->window, cp); - delete cp; - } - } - return NS_OK; -} - -static DocObserver* NewObserver(nsIWebWidget* ww) -{ - nsISupports* oldContainer; - nsresult rv = ww->GetContainer(&oldContainer); - if (NS_OK == rv) { - if (nsnull == oldContainer) { - DocObserver* it = new DocObserver(ww); - NS_ADDREF(it); - ww->SetContainer(it); - return it; - } - else { - NS_RELEASE(oldContainer); - } - } - return nsnull; -} - -//---------------------------------------------------------------------- - -/* - * Purify methods - */ - -void AddTestDocs(nsDocLoader* aDocLoader) -{ - char url[500]; - for (int docnum = 0; docnum < gNumSamples; docnum++) - { - PR_snprintf(url, 500, "%s/test%d.html", SAMPLES_BASE_URL, docnum); - aDocLoader->AddURL(url); - } -} - -//---------------------------------------------------------------------- - -/* - * SelfTest methods - */ -void AddTestDocsFromFile(nsDocLoader* aDocLoader, char *aFileName) -{ - /* Steve's table test code. - Assumes you have a file in the current working directory called aFileName - that contains a list of URLs, one per line, of files to load. - */ - - PRFileDesc* input = PR_Open(aFileName, PR_RDONLY, 0); - if (nsnull==input) - { - printf("FAILED TO OPEN %s!", aFileName); - return; - } - // read one line of input and pass it in as a URL - char *inputString = new char[10000]; - if (nsnull==inputString) - { - printf("couldn't allocate buffer, insufficient memory\n"); - exit (-1); - } - nsCRT::memset(inputString, 0, 10000); - PR_Read(input, inputString, 10000); - PR_Close(input); - - char *nextInput = inputString; - while (nsnull!=nextInput && nsnull!=*nextInput) - { - char * endOfLine = PL_strchr(nextInput, '\n'); - if (nsnull!=nextInput) - { - if (nsnull!=endOfLine) - { - char save = *endOfLine; - *endOfLine = nsnull; - } - if ('#' != *nextInput) // use '#' as a comment character - { - aDocLoader->AddURL(nextInput); - } - if (nsnull!=endOfLine) - { - nextInput = endOfLine+1; - } - else - nextInput = nsnull; - } - } - if (nsnull!=inputString) - delete [] inputString; -} - -//---------------------------------------------------------------------- - -static nsresult ShowPrintPreview(nsIWebWidget* ww, PRIntn aColumns); +static char* class1Name = "Viewer"; void DestroyConsole() { - if (gConsole) { + if (gConsole) { gConsole->SetNotification(NULL); delete gConsole; gConsole = NULL; } } -static void Destroy(WindowData* wd) +//----------------------------------------------------------------- +// CRT Debug +//----------------------------------------------------------------- + +void nsWin32Viewer::CrtSetDebug(PRUint32 aNewFlags) { - DestroyConsole(); - if (nsnull != wd) { - if (nsnull != wd->ww) { - wd->ww->SetContainer(nsnull); // release the doc observer - NS_RELEASE(wd->ww); - } - if (nsnull != wd->observer) { - NS_RELEASE(wd->observer); - } - gWindows->RemoveElement(wd); - delete wd; - } + int oldFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + _CrtSetDbgFlag(aNewFlags); + printf("Note: crt flags: old=%x new=%x\n", oldFlags, aNewFlags); } -static void DestroyAllWindows() -{ - if (nsnull != gWindows) { - PRInt32 n = gWindows->Count(); - for (PRInt32 i = 0; i < n; i++) { - WindowData* wd = (WindowData*) gWindows->ElementAt(i); - ::DestroyWindow(wd->window); - } - gWindows->Clear(); - } -} -#define FILE_PROTOCOL "file://" - -// Displays the Open common dialog box and lets the user specify an HTML file -// to open. Then passes the filename along to the Web widget. -static void -OpenHTMLFile(WindowData* wd) -{ - char szFile[_MAX_PATH]; - OPENFILENAME ofn; - - // Prompt the user for a filename - ofn.lStructSize = sizeof(OPENFILENAME); - ofn.hwndOwner = wd->window; - ofn.hInstance = gInstance; - ofn.lpstrFilter = "HTML Files\0*.html;*.htm\0All Files\0*.*\0\0"; - ofn.lpstrCustomFilter = NULL; - ofn.nFilterIndex = 1; - szFile[0] = '\0'; - ofn.lpstrFile = szFile; - ofn.nMaxFile = sizeof(szFile); - ofn.lpstrFileTitle = NULL; - ofn.lpstrInitialDir = NULL; // use current directory as initial directory - ofn.lpstrTitle = NULL; - ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON; - ofn.lpstrDefExt = NULL; - ofn.lpTemplateName = NULL; - - if (GetOpenFileName(&ofn)) { - PRInt32 len = strlen(szFile); - char* lpszFileURL = (char*)malloc(len + sizeof(FILE_PROTOCOL)); - - // Translate '\' to '/' - for (PRInt32 i = 0; i < len; i++) { - if (szFile[i] == '\\') { - szFile[i] = '/'; - } - } - - // Build the file URL - wsprintf(lpszFileURL, "%s%s", FILE_PROTOCOL, szFile); - - // Ask the Web widget to load the file URL - wd->ww->LoadURL(lpszFileURL); - free(lpszFileURL); - } -} - -// Selects all the Content -static void -SelectAll(WindowData* wd) -{ - if (wd->ww != nsnull) { - nsIDocument* doc = wd->ww->GetDocument(); - if (doc != nsnull) { - doc->SelectAll(); - wd->ww->ShowFrameBorders(PR_FALSE); - /*PRInt32 numShells = doc->GetNumberOfShells(); - for (PRInt32 i=0;iGetShellAt(i); - //nsIViewManager * viewMgr = shell->GetViewManager(); - nsIFrame * frame = shell->GetRootFrame(); - nsRect rect; - nsIView * view; - nsPoint pnt; - frame->GetOffsetFromView(pnt, view); - frame->GetRect(rect); - rect.x = pnt.x; - rect.y = pnt.y; - if (view != nsnull) { - nsIViewManager * viewMgr = view->GetViewManager(); - if (viewMgr != nsnull) { - viewMgr->UpdateView(view, rect, 0); - } - } - NS_IF_RELEASE(shell); - //NS_IF_RELEASE(frame); - }*/ - - NS_IF_RELEASE(doc); - } - } -} - -// Selects all the Content -static void -CopyTextContent(WindowData* wd, HWND aHWnd) -{ - 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;ilpCreateParams; - SetWindowLong(hWnd, GWL_USERDATA, (LONG) w); - } - - WindowData* wd = (WindowData*) GetWindowLong(hWnd, GWL_USERDATA); - switch (msg) { - case WM_COMMAND: - hMenu = GetMenu(hWnd); - switch (LOWORD(param)) { - case VIEWER_EXIT: - DestroyAllWindows(); - return 0; - case PREVIEW_CLOSE: - ::DestroyWindow(hWnd); - return 0; - - case VIEWER_FILE_OPEN: - OpenHTMLFile(wd); - break; - - case VIEWER_EDIT_CUT: - break; - - case VIEWER_EDIT_COPY: - CopyTextContent(wd, hWnd); - break; - - case VIEWER_EDIT_PASTE: - break; - - case VIEWER_EDIT_SELECTALL: - SelectAll(wd); - break; - - case VIEWER_EDIT_FINDINPAGE: - break; - - case VIEWER_DEMO0: - case VIEWER_DEMO1: - case VIEWER_DEMO2: - case VIEWER_DEMO3: - case VIEWER_DEMO4: - case VIEWER_DEMO5: - case VIEWER_DEMO6: - case VIEWER_DEMO7: - case VIEWER_DEMO8: - if ((nsnull != wd) && (nsnull != wd->ww)) { - PRIntn ix = LOWORD(param) - VIEWER_DEMO0; - char* url = new char[500]; - PR_snprintf(url, 500, "%s/test%d.html", SAMPLES_BASE_URL, ix); - wd->ww->LoadURL(url); - delete url; - } - break; - - case VIEWER_VISUAL_DEBUGGING: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->ShowFrameBorders(PRBool(!wd->ww->GetShowFrameBorders())); - } - break; - case VIEWER_DUMP_CONTENT: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->DumpContent(); - } - break; - case VIEWER_DUMP_FRAMES: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->DumpFrames(); - } - break; - case VIEWER_DUMP_VIEWS: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->DumpViews(); - } - break; - case VIEWER_DUMP_STYLE_SHEETS: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->DumpStyleSheets(); - } - break; - case VIEWER_DUMP_STYLE_CONTEXTS: - if ((nsnull != wd) && (nsnull != wd->ww)) { - wd->ww->DumpStyleContexts(); - } - break; - case VIEWER_DEBUGROBOT: - if ((nsnull != wd) && (nsnull != wd->ww)) { - if (CreateRobotDialog(hWnd)) - { - nsIDocument* doc = wd->ww->GetDocument(); - if (nsnull!=doc) { - const char * str = doc->GetDocumentURL()->GetSpec(); - nsVoidArray * gWorkList = new nsVoidArray(); - gWorkList->AppendElement(new nsString(str)); - DebugRobot( - gWorkList, - gVisualDebug ? wd->ww : nsnull, - gDebugRobotLoads, - PL_strdup(gVerifyDir), - yieldProc); - } - } - } - break; - case VIEWER_ONE_COLUMN: - case VIEWER_TWO_COLUMN: - case VIEWER_THREE_COLUMN: - if ((nsnull != wd) && (nsnull != wd->ww)) { - ShowPrintPreview(wd->ww, LOWORD(param) - VIEWER_ONE_COLUMN + 1); - } - break; - case JS_CONSOLE: - if (!gConsole) { - - // load the accelerator table for the console - if (!JSConsole::sAccelTable) { - JSConsole::sAccelTable = LoadAccelerators(gInstance, - MAKEINTRESOURCE(ACCELERATOR_TABLE)); - } - - nsIScriptContext *context = nsnull; - if (NS_OK == wd->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); - } - } - break; - - default: - break; - } - break; - - case WM_SIZE: - if ((nsnull != wd) && (nsnull != wd->ww)) { - RECT r; - ::GetClientRect(hWnd, &r); - nsRect rr(0, 0, PRInt32(r.right - r.left), PRInt32(r.bottom - r.top)); - wd->ww->SetBounds(rr); - } - return 0; - - case WM_DESTROY: - Destroy(wd); - if (gWindows->Count() == 0) { - PostQuitMessage(0); - } - return 0; - - case WM_CLOSE: - // Let windows destroy the window - break; - - default: - break; - } - return DefWindowProc(hWnd, msg, param, lparam); -} - -static WindowData* CreateTopLevel(const char* clazz, const char* title, - int aWidth, int aHeight) -{ - // Create a simple top level window - WindowData* wd = new WindowData(); - HWND window = ::CreateWindowEx(WS_EX_CLIENTEDGE, - clazz, title, - WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN, - CW_USEDEFAULT, CW_USEDEFAULT, - aWidth, aHeight, - HWND_DESKTOP, - NULL, - gInstance, - wd); - wd->window = window; - gWindows->AppendElement(wd); - ::ShowWindow(window, SW_SHOW); - ::UpdateWindow(window); - return wd; -} - -static nsresult ShowPrintPreview(nsIWebWidget* web, PRIntn aColumns) -{ - if (nsnull != web) { - nsIDocument* doc = web->GetDocument(); - if (nsnull != doc) { - nsIPresContext* cx; - nsresult rv = NS_NewPrintPreviewContext(&cx); - if (NS_OK != rv) { - return rv; - } - - WindowData* wd = CreateTopLevel(class2Name, "Print Preview", 500, 300); - - RECT r; - ::GetClientRect(wd->window, &r); - nsRect rr(0, 0, PRInt32(r.right - r.left), PRInt32(r.bottom - r.top)); - rv = NS_NewWebWidget(&wd->ww); - rv = wd->ww->Init(wd->window, rr, doc, cx); - wd->ww->Show(); - wd->observer = NewObserver(wd->ww); - - NS_RELEASE(cx); - NS_RELEASE(doc); - } - } - return NS_OK; -} - -void ReleaseMemory() -{ - nsGlobalVariables::Release(); - delete gWindows; -} - -#define WIDGET_DLL "raptorwidget.dll" -#define GFXWIN_DLL "raptorgfxwin.dll" -#define VIEW_DLL "raptorview.dll" - -int PASCAL -WinMain(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow) -{ - PL_InitializeEventsLib(""); - - gInstance = instance; - gPrevInstance = prevInstance; - gWindows = new nsVoidArray(); - - static NS_DEFINE_IID(kCWindowIID, NS_WINDOW_CID); - static NS_DEFINE_IID(kCScrollbarIID, NS_VERTSCROLLBAR_CID); - static NS_DEFINE_IID(kCHScrollbarIID, NS_HORZSCROLLBAR_CID); - static NS_DEFINE_IID(kCButtonIID, NS_BUTTON_CID); - static NS_DEFINE_IID(kCComboBoxCID, NS_COMBOBOX_CID); - static NS_DEFINE_IID(kCFileWidgetCID, NS_FILEWIDGET_CID); - static NS_DEFINE_IID(kCListBoxCID, NS_LISTBOX_CID); - static NS_DEFINE_IID(kCRadioButtonCID, NS_RADIOBUTTON_CID); - static NS_DEFINE_IID(kCTextAreaCID, NS_TEXTAREA_CID); - static NS_DEFINE_IID(kCTextFieldCID, NS_TEXTFIELD_CID); - static NS_DEFINE_IID(kCCheckButtonIID, NS_CHECKBUTTON_CID); - static NS_DEFINE_IID(kCChildIID, NS_CHILD_CID); - - NSRepository::RegisterFactory(kCWindowIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCScrollbarIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCHScrollbarIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCButtonIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCComboBoxCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCFileWidgetCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCListBoxCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCRadioButtonCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCTextAreaCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCTextFieldCID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCCheckButtonIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCChildIID, WIDGET_DLL, PR_FALSE, PR_FALSE); - - static NS_DEFINE_IID(kCRenderingContextIID, NS_RENDERING_CONTEXT_CID); - static NS_DEFINE_IID(kCDeviceContextIID, NS_DEVICE_CONTEXT_CID); - static NS_DEFINE_IID(kCFontMetricsIID, NS_FONT_METRICS_CID); - static NS_DEFINE_IID(kCImageIID, NS_IMAGE_CID); - static NS_DEFINE_IID(kCRegionIID, NS_REGION_CID); - - NSRepository::RegisterFactory(kCRenderingContextIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCDeviceContextIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCFontMetricsIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCImageIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCRegionIID, GFXWIN_DLL, PR_FALSE, PR_FALSE); - - static NS_DEFINE_IID(kCViewManagerCID, NS_VIEW_MANAGER_CID); - static NS_DEFINE_IID(kCViewCID, NS_VIEW_CID); - static NS_DEFINE_IID(kCScrollingViewCID, NS_SCROLLING_VIEW_CID); - - NSRepository::RegisterFactory(kCViewManagerCID, VIEW_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCViewCID, VIEW_DLL, PR_FALSE, PR_FALSE); - NSRepository::RegisterFactory(kCScrollingViewCID, VIEW_DLL, PR_FALSE, PR_FALSE); - - // initialize the toolkit (widget library). - // This may look weak and in fact it is...but hey... - //XXX move it somewhere else please - //NS_InitToolkit(PR_GetCurrentThread()); - - if (!prevInstance) { - WNDCLASS wndClass; - wndClass.style = 0; - wndClass.lpfnWndProc = WndProc; - wndClass.cbClsExtra = 0; - wndClass.cbWndExtra = 0; - wndClass.hInstance = gInstance; - wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); - wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); - wndClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); - wndClass.lpszMenuName = class1Name; - wndClass.lpszClassName = class1Name; - RegisterClass(&wndClass); - - wndClass.lpszMenuName = class2Name; - wndClass.lpszClassName = class2Name; - RegisterClass(&wndClass); - } - - // Create our first top level window - WindowData* wd = CreateTopLevel(class1Name, "Raptor HTML Viewer", 620, 400); - - // Now embed the web widget in it - RECT r; - ::GetClientRect(wd->window, &r); - nsresult rv = NS_NewWebWidget(&wd->ww); - nsRect rr(0, 0, PRInt32(r.right - r.left), PRInt32(r.bottom - r.top)); - rv = wd->ww->Init(wd->window, rr); - wd->ww->Show(); - wd->observer = NewObserver(wd->ww); - - - // Determine if we should run the purify test - nsDocLoader* dl = nsnull; - if (gDoPurify) { - dl = new nsDocLoader(wd->ww, gDelay); - - // Add the documents to the loader - for (PRInt32 i=0; iStartTimedLoading(); - } - else if (gLoadTestFromFile) { - dl = new nsDocLoader(wd->ww, gDelay); - for (PRInt32 i=0; iStartTimedLoading(); - } - else { - // Load the starting url if we have one - wd->ww->LoadURL(startURL ? startURL : START_URL); - if (gDoQuantify) { - // Synthesize 20 ResizeReflow commands (+/- 10 pixels) and then - // exit. -#define kNumReflows 20 - for (PRIntn i = 0; i < kNumReflows; i++) { - nsRect r = wd->ww->GetBounds(); - if (i & 1) { - r.width -= 10; - } - else { - r.width += 10; - } - wd->ww->SetBounds(r); - } - exit(0); - } - } - - // 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); - /* Pump Netlib... */ - NET_PollSockets(); - } - } - if (dl != nsnull) - delete dl; - ReleaseMemory(); - if (!prevInstance) { - UnregisterClass(class1Name, gInstance); - UnregisterClass(class2Name, gInstance); - } - return msg.wParam; -} - - /* Debug Robot Dialog options */ BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam,LPARAM lParam) @@ -862,90 +159,186 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam,LPARAM lParam) return TRUE; } + BOOL CreateRobotDialog(HWND hParent) { BOOL result = (DialogBox(gInstance,MAKEINTRESOURCE(IDD_DEBUGROBOT),hParent,(DLGPROC)DlgProc) == IDOK); return result; } -void PrintHelpInfo(char **argv) + +void AddViewerMenu(HINSTANCE hInstance, nsIWidget* aWidget, LPCTSTR lpMenuName) { - fprintf(stderr, "Usage: %s [-p][-q][-md #][-f filename][-d #] [starting url]\n", argv[0]); - fprintf(stderr, "\t-p[#] -- run purify, optionally with a # that says which sample to stop at. For example, -p2 says to run samples 0, 1, and 2.\n"); - fprintf(stderr, "\t-q -- run quantify\n"); - fprintf(stderr, "\t-md # -- set the crt debug flags to #\n"); - fprintf(stderr, "\t-d # -- set the delay between URL loads to # (in milliseconds)\n"); - fprintf(stderr, "\t-r # -- set the repeat count, which is the number of times the URLs will be loaded in batch mode.\n"); - fprintf(stderr, "\t-f filename -- read a list of URLs from \n"); + HMENU menu = ::LoadMenu(hInstance,lpMenuName); + HWND hwnd = aWidget->GetNativeData(NS_NATIVE_WINDOW); + ::SetMenu(hwnd, menu); } +void nsWin32Viewer::AddMenu(nsIWidget* aMainWindow) +{ + AddViewerMenu(gInstance, aMainWindow, class1Name); +} + +//----------------------------------------------------------------- +// nsWin32Viewer Implementation +//----------------------------------------------------------------- + +void nsWin32Viewer::ShowConsole(WindowData* aWinData) +{ + 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)); + } + + 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); + + } + } +} + +void nsWin32Viewer::CloseConsole() +{ + DestroyConsole(); +} + +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); + } + } + } +} + + +// Selects all the Content +void nsWin32Viewer::CopyTextContent(WindowData* wd, HWND aHWnd) +{ + 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;iwindowWidget->GetNativeData(NS_NATIVE_WINDOW)); +} + +void nsWin32Viewer::Stop() +{ + PostQuitMessage(0); +} + +void nsWin32Viewer::Destroy(WindowData* wd) +{ + CloseConsole(); + nsViewer::Destroy(wd); +} + +int PASCAL +RunViewer(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow, nsWin32Viewer* aViewer) +{ + gInstance = instance; + gPrevInstance = prevInstance; + + SetViewer(aViewer); + + nsIWidget *mainWindow = nsnull; + nsDocLoader* dl = aViewer->SetupViewer(&mainWindow); + + // 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(); + } + } + + aViewer->CleanupViewer(dl); + + return msg.wParam; +} + + +//------------------------------------------------------------------ +// Win32 Main +//------------------------------------------------------------------ + void main(int argc, char **argv) { - for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-') { - if (strncmp(argv[i], "-p", 2) == 0) { - gDoPurify = PR_TRUE; - char *optionalSampleStopIndex = &(argv[i][2]); - if (nsnull!=*optionalSampleStopIndex) - { - if (1!=sscanf(optionalSampleStopIndex, "%d", &gNumSamples)) - { - PrintHelpInfo(argv); - exit(-1); - } - } - } - else if (strcmp(argv[i], "-q") == 0) { - gDoQuantify = PR_TRUE; - } - else if (strcmp(argv[i], "-f") == 0) { - gLoadTestFromFile = PR_TRUE; - i++; - if (i>=argc || nsnull==argv[i] || nsnull==*(argv[i])) - { - PrintHelpInfo(argv); - exit(-1); - } - strcpy(gInputFileName, argv[i]); - } - else if (strcmp(argv[i], "-d") == 0) { - i++; - if (i>=argc || 1!=sscanf(argv[i], "%d", &gDelay)) - { - PrintHelpInfo(argv); - exit(-1); - } - } - else if (strcmp(argv[i], "-md") == 0) { - if (i == argc - 1) { - PrintHelpInfo(argv); - exit(-1); - } - int oldFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); - int newFlags = atoi(argv[++i]); - _CrtSetDbgFlag(newFlags); - printf("Note: crt flags: old=%x new=%x\n", oldFlags, newFlags); - } - else if (strcmp(argv[i], "-r") == 0) { - i++; - if (i>=argc || 1!=sscanf(argv[i], "%d", &gRepeatCount)) - { - PrintHelpInfo(argv); - exit(-1); - } - } - else { - PrintHelpInfo(argv); - exit(-1); - } - } - else - break; - } - if (i < argc) { - startURL = argv[i]; - } - WinMain(GetModuleHandle(NULL), NULL, 0, SW_SHOW); + 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) +{ + nsWin32Viewer* viewer = new nsWin32Viewer(); + return(RunViewer(instance, prevInstance, cmdParam, nCmdShow, viewer)); }