-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard_paste_command.cpp
89 lines (70 loc) · 1.7 KB
/
clipboard_paste_command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "clipboard_paste_command.h"
#include <sstream>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
const std::string CLIPBOARD_TARGET = "libcppgef";
namespace cppgef
{
ClipboardPasteCommand::ClipboardPasteCommand(shared_ptr<Diagram> diagram) :
diagram_( diagram ),
first_execute_( true )
{
}
ClipboardPasteCommand::~ClipboardPasteCommand()
{
}
shared_ptr<Diagram> ClipboardPasteCommand::getDiagram()
{
return diagram_;
}
bool ClipboardPasteCommand::allowUndo() const
{
return true;
}
bool ClipboardPasteCommand::execute()
{
if (first_execute_)
{
ClipboardManager::getInstance()->paste (sigc::mem_fun(*this, &ClipboardPasteCommand::onClipboardReceived));
}
else
{
for (size_t i = 0; i < items_.size(); i++)
{
diagram_->addChild (items_[i]);
}
}
return true;
}
bool ClipboardPasteCommand::unexecute()
{
for (size_t i = 0; i < items_.size(); i++)
{
diagram_->eraseChild (items_[i]);
}
return true;
}
void ClipboardPasteCommand::onClipboardReceived(const Gtk::SelectionData& selection_data)
{
const std::string target = selection_data.get_target();
//It should always be this, because that' what we asked for when calling
//request_contents().
if(target == ClipboardManager::getInstance()->getClipboardTarget())
{
std::string clipboard_data = selection_data.get_data_as_string();
if (!clipboard_data.empty())
{
std::stringstream ifs(clipboard_data);
boost::archive::text_iarchive ia(ifs);
ia >> items_;
for (size_t i = 0; i < items_.size(); i++)
{
diagram_->addChild (items_[i]);
}
first_execute_ = false;
}
}
}
}