forked from sensboston/fictionbookeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementDescriptor.cpp
334 lines (278 loc) · 7.18 KB
/
ElementDescriptor.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "stdafx.h"
#include "apputils.h"
#include "utils.h"
#include "ElementDescriptor.h"
#include "FBDoc.h"
#include "Settings.h"
extern CSettings _Settings;
const int IMG_ID_BODY = 0;
const int IMG_ID_SECTION = 0;
const int IMG_ID_IMAGE = 3;
const int IMG_ID_POEM = 3;
CString GetTitleSc(const MSHTML::IHTMLElementPtr elem)
{
CString text = elem->innerText;
text = text.Left(20) + L"...";
U::NormalizeInplace(text);
return text;
}
///===================================================================================
// CElementDescriptor
CElementDescriptor::CElementDescriptor()
{
}
bool CElementDescriptor::Init(FN_IsMe fn1, FN_GetTitle fn2, int imageID, bool default_state, CString caption)
{
m_script = false;
m_getTitle = fn2;
m_isMe = fn1;
m_imageID = imageID;
m_viewInTree = _Settings.GetDocTreeItemState(caption, default_state);
m_caption = caption;
m_title_from_script = false;
return true;
}
bool CElementDescriptor::Load(CString path)
{
m_script = true;
CString noExt = path.Left(path.ReverseFind(L'.'));
m_caption = noExt.Right(noExt.GetLength() - noExt.ReverseFind(L'\\') - 1);
CString folder = path.Left(path.ReverseFind((L'\\'))+ 1);
m_script_path = path;
m_viewInTree = false;
m_imageID = 0;
m_class_name = AskClassName();
m_getTitle = (FN_GetTitle)GetTitleSc;
m_picture = 0;
m_imageID = 9;
if(m_class_name.GetLength() == 0)
return false;
WIN32_FIND_DATA picFd;
wchar_t* picName = new wchar_t[path.GetLength() + 1];
wcscpy(picName, m_caption);
CString picPathNoExt = (folder + picName);
HANDLE hPicture = FindFirstFile(picPathNoExt + L".bmp", &picFd);
HANDLE hIcon = FindFirstFile(picPathNoExt + L".ico", &picFd);
if(hPicture != INVALID_HANDLE_VALUE)
{
HBITMAP bitmap = (HBITMAP)LoadImage(NULL, (picPathNoExt + L".bmp").GetBuffer(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(bitmap != NULL)
{
m_picture = bitmap;
m_pictType = 0;
}
}
else if(hIcon != INVALID_HANDLE_VALUE)
{
HICON icon = (HICON)LoadImage(NULL, (picPathNoExt + L".ico").GetBuffer(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
if(icon != NULL)
{
m_picture = icon;
m_pictType = 1;
}
}
FindClose(hPicture);
FindClose(hIcon);
delete[] picName;
return true;
}
CString CElementDescriptor::AskClassName()
{
CComVariant vtResult;
CComVariant params(m_script_path);
FB::Doc::m_active_doc->InvokeFunc(L"apiGetClassName", ¶ms, 1, vtResult);
return vtResult.bstrVal;
}
bool CElementDescriptor::ViewInTree()
{
return m_viewInTree;
}
int CElementDescriptor::GetDTImageID()
{
return m_imageID;
}
bool CElementDescriptor::IsMe(const MSHTML::IHTMLElementPtr elem)
{
if(m_script)
{
if(U::scmp(elem->className, m_class_name) == 0)
return true;
return false;
}
else
{
return m_isMe(elem);
}
}
CString CElementDescriptor::GetTitle(const MSHTML::IHTMLElementPtr elem)
{
if(m_script)
{
// ??????? ?????? title ????? ??????
CComVariant vtResult;
CComVariant params(m_script_path);
FB::Doc::m_active_doc->InvokeFunc(L"apiGetTitle", ¶ms, 1, vtResult);
if(vtResult.bstrVal != L"")
return vtResult.bstrVal;
else
return m_getTitle(elem);
}
else
{
return m_getTitle(elem);
}
}
CString CElementDescriptor::GetCaption()
{
return m_caption;
}
void CElementDescriptor::SetViewInTree(bool view)
{
m_viewInTree = view;
if(!m_script)
_Settings.SetDocTreeItemState(m_caption, view);
}
void CElementDescriptor::ProcessScript()
{
if(!m_script)
return;
CComVariant vtResult;
CComVariant params(m_script_path);
FB::Doc::m_active_doc->InvokeFunc(L"apiProcessCmd", ¶ms, 1, vtResult);
}
void CElementDescriptor::CleanUp()
{
if(!m_script)
return;
CComVariant vtResult;
CComVariant params(m_class_name);
FB::Doc::m_active_doc->InvokeFunc(L"apiCleanUp", ¶ms, 1, vtResult);
}
bool CElementDescriptor::GetPic(HANDLE& handle, int& type)
{
if(!m_picture)
return false;
handle = m_picture;
type = m_pictType;
return true;
}
void CElementDescriptor::SetImageID(int ID)
{
m_imageID = ID;
}
/*///===================================================================================
// CBodyED
bool CBodyED::IsMe(const MSHTML::IHTMLElementPtr elem)
{
CString tagName = elem->tagName;
CString className = elem->className;
if(tagName == L"DIV" && className == L"body")
return true;
return false;
}
CString CBodyED::GetElementCaption(const MSHTML::IHTMLElementPtr elem)
{
CString txt=AU::GetAttrCS(elem,L"fbname");
U::NormalizeInplace(txt);
if (txt.IsEmpty())
{
txt = FindTitle(elem);
}
return txt;
}
int CBodyED::GetDTImageID()
{
return IMG_ID_BODY;
}
///=====================================================================================
// CSectionED
bool CSectionED::IsMe(const MSHTML::IHTMLElementPtr elem)
{
CString tagName = elem->tagName;
CString className = elem->className;
if(tagName == L"DIV" && className == L"section")
return true;
return false;
}
CString CSectionED::GetElementCaption(const MSHTML::IHTMLElementPtr elem)
{
return FindTitle(elem);
}
int CSectionED::GetDTImageID()
{
return IMG_ID_SECTION;
}
///=====================================================================================
// CImageED
bool CImageED::IsMe(const MSHTML::IHTMLElementPtr elem)
{
CString tagName = elem->tagName;
CString className = elem->className;
if(tagName == L"DIV" && className == L"image")
return true;
return false;
}
CString CImageED::GetElementCaption(const MSHTML::IHTMLElementPtr elem)
{
return FindTitle(elem);
}
int CImageED::GetDTImageID()
{
return IMG_ID_IMAGE;
}
///=====================================================================================
// CPoemED
bool CPoemED::IsMe(const MSHTML::IHTMLElementPtr elem)
{
CString tagName = elem->tagName;
CString className = elem->className;
if(tagName == L"DIV" && (className == L"poem" || className == L"stanza"))
return true;
return false;
}
CString CPoemED::GetElementCaption(const MSHTML::IHTMLElementPtr elem)
{
return GetImageFileName(elem);
}
int CPoemED::GetDTImageID()
{
return IMG_ID_POEM;
}
///=====================================================================================
// ??????????????? ???????
MSHTML::IHTMLElementPtr CElementDescriptor::FindTitleNode(MSHTML::IHTMLDOMNodePtr elem)
{
MSHTML::IHTMLDOMNodePtr node(elem->firstChild);
_bstr_t cls(MSHTML::IHTMLElementPtr(elem)->className);
if ((bool)node && node->nodeType==1 && U::scmp(node->nodeName,L"DIV")==0)
{
_bstr_t cls(MSHTML::IHTMLElementPtr(node)->className);
if (U::scmp(cls,L"image")==0) {
node=node->nextSibling;
if (node->nodeType!=1 || U::scmp(node->nodeName,L"DIV"))
return NULL;
cls=MSHTML::IHTMLElementPtr(node)->className;
}
if (U::scmp(cls,L"title")==0)
return MSHTML::IHTMLElementPtr(node);
}
return NULL;
}
CString CElementDescriptor::FindTitle(MSHTML::IHTMLDOMNodePtr elem)
{
MSHTML::IHTMLElementPtr tn(FindTitleNode(elem));
if (tn)
return (const wchar_t *)tn->innerText;
return CString();
}
CString CElementDescriptor::GetImageFileName(MSHTML::IHTMLDOMNodePtr elem)
{
_bstr_t cls(MSHTML::IHTMLElementPtr(elem)->className);
if (U::scmp(cls,L"image") != 0)
return L"";
CString href(MSHTML::IHTMLElementPtr(elem)->getAttribute(L"href", 0));
if(!href.GetLength())
return L"";
CString name = href.Right(href.GetLength() - 1);
return name;
}*/