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

165 lines
4.5 KiB
C++
Raw Normal View History

1998-06-24 00:29:00 +00:00
/* -*- Mode: C++; tab-width: 4; 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.
*/
1999-08-09 01:43:24 +00:00
#include "nsCOMPtr.h"
1998-06-24 00:29:00 +00:00
#include "nsEditorMode.h"
#include "nsString.h"
#include "nsIDOMDocument.h"
1998-06-24 00:29:00 +00:00
#include "nsIEditor.h"
#include "nsIHTMLEditor.h"
1999-08-09 01:43:24 +00:00
#include "nsITableEditor.h"
1999-01-06 20:31:08 +00:00
#include "nsEditorCID.h"
#include "nsIComponentManager.h"
1999-01-06 20:31:08 +00:00
#include "nsIServiceManager.h"
#include "resources.h"
1999-01-06 20:31:08 +00:00
1999-08-09 01:43:24 +00:00
static nsIEditor *gEditor;
1998-06-24 00:29:00 +00:00
static NS_DEFINE_CID(kHTMLEditorCID, NS_HTMLEDITOR_CID);
1999-01-06 20:31:08 +00:00
static NS_DEFINE_CID(kEditorCID, NS_EDITOR_CID);
#ifdef XP_PC
#define EDITOR_DLL "ender.dll"
#else
#ifdef XP_MAC
#define EDITOR_DLL "ENDER_DLL"
1999-06-26 07:16:51 +00:00
#else // XP_UNIX || XP_BEOS
#define EDITOR_DLL "libender"MOZ_DLL_SUFFIX
1999-01-06 20:31:08 +00:00
#endif
#endif
1998-06-24 00:29:00 +00:00
nsresult NS_InitEditorMode(nsIDOMDocument *aDOMDocument, nsIPresShell* aPresShell)
1998-06-24 00:29:00 +00:00
{
1999-01-06 20:31:08 +00:00
nsresult result = NS_OK;
1999-07-03 00:26:59 +00:00
// This is a big leak if called > once, but its only temp test code, correct?
if (gEditor)
gEditor=nsnull;
1999-01-06 20:31:08 +00:00
NS_ASSERTION(nsnull!=aDOMDocument, "null document");
NS_ASSERTION(nsnull!=aPresShell, "null presentation shell");
if ((nsnull==aDOMDocument) || (nsnull==aPresShell))
return NS_ERROR_NULL_POINTER;
/*
1999-01-06 20:31:08 +00:00
nsISupports *isup = nsnull;
result = nsServiceManager::GetService(kTextEditorCID,
kITextEditorIID, &isup);
*/
result = nsComponentManager::CreateInstance(kHTMLEditorCID,
nsnull,
1999-08-09 01:43:24 +00:00
nsIEditor::GetIID(), (void **)&gEditor);
if (NS_FAILED(result))
return result;
if (!gEditor) {
1999-01-06 20:31:08 +00:00
return NS_ERROR_OUT_OF_MEMORY;
}
1999-08-09 01:43:24 +00:00
result = gEditor->Init(aDOMDocument, aPresShell, 0);
1999-01-06 20:31:08 +00:00
return result;
1998-06-24 00:29:00 +00:00
}
1999-03-06 20:49:34 +00:00
1999-08-09 01:43:24 +00:00
static nsresult PrintEditorOutput(nsIEditor* editor, PRInt32 aCommandID)
1999-03-06 20:49:34 +00:00
{
nsString outString;
char* cString;
nsString formatString;
PRUint32 flags;
1999-03-06 20:49:34 +00:00
switch (aCommandID)
{
case VIEWER_DISPLAYTEXT:
formatString = "text/plain";
flags = nsIEditor::EditorOutputFormatted;
1999-08-09 01:43:24 +00:00
editor->OutputToString(outString, formatString, flags);
1999-03-06 20:49:34 +00:00
break;
case VIEWER_DISPLAYHTML:
formatString = "text/html";
1999-08-09 01:43:24 +00:00
editor->OutputToString(outString, formatString, flags);
1999-03-06 20:49:34 +00:00
break;
}
cString = outString.ToNewCString();
printf(cString);
delete [] cString;
return NS_OK;
}
nsresult NS_DoEditorTest(PRInt32 aCommandID)
{
1999-08-09 01:43:24 +00:00
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(gEditor);
nsCOMPtr<nsITableEditor> tableEditor = do_QueryInterface(gEditor);
if (htmlEditor && tableEditor)
{
switch(aCommandID)
{
case VIEWER_EDIT_SET_BGCOLOR_RED:
1999-08-09 01:43:24 +00:00
htmlEditor->SetBodyAttribute("bgcolor", "red");
break;
case VIEWER_EDIT_SET_BGCOLOR_YELLOW:
1999-08-09 01:43:24 +00:00
htmlEditor->SetBodyAttribute("bgcolor", "yellow");
break;
case VIEWER_EDIT_INSERT_CELL:
1999-08-09 01:43:24 +00:00
tableEditor->InsertTableCell(1, PR_FALSE);
break;
case VIEWER_EDIT_INSERT_COLUMN:
1999-08-09 01:43:24 +00:00
tableEditor->InsertTableColumn(1, PR_FALSE);
break;
case VIEWER_EDIT_INSERT_ROW:
1999-08-09 01:43:24 +00:00
tableEditor->InsertTableRow(1, PR_FALSE);
break;
case VIEWER_EDIT_DELETE_TABLE:
1999-08-09 01:43:24 +00:00
tableEditor->DeleteTable();
break;
case VIEWER_EDIT_DELETE_CELL:
1999-08-09 01:43:24 +00:00
tableEditor->DeleteTableCell(1);
break;
case VIEWER_EDIT_DELETE_COLUMN:
1999-08-09 01:43:24 +00:00
tableEditor->DeleteTableColumn(1);
break;
case VIEWER_EDIT_DELETE_ROW:
1999-08-09 01:43:24 +00:00
tableEditor->DeleteTableRow(1);
break;
case VIEWER_EDIT_JOIN_CELL_RIGHT:
1999-08-09 01:43:24 +00:00
tableEditor->JoinTableCells();
break;
case VIEWER_EDIT_JOIN_CELL_BELOW:
1999-08-09 01:43:24 +00:00
tableEditor->JoinTableCells();
break;
case VIEWER_DISPLAYTEXT:
1999-03-06 20:49:34 +00:00
case VIEWER_DISPLAYHTML:
PrintEditorOutput(gEditor, aCommandID);
break;
default:
return NS_ERROR_NOT_IMPLEMENTED;
}
}
return NS_OK;
}