|
1 | | -// Copyright (c) 2008 The Chromium Embedded Framework Authors. |
2 | | -// Portions copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
3 | | -// Use of this source code is governed by a BSD-style license that can be |
4 | | -// found in the LICENSE file. |
| 1 | +// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights |
| 2 | +// reserved. Use of this source code is governed by a BSD-style license that |
| 3 | +// can be found in the LICENSE file. |
5 | 4 |
|
6 | 5 | #include "cefclient/clientplugin.h" |
7 | 6 |
|
8 | 7 | #if defined(OS_WIN) |
9 | 8 |
|
10 | | -// Initialized in NP_Initialize. |
11 | | -NPNetscapeFuncs* g_browser = NULL; |
| 9 | +#include <windows.h> |
12 | 10 |
|
13 | 11 | namespace { |
14 | 12 |
|
15 | | -NPError NPP_ClientNew(NPMIMEType plugin_type, NPP instance, uint16_t mode, |
16 | | - int16_t argc, char* argn[], char* argv[], NPSavedData* saved) { |
17 | | - if (instance == NULL) |
18 | | - return NPERR_INVALID_INSTANCE_ERROR; |
| 13 | +// Client plugin window implementation. |
| 14 | +class ClientPlugin { |
| 15 | + public: |
| 16 | + ClientPlugin() |
| 17 | + : hwnd_(NULL) { |
| 18 | + } |
19 | 19 |
|
20 | | - ClientPlugin* plugin_impl = new ClientPlugin(mode); |
21 | | - plugin_impl->Initialize(GetModuleHandle(NULL), instance, plugin_type, argc, |
22 | | - argn, argv); |
23 | | - instance->pdata = reinterpret_cast<void*>(plugin_impl); |
24 | | - return NPERR_NO_ERROR; |
25 | | -} |
| 20 | + ~ClientPlugin() { |
| 21 | + if (IsWindow(hwnd_)) |
| 22 | + DestroyWindow(hwnd_); |
| 23 | + } |
26 | 24 |
|
27 | | -NPError NPP_ClientDestroy(NPP instance, NPSavedData** save) { |
28 | | - ClientPlugin* plugin_impl = reinterpret_cast<ClientPlugin*>(instance->pdata); |
| 25 | + void Initialize(HWND parent_hwnd) { |
| 26 | + if (hwnd_ != NULL) |
| 27 | + return; |
| 28 | + |
| 29 | + HINSTANCE hInstance = GetModuleHandle(NULL); |
| 30 | + |
| 31 | + static bool class_registered = false; |
| 32 | + if (!class_registered) { |
| 33 | + // Register the window class. |
| 34 | + WNDCLASS wc = {0}; |
| 35 | + wc.style = CS_OWNDC; |
| 36 | + wc.lpfnWndProc = PluginWndProc; |
| 37 | + wc.cbClsExtra = 0; |
| 38 | + wc.cbWndExtra = 0; |
| 39 | + wc.hInstance = hInstance; |
| 40 | + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 41 | + wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 42 | + wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 43 | + wc.lpszMenuName = NULL; |
| 44 | + wc.lpszClassName = L"ClientPlugin"; |
| 45 | + RegisterClass(&wc); |
| 46 | + |
| 47 | + class_registered = true; |
| 48 | + } |
| 49 | + |
| 50 | + // Create the main window. |
| 51 | + hwnd_ = CreateWindow(L"ClientPlugin", L"Client Plugin", |
| 52 | + WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 0, 0, parent_hwnd, |
| 53 | + NULL, hInstance, NULL); |
| 54 | + |
| 55 | + SetWindowLongPtr(hwnd_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
| 56 | + |
| 57 | + // Size and display the plugin window. |
| 58 | + RECT parent_rect; |
| 59 | + GetClientRect(parent_hwnd, &parent_rect); |
| 60 | + SetWindowPos(hwnd_, NULL, parent_rect.left, parent_rect.top, |
| 61 | + parent_rect.right - parent_rect.left, |
| 62 | + parent_rect.bottom - parent_rect.top, SWP_SHOWWINDOW); |
| 63 | + } |
29 | 64 |
|
30 | | - if (plugin_impl) { |
31 | | - plugin_impl->Shutdown(); |
32 | | - delete plugin_impl; |
| 65 | + void EraseBackground(HDC hdc) { |
| 66 | + RECT erase_rect; |
| 67 | + GetClipBox(hdc, &erase_rect); |
| 68 | + HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); |
| 69 | + FillRect(hdc, &erase_rect, brush); |
| 70 | + DeleteObject(brush); |
33 | 71 | } |
34 | 72 |
|
35 | | - return NPERR_NO_ERROR; |
36 | | -} |
| 73 | + void Paint(HDC hdc) { |
| 74 | + static LPCWSTR text = L"Left click in the green area for a message box!"; |
37 | 75 |
|
38 | | -NPError NPP_ClientSetWindow(NPP instance, NPWindow* window_info) { |
39 | | - if (instance == NULL) |
40 | | - return NPERR_INVALID_INSTANCE_ERROR; |
| 76 | + RECT client_rect; |
| 77 | + GetClientRect(hwnd_, &client_rect); |
41 | 78 |
|
42 | | - if (window_info == NULL) |
43 | | - return NPERR_GENERIC_ERROR; |
| 79 | + int old_mode = SetBkMode(hdc, TRANSPARENT); |
| 80 | + COLORREF old_color = SetTextColor(hdc, RGB(0, 0, 255)); |
44 | 81 |
|
45 | | - ClientPlugin* plugin_impl = reinterpret_cast<ClientPlugin*>(instance->pdata); |
| 82 | + RECT text_rect = client_rect; |
| 83 | + DrawText(hdc, text, -1, &text_rect, DT_CENTER | DT_CALCRECT); |
46 | 84 |
|
47 | | - if (plugin_impl == NULL) |
48 | | - return NPERR_GENERIC_ERROR; |
| 85 | + client_rect.top = ((client_rect.bottom - client_rect.top) - |
| 86 | + (text_rect.bottom - text_rect.top)) / 2; |
| 87 | + DrawText(hdc, text, -1, &client_rect, DT_CENTER); |
49 | 88 |
|
50 | | - HWND window_handle = reinterpret_cast<HWND>(window_info->window); |
51 | | - if (!plugin_impl->SetWindow(window_handle)) { |
52 | | - delete plugin_impl; |
53 | | - return NPERR_GENERIC_ERROR; |
| 89 | + SetBkMode(hdc, old_mode); |
| 90 | + SetTextColor(hdc, old_color); |
54 | 91 | } |
55 | 92 |
|
56 | | - return NPERR_NO_ERROR; |
57 | | -} |
| 93 | + HWND GetWindow() const { return hwnd_; } |
| 94 | + |
| 95 | + // Plugin window procedure. |
| 96 | + static LRESULT CALLBACK PluginWndProc(HWND hwnd_, UINT message, WPARAM wParam, |
| 97 | + LPARAM lParam) { |
| 98 | + ClientPlugin* plugin = |
| 99 | + reinterpret_cast<ClientPlugin*>(GetWindowLongPtr(hwnd_, GWLP_USERDATA)); |
| 100 | + |
| 101 | + switch (message) { |
| 102 | + case WM_DESTROY: |
| 103 | + return 0; |
| 104 | + |
| 105 | + case WM_LBUTTONDOWN: |
| 106 | + MessageBox(plugin->GetWindow(), |
| 107 | + L"You clicked on the client plugin!", L"Client Plugin", MB_OK); |
| 108 | + return 0; |
| 109 | + |
| 110 | + case WM_PAINT: { |
| 111 | + PAINTSTRUCT ps; |
| 112 | + BeginPaint(hwnd_, &ps); |
| 113 | + plugin->Paint(ps.hdc); |
| 114 | + EndPaint(hwnd_, &ps); |
| 115 | + return 0; |
| 116 | + } |
| 117 | + |
| 118 | + case WM_PRINTCLIENT: |
| 119 | + plugin->Paint(reinterpret_cast<HDC>(wParam)); |
| 120 | + return 0; |
| 121 | + |
| 122 | + case WM_ERASEBKGND: |
| 123 | + plugin->EraseBackground(reinterpret_cast<HDC>(wParam)); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + |
| 127 | + return DefWindowProc(hwnd_, message, wParam, lParam); |
| 128 | + } |
58 | 129 |
|
59 | | -} // namespace |
| 130 | + private: |
| 131 | + HWND hwnd_; |
| 132 | +}; |
60 | 133 |
|
61 | | -NPError API_CALL NP_ClientGetEntryPoints(NPPluginFuncs* pFuncs) { |
62 | | - pFuncs->newp = NPP_ClientNew; |
63 | | - pFuncs->destroy = NPP_ClientDestroy; |
64 | | - pFuncs->setwindow = NPP_ClientSetWindow; |
65 | | - return NPERR_NO_ERROR; |
66 | | -} |
| 134 | +NPError NPP_NewImpl(NPMIMEType plugin_type, NPP instance, uint16 mode, |
| 135 | + int16 argc, char* argn[], char* argv[], |
| 136 | + NPSavedData* saved) { |
| 137 | + if (instance == NULL) |
| 138 | + return NPERR_INVALID_INSTANCE_ERROR; |
67 | 139 |
|
68 | | -NPError API_CALL NP_ClientInitialize(NPNetscapeFuncs* pFuncs) { |
69 | | - g_browser = pFuncs; |
70 | | - return NPERR_NO_ERROR; |
71 | | -} |
| 140 | + ClientPlugin* plugin = new ClientPlugin(); |
| 141 | + instance->pdata = reinterpret_cast<void*>(plugin); |
72 | 142 |
|
73 | | -NPError API_CALL NP_ClientShutdown(void) { |
74 | | - g_browser = NULL; |
75 | 143 | return NPERR_NO_ERROR; |
76 | 144 | } |
77 | 145 |
|
| 146 | +NPError NPP_DestroyImpl(NPP instance, NPSavedData** save) { |
| 147 | + ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata); |
| 148 | + if (plugin) |
| 149 | + delete plugin; |
78 | 150 |
|
79 | | -// ClientPlugin Implementation |
80 | | - |
81 | | -ClientPlugin::ClientPlugin(int16 mode) |
82 | | - : mode_(mode) { |
83 | | -} |
84 | | - |
85 | | -ClientPlugin::~ClientPlugin() { |
86 | | -} |
87 | | - |
88 | | -bool ClientPlugin::Initialize(HINSTANCE module_handle, NPP instance, |
89 | | - NPMIMEType mime_type, int16 argc, char* argn[], |
90 | | - char* argv[]) { |
91 | | - RefreshDisplay(); |
92 | | - return true; |
| 151 | + return NPERR_NO_ERROR; |
93 | 152 | } |
94 | 153 |
|
95 | | -bool ClientPlugin::SetWindow(HWND parent_window) { |
96 | | - if (!::IsWindow(parent_window)) { |
97 | | - // No window created yet. Ignore this call. |
98 | | - if (!IsWindow()) |
99 | | - return true; |
100 | | - // Parent window has been destroyed. |
101 | | - Shutdown(); |
102 | | - return true; |
103 | | - } |
104 | | - |
105 | | - RECT parent_rect; |
106 | | - |
107 | | - if (IsWindow()) { |
108 | | - ::GetClientRect(parent_window, &parent_rect); |
109 | | - SetWindowPos(NULL, &parent_rect, SWP_SHOWWINDOW); |
110 | | - return true; |
111 | | - } |
112 | | - // First time in -- no window created by plugin yet. |
113 | | - ::GetClientRect(parent_window, &parent_rect); |
114 | | - Create(parent_window, parent_rect, NULL, WS_CHILD | WS_BORDER); |
115 | | - |
116 | | - UpdateWindow(); |
117 | | - ShowWindow(SW_SHOW); |
118 | | - |
119 | | - return true; |
120 | | -} |
| 154 | +NPError NPP_SetWindowImpl(NPP instance, NPWindow* window_info) { |
| 155 | + if (instance == NULL) |
| 156 | + return NPERR_INVALID_INSTANCE_ERROR; |
121 | 157 |
|
122 | | -void ClientPlugin::Shutdown() { |
123 | | - if (IsWindow()) { |
124 | | - DestroyWindow(); |
125 | | - } |
126 | | -} |
| 158 | + if (window_info == NULL) |
| 159 | + return NPERR_GENERIC_ERROR; |
127 | 160 |
|
128 | | -LRESULT ClientPlugin::OnPaint(UINT message, WPARAM wparam, LPARAM lparam, |
129 | | - BOOL& handled) { |
130 | | - PAINTSTRUCT paint_struct; |
131 | | - BeginPaint(&paint_struct); |
132 | | - Paint(paint_struct.hdc); |
133 | | - EndPaint(&paint_struct); |
134 | | - return 0; |
135 | | -} |
| 161 | + ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata); |
| 162 | + HWND parent_hwnd = reinterpret_cast<HWND>(window_info->window); |
| 163 | + plugin->Initialize(parent_hwnd); |
136 | 164 |
|
137 | | -// PrintClient is necessary to support off-screen rendering. |
138 | | -LRESULT ClientPlugin::OnPrintClient(UINT message, WPARAM wparam, LPARAM lparam, |
139 | | - BOOL& handled) { |
140 | | - Paint(reinterpret_cast<HDC>(wparam)); |
141 | | - return 0; |
| 165 | + return NPERR_NO_ERROR; |
142 | 166 | } |
143 | 167 |
|
144 | | -LRESULT ClientPlugin::OnEraseBackGround(UINT message, WPARAM wparam, |
145 | | - LPARAM lparam, BOOL& handled) { |
146 | | - HDC paint_device_context = reinterpret_cast<HDC>(wparam); |
147 | | - RECT erase_rect; |
148 | | - GetClipBox(paint_device_context, &erase_rect); |
149 | | - HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); |
150 | | - FillRect(paint_device_context, &erase_rect, brush); |
151 | | - DeleteObject(brush); |
152 | | - return 1; |
153 | | -} |
| 168 | +} // namespace |
154 | 169 |
|
155 | | -LRESULT ClientPlugin::OnLButtonDown(UINT message, WPARAM wparam, LPARAM lparam, |
156 | | - BOOL& handled) { |
157 | | - MessageBox(L"You clicked on the client plugin!", L"Client Plugin", MB_OK); |
158 | | - return 0; |
| 170 | +NPError API_CALL NP_ClientGetEntryPoints(NPPluginFuncs* pFuncs) { |
| 171 | + pFuncs->newp = NPP_NewImpl; |
| 172 | + pFuncs->destroy = NPP_DestroyImpl; |
| 173 | + pFuncs->setwindow = NPP_SetWindowImpl; |
| 174 | + return NPERR_NO_ERROR; |
159 | 175 | } |
160 | 176 |
|
161 | | -void ClientPlugin::RefreshDisplay() { |
162 | | - if (!IsWindow()) |
163 | | - return; |
164 | | - |
165 | | - InvalidateRect(NULL, TRUE); |
166 | | - UpdateWindow(); |
| 177 | +NPError API_CALL NP_ClientInitialize(NPNetscapeFuncs* pFuncs) { |
| 178 | + return NPERR_NO_ERROR; |
167 | 179 | } |
168 | 180 |
|
169 | | -void ClientPlugin::Paint(HDC hdc) { |
170 | | - static LPCWSTR text = L"Left click in the green area for a message box!"; |
171 | | - |
172 | | - RECT client_rect; |
173 | | - GetClientRect(&client_rect); |
174 | | - |
175 | | - int old_mode = SetBkMode(hdc, TRANSPARENT); |
176 | | - COLORREF old_color = SetTextColor(hdc, RGB(0, 0, 255)); |
177 | | - |
178 | | - RECT text_rect = client_rect; |
179 | | - DrawText(hdc, text, -1, &text_rect, DT_CENTER | DT_CALCRECT); |
180 | | - |
181 | | - client_rect.top = ((client_rect.bottom - client_rect.top) |
182 | | - - (text_rect.bottom - text_rect.top)) / 2; |
183 | | - DrawText(hdc, text, -1, &client_rect, DT_CENTER); |
184 | | - |
185 | | - SetBkMode(hdc, old_mode); |
186 | | - SetTextColor(hdc, old_color); |
| 181 | +NPError API_CALL NP_ClientShutdown(void) { |
| 182 | + return NPERR_NO_ERROR; |
187 | 183 | } |
188 | 184 |
|
189 | 185 | #endif // OS_WIN |
0 commit comments