2006-04-11 16:37:58 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Japan.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Masayuki Nakano <masayuki@d-toybox.com>
|
2009-02-10 20:56:51 +00:00
|
|
|
* Ningjie Chen <chenn@email.uc.edu>
|
2006-04-11 16:37:58 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIMEStateManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIViewManager.h"
|
|
|
|
#include "nsIViewObserver.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIEditorDocShell.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2007-04-15 13:43:55 +00:00
|
|
|
#include "nsContentUtils.h"
|
2009-02-10 20:56:51 +00:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsRange.h"
|
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsISelectionListener.h"
|
|
|
|
#include "nsISelectionController.h"
|
|
|
|
#include "nsIMutationObserver.h"
|
|
|
|
#include "nsContentEventHandler.h"
|
2010-10-18 16:37:00 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "mozilla/Services.h"
|
2011-01-31 14:23:58 +00:00
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIForm.h"
|
|
|
|
#include "nsHTMLFormElement.h"
|
2006-04-11 16:37:58 +00:00
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* nsIMEStateManager */
|
|
|
|
/******************************************************************/
|
|
|
|
|
|
|
|
nsIContent* nsIMEStateManager::sContent = nsnull;
|
|
|
|
nsPresContext* nsIMEStateManager::sPresContext = nsnull;
|
2007-05-02 15:34:35 +00:00
|
|
|
PRBool nsIMEStateManager::sInstalledMenuKeyboardListener = PR_FALSE;
|
2011-02-09 19:00:58 +00:00
|
|
|
PRBool nsIMEStateManager::sInSecureInputMode = PR_FALSE;
|
2006-04-11 16:37:58 +00:00
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
nsTextStateManager* nsIMEStateManager::sTextStateObserver = nsnull;
|
|
|
|
|
2006-04-11 16:37:58 +00:00
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::OnDestroyPresContext(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
|
|
|
if (aPresContext != sPresContext)
|
|
|
|
return NS_OK;
|
2009-12-04 08:03:27 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget(sPresContext);
|
|
|
|
if (widget) {
|
|
|
|
PRUint32 newState = GetNewIMEState(sPresContext, nsnull);
|
2011-04-20 12:47:40 +00:00
|
|
|
SetIMEState(newState, nsnull, widget, IMEContext::FOCUS_REMOVED);
|
2009-12-04 08:03:27 +00:00
|
|
|
}
|
2006-04-11 16:37:58 +00:00
|
|
|
sContent = nsnull;
|
|
|
|
sPresContext = nsnull;
|
2009-02-10 20:56:51 +00:00
|
|
|
OnTextStateBlur(nsnull, nsnull);
|
2006-04-11 16:37:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::OnRemoveContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
|
|
|
if (!sPresContext || !sContent ||
|
|
|
|
aPresContext != sPresContext ||
|
|
|
|
aContent != sContent)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Current IME transaction should commit
|
2008-07-14 02:56:18 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget(sPresContext);
|
|
|
|
if (widget) {
|
|
|
|
nsresult rv = widget->CancelIMEComposition();
|
2006-04-11 16:37:58 +00:00
|
|
|
if (NS_FAILED(rv))
|
2008-07-14 02:56:18 +00:00
|
|
|
widget->ResetInputState();
|
2009-12-04 08:03:27 +00:00
|
|
|
PRUint32 newState = GetNewIMEState(sPresContext, nsnull);
|
2011-04-20 12:47:40 +00:00
|
|
|
SetIMEState(newState, nsnull, widget, IMEContext::FOCUS_REMOVED);
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sContent = nsnull;
|
|
|
|
sPresContext = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::OnChangeFocus(nsPresContext* aPresContext,
|
2011-04-20 12:47:40 +00:00
|
|
|
nsIContent* aContent,
|
|
|
|
PRUint32 aReason)
|
2006-04-11 16:37:58 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
|
|
|
|
2008-07-14 02:56:18 +00:00
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget(aPresContext);
|
|
|
|
if (!widget) {
|
2006-04-11 16:37:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-09 19:00:58 +00:00
|
|
|
// Handle secure input mode for password field input.
|
|
|
|
PRBool contentIsPassword = PR_FALSE;
|
|
|
|
if (aContent && aContent->GetNameSpaceID() == kNameSpaceID_XHTML) {
|
|
|
|
if (aContent->Tag() == nsGkAtoms::input) {
|
|
|
|
nsAutoString type;
|
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
|
|
|
|
contentIsPassword = type.LowerCaseEqualsLiteral("password");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sInSecureInputMode) {
|
|
|
|
if (!contentIsPassword) {
|
|
|
|
if (NS_SUCCEEDED(widget->EndSecureKeyboardInput())) {
|
|
|
|
sInSecureInputMode = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (contentIsPassword) {
|
|
|
|
if (NS_SUCCEEDED(widget->BeginSecureKeyboardInput())) {
|
|
|
|
sInSecureInputMode = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-11 16:37:58 +00:00
|
|
|
PRUint32 newState = GetNewIMEState(aPresContext, aContent);
|
|
|
|
if (aPresContext == sPresContext && aContent == sContent) {
|
|
|
|
// actual focus isn't changing, but if IME enabled state is changing,
|
|
|
|
// we should do it.
|
|
|
|
PRUint32 newEnabledState = newState & nsIContent::IME_STATUS_MASK_ENABLED;
|
|
|
|
if (newEnabledState == 0) {
|
|
|
|
// the enabled state isn't changing, we should do nothing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-23 06:48:45 +00:00
|
|
|
IMEContext context;
|
2011-03-25 15:03:35 +00:00
|
|
|
if (!widget || NS_FAILED(widget->GetInputMode(context))) {
|
2006-04-11 16:37:58 +00:00
|
|
|
// this platform doesn't support IME controlling
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-23 06:48:45 +00:00
|
|
|
if (context.mStatus ==
|
2008-07-14 02:56:18 +00:00
|
|
|
nsContentUtils::GetWidgetStatusFromIMEStatus(newEnabledState)) {
|
2006-04-11 16:37:58 +00:00
|
|
|
// the enabled state isn't changing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current IME transaction should commit
|
|
|
|
if (sPresContext) {
|
2008-07-14 02:56:18 +00:00
|
|
|
nsCOMPtr<nsIWidget> oldWidget;
|
2006-04-11 16:37:58 +00:00
|
|
|
if (sPresContext == aPresContext)
|
2008-07-14 02:56:18 +00:00
|
|
|
oldWidget = widget;
|
2006-04-11 16:37:58 +00:00
|
|
|
else
|
2008-07-14 02:56:18 +00:00
|
|
|
oldWidget = GetWidget(sPresContext);
|
|
|
|
if (oldWidget)
|
|
|
|
oldWidget->ResetInputState();
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newState != nsIContent::IME_STATUS_NONE) {
|
|
|
|
// Update IME state for new focus widget
|
2011-04-20 12:47:40 +00:00
|
|
|
SetIMEState(newState, aContent, widget, aReason);
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sPresContext = aPresContext;
|
|
|
|
sContent = aContent;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-02 15:34:35 +00:00
|
|
|
void
|
|
|
|
nsIMEStateManager::OnInstalledMenuKeyboardListener(PRBool aInstalling)
|
|
|
|
{
|
|
|
|
sInstalledMenuKeyboardListener = aInstalling;
|
2011-04-20 12:47:40 +00:00
|
|
|
|
|
|
|
PRUint32 reason = aInstalling ? IMEContext::FOCUS_MOVED_TO_MENU
|
|
|
|
: IMEContext::FOCUS_MOVED_FROM_MENU;
|
|
|
|
OnChangeFocus(sPresContext, sContent, reason);
|
2007-05-02 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 17:40:39 +00:00
|
|
|
void
|
2010-11-23 06:48:03 +00:00
|
|
|
nsIMEStateManager::UpdateIMEState(PRUint32 aNewIMEState, nsIContent* aContent)
|
2010-05-04 17:40:39 +00:00
|
|
|
{
|
|
|
|
if (!sPresContext) {
|
|
|
|
NS_WARNING("ISM doesn't know which editor has focus");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_PRECONDITION(aNewIMEState != 0, "aNewIMEState doesn't specify new state.");
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget(sPresContext);
|
|
|
|
if (!widget) {
|
|
|
|
NS_WARNING("focused widget is not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-27 02:04:14 +00:00
|
|
|
// Don't update IME state when enabled state isn't actually changed.
|
2010-11-23 06:48:45 +00:00
|
|
|
IMEContext context;
|
2011-03-25 15:03:35 +00:00
|
|
|
nsresult rv = widget->GetInputMode(context);
|
2010-05-27 02:04:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return; // This platform doesn't support controling the IME state.
|
|
|
|
}
|
|
|
|
PRUint32 newEnabledState = aNewIMEState & nsIContent::IME_STATUS_MASK_ENABLED;
|
2010-11-23 06:48:45 +00:00
|
|
|
if (context.mStatus ==
|
2010-05-27 02:04:14 +00:00
|
|
|
nsContentUtils::GetWidgetStatusFromIMEStatus(newEnabledState)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-04 17:40:39 +00:00
|
|
|
// commit current composition
|
|
|
|
widget->ResetInputState();
|
|
|
|
|
2011-04-20 12:47:40 +00:00
|
|
|
SetIMEState(aNewIMEState, aContent, widget, IMEContext::EDITOR_STATE_MODIFIED);
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
|
|
|
|
2006-04-11 16:37:58 +00:00
|
|
|
PRUint32
|
|
|
|
nsIMEStateManager::GetNewIMEState(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
// On Printing or Print Preview, we don't need IME.
|
|
|
|
if (aPresContext->Type() == nsPresContext::eContext_PrintPreview ||
|
|
|
|
aPresContext->Type() == nsPresContext::eContext_Print) {
|
|
|
|
return nsIContent::IME_STATUS_DISABLE;
|
|
|
|
}
|
|
|
|
|
2008-02-13 12:51:00 +00:00
|
|
|
if (sInstalledMenuKeyboardListener)
|
2007-05-02 15:34:35 +00:00
|
|
|
return nsIContent::IME_STATUS_DISABLE;
|
|
|
|
|
2008-02-13 12:51:00 +00: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();
|
|
|
|
if (doc && doc->HasFlag(NODE_IS_EDITABLE))
|
|
|
|
return nsIContent::IME_STATUS_ENABLE;
|
|
|
|
return nsIContent::IME_STATUS_DISABLE;
|
|
|
|
}
|
|
|
|
|
2008-02-08 17:58:09 +00:00
|
|
|
return aContent->GetDesiredIMEState();
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 16:37:00 +00:00
|
|
|
// Helper class, used for IME enabled state change notification
|
|
|
|
class IMEEnabledStateChangedEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
IMEEnabledStateChangedEvent(PRUint32 aState)
|
|
|
|
: mState(aState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
nsAutoString state;
|
|
|
|
state.AppendInt(mState);
|
|
|
|
observerService->NotifyObservers(nsnull, "ime-enabled-state-changed", state.get());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 mState;
|
|
|
|
};
|
|
|
|
|
2006-04-11 16:37:58 +00:00
|
|
|
void
|
2010-05-04 17:40:39 +00:00
|
|
|
nsIMEStateManager::SetIMEState(PRUint32 aState,
|
2010-11-23 06:48:03 +00:00
|
|
|
nsIContent* aContent,
|
2011-04-20 12:47:40 +00:00
|
|
|
nsIWidget* aWidget,
|
|
|
|
PRUint32 aReason)
|
2006-04-11 16:37:58 +00:00
|
|
|
{
|
|
|
|
if (aState & nsIContent::IME_STATUS_MASK_ENABLED) {
|
2011-03-25 15:03:35 +00:00
|
|
|
if (!aWidget)
|
2010-11-23 06:48:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
PRUint32 state = nsContentUtils::GetWidgetStatusFromIMEStatus(aState);
|
|
|
|
IMEContext context;
|
|
|
|
context.mStatus = state;
|
2010-11-24 02:12:53 +00:00
|
|
|
|
|
|
|
if (aContent && aContent->GetNameSpaceID() == kNameSpaceID_XHTML &&
|
|
|
|
(aContent->Tag() == nsGkAtoms::input ||
|
|
|
|
aContent->Tag() == nsGkAtoms::textarea)) {
|
2010-11-23 06:48:03 +00:00
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::type,
|
|
|
|
context.mHTMLInputType);
|
2010-11-24 02:12:53 +00:00
|
|
|
aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::moz_action_hint,
|
|
|
|
context.mActionHint);
|
2011-01-31 14:23:58 +00:00
|
|
|
|
|
|
|
// if we don't have an action hint and return won't submit the form use "next"
|
|
|
|
if (context.mActionHint.IsEmpty() && aContent->Tag() == nsGkAtoms::input) {
|
|
|
|
PRBool willSubmit = PR_FALSE;
|
|
|
|
nsCOMPtr<nsIFormControl> control(do_QueryInterface(aContent));
|
|
|
|
mozilla::dom::Element* formElement = control->GetFormElement();
|
|
|
|
nsCOMPtr<nsIForm> form;
|
|
|
|
if (control) {
|
|
|
|
// is this a form and does it have a default submit element?
|
|
|
|
if ((form = do_QueryInterface(formElement)) && form->GetDefaultSubmitElement()) {
|
|
|
|
willSubmit = PR_TRUE;
|
|
|
|
// is this an html form and does it only have a single text input element?
|
|
|
|
} else if (formElement && formElement->Tag() == nsGkAtoms::form && formElement->IsHTML() &&
|
|
|
|
static_cast<nsHTMLFormElement*>(formElement)->HasSingleTextControl()) {
|
|
|
|
willSubmit = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2011-07-25 17:16:09 +00:00
|
|
|
context.mActionHint.Assign(willSubmit ? control->GetType() == NS_FORM_INPUT_SEARCH
|
|
|
|
? NS_LITERAL_STRING("search")
|
|
|
|
: NS_LITERAL_STRING("go")
|
|
|
|
: NS_LITERAL_STRING("next"));
|
2011-01-31 14:23:58 +00:00
|
|
|
}
|
2010-11-23 06:48:03 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 12:47:40 +00:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
context.mReason = aReason | IMEContext::FOCUS_FROM_CONTENT_PROCESS;
|
|
|
|
} else {
|
|
|
|
context.mReason = aReason;
|
|
|
|
}
|
|
|
|
|
2011-03-25 15:03:35 +00:00
|
|
|
aWidget->SetInputMode(context);
|
2010-10-18 16:37:00 +00:00
|
|
|
|
|
|
|
nsContentUtils::AddScriptRunner(new IMEEnabledStateChangedEvent(state));
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
if (aState & nsIContent::IME_STATUS_MASK_OPENED) {
|
2008-02-10 06:16:30 +00:00
|
|
|
PRBool open = !!(aState & nsIContent::IME_STATUS_OPEN);
|
2010-05-04 17:40:39 +00:00
|
|
|
aWidget->SetIMEOpenState(open);
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-14 02:56:18 +00:00
|
|
|
nsIWidget*
|
|
|
|
nsIMEStateManager::GetWidget(nsPresContext* aPresContext)
|
2006-04-11 16:37:58 +00:00
|
|
|
{
|
2009-03-11 15:43:08 +00:00
|
|
|
nsIPresShell* shell = aPresContext->GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(shell, nsnull);
|
|
|
|
|
|
|
|
nsIViewManager* vm = shell->GetViewManager();
|
2006-04-11 16:37:58 +00:00
|
|
|
if (!vm)
|
|
|
|
return nsnull;
|
|
|
|
nsCOMPtr<nsIWidget> widget = nsnull;
|
2009-07-22 00:45:05 +00:00
|
|
|
nsresult rv = vm->GetRootWidget(getter_AddRefs(widget));
|
2006-04-11 16:37:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
2008-07-14 02:56:18 +00:00
|
|
|
return widget;
|
2006-04-11 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
|
|
|
|
// nsTextStateManager notifies widget of any text and selection changes
|
|
|
|
// in the currently focused editor
|
|
|
|
// sTextStateObserver points to the currently active nsTextStateManager
|
|
|
|
// sTextStateObserver is null if there is no focused editor
|
|
|
|
|
|
|
|
class nsTextStateManager : public nsISelectionListener,
|
|
|
|
public nsStubMutationObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsTextStateManager();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISELECTIONLISTENER
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
|
|
|
|
|
|
|
|
nsresult Init(nsIWidget* aWidget,
|
|
|
|
nsPresContext* aPresContext,
|
2010-09-24 03:28:15 +00:00
|
|
|
nsINode* aNode,
|
|
|
|
PRBool aWantUpdates);
|
2009-02-10 20:56:51 +00:00
|
|
|
void Destroy(void);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWidget> mWidget;
|
|
|
|
nsCOMPtr<nsISelection> mSel;
|
|
|
|
nsCOMPtr<nsIContent> mRootContent;
|
|
|
|
nsCOMPtr<nsINode> mEditableNode;
|
|
|
|
PRBool mDestroying;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void NotifyContentAdded(nsINode* aContainer, PRInt32 aStart, PRInt32 aEnd);
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTextStateManager::nsTextStateManager()
|
|
|
|
{
|
|
|
|
mDestroying = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextStateManager::Init(nsIWidget* aWidget,
|
|
|
|
nsPresContext* aPresContext,
|
2010-09-24 03:28:15 +00:00
|
|
|
nsINode* aNode,
|
|
|
|
PRBool aWantUpdates)
|
2009-02-10 20:56:51 +00:00
|
|
|
{
|
|
|
|
mWidget = aWidget;
|
2011-08-09 23:05:00 +00:00
|
|
|
MOZ_ASSERT(mWidget);
|
2010-09-24 03:28:15 +00:00
|
|
|
if (!aWantUpdates) {
|
|
|
|
mEditableNode = aNode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
nsIPresShell* presShell = aPresContext->PresShell();
|
|
|
|
|
|
|
|
// get selection and root content
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
if (aNode->IsNodeOfType(nsINode::eCONTENT)) {
|
2009-12-24 21:20:05 +00:00
|
|
|
nsIFrame* frame = static_cast<nsIContent*>(aNode)->GetPrimaryFrame();
|
2009-02-10 20:56:51 +00:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
frame->GetSelectionController(aPresContext,
|
|
|
|
getter_AddRefs(selCon));
|
|
|
|
} else {
|
|
|
|
// aNode is a document
|
|
|
|
selCon = do_QueryInterface(presShell);
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> sel;
|
|
|
|
nsresult rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(sel));
|
|
|
|
NS_ENSURE_TRUE(sel, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> selDomRange;
|
|
|
|
rv = sel->GetRangeAt(0, getter_AddRefs(selDomRange));
|
|
|
|
|
2010-09-24 03:34:28 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIRange> selRange(do_QueryInterface(selDomRange));
|
|
|
|
NS_ENSURE_TRUE(selRange && selRange->GetStartParent(),
|
|
|
|
NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
mRootContent = selRange->GetStartParent()->
|
2009-02-10 20:56:51 +00:00
|
|
|
GetSelectionRootContent(presShell);
|
2010-09-24 03:34:28 +00:00
|
|
|
} else {
|
|
|
|
mRootContent = aNode->GetSelectionRootContent(presShell);
|
|
|
|
}
|
2009-04-17 05:01:23 +00:00
|
|
|
if (!mRootContent && aNode->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// The document node is editable, but there are no contents, this document
|
|
|
|
// is not editable.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(mRootContent, NS_ERROR_UNEXPECTED);
|
2009-02-10 20:56:51 +00:00
|
|
|
|
|
|
|
// add text change observer
|
|
|
|
mRootContent->AddMutationObserver(this);
|
|
|
|
|
|
|
|
// add selection change listener
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(sel));
|
|
|
|
NS_ENSURE_TRUE(selPrivate, NS_ERROR_UNEXPECTED);
|
|
|
|
rv = selPrivate->AddSelectionListener(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mSel = sel;
|
|
|
|
|
|
|
|
mEditableNode = aNode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextStateManager::Destroy(void)
|
|
|
|
{
|
|
|
|
if (mSel) {
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(mSel));
|
|
|
|
if (selPrivate)
|
|
|
|
selPrivate->RemoveSelectionListener(this);
|
|
|
|
mSel = nsnull;
|
|
|
|
}
|
|
|
|
if (mRootContent) {
|
|
|
|
mRootContent->RemoveMutationObserver(this);
|
|
|
|
mRootContent = nsnull;
|
|
|
|
}
|
|
|
|
mEditableNode = nsnull;
|
|
|
|
mWidget = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsTextStateManager,
|
|
|
|
nsIMutationObserver,
|
|
|
|
nsISelectionListener)
|
|
|
|
|
2010-08-04 19:22:50 +00:00
|
|
|
// Helper class, used for selection change notification
|
|
|
|
class SelectionChangeEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
SelectionChangeEvent(nsIWidget *widget)
|
|
|
|
: mWidget(widget)
|
|
|
|
{
|
2011-08-09 23:05:00 +00:00
|
|
|
MOZ_ASSERT(mWidget);
|
2010-08-04 19:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2011-08-09 23:05:00 +00:00
|
|
|
if(mWidget) {
|
|
|
|
mWidget->OnIMESelectionChange();
|
|
|
|
}
|
2010-08-04 19:22:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIWidget> mWidget;
|
|
|
|
};
|
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
nsresult
|
|
|
|
nsTextStateManager::NotifySelectionChanged(nsIDOMDocument* aDoc,
|
|
|
|
nsISelection* aSel,
|
|
|
|
PRInt16 aReason)
|
|
|
|
{
|
|
|
|
PRInt32 count = 0;
|
|
|
|
nsresult rv = aSel->GetRangeCount(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-09 23:05:00 +00:00
|
|
|
if (count > 0 && mWidget) {
|
2010-08-04 19:22:50 +00:00
|
|
|
nsContentUtils::AddScriptRunner(new SelectionChangeEvent(mWidget));
|
2009-02-10 20:56:51 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:22:50 +00:00
|
|
|
// Helper class, used for text change notification
|
|
|
|
class TextChangeEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
TextChangeEvent(nsIWidget *widget,
|
|
|
|
PRUint32 start, PRUint32 oldEnd, PRUint32 newEnd)
|
|
|
|
: mWidget(widget)
|
|
|
|
, mStart(start)
|
|
|
|
, mOldEnd(oldEnd)
|
|
|
|
, mNewEnd(newEnd)
|
|
|
|
{
|
2011-08-09 23:05:00 +00:00
|
|
|
MOZ_ASSERT(mWidget);
|
2010-08-04 19:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2011-08-09 23:05:00 +00:00
|
|
|
if(mWidget) {
|
|
|
|
mWidget->OnIMETextChange(mStart, mOldEnd, mNewEnd);
|
|
|
|
}
|
2010-08-04 19:22:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIWidget> mWidget;
|
|
|
|
PRUint32 mStart, mOldEnd, mNewEnd;
|
|
|
|
};
|
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
void
|
|
|
|
nsTextStateManager::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
|
|
|
|
"character data changed for non-text node");
|
|
|
|
|
|
|
|
PRUint32 offset = 0;
|
|
|
|
// get offsets of change and fire notification
|
|
|
|
if (NS_FAILED(nsContentEventHandler::GetFlatTextOffsetOfRange(
|
|
|
|
mRootContent, aContent, aInfo->mChangeStart, &offset)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PRUint32 oldEnd = offset + aInfo->mChangeEnd - aInfo->mChangeStart;
|
|
|
|
PRUint32 newEnd = offset + aInfo->mReplaceLength;
|
2010-08-04 19:22:50 +00:00
|
|
|
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new TextChangeEvent(mWidget, offset, oldEnd, newEnd));
|
2009-02-10 20:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextStateManager::NotifyContentAdded(nsINode* aContainer,
|
|
|
|
PRInt32 aStartIndex,
|
|
|
|
PRInt32 aEndIndex)
|
|
|
|
{
|
|
|
|
PRUint32 offset = 0, newOffset = 0;
|
|
|
|
if (NS_FAILED(nsContentEventHandler::GetFlatTextOffsetOfRange(
|
|
|
|
mRootContent, aContainer, aStartIndex, &offset)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get offset at the end of the last added node
|
|
|
|
if (NS_FAILED(nsContentEventHandler::GetFlatTextOffsetOfRange(
|
|
|
|
aContainer->GetChildAt(aStartIndex),
|
|
|
|
aContainer, aEndIndex, &newOffset)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// fire notification
|
|
|
|
if (newOffset)
|
2010-08-04 19:22:50 +00:00
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new TextChangeEvent(mWidget, offset, offset, offset + newOffset));
|
2009-02-10 20:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextStateManager::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2010-05-11 01:12:34 +00:00
|
|
|
nsIContent* aFirstNewContent,
|
2009-02-10 20:56:51 +00:00
|
|
|
PRInt32 aNewIndexInContainer)
|
|
|
|
{
|
|
|
|
NotifyContentAdded(aContainer, aNewIndexInContainer,
|
|
|
|
aContainer->GetChildCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextStateManager::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
NotifyContentAdded(NODE_FROM(aContainer, aDocument),
|
|
|
|
aIndexInContainer, aIndexInContainer + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextStateManager::ContentRemoved(nsIDocument* aDocument,
|
2010-07-21 22:05:17 +00:00
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer,
|
|
|
|
nsIContent* aPreviousSibling)
|
2009-02-10 20:56:51 +00:00
|
|
|
{
|
|
|
|
PRUint32 offset = 0, childOffset = 1;
|
|
|
|
if (NS_FAILED(nsContentEventHandler::GetFlatTextOffsetOfRange(
|
|
|
|
mRootContent, NODE_FROM(aContainer, aDocument),
|
|
|
|
aIndexInContainer, &offset)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get offset at the end of the deleted node
|
|
|
|
if (aChild->IsNodeOfType(nsINode::eTEXT))
|
|
|
|
childOffset = aChild->TextLength();
|
|
|
|
else if (0 < aChild->GetChildCount())
|
|
|
|
childOffset = aChild->GetChildCount();
|
|
|
|
|
|
|
|
if (NS_FAILED(nsContentEventHandler::GetFlatTextOffsetOfRange(
|
|
|
|
aChild, aChild, childOffset, &childOffset)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// fire notification
|
|
|
|
if (childOffset)
|
2010-08-04 19:22:50 +00:00
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new TextChangeEvent(mWidget, offset, offset + childOffset, offset));
|
2009-02-10 20:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsINode* GetRootEditableNode(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
if (aContent) {
|
2011-03-23 14:45:21 +00:00
|
|
|
nsINode* root = nsnull;
|
|
|
|
nsINode* node = aContent;
|
|
|
|
while (node && node->IsEditable()) {
|
|
|
|
root = node;
|
|
|
|
node = node->GetNodeParent();
|
2009-02-10 20:56:51 +00:00
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
if (aPresContext) {
|
|
|
|
nsIDocument* document = aPresContext->Document();
|
|
|
|
if (document && document->IsEditable())
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::OnTextStateBlur(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
if (!sTextStateObserver || sTextStateObserver->mDestroying ||
|
|
|
|
sTextStateObserver->mEditableNode ==
|
|
|
|
GetRootEditableNode(aPresContext, aContent))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
sTextStateObserver->mDestroying = PR_TRUE;
|
|
|
|
sTextStateObserver->mWidget->OnIMEFocusChange(PR_FALSE);
|
|
|
|
sTextStateObserver->Destroy();
|
|
|
|
NS_RELEASE(sTextStateObserver);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::OnTextStateFocus(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
if (sTextStateObserver) return NS_OK;
|
|
|
|
|
|
|
|
nsINode *editableNode = GetRootEditableNode(aPresContext, aContent);
|
|
|
|
if (!editableNode) return NS_OK;
|
|
|
|
|
2009-03-11 15:43:08 +00:00
|
|
|
nsIPresShell* shell = aPresContext->GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsIViewManager* vm = shell->GetViewManager();
|
2009-02-10 20:56:51 +00:00
|
|
|
NS_ENSURE_TRUE(vm, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWidget> widget;
|
2009-07-22 00:45:05 +00:00
|
|
|
nsresult rv = vm->GetRootWidget(getter_AddRefs(widget));
|
2009-02-10 20:56:51 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
|
2009-11-17 12:40:59 +00:00
|
|
|
if (!widget) {
|
|
|
|
return NS_OK; // Sometimes, there are no widgets.
|
|
|
|
}
|
2009-02-10 20:56:51 +00:00
|
|
|
|
|
|
|
rv = widget->OnIMEFocusChange(PR_TRUE);
|
2009-04-17 05:01:23 +00:00
|
|
|
if (rv == NS_ERROR_NOT_IMPLEMENTED)
|
|
|
|
return NS_OK;
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-02-10 20:56:51 +00:00
|
|
|
|
2010-09-24 03:28:15 +00:00
|
|
|
PRBool wantUpdates = rv != NS_SUCCESS_IME_NO_UPDATES;
|
|
|
|
|
2009-02-10 20:56:51 +00:00
|
|
|
// OnIMEFocusChange may cause focus and sTextStateObserver to change
|
|
|
|
// In that case return and keep the current sTextStateObserver
|
|
|
|
NS_ENSURE_TRUE(!sTextStateObserver, NS_OK);
|
|
|
|
|
|
|
|
sTextStateObserver = new nsTextStateManager();
|
|
|
|
NS_ENSURE_TRUE(sTextStateObserver, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ADDREF(sTextStateObserver);
|
2010-09-24 03:28:15 +00:00
|
|
|
rv = sTextStateObserver->Init(widget, aPresContext,
|
|
|
|
editableNode, wantUpdates);
|
2009-02-10 20:56:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
sTextStateObserver->mDestroying = PR_TRUE;
|
|
|
|
sTextStateObserver->Destroy();
|
|
|
|
NS_RELEASE(sTextStateObserver);
|
|
|
|
widget->OnIMEFocusChange(PR_FALSE);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIMEStateManager::GetFocusSelectionAndRoot(nsISelection** aSel,
|
|
|
|
nsIContent** aRoot)
|
|
|
|
{
|
2010-12-10 17:31:23 +00:00
|
|
|
if (!sTextStateObserver || !sTextStateObserver->mEditableNode ||
|
|
|
|
!sTextStateObserver->mSel)
|
2009-02-10 20:56:51 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
NS_ASSERTION(sTextStateObserver->mSel && sTextStateObserver->mRootContent,
|
|
|
|
"uninitialized text state observer");
|
|
|
|
NS_ADDREF(*aSel = sTextStateObserver->mSel);
|
|
|
|
NS_ADDREF(*aRoot = sTextStateObserver->mRootContent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|