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 :
39 :
40 : #include "nsCRT.h"
41 : #include "nsString.h"
42 :
43 : #include "nsIEditor.h"
44 : #include "nsIPlaintextEditor.h"
45 : #include "nsIEditorMailSupport.h"
46 : #include "nsISelectionController.h"
47 : #include "nsIClipboard.h"
48 :
49 : #include "nsEditorCommands.h"
50 : #include "nsIDocument.h"
51 :
52 :
53 : #define STATE_ENABLED "state_enabled"
54 : #define STATE_DATA "state_data"
55 :
56 :
57 0 : nsBaseEditorCommand::nsBaseEditorCommand()
58 : {
59 0 : }
60 :
61 0 : NS_IMPL_ISUPPORTS1(nsBaseEditorCommand, nsIControllerCommand)
62 :
63 :
64 : NS_IMETHODIMP
65 0 : nsUndoCommand::IsCommandEnabled(const char * aCommandName,
66 : nsISupports *aCommandRefCon,
67 : bool *outCmdEnabled)
68 : {
69 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
70 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
71 0 : if (editor)
72 : {
73 0 : bool isEnabled, isEditable = false;
74 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
75 0 : NS_ENSURE_SUCCESS(rv, rv);
76 0 : if (isEditable)
77 0 : return editor->CanUndo(&isEnabled, outCmdEnabled);
78 : }
79 :
80 0 : *outCmdEnabled = false;
81 0 : return NS_OK;
82 : }
83 :
84 :
85 : NS_IMETHODIMP
86 0 : nsUndoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
87 : {
88 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
89 0 : if (editor)
90 0 : return editor->Undo(1);
91 :
92 0 : return NS_ERROR_FAILURE;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : nsUndoCommand::DoCommandParams(const char *aCommandName,
97 : nsICommandParams *aParams,
98 : nsISupports *aCommandRefCon)
99 : {
100 0 : return DoCommand(aCommandName, aCommandRefCon);
101 : }
102 :
103 : NS_IMETHODIMP
104 0 : nsUndoCommand::GetCommandStateParams(const char *aCommandName,
105 : nsICommandParams *aParams,
106 : nsISupports *aCommandRefCon)
107 : {
108 : bool canUndo;
109 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
110 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
111 : }
112 :
113 : NS_IMETHODIMP
114 0 : nsRedoCommand::IsCommandEnabled(const char * aCommandName,
115 : nsISupports *aCommandRefCon,
116 : bool *outCmdEnabled)
117 : {
118 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
119 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
120 0 : if (editor)
121 : {
122 0 : bool isEnabled, isEditable = false;
123 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
124 0 : NS_ENSURE_SUCCESS(rv, rv);
125 0 : if (isEditable)
126 0 : return editor->CanRedo(&isEnabled, outCmdEnabled);
127 : }
128 :
129 0 : *outCmdEnabled = false;
130 0 : return NS_OK;
131 : }
132 :
133 :
134 : NS_IMETHODIMP
135 0 : nsRedoCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
136 : {
137 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
138 0 : if (editor)
139 0 : return editor->Redo(1);
140 :
141 0 : return NS_ERROR_FAILURE;
142 : }
143 :
144 : NS_IMETHODIMP
145 0 : nsRedoCommand::DoCommandParams(const char *aCommandName,
146 : nsICommandParams *aParams,
147 : nsISupports *aCommandRefCon)
148 : {
149 0 : return DoCommand(aCommandName, aCommandRefCon);
150 : }
151 :
152 : NS_IMETHODIMP
153 0 : nsRedoCommand::GetCommandStateParams(const char *aCommandName,
154 : nsICommandParams *aParams,
155 : nsISupports *aCommandRefCon)
156 : {
157 : bool canUndo;
158 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
159 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
160 : }
161 :
162 : NS_IMETHODIMP
163 0 : nsClearUndoCommand::IsCommandEnabled(const char * aCommandName,
164 : nsISupports *refCon, bool *outCmdEnabled)
165 : {
166 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
167 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
168 0 : if (editor)
169 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
170 :
171 0 : *outCmdEnabled = false;
172 0 : return NS_OK;
173 : }
174 :
175 :
176 : NS_IMETHODIMP
177 0 : nsClearUndoCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
178 : {
179 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
180 0 : NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
181 :
182 0 : editor->EnableUndo(false); // Turning off undo clears undo/redo stacks.
183 0 : editor->EnableUndo(true); // This re-enables undo/redo.
184 :
185 0 : return NS_OK;
186 : }
187 :
188 : NS_IMETHODIMP
189 0 : nsClearUndoCommand::DoCommandParams(const char *aCommandName,
190 : nsICommandParams *aParams,
191 : nsISupports *refCon)
192 : {
193 0 : return DoCommand(aCommandName, refCon);
194 : }
195 :
196 : NS_IMETHODIMP
197 0 : nsClearUndoCommand::GetCommandStateParams(const char *aCommandName,
198 : nsICommandParams *aParams,
199 : nsISupports *refCon)
200 : {
201 0 : NS_ENSURE_ARG_POINTER(aParams);
202 :
203 : bool enabled;
204 0 : nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
205 0 : NS_ENSURE_SUCCESS(rv, rv);
206 :
207 0 : return aParams->SetBooleanValue(STATE_ENABLED, enabled);
208 : }
209 :
210 : NS_IMETHODIMP
211 0 : nsCutCommand::IsCommandEnabled(const char * aCommandName,
212 : nsISupports *aCommandRefCon,
213 : bool *outCmdEnabled)
214 : {
215 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
216 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
217 0 : if (editor)
218 : {
219 0 : bool isEditable = false;
220 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
221 0 : NS_ENSURE_SUCCESS(rv, rv);
222 0 : if (isEditable)
223 0 : return editor->CanCut(outCmdEnabled);
224 : }
225 :
226 0 : *outCmdEnabled = false;
227 0 : return NS_OK;
228 : }
229 :
230 :
231 : NS_IMETHODIMP
232 0 : nsCutCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
233 : {
234 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
235 0 : if (editor)
236 0 : return editor->Cut();
237 :
238 0 : return NS_ERROR_FAILURE;
239 : }
240 :
241 : NS_IMETHODIMP
242 0 : nsCutCommand::DoCommandParams(const char *aCommandName,
243 : nsICommandParams *aParams,
244 : nsISupports *aCommandRefCon)
245 : {
246 0 : return DoCommand(aCommandName, aCommandRefCon);
247 : }
248 :
249 : NS_IMETHODIMP
250 0 : nsCutCommand::GetCommandStateParams(const char *aCommandName,
251 : nsICommandParams *aParams,
252 : nsISupports *aCommandRefCon)
253 : {
254 : bool canUndo;
255 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
256 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
257 : }
258 :
259 :
260 : NS_IMETHODIMP
261 0 : nsCutOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
262 : nsISupports *aCommandRefCon,
263 : bool *outCmdEnabled)
264 : {
265 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
266 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
267 0 : if (editor)
268 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
269 :
270 0 : *outCmdEnabled = false;
271 0 : return NS_OK;
272 : }
273 :
274 :
275 : NS_IMETHODIMP
276 0 : nsCutOrDeleteCommand::DoCommand(const char *aCommandName,
277 : nsISupports *aCommandRefCon)
278 : {
279 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
280 0 : if (editor)
281 : {
282 0 : nsCOMPtr<nsISelection> selection;
283 0 : nsresult rv = editor->GetSelection(getter_AddRefs(selection));
284 0 : if (NS_SUCCEEDED(rv) && selection)
285 : {
286 : bool isCollapsed;
287 0 : rv = selection->GetIsCollapsed(&isCollapsed);
288 0 : if (NS_SUCCEEDED(rv) && isCollapsed)
289 0 : return editor->DeleteSelection(nsIEditor::eNext);
290 : }
291 0 : return editor->Cut();
292 : }
293 :
294 0 : return NS_ERROR_FAILURE;
295 : }
296 :
297 : NS_IMETHODIMP
298 0 : nsCutOrDeleteCommand::DoCommandParams(const char *aCommandName,
299 : nsICommandParams *aParams,
300 : nsISupports *aCommandRefCon)
301 : {
302 0 : return DoCommand(aCommandName, aCommandRefCon);
303 : }
304 :
305 : NS_IMETHODIMP
306 0 : nsCutOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
307 : nsICommandParams *aParams,
308 : nsISupports *aCommandRefCon)
309 : {
310 : bool canUndo;
311 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
312 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
313 : }
314 :
315 : NS_IMETHODIMP
316 0 : nsCopyCommand::IsCommandEnabled(const char * aCommandName,
317 : nsISupports *aCommandRefCon,
318 : bool *outCmdEnabled)
319 : {
320 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
321 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
322 0 : if (editor)
323 0 : return editor->CanCopy(outCmdEnabled);
324 :
325 0 : *outCmdEnabled = false;
326 0 : return NS_OK;
327 : }
328 :
329 :
330 : NS_IMETHODIMP
331 0 : nsCopyCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
332 : {
333 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
334 0 : if (editor)
335 0 : return editor->Copy();
336 :
337 0 : return NS_ERROR_FAILURE;
338 : }
339 :
340 : NS_IMETHODIMP
341 0 : nsCopyCommand::DoCommandParams(const char *aCommandName,
342 : nsICommandParams *aParams,
343 : nsISupports *aCommandRefCon)
344 : {
345 0 : return DoCommand(aCommandName, aCommandRefCon);
346 : }
347 :
348 : NS_IMETHODIMP
349 0 : nsCopyCommand::GetCommandStateParams(const char *aCommandName,
350 : nsICommandParams *aParams,
351 : nsISupports *aCommandRefCon)
352 : {
353 : bool canUndo;
354 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
355 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
356 : }
357 :
358 : NS_IMETHODIMP
359 0 : nsCopyOrDeleteCommand::IsCommandEnabled(const char * aCommandName,
360 : nsISupports *aCommandRefCon,
361 : bool *outCmdEnabled)
362 : {
363 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
364 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
365 0 : if (editor)
366 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
367 :
368 0 : *outCmdEnabled = false;
369 0 : return NS_OK;
370 : }
371 :
372 :
373 : NS_IMETHODIMP
374 0 : nsCopyOrDeleteCommand::DoCommand(const char *aCommandName,
375 : nsISupports *aCommandRefCon)
376 : {
377 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
378 0 : if (editor)
379 : {
380 0 : nsCOMPtr<nsISelection> selection;
381 0 : nsresult rv = editor->GetSelection(getter_AddRefs(selection));
382 0 : if (NS_SUCCEEDED(rv) && selection)
383 : {
384 : bool isCollapsed;
385 0 : rv = selection->GetIsCollapsed(&isCollapsed);
386 0 : if (NS_SUCCEEDED(rv) && isCollapsed)
387 0 : return editor->DeleteSelection(nsIEditor::eNextWord);
388 : }
389 0 : return editor->Copy();
390 : }
391 :
392 0 : return NS_ERROR_FAILURE;
393 : }
394 :
395 : NS_IMETHODIMP
396 0 : nsCopyOrDeleteCommand::DoCommandParams(const char *aCommandName,
397 : nsICommandParams *aParams,
398 : nsISupports *aCommandRefCon)
399 : {
400 0 : return DoCommand(aCommandName, aCommandRefCon);
401 : }
402 :
403 : NS_IMETHODIMP
404 0 : nsCopyOrDeleteCommand::GetCommandStateParams(const char *aCommandName,
405 : nsICommandParams *aParams,
406 : nsISupports *aCommandRefCon)
407 : {
408 : bool canUndo;
409 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
410 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
411 : }
412 :
413 : NS_IMETHODIMP
414 0 : nsPasteCommand::IsCommandEnabled(const char *aCommandName,
415 : nsISupports *aCommandRefCon,
416 : bool *outCmdEnabled)
417 : {
418 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
419 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
420 0 : if (editor)
421 : {
422 0 : bool isEditable = false;
423 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
424 0 : NS_ENSURE_SUCCESS(rv, rv);
425 0 : if (isEditable)
426 0 : return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
427 : }
428 :
429 0 : *outCmdEnabled = false;
430 0 : return NS_OK;
431 : }
432 :
433 :
434 : NS_IMETHODIMP
435 0 : nsPasteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
436 : {
437 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
438 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
439 :
440 0 : return editor->Paste(nsIClipboard::kGlobalClipboard);
441 : }
442 :
443 : NS_IMETHODIMP
444 0 : nsPasteCommand::DoCommandParams(const char *aCommandName,
445 : nsICommandParams *aParams,
446 : nsISupports *aCommandRefCon)
447 : {
448 0 : return DoCommand(aCommandName, aCommandRefCon);
449 : }
450 :
451 : NS_IMETHODIMP
452 0 : nsPasteCommand::GetCommandStateParams(const char *aCommandName,
453 : nsICommandParams *aParams,
454 : nsISupports *aCommandRefCon)
455 : {
456 : bool canUndo;
457 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
458 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
459 : }
460 :
461 : NS_IMETHODIMP
462 0 : nsPasteTransferableCommand::IsCommandEnabled(const char *aCommandName,
463 : nsISupports *aCommandRefCon,
464 : bool *outCmdEnabled)
465 : {
466 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
467 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
468 0 : if (editor)
469 : {
470 0 : bool isEditable = false;
471 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
472 0 : NS_ENSURE_SUCCESS(rv, rv);
473 0 : if (isEditable)
474 0 : return editor->CanPasteTransferable(nsnull, outCmdEnabled);
475 : }
476 :
477 0 : *outCmdEnabled = false;
478 0 : return NS_OK;
479 : }
480 :
481 : NS_IMETHODIMP
482 0 : nsPasteTransferableCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
483 : {
484 0 : return NS_ERROR_FAILURE;
485 : }
486 :
487 : NS_IMETHODIMP
488 0 : nsPasteTransferableCommand::DoCommandParams(const char *aCommandName,
489 : nsICommandParams *aParams,
490 : nsISupports *aCommandRefCon)
491 : {
492 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
493 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
494 :
495 0 : nsCOMPtr<nsISupports> supports;
496 0 : aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
497 0 : NS_ENSURE_TRUE(supports, NS_ERROR_FAILURE);
498 :
499 0 : nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
500 0 : NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
501 :
502 0 : return editor->PasteTransferable(trans);
503 : }
504 :
505 : NS_IMETHODIMP
506 0 : nsPasteTransferableCommand::GetCommandStateParams(const char *aCommandName,
507 : nsICommandParams *aParams,
508 : nsISupports *aCommandRefCon)
509 : {
510 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
511 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
512 :
513 0 : nsCOMPtr<nsITransferable> trans;
514 :
515 0 : nsCOMPtr<nsISupports> supports;
516 0 : aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
517 0 : if (supports) {
518 0 : trans = do_QueryInterface(supports);
519 0 : NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
520 : }
521 :
522 : bool canPaste;
523 0 : nsresult rv = editor->CanPasteTransferable(trans, &canPaste);
524 0 : NS_ENSURE_SUCCESS(rv, rv);
525 :
526 0 : return aParams->SetBooleanValue(STATE_ENABLED, canPaste);
527 : }
528 :
529 : NS_IMETHODIMP
530 0 : nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
531 : nsISupports *aCommandRefCon,
532 : bool *outCmdEnabled)
533 : {
534 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
535 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
536 0 : if (editor)
537 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
538 :
539 0 : *outCmdEnabled = false;
540 0 : return NS_OK;
541 : }
542 :
543 : NS_IMETHODIMP
544 0 : nsSwitchTextDirectionCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
545 : {
546 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
547 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
548 :
549 0 : return editor->SwitchTextDirection();
550 : }
551 :
552 : NS_IMETHODIMP
553 0 : nsSwitchTextDirectionCommand::DoCommandParams(const char *aCommandName,
554 : nsICommandParams *aParams,
555 : nsISupports *aCommandRefCon)
556 : {
557 0 : return DoCommand(aCommandName, aCommandRefCon);
558 : }
559 :
560 : NS_IMETHODIMP
561 0 : nsSwitchTextDirectionCommand::GetCommandStateParams(const char *aCommandName,
562 : nsICommandParams *aParams,
563 : nsISupports *aCommandRefCon)
564 : {
565 0 : bool canSwitchTextDirection = true;
566 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
567 0 : return aParams->SetBooleanValue(STATE_ENABLED, canSwitchTextDirection);
568 : }
569 :
570 : NS_IMETHODIMP
571 0 : nsDeleteCommand::IsCommandEnabled(const char * aCommandName,
572 : nsISupports *aCommandRefCon,
573 : bool *outCmdEnabled)
574 : {
575 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
576 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
577 0 : *outCmdEnabled = false;
578 :
579 : // we can delete when we can cut
580 0 : NS_ENSURE_TRUE(editor, NS_OK);
581 :
582 0 : bool isEditable = false;
583 0 : nsresult rv = editor->GetIsSelectionEditable(&isEditable);
584 0 : NS_ENSURE_SUCCESS(rv, rv);
585 :
586 0 : if (!isEditable)
587 0 : return NS_OK;
588 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_delete"))
589 0 : return editor->CanCut(outCmdEnabled);
590 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharBackward"))
591 0 : *outCmdEnabled = true;
592 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteCharForward"))
593 0 : *outCmdEnabled = true;
594 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordBackward"))
595 0 : *outCmdEnabled = true;
596 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteWordForward"))
597 0 : *outCmdEnabled = true;
598 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToBeginningOfLine"))
599 0 : *outCmdEnabled = true;
600 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_deleteToEndOfLine"))
601 0 : *outCmdEnabled = true;
602 :
603 0 : return NS_OK;
604 : }
605 :
606 :
607 : NS_IMETHODIMP
608 0 : nsDeleteCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
609 : {
610 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
611 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
612 :
613 0 : nsIEditor::EDirection deleteDir = nsIEditor::eNone;
614 :
615 0 : if (!nsCRT::strcmp("cmd_delete",aCommandName))
616 0 : deleteDir = nsIEditor::ePrevious;
617 0 : else if (!nsCRT::strcmp("cmd_deleteCharBackward",aCommandName))
618 0 : deleteDir = nsIEditor::ePrevious;
619 0 : else if (!nsCRT::strcmp("cmd_deleteCharForward",aCommandName))
620 0 : deleteDir = nsIEditor::eNext;
621 0 : else if (!nsCRT::strcmp("cmd_deleteWordBackward",aCommandName))
622 0 : deleteDir = nsIEditor::ePreviousWord;
623 0 : else if (!nsCRT::strcmp("cmd_deleteWordForward",aCommandName))
624 0 : deleteDir = nsIEditor::eNextWord;
625 0 : else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine",aCommandName))
626 0 : deleteDir = nsIEditor::eToBeginningOfLine;
627 0 : else if (!nsCRT::strcmp("cmd_deleteToEndOfLine",aCommandName))
628 0 : deleteDir = nsIEditor::eToEndOfLine;
629 :
630 0 : return editor->DeleteSelection(deleteDir);
631 : }
632 :
633 : NS_IMETHODIMP
634 0 : nsDeleteCommand::DoCommandParams(const char *aCommandName,
635 : nsICommandParams *aParams,
636 : nsISupports *aCommandRefCon)
637 : {
638 0 : return DoCommand(aCommandName, aCommandRefCon);
639 : }
640 :
641 : NS_IMETHODIMP
642 0 : nsDeleteCommand::GetCommandStateParams(const char *aCommandName,
643 : nsICommandParams *aParams,
644 : nsISupports *aCommandRefCon)
645 : {
646 : bool canUndo;
647 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
648 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
649 : }
650 :
651 : NS_IMETHODIMP
652 0 : nsSelectAllCommand::IsCommandEnabled(const char * aCommandName,
653 : nsISupports *aCommandRefCon,
654 : bool *outCmdEnabled)
655 : {
656 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
657 :
658 0 : nsresult rv = NS_OK;
659 0 : *outCmdEnabled = false;
660 : bool docIsEmpty;
661 :
662 : // you can select all if there is an editor which is non-empty
663 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
664 0 : if (editor) {
665 0 : rv = editor->GetDocumentIsEmpty(&docIsEmpty);
666 0 : NS_ENSURE_SUCCESS(rv, rv);
667 0 : *outCmdEnabled = !docIsEmpty;
668 : }
669 :
670 0 : return rv;
671 : }
672 :
673 :
674 : NS_IMETHODIMP
675 0 : nsSelectAllCommand::DoCommand(const char *aCommandName,
676 : nsISupports *aCommandRefCon)
677 : {
678 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
679 0 : if (editor)
680 0 : return editor->SelectAll();
681 :
682 0 : return NS_ERROR_FAILURE;
683 : }
684 :
685 : NS_IMETHODIMP
686 0 : nsSelectAllCommand::DoCommandParams(const char *aCommandName,
687 : nsICommandParams *aParams,
688 : nsISupports *aCommandRefCon)
689 : {
690 0 : return DoCommand(aCommandName, aCommandRefCon);
691 : }
692 :
693 : NS_IMETHODIMP
694 0 : nsSelectAllCommand::GetCommandStateParams(const char *aCommandName,
695 : nsICommandParams *aParams,
696 : nsISupports *aCommandRefCon)
697 : {
698 : bool canUndo;
699 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
700 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
701 : }
702 :
703 :
704 : NS_IMETHODIMP
705 0 : nsSelectionMoveCommands::IsCommandEnabled(const char * aCommandName,
706 : nsISupports *aCommandRefCon,
707 : bool *outCmdEnabled)
708 : {
709 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
710 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
711 0 : if (editor)
712 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
713 :
714 0 : *outCmdEnabled = false;
715 0 : return NS_OK;
716 : }
717 :
718 : NS_IMETHODIMP
719 0 : nsSelectionMoveCommands::DoCommand(const char *aCommandName,
720 : nsISupports *aCommandRefCon)
721 : {
722 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
723 0 : NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
724 :
725 0 : nsCOMPtr<nsIDOMDocument> domDoc;
726 0 : editor->GetDocument(getter_AddRefs(domDoc));
727 0 : nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
728 0 : if (doc) {
729 : // Most of the commands below (possibly all of them) need layout to
730 : // be up to date.
731 0 : doc->FlushPendingNotifications(Flush_Layout);
732 : }
733 :
734 0 : nsCOMPtr<nsISelectionController> selCont;
735 0 : nsresult rv = editor->GetSelectionController(getter_AddRefs(selCont));
736 0 : NS_ENSURE_SUCCESS(rv, rv);
737 0 : NS_ENSURE_TRUE(selCont, NS_ERROR_FAILURE);
738 :
739 : // complete scroll commands
740 0 : if (!nsCRT::strcmp(aCommandName,"cmd_scrollTop"))
741 0 : return selCont->CompleteScroll(false);
742 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_scrollBottom"))
743 0 : return selCont->CompleteScroll(true);
744 :
745 : // complete move commands
746 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_moveTop"))
747 0 : return selCont->CompleteMove(false, false);
748 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_moveBottom"))
749 0 : return selCont->CompleteMove(true, false);
750 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectTop"))
751 0 : return selCont->CompleteMove(false, true);
752 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectBottom"))
753 0 : return selCont->CompleteMove(true, true);
754 :
755 : // line move commands
756 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_lineNext"))
757 0 : return selCont->LineMove(true, false);
758 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_linePrevious"))
759 0 : return selCont->LineMove(false, false);
760 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectLineNext"))
761 0 : return selCont->LineMove(true, true);
762 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectLinePrevious"))
763 0 : return selCont->LineMove(false, true);
764 :
765 : // character move commands
766 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_charPrevious"))
767 0 : return selCont->CharacterMove(false, false);
768 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_charNext"))
769 0 : return selCont->CharacterMove(true, false);
770 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharPrevious"))
771 0 : return selCont->CharacterMove(false, true);
772 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectCharNext"))
773 0 : return selCont->CharacterMove(true, true);
774 :
775 : // intra line move commands
776 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_beginLine"))
777 0 : return selCont->IntraLineMove(false, false);
778 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_endLine"))
779 0 : return selCont->IntraLineMove(true, false);
780 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectBeginLine"))
781 0 : return selCont->IntraLineMove(false, true);
782 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectEndLine"))
783 0 : return selCont->IntraLineMove(true, true);
784 :
785 : // word move commands
786 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_wordPrevious"))
787 0 : return selCont->WordMove(false, false);
788 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_wordNext"))
789 0 : return selCont->WordMove(true, false);
790 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordPrevious"))
791 0 : return selCont->WordMove(false, true);
792 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectWordNext"))
793 0 : return selCont->WordMove(true, true);
794 :
795 : // scroll page commands
796 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageUp"))
797 0 : return selCont->ScrollPage(false);
798 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_scrollPageDown"))
799 0 : return selCont->ScrollPage(true);
800 :
801 : // scroll line commands
802 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineUp"))
803 0 : return selCont->ScrollLine(false);
804 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_scrollLineDown"))
805 0 : return selCont->ScrollLine(true);
806 :
807 : // page move commands
808 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_movePageUp"))
809 0 : return selCont->PageMove(false, false);
810 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_movePageDown"))
811 0 : return selCont->PageMove(true, false);
812 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageUp"))
813 0 : return selCont->PageMove(false, true);
814 0 : else if (!nsCRT::strcmp(aCommandName,"cmd_selectPageDown"))
815 0 : return selCont->PageMove(true, true);
816 :
817 0 : return NS_ERROR_FAILURE;
818 : }
819 :
820 : NS_IMETHODIMP
821 0 : nsSelectionMoveCommands::DoCommandParams(const char *aCommandName,
822 : nsICommandParams *aParams,
823 : nsISupports *aCommandRefCon)
824 : {
825 0 : return DoCommand(aCommandName, aCommandRefCon);
826 : }
827 :
828 : NS_IMETHODIMP
829 0 : nsSelectionMoveCommands::GetCommandStateParams(const char *aCommandName,
830 : nsICommandParams *aParams,
831 : nsISupports *aCommandRefCon)
832 : {
833 : bool canUndo;
834 0 : IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
835 0 : return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
836 : }
837 :
838 :
839 : NS_IMETHODIMP
840 0 : nsInsertPlaintextCommand::IsCommandEnabled(const char * aCommandName,
841 : nsISupports *refCon,
842 : bool *outCmdEnabled)
843 : {
844 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
845 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
846 0 : if (editor)
847 0 : return editor->GetIsSelectionEditable(outCmdEnabled);
848 :
849 0 : *outCmdEnabled = false;
850 0 : return NS_ERROR_NOT_IMPLEMENTED;
851 : }
852 :
853 :
854 : NS_IMETHODIMP
855 0 : nsInsertPlaintextCommand::DoCommand(const char *aCommandName,
856 : nsISupports *refCon)
857 : {
858 0 : return NS_ERROR_NOT_IMPLEMENTED;
859 : }
860 :
861 : NS_IMETHODIMP
862 0 : nsInsertPlaintextCommand::DoCommandParams(const char *aCommandName,
863 : nsICommandParams *aParams,
864 : nsISupports *refCon)
865 : {
866 0 : NS_ENSURE_ARG_POINTER(aParams);
867 :
868 0 : nsCOMPtr<nsIPlaintextEditor> editor = do_QueryInterface(refCon);
869 0 : NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
870 :
871 : // Get text to insert from command params
872 0 : nsAutoString text;
873 0 : nsresult rv = aParams->GetStringValue(STATE_DATA, text);
874 0 : NS_ENSURE_SUCCESS(rv, rv);
875 :
876 0 : if (!text.IsEmpty())
877 0 : return editor->InsertText(text);
878 :
879 0 : return NS_OK;
880 : }
881 :
882 : NS_IMETHODIMP
883 0 : nsInsertPlaintextCommand::GetCommandStateParams(const char *aCommandName,
884 : nsICommandParams *aParams,
885 : nsISupports *refCon)
886 : {
887 0 : NS_ENSURE_ARG_POINTER(aParams);
888 :
889 0 : bool outCmdEnabled = false;
890 0 : IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
891 0 : return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
892 : }
893 :
894 :
895 : NS_IMETHODIMP
896 0 : nsPasteQuotationCommand::IsCommandEnabled(const char * aCommandName,
897 : nsISupports *refCon,
898 : bool *outCmdEnabled)
899 : {
900 0 : NS_ENSURE_ARG_POINTER(outCmdEnabled);
901 :
902 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
903 0 : nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
904 0 : if (editor && mailEditor) {
905 : PRUint32 flags;
906 0 : editor->GetFlags(&flags);
907 0 : if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask))
908 0 : return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
909 : }
910 :
911 0 : *outCmdEnabled = false;
912 0 : return NS_OK;
913 : }
914 :
915 :
916 : NS_IMETHODIMP
917 0 : nsPasteQuotationCommand::DoCommand(const char *aCommandName,
918 : nsISupports *refCon)
919 : {
920 0 : nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
921 0 : if (mailEditor)
922 0 : return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
923 :
924 0 : return NS_ERROR_NOT_IMPLEMENTED;
925 : }
926 :
927 : NS_IMETHODIMP
928 0 : nsPasteQuotationCommand::DoCommandParams(const char *aCommandName,
929 : nsICommandParams *aParams,
930 : nsISupports *refCon)
931 : {
932 0 : nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(refCon);
933 0 : if (mailEditor)
934 0 : return mailEditor->PasteAsQuotation(nsIClipboard::kGlobalClipboard);
935 :
936 0 : return NS_ERROR_NOT_IMPLEMENTED;
937 : }
938 :
939 : NS_IMETHODIMP
940 0 : nsPasteQuotationCommand::GetCommandStateParams(const char *aCommandName,
941 : nsICommandParams *aParams,
942 : nsISupports *refCon)
943 : {
944 0 : nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
945 0 : if (editor)
946 : {
947 0 : bool enabled = false;
948 0 : editor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled);
949 0 : aParams->SetBooleanValue(STATE_ENABLED, enabled);
950 : }
951 :
952 0 : return NS_OK;
953 : }
954 :
|