Skip to content

Commit

Permalink
Expose the IME methods/callback added with https://bitbucket.org/chro…
Browse files Browse the repository at this point in the history
…miumembedded/cef/commits/f7014be

It should now be possible for someone to implement IME support in the WPF control to resolve #1262
  • Loading branch information
amaitland committed Dec 13, 2016
1 parent 51ef5d7 commit fdd0889
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 0 deletions.
51 changes: 51 additions & 0 deletions CefSharp.Core/Internals/CefBrowserHostWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,57 @@ void CefBrowserHostWrapper::Invalidate(PaintElementType type)
_browserHost->Invalidate((CefBrowserHost::PaintElementType)type);
}

void CefBrowserHostWrapper::ImeSetComposition(String^ text, cli::array<CompositionUnderline>^ underlines, Nullable<Range> selectionRange)
{
ThrowIfDisposed();

std::vector<CefCompositionUnderline> underlinesVector = std::vector<CefCompositionUnderline>();
CefRange range;

if (underlines != nullptr && underlines->Length > 0)
{
for each (CompositionUnderline underline in underlines)
{
auto c = CefCompositionUnderline();
c.range = CefRange(underline.Range.From, underline.Range.To);
c.color = underline.Color;
c.background_color = underline.BackgroundColor;
c.thick = (int)underline.Thick;
underlinesVector.push_back(c);
}
}

if (selectionRange.HasValue)
{
range = CefRange(selectionRange.Value.From, selectionRange.Value.To);
}

//Replacement Range is Mac OSX only
_browserHost->ImeSetComposition(StringUtils::ToNative(text), underlinesVector, CefRange(), range);
}

void CefBrowserHostWrapper::ImeCommitText(String^ text)
{
ThrowIfDisposed();

//Range and cursor position are Mac OSX only
_browserHost->ImeCommitText(StringUtils::ToNative(text), CefRange(), NULL);
}

void CefBrowserHostWrapper::ImeFinishComposingText(bool keepSelection)
{
ThrowIfDisposed();

_browserHost->ImeFinishComposingText(keepSelection);
}

void CefBrowserHostWrapper::ImeCancelComposition()
{
ThrowIfDisposed();

_browserHost->ImeCancelComposition();
}

void CefBrowserHostWrapper::SendMouseClickEvent(int x, int y, MouseButtonType mouseButtonType, bool mouseUp, int clickCount, CefEventFlags modifiers)
{
ThrowIfDisposed();
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.Core/Internals/CefBrowserHostWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ namespace CefSharp

virtual void Invalidate(PaintElementType type);

virtual void ImeSetComposition(String^ text, cli::array<CompositionUnderline>^ underlines, Nullable<Range> selectionRange);
virtual void ImeCommitText(String^ text);
virtual void ImeFinishComposingText(bool keepSelection);
virtual void ImeCancelComposition();

virtual void SendMouseClickEvent(int x, int y, MouseButtonType mouseButtonType, bool mouseUp, int clickCount, CefEventFlags modifiers);

virtual void SendMouseMoveEvent(int x, int y, bool mouseLeave, CefEventFlags modifiers);
Expand Down
31 changes: 31 additions & 0 deletions CefSharp.Core/Internals/RenderClientAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,37 @@ namespace CefSharp
return _renderWebBrowser->StartDragging(%dragDataWrapper, (CefSharp::DragOperationsMask)allowedOps, x, y);
}

///
// Called when the web view wants to update the mouse cursor during a
// drag & drop operation. |operation| describes the allowed operation
// (none, move, copy, link).
///
/*--cef()--*/
virtual DECL void UpdateDragCursor(CefRefPtr<CefBrowser> browser, CefRenderHandler::DragOperation operation)
{
return _renderWebBrowser->UpdateDragCursor((CefSharp::DragOperationsMask)operation);
}

///
// Called when the IME composition range has changed. |selected_range| is the
// range of characters that have been selected. |character_bounds| is the
// bounds of each character in view coordinates.
///
/*--cef()--*/
virtual DECL void OnImeCompositionRangeChanged(CefRefPtr<CefBrowser> browser, const CefRange& selectedRange, const RectList& characterBounds)
{
//TODO: use cli:array rather then creating a list then calling ToArray()
auto charBounds = gcnew List<Rect>((int)characterBounds.size());

std::vector<CefRect>::const_iterator it =
characterBounds.begin();
for (; it != characterBounds.end(); ++it)
{
charBounds->Add(Rect((*it).x, (*it).y, (*it).width, (*it).height));
}
_renderWebBrowser->OnImeCompositionRangeChanged(Range(selectedRange.from, selectedRange.to), charBounds->ToArray());
}

private:
void ReleaseBitmapHandlers(BitmapInfo^ bitmapInfo)
{
Expand Down
10 changes: 10 additions & 0 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,11 @@ bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask
return false;
}

void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
{
//TODO: Someone should implement this
}

/// <summary>
/// Sets the popup is open.
/// </summary>
Expand Down Expand Up @@ -780,6 +785,11 @@ void IRenderWebBrowser.SetPopupSizeAndPosition(int width, int height, int x, int
popupSize.Height = height;
}

void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
{
//TODO: Implement this
}

/// <summary>
/// Handles the <see cref="E:ConsoleMessage" /> event.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask
return true;
}

void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
{
//TODO: Someone should implement this
}

