-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExternalHelper.h
300 lines (255 loc) · 6.88 KB
/
ExternalHelper.h
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
#ifndef EXTERNALHELPER_H
#define EXTERNALHELPER_H
#include <fcntl.h>
#include "Settings.h"
extern CSettings _Settings;
static int modalResultCode;
extern "C"
{
extern const char* build_timestamp;
extern const char* build_name;
};
class ExternalHelper :
public CComObjectRoot,
public IDispatchImpl<IExternalHelper, &IID_IExternalHelper>
{
public:
DECLARE_NO_REGISTRY()
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(ExternalHelper)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IExternalHelper)
END_COM_MAP()
// IExternalHelper
STDMETHOD(BeginUndoUnit)(IDispatch *obj,BSTR name) {
MSHTML::IMarkupServices *srv;
HRESULT hr=obj->QueryInterface(&srv);
if (FAILED(hr))
return hr;
hr=srv->raw_BeginUndoUnit(name);
srv->Release();
return hr;
}
STDMETHOD(EndUndoUnit)(IDispatch *obj) {
MSHTML::IMarkupServices *srv;
HRESULT hr=obj->QueryInterface(&srv);
if (FAILED(hr))
return hr;
hr=srv->raw_EndUndoUnit();
srv->Release();
return hr;
}
STDMETHOD(get_inflateBlock)(IDispatch *obj,BOOL *ifb) {
MSHTML::IHTMLElement3 *elem;
HRESULT hr=obj->QueryInterface(&elem);
if (FAILED(hr))
return hr;
VARIANT_BOOL vb;
hr=elem->get_inflateBlock(&vb);
*ifb=SUCCEEDED(hr) && vb==VARIANT_TRUE ? TRUE : FALSE;
elem->Release();
return hr;
}
STDMETHOD(put_inflateBlock)(IDispatch *obj,BOOL ifb) {
MSHTML::IHTMLElement3 *elem;
HRESULT hr=obj->QueryInterface(&elem);
if (FAILED(hr))
return hr;
hr=elem->put_inflateBlock(ifb ? VARIANT_TRUE : VARIANT_FALSE);
elem->Release();
return hr;
}
STDMETHOD(GenrePopup)(IDispatch *obj,LONG x,LONG y,BSTR *name);
STDMETHOD(DescShowMenu)(IDispatch *obj,LONG x,LONG y,BSTR *element_id);
// Modification by Pilgrim
/*STDMETHOD(LangPopup)(IDispatch *obj,LONG x,LONG y,BSTR *name);
STDMETHOD(SrcLangPopup)(IDispatch *obj,LONG x,LONG y,BSTR *name);
STDMETHOD(STILangPopup)(IDispatch *obj,LONG x,LONG y,BSTR *name);
STDMETHOD(STISrcLangPopup)(IDispatch *obj,LONG x,LONG y,BSTR *name);*/
STDMETHOD(GetStylePath)(BSTR *name)
{
wchar_t path[MAX_PATH + 1];
GetModuleFileName(0, path, MAX_PATH);
PathRemoveFileSpec(path);
CString us(path);
*name = us.AllocSysString();
return S_OK;
}
STDMETHOD(GetBinarySize)(BSTR data, int *length)
{
*length = SysStringByteLen(data);
return S_OK;
}
STDMETHOD(InflateParagraphs)(IDispatch *elem)
{
MSHTML::IHTMLElement2Ptr el;
elem->QueryInterface(IID_IHTMLElement2,(void**)&el);
MSHTML::IHTMLElementCollectionPtr pp(el->getElementsByTagName(L"P"));
for (long l=0;l<pp->length;++l)
{
MSHTML::IHTMLElement3Ptr(pp->item(l))->inflateBlock=VARIANT_TRUE;
}
return S_OK;
}
STDMETHOD(GetUUID)(BSTR *uid)
{
UUID uuid;
unsigned char *str;
if (UuidCreate(&uuid)==RPC_S_OK && UuidToStringA(&uuid,&str)==RPC_S_OK)
{
CString us(str);
RpcStringFreeA(&str);
us.MakeUpper();
*uid = us.AllocSysString();
return S_OK;
}
return S_FALSE;
}
STDMETHOD(GetNBSP)(BSTR *nbsp)
{
CString s_nbsp = _Settings.GetNBSPChar();
*nbsp = s_nbsp.AllocSysString();
return S_OK;
}
STDMETHOD(MsgBox)(BSTR message)
{
wchar_t cpt[MAX_LOAD_STRING + 1];
::LoadString(_Module.GetResourceInstance(), IDS_SCRIPT_MSG_CPT, cpt, MAX_LOAD_STRING);
MessageBoxW(GetActiveWindow(), message, cpt, MB_ICONINFORMATION|MB_OK);
return S_OK;
}
STDMETHOD(AskYesNo)(BSTR message, BOOL *pVal)
{
wchar_t cpt[MAX_LOAD_STRING + 1];
::LoadString(_Module.GetResourceInstance(), IDS_SCRIPT_MSG_CPT, cpt, MAX_LOAD_STRING);
if (IDYES == MessageBoxW(GetActiveWindow(), message, cpt, MB_ICONQUESTION|MB_YESNO))
{
*pVal = true;
}
else
{
*pVal = false;
}
return S_OK;
}
STDMETHOD(SaveBinary)(BSTR path, BSTR data, BOOL prompt, BOOL* ret)
{
INT_PTR modalResult = IDOK;
*ret = false;
CString file_name = CString(path);
if (prompt)
{
CString fname = ATLPath::FindFileName(file_name );
CString fpath(file_name);
fpath = fpath.Left(file_name.GetLength()-fname.GetLength());
CFileDialog imgSaveDlg(FALSE, NULL, fname);
imgSaveDlg.m_ofn.lpstrInitialDir = fpath;
// add file types
imgSaveDlg.m_ofn.lpstrFilter = L"JPEG files (*.jpg)\0*.jpg\0PNG files (*.png)\0*.png\0All files (*.*)\0*.*\0\0";
imgSaveDlg.m_ofn.nFilterIndex = 0;
imgSaveDlg.m_ofn.lpstrDefExt = L"jpg";
modalResult = imgSaveDlg.DoModal(NULL);
file_name.SetString(imgSaveDlg.m_szFileName);
}
if (modalResult == IDOK)
{
int len = SysStringByteLen(data);
void* buf = (void*)data;
HANDLE file = CreateFile(file_name, GENERIC_WRITE | FILE_WRITE_DATA, FILE_SHARE_WRITE, 0, CREATE_NEW, 0, 0);
if(INVALID_HANDLE_VALUE == file)
{
return S_OK;
}
DWORD writen = 0;
WriteFile(file, buf, len, &writen, 0);
CloseHandle(file);
*ret = true;
}
return S_OK;
}
STDMETHOD(GetExtendedStyle)(BSTR elem, BOOL* ext)
{
*ext = _Settings.GetExtElementStyle(elem);
return S_OK;
}
STDMETHOD(IsFastMode)(BOOL* ext)
{
*ext = _Settings.FastMode();
return S_OK;
}
STDMETHOD(DescShowElement)(BSTR elem, BOOL show)
{
_Settings.SetExtElementStyle(elem, show != 0);
return S_OK;
}
STDMETHOD(SetStyleEx)(IDispatch* doc, IDispatch* elem, BSTR style)
{
MSHTML::IHTMLElementPtr el = elem;
U::ChangeAttribute(el, L"class", style);
return S_OK;
}
STDMETHOD(GetImageDimsByPath)(BSTR path, BSTR* dims)
{
int nWidth, nHeight;
if(U::GetImageDimsByPath(path, &nWidth, &nHeight))
{
CString format;
format.Format(L"%dx%d", nWidth, nHeight);
*dims = format.AllocSysString();
}
else *dims = L"";
return S_OK;
}
STDMETHOD(GetImageDimsByData)(VARIANT* data, BSTR* dims)
{
int nWidth, nHeight;
SAFEARRAY* psa = data->parray;
long lUbound;
if(SafeArrayGetUBound(psa, 1, &lUbound) == S_OK && U::GetImageDimsByData(psa, lUbound, &nWidth, &nHeight))
{
CString format;
format.Format(L"%dx%d", nWidth, nHeight);
*dims = format.AllocSysString();
}
else *dims = L"";
return S_OK;
}
STDMETHOD(GetViewWidth)(int* width)
{
*width = _Settings.GetViewWidth();
return S_OK;
}
STDMETHOD(GetViewHeight)(int* height)
{
*height = _Settings.GetViewHeight();
return S_OK;
}
STDMETHOD(GetProgramVersion)(BSTR* ver)
{
CString version(build_name);
*ver = version.AllocSysString();
return S_OK;
}
STDMETHOD(InputBox)(BSTR prompt, BSTR title, BSTR value, BSTR* input)
{
CString sPrompt(prompt);
CString sTitle(title);
CString sInput(value);
modalResultCode = AU::InputBox (sInput, sTitle, sPrompt);
if (modalResultCode != IDYES) sInput.SetString(L"");
*input = sInput.AllocSysString();
return S_OK;
}
STDMETHOD(GetModalResult)(int* modalResult)
{
*modalResult = modalResultCode;
return S_OK;
}
STDMETHOD(SetStatusBarText)(BSTR text)
{
CString sbtext(text);
::SendMessage(_Settings.GetMainWindow(), AU::WM_SETSTATUSTEXT, 0, (LPARAM) (LPCTSTR) sbtext.GetBuffer());
return S_OK;
}
};
#endif