Back to index

lightning-sunbird  0.9+nobinonly
nsEditorCommands.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Kathleen Brade <brade@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nsCRT.h"
00041 #include "nsString.h"
00042 
00043 #include "nsIEditor.h"
00044 #include "nsIPlaintextEditor.h"
00045 #include "nsIEditorMailSupport.h"
00046 #include "nsISelectionController.h"
00047 #include "nsIClipboard.h"
00048 
00049 #include "nsEditorCommands.h"
00050 
00051 
00052 #define STATE_ENABLED  "state_enabled"
00053 #define STATE_DATA "state_data"
00054 
00055 
00056 nsBaseEditorCommand::nsBaseEditorCommand()
00057 {
00058 }
00059 
00060 NS_IMPL_ISUPPORTS1(nsBaseEditorCommand, nsIControllerCommand)
00061 
00062 #ifdef XP_MAC
00063 #pragma mark -
00064 #endif
00065 
00066 
00067 NS_IMETHODIMP
00068 nsUndoCommand::IsCommandEnabled(const char * aCommandName, 
00069                                 nsISupports *aCommandRefCon, 
00070                                 PRBool *outCmdEnabled)
00071 {
00072   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00073   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00074   if (editor)
00075   {
00076     PRBool isEnabled;
00077     return editor->CanUndo(&isEnabled, outCmdEnabled);
00078   }
00079 
00080   *outCmdEnabled = PR_FALSE;
00081   return NS_OK;
00082 }
00083 
00084 
00085 NS_IMETHODIMP
00086 nsUndoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00087 {
00088   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00089   if (editor)
00090     return editor->Undo(1);
00091     
00092   return NS_ERROR_FAILURE;
00093 }
00094 
00095 NS_IMETHODIMP 
00096 nsUndoCommand::DoCommandParams(const char *aCommandName,
00097                                nsICommandParams *aParams,
00098                                nsISupports *aCommandRefCon)
00099 {
00100   return DoCommand(aCommandName, aCommandRefCon);
00101 }
00102 
00103 NS_IMETHODIMP 
00104 nsUndoCommand::GetCommandStateParams(const char *aCommandName,
00105                                      nsICommandParams *aParams,
00106                                      nsISupports *aCommandRefCon)
00107 {
00108   PRBool canUndo;
00109   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00110   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00111 }
00112 
00113 NS_IMETHODIMP
00114 nsRedoCommand::IsCommandEnabled(const char * aCommandName,
00115                                 nsISupports *aCommandRefCon,
00116                                 PRBool *outCmdEnabled)
00117 {
00118   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00119   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00120   if (editor)
00121   {
00122     PRBool isEnabled;
00123     return editor->CanRedo(&isEnabled, outCmdEnabled);
00124   }
00125 
00126   *outCmdEnabled = PR_FALSE;
00127   return NS_OK;
00128 }
00129 
00130 
00131 NS_IMETHODIMP
00132 nsRedoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00133 {
00134   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00135   if (editor)
00136     return editor->Redo(1);
00137     
00138   return NS_ERROR_FAILURE;
00139 }
00140 
00141 NS_IMETHODIMP 
00142 nsRedoCommand::DoCommandParams(const char *aCommandName,
00143                                nsICommandParams *aParams,
00144                                nsISupports *aCommandRefCon)
00145 {
00146   return DoCommand(aCommandName, aCommandRefCon);
00147 }
00148 
00149 NS_IMETHODIMP 
00150 nsRedoCommand::GetCommandStateParams(const char *aCommandName,
00151                                      nsICommandParams *aParams,
00152                                      nsISupports *aCommandRefCon)
00153 {
00154   PRBool canUndo;
00155   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00156   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00157 }
00158 
00159 NS_IMETHODIMP
00160 nsClearUndoCommand::IsCommandEnabled(const char * aCommandName,
00161                                      nsISupports *refCon, PRBool *outCmdEnabled)
00162 { 
00163   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00164   
00165   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
00166   *outCmdEnabled = (editor != nsnull);
00167   return NS_OK;
00168 }
00169   
00170 
00171 NS_IMETHODIMP
00172 nsClearUndoCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
00173 { 
00174   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
00175   if (!editor) return NS_ERROR_NOT_IMPLEMENTED;
00176   
00177   editor->EnableUndo(PR_FALSE);  // Turning off undo clears undo/redo stacks.
00178   editor->EnableUndo(PR_TRUE);   // This re-enables undo/redo.
00179   
00180   return NS_OK;
00181 }
00182                                   
00183 NS_IMETHODIMP                       
00184 nsClearUndoCommand::DoCommandParams(const char *aCommandName,
00185                                     nsICommandParams *aParams,
00186                                     nsISupports *refCon)
00187 {
00188   return DoCommand(aCommandName, refCon);
00189 }
00190                                                   
00191 NS_IMETHODIMP                                     
00192 nsClearUndoCommand::GetCommandStateParams(const char *aCommandName,
00193                                           nsICommandParams *aParams,
00194                                           nsISupports *refCon)
00195 { 
00196   NS_ENSURE_ARG_POINTER(aParams);
00197   
00198   PRBool enabled;
00199   nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
00200   NS_ENSURE_SUCCESS(rv, rv);
00201    
00202   return aParams->SetBooleanValue(STATE_ENABLED, enabled);
00203 }
00204 
00205 NS_IMETHODIMP
00206 nsCutCommand::IsCommandEnabled(const char * aCommandName,
00207                                nsISupports *aCommandRefCon,
00208                                PRBool *outCmdEnabled)
00209 {
00210   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00211   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00212   if (editor)
00213     return editor->CanCut(outCmdEnabled);
00214 
00215   *outCmdEnabled = PR_FALSE;
00216   return NS_OK;
00217 }
00218 
00219 
00220 NS_IMETHODIMP
00221 nsCutCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00222 {
00223   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00224   if (editor)
00225     return editor->Cut();
00226     
00227   return NS_ERROR_FAILURE;
00228 }
00229 
00230 NS_IMETHODIMP 
00231 nsCutCommand::DoCommandParams(const char *aCommandName,
00232                               nsICommandParams *aParams,
00233                               nsISupports *aCommandRefCon)
00234 {
00235   return DoCommand(aCommandName, aCommandRefCon);
00236 }
00237 
00238 NS_IMETHODIMP 
00239 nsCutCommand::GetCommandStateParams(const char *aCommandName,
00240                                     nsICommandParams *aParams,
00241                                     nsISupports *aCommandRefCon)
00242 {
00243   PRBool canUndo;
00244   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00245   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00246 }
00247 
00248 
00249 NS_IMETHODIMP
00250 nsCutOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
00251                                        nsISupports *aCommandRefCon,
00252                                        PRBool *outCmdEnabled)
00253 {
00254   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00255   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00256   *outCmdEnabled = (editor != nsnull);
00257   return NS_OK;
00258 }
00259 
00260 
00261 NS_IMETHODIMP
00262 nsCutOrDeleteCommand::DoCommand(const char *aCommandName,
00263                                 nsISupports *aCommandRefCon)
00264 {
00265   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00266   if (editor)
00267   {
00268     nsCOMPtr<nsISelection> selection;
00269     nsresult rv = editor->GetSelection(getter_AddRefs(selection));
00270     if (NS_SUCCEEDED(rv) && selection)
00271     {
00272       PRBool isCollapsed;
00273       rv = selection->GetIsCollapsed(&isCollapsed);
00274       if (NS_SUCCEEDED(rv) && isCollapsed)
00275         return editor->DeleteSelection(nsIEditor::eNext);
00276     }
00277     return editor->Cut();
00278   }
00279     
00280   return NS_ERROR_FAILURE;
00281 }
00282 
00283 NS_IMETHODIMP 
00284 nsCutOrDeleteCommand::DoCommandParams(const char *aCommandName,
00285                                       nsICommandParams *aParams,
00286                                       nsISupports *aCommandRefCon)
00287 {
00288   return DoCommand(aCommandName, aCommandRefCon);
00289 }
00290 
00291 NS_IMETHODIMP 
00292 nsCutOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
00293                                             nsICommandParams *aParams,
00294                                             nsISupports *aCommandRefCon)
00295 {
00296   PRBool canUndo;
00297   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00298   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00299 }
00300 
00301 NS_IMETHODIMP
00302 nsCopyCommand::IsCommandEnabled(const char * aCommandName,
00303                                 nsISupports *aCommandRefCon,
00304                                 PRBool *outCmdEnabled)
00305 {
00306   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00307   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00308   if (editor)
00309     return editor->CanCopy(outCmdEnabled);
00310 
00311   *outCmdEnabled = PR_FALSE;
00312   return NS_OK;
00313 }
00314 
00315 
00316 NS_IMETHODIMP
00317 nsCopyCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00318 {
00319   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00320   if (editor)
00321     return editor->Copy();
00322     
00323   return NS_ERROR_FAILURE;
00324 }
00325 
00326 NS_IMETHODIMP 
00327 nsCopyCommand::DoCommandParams(const char *aCommandName,
00328                                nsICommandParams *aParams,
00329                                nsISupports *aCommandRefCon)
00330 {
00331   return DoCommand(aCommandName, aCommandRefCon);
00332 }
00333 
00334 NS_IMETHODIMP 
00335 nsCopyCommand::GetCommandStateParams(const char *aCommandName,
00336                                      nsICommandParams *aParams,
00337                                      nsISupports *aCommandRefCon)
00338 {
00339   PRBool canUndo;
00340   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00341   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00342 }
00343 
00344 NS_IMETHODIMP
00345 nsCopyOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
00346                                         nsISupports *aCommandRefCon,
00347                                         PRBool *outCmdEnabled)
00348 {
00349   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00350   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00351   *outCmdEnabled = (editor != nsnull);
00352   return NS_OK;
00353 }
00354 
00355 
00356 NS_IMETHODIMP
00357 nsCopyOrDeleteCommand::DoCommand(const char *aCommandName,
00358                                  nsISupports *aCommandRefCon)
00359 {
00360   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00361   if (editor)
00362   {
00363     nsCOMPtr<nsISelection> selection;
00364     nsresult rv = editor->GetSelection(getter_AddRefs(selection));
00365     if (NS_SUCCEEDED(rv) && selection)
00366     {
00367       PRBool isCollapsed;
00368       rv = selection->GetIsCollapsed(&isCollapsed);
00369       if (NS_SUCCEEDED(rv) && isCollapsed)
00370         return editor->DeleteSelection(nsIEditor::eNextWord);
00371     }
00372     return editor->Copy();
00373   }
00374     
00375   return NS_ERROR_FAILURE;
00376 }
00377 
00378 NS_IMETHODIMP 
00379 nsCopyOrDeleteCommand::DoCommandParams(const char *aCommandName,
00380                                        nsICommandParams *aParams,
00381                                        nsISupports *aCommandRefCon)
00382 {
00383   return DoCommand(aCommandName, aCommandRefCon);
00384 }
00385 
00386 NS_IMETHODIMP 
00387 nsCopyOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
00388                                              nsICommandParams *aParams,
00389                                              nsISupports *aCommandRefCon)
00390 {
00391   PRBool canUndo;
00392   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00393   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00394 }
00395 
00396 NS_IMETHODIMP
00397 nsPasteCommand::IsCommandEnabled(const char *aCommandName,
00398                                  nsISupports *aCommandRefCon,
00399                                  PRBool *outCmdEnabled)
00400 {
00401   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00402   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00403   if (editor)
00404     return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
00405 
00406   *outCmdEnabled = PR_FALSE;
00407   return NS_OK;
00408 }
00409 
00410 
00411 NS_IMETHODIMP
00412 nsPasteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00413 {
00414   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00415   if (!editor)
00416     return NS_ERROR_FAILURE;
00417   
00418   return editor->Paste(nsIClipboard::kGlobalClipboard);
00419 }
00420 
00421 NS_IMETHODIMP 
00422 nsPasteCommand::DoCommandParams(const char *aCommandName,
00423                                 nsICommandParams *aParams,
00424                                 nsISupports *aCommandRefCon)
00425 {
00426   return DoCommand(aCommandName, aCommandRefCon);
00427 }
00428 
00429 NS_IMETHODIMP 
00430 nsPasteCommand::GetCommandStateParams(const char *aCommandName,
00431                                       nsICommandParams *aParams,
00432                                       nsISupports *aCommandRefCon)
00433 {
00434   PRBool canUndo;
00435   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00436   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00437 }
00438 
00439 NS_IMETHODIMP
00440 nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
00441                                  nsISupports *aCommandRefCon,
00442                                  PRBool *outCmdEnabled)
00443 {
00444   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00445   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00446   *outCmdEnabled = (editor != nsnull);
00447 
00448   return NS_OK;
00449 }
00450 
00451 NS_IMETHODIMP
00452 nsSwitchTextDirectionCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00453 {
00454   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00455   if (!editor)
00456     return NS_ERROR_FAILURE;
00457 
00458   return editor->SwitchTextDirection();
00459 }
00460 
00461 NS_IMETHODIMP 
00462 nsSwitchTextDirectionCommand::DoCommandParams(const char *aCommandName,
00463                                 nsICommandParams *aParams,
00464                                 nsISupports *aCommandRefCon)
00465 {
00466   return DoCommand(aCommandName, aCommandRefCon);
00467 }
00468 
00469 NS_IMETHODIMP 
00470 nsSwitchTextDirectionCommand::GetCommandStateParams(const char *aCommandName,
00471                                       nsICommandParams *aParams,
00472                                       nsISupports *aCommandRefCon)
00473 {
00474   PRBool canSwitchTextDirection = PR_TRUE;
00475   IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
00476   return aParams->SetBooleanValue(STATE_ENABLED, canSwitchTextDirection);
00477 }
00478 
00479 NS_IMETHODIMP
00480 nsDeleteCommand::IsCommandEnabled(const char * aCommandName,
00481                                   nsISupports *aCommandRefCon,
00482                                   PRBool *outCmdEnabled)
00483 {
00484   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00485   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00486   *outCmdEnabled = PR_FALSE;
00487   // we can delete when we can cut
00488   if (!editor)
00489     return NS_OK;
00490     
00491   if (!nsCRT::strcmp(aCommandName,"cmd_delete"))
00492     return editor->CanCut(outCmdEnabled);
00493   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharBackward"))
00494     *outCmdEnabled = PR_TRUE;
00495   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharForward"))
00496     *outCmdEnabled = PR_TRUE;
00497   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordBackward"))
00498     *outCmdEnabled = PR_TRUE;
00499   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordForward"))
00500     *outCmdEnabled = PR_TRUE;
00501   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToBeginningOfLine"))
00502     *outCmdEnabled = PR_TRUE;
00503   else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToEndOfLine"))
00504     *outCmdEnabled = PR_TRUE;  
00505 
00506   return NS_OK;
00507 }
00508 
00509 
00510 NS_IMETHODIMP
00511 nsDeleteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
00512 {
00513   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00514   if (!editor)
00515     return NS_ERROR_FAILURE;
00516     
00517   nsIEditor::EDirection deleteDir = nsIEditor::eNone;
00518   
00519   if (!nsCRT::strcmp("cmd_delete",aCommandName))
00520     deleteDir = nsIEditor::ePrevious;
00521   else if (!nsCRT::strcmp("cmd_deleteCharBackward",aCommandName))
00522     deleteDir = nsIEditor::ePrevious;
00523   else if (!nsCRT::strcmp("cmd_deleteCharForward",aCommandName))
00524     deleteDir = nsIEditor::eNext;
00525   else if (!nsCRT::strcmp("cmd_deleteWordBackward",aCommandName))
00526     deleteDir = nsIEditor::ePreviousWord;
00527   else if (!nsCRT::strcmp("cmd_deleteWordForward",aCommandName))
00528     deleteDir = nsIEditor::eNextWord;
00529   else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine",aCommandName))
00530     deleteDir = nsIEditor::eToBeginningOfLine;
00531   else if (!nsCRT::strcmp("cmd_deleteToEndOfLine",aCommandName))
00532     deleteDir = nsIEditor::eToEndOfLine;
00533 
00534   return editor->DeleteSelection(deleteDir);
00535 }
00536 
00537 NS_IMETHODIMP 
00538 nsDeleteCommand::DoCommandParams(const char *aCommandName,
00539                                  nsICommandParams *aParams,
00540                                  nsISupports *aCommandRefCon)
00541 {
00542   return DoCommand(aCommandName, aCommandRefCon);
00543 }
00544 
00545 NS_IMETHODIMP 
00546 nsDeleteCommand::GetCommandStateParams(const char *aCommandName,
00547                                        nsICommandParams *aParams,
00548                                        nsISupports *aCommandRefCon)
00549 {
00550   PRBool canUndo;
00551   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00552   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00553 }
00554 
00555 NS_IMETHODIMP
00556 nsSelectAllCommand::IsCommandEnabled(const char * aCommandName,
00557                                      nsISupports *aCommandRefCon,
00558                                      PRBool *outCmdEnabled)
00559 {
00560   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00561 
00562   // you can select all if there is an editor (and potentially no contents)
00563   // some day we may want to change this
00564   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00565   *outCmdEnabled = (editor != nsnull);
00566 
00567   return NS_OK;
00568 }
00569 
00570 
00571 NS_IMETHODIMP
00572 nsSelectAllCommand::DoCommand(const char *aCommandName,
00573                               nsISupports *aCommandRefCon)
00574 {
00575   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00576   if (editor)
00577     return editor->SelectAll();
00578     
00579   return NS_ERROR_FAILURE;
00580 }
00581 
00582 NS_IMETHODIMP 
00583 nsSelectAllCommand::DoCommandParams(const char *aCommandName,
00584                                     nsICommandParams *aParams,
00585                                     nsISupports *aCommandRefCon)
00586 {
00587   return DoCommand(aCommandName, aCommandRefCon);
00588 }
00589 
00590 NS_IMETHODIMP 
00591 nsSelectAllCommand::GetCommandStateParams(const char *aCommandName,
00592                                           nsICommandParams *aParams,
00593                                           nsISupports *aCommandRefCon)
00594 {
00595   PRBool canUndo;
00596   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00597   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00598 }
00599 
00600 
00601 NS_IMETHODIMP
00602 nsSelectionMoveCommands::IsCommandEnabled(const char * aCommandName,
00603                                           nsISupports *aCommandRefCon,
00604                                           PRBool *outCmdEnabled)
00605 {
00606   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00607   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00608   *outCmdEnabled = (editor != nsnull);
00609   if (!editor)
00610     return NS_ERROR_FAILURE;
00611 
00612   return NS_OK;
00613 }
00614 
00615 
00616 NS_IMETHODIMP
00617 nsSelectionMoveCommands::DoCommand(const char *aCommandName,
00618                                    nsISupports *aCommandRefCon)
00619 {
00620   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
00621   if (!editor)
00622     return NS_ERROR_FAILURE;
00623 
00624   nsCOMPtr<nsISelectionController> selCont;
00625   nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont)); 
00626   if (NS_FAILED(rv))
00627     return rv;
00628   if (!selCont)
00629     return NS_ERROR_FAILURE;
00630 
00631   // complete scroll commands
00632   if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop"))
00633     return selCont->CompleteScroll(PR_FALSE);
00634   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom"))
00635     return selCont->CompleteScroll(PR_TRUE);
00636 
00637   // complete move commands
00638   else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop"))
00639     return selCont->CompleteMove(PR_FALSE, PR_FALSE);
00640   else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom"))
00641     return selCont->CompleteMove(PR_TRUE, PR_FALSE);
00642   else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop"))
00643     return selCont->CompleteMove(PR_FALSE, PR_TRUE);
00644   else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom"))
00645     return selCont->CompleteMove(PR_TRUE, PR_TRUE);
00646 
00647   // line move commands
00648   else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext"))
00649     return selCont->LineMove(PR_TRUE, PR_FALSE);
00650   else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious"))
00651     return selCont->LineMove(PR_FALSE, PR_FALSE);
00652   else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext"))
00653     return selCont->LineMove(PR_TRUE, PR_TRUE);
00654   else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious"))
00655     return selCont->LineMove(PR_FALSE, PR_TRUE);
00656 
00657   // character move commands
00658   else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious"))
00659     return selCont->CharacterMove(PR_FALSE, PR_FALSE);
00660   else if (!nsCRT::strcmp(aCommandName,"cmd_charNext"))
00661     return selCont->CharacterMove(PR_TRUE, PR_FALSE);
00662   else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious"))
00663     return selCont->CharacterMove(PR_FALSE, PR_TRUE);
00664   else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext"))
00665     return selCont->CharacterMove(PR_TRUE, PR_TRUE);
00666 
00667   // intra line move commands
00668   else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine"))
00669     return selCont->IntraLineMove(PR_FALSE, PR_FALSE);
00670   else if (!nsCRT::strcmp(aCommandName,"cmd_endLine"))
00671     return selCont->IntraLineMove(PR_TRUE, PR_FALSE);
00672   else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine"))
00673     return selCont->IntraLineMove(PR_FALSE, PR_TRUE);
00674   else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine"))
00675     return selCont->IntraLineMove(PR_TRUE, PR_TRUE);
00676   
00677   // word move commands
00678   else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious"))
00679     return selCont->WordMove(PR_FALSE, PR_FALSE);
00680   else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext"))
00681     return selCont->WordMove(PR_TRUE, PR_FALSE);
00682   else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious"))
00683     return selCont->WordMove(PR_FALSE, PR_TRUE);
00684   else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext"))
00685     return selCont->WordMove(PR_TRUE, PR_TRUE);
00686   
00687   // scroll page commands
00688   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp"))
00689     return selCont->ScrollPage(PR_FALSE);
00690   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown"))
00691     return selCont->ScrollPage(PR_TRUE);
00692   
00693   // scroll line commands
00694   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp"))
00695     return selCont->ScrollLine(PR_FALSE);
00696   else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown"))
00697     return selCont->ScrollLine(PR_TRUE);
00698   
00699   // page move commands
00700   else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp"))
00701     return selCont->PageMove(PR_FALSE, PR_FALSE);
00702   else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown"))
00703     return selCont->PageMove(PR_TRUE, PR_FALSE);
00704   else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp"))
00705     return selCont->PageMove(PR_FALSE, PR_TRUE);
00706   else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown"))
00707     return selCont->PageMove(PR_TRUE, PR_TRUE);
00708     
00709   return NS_ERROR_FAILURE;
00710 }
00711 
00712 NS_IMETHODIMP 
00713 nsSelectionMoveCommands::DoCommandParams(const char *aCommandName,
00714                                          nsICommandParams *aParams,
00715                                          nsISupports *aCommandRefCon)
00716 {
00717   return DoCommand(aCommandName, aCommandRefCon);
00718 }
00719 
00720 NS_IMETHODIMP 
00721 nsSelectionMoveCommands::GetCommandStateParams(const char *aCommandName,
00722                                                nsICommandParams *aParams,
00723                                                nsISupports *aCommandRefCon)
00724 {
00725   PRBool canUndo;
00726   IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
00727   return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
00728 }
00729 
00730 #ifdef XP_MAC
00731 #pragma mark -
00732 #endif
00733 
00734 NS_IMETHODIMP
00735 nsInsertPlaintextCommand::IsCommandEnabled(const char * aCommandName,
00736                                            nsISupports *refCon, 
00737                                            PRBool *outCmdEnabled)
00738 {
00739   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00740   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
00741   if (editor)
00742     return editor->GetIsDocumentEditable(outCmdEnabled);
00743 
00744   *outCmdEnabled = PR_FALSE;
00745   return NS_ERROR_NOT_IMPLEMENTED;
00746 }
00747 
00748 
00749 NS_IMETHODIMP
00750 nsInsertPlaintextCommand::DoCommand(const char *aCommandName,
00751                                     nsISupports *refCon)
00752 {
00753   return NS_ERROR_NOT_IMPLEMENTED;
00754 }
00755 
00756 NS_IMETHODIMP
00757 nsInsertPlaintextCommand::DoCommandParams(const char *aCommandName,
00758                                           nsICommandParams *aParams,
00759                                           nsISupports *refCon)
00760 {
00761   NS_ENSURE_ARG_POINTER(aParams);
00762 
00763   nsCOMPtr<nsIPlaintextEditor> editor = do_QueryInterface(refCon);
00764   if (!editor)
00765     return NS_ERROR_NOT_IMPLEMENTED;
00766 
00767   // Get text to insert from command params
00768   nsAutoString text;
00769   nsresult rv = aParams->GetStringValue(STATE_DATA, text);
00770   NS_ENSURE_SUCCESS(rv, rv);
00771 
00772   if (!text.IsEmpty())
00773     return editor->InsertText(text);
00774 
00775   return NS_OK;
00776 }
00777 
00778 NS_IMETHODIMP
00779 nsInsertPlaintextCommand::GetCommandStateParams(const char *aCommandName,
00780                                                 nsICommandParams *aParams,
00781                                                 nsISupports *refCon)
00782 {
00783   NS_ENSURE_ARG_POINTER(aParams);
00784 
00785   PRBool outCmdEnabled = PR_FALSE;
00786   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
00787   return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
00788 }
00789 
00790 
00791 #ifdef XP_MAC
00792 #pragma mark -
00793 #endif
00794 
00795 NS_IMETHODIMP
00796 nsPasteQuotationCommand::IsCommandEnabled(const char * aCommandName,
00797                                           nsISupports *refCon,
00798                                           PRBool *outCmdEnabled)
00799 {
00800   NS_ENSURE_ARG_POINTER(outCmdEnabled);
00801 
00802   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
00803   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
00804   if (editor && mailEditor) {
00805     PRUint32 flags;
00806     editor->GetFlags(&flags);
00807     if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask))
00808       return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
00809   }
00810 
00811   *outCmdEnabled = PR_FALSE;
00812   return NS_OK;
00813 }
00814 
00815 
00816 NS_IMETHODIMP
00817 nsPasteQuotationCommand::DoCommand(const char *aCommandName,
00818                                    nsISupports *refCon)
00819 {
00820   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
00821   if (mailEditor)
00822     return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
00823 
00824   return NS_ERROR_NOT_IMPLEMENTED;
00825 }
00826 
00827 NS_IMETHODIMP
00828 nsPasteQuotationCommand::DoCommandParams(const char *aCommandName,
00829                                          nsICommandParams *aParams,
00830                                          nsISupports *refCon)
00831 {
00832   nsCOMPtr<nsIEditorMailSupport>  mailEditor = do_QueryInterface(refCon);
00833   if (mailEditor)
00834     return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
00835 
00836   return NS_ERROR_NOT_IMPLEMENTED;
00837 }
00838 
00839 NS_IMETHODIMP
00840 nsPasteQuotationCommand::GetCommandStateParams(const char *aCommandName,
00841                                                nsICommandParams *aParams,
00842                                                nsISupports *refCon)
00843 {
00844   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
00845   if (editor)
00846   {
00847     PRBool enabled = PR_FALSE;
00848     editor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled);
00849     aParams->SetBooleanValue(STATE_ENABLED, enabled);
00850   }
00851  
00852   return NS_OK;
00853 }
00854