Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / widget / src / xpwidgets / nsClipboardHelper.cpp
blob1dec54794dc9b4650b6ddeaeb2f5378c32540174
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corp.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * 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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsClipboardHelper.h"
41 // basics
42 #include "nsCOMPtr.h"
43 #include "nsXPCOM.h"
44 #include "nsISupportsPrimitives.h"
45 #include "nsIServiceManager.h"
47 // helpers
48 #include "nsIClipboard.h"
49 #include "nsITransferable.h"
50 #include "nsReadableUtils.h"
52 NS_IMPL_ISUPPORTS1(nsClipboardHelper, nsIClipboardHelper)
54 /*****************************************************************************
55 * nsClipboardHelper ctor / dtor
56 *****************************************************************************/
58 nsClipboardHelper::nsClipboardHelper()
62 nsClipboardHelper::~nsClipboardHelper()
64 // no members, nothing to destroy
67 /*****************************************************************************
68 * nsIClipboardHelper methods
69 *****************************************************************************/
71 NS_IMETHODIMP
72 nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
73 PRInt32 aClipboardID)
75 nsresult rv;
77 // get the clipboard
78 nsCOMPtr<nsIClipboard>
79 clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
80 NS_ENSURE_SUCCESS(rv, rv);
81 NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
83 // don't go any further if they're asking for the selection
84 // clipboard on a platform which doesn't support it (i.e., unix)
85 if (nsIClipboard::kSelectionClipboard == aClipboardID) {
86 PRBool clipboardSupported;
87 rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
88 NS_ENSURE_SUCCESS(rv, rv);
89 if (!clipboardSupported)
90 return NS_ERROR_FAILURE;
93 // create a transferable for putting data on the clipboard
94 nsCOMPtr<nsITransferable>
95 trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
96 NS_ENSURE_SUCCESS(rv, rv);
97 NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
99 // Add the text data flavor to the transferable
100 rv = trans->AddDataFlavor(kUnicodeMime);
101 NS_ENSURE_SUCCESS(rv, rv);
103 // get wStrings to hold clip data
104 nsCOMPtr<nsISupportsString>
105 data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
106 NS_ENSURE_SUCCESS(rv, rv);
107 NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
109 // populate the string
110 rv = data->SetData(aString);
111 NS_ENSURE_SUCCESS(rv, rv);
113 // qi the data object an |nsISupports| so that when the transferable holds
114 // onto it, it will addref the correct interface.
115 nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
116 NS_ENSURE_SUCCESS(rv, rv);
117 NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
119 // set the transfer data
120 rv = trans->SetTransferData(kUnicodeMime, genericData,
121 aString.Length() * 2);
122 NS_ENSURE_SUCCESS(rv, rv);
124 // put the transferable on the clipboard
125 rv = clipboard->SetData(trans, nsnull, aClipboardID);
126 NS_ENSURE_SUCCESS(rv, rv);
128 return NS_OK;
131 NS_IMETHODIMP
132 nsClipboardHelper::CopyString(const nsAString& aString)
134 nsresult rv;
136 // copy to the global clipboard. it's bad if this fails in any way.
137 rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
138 NS_ENSURE_SUCCESS(rv, rv);
140 // unix also needs us to copy to the selection clipboard. this will
141 // fail in CopyStringToClipboard if we're not on a platform that
142 // supports the selection clipboard. (this could have been #ifdef
143 // XP_UNIX, but using the SupportsSelectionClipboard call is the
144 // more correct thing to do.
146 // if this fails in any way other than "not being unix", we'll get
147 // the assertion we need in CopyStringToClipboard, and we needn't
148 // assert again here.
149 CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
151 return NS_OK;