mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
Added IFocusHandler and WinForms example.
This commit is contained in:
@@ -375,31 +375,39 @@ namespace CefSharp
|
||||
|
||||
void ClientAdapter::OnGotFocus(CefRefPtr<CefBrowser> browser)
|
||||
{
|
||||
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
|
||||
if (winFormsControl != nullptr)
|
||||
{
|
||||
winFormsControl->OnGotFocus();
|
||||
}
|
||||
IFocusHandler^ handler = _browserControl->FocusHandler;
|
||||
|
||||
if (handler == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handler->OnGotFocus();
|
||||
}
|
||||
|
||||
bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
|
||||
bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
|
||||
{
|
||||
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
|
||||
if (winFormsControl == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IFocusHandler^ handler = _browserControl->FocusHandler;
|
||||
|
||||
return winFormsControl->OnSetFocus((CefFocusSource)source);
|
||||
if (handler == nullptr)
|
||||
{
|
||||
// Allow the focus to be set by default.
|
||||
return false;
|
||||
}
|
||||
|
||||
return handler->OnSetFocus((CefFocusSource)source);
|
||||
}
|
||||
|
||||
void ClientAdapter::OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next)
|
||||
{
|
||||
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
|
||||
if (winFormsControl != nullptr)
|
||||
{
|
||||
winFormsControl->OnTakeFocus(next);
|
||||
}
|
||||
IFocusHandler^ handler = _browserControl->FocusHandler;
|
||||
|
||||
if (handler == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handler->OnTakeFocus(next);
|
||||
}
|
||||
|
||||
bool ClientAdapter::OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,
|
||||
|
||||
@@ -176,6 +176,7 @@ namespace CefSharp.OffScreen
|
||||
public IKeyboardHandler KeyboardHandler { get; set; }
|
||||
public ILifeSpanHandler LifeSpanHandler { get; set; }
|
||||
public IMenuHandler MenuHandler { get; set; }
|
||||
public IFocusHandler FocusHandler { get; set; }
|
||||
public IRequestHandler RequestHandler { get; set; }
|
||||
|
||||
public event EventHandler<LoadErrorEventArgs> LoadError;
|
||||
@@ -451,11 +452,6 @@ namespace CefSharp.OffScreen
|
||||
}
|
||||
}
|
||||
|
||||
void IWebBrowserInternal.OnTakeFocus(bool next)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IWebBrowserInternal.OnStatusMessage(string value)
|
||||
{
|
||||
var handler = StatusMessage;
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace CefSharp.WinForms.Example
|
||||
Browser = browser;
|
||||
|
||||
browser.MenuHandler = new MenuHandler();
|
||||
browser.FocusHandler = new FocusHandler(browser, urlTextBox);
|
||||
browser.NavStateChanged += OnBrowserNavStateChanged;
|
||||
browser.ConsoleMessage += OnBrowserConsoleMessage;
|
||||
browser.TitleChanged += OnBrowserTitleChanged;
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
<Compile Include="BrowserTabUserControl.Designer.cs">
|
||||
<DependentUpon>BrowserTabUserControl.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="FocusHandler.cs" />
|
||||
<Compile Include="MenuHandler.cs" />
|
||||
<Compile Include="Minimal\SimpleBrowserForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System.Windows.Forms;
|
||||
namespace CefSharp.WinForms.Example
|
||||
{
|
||||
internal class FocusHandler : IFocusHandler
|
||||
{
|
||||
private ChromiumWebBrowser browser;
|
||||
private ToolStripTextBox urlTextBox;
|
||||
|
||||
public FocusHandler(ChromiumWebBrowser browser, ToolStripTextBox urlTextBox)
|
||||
{
|
||||
this.browser = browser;
|
||||
this.urlTextBox = urlTextBox;
|
||||
}
|
||||
|
||||
public void OnGotFocus()
|
||||
{
|
||||
// Fired when Chromium finally receives focus.
|
||||
}
|
||||
|
||||
public bool OnSetFocus(CefFocusSource source)
|
||||
{
|
||||
// Returning false means that we allow Chromium to take the focus away from our WinForms control.
|
||||
// You could also return true to keep focus in the address bar.
|
||||
return false;
|
||||
}
|
||||
|
||||
public void OnTakeFocus(bool next)
|
||||
{
|
||||
// Fired when Chromium is giving up focus because the user
|
||||
// pressed Tab on the last link/field in the HTML document (in which case 'next' is true)
|
||||
// or because they pressed Shift+Tab on the first link/field (in which case 'next' is false).
|
||||
|
||||
// Here we always focus on the address bar.
|
||||
urlTextBox.Focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ namespace CefSharp.WinForms
|
||||
public IDownloadHandler DownloadHandler { get; set; }
|
||||
public ILifeSpanHandler LifeSpanHandler { get; set; }
|
||||
public IMenuHandler MenuHandler { get; set; }
|
||||
public IFocusHandler FocusHandler { get; set; }
|
||||
|
||||
public bool CanGoForward { get; private set; }
|
||||
public bool CanGoBack { get; private set; }
|
||||
@@ -208,21 +209,6 @@ namespace CefSharp.WinForms
|
||||
}
|
||||
}
|
||||
|
||||
void IWinFormsWebBrowser.OnGotFocus()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool IWinFormsWebBrowser.OnSetFocus(CefFocusSource source)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void IWinFormsWebBrowser.OnTakeFocus(bool next)
|
||||
{
|
||||
SelectNextControl(this, next, true, true, true);
|
||||
}
|
||||
|
||||
void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line)
|
||||
{
|
||||
var handler = ConsoleMessage;
|
||||
|
||||
@@ -51,6 +51,7 @@ namespace CefSharp.Wpf
|
||||
public IDownloadHandler DownloadHandler { get; set; }
|
||||
public ILifeSpanHandler LifeSpanHandler { get; set; }
|
||||
public IMenuHandler MenuHandler { get; set; }
|
||||
public IFocusHandler FocusHandler { get; set; }
|
||||
|
||||
public event EventHandler<ConsoleMessageEventArgs> ConsoleMessage;
|
||||
public event EventHandler<StatusMessageEventArgs> StatusMessage;
|
||||
|
||||
@@ -9,13 +9,13 @@ namespace CefSharp
|
||||
/// </summary>
|
||||
public enum CefFocusSource
|
||||
{
|
||||
///
|
||||
// The source is explicit navigation via the API (LoadURL(), etc).
|
||||
///
|
||||
/// <summary>
|
||||
/// The source is explicit navigation via the API (LoadURL(), etc).
|
||||
/// </summary>
|
||||
FocusSourceNavigation = 0,
|
||||
///
|
||||
// The source is a system-generated focus event.
|
||||
///
|
||||
/// <summary>
|
||||
/// The source is a system-generated focus event.
|
||||
/// </summary>
|
||||
FocusSourceSystem
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="IFocusHandler.cs" />
|
||||
<Compile Include="IsBrowserInitializedChangedEventArgs.cs" />
|
||||
<Compile Include="CefTerminationStatus.cs" />
|
||||
<Compile Include="IDialogHandler.cs" />
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
public interface IFocusHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the browser component has received focus.
|
||||
/// </summary>
|
||||
void OnGotFocus();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the browser component is requesting focus.
|
||||
/// </summary>
|
||||
/// <param name="source">Indicates where the focus request is originating from.</param>
|
||||
/// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns>
|
||||
bool OnSetFocus(CefFocusSource source);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the browser component is about to lose focus.
|
||||
/// For instance, if focus was on the last HTML element and the user pressed the TAB key.
|
||||
/// </summary>
|
||||
/// <param name="next">Will be true if the browser is giving focus to the next component
|
||||
/// and false if the browser is giving focus to the previous component.</param>
|
||||
void OnTakeFocus(bool next);
|
||||
}
|
||||
}
|
||||
@@ -110,6 +110,11 @@ namespace CefSharp
|
||||
/// </summary>
|
||||
IMenuHandler MenuHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Implement <see cref="IFocusHandler"/> and assign to handle events related to the browser component's focus
|
||||
/// </summary>
|
||||
IFocusHandler FocusHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A flag that indicates whether the WebBrowser is initialized (true) or not (false).
|
||||
/// </summary>
|
||||
|
||||
@@ -20,9 +20,5 @@ namespace CefSharp
|
||||
void Paste();
|
||||
void Delete();
|
||||
void SelectAll();
|
||||
|
||||
void OnGotFocus();
|
||||
bool OnSetFocus(CefFocusSource source);
|
||||
void OnTakeFocus(bool next);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user