Fix register allocation bug in return values (bug 604996, r=dmandelin).
[mozilla-central.git] / dom / base / nsGlobalWindowCommands.cpp
blobf6f6c1b07389f085c39af5eeabd45bafef05021a
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications, Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Kathleen Brade <brade@netscape.com>
23 * Simon Fraser <sfraser@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsGlobalWindowCommands.h"
42 #include "nsIComponentManager.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "nsCRT.h"
46 #include "nsString.h"
47 #include "nsContentUtils.h"
49 #include "nsIControllerCommandTable.h"
50 #include "nsICommandParams.h"
52 #include "nsPIDOMWindow.h"
53 #include "nsIPresShell.h"
54 #include "nsIDocShell.h"
55 #include "nsIDocShellTreeItem.h"
56 #include "nsISelectionController.h"
57 #include "nsIWebNavigation.h"
58 #include "nsIContentViewerEdit.h"
59 #include "nsIContentViewer.h"
60 #include "nsFocusManager.h"
61 #include "nsCopySupport.h"
62 #include "nsGUIEvent.h"
64 #include "nsIClipboardDragDropHooks.h"
65 #include "nsIClipboardDragDropHookList.h"
67 const char * const sSelectAllString = "cmd_selectAll";
68 const char * const sSelectNoneString = "cmd_selectNone";
69 const char * const sCopyImageLocationString = "cmd_copyImageLocation";
70 const char * const sCopyImageContentsString = "cmd_copyImageContents";
71 const char * const sCopyImageString = "cmd_copyImage";
73 const char * const sScrollTopString = "cmd_scrollTop";
74 const char * const sScrollBottomString = "cmd_scrollBottom";
75 const char * const sScrollPageUpString = "cmd_scrollPageUp";
76 const char * const sScrollPageDownString = "cmd_scrollPageDown";
77 const char * const sMovePageUpString = "cmd_movePageUp";
78 const char * const sMovePageDownString = "cmd_movePageDown";
79 const char * const sScrollLineUpString = "cmd_scrollLineUp";
80 const char * const sScrollLineDownString = "cmd_scrollLineDown";
81 const char * const sScrollLeftString = "cmd_scrollLeft";
82 const char * const sScrollRightString = "cmd_scrollRight";
84 // These are so the browser can use editor navigation key bindings
85 // helps with accessibility (boolean pref accessibility.browsewithcaret)
87 const char * const sSelectCharPreviousString = "cmd_selectCharPrevious";
88 const char * const sSelectCharNextString = "cmd_selectCharNext";
90 const char * const sWordPreviousString = "cmd_wordPrevious";
91 const char * const sWordNextString = "cmd_wordNext";
92 const char * const sSelectWordPreviousString = "cmd_selectWordPrevious";
93 const char * const sSelectWordNextString = "cmd_selectWordNext";
95 const char * const sBeginLineString = "cmd_beginLine";
96 const char * const sEndLineString = "cmd_endLine";
97 const char * const sSelectBeginLineString = "cmd_selectBeginLine";
98 const char * const sSelectEndLineString = "cmd_selectEndLine";
100 const char * const sSelectLinePreviousString = "cmd_selectLinePrevious";
101 const char * const sSelectLineNextString = "cmd_selectLineNext";
103 const char * const sSelectPagePreviousString = "cmd_selectPagePrevious";
104 const char * const sSelectPageNextString = "cmd_selectPageNext";
106 const char * const sSelectTopString = "cmd_selectTop";
107 const char * const sSelectBottomString = "cmd_selectBottom";
110 #if 0
111 #pragma mark -
112 #endif
114 // a base class for selection-related commands, for code sharing
115 class nsSelectionCommandsBase : public nsIControllerCommand
117 public:
119 NS_DECL_ISUPPORTS
120 NS_DECL_NSICONTROLLERCOMMAND
122 protected:
124 // subclasses override DoSelectCommand
125 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow) = 0;
127 static nsresult GetPresShellFromWindow(nsIDOMWindow *aWindow, nsIPresShell **aPresShell);
128 static nsresult GetSelectionControllerFromWindow(nsIDOMWindow *aWindow, nsISelectionController **aSelCon);
130 // no member variables, please, we're stateless!
133 // this class implements commands whose behavior depends on the 'browse with caret' setting
134 class nsSelectMoveScrollCommand : public nsSelectionCommandsBase
136 protected:
138 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow);
140 nsresult DoCommandBrowseWithCaretOn(const char *aCommandName,
141 nsIDOMWindow *aWindow,
142 nsISelectionController *aSelectionController);
143 nsresult DoCommandBrowseWithCaretOff(const char *aCommandName, nsISelectionController *aSelectionController);
145 // no member variables, please, we're stateless!
148 // this class implements other selection commands
149 class nsSelectCommand : public nsSelectionCommandsBase
151 protected:
153 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow);
155 // no member variables, please, we're stateless!
158 #if 0
159 #pragma mark -
160 #endif
163 NS_IMPL_ISUPPORTS1(nsSelectionCommandsBase, nsIControllerCommand)
165 /* boolean isCommandEnabled (in string aCommandName, in nsISupports aCommandContext); */
166 NS_IMETHODIMP
167 nsSelectionCommandsBase::IsCommandEnabled(const char * aCommandName,
168 nsISupports *aCommandContext,
169 PRBool *outCmdEnabled)
171 // XXX this needs fixing. e.g. you can't scroll up if you're already at the top of
172 // the document.
173 *outCmdEnabled = PR_TRUE;
174 return NS_OK;
177 /* void getCommandStateParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
178 NS_IMETHODIMP
179 nsSelectionCommandsBase::GetCommandStateParams(const char *aCommandName,
180 nsICommandParams *aParams, nsISupports *aCommandContext)
182 // XXX we should probably return the enabled state
183 return NS_ERROR_NOT_IMPLEMENTED;
186 NS_IMETHODIMP
187 nsSelectionCommandsBase::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
189 nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(aCommandContext);
190 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
192 return DoSelectCommand(aCommandName, window);
195 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
196 NS_IMETHODIMP
197 nsSelectionCommandsBase::DoCommandParams(const char *aCommandName,
198 nsICommandParams *aParams, nsISupports *aCommandContext)
200 return DoCommand(aCommandName, aCommandContext);
203 // protected methods
205 nsresult
206 nsSelectionCommandsBase::GetPresShellFromWindow(nsIDOMWindow *aWindow, nsIPresShell **aPresShell)
208 *aPresShell = nsnull;
210 nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(aWindow));
211 NS_ENSURE_TRUE(win, NS_ERROR_FAILURE);
213 nsIDocShell *docShell = win->GetDocShell();
214 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
216 return docShell->GetPresShell(aPresShell);
219 nsresult
220 nsSelectionCommandsBase::GetSelectionControllerFromWindow(nsIDOMWindow *aWindow, nsISelectionController **aSelCon)
222 *aSelCon = nsnull;
224 nsCOMPtr<nsIPresShell> presShell;
225 GetPresShellFromWindow(aWindow, getter_AddRefs(presShell));
226 if (presShell)
227 return CallQueryInterface(presShell, aSelCon);
229 return NS_ERROR_FAILURE;
232 #if 0
233 #pragma mark -
234 #endif
236 nsresult
237 nsSelectMoveScrollCommand::DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow)
239 nsCOMPtr<nsISelectionController> selCont;
240 GetSelectionControllerFromWindow(aWindow, getter_AddRefs(selCont));
241 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
243 // We allow the caret to be moved with arrow keys on any window for which
244 // the caret is enabled. In particular, this includes caret-browsing mode
245 // in non-chrome documents.
246 PRBool caretOn = PR_FALSE;
247 selCont->GetCaretEnabled(&caretOn);
248 if (!caretOn) {
249 caretOn = nsContentUtils::GetBoolPref("accessibility.browsewithcaret");
250 if (caretOn) {
251 nsCOMPtr<nsPIDOMWindow> piWindow = do_QueryInterface(aWindow);
252 if (piWindow) {
253 nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface(piWindow->GetDocShell());
254 if (dsti) {
255 PRInt32 itemType;
256 dsti->GetItemType(&itemType);
257 if (itemType == nsIDocShellTreeItem::typeChrome) {
258 caretOn = PR_FALSE;
265 if (caretOn) {
266 return DoCommandBrowseWithCaretOn(aCommandName, aWindow, selCont);
269 return DoCommandBrowseWithCaretOff(aCommandName, selCont);
272 nsresult
273 nsSelectMoveScrollCommand::DoCommandBrowseWithCaretOn(const char *aCommandName,
274 nsIDOMWindow *aWindow, nsISelectionController *aSelectionController)
276 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
278 if (!nsCRT::strcmp(aCommandName, sScrollTopString))
279 rv = aSelectionController->CompleteMove(PR_FALSE, PR_FALSE);
280 else if (!nsCRT::strcmp(aCommandName,sScrollBottomString))
281 rv = aSelectionController->CompleteMove(PR_TRUE, PR_FALSE);
282 // cmd_MovePageUp/Down are used on Window/Unix. They move the caret
283 // in caret browsing mode.
284 else if (!nsCRT::strcmp(aCommandName, sMovePageUpString))
285 rv = aSelectionController->PageMove(PR_FALSE, PR_FALSE);
286 else if (!nsCRT::strcmp(aCommandName, sMovePageDownString))
287 rv = aSelectionController->PageMove(PR_TRUE, PR_FALSE);
288 // cmd_ScrollPageUp/Down are used on Mac, and for the spacebar on all platforms.
289 // They do not move the caret in caret browsing mode.
290 else if (!nsCRT::strcmp(aCommandName, sScrollPageUpString))
291 rv = aSelectionController->ScrollPage(PR_FALSE);
292 else if (!nsCRT::strcmp(aCommandName, sScrollPageDownString))
293 rv = aSelectionController->ScrollPage(PR_TRUE);
294 else if (!nsCRT::strcmp(aCommandName, sScrollLineUpString))
295 rv = aSelectionController->LineMove(PR_FALSE, PR_FALSE);
296 else if (!nsCRT::strcmp(aCommandName, sScrollLineDownString))
297 rv = aSelectionController->LineMove(PR_TRUE, PR_FALSE);
298 else if (!nsCRT::strcmp(aCommandName, sWordPreviousString))
299 rv = aSelectionController->WordMove(PR_FALSE, PR_FALSE);
300 else if (!nsCRT::strcmp(aCommandName, sWordNextString))
301 rv = aSelectionController->WordMove(PR_TRUE, PR_FALSE);
302 else if (!nsCRT::strcmp(aCommandName, sScrollLeftString))
303 rv = aSelectionController->CharacterMove(PR_FALSE, PR_FALSE);
304 else if (!nsCRT::strcmp(aCommandName, sScrollRightString))
305 rv = aSelectionController->CharacterMove(PR_TRUE, PR_FALSE);
306 else if (!nsCRT::strcmp(aCommandName, sBeginLineString))
307 rv = aSelectionController->IntraLineMove(PR_FALSE, PR_FALSE);
308 else if (!nsCRT::strcmp(aCommandName, sEndLineString))
309 rv = aSelectionController->IntraLineMove(PR_TRUE, PR_FALSE);
311 if (NS_SUCCEEDED(rv))
313 // adjust the focus to the new caret position
314 nsIFocusManager* fm = nsFocusManager::GetFocusManager();
315 if (fm) {
316 nsCOMPtr<nsIDOMElement> result;
317 fm->MoveFocus(aWindow, nsnull, nsIFocusManager::MOVEFOCUS_CARET,
318 nsIFocusManager::FLAG_NOSCROLL,
319 getter_AddRefs(result));
323 return rv;
326 nsresult
327 nsSelectMoveScrollCommand::DoCommandBrowseWithCaretOff(const char *aCommandName, nsISelectionController *aSelectionController)
329 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
331 if (!nsCRT::strcmp(aCommandName, sScrollTopString))
332 rv = aSelectionController->CompleteScroll(PR_FALSE);
333 else if (!nsCRT::strcmp(aCommandName,sScrollBottomString))
334 rv = aSelectionController->CompleteScroll(PR_TRUE);
336 // cmd_MovePageUp/Down are used on Window/Unix. They move the caret
337 // in caret browsing mode.
338 else if (!nsCRT::strcmp(aCommandName, sMovePageUpString))
339 rv = aSelectionController->ScrollPage(PR_FALSE);
340 else if (!nsCRT::strcmp(aCommandName, sMovePageDownString))
341 rv = aSelectionController->ScrollPage(PR_TRUE);
342 // cmd_ScrollPageUp/Down are used on Mac. They do not move the
343 // caret in caret browsing mode.
344 else if (!nsCRT::strcmp(aCommandName, sScrollPageUpString))
345 rv = aSelectionController->ScrollPage(PR_FALSE);
346 else if (!nsCRT::strcmp(aCommandName, sScrollPageDownString))
347 rv = aSelectionController->ScrollPage(PR_TRUE);
349 else if (!nsCRT::strcmp(aCommandName, sScrollLineUpString))
350 rv = aSelectionController->ScrollLine(PR_FALSE);
351 else if (!nsCRT::strcmp(aCommandName, sScrollLineDownString))
352 rv = aSelectionController->ScrollLine(PR_TRUE);
353 else if (!nsCRT::strcmp(aCommandName, sScrollLeftString))
354 rv = aSelectionController->ScrollHorizontal(PR_TRUE);
355 else if (!nsCRT::strcmp(aCommandName, sScrollRightString))
356 rv = aSelectionController->ScrollHorizontal(PR_FALSE);
357 // cmd_beginLine/endLine with caret browsing off
358 // will act as cmd_scrollTop/Bottom
359 else if (!nsCRT::strcmp(aCommandName, sBeginLineString))
360 rv = aSelectionController->CompleteScroll(PR_FALSE);
361 else if (!nsCRT::strcmp(aCommandName, sEndLineString))
362 rv = aSelectionController->CompleteScroll(PR_TRUE);
364 return rv;
368 #if 0
369 #pragma mark -
370 #endif
372 nsresult
373 nsSelectCommand::DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow)
375 nsCOMPtr<nsISelectionController> selCont;
376 GetSelectionControllerFromWindow(aWindow, getter_AddRefs(selCont));
377 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
379 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
381 // These commands are so the browser can use caret navigation key bindings -
382 // Helps with accessibility - aaronl@netscape.com
383 if (!nsCRT::strcmp(aCommandName, sSelectCharPreviousString))
384 rv = selCont->CharacterMove(PR_FALSE, PR_TRUE);
385 else if (!nsCRT::strcmp(aCommandName, sSelectCharNextString))
386 rv = selCont->CharacterMove(PR_TRUE, PR_TRUE);
387 else if (!nsCRT::strcmp(aCommandName, sSelectWordPreviousString))
388 rv = selCont->WordMove(PR_FALSE, PR_TRUE);
389 else if (!nsCRT::strcmp(aCommandName, sSelectWordNextString))
390 rv = selCont->WordMove(PR_TRUE, PR_TRUE);
391 else if (!nsCRT::strcmp(aCommandName, sSelectBeginLineString))
392 rv = selCont->IntraLineMove(PR_FALSE, PR_TRUE);
393 else if (!nsCRT::strcmp(aCommandName, sSelectEndLineString))
394 rv = selCont->IntraLineMove(PR_TRUE, PR_TRUE);
395 else if (!nsCRT::strcmp(aCommandName, sSelectLinePreviousString))
396 rv = selCont->LineMove(PR_FALSE, PR_TRUE);
397 else if (!nsCRT::strcmp(aCommandName, sSelectLineNextString))
398 rv = selCont->LineMove(PR_TRUE, PR_TRUE);
399 else if (!nsCRT::strcmp(aCommandName, sSelectTopString))
400 rv = selCont->CompleteMove(PR_FALSE, PR_TRUE);
401 else if (!nsCRT::strcmp(aCommandName, sSelectBottomString))
402 rv = selCont->CompleteMove(PR_TRUE, PR_TRUE);
404 return rv;
407 #if 0
408 #pragma mark -
409 #endif
411 class nsClipboardCommand : public nsIControllerCommand
413 public:
415 NS_DECL_ISUPPORTS
416 NS_DECL_NSICONTROLLERCOMMAND
419 NS_IMPL_ISUPPORTS1(nsClipboardCommand, nsIControllerCommand)
421 nsresult
422 nsClipboardCommand::IsCommandEnabled(const char* aCommandName, nsISupports *aContext, PRBool *outCmdEnabled)
424 NS_ENSURE_ARG_POINTER(outCmdEnabled);
425 *outCmdEnabled = PR_FALSE;
427 if (strcmp(aCommandName, "cmd_copy"))
428 return NS_OK;
430 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
431 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
433 nsCOMPtr<nsIDocument> doc = do_QueryInterface(window->GetExtantDocument());
434 *outCmdEnabled = nsCopySupport::CanCopy(doc);
435 return NS_OK;
438 nsresult
439 nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext)
441 if (strcmp(aCommandName, "cmd_copy"))
442 return NS_OK;
444 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
445 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
447 nsIDocShell *docShell = window->GetDocShell();
448 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
450 nsCOMPtr<nsIPresShell> presShell;
451 docShell->GetPresShell(getter_AddRefs(presShell));
452 NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
454 nsCopySupport::FireClipboardEvent(NS_COPY, presShell, nsnull);
455 return NS_OK;
458 NS_IMETHODIMP
459 nsClipboardCommand::GetCommandStateParams(const char *aCommandName,
460 nsICommandParams *aParams, nsISupports *aCommandContext)
462 return NS_ERROR_NOT_IMPLEMENTED;
465 nsresult
466 nsClipboardCommand::DoCommandParams(const char *aCommandName, nsICommandParams* aParams, nsISupports *aContext)
468 return DoCommand(aCommandName, aContext);
471 #if 0
472 #pragma mark -
473 #endif
475 class nsSelectionCommand : public nsIControllerCommand
477 public:
479 NS_DECL_ISUPPORTS
480 NS_DECL_NSICONTROLLERCOMMAND
482 protected:
484 virtual nsresult IsClipboardCommandEnabled(const char * aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled) = 0;
485 virtual nsresult DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) = 0;
487 static nsresult GetContentViewerEditFromContext(nsISupports *aContext, nsIContentViewerEdit **aEditInterface);
489 // no member variables, please, we're stateless!
493 NS_IMPL_ISUPPORTS1(nsSelectionCommand, nsIControllerCommand)
496 /*---------------------------------------------------------------------------
498 nsSelectionCommand
500 ----------------------------------------------------------------------------*/
502 NS_IMETHODIMP
503 nsSelectionCommand::IsCommandEnabled(const char * aCommandName,
504 nsISupports *aCommandContext,
505 PRBool *outCmdEnabled)
507 NS_ENSURE_ARG_POINTER(outCmdEnabled);
508 *outCmdEnabled = PR_FALSE;
510 nsCOMPtr<nsIContentViewerEdit> contentEdit;
511 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
512 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
514 return IsClipboardCommandEnabled(aCommandName, contentEdit, outCmdEnabled);
517 NS_IMETHODIMP
518 nsSelectionCommand::DoCommand(const char *aCommandName,
519 nsISupports *aCommandContext)
521 nsCOMPtr<nsIContentViewerEdit> contentEdit;
522 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
523 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
525 return DoClipboardCommand(aCommandName, contentEdit, nsnull);
528 NS_IMETHODIMP
529 nsSelectionCommand::GetCommandStateParams(const char *aCommandName,
530 nsICommandParams *aParams,
531 nsISupports *aCommandContext)
533 return NS_ERROR_NOT_IMPLEMENTED;
536 NS_IMETHODIMP
537 nsSelectionCommand::DoCommandParams(const char *aCommandName,
538 nsICommandParams *aParams,
539 nsISupports *aCommandContext)
541 nsCOMPtr<nsIContentViewerEdit> contentEdit;
542 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
543 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
545 return DoClipboardCommand(aCommandName, contentEdit, aParams);
548 nsresult
549 nsSelectionCommand::GetContentViewerEditFromContext(nsISupports *aContext,
550 nsIContentViewerEdit **aEditInterface)
552 NS_ENSURE_ARG(aEditInterface);
553 *aEditInterface = nsnull;
555 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
556 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
558 nsIDocShell *docShell = window->GetDocShell();
559 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
561 nsCOMPtr<nsIContentViewer> viewer;
562 docShell->GetContentViewer(getter_AddRefs(viewer));
563 nsCOMPtr<nsIContentViewerEdit> edit(do_QueryInterface(viewer));
564 NS_ENSURE_TRUE(edit, NS_ERROR_FAILURE);
566 *aEditInterface = edit;
567 NS_ADDREF(*aEditInterface);
568 return NS_OK;
571 #if 0
572 #pragma mark -
573 #endif
575 #define NS_DECL_CLIPBOARD_COMMAND(_cmd) \
576 class _cmd : public nsSelectionCommand \
578 protected: \
580 virtual nsresult IsClipboardCommandEnabled(const char* aCommandName, \
581 nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled); \
582 virtual nsresult DoClipboardCommand(const char* aCommandName, \
583 nsIContentViewerEdit* aEdit, nsICommandParams* aParams); \
584 /* no member variables, please, we're stateless! */ \
587 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyLinkCommand)
588 NS_DECL_CLIPBOARD_COMMAND(nsClipboardImageCommands)
589 NS_DECL_CLIPBOARD_COMMAND(nsClipboardSelectAllNoneCommands)
590 NS_DECL_CLIPBOARD_COMMAND(nsClipboardGetContentsCommand)
592 nsresult
593 nsClipboardCopyLinkCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
595 return aEdit->GetInLink(outCmdEnabled);
598 nsresult
599 nsClipboardCopyLinkCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
601 return aEdit->CopyLinkLocation();
604 #if 0
605 #pragma mark -
606 #endif
608 nsresult
609 nsClipboardImageCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
611 return aEdit->GetInImage(outCmdEnabled);
614 nsresult
615 nsClipboardImageCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
617 if (!nsCRT::strcmp(sCopyImageLocationString, aCommandName))
618 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_TEXT);
619 if (!nsCRT::strcmp(sCopyImageContentsString, aCommandName))
620 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_DATA);
622 PRInt32 copyFlags = nsIContentViewerEdit::COPY_IMAGE_ALL;
623 if (aParams)
624 aParams->GetLongValue("imageCopy", &copyFlags);
625 return aEdit->CopyImage(copyFlags);
628 #if 0
629 #pragma mark -
630 #endif
632 nsresult
633 nsClipboardSelectAllNoneCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
635 *outCmdEnabled = PR_TRUE;
636 return NS_OK;
639 nsresult
640 nsClipboardSelectAllNoneCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
642 if (!nsCRT::strcmp(sSelectAllString, aCommandName))
643 return aEdit->SelectAll();
645 return aEdit->ClearSelection();
649 #if 0
650 #pragma mark -
651 #endif
653 nsresult
654 nsClipboardGetContentsCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
656 return aEdit->GetCanGetContents(outCmdEnabled);
659 nsresult
660 nsClipboardGetContentsCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
662 NS_ENSURE_ARG(aParams);
664 nsCAutoString mimeType("text/plain");
666 nsXPIDLCString format; // nsICommandParams needs to use nsACString
667 if (NS_SUCCEEDED(aParams->GetCStringValue("format", getter_Copies(format))))
668 mimeType.Assign(format);
670 PRBool selectionOnly = PR_FALSE;
671 aParams->GetBooleanValue("selection_only", &selectionOnly);
673 nsAutoString contents;
674 nsresult rv = aEdit->GetContents(mimeType.get(), selectionOnly, contents);
675 if (NS_FAILED(rv))
676 return rv;
678 return aParams->SetStringValue("result", contents);
682 #if 0
683 #pragma mark -
684 #endif
686 class nsWebNavigationBaseCommand : public nsIControllerCommand
688 public:
690 NS_DECL_ISUPPORTS
691 NS_DECL_NSICONTROLLERCOMMAND
693 protected:
695 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled) = 0;
696 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) = 0;
698 static nsresult GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation);
700 // no member variables, please, we're stateless!
703 #if 0 // Remove unless needed again, bug 204777
704 class nsGoForwardCommand : public nsWebNavigationBaseCommand
706 protected:
708 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled);
709 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
710 // no member variables, please, we're stateless!
713 class nsGoBackCommand : public nsWebNavigationBaseCommand
715 protected:
717 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled);
718 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
719 // no member variables, please, we're stateless!
721 #endif
723 /*---------------------------------------------------------------------------
725 nsWebNavigationCommands
726 no params
727 ----------------------------------------------------------------------------*/
729 NS_IMPL_ISUPPORTS1(nsWebNavigationBaseCommand, nsIControllerCommand)
731 NS_IMETHODIMP
732 nsWebNavigationBaseCommand::IsCommandEnabled(const char * aCommandName,
733 nsISupports *aCommandContext,
734 PRBool *outCmdEnabled)
736 NS_ENSURE_ARG_POINTER(outCmdEnabled);
737 *outCmdEnabled = PR_FALSE;
739 nsCOMPtr<nsIWebNavigation> webNav;
740 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
741 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
743 return IsCommandEnabled(aCommandName, webNav, outCmdEnabled);
746 NS_IMETHODIMP
747 nsWebNavigationBaseCommand::GetCommandStateParams(const char *aCommandName,
748 nsICommandParams *aParams, nsISupports *aCommandContext)
750 // XXX we should probably return the enabled state
751 return NS_ERROR_NOT_IMPLEMENTED;
754 NS_IMETHODIMP
755 nsWebNavigationBaseCommand::DoCommand(const char *aCommandName,
756 nsISupports *aCommandContext)
758 nsCOMPtr<nsIWebNavigation> webNav;
759 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
760 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
762 return DoWebNavCommand(aCommandName, webNav);
765 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
766 NS_IMETHODIMP
767 nsWebNavigationBaseCommand::DoCommandParams(const char *aCommandName,
768 nsICommandParams *aParams, nsISupports *aCommandContext)
770 return DoCommand(aCommandName, aCommandContext);
773 nsresult
774 nsWebNavigationBaseCommand::GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation)
776 nsCOMPtr<nsIInterfaceRequestor> windowReq = do_QueryInterface(aContext);
777 CallGetInterface(windowReq.get(), aWebNavigation);
778 return (*aWebNavigation) ? NS_OK : NS_ERROR_FAILURE;
781 #if 0
782 #pragma mark -
783 #endif
785 #if 0 // Remove unless needed again, bug 204777
786 nsresult
787 nsGoForwardCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled)
789 return aWebNavigation->GetCanGoForward(outCmdEnabled);
792 nsresult
793 nsGoForwardCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
795 return aWebNavigation->GoForward();
798 nsresult
799 nsGoBackCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled)
801 return aWebNavigation->GetCanGoBack(outCmdEnabled);
804 nsresult
805 nsGoBackCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
807 return aWebNavigation->GoBack();
809 #endif
811 /*---------------------------------------------------------------------------
813 nsClipboardDragDropHookCommand
814 params value type possible values
815 "addhook" isupports nsIClipboardDragDropHooks as nsISupports
816 "removehook" isupports nsIClipboardDragDropHooks as nsISupports
818 ----------------------------------------------------------------------------*/
820 class nsClipboardDragDropHookCommand : public nsIControllerCommand
822 public:
824 NS_DECL_ISUPPORTS
825 NS_DECL_NSICONTROLLERCOMMAND
827 protected:
828 // no member variables, please, we're stateless!
832 NS_IMPL_ISUPPORTS1(nsClipboardDragDropHookCommand, nsIControllerCommand)
834 NS_IMETHODIMP
835 nsClipboardDragDropHookCommand::IsCommandEnabled(const char * aCommandName,
836 nsISupports *aCommandContext,
837 PRBool *outCmdEnabled)
839 *outCmdEnabled = PR_TRUE;
840 return NS_OK;
843 NS_IMETHODIMP
844 nsClipboardDragDropHookCommand::DoCommand(const char *aCommandName,
845 nsISupports *aCommandContext)
847 return NS_ERROR_FAILURE;
850 NS_IMETHODIMP
851 nsClipboardDragDropHookCommand::DoCommandParams(const char *aCommandName,
852 nsICommandParams *aParams,
853 nsISupports *aCommandContext)
855 NS_ENSURE_ARG(aParams);
857 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aCommandContext);
858 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
860 nsIDocShell *docShell = window->GetDocShell();
862 nsCOMPtr<nsIClipboardDragDropHookList> obj = do_GetInterface(docShell);
863 if (!obj) return NS_ERROR_INVALID_ARG;
865 nsCOMPtr<nsISupports> isuppHook;
867 nsresult returnValue = NS_OK;
868 nsresult rv = aParams->GetISupportsValue("addhook", getter_AddRefs(isuppHook));
869 if (NS_SUCCEEDED(rv))
871 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
872 if (hook)
873 returnValue = obj->AddClipboardDragDropHooks(hook);
874 else
875 returnValue = NS_ERROR_INVALID_ARG;
878 rv = aParams->GetISupportsValue("removehook", getter_AddRefs(isuppHook));
879 if (NS_SUCCEEDED(rv))
881 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
882 if (hook)
884 rv = obj->RemoveClipboardDragDropHooks(hook);
885 if (NS_FAILED(rv) && NS_SUCCEEDED(returnValue))
886 returnValue = rv;
888 else
889 returnValue = NS_ERROR_INVALID_ARG;
892 return returnValue;
895 NS_IMETHODIMP
896 nsClipboardDragDropHookCommand::GetCommandStateParams(const char *aCommandName,
897 nsICommandParams *aParams,
898 nsISupports *aCommandContext)
900 NS_ENSURE_ARG_POINTER(aParams);
901 return aParams->SetBooleanValue("state_enabled", PR_TRUE);
904 /*---------------------------------------------------------------------------
906 RegisterWindowCommands
908 ----------------------------------------------------------------------------*/
910 #define NS_REGISTER_ONE_COMMAND(_cmdClass, _cmdName) \
912 _cmdClass* theCmd = new _cmdClass(); \
913 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
914 rv = inCommandTable->RegisterCommand(_cmdName, \
915 static_cast<nsIControllerCommand *>(theCmd)); \
918 #define NS_REGISTER_FIRST_COMMAND(_cmdClass, _cmdName) \
920 _cmdClass* theCmd = new _cmdClass(); \
921 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
922 rv = inCommandTable->RegisterCommand(_cmdName, \
923 static_cast<nsIControllerCommand *>(theCmd));
925 #define NS_REGISTER_NEXT_COMMAND(_cmdClass, _cmdName) \
926 rv = inCommandTable->RegisterCommand(_cmdName, \
927 static_cast<nsIControllerCommand *>(theCmd));
929 #define NS_REGISTER_LAST_COMMAND(_cmdClass, _cmdName) \
930 rv = inCommandTable->RegisterCommand(_cmdName, \
931 static_cast<nsIControllerCommand *>(theCmd)); \
935 // static
936 nsresult
937 nsWindowCommandRegistration::RegisterWindowCommands(
938 nsIControllerCommandTable *inCommandTable)
940 nsresult rv;
942 // XXX rework the macros to use a loop is possible, reducing code size
944 // this set of commands is affected by the 'browse with caret' setting
945 NS_REGISTER_FIRST_COMMAND(nsSelectMoveScrollCommand, sScrollTopString);
946 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollBottomString);
947 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordPreviousString);
948 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordNextString);
949 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sBeginLineString);
950 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sEndLineString);
951 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageUpString);
952 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageDownString);
953 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageUpString);
954 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageDownString);
955 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineUpString);
956 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineDownString);
957 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLeftString);
958 NS_REGISTER_LAST_COMMAND(nsSelectMoveScrollCommand, sScrollRightString);
960 NS_REGISTER_FIRST_COMMAND(nsSelectCommand, sSelectCharPreviousString);
961 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectCharNextString);
962 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordPreviousString);
963 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordNextString);
964 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectBeginLineString);
965 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectEndLineString);
966 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLinePreviousString);
967 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLineNextString);
968 // XXX these commands were never implemented. fix me.
969 // NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPagePreviousString);
970 // NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageNextString);
971 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectTopString);
972 NS_REGISTER_LAST_COMMAND(nsSelectCommand, sSelectBottomString);
974 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_cut");
975 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_copy");
976 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_paste");
977 NS_REGISTER_ONE_COMMAND(nsClipboardCopyLinkCommand, "cmd_copyLink");
978 NS_REGISTER_FIRST_COMMAND(nsClipboardImageCommands, sCopyImageLocationString);
979 NS_REGISTER_NEXT_COMMAND(nsClipboardImageCommands, sCopyImageContentsString);
980 NS_REGISTER_LAST_COMMAND(nsClipboardImageCommands, sCopyImageString);
981 NS_REGISTER_FIRST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectAllString);
982 NS_REGISTER_LAST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectNoneString);
984 NS_REGISTER_ONE_COMMAND(nsClipboardGetContentsCommand, "cmd_getContents");
986 #if 0 // Remove unless needed again, bug 204777
987 NS_REGISTER_ONE_COMMAND(nsGoBackCommand, "cmd_browserBack");
988 NS_REGISTER_ONE_COMMAND(nsGoForwardCommand, "cmd_browserForward");
989 #endif
991 NS_REGISTER_ONE_COMMAND(nsClipboardDragDropHookCommand, "cmd_clipboardDragDropHook");
993 return rv;