/editor/libeditor/base/nsEditorCommands.cpp

https://github.com/diogogmt/mozilla-central · C++ · 954 lines · 752 code · 151 blank · 51 comment · 129 complexity · 0b3bc483573aa24f0275f4a506d9a8dd MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla Communicator client code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Kathleen Brade <brade@netscape.com>
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or 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.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #include "nsCRT.h"
  39. #include "nsString.h"
  40. #include "nsIEditor.h"
  41. #include "nsIPlaintextEditor.h"
  42. #include "nsIEditorMailSupport.h"
  43. #include "nsISelectionController.h"
  44. #include "nsIClipboard.h"
  45. #include "nsEditorCommands.h"
  46. #include "nsIDocument.h"
  47. #define STATE_ENABLED "state_enabled"
  48. #define STATE_DATA "state_data"
  49. nsBaseEditorCommand::nsBaseEditorCommand()
  50. {
  51. }
  52. NS_IMPL_ISUPPORTS1(nsBaseEditorCommand, nsIControllerCommand)
  53. NS_IMETHODIMP
  54. nsUndoCommand::IsCommandEnabled(const char * aCommandName,
  55. nsISupports *aCommandRefCon,
  56. bool *outCmdEnabled)
  57. {
  58. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  59. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  60. if (editor)
  61. {
  62. bool isEnabled, isEditable = false;
  63. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  64. NS_ENSURE_SUCCESS(rv, rv);
  65. if (isEditable)
  66. return editor->CanUndo(&isEnabled, outCmdEnabled);
  67. }
  68. *outCmdEnabled = false;
  69. return NS_OK;
  70. }
  71. NS_IMETHODIMP
  72. nsUndoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  73. {
  74. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  75. if (editor)
  76. return editor->Undo(1);
  77. return NS_ERROR_FAILURE;
  78. }
  79. NS_IMETHODIMP
  80. nsUndoCommand::DoCommandParams(const char *aCommandName,
  81. nsICommandParams *aParams,
  82. nsISupports *aCommandRefCon)
  83. {
  84. return DoCommand(aCommandName, aCommandRefCon);
  85. }
  86. NS_IMETHODIMP
  87. nsUndoCommand::GetCommandStateParams(const char *aCommandName,
  88. nsICommandParams *aParams,
  89. nsISupports *aCommandRefCon)
  90. {
  91. bool canUndo;
  92. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  93. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  94. }
  95. NS_IMETHODIMP
  96. nsRedoCommand::IsCommandEnabled(const char * aCommandName,
  97. nsISupports *aCommandRefCon,
  98. bool *outCmdEnabled)
  99. {
  100. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  101. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  102. if (editor)
  103. {
  104. bool isEnabled, isEditable = false;
  105. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  106. NS_ENSURE_SUCCESS(rv, rv);
  107. if (isEditable)
  108. return editor->CanRedo(&isEnabled, outCmdEnabled);
  109. }
  110. *outCmdEnabled = false;
  111. return NS_OK;
  112. }
  113. NS_IMETHODIMP
  114. nsRedoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  115. {
  116. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  117. if (editor)
  118. return editor->Redo(1);
  119. return NS_ERROR_FAILURE;
  120. }
  121. NS_IMETHODIMP
  122. nsRedoCommand::DoCommandParams(const char *aCommandName,
  123. nsICommandParams *aParams,
  124. nsISupports *aCommandRefCon)
  125. {
  126. return DoCommand(aCommandName, aCommandRefCon);
  127. }
  128. NS_IMETHODIMP
  129. nsRedoCommand::GetCommandStateParams(const char *aCommandName,
  130. nsICommandParams *aParams,
  131. nsISupports *aCommandRefCon)
  132. {
  133. bool canUndo;
  134. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  135. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  136. }
  137. NS_IMETHODIMP
  138. nsClearUndoCommand::IsCommandEnabled(const char * aCommandName,
  139. nsISupports *refCon, bool *outCmdEnabled)
  140. {
  141. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  142. nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
  143. if (editor)
  144. return editor->GetIsSelectionEditable(outCmdEnabled);
  145. *outCmdEnabled = false;
  146. return NS_OK;
  147. }
  148. NS_IMETHODIMP
  149. nsClearUndoCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
  150. {
  151. nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
  152. NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
  153. editor->EnableUndo(false); // Turning off undo clears undo/redo stacks.
  154. editor->EnableUndo(true); // This re-enables undo/redo.
  155. return NS_OK;
  156. }
  157. NS_IMETHODIMP
  158. nsClearUndoCommand::DoCommandParams(const char *aCommandName,
  159. nsICommandParams *aParams,
  160. nsISupports *refCon)
  161. {
  162. return DoCommand(aCommandName, refCon);
  163. }
  164. NS_IMETHODIMP
  165. nsClearUndoCommand::GetCommandStateParams(const char *aCommandName,
  166. nsICommandParams *aParams,
  167. nsISupports *refCon)
  168. {
  169. NS_ENSURE_ARG_POINTER(aParams);
  170. bool enabled;
  171. nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
  172. NS_ENSURE_SUCCESS(rv, rv);
  173. return aParams->SetBooleanValue(STATE_ENABLED, enabled);
  174. }
  175. NS_IMETHODIMP
  176. nsCutCommand::IsCommandEnabled(const char * aCommandName,
  177. nsISupports *aCommandRefCon,
  178. bool *outCmdEnabled)
  179. {
  180. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  181. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  182. if (editor)
  183. {
  184. bool isEditable = false;
  185. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  186. NS_ENSURE_SUCCESS(rv, rv);
  187. if (isEditable)
  188. return editor->CanCut(outCmdEnabled);
  189. }
  190. *outCmdEnabled = false;
  191. return NS_OK;
  192. }
  193. NS_IMETHODIMP
  194. nsCutCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  195. {
  196. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  197. if (editor)
  198. return editor->Cut();
  199. return NS_ERROR_FAILURE;
  200. }
  201. NS_IMETHODIMP
  202. nsCutCommand::DoCommandParams(const char *aCommandName,
  203. nsICommandParams *aParams,
  204. nsISupports *aCommandRefCon)
  205. {
  206. return DoCommand(aCommandName, aCommandRefCon);
  207. }
  208. NS_IMETHODIMP
  209. nsCutCommand::GetCommandStateParams(const char *aCommandName,
  210. nsICommandParams *aParams,
  211. nsISupports *aCommandRefCon)
  212. {
  213. bool canUndo;
  214. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  215. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  216. }
  217. NS_IMETHODIMP
  218. nsCutOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
  219. nsISupports *aCommandRefCon,
  220. bool *outCmdEnabled)
  221. {
  222. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  223. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  224. if (editor)
  225. return editor->GetIsSelectionEditable(outCmdEnabled);
  226. *outCmdEnabled = false;
  227. return NS_OK;
  228. }
  229. NS_IMETHODIMP
  230. nsCutOrDeleteCommand::DoCommand(const char *aCommandName,
  231. nsISupports *aCommandRefCon)
  232. {
  233. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  234. if (editor)
  235. {
  236. nsCOMPtr<nsISelection> selection;
  237. nsresult rv = editor->GetSelection(getter_AddRefs(selection));
  238. if (NS_SUCCEEDED(rv) && selection)
  239. {
  240. bool isCollapsed;
  241. rv = selection->GetIsCollapsed(&isCollapsed);
  242. if (NS_SUCCEEDED(rv) && isCollapsed)
  243. return editor->DeleteSelection(nsIEditor::eNext);
  244. }
  245. return editor->Cut();
  246. }
  247. return NS_ERROR_FAILURE;
  248. }
  249. NS_IMETHODIMP
  250. nsCutOrDeleteCommand::DoCommandParams(const char *aCommandName,
  251. nsICommandParams *aParams,
  252. nsISupports *aCommandRefCon)
  253. {
  254. return DoCommand(aCommandName, aCommandRefCon);
  255. }
  256. NS_IMETHODIMP
  257. nsCutOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
  258. nsICommandParams *aParams,
  259. nsISupports *aCommandRefCon)
  260. {
  261. bool canUndo;
  262. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  263. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  264. }
  265. NS_IMETHODIMP
  266. nsCopyCommand::IsCommandEnabled(const char * aCommandName,
  267. nsISupports *aCommandRefCon,
  268. bool *outCmdEnabled)
  269. {
  270. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  271. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  272. if (editor)
  273. return editor->CanCopy(outCmdEnabled);
  274. *outCmdEnabled = false;
  275. return NS_OK;
  276. }
  277. NS_IMETHODIMP
  278. nsCopyCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  279. {
  280. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  281. if (editor)
  282. return editor->Copy();
  283. return NS_ERROR_FAILURE;
  284. }
  285. NS_IMETHODIMP
  286. nsCopyCommand::DoCommandParams(const char *aCommandName,
  287. nsICommandParams *aParams,
  288. nsISupports *aCommandRefCon)
  289. {
  290. return DoCommand(aCommandName, aCommandRefCon);
  291. }
  292. NS_IMETHODIMP
  293. nsCopyCommand::GetCommandStateParams(const char *aCommandName,
  294. nsICommandParams *aParams,
  295. nsISupports *aCommandRefCon)
  296. {
  297. bool canUndo;
  298. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  299. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  300. }
  301. NS_IMETHODIMP
  302. nsCopyOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
  303. nsISupports *aCommandRefCon,
  304. bool *outCmdEnabled)
  305. {
  306. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  307. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  308. if (editor)
  309. return editor->GetIsSelectionEditable(outCmdEnabled);
  310. *outCmdEnabled = false;
  311. return NS_OK;
  312. }
  313. NS_IMETHODIMP
  314. nsCopyOrDeleteCommand::DoCommand(const char *aCommandName,
  315. nsISupports *aCommandRefCon)
  316. {
  317. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  318. if (editor)
  319. {
  320. nsCOMPtr<nsISelection> selection;
  321. nsresult rv = editor->GetSelection(getter_AddRefs(selection));
  322. if (NS_SUCCEEDED(rv) && selection)
  323. {
  324. bool isCollapsed;
  325. rv = selection->GetIsCollapsed(&isCollapsed);
  326. if (NS_SUCCEEDED(rv) && isCollapsed)
  327. return editor->DeleteSelection(nsIEditor::eNextWord);
  328. }
  329. return editor->Copy();
  330. }
  331. return NS_ERROR_FAILURE;
  332. }
  333. NS_IMETHODIMP
  334. nsCopyOrDeleteCommand::DoCommandParams(const char *aCommandName,
  335. nsICommandParams *aParams,
  336. nsISupports *aCommandRefCon)
  337. {
  338. return DoCommand(aCommandName, aCommandRefCon);
  339. }
  340. NS_IMETHODIMP
  341. nsCopyOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
  342. nsICommandParams *aParams,
  343. nsISupports *aCommandRefCon)
  344. {
  345. bool canUndo;
  346. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  347. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  348. }
  349. NS_IMETHODIMP
  350. nsPasteCommand::IsCommandEnabled(const char *aCommandName,
  351. nsISupports *aCommandRefCon,
  352. bool *outCmdEnabled)
  353. {
  354. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  355. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  356. if (editor)
  357. {
  358. bool isEditable = false;
  359. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  360. NS_ENSURE_SUCCESS(rv, rv);
  361. if (isEditable)
  362. return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
  363. }
  364. *outCmdEnabled = false;
  365. return NS_OK;
  366. }
  367. NS_IMETHODIMP
  368. nsPasteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  369. {
  370. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  371. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  372. return editor->Paste(nsIClipboard::kGlobalClipboard);
  373. }
  374. NS_IMETHODIMP
  375. nsPasteCommand::DoCommandParams(const char *aCommandName,
  376. nsICommandParams *aParams,
  377. nsISupports *aCommandRefCon)
  378. {
  379. return DoCommand(aCommandName, aCommandRefCon);
  380. }
  381. NS_IMETHODIMP
  382. nsPasteCommand::GetCommandStateParams(const char *aCommandName,
  383. nsICommandParams *aParams,
  384. nsISupports *aCommandRefCon)
  385. {
  386. bool canUndo;
  387. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  388. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  389. }
  390. NS_IMETHODIMP
  391. nsPasteTransferableCommand::IsCommandEnabled(const char *aCommandName,
  392. nsISupports *aCommandRefCon,
  393. bool *outCmdEnabled)
  394. {
  395. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  396. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  397. if (editor)
  398. {
  399. bool isEditable = false;
  400. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  401. NS_ENSURE_SUCCESS(rv, rv);
  402. if (isEditable)
  403. return editor->CanPasteTransferable(nsnull, outCmdEnabled);
  404. }
  405. *outCmdEnabled = false;
  406. return NS_OK;
  407. }
  408. NS_IMETHODIMP
  409. nsPasteTransferableCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  410. {
  411. return NS_ERROR_FAILURE;
  412. }
  413. NS_IMETHODIMP
  414. nsPasteTransferableCommand::DoCommandParams(const char *aCommandName,
  415. nsICommandParams *aParams,
  416. nsISupports *aCommandRefCon)
  417. {
  418. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  419. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  420. nsCOMPtr<nsISupports> supports;
  421. aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
  422. NS_ENSURE_TRUE(supports, NS_ERROR_FAILURE);
  423. nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
  424. NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
  425. return editor->PasteTransferable(trans);
  426. }
  427. NS_IMETHODIMP
  428. nsPasteTransferableCommand::GetCommandStateParams(const char *aCommandName,
  429. nsICommandParams *aParams,
  430. nsISupports *aCommandRefCon)
  431. {
  432. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  433. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  434. nsCOMPtr<nsITransferable> trans;
  435. nsCOMPtr<nsISupports> supports;
  436. aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
  437. if (supports) {
  438. trans = do_QueryInterface(supports);
  439. NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
  440. }
  441. bool canPaste;
  442. nsresult rv = editor->CanPasteTransferable(trans, &canPaste);
  443. NS_ENSURE_SUCCESS(rv, rv);
  444. return aParams->SetBooleanValue(STATE_ENABLED, canPaste);
  445. }
  446. NS_IMETHODIMP
  447. nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
  448. nsISupports *aCommandRefCon,
  449. bool *outCmdEnabled)
  450. {
  451. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  452. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  453. if (editor)
  454. return editor->GetIsSelectionEditable(outCmdEnabled);
  455. *outCmdEnabled = false;
  456. return NS_OK;
  457. }
  458. NS_IMETHODIMP
  459. nsSwitchTextDirectionCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  460. {
  461. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  462. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  463. return editor->SwitchTextDirection();
  464. }
  465. NS_IMETHODIMP
  466. nsSwitchTextDirectionCommand::DoCommandParams(const char *aCommandName,
  467. nsICommandParams *aParams,
  468. nsISupports *aCommandRefCon)
  469. {
  470. return DoCommand(aCommandName, aCommandRefCon);
  471. }
  472. NS_IMETHODIMP
  473. nsSwitchTextDirectionCommand::GetCommandStateParams(const char *aCommandName,
  474. nsICommandParams *aParams,
  475. nsISupports *aCommandRefCon)
  476. {
  477. bool canSwitchTextDirection = true;
  478. IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
  479. return aParams->SetBooleanValue(STATE_ENABLED, canSwitchTextDirection);
  480. }
  481. NS_IMETHODIMP
  482. nsDeleteCommand::IsCommandEnabled(const char * aCommandName,
  483. nsISupports *aCommandRefCon,
  484. bool *outCmdEnabled)
  485. {
  486. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  487. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  488. *outCmdEnabled = false;
  489. // we can delete when we can cut
  490. NS_ENSURE_TRUE(editor, NS_OK);
  491. bool isEditable = false;
  492. nsresult rv = editor->GetIsSelectionEditable(&isEditable);
  493. NS_ENSURE_SUCCESS(rv, rv);
  494. if (!isEditable)
  495. return NS_OK;
  496. else if (!nsCRT::strcmp(aCommandName,"cmd_delete"))
  497. return editor->CanCut(outCmdEnabled);
  498. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharBackward"))
  499. *outCmdEnabled = true;
  500. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharForward"))
  501. *outCmdEnabled = true;
  502. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordBackward"))
  503. *outCmdEnabled = true;
  504. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordForward"))
  505. *outCmdEnabled = true;
  506. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToBeginningOfLine"))
  507. *outCmdEnabled = true;
  508. else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToEndOfLine"))
  509. *outCmdEnabled = true;
  510. return NS_OK;
  511. }
  512. NS_IMETHODIMP
  513. nsDeleteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
  514. {
  515. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  516. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  517. nsIEditor::EDirection deleteDir = nsIEditor::eNone;
  518. if (!nsCRT::strcmp("cmd_delete",aCommandName))
  519. deleteDir = nsIEditor::ePrevious;
  520. else if (!nsCRT::strcmp("cmd_deleteCharBackward",aCommandName))
  521. deleteDir = nsIEditor::ePrevious;
  522. else if (!nsCRT::strcmp("cmd_deleteCharForward",aCommandName))
  523. deleteDir = nsIEditor::eNext;
  524. else if (!nsCRT::strcmp("cmd_deleteWordBackward",aCommandName))
  525. deleteDir = nsIEditor::ePreviousWord;
  526. else if (!nsCRT::strcmp("cmd_deleteWordForward",aCommandName))
  527. deleteDir = nsIEditor::eNextWord;
  528. else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine",aCommandName))
  529. deleteDir = nsIEditor::eToBeginningOfLine;
  530. else if (!nsCRT::strcmp("cmd_deleteToEndOfLine",aCommandName))
  531. deleteDir = nsIEditor::eToEndOfLine;
  532. return editor->DeleteSelection(deleteDir);
  533. }
  534. NS_IMETHODIMP
  535. nsDeleteCommand::DoCommandParams(const char *aCommandName,
  536. nsICommandParams *aParams,
  537. nsISupports *aCommandRefCon)
  538. {
  539. return DoCommand(aCommandName, aCommandRefCon);
  540. }
  541. NS_IMETHODIMP
  542. nsDeleteCommand::GetCommandStateParams(const char *aCommandName,
  543. nsICommandParams *aParams,
  544. nsISupports *aCommandRefCon)
  545. {
  546. bool canUndo;
  547. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  548. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  549. }
  550. NS_IMETHODIMP
  551. nsSelectAllCommand::IsCommandEnabled(const char * aCommandName,
  552. nsISupports *aCommandRefCon,
  553. bool *outCmdEnabled)
  554. {
  555. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  556. nsresult rv = NS_OK;
  557. *outCmdEnabled = false;
  558. bool docIsEmpty;
  559. // you can select all if there is an editor which is non-empty
  560. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  561. if (editor) {
  562. rv = editor->GetDocumentIsEmpty(&docIsEmpty);
  563. NS_ENSURE_SUCCESS(rv, rv);
  564. *outCmdEnabled = !docIsEmpty;
  565. }
  566. return rv;
  567. }
  568. NS_IMETHODIMP
  569. nsSelectAllCommand::DoCommand(const char *aCommandName,
  570. nsISupports *aCommandRefCon)
  571. {
  572. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  573. if (editor)
  574. return editor->SelectAll();
  575. return NS_ERROR_FAILURE;
  576. }
  577. NS_IMETHODIMP
  578. nsSelectAllCommand::DoCommandParams(const char *aCommandName,
  579. nsICommandParams *aParams,
  580. nsISupports *aCommandRefCon)
  581. {
  582. return DoCommand(aCommandName, aCommandRefCon);
  583. }
  584. NS_IMETHODIMP
  585. nsSelectAllCommand::GetCommandStateParams(const char *aCommandName,
  586. nsICommandParams *aParams,
  587. nsISupports *aCommandRefCon)
  588. {
  589. bool canUndo;
  590. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  591. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  592. }
  593. NS_IMETHODIMP
  594. nsSelectionMoveCommands::IsCommandEnabled(const char * aCommandName,
  595. nsISupports *aCommandRefCon,
  596. bool *outCmdEnabled)
  597. {
  598. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  599. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  600. if (editor)
  601. return editor->GetIsSelectionEditable(outCmdEnabled);
  602. *outCmdEnabled = false;
  603. return NS_OK;
  604. }
  605. NS_IMETHODIMP
  606. nsSelectionMoveCommands::DoCommand(const char *aCommandName,
  607. nsISupports *aCommandRefCon)
  608. {
  609. nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
  610. NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
  611. nsCOMPtr<nsIDOMDocument> domDoc;
  612. editor->GetDocument(getter_AddRefs(domDoc));
  613. nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
  614. if (doc) {
  615. // Most of the commands below (possibly all of them) need layout to
  616. // be up to date.
  617. doc->FlushPendingNotifications(Flush_Layout);
  618. }
  619. nsCOMPtr<nsISelectionController> selCont;
  620. nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont));
  621. NS_ENSURE_SUCCESS(rv, rv);
  622. NS_ENSURE_TRUE(selCont, NS_ERROR_FAILURE);
  623. // complete scroll commands
  624. if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop"))
  625. return selCont->CompleteScroll(false);
  626. else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom"))
  627. return selCont->CompleteScroll(true);
  628. // complete move commands
  629. else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop"))
  630. return selCont->CompleteMove(false, false);
  631. else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom"))
  632. return selCont->CompleteMove(true, false);
  633. else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop"))
  634. return selCont->CompleteMove(false, true);
  635. else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom"))
  636. return selCont->CompleteMove(true, true);
  637. // line move commands
  638. else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext"))
  639. return selCont->LineMove(true, false);
  640. else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious"))
  641. return selCont->LineMove(false, false);
  642. else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext"))
  643. return selCont->LineMove(true, true);
  644. else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious"))
  645. return selCont->LineMove(false, true);
  646. // character move commands
  647. else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious"))
  648. return selCont->CharacterMove(false, false);
  649. else if (!nsCRT::strcmp(aCommandName,"cmd_charNext"))
  650. return selCont->CharacterMove(true, false);
  651. else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious"))
  652. return selCont->CharacterMove(false, true);
  653. else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext"))
  654. return selCont->CharacterMove(true, true);
  655. // intra line move commands
  656. else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine"))
  657. return selCont->IntraLineMove(false, false);
  658. else if (!nsCRT::strcmp(aCommandName,"cmd_endLine"))
  659. return selCont->IntraLineMove(true, false);
  660. else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine"))
  661. return selCont->IntraLineMove(false, true);
  662. else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine"))
  663. return selCont->IntraLineMove(true, true);
  664. // word move commands
  665. else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious"))
  666. return selCont->WordMove(false, false);
  667. else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext"))
  668. return selCont->WordMove(true, false);
  669. else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious"))
  670. return selCont->WordMove(false, true);
  671. else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext"))
  672. return selCont->WordMove(true, true);
  673. // scroll page commands
  674. else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp"))
  675. return selCont->ScrollPage(false);
  676. else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown"))
  677. return selCont->ScrollPage(true);
  678. // scroll line commands
  679. else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp"))
  680. return selCont->ScrollLine(false);
  681. else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown"))
  682. return selCont->ScrollLine(true);
  683. // page move commands
  684. else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp"))
  685. return selCont->PageMove(false, false);
  686. else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown"))
  687. return selCont->PageMove(true, false);
  688. else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp"))
  689. return selCont->PageMove(false, true);
  690. else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown"))
  691. return selCont->PageMove(true, true);
  692. return NS_ERROR_FAILURE;
  693. }
  694. NS_IMETHODIMP
  695. nsSelectionMoveCommands::DoCommandParams(const char *aCommandName,
  696. nsICommandParams *aParams,
  697. nsISupports *aCommandRefCon)
  698. {
  699. return DoCommand(aCommandName, aCommandRefCon);
  700. }
  701. NS_IMETHODIMP
  702. nsSelectionMoveCommands::GetCommandStateParams(const char *aCommandName,
  703. nsICommandParams *aParams,
  704. nsISupports *aCommandRefCon)
  705. {
  706. bool canUndo;
  707. IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
  708. return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
  709. }
  710. NS_IMETHODIMP
  711. nsInsertPlaintextCommand::IsCommandEnabled(const char * aCommandName,
  712. nsISupports *refCon,
  713. bool *outCmdEnabled)
  714. {
  715. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  716. nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
  717. if (editor)
  718. return editor->GetIsSelectionEditable(outCmdEnabled);
  719. *outCmdEnabled = false;
  720. return NS_ERROR_NOT_IMPLEMENTED;
  721. }
  722. NS_IMETHODIMP
  723. nsInsertPlaintextCommand::DoCommand(const char *aCommandName,
  724. nsISupports *refCon)
  725. {
  726. return NS_ERROR_NOT_IMPLEMENTED;
  727. }
  728. NS_IMETHODIMP
  729. nsInsertPlaintextCommand::DoCommandParams(const char *aCommandName,
  730. nsICommandParams *aParams,
  731. nsISupports *refCon)
  732. {
  733. NS_ENSURE_ARG_POINTER(aParams);
  734. nsCOMPtr<nsIPlaintextEditor> editor = do_QueryInterface(refCon);
  735. NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
  736. // Get text to insert from command params
  737. nsAutoString text;
  738. nsresult rv = aParams->GetStringValue(STATE_DATA, text);
  739. NS_ENSURE_SUCCESS(rv, rv);
  740. if (!text.IsEmpty())
  741. return editor->InsertText(text);
  742. return NS_OK;
  743. }
  744. NS_IMETHODIMP
  745. nsInsertPlaintextCommand::GetCommandStateParams(const char *aCommandName,
  746. nsICommandParams *aParams,
  747. nsISupports *refCon)
  748. {
  749. NS_ENSURE_ARG_POINTER(aParams);
  750. bool outCmdEnabled = false;
  751. IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
  752. return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
  753. }
  754. NS_IMETHODIMP
  755. nsPasteQuotationCommand::IsCommandEnabled(const char * aCommandName,
  756. nsISupports *refCon,
  757. bool *outCmdEnabled)
  758. {
  759. NS_ENSURE_ARG_POINTER(outCmdEnabled);
  760. nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
  761. nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
  762. if (editor && mailEditor) {
  763. PRUint32 flags;
  764. editor->GetFlags(&flags);
  765. if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask))
  766. return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
  767. }
  768. *outCmdEnabled = false;
  769. return NS_OK;
  770. }
  771. NS_IMETHODIMP
  772. nsPasteQuotationCommand::DoCommand(const char *aCommandName,
  773. nsISupports *refCon)
  774. {
  775. nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
  776. if (mailEditor)
  777. return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
  778. return NS_ERROR_NOT_IMPLEMENTED;
  779. }
  780. NS_IMETHODIMP
  781. nsPasteQuotationCommand::DoCommandParams(const char *aCommandName,
  782. nsICommandParams *aParams,
  783. nsISupports *refCon)
  784. {
  785. nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
  786. if (mailEditor)
  787. return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
  788. return NS_ERROR_NOT_IMPLEMENTED;
  789. }
  790. NS_IMETHODIMP
  791. nsPasteQuotationCommand::GetCommandStateParams(const char *aCommandName,
  792. nsICommandParams *aParams,
  793. nsISupports *refCon)
  794. {
  795. nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
  796. if (editor)
  797. {
  798. bool enabled = false;
  799. editor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled);
  800. aParams->SetBooleanValue(STATE_ENABLED, enabled);
  801. }
  802. return NS_OK;
  803. }