-
Notifications
You must be signed in to change notification settings - Fork 1
/
CEFWebReader.cc
458 lines (339 loc) · 10.6 KB
/
CEFWebReader.cc
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "stdafx.h"
#include "CEFWebReader.h"
// WKEWebkitBrowser.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "CEFWebReader.h"
#include <strsafe.h>
#define E_GOFORWORD_TIMER 100
#define E_PROGRESS_TIMER 101
CEFWebReaderWnd* CEFWebReaderWnd::pCEFWebReaderWnd = NULL;
CEFWebReaderWnd::CEFWebReaderWnd()
{
pSearchEditCtrl_ = NULL;
pSearchCtrl_ = NULL;
pWKEWebkitCtrl_ = NULL;
pWebTabContainer_ = NULL;
pProgressCtrl_ = NULL;
Progress_state = 0;
DoSearch = false;
pCEFWebReaderWnd = this;
}
CEFWebReaderWnd::~CEFWebReaderWnd()
{
}
CControlUI* CEFWebReaderWnd::CreateControl(LPCTSTR pstrClassName)
{
if (_tcsicmp(pstrClassName, _T("CEFWebReader")) == 0)
{
return (pWKEWebkitCtrl_ = new CCEFWebkitUI(GetSafeHwnd()));
}
return NULL;
}
void CEFWebReaderWnd::OnFinalMessage(HWND hWnd)
{
__super::OnFinalMessage(hWnd);
}
LRESULT CEFWebReaderWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
::PostQuitMessage(0L);
return __super::OnDestroy(uMsg, wParam, lParam, bHandled);
}
//截取WM_CLOSE消息
LRESULT CEFWebReaderWnd::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
if ((pWKEWebkitCtrl_ != NULL) && (!pWKEWebkitCtrl_->IsClosed()))
{
//pWKEWebkitCtrl_->CloseAllPage();
bHandled = TRUE;
// Cancel the close.
}
else
{
bHandled = TRUE;
}
return 0;
}
void CEFWebReaderWnd::OnClick(TNotifyUI & msg)
{
CDuiString strName = msg.pSender->GetName();
if (_T("ui_btn_close") == strName)
{
if ((pWKEWebkitCtrl_ != NULL) && (!pWKEWebkitCtrl_->IsClosed()))
{
pWKEWebkitCtrl_->CloseAllPage();
//pWKEWebkitCtrl_->clientHandler_->CloseAllBrowsers(false);
}
}
__super::OnClick(msg);
}
void CEFWebReaderWnd::InitWindow()
{
pSearchEditCtrl_ = dynamic_cast<CEditUI*>(FindControl(_T("ui_et_search")));
pWebTabContainer_ = dynamic_cast<CHorizontalLayoutUI*>(FindControl(_T("ui_hl_urltabs")));
pSearchCtrl_ = dynamic_cast<CButtonUI*>(FindControl(_T("ui_btn_search")));
pProgressCtrl_ = static_cast<CProgressUI*>(FindControl(_T("ui_progress_load")));
pSearchComboCtrl_= dynamic_cast<CComboUI*>(FindControl(_T("ui_combo_search")));
if ( (!pWebTabContainer_) || (!pSearchCtrl_) || (!pSearchEditCtrl_) || (!pProgressCtrl_) || (!pSearchComboCtrl_))
{
MessageBox(GetSafeHwnd(), _T("控件初始化失败,检查界面"), _T("Err"), MB_OK);
PostQuitMessage(0);
}
}
void CEFWebReaderWnd::Search_mainfunc()
{
CefRefPtr<CefBrowser> pBrower = pWKEWebkitCtrl_->clientHandler_->browser_list_[0];
if (pBrower)
{
//pWKEWebkitCtrl_->NewPage(_T("file:///htmls/launch.html"));
//MessageBox(GetSafeHwnd(), _T("es pBrower"), _T("OnClick"), MB_OK);
//pBrower->GetMainFrame().get()->ExecuteJavaScript("alert('ExecuteJavaScript succeed!');", pBrower->GetMainFrame().get()->GetURL(), 0);
//pBrower->GetMainFrame().get()->ExecuteJavaScript("window.location.href='http://www.baidu.com'", pBrower->GetMainFrame().get()->GetURL(), 0);
//pBrower->GetMainFrame().get()->ExecuteJavaScript("Change_Func();", pBrower->GetMainFrame().get()->GetURL(), 0);
//pBrower->GetMainFrame().get()->ExecuteJavaScript("$.ajax({url: '//www.yzwiki.com/api.php',data : { action: 'query', list : 'search', srsearch : 'A72', format : 'json' },dataType : 'json',success : function(x) {alert(x.query.search[0].title);}});", pBrower->GetMainFrame().get()->GetURL(), 0);
//pBrower->GetMainFrame().get()->ExecuteJavaScript("var ajaxUrl = 'https://www.yzwiki.com/api.php?action=query&format=json&list=search&srsearch=A72&srlimit=10&srenablerewrites=1';$.ajax({type:'get',url:ajaxUrl,cache:false,dataType:'jsonp',jsonp:'jsonpCallback',jsonpCallback:'callback',success:function(returnData){alert(returnData);},error:function(){alert('error');}});", pBrower->GetMainFrame().get()->GetURL(), 0);
//wstring cmd_str = _T("$.ajax({type: 'GET',url: 'https://www.yzwiki.com/api.php?',dataType : 'jsonp',data : { action: 'opensearch', search : '");
//cmd_str += pSearchEditCtrl_->GetText().GetData();
//cmd_str += _T("', format : 'json' },success : function(result) {alert(result[0]);},error : function(xhr, errorText) {alert('Error ' + xhr.responseText);}});");
pBrower->GetMainFrame()->LoadURL(_T("file:///htmls/launch.html"));
//pBrower->GetMainFrame().get()->ExecuteJavaScript("window.location.href='file:///htmls/launch.html'", pBrower->GetMainFrame().get()->GetURL(), 0);
//pWKEWebkitCtrl_->clientHandler_->browser_list_[0]->Reload();
DoSearch = true;
//wstring cmd_str = _T("Search_Wiki_ajaxjsonp('");
//cmd_str += pSearchEditCtrl_->GetText().GetData();
//cmd_str += _T("');");
//pBrower->GetMainFrame().get()->ExecuteJavaScript(cmd_str, pBrower->GetMainFrame().get()->GetURL(), 0);
//pWKEWebkitCtrl_->NewPage(_T("https://www.yzwiki.com/item/API"));
}
}
void CEFWebReaderWnd::Notify(TNotifyUI& msg)
{
if (msg.sType == DUI_MSGTYPE_CLICK)
{
if (msg.pSender->GetName() == _T("ui_btn_search"))
{
Search_mainfunc();
}
}
//需要关闭richedit的want return属性
if (msg.sType == DUI_MSGTYPE_RETURN)
{
Search_mainfunc();
}
else if (msg.sType == _T("windowinit"))
{
OnInitComplate();
}
else if (msg.sType == DUI_MSGTYPE_TIMER)
{
if (msg.pSender == pWKEWebkitCtrl_)
{
if (msg.wParam == E_GOFORWORD_TIMER)
{
SwitchUIState();
//MessageBox(GetSafeHwnd(), _T("123"), _T("222"), MB_OK);
}
else if (msg.wParam == E_PROGRESS_TIMER)
{
SwitchProgressUIState();
}
}
}
__super::Notify(msg);
}
LRESULT CEFWebReaderWnd::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
//TCHAR szBuf[256];
switch (uMsg)
{
case UM_CEF_POSTQUITMESSAGE:
bHandled = TRUE;
//Sleep(2000);
// CefQuitMessageLoop();
PostQuitMessage(0L);
break;
case UM_CEF_WEBLOADPOPUP:
{
CefString* pStrComplateURL = (CefString*)lParam;
if (pStrComplateURL != NULL)
{
pWKEWebkitCtrl_->NewPage(*pStrComplateURL);
delete pStrComplateURL;
}
}
break;
case UM_CEF_WEBLOADEND:
{
OnWebLoadEnd(wParam, lParam);
}
break;
case UM_CEF_WEBLOADSTART:
{
OnWebLoadStart(wParam, lParam);
}
break;
case UM_CEF_WEBTITLECHANGE:
{
CefString* pStrComplateURL = (CefString*)lParam;
if (pStrComplateURL != NULL)
{
OnTitleChanged(wParam, *pStrComplateURL);
delete pStrComplateURL;
}
}
break;
case UM_CEF_AFTERCREATED:
{
OnAfterCreate(wParam);
}
break;
case UM_CEF_BROWSERCLOSE:
{
OnBrowserClose(wParam);
}
break;
default:
break;
}
bHandled = FALSE;
return 0;
}
void CEFWebReaderWnd::OnInitComplate()
{
if (pWKEWebkitCtrl_)
{
//pWKEWebkitCtrl_->NewPage(_T("https://www.yzwiki.com/item/Main_Page"));
//pWKEWebkitCtrl_->NewPage(_T("file:///htmls/index.html"));
//pWKEWebkitCtrl_->NewPage(_T("https://www.yzwiki.com/item/API"));
pWKEWebkitCtrl_->NewPage(_T("file:///htmls/launch.html"));
}
GetPaintManager()->SetTimer(pWKEWebkitCtrl_, E_GOFORWORD_TIMER, 500);
GetPaintManager()->SetTimer(pWKEWebkitCtrl_, E_PROGRESS_TIMER, 20);
}
void CEFWebReaderWnd::OnTitleChanged(int nID, const CefString str)
{
return;
}
void CEFWebReaderWnd::OnAfterCreate(int nWebBrowserID)
{
// 创建标签OPTION按钮
//CDuiString sAttr;
COptionUI* pOpt = new COptionUI;
//sAttr.Format(_T("name=\"webtab\" width=\"100\" height=\"30\" normalimage=\"tabbar_normal.png\" hotimage=\"tabbar_hover.png\" \
// pushedimage=\"tabbar_pushed.png\" selectedimage=\"tabbar_pushed.png\" group=\"ui_grp_webtab\""));
//pOpt->ApplyAttributeList(sAttr);
int iCount = pWebTabContainer_->GetCount();
pWebTabContainer_->AddAt(pOpt, iCount - 1);
// 把标签按钮和浏览器id关联起来
COptionTag* pTag = new COptionTag(nWebBrowserID);
pWKEWebkitCtrl_->ReFresh(pTag->nID_);
//pOpt->SetTag((UINT_PTR)pTag);
//pOpt->Selected(true);
}
void CEFWebReaderWnd::OnBrowserClose(int nBrowserID)
{
TCHAR strDebugMsg[128];
int nCountWebtab = pWebTabContainer_->GetCount();
for (int idx = 0; idx < nCountWebtab; idx++)
{
COptionUI* pOpt = (COptionUI*)pWebTabContainer_->GetItemAt(idx);
COptionTag* pTag = (COptionTag*)pOpt->GetTag();
if ((pTag != NULL) && (pTag->nID_ == nBrowserID))
{
int nSize = pWebTabContainer_->GetCount();
int nIdx = pWebTabContainer_->GetItemIndex(pOpt);
pWebTabContainer_->Remove(pOpt);
if (nIdx != -1)
{
if (nIdx == (nSize - 2))
{
--nIdx;
}
COptionUI* pShilfOption = dynamic_cast<COptionUI*>(pWebTabContainer_->GetItemAt(nIdx));
COptionTag* pShilfTag = (COptionTag*)pShilfOption->GetTag();
if ((pShilfOption) && (pShilfTag != NULL))
{
pWKEWebkitCtrl_->ReFresh(pShilfTag->nID_);
//pURLEditCtrl_->SetText(pWKEWebkitCtrl_->GetFinalURL(pShilfTag->nID_).c_str());
pShilfOption->Selected(true);
}
}
break;
}
}
StringCbPrintf(strDebugMsg, sizeof(strDebugMsg), _T("delete id=%d\n"), nBrowserID);
OutputDebugString(strDebugMsg);
}
void CEFWebReaderWnd::OnWebLoadEnd(WPARAM wParam, LPARAM lParam)
{
Progress_state = 2;
}
void CEFWebReaderWnd::OnWebLoadStart(WPARAM wParam, LPARAM lParam)
{
Progress_state = 1;
}
COptionUI * CEFWebReaderWnd::GetActiveOption() const
{
COptionUI* pCurrentOpt = NULL;
int nCountWebtab = pWebTabContainer_->GetCount();
for (int idx = 0; idx < nCountWebtab; idx++)
{
COptionUI* pOpt = (COptionUI*)pWebTabContainer_->GetItemAt(idx);
if (pOpt->IsSelected())
{
pCurrentOpt = pOpt;
}
}
return pCurrentOpt;
}
void CEFWebReaderWnd::SwitchUIState()
{
if (DoSearch)
{
CefRefPtr<CefBrowser> pBrower = pWKEWebkitCtrl_->clientHandler_->browser_list_[0];
if (pBrower)
{
int nsel = pSearchComboCtrl_->GetCurSel();
CListLabelElementUI *pItemEle = (CListLabelElementUI *)pSearchComboCtrl_->GetItemAt(nsel);
CDuiString strText = pItemEle->GetText().GetData();
//if (wcscmp(strText, L" 常规")==0)strText = _T("");
if (strText == _T(" 词条"))strText = _T("");
if (strText == _T(" 用户"))strText = _T("User:");
if (strText == _T(" 分类"))strText = _T("Category:");
if (strText == _T(" 模板"))strText = _T("Template:");
if (strText == _T(" 站务"))strText = _T("A72Wiki:");
if (strText == _T(" 模拟器"))strText = _T("Emulator:");
DoSearch = false;
wstring cmd_str = _T("Search_Wiki_ajaxjsonp('");
cmd_str += strText;
cmd_str += pSearchEditCtrl_->GetText().GetData();
cmd_str += _T("');");
pBrower->GetMainFrame().get()->ExecuteJavaScript(cmd_str, pBrower->GetMainFrame().get()->GetURL(), 0);
}
}
}
void CEFWebReaderWnd::SwitchProgressUIState()
{
if (Progress_state == 0)
{
pProgressCtrl_->SetValue(0);
}
else if (Progress_state == 1)
{
int c_pos = pProgressCtrl_->GetValue();
if (c_pos < 95)
{
pProgressCtrl_->SetValue(c_pos + 1);
}
}
else if (Progress_state == 2)
{
int c_pos = pProgressCtrl_->GetValue();
if (c_pos < 95)c_pos = 95;
if (c_pos < 100)
{
pProgressCtrl_->SetValue(c_pos + 1);
}
else Progress_state = 0;
}
}