#include "WebView.h" using namespace System::Drawing; namespace CefSharp { namespace WinForms { void WebView::WaitForInitialized() { if (IsInitialized) { return; } // TODO: risk of infinite lock _initialized->WaitOne(); } void WebView::OnHandleCreated(EventArgs^ e) { if (DesignMode == false) { _clientAdapter = new ClientAdapter(this); CefRefPtr ptr = _clientAdapter.get(); CefString urlStr = toNative(_browserCore->Address); CefWindowInfo windowInfo; HWND hWnd = static_cast(Handle.ToPointer()); RECT rect; GetClientRect(hWnd, &rect); windowInfo.SetAsChild(hWnd, rect); CefBrowser::CreateBrowser(windowInfo, static_cast>(ptr), urlStr, *_settings->_browserSettings); } } void WebView::OnSizeChanged(EventArgs^ e) { if (IsInitialized && !DesignMode) { HWND hWnd = static_cast(Handle.ToPointer()); RECT rect; GetClientRect(hWnd, &rect); HDWP hdwp = BeginDeferWindowPos(1); HWND browserHwnd = _clientAdapter->GetBrowserHwnd(); hdwp = DeferWindowPos(hdwp, browserHwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER); EndDeferWindowPos(hdwp); } } void WebView::OnGotFocus(EventArgs^ e) { if (IsInitialized && !DesignMode) { _clientAdapter->GetCefBrowser()->SetFocus(true); } } void WebView::OnInitialized() { BeginInvoke(gcnew Action(this, &WebView::OnSizeChanged), EventArgs::Empty); _initialized->Set(); } void WebView::Load(String^ url) { WaitForInitialized(); _browserCore->OnLoad(); _clientAdapter->GetCefBrowser()->GetMainFrame()->LoadURL(toNative(url)); } void WebView::Stop() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->StopLoad(); } void WebView::Back() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->GoBack(); } void WebView::Forward() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->GoForward(); } void WebView::Reload() { Reload(false); } void WebView::Reload(bool ignoreCache) { WaitForInitialized(); if (ignoreCache) { _clientAdapter->GetCefBrowser()->ReloadIgnoreCache(); } else { _clientAdapter->GetCefBrowser()->Reload(); } } void WebView::Print() { WaitForInitialized(); _clientAdapter->GetCefBrowser()->GetMainFrame()->Print(); } void WebView::ExecuteScript(String^ script) { WaitForInitialized(); CefRefPtr browser = _clientAdapter->GetCefBrowser(); CefRefPtr frame = browser->GetMainFrame(); _scriptCore->Execute(frame, toNative(script)); } String^ WebView::EvaluateScript(String^ script) { return EvaluateScript(script, TimeSpan::MaxValue); } String^ WebView::EvaluateScript(String^ script, TimeSpan timeout) { WaitForInitialized(); CefRefPtr browser = _clientAdapter->GetCefBrowser(); CefRefPtr frame = browser->GetMainFrame(); CefString result = _scriptCore->Evaluate(frame, toNative(script), timeout.TotalMilliseconds); return toClr(result); } void WebView::SetNavState(bool isLoading, bool canGoBack, bool canGoForward) { _browserCore->SetNavState(isLoading, canGoBack, canGoForward); } void WebView::RaiseConsoleMessage(String^ message, String^ source, int line) { ConsoleMessage(this, gcnew ConsoleMessageEventArgs(message, source, line)); } void WebView::OnFrameLoadStart() { _browserCore->OnFrameLoadStart(); } void WebView::OnFrameLoadEnd() { _browserCore->OnFrameLoadEnd(); } }}