diff --git a/mozilla/content/base/public/nsCopySupport.h b/mozilla/content/base/public/nsCopySupport.h index 5871045b10a..583a10ab8cc 100644 --- a/mozilla/content/base/public/nsCopySupport.h +++ b/mozilla/content/base/public/nsCopySupport.h @@ -46,5 +46,11 @@ class nsCopySupport // class of static helper functions for copy support public: static nsresult HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16 aClipboardID); + static nsresult IsPlainTextContext(nsISelection *aSel, nsIDocument *aDoc, PRBool *aIsPlainTextContext); + + // Get the selection, or entire document, in the format specified by the mime type + // (text/html or text/plain). If aSel is non-null, use it, otherwise get the entire + // doc. + static nsresult GetContents(const nsAString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata); }; diff --git a/mozilla/content/base/src/nsCopySupport.cpp b/mozilla/content/base/src/nsCopySupport.cpp index 0e34acc5500..74e4b81148d 100644 --- a/mozilla/content/base/src/nsCopySupport.cpp +++ b/mozilla/content/base/src/nsCopySupport.cpp @@ -333,3 +333,36 @@ nsresult nsCopySupport::IsPlainTextContext(nsISelection *aSel, nsIDocument *aDoc return NS_OK; } + + +nsresult +nsCopySupport::GetContents(const nsAString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata) +{ + nsresult rv = NS_OK; + + nsCOMPtr docEncoder; + + nsCAutoString encoderContractID(NS_DOC_ENCODER_CONTRACTID_BASE); + encoderContractID.AppendWithConversion(aMimeType); + + docEncoder = do_CreateInstance(encoderContractID.get()); + NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE); + + PRUint32 flags = aFlags; + + if (aMimeType.Equals(NS_LITERAL_STRING("text/plain"))) + flags |= nsIDocumentEncoder::OutputPreformatted; + + rv = docEncoder->Init(aDoc, aMimeType, flags); + if (NS_FAILED(rv)) return rv; + + if (aSel) + { + rv = docEncoder->SetSelection(aSel); + if (NS_FAILED(rv)) return rv; + } + + // encode the selection + return docEncoder->EncodeToString(outdata); +} + diff --git a/mozilla/content/base/src/nsDocumentViewer.cpp b/mozilla/content/base/src/nsDocumentViewer.cpp index d8703afcb31..4c0f5b847e5 100644 --- a/mozilla/content/base/src/nsDocumentViewer.cpp +++ b/mozilla/content/base/src/nsDocumentViewer.cpp @@ -47,6 +47,7 @@ #include "nsIContent.h" #include "nsIContentViewerContainer.h" #include "nsIDocumentViewer.h" +#include "nsIDocumentEncoder.h" #include "nsIDOMWindowInternal.h" #include "nsIDocument.h" @@ -5152,6 +5153,29 @@ NS_IMETHODIMP DocumentViewerImpl::GetPasteable(PRBool *aPasteable) return NS_OK; } +/* AString getContents (in string mimeType, in boolean selectionOnly); */ +NS_IMETHODIMP DocumentViewerImpl::GetContents(const char *mimeType, PRBool selectionOnly, nsAString & _retval) +{ + NS_ENSURE_TRUE(mPresShell, NS_ERROR_NOT_INITIALIZED); + nsAutoString mimeTypeString; mimeTypeString.AssignWithConversion(mimeType); + return mPresShell->DoGetContents(mimeTypeString, 0, selectionOnly, _retval); +} + +/* readonly attribute boolean canGetContents; */ +NS_IMETHODIMP DocumentViewerImpl::GetCanGetContents(PRBool *aCanGetContents) +{ + nsCOMPtr selection; + nsresult rv; + rv = GetDocumentSelection(getter_AddRefs(selection)); + if (NS_FAILED(rv)) return rv; + + PRBool isCollapsed; + selection->GetIsCollapsed(&isCollapsed); + + *aCanGetContents = !isCollapsed; + return NS_OK; +} + #ifdef XP_MAC #pragma mark - #endif diff --git a/mozilla/docshell/base/nsIContentViewerEdit.idl b/mozilla/docshell/base/nsIContentViewerEdit.idl index fe13f3ef1c0..c2c107df144 100644 --- a/mozilla/docshell/base/nsIContentViewerEdit.idl +++ b/mozilla/docshell/base/nsIContentViewerEdit.idl @@ -47,4 +47,7 @@ interface nsIContentViewerEdit : nsISupports void paste(); readonly attribute boolean pasteable; + + AString getContents(in string mimeType, in boolean selectionOnly); + readonly attribute boolean canGetContents; }; diff --git a/mozilla/dom/src/base/Makefile.in b/mozilla/dom/src/base/Makefile.in index f4611ad0cd7..beceb08890a 100644 --- a/mozilla/dom/src/base/Makefile.in +++ b/mozilla/dom/src/base/Makefile.in @@ -53,6 +53,7 @@ REQUIRES = xpcom \ shistory \ plugin \ windowwatcher \ + commandhandler \ htmlparser \ chardet \ find \ diff --git a/mozilla/dom/src/base/nsGlobalWindow.cpp b/mozilla/dom/src/base/nsGlobalWindow.cpp index c44ee737873..c2c4c96fec6 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.cpp +++ b/mozilla/dom/src/base/nsGlobalWindow.cpp @@ -129,6 +129,7 @@ #include "nsIJSNativeInitializer.h" #include "nsIFullScreen.h" #include "nsIStringBundle.h" +#include "nsICommandParams.h" #include "plbase64.h" @@ -5709,6 +5710,7 @@ const char * const sSelectNoneString = "cmd_selectNone"; const char * const sCopyLinkString = "cmd_copyLink"; const char * const sCopyImageLocationString = "cmd_copyImageLocation"; const char * const sCopyImageContentsString = "cmd_copyImageContents"; +const char * const sGetContentsString = "cmd_getContents"; const char * const sScrollTopString = "cmd_scrollTop"; const char * const sScrollBottomString = "cmd_scrollBottom"; @@ -5750,6 +5752,7 @@ NS_IMPL_RELEASE(nsDOMWindowController) NS_INTERFACE_MAP_BEGIN(nsDOMWindowController) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIController) NS_INTERFACE_MAP_ENTRY(nsIController) + NS_INTERFACE_MAP_ENTRY(nsICommandController) NS_INTERFACE_MAP_END @@ -5909,6 +5912,9 @@ nsDOMWindowController::IsCommandEnabled(const nsAString& aCommand, else if (commandName.Equals(sCopyImageContentsString)) { rv = editInterface->GetInImage(aResult); } + else if (commandName.Equals(sGetContentsString)) { + rv = editInterface->GetCanGetContents(aResult); + } return rv; } @@ -5934,6 +5940,7 @@ nsDOMWindowController::SupportsCommand(const nsAString& aCommand, commandName.Equals(sCopyLinkString) || commandName.Equals(sCopyImageLocationString) || commandName.Equals(sCopyImageContentsString) || + commandName.Equals(sGetContentsString) || commandName.Equals(sScrollPageUpString) || commandName.Equals(sScrollPageDownString) || commandName.Equals(sScrollLineUpString) || @@ -5962,6 +5969,56 @@ nsDOMWindowController::SupportsCommand(const nsAString& aCommand, return NS_OK; } +/* void getCommandState (in nsICommandParams aCommandParams); */ +NS_IMETHODIMP +nsDOMWindowController::GetCommandState(nsICommandParams *aCommandParams) +{ + // we don't have any commands that support this yet + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* void doCommand (in nsICommandParams aCommandParams); */ +NS_IMETHODIMP +nsDOMWindowController::DoCommand(nsICommandParams *aCommandParams) +{ + nsresult rv = NS_ERROR_FAILURE; + + if (!aCommandParams) + return rv; + + // get the command name + nsAutoString commandName; + aCommandParams->GetStringValue(NS_LITERAL_STRING("cmd_name"), commandName); + if (commandName.EqualsWithConversion(sGetContentsString)) + { + // get edit interface... + nsCOMPtr editInterface; + rv = GetEditInterface(getter_AddRefs(editInterface)); + // if we can't get an edit interface, that's bad + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(editInterface, NS_ERROR_NOT_INITIALIZED); + + // get the command params + nsCAutoString mimeType("text/plain"); + + nsAutoString format; + if (NS_SUCCEEDED(aCommandParams->GetStringValue(NS_LITERAL_STRING("format"), format))) + mimeType.AssignWithConversion(format); + + PRBool selectionOnly = PR_FALSE; + aCommandParams->GetBooleanValue(NS_LITERAL_STRING("selection_only"), &selectionOnly); + + nsAutoString contents; + rv = editInterface->GetContents(mimeType.get(), selectionOnly, contents); + if (NS_FAILED(rv)) + return rv; + + rv = aCommandParams->SetStringValue(NS_LITERAL_STRING("result"), contents); + } + + return rv; +} + NS_IMETHODIMP nsDOMWindowController::DoCommand(const nsAString & aCommand) { @@ -5969,6 +6026,7 @@ nsDOMWindowController::DoCommand(const nsAString & aCommand) nsCAutoString commandName; commandName.AssignWithConversion(aCommand); + if (commandName.Equals(sCopyString) || commandName.Equals(sSelectAllString) || commandName.Equals(sSelectNoneString) || diff --git a/mozilla/dom/src/base/nsGlobalWindow.h b/mozilla/dom/src/base/nsGlobalWindow.h index 1df853857e9..9a8c670119a 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.h +++ b/mozilla/dom/src/base/nsGlobalWindow.h @@ -438,14 +438,16 @@ class nsIContentViewerEdit; class nsISelectionController; -class nsDOMWindowController : public nsIController +class nsDOMWindowController : public nsIController, + public nsICommandController { public: - nsDOMWindowController( nsIDOMWindowInternal* aWindow ); - ~nsDOMWindowController(); + nsDOMWindowController( nsIDOMWindowInternal* aWindow ); + virtual ~nsDOMWindowController(); NS_DECL_ISUPPORTS NS_DECL_NSICONTROLLER - + NS_DECL_NSICOMMANDCONTROLLER + private: nsresult GetEventStateManager(nsIEventStateManager **esm); static int PR_CALLBACK BrowseWithCaretPrefCallback(const char* aPrefName, void* instance_data); diff --git a/mozilla/editor/idl/nsIPlaintextEditor.idl b/mozilla/editor/idl/nsIPlaintextEditor.idl index 2c0653d9373..3c3d892e791 100644 --- a/mozilla/editor/idl/nsIPlaintextEditor.idl +++ b/mozilla/editor/idl/nsIPlaintextEditor.idl @@ -107,6 +107,12 @@ interface nsIPlaintextEditor : nsISupports */ void insertText(in DOMString aStringToInsert); + /** + * Returns true if text can be inserted + * + */ + boolean canInsertText(); + /** * Insert a line break into the content model. * The interpretation of a break is up to the implementation: diff --git a/mozilla/editor/libeditor/base/nsEditorCommands.cpp b/mozilla/editor/libeditor/base/nsEditorCommands.cpp index c1fc3a4f3fe..29e6485453c 100644 --- a/mozilla/editor/libeditor/base/nsEditorCommands.cpp +++ b/mozilla/editor/libeditor/base/nsEditorCommands.cpp @@ -42,6 +42,7 @@ #include "nsIEditor.h" #include "nsIEditorMailSupport.h" +#include "nsIPlaintextEditor.h" #include "nsISelectionController.h" #include "nsIPresShell.h" #include "nsIClipboard.h" @@ -377,7 +378,53 @@ nsPasteCommand::GetCommandState(nsICommandParams *aParams, nsISupports *aCommand return aParams->SetBooleanValue(STATE_ENABLED,canUndo); } +#pragma mark - +NS_IMETHODIMP +nsInsertTextCommand::IsCommandEnabled(const nsAString & aCommandName, nsISupports *aCommandRefCon, PRBool *outCmdEnabled) +{ + nsCOMPtr aEditor = do_QueryInterface(aCommandRefCon); + *outCmdEnabled = PR_FALSE; + if (aEditor) + return aEditor->CanInsertText(outCmdEnabled); + + return NS_OK; +} + +NS_IMETHODIMP +nsInsertTextCommand::DoCommand(const nsAString & aCommandName, nsISupports *aCommandRefCon) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsInsertTextCommand::DoCommandParams(nsICommandParams *aParams, nsISupports *aCommandRefCon) +{ + nsAutoString tString; + aParams->GetStringValue(COMMAND_NAME, tString); + + nsAutoString value; + aParams->GetStringValue(NS_LITERAL_STRING("data"), value); + + nsCOMPtr aEditor = do_QueryInterface(aCommandRefCon); + if (!aEditor) + return NS_ERROR_FAILURE; + + return aEditor->InsertText(value); +} + +NS_IMETHODIMP +nsInsertTextCommand::GetCommandState(nsICommandParams *aParams, nsISupports *aCommandRefCon) +{ + nsString tString; + aParams->GetStringValue(COMMAND_NAME,tString); + PRBool canUndo; + IsCommandEnabled(tString, aCommandRefCon, &canUndo); + return aParams->SetBooleanValue(STATE_ENABLED,canUndo); +} + +#pragma mark - + NS_IMETHODIMP nsDeleteCommand::IsCommandEnabled(const nsAString & aCommandName, nsISupports *aCommandRefCon, PRBool *outCmdEnabled) { diff --git a/mozilla/editor/libeditor/base/nsEditorCommands.h b/mozilla/editor/libeditor/base/nsEditorCommands.h index 5e04dfc1368..5d0e94763cc 100644 --- a/mozilla/editor/libeditor/base/nsEditorCommands.h +++ b/mozilla/editor/libeditor/base/nsEditorCommands.h @@ -82,6 +82,7 @@ NS_DECL_EDITOR_COMMAND(nsCutOrDeleteCommand) NS_DECL_EDITOR_COMMAND(nsCopyCommand) NS_DECL_EDITOR_COMMAND(nsCopyOrDeleteCommand) NS_DECL_EDITOR_COMMAND(nsPasteCommand) +NS_DECL_EDITOR_COMMAND(nsInsertTextCommand) NS_DECL_EDITOR_COMMAND(nsDeleteCommand) NS_DECL_EDITOR_COMMAND(nsSelectAllCommand) diff --git a/mozilla/editor/libeditor/base/nsEditorController.cpp b/mozilla/editor/libeditor/base/nsEditorController.cpp index 3a607cf232c..52ed5ed65ed 100644 --- a/mozilla/editor/libeditor/base/nsEditorController.cpp +++ b/mozilla/editor/libeditor/base/nsEditorController.cpp @@ -51,6 +51,7 @@ NS_IMPL_RELEASE(nsEditorController) NS_INTERFACE_MAP_BEGIN(nsEditorController) NS_INTERFACE_MAP_ENTRY(nsIController) + NS_INTERFACE_MAP_ENTRY(nsICommandController) NS_INTERFACE_MAP_ENTRY(nsIEditorController) NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIEditorController) @@ -143,6 +144,7 @@ nsresult nsEditorController::RegisterEditorCommands(nsIControllerCommandManager NS_REGISTER_ONE_COMMAND(nsSelectAllCommand, "cmd_selectAll"); NS_REGISTER_ONE_COMMAND(nsPasteCommand, "cmd_paste"); + NS_REGISTER_ONE_COMMAND(nsInsertTextCommand, "cmd_insertText"); NS_REGISTER_FIRST_COMMAND(nsDeleteCommand, "cmd_delete"); NS_REGISTER_NEXT_COMMAND(nsDeleteCommand, "cmd_deleteCharBackward"); @@ -212,6 +214,17 @@ NS_IMETHODIMP nsEditorController::OnEvent(const nsAString & aEventName) return NS_OK; } +/* void getCommandState (in nsICommandParams aCommandParams); */ +NS_IMETHODIMP nsEditorController::GetCommandState(nsICommandParams *aCommandParams) +{ + return mCommandManager->GetCommandState(aCommandParams, mCommandRefCon); +} + +/* void doCommand (in nsICommandParams aCommandParams); */ +NS_IMETHODIMP nsEditorController::DoCommand(nsICommandParams *aCommandParams) +{ + return mCommandManager->DoCommandParams(aCommandParams, mCommandRefCon); +} nsWeakPtr nsEditorController::sEditorCommandManager = NULL; @@ -240,3 +253,4 @@ nsresult nsEditorController::GetEditorCommandManager(nsIControllerCommandManager return NS_OK; } + diff --git a/mozilla/editor/libeditor/base/nsEditorController.h b/mozilla/editor/libeditor/base/nsEditorController.h index a86135302f2..f55e2031781 100644 --- a/mozilla/editor/libeditor/base/nsEditorController.h +++ b/mozilla/editor/libeditor/base/nsEditorController.h @@ -61,6 +61,7 @@ class nsIEditor; // commands in composer. The refCon that gets passed to its commands is an nsIEditor. class nsEditorController : public nsIController, + public nsICommandController, public nsIEditorController, public nsIInterfaceRequestor { @@ -75,6 +76,9 @@ public: // nsIController NS_DECL_NSICONTROLLER + // nsICommandController + NS_DECL_NSICOMMANDCONTROLLER + /** init the controller */ NS_IMETHOD Init(nsISupports *aCommandRefCon); diff --git a/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp b/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp index 80d7462f1f7..ea9a90e2d13 100644 --- a/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp +++ b/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp @@ -993,6 +993,14 @@ NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert) return result; } +NS_IMETHODIMP nsPlaintextEditor::CanInsertText(PRBool *aCanInsert) +{ + if (!aCanInsert) + return NS_ERROR_NULL_POINTER; + + *aCanInsert = !IsModifiable(); + return NS_OK; +} NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak() { diff --git a/mozilla/layout/base/public/nsIPresShell.h b/mozilla/layout/base/public/nsIPresShell.h index 77ee18d6880..eafc11c75e2 100644 --- a/mozilla/layout/base/public/nsIPresShell.h +++ b/mozilla/layout/base/public/nsIPresShell.h @@ -434,6 +434,11 @@ public: NS_IMETHOD DoCopyImageLocation(nsIDOMNode* aNode) = 0; NS_IMETHOD DoCopyImageContents(nsIDOMNode* aNode) = 0; + /** + * Get the doc or the selection as text or html. + */ + NS_IMETHOD DoGetContents(const nsAString& aMimeType, PRUint32 aFlags, PRBool aSelectionOnly, nsAString& outValue) = 0; + /** * Get the caret, if it exists. AddRefs it. */ diff --git a/mozilla/layout/base/src/nsCopySupport.cpp b/mozilla/layout/base/src/nsCopySupport.cpp index 0e34acc5500..74e4b81148d 100644 --- a/mozilla/layout/base/src/nsCopySupport.cpp +++ b/mozilla/layout/base/src/nsCopySupport.cpp @@ -333,3 +333,36 @@ nsresult nsCopySupport::IsPlainTextContext(nsISelection *aSel, nsIDocument *aDoc return NS_OK; } + + +nsresult +nsCopySupport::GetContents(const nsAString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata) +{ + nsresult rv = NS_OK; + + nsCOMPtr docEncoder; + + nsCAutoString encoderContractID(NS_DOC_ENCODER_CONTRACTID_BASE); + encoderContractID.AppendWithConversion(aMimeType); + + docEncoder = do_CreateInstance(encoderContractID.get()); + NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE); + + PRUint32 flags = aFlags; + + if (aMimeType.Equals(NS_LITERAL_STRING("text/plain"))) + flags |= nsIDocumentEncoder::OutputPreformatted; + + rv = docEncoder->Init(aDoc, aMimeType, flags); + if (NS_FAILED(rv)) return rv; + + if (aSel) + { + rv = docEncoder->SetSelection(aSel); + if (NS_FAILED(rv)) return rv; + } + + // encode the selection + return docEncoder->EncodeToString(outdata); +} + diff --git a/mozilla/layout/base/src/nsCopySupport.h b/mozilla/layout/base/src/nsCopySupport.h index 5871045b10a..583a10ab8cc 100644 --- a/mozilla/layout/base/src/nsCopySupport.h +++ b/mozilla/layout/base/src/nsCopySupport.h @@ -46,5 +46,11 @@ class nsCopySupport // class of static helper functions for copy support public: static nsresult HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16 aClipboardID); + static nsresult IsPlainTextContext(nsISelection *aSel, nsIDocument *aDoc, PRBool *aIsPlainTextContext); + + // Get the selection, or entire document, in the format specified by the mime type + // (text/html or text/plain). If aSel is non-null, use it, otherwise get the entire + // doc. + static nsresult GetContents(const nsAString& aMimeType, PRUint32 aFlags, nsISelection *aSel, nsIDocument *aDoc, nsAString& outdata); }; diff --git a/mozilla/modules/plugin/base/src/nsPluginViewer.cpp b/mozilla/modules/plugin/base/src/nsPluginViewer.cpp index 72c755b7463..1040b978db3 100644 --- a/mozilla/modules/plugin/base/src/nsPluginViewer.cpp +++ b/mozilla/modules/plugin/base/src/nsPluginViewer.cpp @@ -835,6 +835,20 @@ NS_IMETHODIMP PluginViewerImpl::GetPasteable(PRBool *aPasteable) return NS_ERROR_NOT_IMPLEMENTED; } +/* AString getContents (in string mimeType, in boolean selectionOnly); */ +NS_IMETHODIMP PluginViewerImpl::GetContents(const char *mimeType, PRBool selectionOnly, nsAString & _retval) +{ + NS_ASSERTION(0, "NOT IMPLEMENTED"); + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* readonly attribute boolean canGetContents; */ +NS_IMETHODIMP PluginViewerImpl::GetCanGetContents(PRBool *aCanGetContents) +{ + NS_ASSERTION(0, "NOT IMPLEMENTED"); + return NS_ERROR_NOT_IMPLEMENTED; +} + /* ======================================================================================== * nsIWebBrowserPrint * ======================================================================================== */