michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "nsGlobalWindowCommands.h" michael@0: michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIDOMElement.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsCRT.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: #include "nsIControllerCommandTable.h" michael@0: #include "nsICommandParams.h" michael@0: michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsISelectionController.h" michael@0: #include "nsIWebNavigation.h" michael@0: #include "nsIContentViewerEdit.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsFocusManager.h" michael@0: #include "nsCopySupport.h" michael@0: #include "nsIClipboard.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/BasicEvents.h" michael@0: michael@0: #include "nsIClipboardDragDropHooks.h" michael@0: #include "nsIClipboardDragDropHookList.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: const char * const sSelectAllString = "cmd_selectAll"; michael@0: const char * const sSelectNoneString = "cmd_selectNone"; michael@0: const char * const sCopyImageLocationString = "cmd_copyImageLocation"; michael@0: const char * const sCopyImageContentsString = "cmd_copyImageContents"; michael@0: const char * const sCopyImageString = "cmd_copyImage"; michael@0: michael@0: const char * const sScrollTopString = "cmd_scrollTop"; michael@0: const char * const sScrollBottomString = "cmd_scrollBottom"; michael@0: const char * const sScrollPageUpString = "cmd_scrollPageUp"; michael@0: const char * const sScrollPageDownString = "cmd_scrollPageDown"; michael@0: const char * const sScrollLineUpString = "cmd_scrollLineUp"; michael@0: const char * const sScrollLineDownString = "cmd_scrollLineDown"; michael@0: const char * const sScrollLeftString = "cmd_scrollLeft"; michael@0: const char * const sScrollRightString = "cmd_scrollRight"; michael@0: const char * const sMoveTopString = "cmd_moveTop"; michael@0: const char * const sMoveBottomString = "cmd_moveBottom"; michael@0: const char * const sMovePageUpString = "cmd_movePageUp"; michael@0: const char * const sMovePageDownString = "cmd_movePageDown"; michael@0: const char * const sLinePreviousString = "cmd_linePrevious"; michael@0: const char * const sLineNextString = "cmd_lineNext"; michael@0: const char * const sCharPreviousString = "cmd_charPrevious"; michael@0: const char * const sCharNextString = "cmd_charNext"; michael@0: michael@0: // These are so the browser can use editor navigation key bindings michael@0: // helps with accessibility (boolean pref accessibility.browsewithcaret) michael@0: michael@0: const char * const sSelectCharPreviousString = "cmd_selectCharPrevious"; michael@0: const char * const sSelectCharNextString = "cmd_selectCharNext"; michael@0: michael@0: const char * const sWordPreviousString = "cmd_wordPrevious"; michael@0: const char * const sWordNextString = "cmd_wordNext"; michael@0: const char * const sSelectWordPreviousString = "cmd_selectWordPrevious"; michael@0: const char * const sSelectWordNextString = "cmd_selectWordNext"; michael@0: michael@0: const char * const sBeginLineString = "cmd_beginLine"; michael@0: const char * const sEndLineString = "cmd_endLine"; michael@0: const char * const sSelectBeginLineString = "cmd_selectBeginLine"; michael@0: const char * const sSelectEndLineString = "cmd_selectEndLine"; michael@0: michael@0: const char * const sSelectLinePreviousString = "cmd_selectLinePrevious"; michael@0: const char * const sSelectLineNextString = "cmd_selectLineNext"; michael@0: michael@0: const char * const sSelectPageUpString = "cmd_selectPageUp"; michael@0: const char * const sSelectPageDownString = "cmd_selectPageDown"; michael@0: michael@0: const char * const sSelectTopString = "cmd_selectTop"; michael@0: const char * const sSelectBottomString = "cmd_selectBottom"; michael@0: michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: // a base class for selection-related commands, for code sharing michael@0: class nsSelectionCommandsBase : public nsIControllerCommand michael@0: { michael@0: public: michael@0: virtual ~nsSelectionCommandsBase() {} michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_IMETHOD IsCommandEnabled(const char * aCommandName, nsISupports *aCommandContext, bool *_retval); michael@0: NS_IMETHOD GetCommandStateParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext); michael@0: NS_IMETHOD DoCommandParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext); michael@0: michael@0: protected: michael@0: michael@0: static nsresult GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell); michael@0: static nsresult GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon); michael@0: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: // this class implements commands whose behavior depends on the 'browse with caret' setting michael@0: class nsSelectMoveScrollCommand : public nsSelectionCommandsBase michael@0: { michael@0: public: michael@0: michael@0: NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext); michael@0: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: // this class implements other selection commands michael@0: class nsSelectCommand : public nsSelectionCommandsBase michael@0: { michael@0: public: michael@0: michael@0: NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext); michael@0: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSelectionCommandsBase, nsIControllerCommand) michael@0: michael@0: /* boolean isCommandEnabled (in string aCommandName, in nsISupports aCommandContext); */ michael@0: NS_IMETHODIMP michael@0: nsSelectionCommandsBase::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandContext, michael@0: bool *outCmdEnabled) michael@0: { michael@0: // XXX this needs fixing. e.g. you can't scroll up if you're already at the top of michael@0: // the document. michael@0: *outCmdEnabled = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void getCommandStateParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */ michael@0: NS_IMETHODIMP michael@0: nsSelectionCommandsBase::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, nsISupports *aCommandContext) michael@0: { michael@0: // XXX we should probably return the enabled state michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */ michael@0: NS_IMETHODIMP michael@0: nsSelectionCommandsBase::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, nsISupports *aCommandContext) michael@0: { michael@0: return DoCommand(aCommandName, aCommandContext); michael@0: } michael@0: michael@0: // protected methods michael@0: michael@0: nsresult michael@0: nsSelectionCommandsBase::GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell) michael@0: { michael@0: *aPresShell = nullptr; michael@0: NS_ENSURE_TRUE(aWindow, NS_ERROR_FAILURE); michael@0: michael@0: nsIDocShell *docShell = aWindow->GetDocShell(); michael@0: NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); michael@0: michael@0: NS_IF_ADDREF(*aPresShell = docShell->GetPresShell()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsSelectionCommandsBase::GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon) michael@0: { michael@0: *aSelCon = nullptr; michael@0: michael@0: nsCOMPtr presShell; michael@0: GetPresShellFromWindow(aWindow, getter_AddRefs(presShell)); michael@0: if (presShell) michael@0: return CallQueryInterface(presShell, aSelCon); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: static const struct BrowseCommand { michael@0: const char *reverse, *forward; michael@0: nsresult (NS_STDCALL nsISelectionController::*scroll)(bool); michael@0: nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool); michael@0: } browseCommands[] = { michael@0: { sScrollTopString, sScrollBottomString, michael@0: &nsISelectionController::CompleteScroll }, michael@0: { sScrollPageUpString, sScrollPageDownString, michael@0: &nsISelectionController::ScrollPage }, michael@0: { sScrollLineUpString, sScrollLineDownString, michael@0: &nsISelectionController::ScrollLine }, michael@0: { sScrollLeftString, sScrollRightString, michael@0: &nsISelectionController::ScrollCharacter }, michael@0: { sMoveTopString, sMoveBottomString, michael@0: &nsISelectionController::CompleteScroll, michael@0: &nsISelectionController::CompleteMove }, michael@0: { sMovePageUpString, sMovePageDownString, michael@0: &nsISelectionController::ScrollPage, michael@0: &nsISelectionController::PageMove }, michael@0: { sLinePreviousString, sLineNextString, michael@0: &nsISelectionController::ScrollLine, michael@0: &nsISelectionController::LineMove }, michael@0: { sWordPreviousString, sWordNextString, michael@0: &nsISelectionController::ScrollCharacter, michael@0: &nsISelectionController::WordMove }, michael@0: { sCharPreviousString, sCharNextString, michael@0: &nsISelectionController::ScrollCharacter, michael@0: &nsISelectionController::CharacterMove }, michael@0: { sBeginLineString, sEndLineString, michael@0: &nsISelectionController::CompleteScroll, michael@0: &nsISelectionController::IntraLineMove } michael@0: }; michael@0: michael@0: nsresult michael@0: nsSelectMoveScrollCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext) michael@0: { michael@0: nsCOMPtr piWindow(do_QueryInterface(aCommandContext)); michael@0: nsCOMPtr selCont; michael@0: GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont)); michael@0: NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: // We allow the caret to be moved with arrow keys on any window for which michael@0: // the caret is enabled. In particular, this includes caret-browsing mode michael@0: // in non-chrome documents. michael@0: bool caretOn = false; michael@0: selCont->GetCaretEnabled(&caretOn); michael@0: if (!caretOn) { michael@0: caretOn = Preferences::GetBool("accessibility.browsewithcaret"); michael@0: if (caretOn) { michael@0: nsCOMPtr docShell = piWindow->GetDocShell(); michael@0: if (docShell && docShell->ItemType() == nsIDocShellTreeItem::typeChrome) { michael@0: caretOn = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (size_t i = 0; i < ArrayLength(browseCommands); i++) { michael@0: bool forward = !strcmp(aCommandName, browseCommands[i].forward); michael@0: if (forward || !strcmp(aCommandName, browseCommands[i].reverse)) { michael@0: if (caretOn && browseCommands[i].move && michael@0: NS_SUCCEEDED((selCont->*(browseCommands[i].move))(forward, false))) { michael@0: // adjust the focus to the new caret position michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) { michael@0: nsCOMPtr result; michael@0: fm->MoveFocus(piWindow, nullptr, nsIFocusManager::MOVEFOCUS_CARET, michael@0: nsIFocusManager::FLAG_NOSCROLL, michael@0: getter_AddRefs(result)); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: return (selCont->*(browseCommands[i].scroll))(forward); michael@0: } michael@0: } michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: nsresult michael@0: nsSelectCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext) michael@0: { michael@0: nsCOMPtr piWindow(do_QueryInterface(aCommandContext)); michael@0: nsCOMPtr selCont; michael@0: GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont)); michael@0: NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: nsresult rv = NS_ERROR_NOT_IMPLEMENTED; michael@0: michael@0: // These commands are so the browser can use caret navigation key bindings - michael@0: // Helps with accessibility - aaronl@netscape.com michael@0: if (!nsCRT::strcmp(aCommandName, sSelectCharPreviousString)) michael@0: rv = selCont->CharacterMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectCharNextString)) michael@0: rv = selCont->CharacterMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectWordPreviousString)) michael@0: rv = selCont->WordMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectWordNextString)) michael@0: rv = selCont->WordMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectBeginLineString)) michael@0: rv = selCont->IntraLineMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectEndLineString)) michael@0: rv = selCont->IntraLineMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectLinePreviousString)) michael@0: rv = selCont->LineMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectLineNextString)) michael@0: rv = selCont->LineMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectPageUpString)) michael@0: rv = selCont->PageMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectPageDownString)) michael@0: rv = selCont->PageMove(true, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectTopString)) michael@0: rv = selCont->CompleteMove(false, true); michael@0: else if (!nsCRT::strcmp(aCommandName, sSelectBottomString)) michael@0: rv = selCont->CompleteMove(true, true); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: class nsClipboardCommand MOZ_FINAL : public nsIControllerCommand michael@0: { michael@0: public: michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSICONTROLLERCOMMAND michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsClipboardCommand, nsIControllerCommand) michael@0: michael@0: nsresult michael@0: nsClipboardCommand::IsCommandEnabled(const char* aCommandName, nsISupports *aContext, bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: *outCmdEnabled = false; michael@0: michael@0: if (strcmp(aCommandName, "cmd_copy")) michael@0: return NS_OK; michael@0: michael@0: nsCOMPtr window = do_QueryInterface(aContext); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr doc = window->GetExtantDoc(); michael@0: *outCmdEnabled = nsCopySupport::CanCopy(doc); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext) michael@0: { michael@0: if (strcmp(aCommandName, "cmd_copy")) michael@0: return NS_OK; michael@0: michael@0: nsCOMPtr window = do_QueryInterface(aContext); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); michael@0: michael@0: nsIDocShell *docShell = window->GetDocShell(); michael@0: NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr presShell = docShell->GetPresShell(); michael@0: NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE); michael@0: michael@0: nsCopySupport::FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard, presShell, nullptr); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClipboardCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, nsISupports *aCommandContext) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardCommand::DoCommandParams(const char *aCommandName, nsICommandParams* aParams, nsISupports *aContext) michael@0: { michael@0: return DoCommand(aCommandName, aContext); michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: class nsSelectionCommand : public nsIControllerCommand michael@0: { michael@0: public: michael@0: virtual ~nsSelectionCommand() {} michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSICONTROLLERCOMMAND michael@0: michael@0: protected: michael@0: michael@0: virtual nsresult IsClipboardCommandEnabled(const char * aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) = 0; michael@0: virtual nsresult DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) = 0; michael@0: michael@0: static nsresult GetContentViewerEditFromContext(nsISupports *aContext, nsIContentViewerEdit **aEditInterface); michael@0: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSelectionCommand, nsIControllerCommand) michael@0: michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: michael@0: nsSelectionCommand michael@0: michael@0: ----------------------------------------------------------------------------*/ michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandContext, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: *outCmdEnabled = false; michael@0: michael@0: nsCOMPtr contentEdit; michael@0: GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit)); michael@0: NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: return IsClipboardCommandEnabled(aCommandName, contentEdit, outCmdEnabled); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: nsCOMPtr contentEdit; michael@0: GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit)); michael@0: NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: return DoClipboardCommand(aCommandName, contentEdit, nullptr); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSelectionCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: nsCOMPtr contentEdit; michael@0: GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit)); michael@0: NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: return DoClipboardCommand(aCommandName, contentEdit, aParams); michael@0: } michael@0: michael@0: nsresult michael@0: nsSelectionCommand::GetContentViewerEditFromContext(nsISupports *aContext, michael@0: nsIContentViewerEdit **aEditInterface) michael@0: { michael@0: NS_ENSURE_ARG(aEditInterface); michael@0: *aEditInterface = nullptr; michael@0: michael@0: nsCOMPtr window = do_QueryInterface(aContext); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG); michael@0: michael@0: nsIDocShell *docShell = window->GetDocShell(); michael@0: NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr viewer; michael@0: docShell->GetContentViewer(getter_AddRefs(viewer)); michael@0: nsCOMPtr edit(do_QueryInterface(viewer)); michael@0: NS_ENSURE_TRUE(edit, NS_ERROR_FAILURE); michael@0: michael@0: *aEditInterface = edit; michael@0: NS_ADDREF(*aEditInterface); michael@0: return NS_OK; michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: #define NS_DECL_CLIPBOARD_COMMAND(_cmd) \ michael@0: class _cmd : public nsSelectionCommand \ michael@0: { \ michael@0: protected: \ michael@0: \ michael@0: virtual nsresult IsClipboardCommandEnabled(const char* aCommandName, \ michael@0: nsIContentViewerEdit* aEdit, bool *outCmdEnabled); \ michael@0: virtual nsresult DoClipboardCommand(const char* aCommandName, \ michael@0: nsIContentViewerEdit* aEdit, nsICommandParams* aParams); \ michael@0: /* no member variables, please, we're stateless! */ \ michael@0: }; michael@0: michael@0: NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyLinkCommand) michael@0: NS_DECL_CLIPBOARD_COMMAND(nsClipboardImageCommands) michael@0: NS_DECL_CLIPBOARD_COMMAND(nsClipboardSelectAllNoneCommands) michael@0: NS_DECL_CLIPBOARD_COMMAND(nsClipboardGetContentsCommand) michael@0: michael@0: nsresult michael@0: nsClipboardCopyLinkCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) michael@0: { michael@0: return aEdit->GetInLink(outCmdEnabled); michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardCopyLinkCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) michael@0: { michael@0: return aEdit->CopyLinkLocation(); michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: nsresult michael@0: nsClipboardImageCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) michael@0: { michael@0: return aEdit->GetInImage(outCmdEnabled); michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardImageCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) michael@0: { michael@0: if (!nsCRT::strcmp(sCopyImageLocationString, aCommandName)) michael@0: return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_TEXT); michael@0: if (!nsCRT::strcmp(sCopyImageContentsString, aCommandName)) michael@0: return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_DATA); michael@0: int32_t copyFlags = nsIContentViewerEdit::COPY_IMAGE_DATA | michael@0: nsIContentViewerEdit::COPY_IMAGE_HTML; michael@0: if (aParams) michael@0: aParams->GetLongValue("imageCopy", ©Flags); michael@0: return aEdit->CopyImage(copyFlags); michael@0: } michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: nsresult michael@0: nsClipboardSelectAllNoneCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) michael@0: { michael@0: *outCmdEnabled = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardSelectAllNoneCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) michael@0: { michael@0: if (!nsCRT::strcmp(sSelectAllString, aCommandName)) michael@0: return aEdit->SelectAll(); michael@0: michael@0: return aEdit->ClearSelection(); michael@0: } michael@0: michael@0: michael@0: #if 0 michael@0: #pragma mark - michael@0: #endif michael@0: michael@0: nsresult michael@0: nsClipboardGetContentsCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) michael@0: { michael@0: return aEdit->GetCanGetContents(outCmdEnabled); michael@0: } michael@0: michael@0: nsresult michael@0: nsClipboardGetContentsCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) michael@0: { michael@0: NS_ENSURE_ARG(aParams); michael@0: michael@0: nsAutoCString mimeType("text/plain"); michael@0: michael@0: nsXPIDLCString format; // nsICommandParams needs to use nsACString michael@0: if (NS_SUCCEEDED(aParams->GetCStringValue("format", getter_Copies(format)))) michael@0: mimeType.Assign(format); michael@0: michael@0: bool selectionOnly = false; michael@0: aParams->GetBooleanValue("selection_only", &selectionOnly); michael@0: michael@0: nsAutoString contents; michael@0: nsresult rv = aEdit->GetContents(mimeType.get(), selectionOnly, contents); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: return aParams->SetStringValue("result", contents); michael@0: } michael@0: michael@0: #if 0 // Remove unless needed again, bug 204777 michael@0: class nsWebNavigationBaseCommand : public nsIControllerCommand michael@0: { michael@0: public: michael@0: virtual ~nsWebNavigationBaseCommand() {} michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSICONTROLLERCOMMAND michael@0: michael@0: protected: michael@0: michael@0: virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled) = 0; michael@0: virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) = 0; michael@0: michael@0: static nsresult GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation); michael@0: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: class nsGoForwardCommand : public nsWebNavigationBaseCommand michael@0: { michael@0: protected: michael@0: michael@0: virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled); michael@0: virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation); michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: class nsGoBackCommand : public nsWebNavigationBaseCommand michael@0: { michael@0: protected: michael@0: michael@0: virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled); michael@0: virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation); michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: michael@0: nsWebNavigationCommands michael@0: no params michael@0: ----------------------------------------------------------------------------*/ michael@0: michael@0: NS_IMPL_ISUPPORTS(nsWebNavigationBaseCommand, nsIControllerCommand) michael@0: michael@0: NS_IMETHODIMP michael@0: nsWebNavigationBaseCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandContext, michael@0: bool *outCmdEnabled) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(outCmdEnabled); michael@0: *outCmdEnabled = false; michael@0: michael@0: nsCOMPtr webNav; michael@0: GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav)); michael@0: NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG); michael@0: michael@0: return IsCommandEnabled(aCommandName, webNav, outCmdEnabled); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWebNavigationBaseCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, nsISupports *aCommandContext) michael@0: { michael@0: // XXX we should probably return the enabled state michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsWebNavigationBaseCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: nsCOMPtr webNav; michael@0: GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav)); michael@0: NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG); michael@0: michael@0: return DoWebNavCommand(aCommandName, webNav); michael@0: } michael@0: michael@0: /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */ michael@0: NS_IMETHODIMP michael@0: nsWebNavigationBaseCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, nsISupports *aCommandContext) michael@0: { michael@0: return DoCommand(aCommandName, aCommandContext); michael@0: } michael@0: michael@0: nsresult michael@0: nsWebNavigationBaseCommand::GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation) michael@0: { michael@0: nsCOMPtr windowReq = do_QueryInterface(aContext); michael@0: CallGetInterface(windowReq.get(), aWebNavigation); michael@0: return (*aWebNavigation) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: nsGoForwardCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled) michael@0: { michael@0: return aWebNavigation->GetCanGoForward(outCmdEnabled); michael@0: } michael@0: michael@0: nsresult michael@0: nsGoForwardCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) michael@0: { michael@0: return aWebNavigation->GoForward(); michael@0: } michael@0: michael@0: nsresult michael@0: nsGoBackCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled) michael@0: { michael@0: return aWebNavigation->GetCanGoBack(outCmdEnabled); michael@0: } michael@0: michael@0: nsresult michael@0: nsGoBackCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) michael@0: { michael@0: return aWebNavigation->GoBack(); michael@0: } michael@0: #endif michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: michael@0: nsClipboardDragDropHookCommand michael@0: params value type possible values michael@0: "addhook" isupports nsIClipboardDragDropHooks as nsISupports michael@0: "removehook" isupports nsIClipboardDragDropHooks as nsISupports michael@0: michael@0: ----------------------------------------------------------------------------*/ michael@0: michael@0: class nsClipboardDragDropHookCommand MOZ_FINAL : public nsIControllerCommand michael@0: { michael@0: public: michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSICONTROLLERCOMMAND michael@0: michael@0: protected: michael@0: // no member variables, please, we're stateless! michael@0: }; michael@0: michael@0: michael@0: NS_IMPL_ISUPPORTS(nsClipboardDragDropHookCommand, nsIControllerCommand) michael@0: michael@0: NS_IMETHODIMP michael@0: nsClipboardDragDropHookCommand::IsCommandEnabled(const char * aCommandName, michael@0: nsISupports *aCommandContext, michael@0: bool *outCmdEnabled) michael@0: { michael@0: *outCmdEnabled = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClipboardDragDropHookCommand::DoCommand(const char *aCommandName, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClipboardDragDropHookCommand::DoCommandParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: NS_ENSURE_ARG(aParams); michael@0: michael@0: nsCOMPtr window = do_QueryInterface(aCommandContext); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); michael@0: michael@0: nsIDocShell *docShell = window->GetDocShell(); michael@0: michael@0: nsCOMPtr obj = do_GetInterface(docShell); michael@0: if (!obj) return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsCOMPtr isuppHook; michael@0: michael@0: nsresult returnValue = NS_OK; michael@0: nsresult rv = aParams->GetISupportsValue("addhook", getter_AddRefs(isuppHook)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: { michael@0: nsCOMPtr hook = do_QueryInterface(isuppHook); michael@0: if (hook) michael@0: returnValue = obj->AddClipboardDragDropHooks(hook); michael@0: else michael@0: returnValue = NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: rv = aParams->GetISupportsValue("removehook", getter_AddRefs(isuppHook)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: { michael@0: nsCOMPtr hook = do_QueryInterface(isuppHook); michael@0: if (hook) michael@0: { michael@0: rv = obj->RemoveClipboardDragDropHooks(hook); michael@0: if (NS_FAILED(rv) && NS_SUCCEEDED(returnValue)) michael@0: returnValue = rv; michael@0: } michael@0: else michael@0: returnValue = NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: return returnValue; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsClipboardDragDropHookCommand::GetCommandStateParams(const char *aCommandName, michael@0: nsICommandParams *aParams, michael@0: nsISupports *aCommandContext) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aParams); michael@0: return aParams->SetBooleanValue("state_enabled", true); michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: michael@0: RegisterWindowCommands michael@0: michael@0: ----------------------------------------------------------------------------*/ michael@0: michael@0: #define NS_REGISTER_ONE_COMMAND(_cmdClass, _cmdName) \ michael@0: { \ michael@0: _cmdClass* theCmd = new _cmdClass(); \ michael@0: if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \ michael@0: rv = inCommandTable->RegisterCommand(_cmdName, \ michael@0: static_cast(theCmd)); \ michael@0: } michael@0: michael@0: #define NS_REGISTER_FIRST_COMMAND(_cmdClass, _cmdName) \ michael@0: { \ michael@0: _cmdClass* theCmd = new _cmdClass(); \ michael@0: if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \ michael@0: rv = inCommandTable->RegisterCommand(_cmdName, \ michael@0: static_cast(theCmd)); michael@0: michael@0: #define NS_REGISTER_NEXT_COMMAND(_cmdClass, _cmdName) \ michael@0: rv = inCommandTable->RegisterCommand(_cmdName, \ michael@0: static_cast(theCmd)); michael@0: michael@0: #define NS_REGISTER_LAST_COMMAND(_cmdClass, _cmdName) \ michael@0: rv = inCommandTable->RegisterCommand(_cmdName, \ michael@0: static_cast(theCmd)); \ michael@0: } michael@0: michael@0: michael@0: // static michael@0: nsresult michael@0: nsWindowCommandRegistration::RegisterWindowCommands( michael@0: nsIControllerCommandTable *inCommandTable) michael@0: { michael@0: nsresult rv; michael@0: michael@0: // XXX rework the macros to use a loop is possible, reducing code size michael@0: michael@0: // this set of commands is affected by the 'browse with caret' setting michael@0: NS_REGISTER_FIRST_COMMAND(nsSelectMoveScrollCommand, sScrollTopString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollBottomString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageUpString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageDownString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineUpString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineDownString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLeftString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollRightString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveTopString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveBottomString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordPreviousString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordNextString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sBeginLineString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sEndLineString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageUpString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageDownString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLinePreviousString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLineNextString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sCharPreviousString); michael@0: NS_REGISTER_LAST_COMMAND(nsSelectMoveScrollCommand, sCharNextString); michael@0: michael@0: NS_REGISTER_FIRST_COMMAND(nsSelectCommand, sSelectCharPreviousString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectCharNextString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordPreviousString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordNextString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectBeginLineString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectEndLineString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLinePreviousString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLineNextString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageUpString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageDownString); michael@0: NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectTopString); michael@0: NS_REGISTER_LAST_COMMAND(nsSelectCommand, sSelectBottomString); michael@0: michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_cut"); michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_copy"); michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_paste"); michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardCopyLinkCommand, "cmd_copyLink"); michael@0: NS_REGISTER_FIRST_COMMAND(nsClipboardImageCommands, sCopyImageLocationString); michael@0: NS_REGISTER_NEXT_COMMAND(nsClipboardImageCommands, sCopyImageContentsString); michael@0: NS_REGISTER_LAST_COMMAND(nsClipboardImageCommands, sCopyImageString); michael@0: NS_REGISTER_FIRST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectAllString); michael@0: NS_REGISTER_LAST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectNoneString); michael@0: michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardGetContentsCommand, "cmd_getContents"); michael@0: michael@0: #if 0 // Remove unless needed again, bug 204777 michael@0: NS_REGISTER_ONE_COMMAND(nsGoBackCommand, "cmd_browserBack"); michael@0: NS_REGISTER_ONE_COMMAND(nsGoForwardCommand, "cmd_browserForward"); michael@0: #endif michael@0: michael@0: NS_REGISTER_ONE_COMMAND(nsClipboardDragDropHookCommand, "cmd_clipboardDragDropHook"); michael@0: michael@0: return rv; michael@0: }