mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
cefclient: Add example of window manipulation via JavaScript (issue #925).
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1166 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
128
tests/cefclient/window_test.cpp
Normal file
128
tests/cefclient/window_test.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
||||
// reserved. Use of this source code is governed by a BSD-style license that
|
||||
// can be found in the LICENSE file.
|
||||
|
||||
#include "cefclient/window_test.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include "include/wrapper/cef_stream_resource_handler.h"
|
||||
#include "cefclient/resource_util.h"
|
||||
|
||||
namespace window_test {
|
||||
|
||||
namespace {
|
||||
|
||||
const char* kTestUrl = "http://tests/window";
|
||||
const char* kMessagePositionName = "WindowTest.Position";
|
||||
const char* kMessageMinimizeName = "WindowTest.Minimize";
|
||||
const char* kMessageMaximizeName = "WindowTest.Maximize";
|
||||
const char* kMessageRestoreName = "WindowTest.Restore";
|
||||
|
||||
// Handle messages in the browser process.
|
||||
class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
|
||||
public:
|
||||
ProcessMessageDelegate() {
|
||||
}
|
||||
|
||||
// From ClientHandler::ProcessMessageDelegate.
|
||||
virtual bool OnProcessMessageReceived(
|
||||
CefRefPtr<ClientHandler> handler,
|
||||
CefRefPtr<CefBrowser> browser,
|
||||
CefProcessId source_process,
|
||||
CefRefPtr<CefProcessMessage> message) OVERRIDE {
|
||||
std::string message_name = message->GetName();
|
||||
if (message_name == kMessagePositionName) {
|
||||
CefRefPtr<CefListValue> args = message->GetArgumentList();
|
||||
if (args->GetSize() >= 4) {
|
||||
int x = args->GetInt(0);
|
||||
int y = args->GetInt(1);
|
||||
int width = args->GetInt(2);
|
||||
int height = args->GetInt(3);
|
||||
SetPos(browser->GetHost()->GetWindowHandle(), x, y, width, height);
|
||||
}
|
||||
return true;
|
||||
} else if (message_name == kMessageMinimizeName) {
|
||||
Minimize(browser->GetHost()->GetWindowHandle());
|
||||
return true;
|
||||
} else if (message_name == kMessageMaximizeName) {
|
||||
Maximize(browser->GetHost()->GetWindowHandle());
|
||||
return true;
|
||||
} else if (message_name == kMessageRestoreName) {
|
||||
Restore(browser->GetHost()->GetWindowHandle());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
IMPLEMENT_REFCOUNTING(ProcessMessageDelegate);
|
||||
};
|
||||
|
||||
// Handle resource loading in the browser process.
|
||||
class RequestDelegate: public ClientHandler::RequestDelegate {
|
||||
public:
|
||||
RequestDelegate() {
|
||||
}
|
||||
|
||||
// From ClientHandler::RequestDelegate.
|
||||
virtual CefRefPtr<CefResourceHandler> GetResourceHandler(
|
||||
CefRefPtr<ClientHandler> handler,
|
||||
CefRefPtr<CefBrowser> browser,
|
||||
CefRefPtr<CefFrame> frame,
|
||||
CefRefPtr<CefRequest> request) OVERRIDE {
|
||||
std::string url = request->GetURL();
|
||||
if (url == kTestUrl) {
|
||||
// Show the contents
|
||||
CefRefPtr<CefStreamReader> stream =
|
||||
GetBinaryResourceReader("window.html");
|
||||
ASSERT(stream.get());
|
||||
return new CefStreamResourceHandler("text/html", stream);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IMPLEMENT_REFCOUNTING(RequestDelegate);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void CreateProcessMessageDelegates(
|
||||
ClientHandler::ProcessMessageDelegateSet& delegates) {
|
||||
delegates.insert(new ProcessMessageDelegate);
|
||||
}
|
||||
|
||||
void CreateRequestDelegates(ClientHandler::RequestDelegateSet& delegates) {
|
||||
delegates.insert(new RequestDelegate);
|
||||
}
|
||||
|
||||
void RunTest(CefRefPtr<CefBrowser> browser) {
|
||||
// Load the test URL.
|
||||
browser->GetMainFrame()->LoadURL(kTestUrl);
|
||||
}
|
||||
|
||||
void ModifyBounds(const CefRect& display, CefRect& window) {
|
||||
window.x += display.x;
|
||||
window.y += display.y;
|
||||
|
||||
if (window.x < display.x)
|
||||
window.x = display.x;
|
||||
if (window.y < display.y)
|
||||
window.y = display.y;
|
||||
if (window.width < 100)
|
||||
window.width = 100;
|
||||
else if (window.width >= display.width)
|
||||
window.width = display.width;
|
||||
if (window.height < 100)
|
||||
window.height = 100;
|
||||
else if (window.height >= display.height)
|
||||
window.height = display.height;
|
||||
if (window.x + window.width >= display.x + display.width)
|
||||
window.x = display.x + display.width - window.width;
|
||||
if (window.y + window.height >= display.y + display.height)
|
||||
window.y = display.y + display.height - window.height;
|
||||
}
|
||||
|
||||
} // namespace window_test
|
Reference in New Issue
Block a user