Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsGlobalWindowCommands.cpp
blob194fbef82db00da35240888c598126c009e43916
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsGlobalWindowCommands.h"
8 #include "nsIComponentManager.h"
9 #include "nsIDOMElement.h"
10 #include "nsIInterfaceRequestor.h"
11 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsCRT.h"
13 #include "nsString.h"
14 #include "mozilla/ArrayUtils.h"
15 #include "mozilla/Preferences.h"
17 #include "nsIControllerCommandTable.h"
18 #include "nsICommandParams.h"
20 #include "nsPIDOMWindow.h"
21 #include "nsIPresShell.h"
22 #include "nsIDocShell.h"
23 #include "nsISelectionController.h"
24 #include "nsIWebNavigation.h"
25 #include "nsIContentViewerEdit.h"
26 #include "nsIContentViewer.h"
27 #include "nsFocusManager.h"
28 #include "nsCopySupport.h"
29 #include "nsIClipboard.h"
30 #include "mozilla/Attributes.h"
31 #include "mozilla/BasicEvents.h"
33 #include "nsIClipboardDragDropHooks.h"
34 #include "nsIClipboardDragDropHookList.h"
36 using namespace mozilla;
38 const char * const sSelectAllString = "cmd_selectAll";
39 const char * const sSelectNoneString = "cmd_selectNone";
40 const char * const sCopyImageLocationString = "cmd_copyImageLocation";
41 const char * const sCopyImageContentsString = "cmd_copyImageContents";
42 const char * const sCopyImageString = "cmd_copyImage";
44 const char * const sScrollTopString = "cmd_scrollTop";
45 const char * const sScrollBottomString = "cmd_scrollBottom";
46 const char * const sScrollPageUpString = "cmd_scrollPageUp";
47 const char * const sScrollPageDownString = "cmd_scrollPageDown";
48 const char * const sScrollLineUpString = "cmd_scrollLineUp";
49 const char * const sScrollLineDownString = "cmd_scrollLineDown";
50 const char * const sScrollLeftString = "cmd_scrollLeft";
51 const char * const sScrollRightString = "cmd_scrollRight";
52 const char * const sMoveTopString = "cmd_moveTop";
53 const char * const sMoveBottomString = "cmd_moveBottom";
54 const char * const sMovePageUpString = "cmd_movePageUp";
55 const char * const sMovePageDownString = "cmd_movePageDown";
56 const char * const sLinePreviousString = "cmd_linePrevious";
57 const char * const sLineNextString = "cmd_lineNext";
58 const char * const sCharPreviousString = "cmd_charPrevious";
59 const char * const sCharNextString = "cmd_charNext";
61 // These are so the browser can use editor navigation key bindings
62 // helps with accessibility (boolean pref accessibility.browsewithcaret)
64 const char * const sSelectCharPreviousString = "cmd_selectCharPrevious";
65 const char * const sSelectCharNextString = "cmd_selectCharNext";
67 const char * const sWordPreviousString = "cmd_wordPrevious";
68 const char * const sWordNextString = "cmd_wordNext";
69 const char * const sSelectWordPreviousString = "cmd_selectWordPrevious";
70 const char * const sSelectWordNextString = "cmd_selectWordNext";
72 const char * const sBeginLineString = "cmd_beginLine";
73 const char * const sEndLineString = "cmd_endLine";
74 const char * const sSelectBeginLineString = "cmd_selectBeginLine";
75 const char * const sSelectEndLineString = "cmd_selectEndLine";
77 const char * const sSelectLinePreviousString = "cmd_selectLinePrevious";
78 const char * const sSelectLineNextString = "cmd_selectLineNext";
80 const char * const sSelectPageUpString = "cmd_selectPageUp";
81 const char * const sSelectPageDownString = "cmd_selectPageDown";
83 const char * const sSelectTopString = "cmd_selectTop";
84 const char * const sSelectBottomString = "cmd_selectBottom";
87 #if 0
88 #pragma mark -
89 #endif
91 // a base class for selection-related commands, for code sharing
92 class nsSelectionCommandsBase : public nsIControllerCommand
94 public:
95 NS_DECL_ISUPPORTS
96 NS_IMETHOD IsCommandEnabled(const char * aCommandName, nsISupports *aCommandContext, bool *_retval);
97 NS_IMETHOD GetCommandStateParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext);
98 NS_IMETHOD DoCommandParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext);
100 protected:
101 virtual ~nsSelectionCommandsBase() {}
103 static nsresult GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell);
104 static nsresult GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon);
106 // no member variables, please, we're stateless!
109 // this class implements commands whose behavior depends on the 'browse with caret' setting
110 class nsSelectMoveScrollCommand : public nsSelectionCommandsBase
112 public:
114 NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext);
116 // no member variables, please, we're stateless!
119 // this class implements other selection commands
120 class nsSelectCommand : public nsSelectionCommandsBase
122 public:
124 NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext);
126 // no member variables, please, we're stateless!
129 #if 0
130 #pragma mark -
131 #endif
134 NS_IMPL_ISUPPORTS(nsSelectionCommandsBase, nsIControllerCommand)
136 /* boolean isCommandEnabled (in string aCommandName, in nsISupports aCommandContext); */
137 NS_IMETHODIMP
138 nsSelectionCommandsBase::IsCommandEnabled(const char * aCommandName,
139 nsISupports *aCommandContext,
140 bool *outCmdEnabled)
142 // XXX this needs fixing. e.g. you can't scroll up if you're already at the top of
143 // the document.
144 *outCmdEnabled = true;
145 return NS_OK;
148 /* void getCommandStateParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
149 NS_IMETHODIMP
150 nsSelectionCommandsBase::GetCommandStateParams(const char *aCommandName,
151 nsICommandParams *aParams, nsISupports *aCommandContext)
153 // XXX we should probably return the enabled state
154 return NS_ERROR_NOT_IMPLEMENTED;
157 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
158 NS_IMETHODIMP
159 nsSelectionCommandsBase::DoCommandParams(const char *aCommandName,
160 nsICommandParams *aParams, nsISupports *aCommandContext)
162 return DoCommand(aCommandName, aCommandContext);
165 // protected methods
167 nsresult
168 nsSelectionCommandsBase::GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell)
170 *aPresShell = nullptr;
171 NS_ENSURE_TRUE(aWindow, NS_ERROR_FAILURE);
173 nsIDocShell *docShell = aWindow->GetDocShell();
174 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
176 NS_IF_ADDREF(*aPresShell = docShell->GetPresShell());
177 return NS_OK;
180 nsresult
181 nsSelectionCommandsBase::GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon)
183 *aSelCon = nullptr;
185 nsCOMPtr<nsIPresShell> presShell;
186 GetPresShellFromWindow(aWindow, getter_AddRefs(presShell));
187 if (presShell)
188 return CallQueryInterface(presShell, aSelCon);
190 return NS_ERROR_FAILURE;
193 #if 0
194 #pragma mark -
195 #endif
197 static const struct BrowseCommand {
198 const char *reverse, *forward;
199 nsresult (NS_STDCALL nsISelectionController::*scroll)(bool);
200 nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool);
201 } browseCommands[] = {
202 { sScrollTopString, sScrollBottomString,
203 &nsISelectionController::CompleteScroll },
204 { sScrollPageUpString, sScrollPageDownString,
205 &nsISelectionController::ScrollPage },
206 { sScrollLineUpString, sScrollLineDownString,
207 &nsISelectionController::ScrollLine },
208 { sScrollLeftString, sScrollRightString,
209 &nsISelectionController::ScrollCharacter },
210 { sMoveTopString, sMoveBottomString,
211 &nsISelectionController::CompleteScroll,
212 &nsISelectionController::CompleteMove },
213 { sMovePageUpString, sMovePageDownString,
214 &nsISelectionController::ScrollPage,
215 &nsISelectionController::PageMove },
216 { sLinePreviousString, sLineNextString,
217 &nsISelectionController::ScrollLine,
218 &nsISelectionController::LineMove },
219 { sWordPreviousString, sWordNextString,
220 &nsISelectionController::ScrollCharacter,
221 &nsISelectionController::WordMove },
222 { sCharPreviousString, sCharNextString,
223 &nsISelectionController::ScrollCharacter,
224 &nsISelectionController::CharacterMove },
225 { sBeginLineString, sEndLineString,
226 &nsISelectionController::CompleteScroll,
227 &nsISelectionController::IntraLineMove }
230 nsresult
231 nsSelectMoveScrollCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
233 nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(aCommandContext));
234 nsCOMPtr<nsISelectionController> selCont;
235 GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont));
236 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
238 // We allow the caret to be moved with arrow keys on any window for which
239 // the caret is enabled. In particular, this includes caret-browsing mode
240 // in non-chrome documents.
241 bool caretOn = false;
242 selCont->GetCaretEnabled(&caretOn);
243 if (!caretOn) {
244 caretOn = Preferences::GetBool("accessibility.browsewithcaret");
245 if (caretOn) {
246 nsCOMPtr<nsIDocShell> docShell = piWindow->GetDocShell();
247 if (docShell && docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
248 caretOn = false;
253 for (size_t i = 0; i < ArrayLength(browseCommands); i++) {
254 bool forward = !strcmp(aCommandName, browseCommands[i].forward);
255 if (forward || !strcmp(aCommandName, browseCommands[i].reverse)) {
256 if (caretOn && browseCommands[i].move &&
257 NS_SUCCEEDED((selCont->*(browseCommands[i].move))(forward, false))) {
258 // adjust the focus to the new caret position
259 nsIFocusManager* fm = nsFocusManager::GetFocusManager();
260 if (fm) {
261 nsCOMPtr<nsIDOMElement> result;
262 fm->MoveFocus(piWindow, nullptr, nsIFocusManager::MOVEFOCUS_CARET,
263 nsIFocusManager::FLAG_NOSCROLL,
264 getter_AddRefs(result));
266 return NS_OK;
268 return (selCont->*(browseCommands[i].scroll))(forward);
271 return NS_ERROR_NOT_IMPLEMENTED;
275 #if 0
276 #pragma mark -
277 #endif
279 nsresult
280 nsSelectCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
282 nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(aCommandContext));
283 nsCOMPtr<nsISelectionController> selCont;
284 GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont));
285 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
287 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
289 // These commands are so the browser can use caret navigation key bindings -
290 // Helps with accessibility - aaronl@netscape.com
291 if (!nsCRT::strcmp(aCommandName, sSelectCharPreviousString))
292 rv = selCont->CharacterMove(false, true);
293 else if (!nsCRT::strcmp(aCommandName, sSelectCharNextString))
294 rv = selCont->CharacterMove(true, true);
295 else if (!nsCRT::strcmp(aCommandName, sSelectWordPreviousString))
296 rv = selCont->WordMove(false, true);
297 else if (!nsCRT::strcmp(aCommandName, sSelectWordNextString))
298 rv = selCont->WordMove(true, true);
299 else if (!nsCRT::strcmp(aCommandName, sSelectBeginLineString))
300 rv = selCont->IntraLineMove(false, true);
301 else if (!nsCRT::strcmp(aCommandName, sSelectEndLineString))
302 rv = selCont->IntraLineMove(true, true);
303 else if (!nsCRT::strcmp(aCommandName, sSelectLinePreviousString))
304 rv = selCont->LineMove(false, true);
305 else if (!nsCRT::strcmp(aCommandName, sSelectLineNextString))
306 rv = selCont->LineMove(true, true);
307 else if (!nsCRT::strcmp(aCommandName, sSelectPageUpString))
308 rv = selCont->PageMove(false, true);
309 else if (!nsCRT::strcmp(aCommandName, sSelectPageDownString))
310 rv = selCont->PageMove(true, true);
311 else if (!nsCRT::strcmp(aCommandName, sSelectTopString))
312 rv = selCont->CompleteMove(false, true);
313 else if (!nsCRT::strcmp(aCommandName, sSelectBottomString))
314 rv = selCont->CompleteMove(true, true);
316 return rv;
319 #if 0
320 #pragma mark -
321 #endif
323 class nsClipboardCommand MOZ_FINAL : public nsIControllerCommand
325 ~nsClipboardCommand() {}
327 public:
329 NS_DECL_ISUPPORTS
330 NS_DECL_NSICONTROLLERCOMMAND
333 NS_IMPL_ISUPPORTS(nsClipboardCommand, nsIControllerCommand)
335 nsresult
336 nsClipboardCommand::IsCommandEnabled(const char* aCommandName, nsISupports *aContext, bool *outCmdEnabled)
338 NS_ENSURE_ARG_POINTER(outCmdEnabled);
339 *outCmdEnabled = false;
341 if (strcmp(aCommandName, "cmd_copy"))
342 return NS_OK;
344 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
345 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
347 nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
348 *outCmdEnabled = nsCopySupport::CanCopy(doc);
349 return NS_OK;
352 nsresult
353 nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext)
355 if (strcmp(aCommandName, "cmd_copy"))
356 return NS_OK;
358 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
359 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
361 nsIDocShell *docShell = window->GetDocShell();
362 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
364 nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
365 NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
367 nsCopySupport::FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard, presShell, nullptr);
368 return NS_OK;
371 NS_IMETHODIMP
372 nsClipboardCommand::GetCommandStateParams(const char *aCommandName,
373 nsICommandParams *aParams, nsISupports *aCommandContext)
375 return NS_ERROR_NOT_IMPLEMENTED;
378 nsresult
379 nsClipboardCommand::DoCommandParams(const char *aCommandName, nsICommandParams* aParams, nsISupports *aContext)
381 return DoCommand(aCommandName, aContext);
384 #if 0
385 #pragma mark -
386 #endif
388 class nsSelectionCommand : public nsIControllerCommand
390 public:
391 NS_DECL_ISUPPORTS
392 NS_DECL_NSICONTROLLERCOMMAND
394 protected:
395 virtual ~nsSelectionCommand() {}
397 virtual nsresult IsClipboardCommandEnabled(const char * aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) = 0;
398 virtual nsresult DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) = 0;
400 static nsresult GetContentViewerEditFromContext(nsISupports *aContext, nsIContentViewerEdit **aEditInterface);
402 // no member variables, please, we're stateless!
406 NS_IMPL_ISUPPORTS(nsSelectionCommand, nsIControllerCommand)
409 /*---------------------------------------------------------------------------
411 nsSelectionCommand
413 ----------------------------------------------------------------------------*/
415 NS_IMETHODIMP
416 nsSelectionCommand::IsCommandEnabled(const char * aCommandName,
417 nsISupports *aCommandContext,
418 bool *outCmdEnabled)
420 NS_ENSURE_ARG_POINTER(outCmdEnabled);
421 *outCmdEnabled = false;
423 nsCOMPtr<nsIContentViewerEdit> contentEdit;
424 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
425 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
427 return IsClipboardCommandEnabled(aCommandName, contentEdit, outCmdEnabled);
430 NS_IMETHODIMP
431 nsSelectionCommand::DoCommand(const char *aCommandName,
432 nsISupports *aCommandContext)
434 nsCOMPtr<nsIContentViewerEdit> contentEdit;
435 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
436 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
438 return DoClipboardCommand(aCommandName, contentEdit, nullptr);
441 NS_IMETHODIMP
442 nsSelectionCommand::GetCommandStateParams(const char *aCommandName,
443 nsICommandParams *aParams,
444 nsISupports *aCommandContext)
446 return NS_ERROR_NOT_IMPLEMENTED;
449 NS_IMETHODIMP
450 nsSelectionCommand::DoCommandParams(const char *aCommandName,
451 nsICommandParams *aParams,
452 nsISupports *aCommandContext)
454 nsCOMPtr<nsIContentViewerEdit> contentEdit;
455 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
456 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
458 return DoClipboardCommand(aCommandName, contentEdit, aParams);
461 nsresult
462 nsSelectionCommand::GetContentViewerEditFromContext(nsISupports *aContext,
463 nsIContentViewerEdit **aEditInterface)
465 NS_ENSURE_ARG(aEditInterface);
466 *aEditInterface = nullptr;
468 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
469 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
471 nsIDocShell *docShell = window->GetDocShell();
472 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
474 nsCOMPtr<nsIContentViewer> viewer;
475 docShell->GetContentViewer(getter_AddRefs(viewer));
476 nsCOMPtr<nsIContentViewerEdit> edit(do_QueryInterface(viewer));
477 NS_ENSURE_TRUE(edit, NS_ERROR_FAILURE);
479 *aEditInterface = edit;
480 NS_ADDREF(*aEditInterface);
481 return NS_OK;
484 #if 0
485 #pragma mark -
486 #endif
488 #define NS_DECL_CLIPBOARD_COMMAND(_cmd) \
489 class _cmd : public nsSelectionCommand \
491 protected: \
493 virtual nsresult IsClipboardCommandEnabled(const char* aCommandName, \
494 nsIContentViewerEdit* aEdit, bool *outCmdEnabled); \
495 virtual nsresult DoClipboardCommand(const char* aCommandName, \
496 nsIContentViewerEdit* aEdit, nsICommandParams* aParams); \
497 /* no member variables, please, we're stateless! */ \
500 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyLinkCommand)
501 NS_DECL_CLIPBOARD_COMMAND(nsClipboardImageCommands)
502 NS_DECL_CLIPBOARD_COMMAND(nsClipboardSelectAllNoneCommands)
503 NS_DECL_CLIPBOARD_COMMAND(nsClipboardGetContentsCommand)
505 nsresult
506 nsClipboardCopyLinkCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
508 return aEdit->GetInLink(outCmdEnabled);
511 nsresult
512 nsClipboardCopyLinkCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
514 return aEdit->CopyLinkLocation();
517 #if 0
518 #pragma mark -
519 #endif
521 nsresult
522 nsClipboardImageCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
524 return aEdit->GetInImage(outCmdEnabled);
527 nsresult
528 nsClipboardImageCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
530 if (!nsCRT::strcmp(sCopyImageLocationString, aCommandName))
531 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_TEXT);
532 if (!nsCRT::strcmp(sCopyImageContentsString, aCommandName))
533 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_DATA);
534 int32_t copyFlags = nsIContentViewerEdit::COPY_IMAGE_DATA |
535 nsIContentViewerEdit::COPY_IMAGE_HTML;
536 if (aParams)
537 aParams->GetLongValue("imageCopy", &copyFlags);
538 return aEdit->CopyImage(copyFlags);
541 #if 0
542 #pragma mark -
543 #endif
545 nsresult
546 nsClipboardSelectAllNoneCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
548 *outCmdEnabled = true;
549 return NS_OK;
552 nsresult
553 nsClipboardSelectAllNoneCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
555 if (!nsCRT::strcmp(sSelectAllString, aCommandName))
556 return aEdit->SelectAll();
558 return aEdit->ClearSelection();
562 #if 0
563 #pragma mark -
564 #endif
566 nsresult
567 nsClipboardGetContentsCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
569 return aEdit->GetCanGetContents(outCmdEnabled);
572 nsresult
573 nsClipboardGetContentsCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
575 NS_ENSURE_ARG(aParams);
577 nsAutoCString mimeType("text/plain");
579 nsXPIDLCString format; // nsICommandParams needs to use nsACString
580 if (NS_SUCCEEDED(aParams->GetCStringValue("format", getter_Copies(format))))
581 mimeType.Assign(format);
583 bool selectionOnly = false;
584 aParams->GetBooleanValue("selection_only", &selectionOnly);
586 nsAutoString contents;
587 nsresult rv = aEdit->GetContents(mimeType.get(), selectionOnly, contents);
588 if (NS_FAILED(rv))
589 return rv;
591 return aParams->SetStringValue("result", contents);
594 #if 0 // Remove unless needed again, bug 204777
595 class nsWebNavigationBaseCommand : public nsIControllerCommand
597 public:
598 virtual ~nsWebNavigationBaseCommand() {}
600 NS_DECL_ISUPPORTS
601 NS_DECL_NSICONTROLLERCOMMAND
603 protected:
605 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled) = 0;
606 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) = 0;
608 static nsresult GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation);
610 // no member variables, please, we're stateless!
613 class nsGoForwardCommand : public nsWebNavigationBaseCommand
615 protected:
617 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled);
618 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
619 // no member variables, please, we're stateless!
622 class nsGoBackCommand : public nsWebNavigationBaseCommand
624 protected:
626 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled);
627 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
628 // no member variables, please, we're stateless!
631 /*---------------------------------------------------------------------------
633 nsWebNavigationCommands
634 no params
635 ----------------------------------------------------------------------------*/
637 NS_IMPL_ISUPPORTS(nsWebNavigationBaseCommand, nsIControllerCommand)
639 NS_IMETHODIMP
640 nsWebNavigationBaseCommand::IsCommandEnabled(const char * aCommandName,
641 nsISupports *aCommandContext,
642 bool *outCmdEnabled)
644 NS_ENSURE_ARG_POINTER(outCmdEnabled);
645 *outCmdEnabled = false;
647 nsCOMPtr<nsIWebNavigation> webNav;
648 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
649 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
651 return IsCommandEnabled(aCommandName, webNav, outCmdEnabled);
654 NS_IMETHODIMP
655 nsWebNavigationBaseCommand::GetCommandStateParams(const char *aCommandName,
656 nsICommandParams *aParams, nsISupports *aCommandContext)
658 // XXX we should probably return the enabled state
659 return NS_ERROR_NOT_IMPLEMENTED;
662 NS_IMETHODIMP
663 nsWebNavigationBaseCommand::DoCommand(const char *aCommandName,
664 nsISupports *aCommandContext)
666 nsCOMPtr<nsIWebNavigation> webNav;
667 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
668 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
670 return DoWebNavCommand(aCommandName, webNav);
673 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
674 NS_IMETHODIMP
675 nsWebNavigationBaseCommand::DoCommandParams(const char *aCommandName,
676 nsICommandParams *aParams, nsISupports *aCommandContext)
678 return DoCommand(aCommandName, aCommandContext);
681 nsresult
682 nsWebNavigationBaseCommand::GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation)
684 nsCOMPtr<nsIInterfaceRequestor> windowReq = do_QueryInterface(aContext);
685 CallGetInterface(windowReq.get(), aWebNavigation);
686 return (*aWebNavigation) ? NS_OK : NS_ERROR_FAILURE;
689 nsresult
690 nsGoForwardCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled)
692 return aWebNavigation->GetCanGoForward(outCmdEnabled);
695 nsresult
696 nsGoForwardCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
698 return aWebNavigation->GoForward();
701 nsresult
702 nsGoBackCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled)
704 return aWebNavigation->GetCanGoBack(outCmdEnabled);
707 nsresult
708 nsGoBackCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
710 return aWebNavigation->GoBack();
712 #endif
714 /*---------------------------------------------------------------------------
716 nsClipboardDragDropHookCommand
717 params value type possible values
718 "addhook" isupports nsIClipboardDragDropHooks as nsISupports
719 "removehook" isupports nsIClipboardDragDropHooks as nsISupports
721 ----------------------------------------------------------------------------*/
723 class nsClipboardDragDropHookCommand MOZ_FINAL : public nsIControllerCommand
725 ~nsClipboardDragDropHookCommand() {}
727 public:
729 NS_DECL_ISUPPORTS
730 NS_DECL_NSICONTROLLERCOMMAND
732 protected:
733 // no member variables, please, we're stateless!
737 NS_IMPL_ISUPPORTS(nsClipboardDragDropHookCommand, nsIControllerCommand)
739 NS_IMETHODIMP
740 nsClipboardDragDropHookCommand::IsCommandEnabled(const char * aCommandName,
741 nsISupports *aCommandContext,
742 bool *outCmdEnabled)
744 *outCmdEnabled = true;
745 return NS_OK;
748 NS_IMETHODIMP
749 nsClipboardDragDropHookCommand::DoCommand(const char *aCommandName,
750 nsISupports *aCommandContext)
752 return NS_ERROR_FAILURE;
755 NS_IMETHODIMP
756 nsClipboardDragDropHookCommand::DoCommandParams(const char *aCommandName,
757 nsICommandParams *aParams,
758 nsISupports *aCommandContext)
760 NS_ENSURE_ARG(aParams);
762 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aCommandContext);
763 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
765 nsIDocShell *docShell = window->GetDocShell();
767 nsCOMPtr<nsIClipboardDragDropHookList> obj = do_GetInterface(docShell);
768 if (!obj) return NS_ERROR_INVALID_ARG;
770 nsCOMPtr<nsISupports> isuppHook;
772 nsresult returnValue = NS_OK;
773 nsresult rv = aParams->GetISupportsValue("addhook", getter_AddRefs(isuppHook));
774 if (NS_SUCCEEDED(rv))
776 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
777 if (hook)
778 returnValue = obj->AddClipboardDragDropHooks(hook);
779 else
780 returnValue = NS_ERROR_INVALID_ARG;
783 rv = aParams->GetISupportsValue("removehook", getter_AddRefs(isuppHook));
784 if (NS_SUCCEEDED(rv))
786 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
787 if (hook)
789 rv = obj->RemoveClipboardDragDropHooks(hook);
790 if (NS_FAILED(rv) && NS_SUCCEEDED(returnValue))
791 returnValue = rv;
793 else
794 returnValue = NS_ERROR_INVALID_ARG;
797 return returnValue;
800 NS_IMETHODIMP
801 nsClipboardDragDropHookCommand::GetCommandStateParams(const char *aCommandName,
802 nsICommandParams *aParams,
803 nsISupports *aCommandContext)
805 NS_ENSURE_ARG_POINTER(aParams);
806 return aParams->SetBooleanValue("state_enabled", true);
809 /*---------------------------------------------------------------------------
811 RegisterWindowCommands
813 ----------------------------------------------------------------------------*/
815 #define NS_REGISTER_ONE_COMMAND(_cmdClass, _cmdName) \
817 _cmdClass* theCmd = new _cmdClass(); \
818 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
819 rv = inCommandTable->RegisterCommand(_cmdName, \
820 static_cast<nsIControllerCommand *>(theCmd)); \
823 #define NS_REGISTER_FIRST_COMMAND(_cmdClass, _cmdName) \
825 _cmdClass* theCmd = new _cmdClass(); \
826 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
827 rv = inCommandTable->RegisterCommand(_cmdName, \
828 static_cast<nsIControllerCommand *>(theCmd));
830 #define NS_REGISTER_NEXT_COMMAND(_cmdClass, _cmdName) \
831 rv = inCommandTable->RegisterCommand(_cmdName, \
832 static_cast<nsIControllerCommand *>(theCmd));
834 #define NS_REGISTER_LAST_COMMAND(_cmdClass, _cmdName) \
835 rv = inCommandTable->RegisterCommand(_cmdName, \
836 static_cast<nsIControllerCommand *>(theCmd)); \
840 // static
841 nsresult
842 nsWindowCommandRegistration::RegisterWindowCommands(
843 nsIControllerCommandTable *inCommandTable)
845 nsresult rv;
847 // XXX rework the macros to use a loop is possible, reducing code size
849 // this set of commands is affected by the 'browse with caret' setting
850 NS_REGISTER_FIRST_COMMAND(nsSelectMoveScrollCommand, sScrollTopString);
851 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollBottomString);
852 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageUpString);
853 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageDownString);
854 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineUpString);
855 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineDownString);
856 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLeftString);
857 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollRightString);
858 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveTopString);
859 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveBottomString);
860 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordPreviousString);
861 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordNextString);
862 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sBeginLineString);
863 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sEndLineString);
864 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageUpString);
865 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageDownString);
866 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLinePreviousString);
867 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLineNextString);
868 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sCharPreviousString);
869 NS_REGISTER_LAST_COMMAND(nsSelectMoveScrollCommand, sCharNextString);
871 NS_REGISTER_FIRST_COMMAND(nsSelectCommand, sSelectCharPreviousString);
872 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectCharNextString);
873 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordPreviousString);
874 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordNextString);
875 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectBeginLineString);
876 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectEndLineString);
877 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLinePreviousString);
878 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLineNextString);
879 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageUpString);
880 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageDownString);
881 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectTopString);
882 NS_REGISTER_LAST_COMMAND(nsSelectCommand, sSelectBottomString);
884 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_cut");
885 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_copy");
886 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_paste");
887 NS_REGISTER_ONE_COMMAND(nsClipboardCopyLinkCommand, "cmd_copyLink");
888 NS_REGISTER_FIRST_COMMAND(nsClipboardImageCommands, sCopyImageLocationString);
889 NS_REGISTER_NEXT_COMMAND(nsClipboardImageCommands, sCopyImageContentsString);
890 NS_REGISTER_LAST_COMMAND(nsClipboardImageCommands, sCopyImageString);
891 NS_REGISTER_FIRST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectAllString);
892 NS_REGISTER_LAST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectNoneString);
894 NS_REGISTER_ONE_COMMAND(nsClipboardGetContentsCommand, "cmd_getContents");
896 #if 0 // Remove unless needed again, bug 204777
897 NS_REGISTER_ONE_COMMAND(nsGoBackCommand, "cmd_browserBack");
898 NS_REGISTER_ONE_COMMAND(nsGoForwardCommand, "cmd_browserForward");
899 #endif
901 NS_REGISTER_ONE_COMMAND(nsClipboardDragDropHookCommand, "cmd_clipboardDragDropHook");
903 return rv;