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