Bug 464858 - intermittent / recurring fail of test_db_update_v1.js. bustagefix.
[wine-gecko.git] / dom / src / base / nsGlobalWindowCommands.cpp
blob60ada03d7ebc5e686d300329b3879f8823011d51
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"
48 #include "nsIControllerCommandTable.h"
49 #include "nsICommandParams.h"
51 #include "nsPIDOMWindow.h"
52 #include "nsIPresShell.h"
53 #include "nsPresContext.h"
54 #include "nsIDocShell.h"
55 #include "nsISelectionController.h"
56 #include "nsIEventStateManager.h"
57 #include "nsIWebNavigation.h"
58 #include "nsIContentViewerEdit.h"
59 #include "nsIContentViewer.h"
61 #include "nsIClipboardDragDropHooks.h"
62 #include "nsIClipboardDragDropHookList.h"
64 const char * const sSelectAllString = "cmd_selectAll";
65 const char * const sSelectNoneString = "cmd_selectNone";
66 const char * const sCopyImageLocationString = "cmd_copyImageLocation";
67 const char * const sCopyImageContentsString = "cmd_copyImageContents";
68 const char * const sCopyImageString = "cmd_copyImage";
70 const char * const sScrollTopString = "cmd_scrollTop";
71 const char * const sScrollBottomString = "cmd_scrollBottom";
72 const char * const sScrollPageUpString = "cmd_scrollPageUp";
73 const char * const sScrollPageDownString = "cmd_scrollPageDown";
74 const char * const sMovePageUpString = "cmd_movePageUp";
75 const char * const sMovePageDownString = "cmd_movePageDown";
76 const char * const sScrollLineUpString = "cmd_scrollLineUp";
77 const char * const sScrollLineDownString = "cmd_scrollLineDown";
78 const char * const sScrollLeftString = "cmd_scrollLeft";
79 const char * const sScrollRightString = "cmd_scrollRight";
81 // These are so the browser can use editor navigation key bindings
82 // helps with accessibility (boolean pref accessibility.browsewithcaret)
84 const char * const sSelectCharPreviousString = "cmd_selectCharPrevious";
85 const char * const sSelectCharNextString = "cmd_selectCharNext";
87 const char * const sWordPreviousString = "cmd_wordPrevious";
88 const char * const sWordNextString = "cmd_wordNext";
89 const char * const sSelectWordPreviousString = "cmd_selectWordPrevious";
90 const char * const sSelectWordNextString = "cmd_selectWordNext";
92 const char * const sBeginLineString = "cmd_beginLine";
93 const char * const sEndLineString = "cmd_endLine";
94 const char * const sSelectBeginLineString = "cmd_selectBeginLine";
95 const char * const sSelectEndLineString = "cmd_selectEndLine";
97 const char * const sSelectLinePreviousString = "cmd_selectLinePrevious";
98 const char * const sSelectLineNextString = "cmd_selectLineNext";
100 const char * const sSelectPagePreviousString = "cmd_selectPagePrevious";
101 const char * const sSelectPageNextString = "cmd_selectPageNext";
103 const char * const sSelectTopString = "cmd_selectTop";
104 const char * const sSelectBottomString = "cmd_selectBottom";
107 #if 0
108 #pragma mark -
109 #endif
111 // a base class for selection-related commands, for code sharing
112 class nsSelectionCommandsBase : public nsIControllerCommand
114 public:
116 NS_DECL_ISUPPORTS
117 NS_DECL_NSICONTROLLERCOMMAND
119 protected:
121 // subclasses override DoSelectCommand
122 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow) = 0;
124 static nsresult GetPresShellFromWindow(nsIDOMWindow *aWindow, nsIPresShell **aPresShell);
125 static nsresult GetSelectionControllerFromWindow(nsIDOMWindow *aWindow, nsISelectionController **aSelCon);
126 static nsresult GetEventStateManagerForWindow(nsIDOMWindow *aWindow, nsIEventStateManager **aEventStateManager);
128 // no member variables, please, we're stateless!
131 // this class implements commands whose behavior depends on the 'browse with caret' setting
132 class nsSelectMoveScrollCommand : public nsSelectionCommandsBase
134 protected:
136 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow);
138 nsresult DoCommandBrowseWithCaretOn(const char *aCommandName, nsISelectionController *aSelectionController, nsIEventStateManager* aESM);
139 nsresult DoCommandBrowseWithCaretOff(const char *aCommandName, nsISelectionController *aSelectionController);
141 // no member variables, please, we're stateless!
144 // this class implements other selection commands
145 class nsSelectCommand : public nsSelectionCommandsBase
147 protected:
149 virtual nsresult DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow);
151 // no member variables, please, we're stateless!
154 #if 0
155 #pragma mark -
156 #endif
159 NS_IMPL_ISUPPORTS1(nsSelectionCommandsBase, nsIControllerCommand)
161 /* boolean isCommandEnabled (in string aCommandName, in nsISupports aCommandContext); */
162 NS_IMETHODIMP
163 nsSelectionCommandsBase::IsCommandEnabled(const char * aCommandName,
164 nsISupports *aCommandContext,
165 PRBool *outCmdEnabled)
167 // XXX this needs fixing. e.g. you can't scroll up if you're already at the top of
168 // the document.
169 *outCmdEnabled = PR_TRUE;
170 return NS_OK;
173 /* void getCommandStateParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
174 NS_IMETHODIMP
175 nsSelectionCommandsBase::GetCommandStateParams(const char *aCommandName,
176 nsICommandParams *aParams, nsISupports *aCommandContext)
178 // XXX we should probably return the enabled state
179 return NS_ERROR_NOT_IMPLEMENTED;
182 NS_IMETHODIMP
183 nsSelectionCommandsBase::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
185 nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(aCommandContext);
186 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
188 return DoSelectCommand(aCommandName, window);
191 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
192 NS_IMETHODIMP
193 nsSelectionCommandsBase::DoCommandParams(const char *aCommandName,
194 nsICommandParams *aParams, nsISupports *aCommandContext)
196 return DoCommand(aCommandName, aCommandContext);
199 // protected methods
201 nsresult
202 nsSelectionCommandsBase::GetPresShellFromWindow(nsIDOMWindow *aWindow, nsIPresShell **aPresShell)
204 *aPresShell = nsnull;
206 nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(aWindow));
207 NS_ENSURE_TRUE(win, NS_ERROR_FAILURE);
209 nsIDocShell *docShell = win->GetDocShell();
210 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
212 return docShell->GetPresShell(aPresShell);
215 nsresult
216 nsSelectionCommandsBase::GetSelectionControllerFromWindow(nsIDOMWindow *aWindow, nsISelectionController **aSelCon)
218 *aSelCon = nsnull;
220 nsCOMPtr<nsIPresShell> presShell;
221 GetPresShellFromWindow(aWindow, getter_AddRefs(presShell));
222 if (presShell)
223 return CallQueryInterface(presShell, aSelCon);
225 return NS_ERROR_FAILURE;
228 nsresult
229 nsSelectionCommandsBase::GetEventStateManagerForWindow(nsIDOMWindow *aWindow,
230 nsIEventStateManager **aEventStateManager)
232 *aEventStateManager = nsnull;
234 nsCOMPtr<nsIPresShell> presShell;
235 GetPresShellFromWindow(aWindow, getter_AddRefs(presShell));
236 if (presShell)
238 nsPresContext *presContext = presShell->GetPresContext();
239 if (presContext) {
240 NS_ADDREF(*aEventStateManager = presContext->EventStateManager());
241 return NS_OK;
244 return NS_ERROR_FAILURE;
247 #if 0
248 #pragma mark -
249 #endif
251 nsresult
252 nsSelectMoveScrollCommand::DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow)
254 nsCOMPtr<nsISelectionController> selCont;
255 GetSelectionControllerFromWindow(aWindow, getter_AddRefs(selCont));
256 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
258 PRBool caretOn = PR_FALSE;
259 selCont->GetCaretEnabled(&caretOn);
261 nsCOMPtr<nsIEventStateManager> esm;
262 GetEventStateManagerForWindow(aWindow, getter_AddRefs(esm));
264 nsresult rv;
265 // We allow the caret to be moved with arrow keys on any window for which
266 // the caret is enabled. In particular, this includes caret-browsing mode,
267 // but we refer to this mode again in the test condition for readability.
268 if (caretOn || (esm && esm->GetBrowseWithCaret()))
269 rv = DoCommandBrowseWithCaretOn(aCommandName, selCont, esm);
270 else
271 rv = DoCommandBrowseWithCaretOff(aCommandName, selCont);
273 return rv;
276 nsresult
277 nsSelectMoveScrollCommand::DoCommandBrowseWithCaretOn(const char *aCommandName,
278 nsISelectionController *aSelectionController, nsIEventStateManager* aESM)
280 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
282 if (!nsCRT::strcmp(aCommandName, sScrollTopString))
283 rv = aSelectionController->CompleteMove(PR_FALSE, PR_FALSE);
284 else if (!nsCRT::strcmp(aCommandName,sScrollBottomString))
285 rv = aSelectionController->CompleteMove(PR_TRUE, PR_FALSE);
286 // cmd_MovePageUp/Down are used on Window/Unix. They move the caret
287 // in caret browsing mode.
288 else if (!nsCRT::strcmp(aCommandName, sMovePageUpString))
289 rv = aSelectionController->PageMove(PR_FALSE, PR_FALSE);
290 else if (!nsCRT::strcmp(aCommandName, sMovePageDownString))
291 rv = aSelectionController->PageMove(PR_TRUE, PR_FALSE);
292 // cmd_ScrollPageUp/Down are used on Mac, and for the spacebar on all platforms.
293 // They do not move the caret in caret browsing mode.
294 else if (!nsCRT::strcmp(aCommandName, sScrollPageUpString))
295 rv = aSelectionController->ScrollPage(PR_FALSE);
296 else if (!nsCRT::strcmp(aCommandName, sScrollPageDownString))
297 rv = aSelectionController->ScrollPage(PR_TRUE);
298 else if (!nsCRT::strcmp(aCommandName, sScrollLineUpString))
299 rv = aSelectionController->LineMove(PR_FALSE, PR_FALSE);
300 else if (!nsCRT::strcmp(aCommandName, sScrollLineDownString))
301 rv = aSelectionController->LineMove(PR_TRUE, PR_FALSE);
302 else if (!nsCRT::strcmp(aCommandName, sWordPreviousString))
303 rv = aSelectionController->WordMove(PR_FALSE, PR_FALSE);
304 else if (!nsCRT::strcmp(aCommandName, sWordNextString))
305 rv = aSelectionController->WordMove(PR_TRUE, PR_FALSE);
306 else if (!nsCRT::strcmp(aCommandName, sScrollLeftString))
307 rv = aSelectionController->CharacterMove(PR_FALSE, PR_FALSE);
308 else if (!nsCRT::strcmp(aCommandName, sScrollRightString))
309 rv = aSelectionController->CharacterMove(PR_TRUE, PR_FALSE);
310 else if (!nsCRT::strcmp(aCommandName, sBeginLineString))
311 rv = aSelectionController->IntraLineMove(PR_FALSE, PR_FALSE);
312 else if (!nsCRT::strcmp(aCommandName, sEndLineString))
313 rv = aSelectionController->IntraLineMove(PR_TRUE, PR_FALSE);
315 if (NS_SUCCEEDED(rv) && aESM)
317 PRBool dummy;
318 aESM->MoveFocusToCaret(PR_TRUE, &dummy);
321 return rv;
324 nsresult
325 nsSelectMoveScrollCommand::DoCommandBrowseWithCaretOff(const char *aCommandName, nsISelectionController *aSelectionController)
327 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
329 if (!nsCRT::strcmp(aCommandName, sScrollTopString))
330 rv = aSelectionController->CompleteScroll(PR_FALSE);
331 else if (!nsCRT::strcmp(aCommandName,sScrollBottomString))
332 rv = aSelectionController->CompleteScroll(PR_TRUE);
334 // cmd_MovePageUp/Down are used on Window/Unix. They move the caret
335 // in caret browsing mode.
336 else if (!nsCRT::strcmp(aCommandName, sMovePageUpString))
337 rv = aSelectionController->ScrollPage(PR_FALSE);
338 else if (!nsCRT::strcmp(aCommandName, sMovePageDownString))
339 rv = aSelectionController->ScrollPage(PR_TRUE);
340 // cmd_ScrollPageUp/Down are used on Mac. They do not move the
341 // caret in caret browsing mode.
342 else if (!nsCRT::strcmp(aCommandName, sScrollPageUpString))
343 rv = aSelectionController->ScrollPage(PR_FALSE);
344 else if (!nsCRT::strcmp(aCommandName, sScrollPageDownString))
345 rv = aSelectionController->ScrollPage(PR_TRUE);
347 else if (!nsCRT::strcmp(aCommandName, sScrollLineUpString))
348 rv = aSelectionController->ScrollLine(PR_FALSE);
349 else if (!nsCRT::strcmp(aCommandName, sScrollLineDownString))
350 rv = aSelectionController->ScrollLine(PR_TRUE);
351 else if (!nsCRT::strcmp(aCommandName, sScrollLeftString))
352 rv = aSelectionController->ScrollHorizontal(PR_TRUE);
353 else if (!nsCRT::strcmp(aCommandName, sScrollRightString))
354 rv = aSelectionController->ScrollHorizontal(PR_FALSE);
355 // cmd_beginLine/endLine with caret browsing off
356 // will act as cmd_scrollTop/Bottom
357 else if (!nsCRT::strcmp(aCommandName, sBeginLineString))
358 rv = aSelectionController->CompleteScroll(PR_FALSE);
359 else if (!nsCRT::strcmp(aCommandName, sEndLineString))
360 rv = aSelectionController->CompleteScroll(PR_TRUE);
362 return rv;
366 #if 0
367 #pragma mark -
368 #endif
370 nsresult
371 nsSelectCommand::DoSelectCommand(const char *aCommandName, nsIDOMWindow *aWindow)
373 nsCOMPtr<nsISelectionController> selCont;
374 GetSelectionControllerFromWindow(aWindow, getter_AddRefs(selCont));
375 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
377 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
379 // These commands are so the browser can use caret navigation key bindings -
380 // Helps with accessibility - aaronl@netscape.com
381 if (!nsCRT::strcmp(aCommandName, sSelectCharPreviousString))
382 rv = selCont->CharacterMove(PR_FALSE, PR_TRUE);
383 else if (!nsCRT::strcmp(aCommandName, sSelectCharNextString))
384 rv = selCont->CharacterMove(PR_TRUE, PR_TRUE);
385 else if (!nsCRT::strcmp(aCommandName, sSelectWordPreviousString))
386 rv = selCont->WordMove(PR_FALSE, PR_TRUE);
387 else if (!nsCRT::strcmp(aCommandName, sSelectWordNextString))
388 rv = selCont->WordMove(PR_TRUE, PR_TRUE);
389 else if (!nsCRT::strcmp(aCommandName, sSelectBeginLineString))
390 rv = selCont->IntraLineMove(PR_FALSE, PR_TRUE);
391 else if (!nsCRT::strcmp(aCommandName, sSelectEndLineString))
392 rv = selCont->IntraLineMove(PR_TRUE, PR_TRUE);
393 else if (!nsCRT::strcmp(aCommandName, sSelectLinePreviousString))
394 rv = selCont->LineMove(PR_FALSE, PR_TRUE);
395 else if (!nsCRT::strcmp(aCommandName, sSelectLineNextString))
396 rv = selCont->LineMove(PR_TRUE, PR_TRUE);
397 else if (!nsCRT::strcmp(aCommandName, sSelectTopString))
398 rv = selCont->CompleteMove(PR_FALSE, PR_TRUE);
399 else if (!nsCRT::strcmp(aCommandName, sSelectBottomString))
400 rv = selCont->CompleteMove(PR_TRUE, PR_TRUE);
402 return rv;
405 #if 0
406 #pragma mark -
407 #endif
409 class nsClipboardBaseCommand : public nsIControllerCommand
411 public:
413 NS_DECL_ISUPPORTS
414 NS_DECL_NSICONTROLLERCOMMAND
416 protected:
418 virtual nsresult IsClipboardCommandEnabled(const char * aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled) = 0;
419 virtual nsresult DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) = 0;
421 static nsresult GetContentViewerEditFromContext(nsISupports *aContext, nsIContentViewerEdit **aEditInterface);
423 // no member variables, please, we're stateless!
427 NS_IMPL_ISUPPORTS1(nsClipboardBaseCommand, nsIControllerCommand)
430 /*---------------------------------------------------------------------------
432 nsClipboardBaseCommand
434 ----------------------------------------------------------------------------*/
436 NS_IMETHODIMP
437 nsClipboardBaseCommand::IsCommandEnabled(const char * aCommandName,
438 nsISupports *aCommandContext,
439 PRBool *outCmdEnabled)
441 NS_ENSURE_ARG_POINTER(outCmdEnabled);
442 *outCmdEnabled = PR_FALSE;
444 nsCOMPtr<nsIContentViewerEdit> contentEdit;
445 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
446 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
448 return IsClipboardCommandEnabled(aCommandName, contentEdit, outCmdEnabled);
451 NS_IMETHODIMP
452 nsClipboardBaseCommand::DoCommand(const char *aCommandName,
453 nsISupports *aCommandContext)
455 nsCOMPtr<nsIContentViewerEdit> contentEdit;
456 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
457 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
459 return DoClipboardCommand(aCommandName, contentEdit, nsnull);
462 NS_IMETHODIMP
463 nsClipboardBaseCommand::GetCommandStateParams(const char *aCommandName,
464 nsICommandParams *aParams,
465 nsISupports *aCommandContext)
467 return NS_ERROR_NOT_IMPLEMENTED;
470 NS_IMETHODIMP
471 nsClipboardBaseCommand::DoCommandParams(const char *aCommandName,
472 nsICommandParams *aParams,
473 nsISupports *aCommandContext)
475 nsCOMPtr<nsIContentViewerEdit> contentEdit;
476 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
477 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
479 return DoClipboardCommand(aCommandName, contentEdit, aParams);
482 nsresult
483 nsClipboardBaseCommand::GetContentViewerEditFromContext(nsISupports *aContext,
484 nsIContentViewerEdit **aEditInterface)
486 NS_ENSURE_ARG(aEditInterface);
487 *aEditInterface = nsnull;
489 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
490 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
492 nsIDocShell *docShell = window->GetDocShell();
493 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
495 nsCOMPtr<nsIContentViewer> viewer;
496 docShell->GetContentViewer(getter_AddRefs(viewer));
497 nsCOMPtr<nsIContentViewerEdit> edit(do_QueryInterface(viewer));
498 NS_ENSURE_TRUE(edit, NS_ERROR_FAILURE);
500 *aEditInterface = edit;
501 NS_ADDREF(*aEditInterface);
502 return NS_OK;
505 #if 0
506 #pragma mark -
507 #endif
509 #define NS_DECL_CLIPBOARD_COMMAND(_cmd) \
510 class _cmd : public nsClipboardBaseCommand \
512 protected: \
514 virtual nsresult IsClipboardCommandEnabled(const char* aCommandName, \
515 nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled); \
516 virtual nsresult DoClipboardCommand(const char* aCommandName, \
517 nsIContentViewerEdit* aEdit, nsICommandParams* aParams); \
518 /* no member variables, please, we're stateless! */ \
521 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyCommand)
522 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCutCommand)
523 NS_DECL_CLIPBOARD_COMMAND(nsClipboardPasteCommand)
524 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyLinkCommand)
525 NS_DECL_CLIPBOARD_COMMAND(nsClipboardImageCommands)
526 NS_DECL_CLIPBOARD_COMMAND(nsClipboardSelectAllNoneCommands)
527 NS_DECL_CLIPBOARD_COMMAND(nsClipboardGetContentsCommand)
529 #if 0
530 #pragma mark -
531 #endif
534 nsresult
535 nsClipboardCutCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
537 return aEdit->GetCutable(outCmdEnabled);
540 nsresult
541 nsClipboardCutCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
543 return aEdit->CutSelection();
546 #if 0
547 #pragma mark -
548 #endif
550 nsresult
551 nsClipboardCopyCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
553 return aEdit->GetCopyable(outCmdEnabled);
556 nsresult
557 nsClipboardCopyCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
559 return aEdit->CopySelection();
562 #if 0
563 #pragma mark -
564 #endif
566 nsresult
567 nsClipboardPasteCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
569 return aEdit->GetPasteable(outCmdEnabled);
572 nsresult
573 nsClipboardPasteCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
575 return aEdit->Paste();
579 #if 0
580 #pragma mark -
581 #endif
583 nsresult
584 nsClipboardCopyLinkCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
586 return aEdit->GetInLink(outCmdEnabled);
589 nsresult
590 nsClipboardCopyLinkCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
592 return aEdit->CopyLinkLocation();
595 #if 0
596 #pragma mark -
597 #endif
599 nsresult
600 nsClipboardImageCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
602 return aEdit->GetInImage(outCmdEnabled);
605 nsresult
606 nsClipboardImageCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
608 if (!nsCRT::strcmp(sCopyImageLocationString, aCommandName))
609 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_TEXT);
610 if (!nsCRT::strcmp(sCopyImageContentsString, aCommandName))
611 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_DATA);
613 PRInt32 copyFlags = nsIContentViewerEdit::COPY_IMAGE_ALL;
614 if (aParams)
615 aParams->GetLongValue("imageCopy", &copyFlags);
616 return aEdit->CopyImage(copyFlags);
619 #if 0
620 #pragma mark -
621 #endif
623 nsresult
624 nsClipboardSelectAllNoneCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
626 *outCmdEnabled = PR_TRUE;
627 return NS_OK;
630 nsresult
631 nsClipboardSelectAllNoneCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
633 if (!nsCRT::strcmp(sSelectAllString, aCommandName))
634 return aEdit->SelectAll();
636 return aEdit->ClearSelection();
640 #if 0
641 #pragma mark -
642 #endif
644 nsresult
645 nsClipboardGetContentsCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, PRBool *outCmdEnabled)
647 return aEdit->GetCanGetContents(outCmdEnabled);
650 nsresult
651 nsClipboardGetContentsCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
653 NS_ENSURE_ARG(aParams);
655 nsCAutoString mimeType("text/plain");
657 nsXPIDLCString format; // nsICommandParams needs to use nsACString
658 if (NS_SUCCEEDED(aParams->GetCStringValue("format", getter_Copies(format))))
659 mimeType.Assign(format);
661 PRBool selectionOnly = PR_FALSE;
662 aParams->GetBooleanValue("selection_only", &selectionOnly);
664 nsAutoString contents;
665 nsresult rv = aEdit->GetContents(mimeType.get(), selectionOnly, contents);
666 if (NS_FAILED(rv))
667 return rv;
669 return aParams->SetStringValue("result", contents);
673 #if 0
674 #pragma mark -
675 #endif
677 class nsWebNavigationBaseCommand : public nsIControllerCommand
679 public:
681 NS_DECL_ISUPPORTS
682 NS_DECL_NSICONTROLLERCOMMAND
684 protected:
686 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled) = 0;
687 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) = 0;
689 static nsresult GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation);
691 // no member variables, please, we're stateless!
694 #if 0 // Remove unless needed again, bug 204777
695 class nsGoForwardCommand : public nsWebNavigationBaseCommand
697 protected:
699 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled);
700 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
701 // no member variables, please, we're stateless!
704 class nsGoBackCommand : 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!
712 #endif
714 /*---------------------------------------------------------------------------
716 nsWebNavigationCommands
717 no params
718 ----------------------------------------------------------------------------*/
720 NS_IMPL_ISUPPORTS1(nsWebNavigationBaseCommand, nsIControllerCommand)
722 NS_IMETHODIMP
723 nsWebNavigationBaseCommand::IsCommandEnabled(const char * aCommandName,
724 nsISupports *aCommandContext,
725 PRBool *outCmdEnabled)
727 NS_ENSURE_ARG_POINTER(outCmdEnabled);
728 *outCmdEnabled = PR_FALSE;
730 nsCOMPtr<nsIWebNavigation> webNav;
731 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
732 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
734 return IsCommandEnabled(aCommandName, webNav, outCmdEnabled);
737 NS_IMETHODIMP
738 nsWebNavigationBaseCommand::GetCommandStateParams(const char *aCommandName,
739 nsICommandParams *aParams, nsISupports *aCommandContext)
741 // XXX we should probably return the enabled state
742 return NS_ERROR_NOT_IMPLEMENTED;
745 NS_IMETHODIMP
746 nsWebNavigationBaseCommand::DoCommand(const char *aCommandName,
747 nsISupports *aCommandContext)
749 nsCOMPtr<nsIWebNavigation> webNav;
750 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
751 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
753 return DoWebNavCommand(aCommandName, webNav);
756 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
757 NS_IMETHODIMP
758 nsWebNavigationBaseCommand::DoCommandParams(const char *aCommandName,
759 nsICommandParams *aParams, nsISupports *aCommandContext)
761 return DoCommand(aCommandName, aCommandContext);
764 nsresult
765 nsWebNavigationBaseCommand::GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation)
767 nsCOMPtr<nsIInterfaceRequestor> windowReq = do_QueryInterface(aContext);
768 CallGetInterface(windowReq.get(), aWebNavigation);
769 return (*aWebNavigation) ? NS_OK : NS_ERROR_FAILURE;
772 #if 0
773 #pragma mark -
774 #endif
776 #if 0 // Remove unless needed again, bug 204777
777 nsresult
778 nsGoForwardCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled)
780 return aWebNavigation->GetCanGoForward(outCmdEnabled);
783 nsresult
784 nsGoForwardCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
786 return aWebNavigation->GoForward();
789 nsresult
790 nsGoBackCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, PRBool *outCmdEnabled)
792 return aWebNavigation->GetCanGoBack(outCmdEnabled);
795 nsresult
796 nsGoBackCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
798 return aWebNavigation->GoBack();
800 #endif
802 /*---------------------------------------------------------------------------
804 nsClipboardDragDropHookCommand
805 params value type possible values
806 "addhook" isupports nsIClipboardDragDropHooks as nsISupports
807 "removehook" isupports nsIClipboardDragDropHooks as nsISupports
809 ----------------------------------------------------------------------------*/
811 class nsClipboardDragDropHookCommand : public nsIControllerCommand
813 public:
815 NS_DECL_ISUPPORTS
816 NS_DECL_NSICONTROLLERCOMMAND
818 protected:
819 // no member variables, please, we're stateless!
823 NS_IMPL_ISUPPORTS1(nsClipboardDragDropHookCommand, nsIControllerCommand)
825 NS_IMETHODIMP
826 nsClipboardDragDropHookCommand::IsCommandEnabled(const char * aCommandName,
827 nsISupports *aCommandContext,
828 PRBool *outCmdEnabled)
830 *outCmdEnabled = PR_TRUE;
831 return NS_OK;
834 NS_IMETHODIMP
835 nsClipboardDragDropHookCommand::DoCommand(const char *aCommandName,
836 nsISupports *aCommandContext)
838 return NS_ERROR_FAILURE;
841 NS_IMETHODIMP
842 nsClipboardDragDropHookCommand::DoCommandParams(const char *aCommandName,
843 nsICommandParams *aParams,
844 nsISupports *aCommandContext)
846 NS_ENSURE_ARG(aParams);
848 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aCommandContext);
849 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
851 nsIDocShell *docShell = window->GetDocShell();
853 nsCOMPtr<nsIClipboardDragDropHookList> obj = do_GetInterface(docShell);
854 if (!obj) return NS_ERROR_INVALID_ARG;
856 nsCOMPtr<nsISupports> isuppHook;
858 nsresult returnValue = NS_OK;
859 nsresult rv = aParams->GetISupportsValue("addhook", getter_AddRefs(isuppHook));
860 if (NS_SUCCEEDED(rv))
862 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
863 if (hook)
864 returnValue = obj->AddClipboardDragDropHooks(hook);
865 else
866 returnValue = NS_ERROR_INVALID_ARG;
869 rv = aParams->GetISupportsValue("removehook", getter_AddRefs(isuppHook));
870 if (NS_SUCCEEDED(rv))
872 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
873 if (hook)
875 rv = obj->RemoveClipboardDragDropHooks(hook);
876 if (NS_FAILED(rv) && NS_SUCCEEDED(returnValue))
877 returnValue = rv;
879 else
880 returnValue = NS_ERROR_INVALID_ARG;
883 return returnValue;
886 NS_IMETHODIMP
887 nsClipboardDragDropHookCommand::GetCommandStateParams(const char *aCommandName,
888 nsICommandParams *aParams,
889 nsISupports *aCommandContext)
891 NS_ENSURE_ARG_POINTER(aParams);
892 return aParams->SetBooleanValue("state_enabled", PR_TRUE);
895 /*---------------------------------------------------------------------------
897 RegisterWindowCommands
899 ----------------------------------------------------------------------------*/
901 #define NS_REGISTER_ONE_COMMAND(_cmdClass, _cmdName) \
903 _cmdClass* theCmd; \
904 NS_NEWXPCOM(theCmd, _cmdClass); \
905 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
906 rv = inCommandTable->RegisterCommand(_cmdName, \
907 static_cast<nsIControllerCommand *>(theCmd)); \
910 #define NS_REGISTER_FIRST_COMMAND(_cmdClass, _cmdName) \
912 _cmdClass* theCmd; \
913 NS_NEWXPCOM(theCmd, _cmdClass); \
914 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
915 rv = inCommandTable->RegisterCommand(_cmdName, \
916 static_cast<nsIControllerCommand *>(theCmd));
918 #define NS_REGISTER_NEXT_COMMAND(_cmdClass, _cmdName) \
919 rv = inCommandTable->RegisterCommand(_cmdName, \
920 static_cast<nsIControllerCommand *>(theCmd));
922 #define NS_REGISTER_LAST_COMMAND(_cmdClass, _cmdName) \
923 rv = inCommandTable->RegisterCommand(_cmdName, \
924 static_cast<nsIControllerCommand *>(theCmd)); \
928 // static
929 nsresult
930 nsWindowCommandRegistration::RegisterWindowCommands(
931 nsIControllerCommandTable *inCommandTable)
933 nsresult rv;
935 // XXX rework the macros to use a loop is possible, reducing code size
937 // this set of commands is affected by the 'browse with caret' setting
938 NS_REGISTER_FIRST_COMMAND(nsSelectMoveScrollCommand, sScrollTopString);
939 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollBottomString);
940 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordPreviousString);
941 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordNextString);
942 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sBeginLineString);
943 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sEndLineString);
944 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageUpString);
945 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageDownString);
946 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageUpString);
947 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageDownString);
948 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineUpString);
949 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineDownString);
950 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLeftString);
951 NS_REGISTER_LAST_COMMAND(nsSelectMoveScrollCommand, sScrollRightString);
953 NS_REGISTER_FIRST_COMMAND(nsSelectCommand, sSelectCharPreviousString);
954 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectCharNextString);
955 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordPreviousString);
956 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordNextString);
957 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectBeginLineString);
958 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectEndLineString);
959 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLinePreviousString);
960 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLineNextString);
961 // XXX these commands were never implemented. fix me.
962 // NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPagePreviousString);
963 // NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageNextString);
964 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectTopString);
965 NS_REGISTER_LAST_COMMAND(nsSelectCommand, sSelectBottomString);
967 NS_REGISTER_ONE_COMMAND(nsClipboardCopyCommand, "cmd_copy");
968 NS_REGISTER_ONE_COMMAND(nsClipboardCutCommand, "cmd_cut");
969 NS_REGISTER_ONE_COMMAND(nsClipboardPasteCommand, "cmd_paste");
970 NS_REGISTER_ONE_COMMAND(nsClipboardCopyLinkCommand, "cmd_copyLink");
971 NS_REGISTER_FIRST_COMMAND(nsClipboardImageCommands, sCopyImageLocationString);
972 NS_REGISTER_NEXT_COMMAND(nsClipboardImageCommands, sCopyImageContentsString);
973 NS_REGISTER_LAST_COMMAND(nsClipboardImageCommands, sCopyImageString);
974 NS_REGISTER_FIRST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectAllString);
975 NS_REGISTER_LAST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectNoneString);
977 NS_REGISTER_ONE_COMMAND(nsClipboardGetContentsCommand, "cmd_getContents");
979 #if 0 // Remove unless needed again, bug 204777
980 NS_REGISTER_ONE_COMMAND(nsGoBackCommand, "cmd_browserBack");
981 NS_REGISTER_ONE_COMMAND(nsGoForwardCommand, "cmd_browserForward");
982 #endif
984 NS_REGISTER_ONE_COMMAND(nsClipboardDragDropHookCommand, "cmd_clipboardDragDropHook");
986 return rv;