/// <summary>
/// Invokes the render asynchronous.
/// </summary>
Expand Down Expand Up @@ -676,6 +681,11 @@ void IRenderWebBrowser.SetCursor(IntPtr handle, CefCursorType type)
}
}

void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
{
//TODO: Implement this
}

/// <summary>
/// Sets the address.
/// </summary>
Expand Down Expand Up @@ -2076,5 +2086,6 @@ public bool IsDisposed
// }
// }
//}

}
}
2 changes: 2 additions & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="CefMenuCommand.cs" />
<Compile Include="CefPdfPrintMarginType.cs" />
<Compile Include="CertStatus.cs" />
<Compile Include="CompositionUnderline.cs" />
<Compile Include="ContextMenuEditState.cs" />
<Compile Include="ContextMenuMediaState.cs" />
<Compile Include="ContextMenuMediaType.cs" />
Expand Down Expand Up @@ -209,6 +210,7 @@
<Compile Include="PluginPolicy.cs" />
<Compile Include="PostDataElementType.cs" />
<Compile Include="PostDataExtensions.cs" />
<Compile Include="Range.cs" />
<Compile Include="Rect.cs" />
<Compile Include="ReferrerPolicy.cs" />
<Compile Include="ResolveCallbackResult.cs" />
Expand Down
45 changes: 45 additions & 0 deletions CefSharp/CompositionUnderline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2010-2016 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
{
/// <summary>
/// Represents an IME composition underline.
/// </summary>
public struct CompositionUnderline
{
public CompositionUnderline(Range range, uint color, uint backGroundColor, bool thick)
: this()
{
Range = range;
Color = color;
BackgroundColor = backGroundColor;
Thick = thick;
}

/// <summary>
/// Underline character range.
/// </summary>
public Range Range { get; private set; }

/// <summary>
/// Text color. 32-bit ARGB color value, not premultiplied. The color components are always
/// in a known order. Equivalent to the SkColor type.
/// </summary>
public uint Color { get; private set; }
/// <summary>
/// Background color. 32-bit ARGB color value, not premultiplied. The color components are always
/// in a known order. Equivalent to the SkColor type.
/// </summary>
public uint BackgroundColor { get; private set; }

/// <summary>
/// true for thickunderline
/// </summary>
public bool Thick { get; private set; }


}
}

46 changes: 46 additions & 0 deletions CefSharp/IBrowserHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ public interface IBrowserHost : IDisposable
/// <param name="type">indicates which surface to re-paint either View or Popup.</param>
void Invalidate(PaintElementType type);

/// <summary>
/// Begins a new composition or updates the existing composition. Blink has a
/// special node (a composition node) that allows the input method to change
/// text without affecting other DOM nodes.
///
/// This method may be called multiple times as the composition changes. When
/// the client is done making changes the composition should either be canceled
/// or completed. To cancel the composition call ImeCancelComposition. To
/// complete the composition call either ImeCommitText or
/// ImeFinishComposingText. Completion is usually signaled when:
/// The client receives a WM_IME_COMPOSITION message with a GCS_RESULTSTR
/// flag (on Windows).
/// This method is only used when window rendering is disabled. (WPF and OffScreen)
/// </summary>
/// <param name="text">is the optional text that
/// will be inserted into the composition node</param>
/// <param name="underlines">is an optional set
/// of ranges that will be underlined in the resulting text.</param>
/// <param name="selectionRange"> is an optional range of the resulting text that
/// will be selected after insertion or replacement. </param>
void ImeSetComposition(string text, CompositionUnderline[] underlines, Range? selectionRange);

/// <summary>
/// Completes the existing composition by optionally inserting the specified
/// text into the composition node.
/// This method is only used when window rendering is disabled. (WPF and OffScreen)
/// </summary>
/// </summary>
/// <param name="text">text that will be committed</param>
void ImeCommitText(string text);
/// <summary>
/// Completes the existing composition by applying the current composition node
/// contents. See comments on ImeSetComposition for usage.
/// This method is only used when window rendering is disabled. (WPF and OffScreen)
/// </summary>
/// <param name="keepSelection">If keepSelection is false the current selection, if any, will be discarded.</param>
void ImeFinishComposingText(bool keepSelection);

/// <summary>
/// Cancels the existing composition and discards the composition node
/// contents without applying them. See comments on ImeSetComposition for
/// usage.
/// This method is only used when window rendering is disabled. (WPF and OffScreen)
/// </summary>
void ImeCancelComposition();

/// <summary>
/// Get/Set Mouse cursor change disabled
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions CefSharp/Internals/IRenderWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public interface IRenderWebBrowser : IWebBrowserInternal
void SetCursor(IntPtr cursor, CefCursorType type);

bool StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y);
void UpdateDragCursor(DragOperationsMask operation);

void SetPopupIsOpen(bool show);
void SetPopupSizeAndPosition(int width, int height, int x, int y);

/// <summary>
/// Called when the IME composition range has changed.
/// </summary>
/// <param name="selectedRange">is the range of characters that have been selected</param>
/// <param name="characterBounds">is the bounds of each character in view coordinates.</param>
void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds);
};
}
22 changes: 22 additions & 0 deletions CefSharp/Range.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright © 2010-2016 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
{
/// <summary>
/// Represents a range
/// </summary>
public struct Range
{
public Range(int from, int to)
: this()
{
From = from;
To = to;
}

public int From { get; private set; }
public int To { get; private set; }
}
}

0 comments on commit fdd0889

Please sign in to comment.