2006-04-11 20:37:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
#include "prlog.h"
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2013-11-20 13:32:46 +04:00
|
|
|
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2014-08-29 14:08:43 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2014-04-03 08:18:37 +04:00
|
|
|
#include "mozilla/TextComposition.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
|
|
|
#include "mozilla/dom/HTMLFormElement.h"
|
|
|
|
|
2013-11-20 13:32:46 +04:00
|
|
|
#include "HTMLInputElement.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "IMEContentObserver.h"
|
|
|
|
|
2006-04-11 20:37:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-04-11 20:37:58 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2011-11-27 15:51:53 +04:00
|
|
|
#include "nsIDOMMouseEvent.h"
|
2014-11-05 16:12:44 +03:00
|
|
|
#include "nsIEditor.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsIFormControl.h"
|
2009-02-10 23:56:51 +03:00
|
|
|
#include "nsINode.h"
|
2010-10-18 20:37:00 +04:00
|
|
|
#include "nsIObserverService.h"
|
2014-03-08 05:20:08 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsPresContext.h"
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
namespace mozilla {
|
2011-11-27 15:51:52 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
using namespace dom;
|
|
|
|
using namespace widget;
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
/**
|
|
|
|
* When a method is called, log its arguments and/or related static variables
|
|
|
|
* with PR_LOG_ALWAYS. However, if it puts too many logs like
|
|
|
|
* OnDestroyPresContext(), should long only when the method actually does
|
|
|
|
* something. In this case, the log should start with "ISM: <method name>".
|
|
|
|
*
|
|
|
|
* When a method quits due to unexpected situation, log the reason with
|
|
|
|
* PR_LOG_ERROR. In this case, the log should start with
|
|
|
|
* "ISM: <method name>(), FAILED". The indent makes the log look easier.
|
|
|
|
*
|
|
|
|
* When a method does something only in some situations and it may be important
|
|
|
|
* for debug, log the information with PR_LOG_DEBUG. In this case, the log
|
|
|
|
* should start with "ISM: <method name>(),".
|
|
|
|
*/
|
|
|
|
PRLogModuleInfo* sISMLog = nullptr;
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetBoolName(bool aBool)
|
|
|
|
{
|
|
|
|
return aBool ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetActionCauseName(InputContextAction::Cause aCause)
|
|
|
|
{
|
|
|
|
switch (aCause) {
|
|
|
|
case InputContextAction::CAUSE_UNKNOWN:
|
|
|
|
return "CAUSE_UNKNOWN";
|
|
|
|
case InputContextAction::CAUSE_UNKNOWN_CHROME:
|
|
|
|
return "CAUSE_UNKNOWN_CHROME";
|
|
|
|
case InputContextAction::CAUSE_KEY:
|
|
|
|
return "CAUSE_KEY";
|
|
|
|
case InputContextAction::CAUSE_MOUSE:
|
|
|
|
return "CAUSE_MOUSE";
|
|
|
|
default:
|
|
|
|
return "illegal value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetActionFocusChangeName(InputContextAction::FocusChange aFocusChange)
|
|
|
|
{
|
|
|
|
switch (aFocusChange) {
|
|
|
|
case InputContextAction::FOCUS_NOT_CHANGED:
|
|
|
|
return "FOCUS_NOT_CHANGED";
|
|
|
|
case InputContextAction::GOT_FOCUS:
|
|
|
|
return "GOT_FOCUS";
|
|
|
|
case InputContextAction::LOST_FOCUS:
|
|
|
|
return "LOST_FOCUS";
|
|
|
|
case InputContextAction::MENU_GOT_PSEUDO_FOCUS:
|
|
|
|
return "MENU_GOT_PSEUDO_FOCUS";
|
|
|
|
case InputContextAction::MENU_LOST_PSEUDO_FOCUS:
|
|
|
|
return "MENU_LOST_PSEUDO_FOCUS";
|
|
|
|
default:
|
|
|
|
return "illegal value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetIMEStateEnabledName(IMEState::Enabled aEnabled)
|
|
|
|
{
|
|
|
|
switch (aEnabled) {
|
|
|
|
case IMEState::DISABLED:
|
|
|
|
return "DISABLED";
|
|
|
|
case IMEState::ENABLED:
|
|
|
|
return "ENABLED";
|
|
|
|
case IMEState::PASSWORD:
|
|
|
|
return "PASSWORD";
|
|
|
|
case IMEState::PLUGIN:
|
|
|
|
return "PLUGIN";
|
|
|
|
default:
|
|
|
|
return "illegal value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetIMEStateSetOpenName(IMEState::Open aOpen)
|
|
|
|
{
|
|
|
|
switch (aOpen) {
|
|
|
|
case IMEState::DONT_CHANGE_OPEN_STATE:
|
|
|
|
return "DONT_CHANGE_OPEN_STATE";
|
|
|
|
case IMEState::OPEN:
|
|
|
|
return "OPEN";
|
|
|
|
case IMEState::CLOSED:
|
|
|
|
return "CLOSED";
|
|
|
|
default:
|
|
|
|
return "illegal value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetEventMessageName(uint32_t aMessage)
|
|
|
|
{
|
|
|
|
switch (aMessage) {
|
|
|
|
case NS_COMPOSITION_START:
|
|
|
|
return "NS_COMPOSITION_START";
|
|
|
|
case NS_COMPOSITION_END:
|
|
|
|
return "NS_COMPOSITION_END";
|
|
|
|
case NS_COMPOSITION_UPDATE:
|
|
|
|
return "NS_COMPOSITION_UPDATE";
|
2014-10-07 14:01:47 +04:00
|
|
|
case NS_COMPOSITION_CHANGE:
|
|
|
|
return "NS_COMPOSITION_CHANGE";
|
2014-07-16 09:13:28 +04:00
|
|
|
default:
|
|
|
|
return "unacceptable event message";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetNotifyIMEMessageName(IMEMessage aMessage)
|
|
|
|
{
|
|
|
|
switch (aMessage) {
|
|
|
|
case NOTIFY_IME_OF_FOCUS:
|
|
|
|
return "NOTIFY_IME_OF_FOCUS";
|
|
|
|
case NOTIFY_IME_OF_BLUR:
|
|
|
|
return "NOTIFY_IME_OF_BLUR";
|
|
|
|
case NOTIFY_IME_OF_SELECTION_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_SELECTION_CHANGE";
|
|
|
|
case NOTIFY_IME_OF_TEXT_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_TEXT_CHANGE";
|
|
|
|
case NOTIFY_IME_OF_COMPOSITION_UPDATE:
|
|
|
|
return "NOTIFY_IME_OF_COMPOSITION_UPDATE";
|
|
|
|
case NOTIFY_IME_OF_POSITION_CHANGE:
|
|
|
|
return "NOTIFY_IME_OF_POSITION_CHANGE";
|
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION:
|
|
|
|
return "REQUEST_TO_COMMIT_COMPOSITION";
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION:
|
|
|
|
return "REQUEST_TO_CANCEL_COMPOSITION";
|
|
|
|
default:
|
|
|
|
return "unacceptable IME notification message";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // #ifdef PR_LOGGING
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
nsIContent* IMEStateManager::sContent = nullptr;
|
|
|
|
nsPresContext* IMEStateManager::sPresContext = nullptr;
|
|
|
|
bool IMEStateManager::sInstalledMenuKeyboardListener = false;
|
|
|
|
bool IMEStateManager::sIsTestingIME = false;
|
2014-07-17 09:08:44 +04:00
|
|
|
bool IMEStateManager::sIsGettingNewIMEState = false;
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// sActiveIMEContentObserver points to the currently active IMEContentObserver.
|
|
|
|
// sActiveIMEContentObserver is null if there is no focused editor.
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEContentObserver* IMEStateManager::sActiveIMEContentObserver = nullptr;
|
|
|
|
TextCompositionArray* IMEStateManager::sTextCompositions = nullptr;
|
2012-09-26 09:47:45 +04:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
IMEStateManager::Init()
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!sISMLog) {
|
|
|
|
sISMLog = PR_NewLogModule("IMEStateManager");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-09-26 09:47:45 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::Shutdown()
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::Shutdown(), "
|
|
|
|
"sTextCompositions=0x%p, sTextCompositions->Length()=%u",
|
|
|
|
sTextCompositions, sTextCompositions ? sTextCompositions->Length() : 0));
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
MOZ_ASSERT(!sTextCompositions || !sTextCompositions->Length());
|
|
|
|
delete sTextCompositions;
|
|
|
|
sTextCompositions = nullptr;
|
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2006-04-11 20:37:58 +04:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnDestroyPresContext(nsPresContext* aPresContext)
|
2006-04-11 20:37:58 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
2012-09-26 09:47:45 +04:00
|
|
|
|
|
|
|
// First, if there is a composition in the aPresContext, clean up it.
|
|
|
|
if (sTextCompositions) {
|
|
|
|
TextCompositionArray::index_type i =
|
|
|
|
sTextCompositions->IndexOf(aPresContext);
|
|
|
|
if (i != TextCompositionArray::NoIndex) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnDestroyPresContext(), "
|
|
|
|
"removing TextComposition instance from the array (index=%u)", i));
|
2012-09-26 09:47:45 +04:00
|
|
|
// there should be only one composition per presContext object.
|
2014-02-25 10:59:28 +04:00
|
|
|
sTextCompositions->ElementAt(i)->Destroy();
|
2012-09-26 09:47:45 +04:00
|
|
|
sTextCompositions->RemoveElementAt(i);
|
2014-07-16 09:13:28 +04:00
|
|
|
#if defined DEBUG || PR_LOGGING
|
|
|
|
if (sTextCompositions->IndexOf(aPresContext) !=
|
|
|
|
TextCompositionArray::NoIndex) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::OnDestroyPresContext(), FAILED to remove "
|
|
|
|
"TextComposition instance from the array"));
|
|
|
|
MOZ_CRASH("Failed to remove TextComposition instance from the array");
|
|
|
|
}
|
|
|
|
#endif // #if defined DEBUG || PR_LOGGING
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:08 +04:00
|
|
|
if (aPresContext != sPresContext) {
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
2014-03-08 05:20:08 +04:00
|
|
|
}
|
2012-11-09 12:40:39 +04:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnDestroyPresContext(aPresContext=0x%p), "
|
|
|
|
"sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p",
|
|
|
|
aPresContext, sPresContext, sContent, sTextCompositions));
|
|
|
|
|
|
|
|
DestroyIMEContentObserver();
|
2012-11-09 12:40:39 +04:00
|
|
|
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
|
2009-12-04 11:03:27 +03:00
|
|
|
if (widget) {
|
2012-07-30 18:20:58 +04:00
|
|
|
IMEState newState = GetNewIMEState(sPresContext, nullptr);
|
2011-11-27 15:51:52 +04:00
|
|
|
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
|
|
|
|
InputContextAction::LOST_FOCUS);
|
2012-07-30 18:20:58 +04:00
|
|
|
SetIMEState(newState, nullptr, widget, action);
|
2009-12-04 11:03:27 +03:00
|
|
|
}
|
2012-09-20 20:13:18 +04:00
|
|
|
NS_IF_RELEASE(sContent);
|
2012-07-30 18:20:58 +04:00
|
|
|
sPresContext = nullptr;
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2006-04-11 20:37:58 +04:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnRemoveContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
2006-04-11 20:37:58 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
2012-09-26 09:47:45 +04:00
|
|
|
|
|
|
|
// First, if there is a composition in the aContent, clean up it.
|
|
|
|
if (sTextCompositions) {
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> compositionInContent =
|
2012-09-26 09:47:45 +04:00
|
|
|
sTextCompositions->GetCompositionInContent(aPresContext, aContent);
|
2012-09-26 09:47:51 +04:00
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
if (compositionInContent) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnRemoveContent(), "
|
|
|
|
"composition is in the content"));
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
// Try resetting the native IME state. Be aware, typically, this method
|
|
|
|
// is called during the content being removed. Then, the native
|
|
|
|
// composition events which are caused by following APIs are ignored due
|
|
|
|
// to unsafe to run script (in PresShell::HandleEvent()).
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = aPresContext->GetRootWidget();
|
2014-09-26 04:05:11 +04:00
|
|
|
MOZ_ASSERT(widget, "Why is there no widget?");
|
|
|
|
nsresult rv =
|
|
|
|
compositionInContent->NotifyIME(REQUEST_TO_CANCEL_COMPOSITION);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
compositionInContent->NotifyIME(REQUEST_TO_COMMIT_COMPOSITION);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-11 20:37:58 +04:00
|
|
|
if (!sPresContext || !sContent ||
|
2012-09-26 09:47:45 +04:00
|
|
|
!nsContentUtils::ContentIsDescendantOf(sContent, aContent)) {
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnRemoveContent(aPresContext=0x%p, "
|
|
|
|
"aContent=0x%p), sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p",
|
|
|
|
aPresContext, aContent, sPresContext, sContent, sTextCompositions));
|
|
|
|
|
|
|
|
DestroyIMEContentObserver();
|
2012-11-09 12:40:39 +04:00
|
|
|
|
2006-04-11 20:37:58 +04:00
|
|
|
// Current IME transaction should commit
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
|
2008-07-14 06:56:18 +04:00
|
|
|
if (widget) {
|
2012-07-30 18:20:58 +04:00
|
|
|
IMEState newState = GetNewIMEState(sPresContext, nullptr);
|
2011-11-27 15:51:52 +04:00
|
|
|
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
|
|
|
|
InputContextAction::LOST_FOCUS);
|
2012-07-30 18:20:58 +04:00
|
|
|
SetIMEState(newState, nullptr, widget, action);
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 20:13:18 +04:00
|
|
|
NS_IF_RELEASE(sContent);
|
2012-07-30 18:20:58 +04:00
|
|
|
sPresContext = nullptr;
|
2006-04-11 20:37:58 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2006-04-11 20:37:58 +04:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnChangeFocus(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
InputContextAction::Cause aCause)
|
2011-11-27 15:51:52 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnChangeFocus(aPresContext=0x%p, "
|
|
|
|
"aContent=0x%p, aCause=%s)",
|
|
|
|
aPresContext, aContent, GetActionCauseName(aCause)));
|
|
|
|
|
2011-11-27 15:51:52 +04:00
|
|
|
InputContextAction action(aCause);
|
|
|
|
return OnChangeFocusInternal(aPresContext, aContent, action);
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2011-11-27 15:51:52 +04:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnChangeFocusInternal(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
InputContextAction aAction)
|
2006-04-11 20:37:58 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnChangeFocusInternal(aPresContext=0x%p, "
|
|
|
|
"aContent=0x%p, aAction={ mCause=%s, mFocusChange=%s }), "
|
|
|
|
"sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p",
|
|
|
|
aPresContext, aContent, GetActionCauseName(aAction.mCause),
|
|
|
|
GetActionFocusChangeName(aAction.mFocusChange),
|
|
|
|
sPresContext, sContent, sActiveIMEContentObserver));
|
|
|
|
|
2012-10-31 03:22:22 +04:00
|
|
|
bool focusActuallyChanging =
|
|
|
|
(sContent != aContent || sPresContext != aPresContext);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWidget> oldWidget =
|
2013-02-06 06:50:02 +04:00
|
|
|
sPresContext ? sPresContext->GetRootWidget() : nullptr;
|
2012-10-31 03:22:22 +04:00
|
|
|
if (oldWidget && focusActuallyChanging) {
|
|
|
|
// If we're deactivating, we shouldn't commit composition forcibly because
|
|
|
|
// the user may want to continue the composition.
|
|
|
|
if (aPresContext) {
|
|
|
|
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
if (sActiveIMEContentObserver &&
|
|
|
|
(aPresContext || !sActiveIMEContentObserver->KeepAliveDuringDeactive()) &&
|
|
|
|
!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
|
2014-07-16 09:13:28 +04:00
|
|
|
DestroyIMEContentObserver();
|
2012-10-26 04:49:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aPresContext) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnChangeFocusInternal(), "
|
|
|
|
"no nsPresContext is being activated"));
|
2012-10-26 04:49:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2012-10-31 03:22:22 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget =
|
|
|
|
(sPresContext == aPresContext) ? oldWidget.get() :
|
2013-02-06 06:50:02 +04:00
|
|
|
aPresContext->GetRootWidget();
|
2014-07-16 09:13:28 +04:00
|
|
|
if (NS_WARN_IF(!widget)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::OnChangeFocusInternal(), FAILED due to "
|
|
|
|
"no widget to manage its IME state"));
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
IMEState newState = GetNewIMEState(aPresContext, aContent);
|
2012-10-31 03:22:22 +04:00
|
|
|
if (!focusActuallyChanging) {
|
2006-04-11 20:37:58 +04:00
|
|
|
// actual focus isn't changing, but if IME enabled state is changing,
|
|
|
|
// we should do it.
|
2011-11-27 15:51:52 +04:00
|
|
|
InputContext context = widget->GetInputContext();
|
2011-11-27 15:51:53 +04:00
|
|
|
if (context.mIMEState.mEnabled == newState.mEnabled) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnChangeFocusInternal(), "
|
|
|
|
"neither focus nor IME state is changing"));
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-27 15:51:52 +04:00
|
|
|
aAction.mFocusChange = InputContextAction::FOCUS_NOT_CHANGED;
|
2012-10-31 03:22:22 +04:00
|
|
|
|
|
|
|
// Even if focus isn't changing actually, we should commit current
|
|
|
|
// composition here since the IME state is changing.
|
|
|
|
if (sPresContext && oldWidget && !focusActuallyChanging) {
|
|
|
|
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget);
|
|
|
|
}
|
2011-11-27 15:51:52 +04:00
|
|
|
} else if (aAction.mFocusChange == InputContextAction::FOCUS_NOT_CHANGED) {
|
|
|
|
// If aContent isn't null or aContent is null but editable, somebody gets
|
|
|
|
// focus.
|
2011-11-27 15:51:53 +04:00
|
|
|
bool gotFocus = aContent || (newState.mEnabled == IMEState::ENABLED);
|
2011-11-27 15:51:52 +04:00
|
|
|
aAction.mFocusChange =
|
|
|
|
gotFocus ? InputContextAction::GOT_FOCUS : InputContextAction::LOST_FOCUS;
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
// Update IME state for new focus widget
|
|
|
|
SetIMEState(newState, aContent, widget, aAction);
|
2006-04-11 20:37:58 +04:00
|
|
|
|
|
|
|
sPresContext = aPresContext;
|
2012-09-20 20:13:18 +04:00
|
|
|
if (sContent != aContent) {
|
|
|
|
NS_IF_RELEASE(sContent);
|
|
|
|
NS_IF_ADDREF(sContent = aContent);
|
|
|
|
}
|
2006-04-11 20:37:58 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// Don't call CreateIMEContentObserver() here, it should be called from
|
2012-10-26 04:49:13 +04:00
|
|
|
// focus event handler of editor.
|
|
|
|
|
2006-04-11 20:37:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2007-05-02 19:34:35 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnInstalledMenuKeyboardListener(bool aInstalling)
|
2007-05-02 19:34:35 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnInstalledMenuKeyboardListener(aInstalling=%s), "
|
|
|
|
"sInstalledMenuKeyboardListener=%s",
|
|
|
|
GetBoolName(aInstalling), GetBoolName(sInstalledMenuKeyboardListener)));
|
|
|
|
|
2007-05-02 19:34:35 +04:00
|
|
|
sInstalledMenuKeyboardListener = aInstalling;
|
2011-04-20 16:47:40 +04:00
|
|
|
|
2011-11-27 15:51:52 +04:00
|
|
|
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
|
|
|
|
aInstalling ? InputContextAction::MENU_GOT_PSEUDO_FOCUS :
|
|
|
|
InputContextAction::MENU_LOST_PSEUDO_FOCUS);
|
|
|
|
OnChangeFocusInternal(sPresContext, sContent, action);
|
2007-05-02 19:34:35 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 14:08:43 +04:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IMEStateManager::OnMouseButtonEventInEditor(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIDOMMouseEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(aPresContext=0x%p, "
|
|
|
|
"aContent=0x%p, aMouseEvent=0x%p), sPresContext=0x%p, sContent=0x%p",
|
|
|
|
aPresContext, aContent, aMouseEvent, sPresContext, sContent));
|
|
|
|
|
|
|
|
if (sPresContext != aPresContext || sContent != aContent) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(), "
|
|
|
|
"the mouse event isn't fired on the editor managed by ISM"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sActiveIMEContentObserver) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(), "
|
|
|
|
"there is no active IMEContentObserver"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(), "
|
|
|
|
"the active IMEContentObserver isn't managing the editor"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetMouseEvent* internalEvent =
|
|
|
|
aMouseEvent->GetInternalNSEvent()->AsMouseEvent();
|
|
|
|
if (NS_WARN_IF(!internalEvent)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(), "
|
|
|
|
"the internal event of aMouseEvent isn't WidgetMouseEvent"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool consumed =
|
|
|
|
sActiveIMEContentObserver->OnMouseButtonEvent(aPresContext, internalEvent);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
nsAutoString eventType;
|
|
|
|
aMouseEvent->GetType(eventType);
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnMouseButtonEventInEditor(), "
|
|
|
|
"mouse event (type=%s, button=%d) is %s",
|
|
|
|
NS_ConvertUTF16toUTF8(eventType).get(), internalEvent->button,
|
|
|
|
consumed ? "consumed" : "not consumed"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2011-11-27 15:51:53 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnClickInEditor(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIDOMMouseEvent* aMouseEvent)
|
2011-11-27 15:51:53 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnClickInEditor(aPresContext=0x%p, aContent=0x%p, "
|
|
|
|
"aMouseEvent=0x%p), sPresContext=0x%p, sContent=0x%p",
|
|
|
|
aPresContext, aContent, aMouseEvent, sPresContext, sContent));
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
if (sPresContext != aPresContext || sContent != aContent) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnClickInEditor(), "
|
|
|
|
"the mouse event isn't fired on the editor managed by ISM"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = aPresContext->GetRootWidget();
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(widget);
|
2011-11-27 15:51:53 +04:00
|
|
|
|
|
|
|
bool isTrusted;
|
2012-08-04 11:44:00 +04:00
|
|
|
nsresult rv = aMouseEvent->GetIsTrusted(&isTrusted);
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-11-27 15:51:53 +04:00
|
|
|
if (!isTrusted) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnClickInEditor(), "
|
|
|
|
"the mouse event isn't a trusted event"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return; // ignore untrusted event.
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:38:13 +04:00
|
|
|
int16_t button;
|
2011-11-27 15:51:53 +04:00
|
|
|
rv = aMouseEvent->GetButton(&button);
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-11-27 15:51:53 +04:00
|
|
|
if (button != 0) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnClickInEditor(), "
|
|
|
|
"the mouse event isn't a left mouse button event"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return; // not a left click event.
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t clickCount;
|
2011-11-27 15:51:53 +04:00
|
|
|
rv = aMouseEvent->GetDetail(&clickCount);
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-11-27 15:51:53 +04:00
|
|
|
if (clickCount != 1) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnClickInEditor(), "
|
|
|
|
"the mouse event isn't a single click event"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return; // should notify only first click event.
|
|
|
|
}
|
|
|
|
|
|
|
|
InputContextAction action(InputContextAction::CAUSE_MOUSE,
|
|
|
|
InputContextAction::FOCUS_NOT_CHANGED);
|
|
|
|
IMEState newState = GetNewIMEState(aPresContext, aContent);
|
|
|
|
SetIMEState(newState, aContent, widget, action);
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-26 04:49:13 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::OnFocusInEditor(nsPresContext* aPresContext,
|
2014-11-05 16:12:44 +03:00
|
|
|
nsIContent* aContent,
|
|
|
|
nsIEditor* aEditor)
|
2012-10-26 04:49:13 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
2014-11-05 16:12:44 +03:00
|
|
|
("ISM: IMEStateManager::OnFocusInEditor(aPresContext=0x%p, aContent=0x%p, "
|
|
|
|
"aEditor=0x%p), sPresContext=0x%p, sContent=0x%p, "
|
|
|
|
"sActiveIMEContentObserver=0x%p",
|
|
|
|
aPresContext, aContent, aEditor, sPresContext, sContent,
|
2014-07-16 09:13:28 +04:00
|
|
|
sActiveIMEContentObserver));
|
|
|
|
|
2012-10-26 04:49:13 +04:00
|
|
|
if (sPresContext != aPresContext || sContent != aContent) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnFocusInEditor(), "
|
|
|
|
"an editor not managed by ISM gets focus"));
|
2012-10-26 04:49:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// If the IMEContentObserver instance isn't managing the editor actually,
|
2012-11-09 12:40:39 +04:00
|
|
|
// we need to recreate the instance.
|
2014-03-08 05:20:07 +04:00
|
|
|
if (sActiveIMEContentObserver) {
|
|
|
|
if (sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::OnFocusInEditor(), "
|
|
|
|
"the editor is already being managed by sActiveIMEContentObserver"));
|
2012-11-09 12:40:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-16 09:13:28 +04:00
|
|
|
DestroyIMEContentObserver();
|
2012-11-09 12:40:39 +04:00
|
|
|
}
|
|
|
|
|
2014-11-05 16:12:44 +03:00
|
|
|
CreateIMEContentObserver(aEditor);
|
2012-10-26 04:49:13 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2010-05-04 21:40:39 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::UpdateIMEState(const IMEState& aNewIMEState,
|
2014-11-05 16:12:44 +03:00
|
|
|
nsIContent* aContent,
|
|
|
|
nsIEditor* aEditor)
|
2010-05-04 21:40:39 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::UpdateIMEState(aNewIMEState={ mEnabled=%s, "
|
2014-11-05 16:12:44 +03:00
|
|
|
"mOpen=%s }, aContent=0x%p, aEditor=0x%p), "
|
2014-07-17 09:08:44 +04:00
|
|
|
"sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p, "
|
|
|
|
"sIsGettingNewIMEState=%s",
|
2014-07-16 09:13:28 +04:00
|
|
|
GetIMEStateEnabledName(aNewIMEState.mEnabled),
|
2014-11-05 16:12:44 +03:00
|
|
|
GetIMEStateSetOpenName(aNewIMEState.mOpen), aContent, aEditor,
|
2014-07-17 09:08:44 +04:00
|
|
|
sPresContext, sContent, sActiveIMEContentObserver,
|
|
|
|
GetBoolName(sIsGettingNewIMEState)));
|
|
|
|
|
|
|
|
if (sIsGettingNewIMEState) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::UpdateIMEState(), "
|
|
|
|
"does nothing because of called while getting new IME state"));
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 09:13:28 +04:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!sPresContext)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::UpdateIMEState(), FAILED due to "
|
|
|
|
"no managing nsPresContext"));
|
2010-05-04 21:40:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
|
2014-07-16 09:13:28 +04:00
|
|
|
if (NS_WARN_IF(!widget)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::UpdateIMEState(), FAILED due to "
|
|
|
|
"no widget for the managing nsPresContext"));
|
2010-05-04 21:40:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// If the IMEContentObserver instance isn't managing the editor's current
|
2012-11-09 12:40:39 +04:00
|
|
|
// editable root content, the editor frame might be reframed. We should
|
|
|
|
// recreate the instance at that time.
|
|
|
|
bool createTextStateManager =
|
2014-03-08 05:20:07 +04:00
|
|
|
(!sActiveIMEContentObserver ||
|
|
|
|
!sActiveIMEContentObserver->IsManaging(sPresContext, aContent));
|
2010-05-27 06:04:14 +04:00
|
|
|
|
2012-11-09 12:40:39 +04:00
|
|
|
bool updateIMEState =
|
|
|
|
(widget->GetInputContext().mIMEState.mEnabled != aNewIMEState.mEnabled);
|
2010-05-04 21:40:39 +04:00
|
|
|
|
2012-11-09 12:40:39 +04:00
|
|
|
if (updateIMEState) {
|
|
|
|
// commit current composition before modifying IME state.
|
|
|
|
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, widget);
|
|
|
|
}
|
2012-10-26 04:49:13 +04:00
|
|
|
|
2012-11-09 12:40:39 +04:00
|
|
|
if (createTextStateManager) {
|
2014-07-16 09:13:28 +04:00
|
|
|
DestroyIMEContentObserver();
|
2012-11-09 12:40:39 +04:00
|
|
|
}
|
2012-10-26 04:49:13 +04:00
|
|
|
|
2012-11-09 12:40:39 +04:00
|
|
|
if (updateIMEState) {
|
|
|
|
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
|
|
|
|
InputContextAction::FOCUS_NOT_CHANGED);
|
|
|
|
SetIMEState(aNewIMEState, aContent, widget, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (createTextStateManager) {
|
2014-11-05 16:12:44 +03:00
|
|
|
CreateIMEContentObserver(aEditor);
|
2012-11-09 12:40:39 +04:00
|
|
|
}
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2011-11-27 15:51:53 +04:00
|
|
|
IMEState
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::GetNewIMEState(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
2006-04-11 20:37:58 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState(aPresContext=0x%p, aContent=0x%p), "
|
|
|
|
"sInstalledMenuKeyboardListener=%s",
|
|
|
|
aPresContext, aContent, GetBoolName(sInstalledMenuKeyboardListener)));
|
|
|
|
|
2006-04-11 20:37:58 +04:00
|
|
|
// On Printing or Print Preview, we don't need IME.
|
|
|
|
if (aPresContext->Type() == nsPresContext::eContext_PrintPreview ||
|
|
|
|
aPresContext->Type() == nsPresContext::eContext_Print) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState() returns DISABLED because "
|
|
|
|
"the nsPresContext is for print or print preview"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return IMEState(IMEState::DISABLED);
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
if (sInstalledMenuKeyboardListener) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState() returns DISABLED because "
|
|
|
|
"menu keyboard listener was installed"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return IMEState(IMEState::DISABLED);
|
|
|
|
}
|
2007-05-02 19:34:35 +04:00
|
|
|
|
2008-02-13 15:51:00 +03:00
|
|
|
if (!aContent) {
|
|
|
|
// Even if there are no focused content, the focused document might be
|
|
|
|
// editable, such case is design mode.
|
|
|
|
nsIDocument* doc = aPresContext->Document();
|
2011-11-27 15:51:53 +04:00
|
|
|
if (doc && doc->HasFlag(NODE_IS_EDITABLE)) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState() returns ENABLED because "
|
|
|
|
"design mode editor has focus"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return IMEState(IMEState::ENABLED);
|
|
|
|
}
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState() returns DISABLED because "
|
|
|
|
"no content has focus"));
|
2011-11-27 15:51:53 +04:00
|
|
|
return IMEState(IMEState::DISABLED);
|
2008-02-13 15:51:00 +03:00
|
|
|
}
|
|
|
|
|
2014-07-17 09:08:44 +04:00
|
|
|
// nsIContent::GetDesiredIMEState() may cause a call of UpdateIMEState()
|
|
|
|
// from nsEditor::PostCreate() because GetDesiredIMEState() needs to retrieve
|
|
|
|
// an editor instance for the element if it's editable element.
|
|
|
|
// For avoiding such nested IME state updates, we should set
|
|
|
|
// sIsGettingNewIMEState here and UpdateIMEState() should check it.
|
|
|
|
GettingNewIMEStateBlocker blocker;
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
IMEState newIMEState = aContent->GetDesiredIMEState();
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::GetNewIMEState() returns { mEnabled=%s, "
|
|
|
|
"mOpen=%s }",
|
|
|
|
GetIMEStateEnabledName(newIMEState.mEnabled),
|
|
|
|
GetIMEStateSetOpenName(newIMEState.mOpen)));
|
|
|
|
return newIMEState;
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
|
|
|
|
2010-10-18 20:37:00 +04:00
|
|
|
// Helper class, used for IME enabled state change notification
|
|
|
|
class IMEEnabledStateChangedEvent : public nsRunnable {
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit IMEEnabledStateChangedEvent(uint32_t aState)
|
2010-10-18 20:37:00 +04:00
|
|
|
: mState(aState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:20:08 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-03-08 05:20:07 +04:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
services::GetObserverService();
|
2010-10-18 20:37:00 +04:00
|
|
|
if (observerService) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEEnabledStateChangedEvent::Run(), notifies observers of "
|
|
|
|
"\"ime-enabled-state-changed\""));
|
2010-10-18 20:37:00 +04:00
|
|
|
nsAutoString state;
|
|
|
|
state.AppendInt(mState);
|
2014-03-08 05:20:08 +04:00
|
|
|
observerService->NotifyObservers(nullptr, "ime-enabled-state-changed",
|
|
|
|
state.get());
|
2010-10-18 20:37:00 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mState;
|
2010-10-18 20:37:00 +04:00
|
|
|
};
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2006-04-11 20:37:58 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::SetIMEState(const IMEState& aState,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIWidget* aWidget,
|
|
|
|
InputContextAction aAction)
|
2006-04-11 20:37:58 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::SetIMEState(aState={ mEnabled=%s, mOpen=%s }, "
|
|
|
|
"aContent=0x%p, aWidget=0x%p, aAction={ mCause=%s, mFocusChange=%s })",
|
|
|
|
GetIMEStateEnabledName(aState.mEnabled),
|
|
|
|
GetIMEStateSetOpenName(aState.mOpen), aContent, aWidget,
|
|
|
|
GetActionCauseName(aAction.mCause),
|
|
|
|
GetActionFocusChangeName(aAction.mFocusChange)));
|
|
|
|
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(aWidget);
|
2011-11-27 15:51:53 +04:00
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
InputContext oldContext = aWidget->GetInputContext();
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
InputContext context;
|
2011-11-27 15:51:53 +04:00
|
|
|
context.mIMEState = aState;
|
2011-11-27 15:51:53 +04:00
|
|
|
|
|
|
|
if (aContent && aContent->GetNameSpaceID() == kNameSpaceID_XHTML &&
|
|
|
|
(aContent->Tag() == nsGkAtoms::input ||
|
|
|
|
aContent->Tag() == nsGkAtoms::textarea)) {
|
2013-03-29 20:00:59 +04:00
|
|
|
if (aContent->Tag() != nsGkAtoms::textarea) {
|
2013-11-20 13:32:46 +04:00
|
|
|
// <input type=number> has an anonymous <input type=text> descendant
|
|
|
|
// that gets focus whenever anyone tries to focus the number control. We
|
|
|
|
// need to check if aContent is one of those anonymous text controls and,
|
|
|
|
// if so, use the number control instead:
|
|
|
|
nsIContent* content = aContent;
|
|
|
|
HTMLInputElement* inputElement =
|
|
|
|
HTMLInputElement::FromContentOrNull(aContent);
|
|
|
|
if (inputElement) {
|
|
|
|
HTMLInputElement* ownerNumberControl =
|
|
|
|
inputElement->GetOwnerNumberControl();
|
|
|
|
if (ownerNumberControl) {
|
|
|
|
content = ownerNumberControl; // an <input type=number>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content->GetAttr(kNameSpaceID_None, nsGkAtoms::type,
|
|
|
|
context.mHTMLInputType);
|
2013-03-29 20:00:59 +04:00
|
|
|
} else {
|
|
|
|
context.mHTMLInputType.Assign(nsGkAtoms::textarea->GetUTF16String());
|
|
|
|
}
|
2013-05-01 19:58:30 +04:00
|
|
|
|
|
|
|
if (Preferences::GetBool("dom.forms.inputmode", false)) {
|
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::inputmode,
|
|
|
|
context.mHTMLInputInputmode);
|
|
|
|
}
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::moz_action_hint,
|
|
|
|
context.mActionHint);
|
|
|
|
|
2014-07-18 20:53:41 +04:00
|
|
|
// Get the input content corresponding to the focused node,
|
|
|
|
// which may be an anonymous child of the input content.
|
|
|
|
nsIContent* inputContent = aContent->FindFirstNonChromeOnlyAccessContent();
|
|
|
|
|
|
|
|
// If we don't have an action hint and
|
|
|
|
// return won't submit the form, use "next".
|
|
|
|
if (context.mActionHint.IsEmpty() &&
|
|
|
|
inputContent->Tag() == nsGkAtoms::input) {
|
2011-11-27 15:51:53 +04:00
|
|
|
bool willSubmit = false;
|
2014-07-18 20:53:41 +04:00
|
|
|
nsCOMPtr<nsIFormControl> control(do_QueryInterface(inputContent));
|
2011-11-27 15:51:53 +04:00
|
|
|
mozilla::dom::Element* formElement = control->GetFormElement();
|
|
|
|
nsCOMPtr<nsIForm> form;
|
|
|
|
if (control) {
|
|
|
|
// is this a form and does it have a default submit element?
|
2014-03-08 05:20:08 +04:00
|
|
|
if ((form = do_QueryInterface(formElement)) &&
|
|
|
|
form->GetDefaultSubmitElement()) {
|
2011-11-27 15:51:53 +04:00
|
|
|
willSubmit = true;
|
|
|
|
// is this an html form and does it only have a single text input element?
|
2014-03-08 05:20:08 +04:00
|
|
|
} else if (formElement && formElement->Tag() == nsGkAtoms::form &&
|
|
|
|
formElement->IsHTML() &&
|
|
|
|
!static_cast<dom::HTMLFormElement*>(formElement)->
|
|
|
|
ImplicitSubmissionIsDisabled()) {
|
2011-11-27 15:51:53 +04:00
|
|
|
willSubmit = true;
|
2011-01-31 17:23:58 +03:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:20:08 +04:00
|
|
|
context.mActionHint.Assign(
|
|
|
|
willSubmit ? (control->GetType() == NS_FORM_INPUT_SEARCH ?
|
|
|
|
NS_LITERAL_STRING("search") : NS_LITERAL_STRING("go")) :
|
|
|
|
(formElement ?
|
|
|
|
NS_LITERAL_STRING("next") : EmptyString()));
|
2010-11-23 09:48:03 +03:00
|
|
|
}
|
2011-11-27 15:51:53 +04:00
|
|
|
}
|
2010-11-23 09:48:03 +03:00
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
// XXX I think that we should use nsContentUtils::IsCallerChrome() instead
|
|
|
|
// of the process type.
|
|
|
|
if (aAction.mCause == InputContextAction::CAUSE_UNKNOWN &&
|
|
|
|
XRE_GetProcessType() != GeckoProcessType_Content) {
|
|
|
|
aAction.mCause = InputContextAction::CAUSE_UNKNOWN_CHROME;
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
2011-11-27 15:51:53 +04:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
|
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::SetIMEState(), "
|
|
|
|
"calling nsIWidget::SetInputContext(context={ mIMEState={ mEnabled=%s, "
|
|
|
|
"mOpen=%s }, mHTMLInputType=\"%s\", mHTMLInputInputmode=\"%s\", "
|
|
|
|
"mActionHint=\"%s\" }, aAction={ mCause=%s, mAction=%s })",
|
|
|
|
GetIMEStateEnabledName(context.mIMEState.mEnabled),
|
|
|
|
GetIMEStateSetOpenName(context.mIMEState.mOpen),
|
|
|
|
NS_ConvertUTF16toUTF8(context.mHTMLInputType).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(context.mHTMLInputInputmode).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(context.mActionHint).get(),
|
|
|
|
GetActionCauseName(aAction.mCause),
|
|
|
|
GetActionFocusChangeName(aAction.mFocusChange)));
|
|
|
|
|
2011-11-27 15:51:53 +04:00
|
|
|
aWidget->SetInputContext(context, aAction);
|
2014-03-08 05:20:08 +04:00
|
|
|
if (oldContext.mIMEState.mEnabled == context.mIMEState.mEnabled) {
|
|
|
|
return;
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
2014-03-08 05:20:08 +04:00
|
|
|
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new IMEEnabledStateChangedEvent(context.mIMEState.mEnabled));
|
2006-04-11 20:37:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-09-26 09:47:45 +04:00
|
|
|
void
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::EnsureTextCompositionArray()
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
|
|
|
if (sTextCompositions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sTextCompositions = new TextCompositionArray();
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-09-26 09:47:45 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(
|
|
|
|
nsINode* aEventTargetNode,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetCompositionEvent* aCompositionEvent,
|
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack,
|
|
|
|
bool aIsSynthesized)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::DispatchCompositionEvent(aNode=0x%p, "
|
2014-10-07 14:01:49 +04:00
|
|
|
"aPresContext=0x%p, aCompositionEvent={ message=%s, "
|
2014-09-26 04:05:11 +04:00
|
|
|
"mFlags={ mIsTrusted=%s, mPropagationStopped=%s } }, "
|
|
|
|
"aIsSynthesized=%s)",
|
2014-07-16 09:13:28 +04:00
|
|
|
aEventTargetNode, aPresContext,
|
2014-10-07 14:01:49 +04:00
|
|
|
GetEventMessageName(aCompositionEvent->message),
|
|
|
|
GetBoolName(aCompositionEvent->mFlags.mIsTrusted),
|
|
|
|
GetBoolName(aCompositionEvent->mFlags.mPropagationStopped),
|
2014-09-26 04:05:11 +04:00
|
|
|
GetBoolName(aIsSynthesized)));
|
2014-07-16 09:13:28 +04:00
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
if (!aCompositionEvent->mFlags.mIsTrusted ||
|
|
|
|
aCompositionEvent->mFlags.mPropagationStopped) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(aCompositionEvent->message != NS_COMPOSITION_UPDATE,
|
2014-10-03 10:33:49 +04:00
|
|
|
"compositionupdate event shouldn't be dispatched manually");
|
2014-10-03 10:33:47 +04:00
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
EnsureTextCompositionArray();
|
|
|
|
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> composition =
|
2014-10-07 14:01:49 +04:00
|
|
|
sTextCompositions->GetCompositionFor(aCompositionEvent->widget);
|
2012-09-26 09:47:45 +04:00
|
|
|
if (!composition) {
|
2014-09-26 04:05:11 +04:00
|
|
|
// If synthesized event comes after delayed native composition events
|
|
|
|
// for request of commit or cancel, we should ignore it.
|
|
|
|
if (NS_WARN_IF(aIsSynthesized)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::DispatchCompositionEvent(), "
|
|
|
|
"adding new TextComposition to the array"));
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(aCompositionEvent->message == NS_COMPOSITION_START);
|
|
|
|
composition =
|
|
|
|
new TextComposition(aPresContext, aEventTargetNode, aCompositionEvent);
|
2014-01-28 12:19:29 +04:00
|
|
|
sTextCompositions->AppendElement(composition);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(aCompositionEvent->message != NS_COMPOSITION_START);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
#endif // #ifdef DEBUG
|
|
|
|
|
|
|
|
// Dispatch the event on composing target.
|
2014-10-07 14:01:49 +04:00
|
|
|
composition->DispatchCompositionEvent(aCompositionEvent, aStatus, aCallBack,
|
|
|
|
aIsSynthesized);
|
2012-09-26 09:47:45 +04:00
|
|
|
|
|
|
|
// WARNING: the |composition| might have been destroyed already.
|
|
|
|
|
|
|
|
// Remove the ended composition from the array.
|
2014-09-26 04:05:11 +04:00
|
|
|
// NOTE: When TextComposition is synthesizing compositionend event for
|
|
|
|
// emulating a commit, the instance shouldn't be removed from the array
|
|
|
|
// because IME may perform it later. Then, we need to ignore the
|
|
|
|
// following commit events in TextComposition::DispatchEvent().
|
2014-09-26 04:05:12 +04:00
|
|
|
// However, if commit or cancel for a request is performed synchronously
|
|
|
|
// during not safe to dispatch events, PresShell must have discarded
|
|
|
|
// compositionend event. Then, the synthesized compositionend event is
|
|
|
|
// the last event for the composition. In this case, we need to
|
|
|
|
// destroy the TextComposition with synthesized compositionend event.
|
|
|
|
if ((!aIsSynthesized ||
|
|
|
|
composition->WasNativeCompositionEndEventDiscarded()) &&
|
2014-11-25 08:02:30 +03:00
|
|
|
aCompositionEvent->CausesDOMCompositionEndEvent()) {
|
2012-09-26 09:47:45 +04:00
|
|
|
TextCompositionArray::index_type i =
|
2014-10-07 14:01:49 +04:00
|
|
|
sTextCompositions->IndexOf(aCompositionEvent->widget);
|
2012-09-26 09:47:45 +04:00
|
|
|
if (i != TextCompositionArray::NoIndex) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::DispatchCompositionEvent(), "
|
|
|
|
"removing TextComposition from the array since NS_COMPOSTION_END "
|
|
|
|
"was dispatched"));
|
2014-02-25 10:59:28 +04:00
|
|
|
sTextCompositions->ElementAt(i)->Destroy();
|
2012-09-26 09:47:45 +04:00
|
|
|
sTextCompositions->RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
// static
|
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
IMEStateManager::OnCompositionEventDiscarded(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
2014-09-26 04:05:12 +04:00
|
|
|
{
|
|
|
|
// Note that this method is never called for synthesized events for emulating
|
|
|
|
// commit or cancel composition.
|
|
|
|
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
2014-10-07 14:01:49 +04:00
|
|
|
("ISM: IMEStateManager::OnCompositionEventDiscarded(aCompositionEvent={ "
|
2014-09-26 04:05:12 +04:00
|
|
|
"message=%s, mFlags={ mIsTrusted=%s } })",
|
2014-10-07 14:01:49 +04:00
|
|
|
GetEventMessageName(aCompositionEvent->message),
|
|
|
|
GetBoolName(aCompositionEvent->mFlags.mIsTrusted)));
|
2014-09-26 04:05:12 +04:00
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
if (!aCompositionEvent->mFlags.mIsTrusted) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore compositionstart for now because sTextCompositions may not have
|
|
|
|
// been created yet.
|
2014-10-07 14:01:49 +04:00
|
|
|
if (aCompositionEvent->message == NS_COMPOSITION_START) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<TextComposition> composition =
|
2014-10-07 14:01:49 +04:00
|
|
|
sTextCompositions->GetCompositionFor(aCompositionEvent->widget);
|
2014-10-14 19:40:06 +04:00
|
|
|
if (!composition) {
|
|
|
|
// If the PresShell has been being destroyed during composition,
|
|
|
|
// a TextComposition instance for the composition was already removed from
|
|
|
|
// the array and destroyed in OnDestroyPresContext(). Therefore, we may
|
|
|
|
// fail to retrieve a TextComposition instance here.
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::OnCompositionEventDiscarded(), "
|
|
|
|
"TextComposition instance for the widget has already gone"));
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 14:01:49 +04:00
|
|
|
composition->OnCompositionEventDiscarded(aCompositionEvent);
|
2014-09-26 04:05:12 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::NotifyIME(IMEMessage aMessage,
|
|
|
|
nsIWidget* aWidget)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> composition;
|
2014-07-16 09:13:28 +04:00
|
|
|
if (aWidget && sTextCompositions) {
|
2012-09-26 09:47:51 +04:00
|
|
|
composition = sTextCompositions->GetCompositionFor(aWidget);
|
|
|
|
}
|
2014-07-16 09:13:28 +04:00
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
bool isSynthesizedForTests =
|
|
|
|
composition && composition->IsSynthesizedForTests();
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::NotifyIME(aMessage=%s, aWidget=0x%p), "
|
|
|
|
"composition=0x%p, composition->IsSynthesizedForTests()=%s",
|
|
|
|
GetNotifyIMEMessageName(aMessage), aWidget, composition.get(),
|
2014-09-26 04:05:12 +04:00
|
|
|
GetBoolName(isSynthesizedForTests)));
|
2014-07-16 09:13:28 +04:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!aWidget)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::NotifyIME(), FAILED due to no widget"));
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
switch (aMessage) {
|
2014-09-26 04:05:12 +04:00
|
|
|
case REQUEST_TO_COMMIT_COMPOSITION:
|
|
|
|
return composition ?
|
|
|
|
composition->RequestToCommit(aWidget, false) : NS_OK;
|
|
|
|
case REQUEST_TO_CANCEL_COMPOSITION:
|
|
|
|
return composition ?
|
|
|
|
composition->RequestToCommit(aWidget, true) : NS_OK;
|
|
|
|
case NOTIFY_IME_OF_COMPOSITION_UPDATE:
|
|
|
|
return composition && !isSynthesizedForTests ?
|
|
|
|
aWidget->NotifyIME(IMENotification(aMessage)) : NS_OK;
|
2012-09-26 09:47:51 +04:00
|
|
|
default:
|
2014-09-26 04:05:12 +04:00
|
|
|
MOZ_CRASH("Unsupported notification");
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
2014-09-26 04:05:12 +04:00
|
|
|
MOZ_CRASH(
|
|
|
|
"Failed to handle the notification for non-synthesized composition");
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::NotifyIME(IMEMessage aMessage,
|
|
|
|
nsPresContext* aPresContext)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::NotifyIME(aMessage=%s, aPresContext=0x%p)",
|
|
|
|
GetNotifyIMEMessageName(aMessage), aPresContext));
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
NS_ENSURE_TRUE(aPresContext, NS_ERROR_INVALID_ARG);
|
|
|
|
|
2013-02-06 06:50:02 +04:00
|
|
|
nsIWidget* widget = aPresContext->GetRootWidget();
|
2014-07-16 09:13:28 +04:00
|
|
|
if (NS_WARN_IF(!widget)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::NotifyIME(), FAILED due to no widget for the "
|
|
|
|
"nsPresContext"));
|
2012-09-26 09:47:51 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-02-18 04:00:15 +04:00
|
|
|
return NotifyIME(aMessage, widget);
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-26 04:49:13 +04:00
|
|
|
bool
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::IsEditable(nsINode* node)
|
2012-10-26 04:49:13 +04:00
|
|
|
{
|
2012-05-25 01:49:44 +04:00
|
|
|
if (node->IsEditable()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// |node| might be readwrite (for example, a text control)
|
2014-03-08 05:20:08 +04:00
|
|
|
if (node->IsElement() &&
|
|
|
|
node->AsElement()->State().HasState(NS_EVENT_STATE_MOZ_READWRITE)) {
|
2012-05-25 01:49:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-26 04:49:13 +04:00
|
|
|
nsINode*
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::GetRootEditableNode(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
2009-02-10 23:56:51 +03:00
|
|
|
{
|
|
|
|
if (aContent) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsINode* root = nullptr;
|
2011-03-23 17:45:21 +03:00
|
|
|
nsINode* node = aContent;
|
2012-05-25 01:49:44 +04:00
|
|
|
while (node && IsEditable(node)) {
|
2014-09-26 04:10:49 +04:00
|
|
|
// If the node has independent selection like <input type="text"> or
|
|
|
|
// <textarea>, the node should be the root editable node for aContent.
|
|
|
|
// FYI: <select> element also has independent selection but IsEditable()
|
|
|
|
// returns false.
|
|
|
|
// XXX: If somebody adds new editable element which has independent
|
|
|
|
// selection but doesn't own editor, we'll need more checks here.
|
|
|
|
if (node->IsContent() &&
|
|
|
|
node->AsContent()->HasIndependentSelection()) {
|
|
|
|
return node;
|
|
|
|
}
|
2011-03-23 17:45:21 +03:00
|
|
|
root = node;
|
2012-10-09 16:31:24 +04:00
|
|
|
node = node->GetParentNode();
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
if (aPresContext) {
|
|
|
|
nsIDocument* document = aPresContext->Document();
|
2014-03-08 05:20:08 +04:00
|
|
|
if (document && document->IsEditable()) {
|
2009-02-10 23:56:51 +03:00
|
|
|
return document;
|
2014-03-08 05:20:08 +04:00
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-31 03:22:22 +04:00
|
|
|
bool
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::IsEditableIMEState(nsIWidget* aWidget)
|
2012-10-31 03:22:22 +04:00
|
|
|
{
|
|
|
|
switch (aWidget->GetInputContext().mIMEState.mEnabled) {
|
2014-03-08 05:20:07 +04:00
|
|
|
case IMEState::ENABLED:
|
|
|
|
case IMEState::PASSWORD:
|
2012-10-31 03:22:22 +04:00
|
|
|
return true;
|
2014-03-08 05:20:07 +04:00
|
|
|
case IMEState::PLUGIN:
|
|
|
|
case IMEState::DISABLED:
|
2012-10-31 03:22:22 +04:00
|
|
|
return false;
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unknown IME enable state");
|
2012-10-31 03:22:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-26 04:49:13 +04:00
|
|
|
void
|
2014-07-16 09:13:28 +04:00
|
|
|
IMEStateManager::DestroyIMEContentObserver()
|
2009-02-10 23:56:51 +03:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
|
|
|
("ISM: IMEStateManager::DestroyIMEContentObserver(), "
|
|
|
|
"sActiveIMEContentObserver=0x%p",
|
|
|
|
sActiveIMEContentObserver));
|
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
if (!sActiveIMEContentObserver) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::DestroyIMEContentObserver() does nothing"));
|
2012-10-26 04:49:13 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::DestroyIMEContentObserver(), destroying "
|
|
|
|
"the active IMEContentObserver..."));
|
2014-03-08 05:20:07 +04:00
|
|
|
nsRefPtr<IMEContentObserver> tsm;
|
|
|
|
tsm.swap(sActiveIMEContentObserver);
|
2012-11-09 12:40:39 +04:00
|
|
|
tsm->Destroy();
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2012-10-26 04:49:13 +04:00
|
|
|
void
|
2014-11-05 16:12:44 +03:00
|
|
|
IMEStateManager::CreateIMEContentObserver(nsIEditor* aEditor)
|
2009-02-10 23:56:51 +03:00
|
|
|
{
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ALWAYS,
|
2014-11-05 16:12:44 +03:00
|
|
|
("ISM: IMEStateManager::CreateIMEContentObserver(aEditor=0x%p), "
|
2014-07-16 09:13:28 +04:00
|
|
|
"sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p, "
|
|
|
|
"sActiveIMEContentObserver->IsManaging(sPresContext, sContent)=%s",
|
2014-11-05 16:12:44 +03:00
|
|
|
aEditor, sPresContext, sContent, sActiveIMEContentObserver,
|
2014-07-16 09:13:28 +04:00
|
|
|
GetBoolName(sActiveIMEContentObserver ?
|
|
|
|
sActiveIMEContentObserver->IsManaging(sPresContext, sContent) : false)));
|
|
|
|
|
|
|
|
if (NS_WARN_IF(sActiveIMEContentObserver)) {
|
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::CreateIMEContentObserver(), FAILED due to "
|
|
|
|
"there is already an active IMEContentObserver"));
|
2014-03-08 05:20:07 +04:00
|
|
|
MOZ_ASSERT(sActiveIMEContentObserver->IsManaging(sPresContext, sContent));
|
2012-10-26 04:49:13 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
|
2013-02-06 06:50:02 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
|
2012-10-26 04:49:13 +04:00
|
|
|
if (!widget) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_ERROR,
|
|
|
|
("ISM: IMEStateManager::CreateIMEContentObserver(), FAILED due to "
|
|
|
|
"there is a root widget for the nsPresContext"));
|
2012-10-26 04:49:13 +04:00
|
|
|
return; // Sometimes, there are no widgets.
|
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// If it's not text ediable, we don't need to create IMEContentObserver.
|
2012-10-31 03:22:22 +04:00
|
|
|
if (!IsEditableIMEState(widget)) {
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::CreateIMEContentObserver() doesn't create "
|
|
|
|
"IMEContentObserver because of non-editable IME state"));
|
2012-10-31 03:22:22 +04:00
|
|
|
return;
|
2012-10-26 04:49:13 +04:00
|
|
|
}
|
2009-02-10 23:56:51 +03:00
|
|
|
|
2012-11-09 12:40:40 +04:00
|
|
|
static bool sInitializeIsTestingIME = true;
|
|
|
|
if (sInitializeIsTestingIME) {
|
|
|
|
Preferences::AddBoolVarCache(&sIsTestingIME, "test.IME", false);
|
|
|
|
sInitializeIsTestingIME = false;
|
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
PR_LOG(sISMLog, PR_LOG_DEBUG,
|
|
|
|
("ISM: IMEStateManager::CreateIMEContentObserver() is creating an "
|
|
|
|
"IMEContentObserver instance..."));
|
2014-03-08 05:20:07 +04:00
|
|
|
sActiveIMEContentObserver = new IMEContentObserver();
|
|
|
|
NS_ADDREF(sActiveIMEContentObserver);
|
2012-11-16 20:43:04 +04:00
|
|
|
|
2014-03-08 05:20:07 +04:00
|
|
|
// IMEContentObserver::Init() might create another IMEContentObserver
|
|
|
|
// instance. So, sActiveIMEContentObserver would be replaced with new one.
|
2012-11-16 20:43:04 +04:00
|
|
|
// We should hold the current instance here.
|
2014-03-08 05:20:07 +04:00
|
|
|
nsRefPtr<IMEContentObserver> kungFuDeathGrip(sActiveIMEContentObserver);
|
2014-11-05 16:12:44 +03:00
|
|
|
sActiveIMEContentObserver->Init(widget, sPresContext, sContent, aEditor);
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
|
|
|
|
2014-07-16 09:13:28 +04:00
|
|
|
// static
|
2009-02-10 23:56:51 +03:00
|
|
|
nsresult
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::GetFocusSelectionAndRoot(nsISelection** aSelection,
|
|
|
|
nsIContent** aRootContent)
|
2009-02-10 23:56:51 +03:00
|
|
|
{
|
2014-03-08 05:20:07 +04:00
|
|
|
if (!sActiveIMEContentObserver) {
|
2009-02-10 23:56:51 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-03-08 05:20:07 +04:00
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
return sActiveIMEContentObserver->GetSelectionAndRoot(aSelection,
|
|
|
|
aRootContent);
|
2009-02-10 23:56:51 +03:00
|
|
|
}
|
2013-11-07 04:11:11 +04:00
|
|
|
|
2014-02-12 17:02:55 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<TextComposition>
|
2014-03-08 05:20:07 +04:00
|
|
|
IMEStateManager::GetTextCompositionFor(nsIWidget* aWidget)
|
2013-11-07 04:11:11 +04:00
|
|
|
{
|
2014-02-12 17:02:55 +04:00
|
|
|
if (!sTextCompositions) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsRefPtr<TextComposition> textComposition =
|
|
|
|
sTextCompositions->GetCompositionFor(aWidget);
|
|
|
|
return textComposition.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<TextComposition>
|
2014-10-07 14:01:49 +04:00
|
|
|
IMEStateManager::GetTextCompositionFor(WidgetGUIEvent* aGUIEvent)
|
2014-02-12 17:02:55 +04:00
|
|
|
{
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(aGUIEvent->AsCompositionEvent() || aGUIEvent->AsKeyboardEvent(),
|
|
|
|
"aGUIEvent has to be WidgetCompositionEvent or WidgetKeyboardEvent");
|
|
|
|
return GetTextCompositionFor(aGUIEvent->widget);
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
2014-03-08 05:20:07 +04:00
|
|
|
|
|
|
|
} // namespace mozilla
|