-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathview_win.cc
173 lines (137 loc) · 4.63 KB
/
view_win.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
#include "view.h"
#include <Awesomium/WebCore.h>
#include <Awesomium/STLHelpers.h>
#include <vector>
class ViewWin;
static bool g_is_initialized = false;
static std::vector<ViewWin*> g_active_views_;
const wchar_t szWindowClass[] = L"ViewWinClass";
const wchar_t szTitle[] = L"Application";
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
using namespace Awesomium;
class ViewWin : public View,
public WebViewListener::View {
public:
ViewWin(int width, int height) {
PlatformInit();
web_view_ = WebCore::instance()->CreateWebView(width,
height,
0,
Awesomium::kWebViewType_Window);
web_view_->set_view_listener(this);
// Create our WinAPI Window
HINSTANCE hInstance = GetModuleHandle(0);
hwnd_ = CreateWindow(szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width + 20,
height + 40,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd_)
exit(-1);
web_view_->set_parent_window(hwnd_);
ShowWindow(hwnd_, SW_SHOWNORMAL);
UpdateWindow(hwnd_);
SetTimer (hwnd_, 0, 15, NULL );
g_active_views_.push_back(this);
}
virtual ~ViewWin() {
for (std::vector<ViewWin*>::iterator i = g_active_views_.begin();
i != g_active_views_.end(); i++) {
if (*i == this) {
g_active_views_.erase(i);
break;
}
}
web_view_->Destroy();
}
HWND hwnd() { return hwnd_; }
static void PlatformInit() {
if (g_is_initialized)
return;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(0);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWindowClass;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
exit(-1);
}
g_is_initialized = true;
}
static ViewWin* GetFromHandle(HWND handle) {
for (std::vector<ViewWin*>::iterator i = g_active_views_.begin();
i != g_active_views_.end(); i++) {
if ((*i)->hwnd() == handle) {
return *i;
}
}
return NULL;
}
// Following methods are inherited from WebViewListener::View
virtual void OnChangeTitle(Awesomium::WebView* caller,
const Awesomium::WebString& title) {
std::string title_utf8(ToString(title));
std::wstring title_wide(title_utf8.begin(), title_utf8.end());
SetWindowText(hwnd_, title_wide.c_str());
}
virtual void OnChangeAddressBar(Awesomium::WebView* caller,
const Awesomium::WebURL& url) { }
virtual void OnChangeTooltip(Awesomium::WebView* caller,
const Awesomium::WebString& tooltip) { }
virtual void OnChangeTargetURL(Awesomium::WebView* caller,
const Awesomium::WebURL& url) { }
virtual void OnChangeCursor(Awesomium::WebView* caller,
Awesomium::Cursor cursor) { }
virtual void OnChangeFocus(Awesomium::WebView* caller,
Awesomium::FocusedElementType focused_type) { }
virtual void OnShowCreatedWebView(Awesomium::WebView* caller,
Awesomium::WebView* new_view,
const Awesomium::WebURL& opener_url,
const Awesomium::WebURL& target_url,
const Awesomium::Rect& initial_pos,
bool is_popup) { }
virtual void OnAddConsoleMessage(Awesomium::WebView* caller,
const Awesomium::WebString& message,
int line_number,
const Awesomium::WebString& source) { }
protected:
HWND hwnd_;
};
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
ViewWin* view = ViewWin::GetFromHandle(hWnd);
switch (message) {
case WM_COMMAND:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_TIMER:
break;
case WM_SIZE:
view->web_view()->Resize(LOWORD(lParam), HIWORD(lParam));
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_QUIT:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
View* View::Create(int width, int height) {
return new ViewWin(width, height);
}