2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2008 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/clientplugin.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#if defined(OS_WIN)
|
2010-10-28 22:38:27 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Initialized in NP_Initialize.
|
|
|
|
NPNetscapeFuncs* g_browser = NULL;
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
NPError NPP_ClientNew(NPMIMEType plugin_type, NPP instance, uint16_t mode,
|
|
|
|
int16_t argc, char* argn[], char* argv[], NPSavedData* saved) {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
|
|
|
ClientPlugin* plugin_impl = new ClientPlugin(mode);
|
|
|
|
plugin_impl->Initialize(GetModuleHandle(NULL), instance, plugin_type, argc,
|
|
|
|
argn, argv);
|
|
|
|
instance->pdata = reinterpret_cast<void*>(plugin_impl);
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
NPError NPP_ClientDestroy(NPP instance, NPSavedData** save) {
|
2010-10-03 23:04:50 +02:00
|
|
|
ClientPlugin* plugin_impl = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
|
|
|
|
|
|
|
if (plugin_impl) {
|
|
|
|
plugin_impl->Shutdown();
|
|
|
|
delete plugin_impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
NPError NPP_ClientSetWindow(NPP instance, NPWindow* window_info) {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
|
|
|
if (window_info == NULL)
|
|
|
|
return NPERR_GENERIC_ERROR;
|
|
|
|
|
|
|
|
ClientPlugin* plugin_impl = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
|
|
|
|
|
|
|
if (plugin_impl == NULL)
|
|
|
|
return NPERR_GENERIC_ERROR;
|
|
|
|
|
|
|
|
HWND window_handle = reinterpret_cast<HWND>(window_info->window);
|
|
|
|
if (!plugin_impl->SetWindow(window_handle)) {
|
|
|
|
delete plugin_impl;
|
|
|
|
return NPERR_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-05-16 18:56:12 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_ClientGetEntryPoints(NPPluginFuncs* pFuncs) {
|
2011-05-16 18:56:12 +02:00
|
|
|
pFuncs->newp = NPP_ClientNew;
|
|
|
|
pFuncs->destroy = NPP_ClientDestroy;
|
|
|
|
pFuncs->setwindow = NPP_ClientSetWindow;
|
2010-10-03 23:04:50 +02:00
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_ClientInitialize(NPNetscapeFuncs* pFuncs) {
|
2010-10-03 23:04:50 +02:00
|
|
|
g_browser = pFuncs;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_ClientShutdown(void) {
|
2010-10-03 23:04:50 +02:00
|
|
|
g_browser = NULL;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ClientPlugin Implementation
|
|
|
|
|
|
|
|
ClientPlugin::ClientPlugin(int16 mode)
|
2012-01-10 00:46:23 +01:00
|
|
|
: mode_(mode) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin::~ClientPlugin() {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientPlugin::Initialize(HINSTANCE module_handle, NPP instance,
|
|
|
|
NPMIMEType mime_type, int16 argc, char* argn[],
|
2012-01-10 00:46:23 +01:00
|
|
|
char* argv[]) {
|
2010-10-03 23:04:50 +02:00
|
|
|
RefreshDisplay();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool ClientPlugin::SetWindow(HWND parent_window) {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (!::IsWindow(parent_window)) {
|
|
|
|
// No window created yet. Ignore this call.
|
|
|
|
if (!IsWindow())
|
|
|
|
return true;
|
|
|
|
// Parent window has been destroyed.
|
|
|
|
Shutdown();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RECT parent_rect;
|
|
|
|
|
|
|
|
if (IsWindow()) {
|
|
|
|
::GetClientRect(parent_window, &parent_rect);
|
|
|
|
SetWindowPos(NULL, &parent_rect, SWP_SHOWWINDOW);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// First time in -- no window created by plugin yet.
|
|
|
|
::GetClientRect(parent_window, &parent_rect);
|
|
|
|
Create(parent_window, parent_rect, NULL, WS_CHILD | WS_BORDER);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
UpdateWindow();
|
|
|
|
ShowWindow(SW_SHOW);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void ClientPlugin::Shutdown() {
|
2010-10-03 23:04:50 +02:00
|
|
|
if (IsWindow()) {
|
|
|
|
DestroyWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT ClientPlugin::OnPaint(UINT message, WPARAM wparam, LPARAM lparam,
|
2012-01-10 00:46:23 +01:00
|
|
|
BOOL& handled) {
|
2010-10-03 23:04:50 +02:00
|
|
|
PAINTSTRUCT paint_struct;
|
|
|
|
BeginPaint(&paint_struct);
|
2011-03-24 21:36:47 +01:00
|
|
|
Paint(paint_struct.hdc);
|
2010-10-03 23:04:50 +02:00
|
|
|
EndPaint(&paint_struct);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// PrintClient is necessary to support off-screen rendering.
|
|
|
|
LRESULT ClientPlugin::OnPrintClient(UINT message, WPARAM wparam, LPARAM lparam,
|
2012-01-10 00:46:23 +01:00
|
|
|
BOOL& handled) {
|
2011-03-24 21:36:47 +01:00
|
|
|
Paint(reinterpret_cast<HDC>(wparam));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
LRESULT ClientPlugin::OnEraseBackGround(UINT message, WPARAM wparam,
|
2012-01-10 00:46:23 +01:00
|
|
|
LPARAM lparam, BOOL& handled) {
|
2010-10-03 23:04:50 +02:00
|
|
|
HDC paint_device_context = reinterpret_cast<HDC>(wparam);
|
|
|
|
RECT erase_rect;
|
|
|
|
GetClipBox(paint_device_context, &erase_rect);
|
|
|
|
HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0));
|
|
|
|
FillRect(paint_device_context, &erase_rect, brush);
|
|
|
|
DeleteObject(brush);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT ClientPlugin::OnLButtonDown(UINT message, WPARAM wparam, LPARAM lparam,
|
2012-01-10 00:46:23 +01:00
|
|
|
BOOL& handled) {
|
2010-10-03 23:04:50 +02:00
|
|
|
MessageBox(L"You clicked on the client plugin!", L"Client Plugin", MB_OK);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientPlugin::RefreshDisplay() {
|
|
|
|
if (!IsWindow())
|
|
|
|
return;
|
|
|
|
|
|
|
|
InvalidateRect(NULL, TRUE);
|
|
|
|
UpdateWindow();
|
|
|
|
}
|
2010-10-28 22:38:27 +02:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
void ClientPlugin::Paint(HDC hdc) {
|
|
|
|
static LPCWSTR text = L"Left click in the green area for a message box!";
|
|
|
|
|
|
|
|
RECT client_rect;
|
|
|
|
GetClientRect(&client_rect);
|
|
|
|
|
|
|
|
int old_mode = SetBkMode(hdc, TRANSPARENT);
|
|
|
|
COLORREF old_color = SetTextColor(hdc, RGB(0, 0, 255));
|
|
|
|
|
|
|
|
RECT text_rect = client_rect;
|
|
|
|
DrawText(hdc, text, -1, &text_rect, DT_CENTER | DT_CALCRECT);
|
|
|
|
|
|
|
|
client_rect.top = ((client_rect.bottom - client_rect.top)
|
|
|
|
- (text_rect.bottom - text_rect.top)) / 2;
|
|
|
|
DrawText(hdc, text, -1, &client_rect, DT_CENTER);
|
|
|
|
|
|
|
|
SetBkMode(hdc, old_mode);
|
|
|
|
SetTextColor(hdc, old_color);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // OS_WIN
|