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/. */
6 #ifndef nsCopySupport_h__
7 #define nsCopySupport_h__
10 #include "ErrorList.h"
11 #include "mozilla/AlreadyAddRefed.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/BasicEvents.h"
14 #include "nsStringFwd.h"
17 class nsIImageLoadingContent
;
18 class nsITransferable
;
27 } // namespace mozilla
30 // class of static helper functions for copy support
32 static nsresult
ClearSelectionCache();
35 * @param aDoc Needs to be not nullptr.
37 static nsresult
EncodeDocumentWithContextAndPutToClipboard(
38 mozilla::dom::Selection
* aSel
, mozilla::dom::Document
* aDoc
,
39 int16_t aClipboardID
, bool aWithRubyAnnotation
);
41 // Get the selection, or entire document, in the format specified by the mime
42 // type (text/html or text/plain). If aSel is non-null, use it, otherwise get
44 static nsresult
GetContents(const nsACString
& aMimeType
, uint32_t aFlags
,
45 mozilla::dom::Selection
* aSel
,
46 mozilla::dom::Document
* aDoc
, nsAString
& outdata
);
48 static nsresult
ImageCopy(nsIImageLoadingContent
* aImageElement
,
49 nsILoadContext
* aLoadContext
, int32_t aCopyFlags
);
51 // Get the selection as a transferable.
52 // @param aSelection Can be nullptr.
53 // @param aDocument Needs to be not nullptr.
54 // @param aTransferable Needs to be not nullptr.
55 static nsresult
GetTransferableForSelection(
56 mozilla::dom::Selection
* aSelection
, mozilla::dom::Document
* aDocument
,
57 nsITransferable
** aTransferable
);
59 // Same as GetTransferableForSelection, but doesn't skip invisible content.
60 // @param aNode Needs to be not nullptr.
61 // @param aDoc Needs to be not nullptr.
62 // @param aTransferable Needs to be not nullptr.
63 MOZ_CAN_RUN_SCRIPT_BOUNDARY
64 static nsresult
GetTransferableForNode(nsINode
* aNode
,
65 mozilla::dom::Document
* aDoc
,
66 nsITransferable
** aTransferable
);
68 * Retrieve the selection for the given document. If the current focus
69 * within the document has its own selection, aSelection will be set to it
70 * and this focused content node returned. Otherwise, aSelection will be
71 * set to the document's selection and null will be returned.
73 static already_AddRefed
<mozilla::dom::Selection
> GetSelectionForCopy(
74 mozilla::dom::Document
* aDocument
);
77 * Returns true if a copy operation is currently permitted based on the
78 * current focus and selection within the specified document.
80 static bool CanCopy(mozilla::dom::Document
* aDocument
);
83 * Fires a cut, copy or paste event, on the given presshell, depending
84 * on the value of aEventMessage, which should be either eCut, eCopy or
85 * ePaste, and perform the default copy action if the event was not
88 * If aSelection is specified, then this selection is used as the target
89 * of the operation. Otherwise, GetSelectionForCopy is used to retrieve
90 * the current selection.
92 * This will fire a cut, copy or paste event at the node at the start
93 * point of the selection. If a cut or copy event is not cancelled, the
94 * selection is copied to the clipboard and true is returned. Paste events
95 * have no default behaviour but true will be returned. It is expected
96 * that the caller will execute any needed default paste behaviour. Also,
97 * note that this method only copies text to the clipboard, the caller is
98 * responsible for removing the content during a cut operation if true is
101 * aClipboardType specifies which clipboard to use, from nsIClipboard.
103 * If aActionTaken is non-NULL, it will be set to true if an action was
104 * taken, whether it be the default action or the default being prevented.
106 * If the event is cancelled or an error occurs, false will be returned.
108 MOZ_CAN_RUN_SCRIPT_BOUNDARY
109 static bool FireClipboardEvent(mozilla::EventMessage aEventMessage
,
110 int32_t aClipboardType
,
111 mozilla::PresShell
* aPresShell
,
112 mozilla::dom::Selection
* aSelection
,
113 bool* aActionTaken
= nullptr);