electron/atom/common/api/atom_api_clipboard.cc

63 lines
1.8 KiB
C++
Raw Normal View History

2013-04-30 08:10:25 +00:00
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
2013-04-30 08:10:25 +00:00
2014-04-16 07:31:59 +00:00
#include "native_mate/dictionary.h"
2013-04-30 08:10:25 +00:00
#include "ui/base/clipboard/clipboard.h"
#include "atom/common/node_includes.h"
2013-04-30 08:10:25 +00:00
namespace {
2013-04-30 08:10:25 +00:00
bool Has(const std::string& format_string) {
2013-04-30 08:10:25 +00:00
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD);
2013-04-30 08:10:25 +00:00
}
std::string Read(const std::string& format_string) {
2013-04-30 08:10:25 +00:00
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
std::string data;
clipboard->ReadData(format, &data);
return data;
2013-04-30 08:10:25 +00:00
}
std::string ReadText() {
2013-04-30 08:10:25 +00:00
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
std::string data;
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
return data;
2013-04-30 08:10:25 +00:00
}
void WriteText(const std::string text) {
2013-04-30 08:10:25 +00:00
ui::Clipboard::ObjectMap object_map;
object_map[ui::Clipboard::CBF_TEXT].push_back(
std::vector<char>(text.begin(), text.end()));
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
2013-04-30 08:10:25 +00:00
}
void Clear() {
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
2013-04-30 08:10:25 +00:00
}
void Initialize(v8::Handle<v8::Object> exports) {
2014-04-16 07:31:59 +00:00
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
dict.SetMethod("has", &Has);
dict.SetMethod("read", &Read);
dict.SetMethod("readText", &ReadText);
dict.SetMethod("writeText", &WriteText);
dict.SetMethod("clear", &Clear);
2013-04-30 08:10:25 +00:00
}
} // namespace
2013-04-30 08:10:25 +00:00
NODE_MODULE(atom_common_clipboard, Initialize)