2013-09-12 08:19:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "CompositionStringSynthesizer.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsView.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/TextEvents.h"
|
2013-09-12 08:19:00 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(CompositionStringSynthesizer,
|
|
|
|
nsICompositionStringSynthesizer)
|
|
|
|
|
|
|
|
CompositionStringSynthesizer::CompositionStringSynthesizer(
|
|
|
|
nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
mWindow = do_GetWeakReference(aWindow);
|
2014-03-04 05:48:26 -08:00
|
|
|
mClauses = new TextRangeArray();
|
2013-09-12 08:19:00 -07:00
|
|
|
ClearInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositionStringSynthesizer::~CompositionStringSynthesizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompositionStringSynthesizer::ClearInternal()
|
|
|
|
{
|
|
|
|
mString.Truncate();
|
2014-03-04 05:48:26 -08:00
|
|
|
mClauses->Clear();
|
2013-09-12 08:19:00 -07:00
|
|
|
mCaret.mRangeType = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIWidget*
|
|
|
|
CompositionStringSynthesizer::GetWidget()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsIDocShell *docShell = window->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
|
|
|
|
if (!presShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsIFrame* frame = presShell->GetRootFrame();
|
|
|
|
if (!frame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return frame->GetView()->GetNearestWidget(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CompositionStringSynthesizer::SetString(const nsAString& aString)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget();
|
|
|
|
NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mString = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CompositionStringSynthesizer::AppendClause(uint32_t aLength,
|
|
|
|
uint32_t aAttribute)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget();
|
|
|
|
NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
switch (aAttribute) {
|
|
|
|
case ATTR_RAWINPUT:
|
|
|
|
case ATTR_SELECTEDRAWTEXT:
|
|
|
|
case ATTR_CONVERTEDTEXT:
|
|
|
|
case ATTR_SELECTEDCONVERTEDTEXT: {
|
2013-10-01 00:22:59 -07:00
|
|
|
TextRange textRange;
|
2013-09-12 08:19:00 -07:00
|
|
|
textRange.mStartOffset =
|
2014-03-04 05:48:26 -08:00
|
|
|
mClauses->IsEmpty() ? 0 : mClauses->LastElement().mEndOffset;
|
2013-09-12 08:19:00 -07:00
|
|
|
textRange.mEndOffset = textRange.mStartOffset + aLength;
|
|
|
|
textRange.mRangeType = aAttribute;
|
2014-03-04 05:48:26 -08:00
|
|
|
mClauses->AppendElement(textRange);
|
2013-09-12 08:19:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CompositionStringSynthesizer::SetCaret(uint32_t aOffset, uint32_t aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget();
|
|
|
|
NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
mCaret.mStartOffset = aOffset;
|
|
|
|
mCaret.mEndOffset = mCaret.mStartOffset + aLength;
|
|
|
|
mCaret.mRangeType = NS_TEXTRANGE_CARETPOSITION;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CompositionStringSynthesizer::DispatchEvent(bool* aDefaultPrevented)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aDefaultPrevented);
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget();
|
|
|
|
NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2014-03-04 05:48:26 -08:00
|
|
|
if (!mClauses->IsEmpty() &&
|
|
|
|
mClauses->LastElement().mEndOffset != mString.Length()) {
|
2013-09-12 08:19:00 -07:00
|
|
|
NS_WARNING("Sum of length of the all clauses must be same as the string "
|
|
|
|
"length");
|
|
|
|
ClearInternal();
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2013-09-12 08:19:00 -07:00
|
|
|
}
|
|
|
|
if (mCaret.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
|
2013-09-12 08:19:00 -07:00
|
|
|
if (mCaret.mEndOffset > mString.Length()) {
|
|
|
|
NS_WARNING("Caret position is out of the composition string");
|
|
|
|
ClearInternal();
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
2014-03-04 05:48:26 -08:00
|
|
|
mClauses->AppendElement(mCaret);
|
2013-09-12 08:19:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-01 00:22:59 -07:00
|
|
|
WidgetTextEvent textEvent(true, NS_TEXT_TEXT, widget);
|
2013-09-12 08:19:00 -07:00
|
|
|
textEvent.time = PR_IntervalNow();
|
|
|
|
textEvent.theText = mString;
|
2014-03-04 05:48:28 -08:00
|
|
|
if (!mClauses->IsEmpty()) {
|
|
|
|
textEvent.mRanges = mClauses;
|
|
|
|
}
|
2013-09-12 08:19:00 -07:00
|
|
|
|
|
|
|
// XXX How should we set false for this on b2g?
|
|
|
|
textEvent.mFlags.mIsSynthesizedForTests = true;
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsresult rv = widget->DispatchEvent(&textEvent, status);
|
|
|
|
*aDefaultPrevented = (status == nsEventStatus_eConsumeNoDefault);
|
|
|
|
|
|
|
|
ClearInternal();
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|