-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkSubject.cpp
179 lines (150 loc) · 3.32 KB
/
WorkSubject.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <cstdio>
#include "WorkSubject.hpp"
#include "ProjectCtl.hpp"
#include "utils.hpp"
using namespace wayround_i2p::ccedit;
WorkSubject_shared WorkSubject::create(
ProjectCtl_shared project_ctl,
std::filesystem::path fpth
)
{
auto ret = WorkSubject_shared(
new WorkSubject(
project_ctl,
fpth
)
);
ret->own_ptr = ret;
project_ctl->registerWorkSubject(ret);
return ret;
}
WorkSubject::WorkSubject(
ProjectCtl_shared project_ctl,
std::filesystem::path fpth
) :
destroyer(
[this]()
{
this->project_ctl->destroyWorkSubjectEditors(own_ptr);
this->project_ctl->unregisterWorkSubject(own_ptr);
own_ptr.reset();
}
)
{
this->project_ctl = project_ctl;
this->fpth = fpth;
this->controller = project_ctl->getController();
createNew();
}
WorkSubject::~WorkSubject()
{
std::cout << "~WorkSubject()" << std::endl;
destroyer.run();
}
void WorkSubject::destroy()
{
std::cout << "WorkSubject::destroy()" << std::endl;
destroyer.run();
}
Controller_shared WorkSubject::getController()
{
return controller;
}
ProjectCtl_shared WorkSubject::getProject()
{
return project_ctl;
}
// relative to project root
std::filesystem::path WorkSubject::getPath()
{
return fpth;
}
// complete system filepath
std::filesystem::path WorkSubject::getFullPath()
{
// todo: sanity check
return project_ctl->getProjectPath() / fpth;
}
void WorkSubject::createNew()
{
txt_buff = Gtk::TextBuffer::create();
txt_buff->signal_modified_changed().connect(
sigc::mem_fun(
*this,
&WorkSubject::emit_signal_modified_changed
)
);
}
void WorkSubject::emit_signal_modified_changed()
{
std::cout << "WorkSubject::emit_signal_modified_changed(): "
<< txt_buff->get_modified() << std ::endl;
priv_signal_modified_changed.emit();
}
// shortcut to reload() with allow_nonexist=false
int WorkSubject::reload()
{
return reload(false);
}
int WorkSubject::reload(bool allow_nonexist)
{
auto [txt, err] = loadStringFromFile(
getFullPath(),
allow_nonexist
);
if (err != 0)
{
return err;
}
setText(txt);
txt_buff->set_modified(false);
return 0;
}
int WorkSubject::save()
{
auto err = saveStringToFile(
getFullPath(),
txt_buff->get_text()
);
txt_buff->set_modified(false);
return err;
}
bool WorkSubject::modified()
{
return txt_buff->get_modified();
}
void WorkSubject::modified(bool val)
{
txt_buff->set_modified(val);
}
Glib::RefPtr<Gtk::TextBuffer> WorkSubject::getTextBuffer()
{
return txt_buff;
}
void WorkSubject::getBinaryBuffer()
{
// todo:
}
std::string WorkSubject::getText()
{
return txt_buff->get_text();
}
void WorkSubject::setText(std::string txt)
{
priv_signal_editors_save_state.emit();
txt_buff->set_text(txt);
priv_signal_editors_restore_state.emit();
return;
}
sigc::signal<void()> &WorkSubject::signal_editors_save_state()
{
return priv_signal_editors_save_state;
}
sigc::signal<void()> &WorkSubject::signal_editors_restore_state()
{
return priv_signal_editors_restore_state;
}
sigc::signal<void()> &WorkSubject::signal_modified_changed()
{
return priv_signal_modified_changed;
}