blob: a3cec1ac78939cc9579e83eb336a27906b8dd1a5 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
avi@google.comccb3d9f2008-08-13 01:25:23 +09005#ifndef BASE_CLIPBOARD_H_
6#define BASE_CLIPBOARD_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/gfx/size.h"
13#include "base/shared_memory.h"
14
avi@google.comccb3d9f2008-08-13 01:25:23 +090015#if defined(OS_MACOSX)
16@class NSString;
17#endif
18
initial.commit3f4a7322008-07-27 06:49:38 +090019class Clipboard {
20 public:
avi@google.comccb3d9f2008-08-13 01:25:23 +090021#if defined(OS_WIN)
22 typedef unsigned int FormatType;
23#elif defined(OS_MACOSX)
24 typedef NSString *FormatType;
25#endif
26
initial.commit3f4a7322008-07-27 06:49:38 +090027 Clipboard();
28 ~Clipboard();
29
30 // Clears the clipboard. It is usually a good idea to clear the clipboard
31 // before writing content to the clipboard.
avi@google.comccb3d9f2008-08-13 01:25:23 +090032 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +090033
34 // Adds UNICODE and ASCII text to the clipboard.
avi@google.comccb3d9f2008-08-13 01:25:23 +090035 void WriteText(const std::wstring& text);
initial.commit3f4a7322008-07-27 06:49:38 +090036
37 // Adds HTML to the clipboard. The url parameter is optional, but especially
38 // useful if the HTML fragment contains relative links
avi@google.comccb3d9f2008-08-13 01:25:23 +090039 void WriteHTML(const std::wstring& markup, const std::string& src_url);
initial.commit3f4a7322008-07-27 06:49:38 +090040
41 // Adds a bookmark to the clipboard
avi@google.comccb3d9f2008-08-13 01:25:23 +090042 void WriteBookmark(const std::wstring& title, const std::string& url);
initial.commit3f4a7322008-07-27 06:49:38 +090043
44 // Adds both a bookmark and an HTML hyperlink to the clipboard. It is a
45 // convenience wrapper around WriteBookmark and WriteHTML.
avi@google.comccb3d9f2008-08-13 01:25:23 +090046 void WriteHyperlink(const std::wstring& title, const std::string& url);
initial.commit3f4a7322008-07-27 06:49:38 +090047
avi@google.comccb3d9f2008-08-13 01:25:23 +090048#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090049 // Adds a bitmap to the clipboard
50 // This is the slowest way to copy a bitmap to the clipboard as we must first
51 // memcpy the pixels into GDI and the blit the bitmap to the clipboard.
52 // Pixel format is assumed to be 32-bit BI_RGB.
avi@google.comccb3d9f2008-08-13 01:25:23 +090053 void WriteBitmap(const void* pixels, const gfx::Size& size);
initial.commit3f4a7322008-07-27 06:49:38 +090054
55 // Adds a bitmap to the clipboard
56 // This function requires read and write access to the bitmap, but does not
57 // actually modify the shared memory region.
58 // Pixel format is assumed to be 32-bit BI_RGB.
59 void WriteBitmapFromSharedMemory(const SharedMemory& bitmap,
avi@google.comccb3d9f2008-08-13 01:25:23 +090060 const gfx::Size& size);
initial.commit3f4a7322008-07-27 06:49:38 +090061
62 // Adds a bitmap to the clipboard
63 // This is the fastest way to copy a bitmap to the clipboard. The HBITMAP
64 // may either be device-dependent or device-independent.
avi@google.comccb3d9f2008-08-13 01:25:23 +090065 void WriteBitmapFromHandle(HBITMAP hbitmap, const gfx::Size& size);
initial.commit3f4a7322008-07-27 06:49:38 +090066
67 // Used by WebKit to determine whether WebKit wrote the clipboard last
avi@google.comccb3d9f2008-08-13 01:25:23 +090068 void WriteWebSmartPaste();
69#endif
initial.commit3f4a7322008-07-27 06:49:38 +090070
71 // Adds a file or group of files to the clipboard.
avi@google.comccb3d9f2008-08-13 01:25:23 +090072 void WriteFile(const std::wstring& file);
73 void WriteFiles(const std::vector<std::wstring>& files);
initial.commit3f4a7322008-07-27 06:49:38 +090074
75 // Tests whether the clipboard contains a certain format
avi@google.comccb3d9f2008-08-13 01:25:23 +090076 bool IsFormatAvailable(FormatType format) const;
initial.commit3f4a7322008-07-27 06:49:38 +090077
78 // Reads UNICODE text from the clipboard, if available.
79 void ReadText(std::wstring* result) const;
80
81 // Reads ASCII text from the clipboard, if available.
82 void ReadAsciiText(std::string* result) const;
83
84 // Reads HTML from the clipboard, if available.
85 void ReadHTML(std::wstring* markup, std::string* src_url) const;
86
87 // Reads a bookmark from the clipboard, if available.
88 void ReadBookmark(std::wstring* title, std::string* url) const;
89
90 // Reads a file or group of files from the clipboard, if available, into the
ericroman@google.comdbff4f52008-08-19 01:00:38 +090091 // out parameter.
initial.commit3f4a7322008-07-27 06:49:38 +090092 void ReadFile(std::wstring* file) const;
93 void ReadFiles(std::vector<std::wstring>* files) const;
94
95 private:
avi@google.comccb3d9f2008-08-13 01:25:23 +090096#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090097 static void MarkupToHTMLClipboardFormat(const std::wstring& markup,
98 const std::string& src_url,
99 std::string* html_fragment);
100
101 static void ParseHTMLClipboardFormat(const std::string& html_fragment,
102 std::wstring* markup,
103 std::string* src_url);
104
105 static void ParseBookmarkClipboardFormat(const std::wstring& bookmark,
106 std::wstring* title,
107 std::string* url);
108
109 HWND clipboard_owner_;
avi@google.comccb3d9f2008-08-13 01:25:23 +0900110#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900111
112 DISALLOW_EVIL_CONSTRUCTORS(Clipboard);
113};
114
avi@google.comccb3d9f2008-08-13 01:25:23 +0900115#endif // BASE_CLIPBOARD_H_
license.botf003cfe2008-08-24 09:55:55 +0900116