Back to index

lightning-sunbird  0.9+nobinonly
nsEditorMode.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038  
00039 #include "nsCOMPtr.h"
00040 
00041 #include "nsEditorMode.h"
00042 #include "nsString.h"
00043 #include "nsReadableUtils.h"
00044 #include "nsIDOMDocument.h"
00045 
00046 #include "nsIEditor.h"
00047 #include "nsIHTMLEditor.h"
00048 #include "nsITableEditor.h"
00049 #include "nsIDocumentEncoder.h" // for output flags
00050 
00051 #include "nsEditorCID.h"
00052 
00053 #include "nsIComponentManager.h"
00054 #include "nsIServiceManager.h"
00055 #include "resources.h"
00056 #include "nsISelectionController.h"
00057 #include "nsIPresShell.h"
00058 
00059 static nsIEditor *gEditor;
00060 
00061 static NS_DEFINE_CID(kHTMLEditorCID, NS_HTMLEDITOR_CID);
00062 
00063 nsresult NS_InitEditorMode(nsIDOMDocument *aDOMDocument, nsIPresShell* aPresShell)
00064 {
00065   nsresult result = NS_OK;
00066 
00067   // This is a big leak if called > once, but its only temp test code, correct?
00068   if (gEditor)
00069     gEditor=nsnull;
00070 
00071   NS_ASSERTION(nsnull!=aDOMDocument, "null document");
00072   NS_ASSERTION(nsnull!=aPresShell, "null presentation shell");
00073 
00074   if ((nsnull==aDOMDocument) || (nsnull==aPresShell))
00075     return NS_ERROR_NULL_POINTER;
00076 
00077   result = CallCreateInstance(kHTMLEditorCID, &gEditor);
00078   if (NS_FAILED(result))
00079     return result;
00080   if (!gEditor) {
00081     return NS_ERROR_OUT_OF_MEMORY;
00082   }
00083   nsCOMPtr<nsISelectionController> selCon;
00084   selCon = do_QueryInterface(aPresShell);
00085   result = gEditor->Init(aDOMDocument, aPresShell, nsnull, selCon, 0);
00086   if (NS_SUCCEEDED(result))
00087     result = gEditor->PostCreate();
00088   return result;
00089 }
00090 
00091 
00092 static nsresult PrintEditorOutput(nsIEditor* editor, PRInt32 aCommandID)
00093 {
00094        nsString             outString;
00095        char*                cString;
00096        nsString             formatString;
00097     PRUint32        flags = 0;
00098        
00099        switch (aCommandID)
00100        {
00101       case VIEWER_DISPLAYTEXT:
00102         formatString.AssignLiteral("text/plain");
00103         flags = nsIDocumentEncoder::OutputFormatted;
00104         editor->OutputToString(formatString, flags, outString);
00105         break;
00106         
00107       case VIEWER_DISPLAYHTML:
00108         formatString.AssignLiteral("text/html");
00109         editor->OutputToString(formatString, flags, outString);
00110         break;
00111        }
00112 
00113        cString = ToNewCString(outString);
00114        printf(cString);
00115        delete [] cString;
00116        
00117        return NS_OK;
00118 }
00119 
00120 
00121 nsresult NS_DoEditorTest(PRInt32 aCommandID)
00122 {
00123   nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(gEditor);
00124   nsCOMPtr<nsITableEditor> tableEditor = do_QueryInterface(gEditor);
00125   if (htmlEditor && tableEditor)
00126   {
00127     switch(aCommandID)
00128     {
00129       case VIEWER_EDIT_SET_BGCOLOR_RED:
00130         htmlEditor->SetBodyAttribute(NS_LITERAL_STRING("bgcolor"), NS_LITERAL_STRING("red"));
00131         break;
00132       case VIEWER_EDIT_SET_BGCOLOR_YELLOW:
00133         htmlEditor->SetBodyAttribute(NS_LITERAL_STRING("bgcolor"), NS_LITERAL_STRING("yellow"));
00134         break;
00135       case VIEWER_EDIT_INSERT_CELL:  
00136         tableEditor->InsertTableCell(1, PR_FALSE);
00137         break;    
00138       case VIEWER_EDIT_INSERT_COLUMN:
00139         tableEditor->InsertTableColumn(1, PR_FALSE);
00140         break;    
00141       case VIEWER_EDIT_INSERT_ROW:    
00142         tableEditor->InsertTableRow(1, PR_FALSE);
00143         break;    
00144       case VIEWER_EDIT_DELETE_TABLE:  
00145         tableEditor->DeleteTable();
00146         break;    
00147       case VIEWER_EDIT_DELETE_CELL:  
00148         tableEditor->DeleteTableCell(1);
00149         break;    
00150       case VIEWER_EDIT_DELETE_COLUMN:
00151         tableEditor->DeleteTableColumn(1);
00152         break;    
00153       case VIEWER_EDIT_DELETE_ROW:
00154         tableEditor->DeleteTableRow(1);
00155         break;    
00156       case VIEWER_EDIT_JOIN_CELL_RIGHT:
00157         tableEditor->JoinTableCells(PR_FALSE);
00158         break;    
00159       case VIEWER_DISPLAYTEXT:
00160       case VIEWER_DISPLAYHTML:
00161         PrintEditorOutput(gEditor, aCommandID);
00162         break;
00163      default:
00164         return NS_ERROR_NOT_IMPLEMENTED;    
00165     }
00166   }
00167   return NS_OK;
00168 }
00169