-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapp.cpp
More file actions
219 lines (177 loc) · 5.21 KB
/
app.cpp
File metadata and controls
219 lines (177 loc) · 5.21 KB
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
#include "app.h"
#include "cmdline.h"
#include "path.h"
#include <wke.h>
#include <stdio.h>
#include <stdlib.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib")
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
BOOL FixupHtmlFileUrl(LPCWSTR pathOption, LPWSTR urlBuffer, size_t bufferSize)
{
WCHAR htmlPath[MAX_PATH + 1] = { 0 };
if (pathOption[0] == 0)
{
do
{
GetWorkingPath(htmlPath, MAX_PATH, L"index.html");
if (PathFileExistsW(htmlPath))
break;
GetWorkingPath(htmlPath, MAX_PATH, L"main.html");
if (PathFileExistsW(htmlPath))
break;
GetWorkingPath(htmlPath, MAX_PATH, L"wkexe.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"index.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"main.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"wkexe.html");
if (PathFileExistsW(htmlPath))
break;
return FALSE;
}
while (0);
swprintf_s(urlBuffer, bufferSize, L"file:///%s", htmlPath);
return TRUE;
}
else//if (!wcsstr(pathOption, L"://"))
{
do
{
GetWorkingPath(htmlPath, MAX_PATH, pathOption);
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, pathOption);
if (PathFileExistsW(htmlPath))
break;
return FALSE;
}
while (0);
swprintf_s(urlBuffer, bufferSize, L"file:///%s", htmlPath);
return TRUE;
}
return FALSE;
}
BOOL FixupHtmlUrl(Application* app)
{
LPWSTR htmlOption = app->options.htmlFile;
WCHAR htmlUrl[MAX_PATH + 1] = { 0 };
// 包含 :// 说明是完整的URL
if (wcsstr(htmlOption, L"://"))
{
wcsncpy_s(app->url, MAX_PATH, htmlOption, MAX_PATH);
return TRUE;
}
// 若不是完整URL,补全之
if (FixupHtmlFileUrl(htmlOption, htmlUrl, MAX_PATH))
{
wcsncpy_s(app->url, MAX_PATH, htmlUrl, MAX_PATH);
return TRUE;
}
// 无法获得完整的URL,出错
return FALSE;
}
BOOL ProcessOptions(Application* app)
{
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
ParseOptions(argc, argv, &app->options);
LocalFree(argv);
return TRUE;
}
// 回调:点击了关闭、返回 true 将销毁窗口,返回 false 什么都不做。
bool HandleWindowClosing(wkeWebView webWindow, void* param)
{
Application* app = (Application*)param;
return IDYES == MessageBoxW(NULL, L"确定要退出程序吗?", L"wkexe", MB_YESNO|MB_ICONQUESTION);
}
// 回调:窗口已销毁
void HandleWindowDestroy(wkeWebView webWindow, void* param)
{
Application* app = (Application*)param;
app->window = NULL;
PostQuitMessage(0);
}
// 回调:文档加载成功
void HandleDocumentReady(wkeWebView webWindow, void* param)
{
wkeShowWindow(webWindow, TRUE);
}
// 回调:页面标题改变
void HandleTitleChanged(wkeWebView webWindow, void* param, const wkeString title)
{
wkeSetWindowTitleW(webWindow, wkeGetStringW(title));
}
// 回调:创建新的页面,比如说调用了 window.open 或者点击了 <a target="_blank" .../>
wkeWebView HandleCreateView(wkeWebView webWindow, void* param, wkeNavigationType navType, const wkeString url, const wkeWindowFeatures* features)
{
wkeWebView newWindow = wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, NULL, features->x, features->y, features->width, features->height);
wkeShowWindow(newWindow, true);
return newWindow;
}
// 创建主页面窗口
BOOL CreateWebWindow(Application* app)
{
if (app->options.transparent)
app->window = wkeCreateWebWindow(WKE_WINDOW_TYPE_TRANSPARENT, NULL, 0, 0, 640, 480);
else
app->window = wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, NULL, 0, 0, 640, 480);
if (!app->window)
return FALSE;
wkeOnWindowClosing(app->window, HandleWindowClosing, app);
wkeOnWindowDestroy(app->window, HandleWindowDestroy, app);
wkeOnDocumentReady(app->window, HandleDocumentReady, app);
wkeOnTitleChanged(app->window, HandleTitleChanged, app);
wkeOnCreateView(app->window, HandleCreateView, app);
wkeMoveToCenter(app->window);
wkeLoadURLW(app->window, app->url);
return TRUE;
}
void PrintHelpAndQuit(Application* app)
{
PrintHelp();
PostQuitMessage(0);
}
void RunMessageLoop(Application* app)
{
MSG msg = { 0 };
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
void RunApplication(Application* app)
{
memset(app, 0, sizeof(Application));
if (!ProcessOptions(app))
{
PrintHelpAndQuit(app);
return;
}
if (!FixupHtmlUrl(app))
{
PrintHelpAndQuit(app);
//打开默认页面
wcsncpy_s(app->url, MAX_PATH, L"http://www.baidu.com", MAX_PATH);
}
if (!CreateWebWindow(app))
{
PrintHelpAndQuit(app);
return;
}
RunMessageLoop(app);
}
void QuitApplication(Application* app)
{
if (app->window)
{
wkeDestroyWebWindow(app->window);
app->window = NULL;
}
}