editor/libeditor/base/nsEditorCommands.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "mozFlushType.h"
     8 #include "mozilla/Assertions.h"
     9 #include "nsCOMPtr.h"
    10 #include "nsCRT.h"
    11 #include "nsDebug.h"
    12 #include "nsEditorCommands.h"
    13 #include "nsError.h"
    14 #include "nsIClipboard.h"
    15 #include "nsICommandParams.h"
    16 #include "nsID.h"
    17 #include "nsIDOMDocument.h"
    18 #include "nsIDocument.h"
    19 #include "nsIEditor.h"
    20 #include "nsIEditorMailSupport.h"
    21 #include "nsIPlaintextEditor.h"
    22 #include "nsISelection.h"
    23 #include "nsISelectionController.h"
    24 #include "nsITransferable.h"
    25 #include "nsString.h"
    26 #include "nsAString.h"
    28 class nsISupports;
    31 #define STATE_ENABLED  "state_enabled"
    32 #define STATE_DATA "state_data"
    35 nsBaseEditorCommand::nsBaseEditorCommand()
    36 {
    37 }
    39 NS_IMPL_ISUPPORTS(nsBaseEditorCommand, nsIControllerCommand)
    42 NS_IMETHODIMP
    43 nsUndoCommand::IsCommandEnabled(const char * aCommandName, 
    44                                 nsISupports *aCommandRefCon, 
    45                                 bool *outCmdEnabled)
    46 {
    47   NS_ENSURE_ARG_POINTER(outCmdEnabled);
    48   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
    49   if (editor)
    50   {
    51     bool isEnabled, isEditable = false;
    52     nsresult rv = editor->GetIsSelectionEditable(&isEditable);
    53     NS_ENSURE_SUCCESS(rv, rv);
    54     if (isEditable)
    55       return editor->CanUndo(&isEnabled, outCmdEnabled);
    56   }
    58   *outCmdEnabled = false;
    59   return NS_OK;
    60 }
    63 NS_IMETHODIMP
    64 nsUndoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
    65 {
    66   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
    67   if (editor)
    68     return editor->Undo(1);
    70   return NS_ERROR_FAILURE;
    71 }
    73 NS_IMETHODIMP 
    74 nsUndoCommand::DoCommandParams(const char *aCommandName,
    75                                nsICommandParams *aParams,
    76                                nsISupports *aCommandRefCon)
    77 {
    78   return DoCommand(aCommandName, aCommandRefCon);
    79 }
    81 NS_IMETHODIMP 
    82 nsUndoCommand::GetCommandStateParams(const char *aCommandName,
    83                                      nsICommandParams *aParams,
    84                                      nsISupports *aCommandRefCon)
    85 {
    86   bool canUndo;
    87   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
    88   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
    89 }
    91 NS_IMETHODIMP
    92 nsRedoCommand::IsCommandEnabled(const char * aCommandName,
    93                                 nsISupports *aCommandRefCon,
    94                                 bool *outCmdEnabled)
    95 {
    96   NS_ENSURE_ARG_POINTER(outCmdEnabled);
    97   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
    98   if (editor)
    99   {
   100     bool isEnabled, isEditable = false;
   101     nsresult rv = editor->GetIsSelectionEditable(&isEditable);
   102     NS_ENSURE_SUCCESS(rv, rv);
   103     if (isEditable)
   104       return editor->CanRedo(&isEnabled, outCmdEnabled);
   105   }
   107   *outCmdEnabled = false;
   108   return NS_OK;
   109 }
   112 NS_IMETHODIMP
   113 nsRedoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   114 {
   115   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   116   if (editor)
   117     return editor->Redo(1);
   119   return NS_ERROR_FAILURE;
   120 }
   122 NS_IMETHODIMP 
   123 nsRedoCommand::DoCommandParams(const char *aCommandName,
   124                                nsICommandParams *aParams,
   125                                nsISupports *aCommandRefCon)
   126 {
   127   return DoCommand(aCommandName, aCommandRefCon);
   128 }
   130 NS_IMETHODIMP 
   131 nsRedoCommand::GetCommandStateParams(const char *aCommandName,
   132                                      nsICommandParams *aParams,
   133                                      nsISupports *aCommandRefCon)
   134 {
   135   bool canUndo;
   136   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   137   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   138 }
   140 NS_IMETHODIMP
   141 nsClearUndoCommand::IsCommandEnabled(const char * aCommandName,
   142                                      nsISupports *refCon, bool *outCmdEnabled)
   143 { 
   144   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   145   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   146   if (editor)
   147     return editor->GetIsSelectionEditable(outCmdEnabled);
   149   *outCmdEnabled = false;
   150   return NS_OK;
   151 }
   154 NS_IMETHODIMP
   155 nsClearUndoCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
   156 { 
   157   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   158   NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
   160   editor->EnableUndo(false);  // Turning off undo clears undo/redo stacks.
   161   editor->EnableUndo(true);   // This re-enables undo/redo.
   163   return NS_OK;
   164 }
   166 NS_IMETHODIMP                       
   167 nsClearUndoCommand::DoCommandParams(const char *aCommandName,
   168                                     nsICommandParams *aParams,
   169                                     nsISupports *refCon)
   170 {
   171   return DoCommand(aCommandName, refCon);
   172 }
   174 NS_IMETHODIMP                                     
   175 nsClearUndoCommand::GetCommandStateParams(const char *aCommandName,
   176                                           nsICommandParams *aParams,
   177                                           nsISupports *refCon)
   178 { 
   179   NS_ENSURE_ARG_POINTER(aParams);
   181   bool enabled;
   182   nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
   183   NS_ENSURE_SUCCESS(rv, rv);
   185   return aParams->SetBooleanValue(STATE_ENABLED, enabled);
   186 }
   188 NS_IMETHODIMP
   189 nsCutCommand::IsCommandEnabled(const char * aCommandName,
   190                                nsISupports *aCommandRefCon,
   191                                bool *outCmdEnabled)
   192 {
   193   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   194   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   195   if (editor)
   196   {
   197     bool isEditable = false;
   198     nsresult rv = editor->GetIsSelectionEditable(&isEditable);
   199     NS_ENSURE_SUCCESS(rv, rv);
   200     if (isEditable)
   201       return editor->CanCut(outCmdEnabled);
   202   }
   204   *outCmdEnabled = false;
   205   return NS_OK;
   206 }
   209 NS_IMETHODIMP
   210 nsCutCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   211 {
   212   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   213   if (editor)
   214     return editor->Cut();
   216   return NS_ERROR_FAILURE;
   217 }
   219 NS_IMETHODIMP 
   220 nsCutCommand::DoCommandParams(const char *aCommandName,
   221                               nsICommandParams *aParams,
   222                               nsISupports *aCommandRefCon)
   223 {
   224   return DoCommand(aCommandName, aCommandRefCon);
   225 }
   227 NS_IMETHODIMP 
   228 nsCutCommand::GetCommandStateParams(const char *aCommandName,
   229                                     nsICommandParams *aParams,
   230                                     nsISupports *aCommandRefCon)
   231 {
   232   bool canUndo;
   233   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   234   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   235 }
   238 NS_IMETHODIMP
   239 nsCutOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
   240                                        nsISupports *aCommandRefCon,
   241                                        bool *outCmdEnabled)
   242 {
   243   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   244   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   245   if (editor)
   246     return editor->GetIsSelectionEditable(outCmdEnabled);
   248   *outCmdEnabled = false;
   249   return NS_OK;
   250 }
   253 NS_IMETHODIMP
   254 nsCutOrDeleteCommand::DoCommand(const char *aCommandName,
   255                                 nsISupports *aCommandRefCon)
   256 {
   257   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   258   if (editor)
   259   {
   260     nsCOMPtr<nsISelection> selection;
   261     nsresult rv = editor->GetSelection(getter_AddRefs(selection));
   262     if (NS_SUCCEEDED(rv) && selection && selection->Collapsed()) {
   263       return editor->DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
   264     }
   265     return editor->Cut();
   266   }
   268   return NS_ERROR_FAILURE;
   269 }
   271 NS_IMETHODIMP 
   272 nsCutOrDeleteCommand::DoCommandParams(const char *aCommandName,
   273                                       nsICommandParams *aParams,
   274                                       nsISupports *aCommandRefCon)
   275 {
   276   return DoCommand(aCommandName, aCommandRefCon);
   277 }
   279 NS_IMETHODIMP 
   280 nsCutOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
   281                                             nsICommandParams *aParams,
   282                                             nsISupports *aCommandRefCon)
   283 {
   284   bool canUndo;
   285   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   286   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   287 }
   289 NS_IMETHODIMP
   290 nsCopyCommand::IsCommandEnabled(const char * aCommandName,
   291                                 nsISupports *aCommandRefCon,
   292                                 bool *outCmdEnabled)
   293 {
   294   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   295   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   296   if (editor)
   297     return editor->CanCopy(outCmdEnabled);
   299   *outCmdEnabled = false;
   300   return NS_OK;
   301 }
   304 NS_IMETHODIMP
   305 nsCopyCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   306 {
   307   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   308   if (editor)
   309     return editor->Copy();
   311   return NS_ERROR_FAILURE;
   312 }
   314 NS_IMETHODIMP 
   315 nsCopyCommand::DoCommandParams(const char *aCommandName,
   316                                nsICommandParams *aParams,
   317                                nsISupports *aCommandRefCon)
   318 {
   319   return DoCommand(aCommandName, aCommandRefCon);
   320 }
   322 NS_IMETHODIMP 
   323 nsCopyCommand::GetCommandStateParams(const char *aCommandName,
   324                                      nsICommandParams *aParams,
   325                                      nsISupports *aCommandRefCon)
   326 {
   327   bool canUndo;
   328   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   329   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   330 }
   332 NS_IMETHODIMP
   333 nsCopyOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
   334                                         nsISupports *aCommandRefCon,
   335                                         bool *outCmdEnabled)
   336 {
   337   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   338   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   339   if (editor)
   340     return editor->GetIsSelectionEditable(outCmdEnabled);
   342   *outCmdEnabled = false;
   343   return NS_OK;
   344 }
   347 NS_IMETHODIMP
   348 nsCopyOrDeleteCommand::DoCommand(const char *aCommandName,
   349                                  nsISupports *aCommandRefCon)
   350 {
   351   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   352   if (editor)
   353   {
   354     nsCOMPtr<nsISelection> selection;
   355     nsresult rv = editor->GetSelection(getter_AddRefs(selection));
   356     if (NS_SUCCEEDED(rv) && selection && selection->Collapsed()) {
   357       return editor->DeleteSelection(nsIEditor::eNextWord, nsIEditor::eStrip);
   358     }
   359     return editor->Copy();
   360   }
   362   return NS_ERROR_FAILURE;
   363 }
   365 NS_IMETHODIMP 
   366 nsCopyOrDeleteCommand::DoCommandParams(const char *aCommandName,
   367                                        nsICommandParams *aParams,
   368                                        nsISupports *aCommandRefCon)
   369 {
   370   return DoCommand(aCommandName, aCommandRefCon);
   371 }
   373 NS_IMETHODIMP 
   374 nsCopyOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
   375                                              nsICommandParams *aParams,
   376                                              nsISupports *aCommandRefCon)
   377 {
   378   bool canUndo;
   379   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   380   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   381 }
   383 NS_IMETHODIMP
   384 nsPasteCommand::IsCommandEnabled(const char *aCommandName,
   385                                  nsISupports *aCommandRefCon,
   386                                  bool *outCmdEnabled)
   387 {
   388   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   389   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   390   if (editor)
   391   {
   392     bool isEditable = false;
   393     nsresult rv = editor->GetIsSelectionEditable(&isEditable);
   394     NS_ENSURE_SUCCESS(rv, rv);
   395     if (isEditable)
   396       return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
   397   }
   399   *outCmdEnabled = false;
   400   return NS_OK;
   401 }
   404 NS_IMETHODIMP
   405 nsPasteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   406 {
   407   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   408   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   410   return editor->Paste(nsIClipboard::kGlobalClipboard);
   411 }
   413 NS_IMETHODIMP 
   414 nsPasteCommand::DoCommandParams(const char *aCommandName,
   415                                 nsICommandParams *aParams,
   416                                 nsISupports *aCommandRefCon)
   417 {
   418   return DoCommand(aCommandName, aCommandRefCon);
   419 }
   421 NS_IMETHODIMP 
   422 nsPasteCommand::GetCommandStateParams(const char *aCommandName,
   423                                       nsICommandParams *aParams,
   424                                       nsISupports *aCommandRefCon)
   425 {
   426   bool canUndo;
   427   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   428   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   429 }
   431 NS_IMETHODIMP
   432 nsPasteTransferableCommand::IsCommandEnabled(const char *aCommandName,
   433                                              nsISupports *aCommandRefCon,
   434                                              bool *outCmdEnabled)
   435 {
   436   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   437   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   438   if (editor)
   439   {
   440     bool isEditable = false;
   441     nsresult rv = editor->GetIsSelectionEditable(&isEditable);
   442     NS_ENSURE_SUCCESS(rv, rv);
   443     if (isEditable)
   444       return editor->CanPasteTransferable(nullptr, outCmdEnabled);
   445   }
   447   *outCmdEnabled = false;
   448   return NS_OK;
   449 }
   451 NS_IMETHODIMP
   452 nsPasteTransferableCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   453 {
   454   return NS_ERROR_FAILURE;
   455 }
   457 NS_IMETHODIMP 
   458 nsPasteTransferableCommand::DoCommandParams(const char *aCommandName,
   459                                             nsICommandParams *aParams,
   460                                             nsISupports *aCommandRefCon)
   461 {
   462   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   463   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   465   nsCOMPtr<nsISupports> supports;
   466   aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
   467   NS_ENSURE_TRUE(supports, NS_ERROR_FAILURE);
   469   nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
   470   NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
   472   return editor->PasteTransferable(trans);
   473 }
   475 NS_IMETHODIMP 
   476 nsPasteTransferableCommand::GetCommandStateParams(const char *aCommandName,
   477                                                   nsICommandParams *aParams,
   478                                                   nsISupports *aCommandRefCon)
   479 {
   480   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   481   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   483   nsCOMPtr<nsITransferable> trans;
   485   nsCOMPtr<nsISupports> supports;
   486   aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
   487   if (supports) {
   488     trans = do_QueryInterface(supports);
   489     NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
   490   }
   492   bool canPaste;
   493   nsresult rv = editor->CanPasteTransferable(trans, &canPaste);
   494   NS_ENSURE_SUCCESS(rv, rv);
   496   return aParams->SetBooleanValue(STATE_ENABLED, canPaste);
   497 }
   499 NS_IMETHODIMP
   500 nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
   501                                  nsISupports *aCommandRefCon,
   502                                  bool *outCmdEnabled)
   503 {
   504   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   505   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   506   if (editor)
   507     return editor->GetIsSelectionEditable(outCmdEnabled);
   509   *outCmdEnabled = false;
   510   return NS_OK;
   511 }
   513 NS_IMETHODIMP
   514 nsSwitchTextDirectionCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
   515 {
   516   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   517   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   519   return editor->SwitchTextDirection();
   520 }
   522 NS_IMETHODIMP 
   523 nsSwitchTextDirectionCommand::DoCommandParams(const char *aCommandName,
   524                                 nsICommandParams *aParams,
   525                                 nsISupports *aCommandRefCon)
   526 {
   527   return DoCommand(aCommandName, aCommandRefCon);
   528 }
   530 NS_IMETHODIMP 
   531 nsSwitchTextDirectionCommand::GetCommandStateParams(const char *aCommandName,
   532                                       nsICommandParams *aParams,
   533                                       nsISupports *aCommandRefCon)
   534 {
   535   bool canSwitchTextDirection = true;
   536   IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
   537   return aParams->SetBooleanValue(STATE_ENABLED, canSwitchTextDirection);
   538 }
   540 NS_IMETHODIMP
   541 nsDeleteCommand::IsCommandEnabled(const char* aCommandName,
   542                                   nsISupports* aCommandRefCon,
   543                                   bool* outCmdEnabled)
   544 {
   545   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   546   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   547   *outCmdEnabled = false;
   549   NS_ENSURE_TRUE(editor, NS_OK);
   551   // We can generally delete whenever the selection is editable.  However,
   552   // cmd_delete doesn't make sense if the selection is collapsed because it's
   553   // directionless, which is the same condition under which we can't cut.
   554   nsresult rv = editor->GetIsSelectionEditable(outCmdEnabled);
   555   NS_ENSURE_SUCCESS(rv, rv);
   557   if (!nsCRT::strcmp("cmd_delete", aCommandName) && *outCmdEnabled) {
   558     rv = editor->CanCut(outCmdEnabled);
   559     NS_ENSURE_SUCCESS(rv, rv);
   560   }
   562   return NS_OK;
   563 }
   566 NS_IMETHODIMP
   567 nsDeleteCommand::DoCommand(const char* aCommandName,
   568                            nsISupports* aCommandRefCon)
   569 {
   570   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   571   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   573   nsIEditor::EDirection deleteDir = nsIEditor::eNone;
   575   if (!nsCRT::strcmp("cmd_delete", aCommandName)) {
   576     // Really this should probably be eNone, but it only makes a difference if
   577     // the selection is collapsed, and then this command is disabled.  So let's
   578     // keep it as it always was to avoid breaking things.
   579     deleteDir = nsIEditor::ePrevious;
   580   } else if (!nsCRT::strcmp("cmd_deleteCharForward", aCommandName)) {
   581     deleteDir = nsIEditor::eNext;
   582   } else if (!nsCRT::strcmp("cmd_deleteCharBackward", aCommandName)) {
   583     deleteDir = nsIEditor::ePrevious;
   584   } else if (!nsCRT::strcmp("cmd_deleteWordBackward", aCommandName)) {
   585     deleteDir = nsIEditor::ePreviousWord;
   586   } else if (!nsCRT::strcmp("cmd_deleteWordForward", aCommandName)) {
   587     deleteDir = nsIEditor::eNextWord;
   588   } else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine", aCommandName)) {
   589     deleteDir = nsIEditor::eToBeginningOfLine;
   590   } else if (!nsCRT::strcmp("cmd_deleteToEndOfLine", aCommandName)) {
   591     deleteDir = nsIEditor::eToEndOfLine;
   592   } else {
   593     MOZ_CRASH("Unrecognized nsDeleteCommand");
   594   }
   596   return editor->DeleteSelection(deleteDir, nsIEditor::eStrip);
   597 }
   599 NS_IMETHODIMP 
   600 nsDeleteCommand::DoCommandParams(const char *aCommandName,
   601                                  nsICommandParams *aParams,
   602                                  nsISupports *aCommandRefCon)
   603 {
   604   return DoCommand(aCommandName, aCommandRefCon);
   605 }
   607 NS_IMETHODIMP 
   608 nsDeleteCommand::GetCommandStateParams(const char *aCommandName,
   609                                        nsICommandParams *aParams,
   610                                        nsISupports *aCommandRefCon)
   611 {
   612   bool canUndo;
   613   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   614   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   615 }
   617 NS_IMETHODIMP
   618 nsSelectAllCommand::IsCommandEnabled(const char * aCommandName,
   619                                      nsISupports *aCommandRefCon,
   620                                      bool *outCmdEnabled)
   621 {
   622   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   624   nsresult rv = NS_OK;
   625   // You can always select all, unless the selection is editable,
   626   // and the editable region is empty!
   627   *outCmdEnabled = true;
   628   bool docIsEmpty;
   630   // you can select all if there is an editor which is non-empty
   631   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   632   if (editor) {
   633     rv = editor->GetDocumentIsEmpty(&docIsEmpty);
   634     NS_ENSURE_SUCCESS(rv, rv);
   635     *outCmdEnabled = !docIsEmpty;
   636   }
   638   return rv;
   639 }
   642 NS_IMETHODIMP
   643 nsSelectAllCommand::DoCommand(const char *aCommandName,
   644                               nsISupports *aCommandRefCon)
   645 {
   646   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   647   if (editor)
   648     return editor->SelectAll();
   650   return NS_ERROR_FAILURE;
   651 }
   653 NS_IMETHODIMP 
   654 nsSelectAllCommand::DoCommandParams(const char *aCommandName,
   655                                     nsICommandParams *aParams,
   656                                     nsISupports *aCommandRefCon)
   657 {
   658   return DoCommand(aCommandName, aCommandRefCon);
   659 }
   661 NS_IMETHODIMP 
   662 nsSelectAllCommand::GetCommandStateParams(const char *aCommandName,
   663                                           nsICommandParams *aParams,
   664                                           nsISupports *aCommandRefCon)
   665 {
   666   bool canUndo;
   667   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   668   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   669 }
   672 NS_IMETHODIMP
   673 nsSelectionMoveCommands::IsCommandEnabled(const char * aCommandName,
   674                                           nsISupports *aCommandRefCon,
   675                                           bool *outCmdEnabled)
   676 {
   677   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   678   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   679   if (editor)
   680     return editor->GetIsSelectionEditable(outCmdEnabled);
   682   *outCmdEnabled = false;
   683   return NS_OK;
   684 }
   686 NS_IMETHODIMP
   687 nsSelectionMoveCommands::DoCommand(const char *aCommandName,
   688                                    nsISupports *aCommandRefCon)
   689 {
   690   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   691   NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
   693   nsCOMPtr<nsIDOMDocument> domDoc;
   694   editor->GetDocument(getter_AddRefs(domDoc));
   695   nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
   696   if (doc) {
   697     // Most of the commands below (possibly all of them) need layout to
   698     // be up to date.
   699     doc->FlushPendingNotifications(Flush_Layout);
   700   }
   702   nsCOMPtr<nsISelectionController> selCont;
   703   nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont)); 
   704   NS_ENSURE_SUCCESS(rv, rv);
   705   NS_ENSURE_TRUE(selCont, NS_ERROR_FAILURE);
   707   // complete scroll commands
   708   if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop"))
   709     return selCont->CompleteScroll(false);
   710   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom"))
   711     return selCont->CompleteScroll(true);
   713   // complete move commands
   714   else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop"))
   715     return selCont->CompleteMove(false, false);
   716   else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom"))
   717     return selCont->CompleteMove(true, false);
   718   else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop"))
   719     return selCont->CompleteMove(false, true);
   720   else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom"))
   721     return selCont->CompleteMove(true, true);
   723   // line move commands
   724   else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext"))
   725     return selCont->LineMove(true, false);
   726   else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious"))
   727     return selCont->LineMove(false, false);
   728   else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext"))
   729     return selCont->LineMove(true, true);
   730   else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious"))
   731     return selCont->LineMove(false, true);
   733   // character move commands
   734   else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious"))
   735     return selCont->CharacterMove(false, false);
   736   else if (!nsCRT::strcmp(aCommandName,"cmd_charNext"))
   737     return selCont->CharacterMove(true, false);
   738   else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious"))
   739     return selCont->CharacterMove(false, true);
   740   else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext"))
   741     return selCont->CharacterMove(true, true);
   743   // intra line move commands
   744   else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine"))
   745     return selCont->IntraLineMove(false, false);
   746   else if (!nsCRT::strcmp(aCommandName,"cmd_endLine"))
   747     return selCont->IntraLineMove(true, false);
   748   else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine"))
   749     return selCont->IntraLineMove(false, true);
   750   else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine"))
   751     return selCont->IntraLineMove(true, true);
   753   // word move commands
   754   else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious"))
   755     return selCont->WordMove(false, false);
   756   else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext"))
   757     return selCont->WordMove(true, false);
   758   else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious"))
   759     return selCont->WordMove(false, true);
   760   else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext"))
   761     return selCont->WordMove(true, true);
   763   // scroll page commands
   764   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp"))
   765     return selCont->ScrollPage(false);
   766   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown"))
   767     return selCont->ScrollPage(true);
   769   // scroll line commands
   770   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp"))
   771     return selCont->ScrollLine(false);
   772   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown"))
   773     return selCont->ScrollLine(true);
   775   // page move commands
   776   else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp"))
   777     return selCont->PageMove(false, false);
   778   else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown"))
   779     return selCont->PageMove(true, false);
   780   else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp"))
   781     return selCont->PageMove(false, true);
   782   else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown"))
   783     return selCont->PageMove(true, true);
   785   return NS_ERROR_FAILURE;
   786 }
   788 NS_IMETHODIMP 
   789 nsSelectionMoveCommands::DoCommandParams(const char *aCommandName,
   790                                          nsICommandParams *aParams,
   791                                          nsISupports *aCommandRefCon)
   792 {
   793   return DoCommand(aCommandName, aCommandRefCon);
   794 }
   796 NS_IMETHODIMP 
   797 nsSelectionMoveCommands::GetCommandStateParams(const char *aCommandName,
   798                                                nsICommandParams *aParams,
   799                                                nsISupports *aCommandRefCon)
   800 {
   801   bool canUndo;
   802   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
   803   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
   804 }
   807 NS_IMETHODIMP
   808 nsInsertPlaintextCommand::IsCommandEnabled(const char * aCommandName,
   809                                            nsISupports *refCon, 
   810                                            bool *outCmdEnabled)
   811 {
   812   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   813   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   814   if (editor)
   815     return editor->GetIsSelectionEditable(outCmdEnabled);
   817   *outCmdEnabled = false;
   818   return NS_ERROR_NOT_IMPLEMENTED;
   819 }
   822 NS_IMETHODIMP
   823 nsInsertPlaintextCommand::DoCommand(const char *aCommandName,
   824                                     nsISupports *refCon)
   825 {
   826   return NS_ERROR_NOT_IMPLEMENTED;
   827 }
   829 NS_IMETHODIMP
   830 nsInsertPlaintextCommand::DoCommandParams(const char *aCommandName,
   831                                           nsICommandParams *aParams,
   832                                           nsISupports *refCon)
   833 {
   834   NS_ENSURE_ARG_POINTER(aParams);
   836   nsCOMPtr<nsIPlaintextEditor> editor = do_QueryInterface(refCon);
   837   NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
   839   // Get text to insert from command params
   840   nsAutoString text;
   841   nsresult rv = aParams->GetStringValue(STATE_DATA, text);
   842   NS_ENSURE_SUCCESS(rv, rv);
   844   if (!text.IsEmpty())
   845     return editor->InsertText(text);
   847   return NS_OK;
   848 }
   850 NS_IMETHODIMP
   851 nsInsertPlaintextCommand::GetCommandStateParams(const char *aCommandName,
   852                                                 nsICommandParams *aParams,
   853                                                 nsISupports *refCon)
   854 {
   855   NS_ENSURE_ARG_POINTER(aParams);
   857   bool outCmdEnabled = false;
   858   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
   859   return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
   860 }
   863 NS_IMETHODIMP
   864 nsPasteQuotationCommand::IsCommandEnabled(const char * aCommandName,
   865                                           nsISupports *refCon,
   866                                           bool *outCmdEnabled)
   867 {
   868   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   870   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   871   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
   872   if (editor && mailEditor) {
   873     uint32_t flags;
   874     editor->GetFlags(&flags);
   875     if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask))
   876       return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
   877   }
   879   *outCmdEnabled = false;
   880   return NS_OK;
   881 }
   884 NS_IMETHODIMP
   885 nsPasteQuotationCommand::DoCommand(const char *aCommandName,
   886                                    nsISupports *refCon)
   887 {
   888   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
   889   if (mailEditor)
   890     return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
   892   return NS_ERROR_NOT_IMPLEMENTED;
   893 }
   895 NS_IMETHODIMP
   896 nsPasteQuotationCommand::DoCommandParams(const char *aCommandName,
   897                                          nsICommandParams *aParams,
   898                                          nsISupports *refCon)
   899 {
   900   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
   901   if (mailEditor)
   902     return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
   904   return NS_ERROR_NOT_IMPLEMENTED;
   905 }
   907 NS_IMETHODIMP
   908 nsPasteQuotationCommand::GetCommandStateParams(const char *aCommandName,
   909                                                nsICommandParams *aParams,
   910                                                nsISupports *refCon)
   911 {
   912   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
   913   if (editor)
   914   {
   915     bool enabled = false;
   916     editor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled);
   917     aParams->SetBooleanValue(STATE_ENABLED, enabled);
   918   }
   920   return NS_OK;
   921 }

mercurial