dom/base/nsGlobalWindowCommands.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bdecb1cb7cbd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6 #include "nsGlobalWindowCommands.h"
7
8 #include "nsIComponentManager.h"
9 #include "nsIDOMElement.h"
10 #include "nsIInterfaceRequestor.h"
11 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsCRT.h"
13 #include "nsString.h"
14 #include "mozilla/ArrayUtils.h"
15 #include "mozilla/Preferences.h"
16
17 #include "nsIControllerCommandTable.h"
18 #include "nsICommandParams.h"
19
20 #include "nsPIDOMWindow.h"
21 #include "nsIPresShell.h"
22 #include "nsIDocShell.h"
23 #include "nsISelectionController.h"
24 #include "nsIWebNavigation.h"
25 #include "nsIContentViewerEdit.h"
26 #include "nsIContentViewer.h"
27 #include "nsFocusManager.h"
28 #include "nsCopySupport.h"
29 #include "nsIClipboard.h"
30 #include "mozilla/Attributes.h"
31 #include "mozilla/BasicEvents.h"
32
33 #include "nsIClipboardDragDropHooks.h"
34 #include "nsIClipboardDragDropHookList.h"
35
36 using namespace mozilla;
37
38 const char * const sSelectAllString = "cmd_selectAll";
39 const char * const sSelectNoneString = "cmd_selectNone";
40 const char * const sCopyImageLocationString = "cmd_copyImageLocation";
41 const char * const sCopyImageContentsString = "cmd_copyImageContents";
42 const char * const sCopyImageString = "cmd_copyImage";
43
44 const char * const sScrollTopString = "cmd_scrollTop";
45 const char * const sScrollBottomString = "cmd_scrollBottom";
46 const char * const sScrollPageUpString = "cmd_scrollPageUp";
47 const char * const sScrollPageDownString = "cmd_scrollPageDown";
48 const char * const sScrollLineUpString = "cmd_scrollLineUp";
49 const char * const sScrollLineDownString = "cmd_scrollLineDown";
50 const char * const sScrollLeftString = "cmd_scrollLeft";
51 const char * const sScrollRightString = "cmd_scrollRight";
52 const char * const sMoveTopString = "cmd_moveTop";
53 const char * const sMoveBottomString = "cmd_moveBottom";
54 const char * const sMovePageUpString = "cmd_movePageUp";
55 const char * const sMovePageDownString = "cmd_movePageDown";
56 const char * const sLinePreviousString = "cmd_linePrevious";
57 const char * const sLineNextString = "cmd_lineNext";
58 const char * const sCharPreviousString = "cmd_charPrevious";
59 const char * const sCharNextString = "cmd_charNext";
60
61 // These are so the browser can use editor navigation key bindings
62 // helps with accessibility (boolean pref accessibility.browsewithcaret)
63
64 const char * const sSelectCharPreviousString = "cmd_selectCharPrevious";
65 const char * const sSelectCharNextString = "cmd_selectCharNext";
66
67 const char * const sWordPreviousString = "cmd_wordPrevious";
68 const char * const sWordNextString = "cmd_wordNext";
69 const char * const sSelectWordPreviousString = "cmd_selectWordPrevious";
70 const char * const sSelectWordNextString = "cmd_selectWordNext";
71
72 const char * const sBeginLineString = "cmd_beginLine";
73 const char * const sEndLineString = "cmd_endLine";
74 const char * const sSelectBeginLineString = "cmd_selectBeginLine";
75 const char * const sSelectEndLineString = "cmd_selectEndLine";
76
77 const char * const sSelectLinePreviousString = "cmd_selectLinePrevious";
78 const char * const sSelectLineNextString = "cmd_selectLineNext";
79
80 const char * const sSelectPageUpString = "cmd_selectPageUp";
81 const char * const sSelectPageDownString = "cmd_selectPageDown";
82
83 const char * const sSelectTopString = "cmd_selectTop";
84 const char * const sSelectBottomString = "cmd_selectBottom";
85
86
87 #if 0
88 #pragma mark -
89 #endif
90
91 // a base class for selection-related commands, for code sharing
92 class nsSelectionCommandsBase : public nsIControllerCommand
93 {
94 public:
95 virtual ~nsSelectionCommandsBase() {}
96
97 NS_DECL_ISUPPORTS
98 NS_IMETHOD IsCommandEnabled(const char * aCommandName, nsISupports *aCommandContext, bool *_retval);
99 NS_IMETHOD GetCommandStateParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext);
100 NS_IMETHOD DoCommandParams(const char * aCommandName, nsICommandParams *aParams, nsISupports *aCommandContext);
101
102 protected:
103
104 static nsresult GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell);
105 static nsresult GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon);
106
107 // no member variables, please, we're stateless!
108 };
109
110 // this class implements commands whose behavior depends on the 'browse with caret' setting
111 class nsSelectMoveScrollCommand : public nsSelectionCommandsBase
112 {
113 public:
114
115 NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext);
116
117 // no member variables, please, we're stateless!
118 };
119
120 // this class implements other selection commands
121 class nsSelectCommand : public nsSelectionCommandsBase
122 {
123 public:
124
125 NS_IMETHOD DoCommand(const char * aCommandName, nsISupports *aCommandContext);
126
127 // no member variables, please, we're stateless!
128 };
129
130 #if 0
131 #pragma mark -
132 #endif
133
134
135 NS_IMPL_ISUPPORTS(nsSelectionCommandsBase, nsIControllerCommand)
136
137 /* boolean isCommandEnabled (in string aCommandName, in nsISupports aCommandContext); */
138 NS_IMETHODIMP
139 nsSelectionCommandsBase::IsCommandEnabled(const char * aCommandName,
140 nsISupports *aCommandContext,
141 bool *outCmdEnabled)
142 {
143 // XXX this needs fixing. e.g. you can't scroll up if you're already at the top of
144 // the document.
145 *outCmdEnabled = true;
146 return NS_OK;
147 }
148
149 /* void getCommandStateParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
150 NS_IMETHODIMP
151 nsSelectionCommandsBase::GetCommandStateParams(const char *aCommandName,
152 nsICommandParams *aParams, nsISupports *aCommandContext)
153 {
154 // XXX we should probably return the enabled state
155 return NS_ERROR_NOT_IMPLEMENTED;
156 }
157
158 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
159 NS_IMETHODIMP
160 nsSelectionCommandsBase::DoCommandParams(const char *aCommandName,
161 nsICommandParams *aParams, nsISupports *aCommandContext)
162 {
163 return DoCommand(aCommandName, aCommandContext);
164 }
165
166 // protected methods
167
168 nsresult
169 nsSelectionCommandsBase::GetPresShellFromWindow(nsPIDOMWindow *aWindow, nsIPresShell **aPresShell)
170 {
171 *aPresShell = nullptr;
172 NS_ENSURE_TRUE(aWindow, NS_ERROR_FAILURE);
173
174 nsIDocShell *docShell = aWindow->GetDocShell();
175 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
176
177 NS_IF_ADDREF(*aPresShell = docShell->GetPresShell());
178 return NS_OK;
179 }
180
181 nsresult
182 nsSelectionCommandsBase::GetSelectionControllerFromWindow(nsPIDOMWindow *aWindow, nsISelectionController **aSelCon)
183 {
184 *aSelCon = nullptr;
185
186 nsCOMPtr<nsIPresShell> presShell;
187 GetPresShellFromWindow(aWindow, getter_AddRefs(presShell));
188 if (presShell)
189 return CallQueryInterface(presShell, aSelCon);
190
191 return NS_ERROR_FAILURE;
192 }
193
194 #if 0
195 #pragma mark -
196 #endif
197
198 static const struct BrowseCommand {
199 const char *reverse, *forward;
200 nsresult (NS_STDCALL nsISelectionController::*scroll)(bool);
201 nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool);
202 } browseCommands[] = {
203 { sScrollTopString, sScrollBottomString,
204 &nsISelectionController::CompleteScroll },
205 { sScrollPageUpString, sScrollPageDownString,
206 &nsISelectionController::ScrollPage },
207 { sScrollLineUpString, sScrollLineDownString,
208 &nsISelectionController::ScrollLine },
209 { sScrollLeftString, sScrollRightString,
210 &nsISelectionController::ScrollCharacter },
211 { sMoveTopString, sMoveBottomString,
212 &nsISelectionController::CompleteScroll,
213 &nsISelectionController::CompleteMove },
214 { sMovePageUpString, sMovePageDownString,
215 &nsISelectionController::ScrollPage,
216 &nsISelectionController::PageMove },
217 { sLinePreviousString, sLineNextString,
218 &nsISelectionController::ScrollLine,
219 &nsISelectionController::LineMove },
220 { sWordPreviousString, sWordNextString,
221 &nsISelectionController::ScrollCharacter,
222 &nsISelectionController::WordMove },
223 { sCharPreviousString, sCharNextString,
224 &nsISelectionController::ScrollCharacter,
225 &nsISelectionController::CharacterMove },
226 { sBeginLineString, sEndLineString,
227 &nsISelectionController::CompleteScroll,
228 &nsISelectionController::IntraLineMove }
229 };
230
231 nsresult
232 nsSelectMoveScrollCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
233 {
234 nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(aCommandContext));
235 nsCOMPtr<nsISelectionController> selCont;
236 GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont));
237 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
238
239 // We allow the caret to be moved with arrow keys on any window for which
240 // the caret is enabled. In particular, this includes caret-browsing mode
241 // in non-chrome documents.
242 bool caretOn = false;
243 selCont->GetCaretEnabled(&caretOn);
244 if (!caretOn) {
245 caretOn = Preferences::GetBool("accessibility.browsewithcaret");
246 if (caretOn) {
247 nsCOMPtr<nsIDocShell> docShell = piWindow->GetDocShell();
248 if (docShell && docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
249 caretOn = false;
250 }
251 }
252 }
253
254 for (size_t i = 0; i < ArrayLength(browseCommands); i++) {
255 bool forward = !strcmp(aCommandName, browseCommands[i].forward);
256 if (forward || !strcmp(aCommandName, browseCommands[i].reverse)) {
257 if (caretOn && browseCommands[i].move &&
258 NS_SUCCEEDED((selCont->*(browseCommands[i].move))(forward, false))) {
259 // adjust the focus to the new caret position
260 nsIFocusManager* fm = nsFocusManager::GetFocusManager();
261 if (fm) {
262 nsCOMPtr<nsIDOMElement> result;
263 fm->MoveFocus(piWindow, nullptr, nsIFocusManager::MOVEFOCUS_CARET,
264 nsIFocusManager::FLAG_NOSCROLL,
265 getter_AddRefs(result));
266 }
267 return NS_OK;
268 }
269 return (selCont->*(browseCommands[i].scroll))(forward);
270 }
271 }
272 return NS_ERROR_NOT_IMPLEMENTED;
273 }
274
275
276 #if 0
277 #pragma mark -
278 #endif
279
280 nsresult
281 nsSelectCommand::DoCommand(const char *aCommandName, nsISupports *aCommandContext)
282 {
283 nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(aCommandContext));
284 nsCOMPtr<nsISelectionController> selCont;
285 GetSelectionControllerFromWindow(piWindow, getter_AddRefs(selCont));
286 NS_ENSURE_TRUE(selCont, NS_ERROR_NOT_INITIALIZED);
287
288 nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
289
290 // These commands are so the browser can use caret navigation key bindings -
291 // Helps with accessibility - aaronl@netscape.com
292 if (!nsCRT::strcmp(aCommandName, sSelectCharPreviousString))
293 rv = selCont->CharacterMove(false, true);
294 else if (!nsCRT::strcmp(aCommandName, sSelectCharNextString))
295 rv = selCont->CharacterMove(true, true);
296 else if (!nsCRT::strcmp(aCommandName, sSelectWordPreviousString))
297 rv = selCont->WordMove(false, true);
298 else if (!nsCRT::strcmp(aCommandName, sSelectWordNextString))
299 rv = selCont->WordMove(true, true);
300 else if (!nsCRT::strcmp(aCommandName, sSelectBeginLineString))
301 rv = selCont->IntraLineMove(false, true);
302 else if (!nsCRT::strcmp(aCommandName, sSelectEndLineString))
303 rv = selCont->IntraLineMove(true, true);
304 else if (!nsCRT::strcmp(aCommandName, sSelectLinePreviousString))
305 rv = selCont->LineMove(false, true);
306 else if (!nsCRT::strcmp(aCommandName, sSelectLineNextString))
307 rv = selCont->LineMove(true, true);
308 else if (!nsCRT::strcmp(aCommandName, sSelectPageUpString))
309 rv = selCont->PageMove(false, true);
310 else if (!nsCRT::strcmp(aCommandName, sSelectPageDownString))
311 rv = selCont->PageMove(true, true);
312 else if (!nsCRT::strcmp(aCommandName, sSelectTopString))
313 rv = selCont->CompleteMove(false, true);
314 else if (!nsCRT::strcmp(aCommandName, sSelectBottomString))
315 rv = selCont->CompleteMove(true, true);
316
317 return rv;
318 }
319
320 #if 0
321 #pragma mark -
322 #endif
323
324 class nsClipboardCommand MOZ_FINAL : public nsIControllerCommand
325 {
326 public:
327
328 NS_DECL_ISUPPORTS
329 NS_DECL_NSICONTROLLERCOMMAND
330 };
331
332 NS_IMPL_ISUPPORTS(nsClipboardCommand, nsIControllerCommand)
333
334 nsresult
335 nsClipboardCommand::IsCommandEnabled(const char* aCommandName, nsISupports *aContext, bool *outCmdEnabled)
336 {
337 NS_ENSURE_ARG_POINTER(outCmdEnabled);
338 *outCmdEnabled = false;
339
340 if (strcmp(aCommandName, "cmd_copy"))
341 return NS_OK;
342
343 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
344 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
345
346 nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
347 *outCmdEnabled = nsCopySupport::CanCopy(doc);
348 return NS_OK;
349 }
350
351 nsresult
352 nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext)
353 {
354 if (strcmp(aCommandName, "cmd_copy"))
355 return NS_OK;
356
357 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
358 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
359
360 nsIDocShell *docShell = window->GetDocShell();
361 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
362
363 nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
364 NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
365
366 nsCopySupport::FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard, presShell, nullptr);
367 return NS_OK;
368 }
369
370 NS_IMETHODIMP
371 nsClipboardCommand::GetCommandStateParams(const char *aCommandName,
372 nsICommandParams *aParams, nsISupports *aCommandContext)
373 {
374 return NS_ERROR_NOT_IMPLEMENTED;
375 }
376
377 nsresult
378 nsClipboardCommand::DoCommandParams(const char *aCommandName, nsICommandParams* aParams, nsISupports *aContext)
379 {
380 return DoCommand(aCommandName, aContext);
381 }
382
383 #if 0
384 #pragma mark -
385 #endif
386
387 class nsSelectionCommand : public nsIControllerCommand
388 {
389 public:
390 virtual ~nsSelectionCommand() {}
391
392 NS_DECL_ISUPPORTS
393 NS_DECL_NSICONTROLLERCOMMAND
394
395 protected:
396
397 virtual nsresult IsClipboardCommandEnabled(const char * aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled) = 0;
398 virtual nsresult DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams) = 0;
399
400 static nsresult GetContentViewerEditFromContext(nsISupports *aContext, nsIContentViewerEdit **aEditInterface);
401
402 // no member variables, please, we're stateless!
403 };
404
405
406 NS_IMPL_ISUPPORTS(nsSelectionCommand, nsIControllerCommand)
407
408
409 /*---------------------------------------------------------------------------
410
411 nsSelectionCommand
412
413 ----------------------------------------------------------------------------*/
414
415 NS_IMETHODIMP
416 nsSelectionCommand::IsCommandEnabled(const char * aCommandName,
417 nsISupports *aCommandContext,
418 bool *outCmdEnabled)
419 {
420 NS_ENSURE_ARG_POINTER(outCmdEnabled);
421 *outCmdEnabled = false;
422
423 nsCOMPtr<nsIContentViewerEdit> contentEdit;
424 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
425 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
426
427 return IsClipboardCommandEnabled(aCommandName, contentEdit, outCmdEnabled);
428 }
429
430 NS_IMETHODIMP
431 nsSelectionCommand::DoCommand(const char *aCommandName,
432 nsISupports *aCommandContext)
433 {
434 nsCOMPtr<nsIContentViewerEdit> contentEdit;
435 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
436 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
437
438 return DoClipboardCommand(aCommandName, contentEdit, nullptr);
439 }
440
441 NS_IMETHODIMP
442 nsSelectionCommand::GetCommandStateParams(const char *aCommandName,
443 nsICommandParams *aParams,
444 nsISupports *aCommandContext)
445 {
446 return NS_ERROR_NOT_IMPLEMENTED;
447 }
448
449 NS_IMETHODIMP
450 nsSelectionCommand::DoCommandParams(const char *aCommandName,
451 nsICommandParams *aParams,
452 nsISupports *aCommandContext)
453 {
454 nsCOMPtr<nsIContentViewerEdit> contentEdit;
455 GetContentViewerEditFromContext(aCommandContext, getter_AddRefs(contentEdit));
456 NS_ENSURE_TRUE(contentEdit, NS_ERROR_NOT_INITIALIZED);
457
458 return DoClipboardCommand(aCommandName, contentEdit, aParams);
459 }
460
461 nsresult
462 nsSelectionCommand::GetContentViewerEditFromContext(nsISupports *aContext,
463 nsIContentViewerEdit **aEditInterface)
464 {
465 NS_ENSURE_ARG(aEditInterface);
466 *aEditInterface = nullptr;
467
468 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
469 NS_ENSURE_TRUE(window, NS_ERROR_INVALID_ARG);
470
471 nsIDocShell *docShell = window->GetDocShell();
472 NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
473
474 nsCOMPtr<nsIContentViewer> viewer;
475 docShell->GetContentViewer(getter_AddRefs(viewer));
476 nsCOMPtr<nsIContentViewerEdit> edit(do_QueryInterface(viewer));
477 NS_ENSURE_TRUE(edit, NS_ERROR_FAILURE);
478
479 *aEditInterface = edit;
480 NS_ADDREF(*aEditInterface);
481 return NS_OK;
482 }
483
484 #if 0
485 #pragma mark -
486 #endif
487
488 #define NS_DECL_CLIPBOARD_COMMAND(_cmd) \
489 class _cmd : public nsSelectionCommand \
490 { \
491 protected: \
492 \
493 virtual nsresult IsClipboardCommandEnabled(const char* aCommandName, \
494 nsIContentViewerEdit* aEdit, bool *outCmdEnabled); \
495 virtual nsresult DoClipboardCommand(const char* aCommandName, \
496 nsIContentViewerEdit* aEdit, nsICommandParams* aParams); \
497 /* no member variables, please, we're stateless! */ \
498 };
499
500 NS_DECL_CLIPBOARD_COMMAND(nsClipboardCopyLinkCommand)
501 NS_DECL_CLIPBOARD_COMMAND(nsClipboardImageCommands)
502 NS_DECL_CLIPBOARD_COMMAND(nsClipboardSelectAllNoneCommands)
503 NS_DECL_CLIPBOARD_COMMAND(nsClipboardGetContentsCommand)
504
505 nsresult
506 nsClipboardCopyLinkCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
507 {
508 return aEdit->GetInLink(outCmdEnabled);
509 }
510
511 nsresult
512 nsClipboardCopyLinkCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
513 {
514 return aEdit->CopyLinkLocation();
515 }
516
517 #if 0
518 #pragma mark -
519 #endif
520
521 nsresult
522 nsClipboardImageCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
523 {
524 return aEdit->GetInImage(outCmdEnabled);
525 }
526
527 nsresult
528 nsClipboardImageCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
529 {
530 if (!nsCRT::strcmp(sCopyImageLocationString, aCommandName))
531 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_TEXT);
532 if (!nsCRT::strcmp(sCopyImageContentsString, aCommandName))
533 return aEdit->CopyImage(nsIContentViewerEdit::COPY_IMAGE_DATA);
534 int32_t copyFlags = nsIContentViewerEdit::COPY_IMAGE_DATA |
535 nsIContentViewerEdit::COPY_IMAGE_HTML;
536 if (aParams)
537 aParams->GetLongValue("imageCopy", &copyFlags);
538 return aEdit->CopyImage(copyFlags);
539 }
540
541 #if 0
542 #pragma mark -
543 #endif
544
545 nsresult
546 nsClipboardSelectAllNoneCommands::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
547 {
548 *outCmdEnabled = true;
549 return NS_OK;
550 }
551
552 nsresult
553 nsClipboardSelectAllNoneCommands::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
554 {
555 if (!nsCRT::strcmp(sSelectAllString, aCommandName))
556 return aEdit->SelectAll();
557
558 return aEdit->ClearSelection();
559 }
560
561
562 #if 0
563 #pragma mark -
564 #endif
565
566 nsresult
567 nsClipboardGetContentsCommand::IsClipboardCommandEnabled(const char* aCommandName, nsIContentViewerEdit* aEdit, bool *outCmdEnabled)
568 {
569 return aEdit->GetCanGetContents(outCmdEnabled);
570 }
571
572 nsresult
573 nsClipboardGetContentsCommand::DoClipboardCommand(const char *aCommandName, nsIContentViewerEdit* aEdit, nsICommandParams* aParams)
574 {
575 NS_ENSURE_ARG(aParams);
576
577 nsAutoCString mimeType("text/plain");
578
579 nsXPIDLCString format; // nsICommandParams needs to use nsACString
580 if (NS_SUCCEEDED(aParams->GetCStringValue("format", getter_Copies(format))))
581 mimeType.Assign(format);
582
583 bool selectionOnly = false;
584 aParams->GetBooleanValue("selection_only", &selectionOnly);
585
586 nsAutoString contents;
587 nsresult rv = aEdit->GetContents(mimeType.get(), selectionOnly, contents);
588 if (NS_FAILED(rv))
589 return rv;
590
591 return aParams->SetStringValue("result", contents);
592 }
593
594 #if 0 // Remove unless needed again, bug 204777
595 class nsWebNavigationBaseCommand : public nsIControllerCommand
596 {
597 public:
598 virtual ~nsWebNavigationBaseCommand() {}
599
600 NS_DECL_ISUPPORTS
601 NS_DECL_NSICONTROLLERCOMMAND
602
603 protected:
604
605 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled) = 0;
606 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation) = 0;
607
608 static nsresult GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation);
609
610 // no member variables, please, we're stateless!
611 };
612
613 class nsGoForwardCommand : public nsWebNavigationBaseCommand
614 {
615 protected:
616
617 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled);
618 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
619 // no member variables, please, we're stateless!
620 };
621
622 class nsGoBackCommand : public nsWebNavigationBaseCommand
623 {
624 protected:
625
626 virtual nsresult IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled);
627 virtual nsresult DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation);
628 // no member variables, please, we're stateless!
629 };
630
631 /*---------------------------------------------------------------------------
632
633 nsWebNavigationCommands
634 no params
635 ----------------------------------------------------------------------------*/
636
637 NS_IMPL_ISUPPORTS(nsWebNavigationBaseCommand, nsIControllerCommand)
638
639 NS_IMETHODIMP
640 nsWebNavigationBaseCommand::IsCommandEnabled(const char * aCommandName,
641 nsISupports *aCommandContext,
642 bool *outCmdEnabled)
643 {
644 NS_ENSURE_ARG_POINTER(outCmdEnabled);
645 *outCmdEnabled = false;
646
647 nsCOMPtr<nsIWebNavigation> webNav;
648 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
649 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
650
651 return IsCommandEnabled(aCommandName, webNav, outCmdEnabled);
652 }
653
654 NS_IMETHODIMP
655 nsWebNavigationBaseCommand::GetCommandStateParams(const char *aCommandName,
656 nsICommandParams *aParams, nsISupports *aCommandContext)
657 {
658 // XXX we should probably return the enabled state
659 return NS_ERROR_NOT_IMPLEMENTED;
660 }
661
662 NS_IMETHODIMP
663 nsWebNavigationBaseCommand::DoCommand(const char *aCommandName,
664 nsISupports *aCommandContext)
665 {
666 nsCOMPtr<nsIWebNavigation> webNav;
667 GetWebNavigationFromContext(aCommandContext, getter_AddRefs(webNav));
668 NS_ENSURE_TRUE(webNav, NS_ERROR_INVALID_ARG);
669
670 return DoWebNavCommand(aCommandName, webNav);
671 }
672
673 /* void doCommandParams (in string aCommandName, in nsICommandParams aParams, in nsISupports aCommandContext); */
674 NS_IMETHODIMP
675 nsWebNavigationBaseCommand::DoCommandParams(const char *aCommandName,
676 nsICommandParams *aParams, nsISupports *aCommandContext)
677 {
678 return DoCommand(aCommandName, aCommandContext);
679 }
680
681 nsresult
682 nsWebNavigationBaseCommand::GetWebNavigationFromContext(nsISupports *aContext, nsIWebNavigation **aWebNavigation)
683 {
684 nsCOMPtr<nsIInterfaceRequestor> windowReq = do_QueryInterface(aContext);
685 CallGetInterface(windowReq.get(), aWebNavigation);
686 return (*aWebNavigation) ? NS_OK : NS_ERROR_FAILURE;
687 }
688
689 nsresult
690 nsGoForwardCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled)
691 {
692 return aWebNavigation->GetCanGoForward(outCmdEnabled);
693 }
694
695 nsresult
696 nsGoForwardCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
697 {
698 return aWebNavigation->GoForward();
699 }
700
701 nsresult
702 nsGoBackCommand::IsWebNavCommandEnabled(const char * aCommandName, nsIWebNavigation* aWebNavigation, bool *outCmdEnabled)
703 {
704 return aWebNavigation->GetCanGoBack(outCmdEnabled);
705 }
706
707 nsresult
708 nsGoBackCommand::DoWebNavCommand(const char *aCommandName, nsIWebNavigation* aWebNavigation)
709 {
710 return aWebNavigation->GoBack();
711 }
712 #endif
713
714 /*---------------------------------------------------------------------------
715
716 nsClipboardDragDropHookCommand
717 params value type possible values
718 "addhook" isupports nsIClipboardDragDropHooks as nsISupports
719 "removehook" isupports nsIClipboardDragDropHooks as nsISupports
720
721 ----------------------------------------------------------------------------*/
722
723 class nsClipboardDragDropHookCommand MOZ_FINAL : public nsIControllerCommand
724 {
725 public:
726
727 NS_DECL_ISUPPORTS
728 NS_DECL_NSICONTROLLERCOMMAND
729
730 protected:
731 // no member variables, please, we're stateless!
732 };
733
734
735 NS_IMPL_ISUPPORTS(nsClipboardDragDropHookCommand, nsIControllerCommand)
736
737 NS_IMETHODIMP
738 nsClipboardDragDropHookCommand::IsCommandEnabled(const char * aCommandName,
739 nsISupports *aCommandContext,
740 bool *outCmdEnabled)
741 {
742 *outCmdEnabled = true;
743 return NS_OK;
744 }
745
746 NS_IMETHODIMP
747 nsClipboardDragDropHookCommand::DoCommand(const char *aCommandName,
748 nsISupports *aCommandContext)
749 {
750 return NS_ERROR_FAILURE;
751 }
752
753 NS_IMETHODIMP
754 nsClipboardDragDropHookCommand::DoCommandParams(const char *aCommandName,
755 nsICommandParams *aParams,
756 nsISupports *aCommandContext)
757 {
758 NS_ENSURE_ARG(aParams);
759
760 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aCommandContext);
761 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
762
763 nsIDocShell *docShell = window->GetDocShell();
764
765 nsCOMPtr<nsIClipboardDragDropHookList> obj = do_GetInterface(docShell);
766 if (!obj) return NS_ERROR_INVALID_ARG;
767
768 nsCOMPtr<nsISupports> isuppHook;
769
770 nsresult returnValue = NS_OK;
771 nsresult rv = aParams->GetISupportsValue("addhook", getter_AddRefs(isuppHook));
772 if (NS_SUCCEEDED(rv))
773 {
774 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
775 if (hook)
776 returnValue = obj->AddClipboardDragDropHooks(hook);
777 else
778 returnValue = NS_ERROR_INVALID_ARG;
779 }
780
781 rv = aParams->GetISupportsValue("removehook", getter_AddRefs(isuppHook));
782 if (NS_SUCCEEDED(rv))
783 {
784 nsCOMPtr<nsIClipboardDragDropHooks> hook = do_QueryInterface(isuppHook);
785 if (hook)
786 {
787 rv = obj->RemoveClipboardDragDropHooks(hook);
788 if (NS_FAILED(rv) && NS_SUCCEEDED(returnValue))
789 returnValue = rv;
790 }
791 else
792 returnValue = NS_ERROR_INVALID_ARG;
793 }
794
795 return returnValue;
796 }
797
798 NS_IMETHODIMP
799 nsClipboardDragDropHookCommand::GetCommandStateParams(const char *aCommandName,
800 nsICommandParams *aParams,
801 nsISupports *aCommandContext)
802 {
803 NS_ENSURE_ARG_POINTER(aParams);
804 return aParams->SetBooleanValue("state_enabled", true);
805 }
806
807 /*---------------------------------------------------------------------------
808
809 RegisterWindowCommands
810
811 ----------------------------------------------------------------------------*/
812
813 #define NS_REGISTER_ONE_COMMAND(_cmdClass, _cmdName) \
814 { \
815 _cmdClass* theCmd = new _cmdClass(); \
816 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
817 rv = inCommandTable->RegisterCommand(_cmdName, \
818 static_cast<nsIControllerCommand *>(theCmd)); \
819 }
820
821 #define NS_REGISTER_FIRST_COMMAND(_cmdClass, _cmdName) \
822 { \
823 _cmdClass* theCmd = new _cmdClass(); \
824 if (!theCmd) return NS_ERROR_OUT_OF_MEMORY; \
825 rv = inCommandTable->RegisterCommand(_cmdName, \
826 static_cast<nsIControllerCommand *>(theCmd));
827
828 #define NS_REGISTER_NEXT_COMMAND(_cmdClass, _cmdName) \
829 rv = inCommandTable->RegisterCommand(_cmdName, \
830 static_cast<nsIControllerCommand *>(theCmd));
831
832 #define NS_REGISTER_LAST_COMMAND(_cmdClass, _cmdName) \
833 rv = inCommandTable->RegisterCommand(_cmdName, \
834 static_cast<nsIControllerCommand *>(theCmd)); \
835 }
836
837
838 // static
839 nsresult
840 nsWindowCommandRegistration::RegisterWindowCommands(
841 nsIControllerCommandTable *inCommandTable)
842 {
843 nsresult rv;
844
845 // XXX rework the macros to use a loop is possible, reducing code size
846
847 // this set of commands is affected by the 'browse with caret' setting
848 NS_REGISTER_FIRST_COMMAND(nsSelectMoveScrollCommand, sScrollTopString);
849 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollBottomString);
850 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageUpString);
851 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollPageDownString);
852 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineUpString);
853 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLineDownString);
854 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollLeftString);
855 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sScrollRightString);
856 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveTopString);
857 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMoveBottomString);
858 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordPreviousString);
859 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sWordNextString);
860 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sBeginLineString);
861 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sEndLineString);
862 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageUpString);
863 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sMovePageDownString);
864 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLinePreviousString);
865 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sLineNextString);
866 NS_REGISTER_NEXT_COMMAND(nsSelectMoveScrollCommand, sCharPreviousString);
867 NS_REGISTER_LAST_COMMAND(nsSelectMoveScrollCommand, sCharNextString);
868
869 NS_REGISTER_FIRST_COMMAND(nsSelectCommand, sSelectCharPreviousString);
870 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectCharNextString);
871 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordPreviousString);
872 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectWordNextString);
873 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectBeginLineString);
874 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectEndLineString);
875 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLinePreviousString);
876 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectLineNextString);
877 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageUpString);
878 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectPageDownString);
879 NS_REGISTER_NEXT_COMMAND(nsSelectCommand, sSelectTopString);
880 NS_REGISTER_LAST_COMMAND(nsSelectCommand, sSelectBottomString);
881
882 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_cut");
883 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_copy");
884 NS_REGISTER_ONE_COMMAND(nsClipboardCommand, "cmd_paste");
885 NS_REGISTER_ONE_COMMAND(nsClipboardCopyLinkCommand, "cmd_copyLink");
886 NS_REGISTER_FIRST_COMMAND(nsClipboardImageCommands, sCopyImageLocationString);
887 NS_REGISTER_NEXT_COMMAND(nsClipboardImageCommands, sCopyImageContentsString);
888 NS_REGISTER_LAST_COMMAND(nsClipboardImageCommands, sCopyImageString);
889 NS_REGISTER_FIRST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectAllString);
890 NS_REGISTER_LAST_COMMAND(nsClipboardSelectAllNoneCommands, sSelectNoneString);
891
892 NS_REGISTER_ONE_COMMAND(nsClipboardGetContentsCommand, "cmd_getContents");
893
894 #if 0 // Remove unless needed again, bug 204777
895 NS_REGISTER_ONE_COMMAND(nsGoBackCommand, "cmd_browserBack");
896 NS_REGISTER_ONE_COMMAND(nsGoForwardCommand, "cmd_browserForward");
897 #endif
898
899 NS_REGISTER_ONE_COMMAND(nsClipboardDragDropHookCommand, "cmd_clipboardDragDropHook");
900
901 return rv;
902 }

mercurial