2010-03-18 21:21:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=4:tabstop=4:
|
|
|
|
*/
|
2012-05-21 04:12:37 -07: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/. */
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
#define MAEMO_CHANGES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif // MOZ_LOGGING
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#include "nsGtkIMModule.h"
|
|
|
|
#include "nsWindow.h"
|
2011-05-27 01:15:20 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIObserverService.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2010-03-18 21:21:16 -07:00
|
|
|
#endif
|
|
|
|
|
2011-05-27 01:15:20 -07:00
|
|
|
using namespace mozilla;
|
2011-11-27 03:51:52 -08:00
|
|
|
using namespace mozilla::widget;
|
2011-05-27 01:15:20 -07:00
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-07-30 07:20:58 -07:00
|
|
|
PRLogModuleInfo* gGtkIMLog = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
static const char*
|
2012-08-22 08:56:38 -07:00
|
|
|
GetRangeTypeName(uint32_t aRangeType)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
switch (aRangeType) {
|
|
|
|
case NS_TEXTRANGE_RAWINPUT:
|
|
|
|
return "NS_TEXTRANGE_RAWINPUT";
|
|
|
|
case NS_TEXTRANGE_CONVERTEDTEXT:
|
|
|
|
return "NS_TEXTRANGE_CONVERTEDTEXT";
|
|
|
|
case NS_TEXTRANGE_SELECTEDRAWTEXT:
|
|
|
|
return "NS_TEXTRANGE_SELECTEDRAWTEXT";
|
|
|
|
case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT:
|
|
|
|
return "NS_TEXTRANGE_SELECTEDCONVERTEDTEXT";
|
|
|
|
case NS_TEXTRANGE_CARETPOSITION:
|
|
|
|
return "NS_TEXTRANGE_CARETPOSITION";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN SELECTION TYPE!!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2012-08-22 08:56:38 -07:00
|
|
|
GetEnabledStateName(uint32_t aState)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
switch (aState) {
|
2011-11-27 03:51:53 -08:00
|
|
|
case IMEState::DISABLED:
|
2010-03-18 21:21:16 -07:00
|
|
|
return "DISABLED";
|
2011-11-27 03:51:53 -08:00
|
|
|
case IMEState::ENABLED:
|
2010-03-18 21:21:16 -07:00
|
|
|
return "ENABLED";
|
2011-11-27 03:51:53 -08:00
|
|
|
case IMEState::PASSWORD:
|
2010-03-18 21:21:16 -07:00
|
|
|
return "PASSWORD";
|
2011-11-27 03:51:53 -08:00
|
|
|
case IMEState::PLUGIN:
|
2010-03-18 21:21:16 -07:00
|
|
|
return "PLUG_IN";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN ENABLED STATUS!!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsGtkIMModule* nsGtkIMModule::sLastFocusedModule = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gIsVirtualKeyboardOpened = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nsGtkIMModule::nsGtkIMModule(nsWindow* aOwnerWindow) :
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnerWindow(aOwnerWindow), mLastFocusedWindow(nullptr),
|
|
|
|
mContext(nullptr),
|
2010-03-18 21:21:16 -07:00
|
|
|
#ifndef NS_IME_ENABLED_ON_PASSWORD_FIELD
|
2012-07-30 07:20:58 -07:00
|
|
|
mSimpleContext(nullptr),
|
2010-03-18 21:21:16 -07:00
|
|
|
#endif
|
2012-07-30 07:20:58 -07:00
|
|
|
mDummyContext(nullptr),
|
|
|
|
mCompositionStart(PR_UINT32_MAX), mProcessingKeyEvent(nullptr),
|
2012-03-08 20:27:51 -08:00
|
|
|
mCompositionState(eCompositionState_NotComposing),
|
|
|
|
mIsIMFocused(false), mIgnoreNativeCompositionEvent(false)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gGtkIMLog) {
|
|
|
|
gGtkIMLog = PR_NewLogModule("nsGtkIMModuleWidgets");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::Init()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): Init, mOwnerWindow=%p",
|
|
|
|
this, mOwnerWindow));
|
|
|
|
|
|
|
|
MozContainer* container = mOwnerWindow->GetMozContainer();
|
|
|
|
NS_PRECONDITION(container, "container is null");
|
2012-09-13 18:56:59 -07:00
|
|
|
GdkWindow* gdkWindow = gtk_widget_get_window(GTK_WIDGET(container));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
// NOTE: gtk_im_*_new() abort (kill) the whole process when it fails.
|
|
|
|
// So, we don't need to check the result.
|
|
|
|
|
|
|
|
// Normal context.
|
|
|
|
mContext = gtk_im_multicontext_new();
|
|
|
|
gtk_im_context_set_client_window(mContext, gdkWindow);
|
|
|
|
g_signal_connect(mContext, "preedit_changed",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnChangeCompositionCallback),
|
|
|
|
this);
|
2010-03-24 08:04:39 -07:00
|
|
|
g_signal_connect(mContext, "retrieve_surrounding",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnRetrieveSurroundingCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mContext, "delete_surrounding",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnDeleteSurroundingCallback),
|
|
|
|
this);
|
2010-03-18 21:21:16 -07:00
|
|
|
g_signal_connect(mContext, "commit",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnCommitCompositionCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mContext, "preedit_start",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnStartCompositionCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mContext, "preedit_end",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnEndCompositionCallback),
|
|
|
|
this);
|
|
|
|
|
|
|
|
#ifndef NS_IME_ENABLED_ON_PASSWORD_FIELD
|
|
|
|
// Simple context
|
|
|
|
mSimpleContext = gtk_im_context_simple_new();
|
|
|
|
gtk_im_context_set_client_window(mSimpleContext, gdkWindow);
|
|
|
|
g_signal_connect(mSimpleContext, "preedit_changed",
|
|
|
|
G_CALLBACK(&nsGtkIMModule::OnChangeCompositionCallback),
|
|
|
|
this);
|
2010-03-24 08:04:39 -07:00
|
|
|
g_signal_connect(mSimpleContext, "retrieve_surrounding",
|
|
|
|
G_CALLBACK(&nsGtkIMModule::OnRetrieveSurroundingCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mSimpleContext, "delete_surrounding",
|
|
|
|
G_CALLBACK(&nsGtkIMModule::OnDeleteSurroundingCallback),
|
|
|
|
this);
|
2010-03-18 21:21:16 -07:00
|
|
|
g_signal_connect(mSimpleContext, "commit",
|
|
|
|
G_CALLBACK(&nsGtkIMModule::OnCommitCompositionCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mSimpleContext, "preedit_start",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnStartCompositionCallback),
|
|
|
|
this);
|
|
|
|
g_signal_connect(mSimpleContext, "preedit_end",
|
|
|
|
G_CALLBACK(nsGtkIMModule::OnEndCompositionCallback),
|
|
|
|
this);
|
|
|
|
#endif // NS_IME_ENABLED_ON_PASSWORD_FIELD
|
|
|
|
|
|
|
|
// Dummy context
|
|
|
|
mDummyContext = gtk_im_multicontext_new();
|
|
|
|
gtk_im_context_set_client_window(mDummyContext, gdkWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGtkIMModule::~nsGtkIMModule()
|
|
|
|
{
|
|
|
|
if (this == sLastFocusedModule) {
|
2012-07-30 07:20:58 -07:00
|
|
|
sLastFocusedModule = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p) was gone", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnDestroyWindow(nsWindow* aWindow)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnDestroyWindow, aWindow=%p, mLastFocusedWindow=%p, mOwnerWindow=%p, mLastFocusedModule=%p",
|
|
|
|
this, aWindow, mLastFocusedWindow, mOwnerWindow, sLastFocusedModule));
|
|
|
|
|
|
|
|
NS_PRECONDITION(aWindow, "aWindow must not be null");
|
|
|
|
|
|
|
|
if (mLastFocusedWindow == aWindow) {
|
|
|
|
CancelIMEComposition(aWindow);
|
|
|
|
if (mIsIMFocused) {
|
|
|
|
Blur();
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mLastFocusedWindow = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwnerWindow != aWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sLastFocusedModule == this) {
|
2012-07-30 07:20:58 -07:00
|
|
|
sLastFocusedModule = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE:
|
|
|
|
* The given window is the owner of this, so, we must release the
|
|
|
|
* contexts now. But that might be referred from other nsWindows
|
|
|
|
* (they are children of this. But we don't know why there are the
|
|
|
|
* cases). So, we need to clear the pointers that refers to contexts
|
|
|
|
* and this if the other referrers are still alive. See bug 349727.
|
|
|
|
*/
|
|
|
|
if (mContext) {
|
|
|
|
PrepareToDestroyContext(mContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
gtk_im_context_set_client_window(mContext, nullptr);
|
2011-05-11 06:10:36 -07:00
|
|
|
g_object_unref(mContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NS_IME_ENABLED_ON_PASSWORD_FIELD
|
|
|
|
if (mSimpleContext) {
|
2012-07-30 07:20:58 -07:00
|
|
|
gtk_im_context_set_client_window(mSimpleContext, nullptr);
|
2011-05-11 06:10:36 -07:00
|
|
|
g_object_unref(mSimpleContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
mSimpleContext = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
#endif // NS_IME_ENABLED_ON_PASSWORD_FIELD
|
|
|
|
|
|
|
|
if (mDummyContext) {
|
|
|
|
// mContext and mDummyContext have the same slaveType and signal_data
|
|
|
|
// so no need for another workaround_gtk_im_display_closed.
|
2012-07-30 07:20:58 -07:00
|
|
|
gtk_im_context_set_client_window(mDummyContext, nullptr);
|
2011-05-11 06:10:36 -07:00
|
|
|
g_object_unref(mDummyContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDummyContext = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnerWindow = nullptr;
|
|
|
|
mLastFocusedWindow = nullptr;
|
2011-11-27 03:51:53 -08:00
|
|
|
mInputContext.mIMEState.mEnabled = IMEState::DISABLED;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" SUCCEEDED, Completely destroyed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work around gtk bug http://bugzilla.gnome.org/show_bug.cgi?id=483223:
|
|
|
|
// (and the similar issue of GTK+ IIIM)
|
|
|
|
// The GTK+ XIM and IIIM modules register handlers for the "closed" signal
|
|
|
|
// on the display, but:
|
|
|
|
// * The signal handlers are not disconnected when the module is unloaded.
|
|
|
|
//
|
|
|
|
// The GTK+ XIM module has another problem:
|
|
|
|
// * When the signal handler is run (with the module loaded) it tries
|
|
|
|
// XFree (and fails) on a pointer that did not come from Xmalloc.
|
|
|
|
//
|
|
|
|
// To prevent these modules from being unloaded, use static variables to
|
|
|
|
// hold ref of GtkIMContext class.
|
|
|
|
// For GTK+ XIM module, to prevent the signal handler from being run,
|
|
|
|
// find the signal handlers and remove them.
|
|
|
|
//
|
|
|
|
// GtkIMContextXIMs share XOpenIM connections and display closed signal
|
|
|
|
// handlers (where possible).
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::PrepareToDestroyContext(GtkIMContext *aContext)
|
|
|
|
{
|
|
|
|
MozContainer* container = mOwnerWindow->GetMozContainer();
|
|
|
|
NS_PRECONDITION(container, "The container of the window is null");
|
|
|
|
|
|
|
|
GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT(aContext);
|
2012-09-13 18:56:59 -07:00
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
2010-03-18 21:21:16 -07:00
|
|
|
GtkIMContext *slave = multicontext->slave;
|
2012-09-13 18:56:59 -07:00
|
|
|
#else
|
|
|
|
GtkIMContext *slave = NULL; //TODO GTK3
|
|
|
|
#endif
|
2010-03-18 21:21:16 -07:00
|
|
|
if (!slave) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GType slaveType = G_TYPE_FROM_INSTANCE(slave);
|
|
|
|
const gchar *im_type_name = g_type_name(slaveType);
|
|
|
|
if (strcmp(im_type_name, "GtkIMContextXIM") == 0) {
|
|
|
|
if (gtk_check_version(2, 12, 1) == NULL) {
|
|
|
|
return; // gtk bug has been fixed
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GtkIMContextXIM
|
|
|
|
{
|
|
|
|
GtkIMContext parent;
|
|
|
|
gpointer private_data;
|
|
|
|
// ... other fields
|
|
|
|
};
|
|
|
|
|
|
|
|
gpointer signal_data =
|
|
|
|
reinterpret_cast<GtkIMContextXIM*>(slave)->private_data;
|
|
|
|
if (!signal_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_matched(
|
|
|
|
gtk_widget_get_display(GTK_WIDGET(container)),
|
|
|
|
G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, signal_data);
|
|
|
|
|
|
|
|
// Add a reference to prevent the XIM module from being unloaded
|
|
|
|
// and reloaded: each time the module is loaded and used, it
|
|
|
|
// opens (and doesn't close) new XOpenIM connections.
|
|
|
|
static gpointer gtk_xim_context_class =
|
|
|
|
g_type_class_ref(slaveType);
|
|
|
|
// Mute unused variable warning:
|
|
|
|
gtk_xim_context_class = gtk_xim_context_class;
|
|
|
|
} else if (strcmp(im_type_name, "GtkIMContextIIIM") == 0) {
|
|
|
|
// Add a reference to prevent the IIIM module from being unloaded
|
|
|
|
static gpointer gtk_iiim_context_class =
|
|
|
|
g_type_class_ref(slaveType);
|
|
|
|
// Mute unused variable warning:
|
|
|
|
gtk_iiim_context_class = gtk_iiim_context_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnFocusWindow(nsWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (NS_UNLIKELY(IsDestroyed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnFocusWindow, aWindow=%p, mLastFocusedWindow=%p",
|
2011-08-03 15:36:00 -07:00
|
|
|
this, aWindow, mLastFocusedWindow));
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow = aWindow;
|
|
|
|
Focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnBlurWindow(nsWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (NS_UNLIKELY(IsDestroyed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnBlurWindow, aWindow=%p, mLastFocusedWindow=%p, mIsIMFocused=%s",
|
|
|
|
this, aWindow, mLastFocusedWindow, mIsIMFocused ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (!mIsIMFocused || mLastFocusedWindow != aWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Blur();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-01-14 05:40:08 -08:00
|
|
|
nsGtkIMModule::OnKeyEvent(nsWindow* aCaller, GdkEventKey* aEvent,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aKeyDownEventWasSent /* = false */)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aEvent, "aEvent must be non-null");
|
|
|
|
|
|
|
|
if (!IsEditable() || NS_UNLIKELY(IsDestroyed())) {
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-01-14 05:40:08 -08:00
|
|
|
("GtkIMModule(%p): OnKeyEvent, aCaller=%p, aKeyDownEventWasSent=%s",
|
|
|
|
this, aCaller, aKeyDownEventWasSent ? "TRUE" : "FALSE"));
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-03-24 18:08:00 -07:00
|
|
|
(" aEvent: type=%s, keyval=%s, unicode=0x%X",
|
2010-03-18 21:21:16 -07:00
|
|
|
aEvent->type == GDK_KEY_PRESS ? "GDK_KEY_PRESS" :
|
|
|
|
aEvent->type == GDK_KEY_RELEASE ? "GDK_KEY_RELEASE" : "Unknown",
|
2011-03-24 18:08:00 -07:00
|
|
|
gdk_keyval_name(aEvent->keyval),
|
|
|
|
gdk_keyval_to_unicode(aEvent->keyval)));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
if (aCaller != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, the caller isn't focused window, mLastFocusedWindow=%p",
|
|
|
|
mLastFocusedWindow));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkIMContext* im = GetContext();
|
|
|
|
if (NS_UNLIKELY(!im)) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-01-14 05:40:08 -08:00
|
|
|
mKeyDownEventWasSent = aKeyDownEventWasSent;
|
2011-10-03 00:56:21 -07:00
|
|
|
mFilterKeyEvent = true;
|
2010-03-18 21:21:16 -07:00
|
|
|
mProcessingKeyEvent = aEvent;
|
|
|
|
gboolean isFiltered = gtk_im_context_filter_keypress(im, aEvent);
|
2012-07-30 07:20:58 -07:00
|
|
|
mProcessingKeyEvent = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
// We filter the key event if the event was not committed (because
|
|
|
|
// it's probably part of a composition) or if the key event was
|
|
|
|
// committed _and_ changed. This way we still let key press
|
|
|
|
// events go through as simple key press events instead of
|
|
|
|
// composed characters.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool filterThisEvent = isFiltered && mFilterKeyEvent;
|
2011-03-24 18:08:00 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (IsComposing() && !isFiltered) {
|
2011-03-24 18:08:00 -07:00
|
|
|
if (aEvent->type == GDK_KEY_PRESS) {
|
2011-09-22 02:17:40 -07:00
|
|
|
if (!mDispatchedCompositionString.IsEmpty()) {
|
2011-03-24 18:08:00 -07:00
|
|
|
// If there is composition string, we shouldn't dispatch
|
|
|
|
// any keydown events during composition.
|
2011-10-03 00:56:21 -07:00
|
|
|
filterThisEvent = true;
|
2011-03-24 18:08:00 -07:00
|
|
|
} else {
|
|
|
|
// A Hangul input engine for SCIM doesn't emit preedit_end
|
|
|
|
// signal even when composition string becomes empty. On the
|
|
|
|
// other hand, we should allow to make composition with empty
|
|
|
|
// string for other languages because there *might* be such
|
|
|
|
// IM. For compromising this issue, we should dispatch
|
|
|
|
// compositionend event, however, we don't need to reset IM
|
|
|
|
// actually.
|
|
|
|
CommitCompositionBy(EmptyString());
|
2011-10-03 00:56:21 -07:00
|
|
|
filterThisEvent = false;
|
2011-03-24 18:08:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Key release event may not be consumed by IM, however, we
|
|
|
|
// shouldn't dispatch any keyup event during composition.
|
2011-10-03 00:56:21 -07:00
|
|
|
filterThisEvent = true;
|
2011-03-24 18:08:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" filterThisEvent=%s (isFiltered=%s, mFilterKeyEvent=%s)",
|
|
|
|
filterThisEvent ? "TRUE" : "FALSE", isFiltered ? "YES" : "NO",
|
|
|
|
mFilterKeyEvent ? "YES" : "NO"));
|
|
|
|
|
|
|
|
return filterThisEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsGtkIMModule::OnFocusChangeInGecko(bool aFocus)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): OnFocusChangeInGecko, aFocus=%s, "
|
|
|
|
"mCompositionState=%s, mIsIMFocused=%s, "
|
|
|
|
"mIgnoreNativeCompositionEvent=%s",
|
|
|
|
this, aFocus ? "YES" : "NO", GetCompositionStateName(),
|
2010-03-18 21:21:16 -07:00
|
|
|
mIsIMFocused ? "YES" : "NO",
|
|
|
|
mIgnoreNativeCompositionEvent ? "YES" : "NO"));
|
2012-03-08 20:27:51 -08:00
|
|
|
|
|
|
|
// We shouldn't carry over the removed string to another editor.
|
|
|
|
mSelectedString.Truncate();
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
if (aFocus) {
|
|
|
|
// If we failed to commit forcedely in previous focused editor,
|
|
|
|
// we should reopen the gate for native signals in new focused editor.
|
2011-10-03 00:56:21 -07:00
|
|
|
mIgnoreNativeCompositionEvent = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::ResetIME()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): ResetIME, mCompositionState=%s, mIsIMFocused=%s",
|
|
|
|
this, GetCompositionStateName(), mIsIMFocused ? "YES" : "NO"));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (NS_UNLIKELY(!im)) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
mIgnoreNativeCompositionEvent = true;
|
2010-03-18 21:21:16 -07:00
|
|
|
gtk_im_context_reset(im);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGtkIMModule::ResetInputState(nsWindow* aCaller)
|
|
|
|
{
|
|
|
|
if (NS_UNLIKELY(IsDestroyed())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): ResetInputState, aCaller=%p, mCompositionState=%s",
|
|
|
|
this, aCaller, GetCompositionStateName()));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
if (aCaller != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" WARNING: the caller isn't focused window, mLastFocusedWindow=%p",
|
|
|
|
mLastFocusedWindow));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing()) {
|
2010-03-18 21:21:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX We should commit composition ourselves temporary...
|
|
|
|
ResetIME();
|
2011-09-22 02:17:40 -07:00
|
|
|
CommitCompositionBy(mDispatchedCompositionString);
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGtkIMModule::CancelIMEComposition(nsWindow* aCaller)
|
|
|
|
{
|
|
|
|
if (NS_UNLIKELY(IsDestroyed())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): CancelIMEComposition, aCaller=%p",
|
|
|
|
this, aCaller));
|
|
|
|
|
|
|
|
if (aCaller != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, the caller isn't focused window, mLastFocusedWindow=%p",
|
|
|
|
mLastFocusedWindow));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing()) {
|
2010-04-14 04:28:38 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (NS_UNLIKELY(!im)) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetIME();
|
|
|
|
CommitCompositionBy(EmptyString());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-27 03:51:52 -08:00
|
|
|
void
|
|
|
|
nsGtkIMModule::SetInputContext(nsWindow* aCaller,
|
|
|
|
const InputContext* aContext,
|
|
|
|
const InputContextAction* aAction)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
2011-11-27 03:51:53 -08:00
|
|
|
if (NS_UNLIKELY(IsDestroyed())) {
|
2011-11-27 03:51:52 -08:00
|
|
|
return;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-11-27 03:51:52 -08:00
|
|
|
("GtkIMModule(%p): SetInputContext, aCaller=%p, aState=%s mHTMLInputType=%s",
|
2011-11-27 03:51:53 -08:00
|
|
|
this, aCaller, GetEnabledStateName(aContext->mIMEState.mEnabled),
|
2011-08-03 15:36:00 -07:00
|
|
|
NS_ConvertUTF16toUTF8(aContext->mHTMLInputType).get()));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
if (aCaller != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, the caller isn't focused window, mLastFocusedWindow=%p",
|
|
|
|
mLastFocusedWindow));
|
2011-11-27 03:51:52 -08:00
|
|
|
return;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
2011-11-27 03:51:52 -08:00
|
|
|
return;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (sLastFocusedModule != this) {
|
2011-11-27 03:51:52 -08:00
|
|
|
mInputContext = *aContext;
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" SUCCEEDED, but we're not active"));
|
2011-11-27 03:51:52 -08:00
|
|
|
return;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-11-27 03:51:53 -08:00
|
|
|
bool changingEnabledState =
|
|
|
|
aContext->mIMEState.mEnabled != mInputContext.mIMEState.mEnabled;
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
// Release current IME focus if IME is enabled.
|
2011-11-27 03:51:53 -08:00
|
|
|
if (changingEnabledState && IsEditable()) {
|
2010-03-18 21:21:16 -07:00
|
|
|
ResetInputState(mLastFocusedWindow);
|
|
|
|
Blur();
|
|
|
|
}
|
|
|
|
|
2011-11-27 03:51:52 -08:00
|
|
|
mInputContext = *aContext;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
// Even when aState is not enabled state, we need to set IME focus.
|
|
|
|
// Because some IMs are updating the status bar of them at this time.
|
|
|
|
// Be aware, don't use aWindow here because this method shouldn't move
|
|
|
|
// focus actually.
|
2011-11-27 03:51:53 -08:00
|
|
|
if (changingEnabledState) {
|
|
|
|
Focus();
|
|
|
|
}
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2011-01-05 05:48:16 -08:00
|
|
|
#if (MOZ_PLATFORM_MAEMO == 5)
|
2010-03-18 21:21:16 -07:00
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (im) {
|
|
|
|
if (IsEnabled()) {
|
2011-04-20 05:47:40 -07:00
|
|
|
// Ensure that opening the virtual keyboard is allowed for this specific
|
2011-11-27 03:51:52 -08:00
|
|
|
// InputContext depending on the content.ime.strict.policy pref
|
2011-11-27 03:51:53 -08:00
|
|
|
if (mInputContext.mIMEState.mEnabled != IMEState::DISABLED &&
|
|
|
|
mInputContext.mIMEState.mEnabled != IMEState::PLUGIN &&
|
2011-11-27 03:51:52 -08:00
|
|
|
Preferences::GetBool("content.ime.strict_policy", false) &&
|
2011-11-27 03:51:53 -08:00
|
|
|
!aAction->ContentGotFocusByTrustedCause() &&
|
|
|
|
!aAction->UserMightRequestOpenVKB()) {
|
2011-11-27 03:51:52 -08:00
|
|
|
return;
|
2011-04-20 05:47:40 -07:00
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
// It is not desired that the hildon's autocomplete mechanism displays
|
|
|
|
// user previous entered passwds, so lets make completions invisible
|
|
|
|
// in these cases.
|
|
|
|
int mode;
|
2011-05-11 06:10:36 -07:00
|
|
|
g_object_get(im, "hildon-input-mode", &mode, NULL);
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2011-11-27 03:51:53 -08:00
|
|
|
if (mInputContext.mIMEState.mEnabled == IMEState::ENABLED ||
|
|
|
|
mInputContext.mIMEState.mEnabled == IMEState::PLUGIN) {
|
2010-03-18 21:21:16 -07:00
|
|
|
mode &= ~HILDON_GTK_INPUT_MODE_INVISIBLE;
|
2011-11-27 03:51:53 -08:00
|
|
|
} else if (mInputContext.mIMEState.mEnabled == IMEState::PASSWORD) {
|
2010-03-18 21:21:16 -07:00
|
|
|
mode |= HILDON_GTK_INPUT_MODE_INVISIBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn off auto-capitalization for editboxes
|
|
|
|
mode &= ~HILDON_GTK_INPUT_MODE_AUTOCAP;
|
|
|
|
|
2010-07-28 18:09:36 -07:00
|
|
|
// Turn off predictive dictionaries for editboxes
|
|
|
|
mode &= ~HILDON_GTK_INPUT_MODE_DICTIONARY;
|
|
|
|
|
2011-05-11 06:10:36 -07:00
|
|
|
g_object_set(im, "hildon-input-mode",
|
2010-03-18 21:21:16 -07:00
|
|
|
(HildonGtkInputMode)mode, NULL);
|
2011-10-03 00:56:21 -07:00
|
|
|
gIsVirtualKeyboardOpened = true;
|
2010-03-18 21:21:16 -07:00
|
|
|
hildon_gtk_im_context_show(im);
|
|
|
|
} else {
|
2011-10-03 00:56:21 -07:00
|
|
|
gIsVirtualKeyboardOpened = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
hildon_gtk_im_context_hide(im);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
2010-03-18 21:21:16 -07:00
|
|
|
if (observerService) {
|
|
|
|
nsAutoString rectBuf;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t x, y, w, h;
|
2010-03-18 21:21:16 -07:00
|
|
|
gdk_window_get_position(aCaller->GetGdkWindow(), &x, &y);
|
|
|
|
gdk_window_get_size(aCaller->GetGdkWindow(), &w, &h);
|
|
|
|
rectBuf.Assign(NS_LITERAL_STRING("{\"left\": "));
|
|
|
|
rectBuf.AppendInt(x);
|
2010-11-18 14:56:11 -08:00
|
|
|
rectBuf.Append(NS_LITERAL_STRING(", \"top\": "));
|
2010-03-18 21:21:16 -07:00
|
|
|
rectBuf.AppendInt(y);
|
|
|
|
rectBuf.Append(NS_LITERAL_STRING(", \"right\": "));
|
|
|
|
rectBuf.AppendInt(w);
|
|
|
|
rectBuf.Append(NS_LITERAL_STRING(", \"bottom\": "));
|
|
|
|
rectBuf.AppendInt(h);
|
|
|
|
rectBuf.Append(NS_LITERAL_STRING("}"));
|
2012-07-30 07:20:58 -07:00
|
|
|
observerService->NotifyObservers(nullptr, "softkb-change",
|
2010-03-18 21:21:16 -07:00
|
|
|
rectBuf.get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-27 03:51:52 -08:00
|
|
|
InputContext
|
|
|
|
nsGtkIMModule::GetInputContext()
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
2011-11-27 03:51:53 -08:00
|
|
|
mInputContext.mIMEState.mOpen = IMEState::OPEN_STATE_NOT_SUPPORTED;
|
2011-11-27 03:51:52 -08:00
|
|
|
return mInputContext;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::IsVirtualKeyboardOpened()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
return gIsVirtualKeyboardOpened;
|
|
|
|
#else
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIMContext*
|
|
|
|
nsGtkIMModule::GetContext()
|
|
|
|
{
|
|
|
|
if (IsEnabled()) {
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NS_IME_ENABLED_ON_PASSWORD_FIELD
|
2011-11-27 03:51:53 -08:00
|
|
|
if (mInputContext.mIMEState.mEnabled == IMEState::PASSWORD) {
|
2010-03-18 21:21:16 -07:00
|
|
|
return mSimpleContext;
|
|
|
|
}
|
|
|
|
#endif // NS_IME_ENABLED_ON_PASSWORD_FIELD
|
|
|
|
|
|
|
|
return mDummyContext;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::IsEnabled()
|
|
|
|
{
|
2011-11-27 03:51:53 -08:00
|
|
|
return mInputContext.mIMEState.mEnabled == IMEState::ENABLED ||
|
2010-03-18 21:21:16 -07:00
|
|
|
#ifdef NS_IME_ENABLED_ON_PASSWORD_FIELD
|
2011-11-27 03:51:53 -08:00
|
|
|
mInputContext.mIMEState.mEnabled == IMEState::PASSWORD ||
|
2010-03-18 21:21:16 -07:00
|
|
|
#endif // NS_IME_ENABLED_ON_PASSWORD_FIELD
|
2011-11-27 03:51:53 -08:00
|
|
|
mInputContext.mIMEState.mEnabled == IMEState::PLUGIN;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::IsEditable()
|
|
|
|
{
|
2011-11-27 03:51:53 -08:00
|
|
|
return mInputContext.mIMEState.mEnabled == IMEState::ENABLED ||
|
|
|
|
mInputContext.mIMEState.mEnabled == IMEState::PLUGIN ||
|
|
|
|
mInputContext.mIMEState.mEnabled == IMEState::PASSWORD;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::Focus()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): Focus, sLastFocusedModule=%p",
|
|
|
|
this, sLastFocusedModule));
|
|
|
|
|
|
|
|
if (mIsIMFocused) {
|
|
|
|
NS_ASSERTION(sLastFocusedModule == this,
|
|
|
|
"We're not active, but the IM was focused?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (!im) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-08-03 15:36:00 -07:00
|
|
|
(" FAILED, there are no context"));
|
2010-03-18 21:21:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sLastFocusedModule && sLastFocusedModule != this) {
|
|
|
|
sLastFocusedModule->Blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
sLastFocusedModule = this;
|
|
|
|
|
|
|
|
gtk_im_context_focus_in(im);
|
2011-10-03 00:56:21 -07:00
|
|
|
mIsIMFocused = true;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
// We should release IME focus for uim and scim.
|
|
|
|
// These IMs are using snooper that is released at losing focus.
|
|
|
|
Blur();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::Blur()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): Blur, mIsIMFocused=%s",
|
|
|
|
this, mIsIMFocused ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (!mIsIMFocused) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (!im) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_im_context_focus_out(im);
|
2011-10-03 00:56:21 -07:00
|
|
|
mIsIMFocused = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnStartCompositionCallback(GtkIMContext *aContext,
|
|
|
|
nsGtkIMModule* aModule)
|
|
|
|
{
|
|
|
|
aModule->OnStartCompositionNative(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnStartCompositionNative(GtkIMContext *aContext)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnStartCompositionNative, aContext=%p",
|
|
|
|
this, aContext));
|
|
|
|
|
|
|
|
// See bug 472635, we should do nothing if IM context doesn't match.
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DispatchCompositionStart()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetCursorPosition(mCompositionStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnEndCompositionCallback(GtkIMContext *aContext,
|
|
|
|
nsGtkIMModule* aModule)
|
|
|
|
{
|
|
|
|
aModule->OnEndCompositionNative(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnEndCompositionNative(GtkIMContext *aContext)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnEndCompositionNative, aContext=%p",
|
|
|
|
this, aContext));
|
|
|
|
|
|
|
|
// See bug 472635, we should do nothing if IM context doesn't match.
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool shouldIgnoreThisEvent = ShouldIgnoreNativeCompositionEvent();
|
2010-04-14 04:27:20 -07:00
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
// Finish the cancelling mode here rather than DispatchCompositionEnd()
|
|
|
|
// because DispatchCompositionEnd() is called ourselves when we need to
|
|
|
|
// commit the composition string *before* the focus moves completely.
|
|
|
|
// Note that the native commit can be fired *after* ResetIME().
|
2011-10-03 00:56:21 -07:00
|
|
|
mIgnoreNativeCompositionEvent = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing() || shouldIgnoreThisEvent) {
|
2010-03-18 21:21:16 -07:00
|
|
|
// If we already handled the commit event, we should do nothing here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
// Be aware, widget can be gone
|
|
|
|
DispatchCompositionEnd();
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnChangeCompositionCallback(GtkIMContext *aContext,
|
|
|
|
nsGtkIMModule* aModule)
|
|
|
|
{
|
|
|
|
aModule->OnChangeCompositionNative(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnChangeCompositionNative(GtkIMContext *aContext)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnChangeCompositionNative, aContext=%p",
|
|
|
|
this, aContext));
|
|
|
|
|
|
|
|
// See bug 472635, we should do nothing if IM context doesn't match.
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldIgnoreNativeCompositionEvent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
nsAutoString compositionString;
|
|
|
|
GetCompositionString(compositionString);
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing() && compositionString.IsEmpty()) {
|
2011-09-22 02:17:40 -07:00
|
|
|
mDispatchedCompositionString.Truncate();
|
2010-03-18 21:21:16 -07:00
|
|
|
return; // Don't start the composition with empty string.
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
// Be aware, widget can be gone
|
2012-03-08 20:27:51 -08:00
|
|
|
DispatchTextEvent(compositionString, false);
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 08:04:39 -07:00
|
|
|
/* static */
|
|
|
|
gboolean
|
|
|
|
nsGtkIMModule::OnRetrieveSurroundingCallback(GtkIMContext *aContext,
|
|
|
|
nsGtkIMModule *aModule)
|
|
|
|
{
|
|
|
|
return aModule->OnRetrieveSurroundingNative(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
nsGtkIMModule::OnRetrieveSurroundingNative(GtkIMContext *aContext)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnRetrieveSurroundingNative, aContext=%p, current context=%p",
|
|
|
|
this, aContext, GetContext()));
|
|
|
|
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString uniStr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cursorPos;
|
2010-03-24 08:04:39 -07:00
|
|
|
if (NS_FAILED(GetCurrentParagraph(uniStr, cursorPos))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-06-02 20:27:48 -07:00
|
|
|
NS_ConvertUTF16toUTF8 utf8Str(nsDependentSubstring(uniStr, 0, cursorPos));
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cursorPosInUTF8 = utf8Str.Length();
|
2012-06-02 20:27:48 -07:00
|
|
|
AppendUTF16toUTF8(nsDependentSubstring(uniStr, cursorPos), utf8Str);
|
|
|
|
gtk_im_context_set_surrounding(aContext, utf8Str.get(), utf8Str.Length(),
|
|
|
|
cursorPosInUTF8);
|
2010-03-24 08:04:39 -07:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
gboolean
|
|
|
|
nsGtkIMModule::OnDeleteSurroundingCallback(GtkIMContext *aContext,
|
|
|
|
gint aOffset,
|
|
|
|
gint aNChars,
|
|
|
|
nsGtkIMModule *aModule)
|
|
|
|
{
|
|
|
|
return aModule->OnDeleteSurroundingNative(aContext, aOffset, aNChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
nsGtkIMModule::OnDeleteSurroundingNative(GtkIMContext *aContext,
|
|
|
|
gint aOffset,
|
|
|
|
gint aNChars)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnDeleteSurroundingNative, aContext=%p, current context=%p",
|
|
|
|
this, aContext, GetContext()));
|
|
|
|
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (NS_SUCCEEDED(DeleteText(aOffset, (uint32_t)aNChars))) {
|
2010-03-24 08:04:39 -07:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, cannot delete text"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnCommitCompositionCallback(GtkIMContext *aContext,
|
|
|
|
const gchar *aString,
|
|
|
|
nsGtkIMModule* aModule)
|
|
|
|
{
|
|
|
|
aModule->OnCommitCompositionNative(aContext, aString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::OnCommitCompositionNative(GtkIMContext *aContext,
|
|
|
|
const gchar *aUTF8Char)
|
|
|
|
{
|
2011-03-24 18:08:00 -07:00
|
|
|
const gchar emptyStr = 0;
|
|
|
|
const gchar *commitString = aUTF8Char ? aUTF8Char : &emptyStr;
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-03-24 18:08:00 -07:00
|
|
|
("GtkIMModule(%p): OnCommitCompositionNative, aContext=%p, current context=%p, commitString=\"%s\"",
|
|
|
|
this, aContext, GetContext(), commitString));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
// See bug 472635, we should do nothing if IM context doesn't match.
|
|
|
|
if (GetContext() != aContext) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, given context doesn't match, GetContext()=%p",
|
|
|
|
GetContext()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-24 18:08:00 -07:00
|
|
|
// If we are not in composition and committing with empty string,
|
|
|
|
// we need to do nothing because if we continued to handle this
|
|
|
|
// signal, we would dispatch compositionstart, text, compositionend
|
|
|
|
// events with empty string. Of course, they are unnecessary events
|
|
|
|
// for Web applications and our editor.
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing() && !commitString[0]) {
|
2011-03-24 18:08:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
if (ShouldIgnoreNativeCompositionEvent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If IME doesn't change their keyevent that generated this commit,
|
|
|
|
// don't send it through XIM - just send it as a normal key press
|
|
|
|
// event.
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing() && mProcessingKeyEvent) {
|
2010-03-18 21:21:16 -07:00
|
|
|
char keyval_utf8[8]; /* should have at least 6 bytes of space */
|
|
|
|
gint keyval_utf8_len;
|
|
|
|
guint32 keyval_unicode;
|
|
|
|
|
|
|
|
keyval_unicode = gdk_keyval_to_unicode(mProcessingKeyEvent->keyval);
|
|
|
|
keyval_utf8_len = g_unichar_to_utf8(keyval_unicode, keyval_utf8);
|
|
|
|
keyval_utf8[keyval_utf8_len] = '\0';
|
|
|
|
|
2011-03-24 18:08:00 -07:00
|
|
|
if (!strcmp(commitString, keyval_utf8)) {
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): OnCommitCompositionNative, we'll send normal key event",
|
|
|
|
this));
|
2011-10-03 00:56:21 -07:00
|
|
|
mFilterKeyEvent = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-24 18:08:00 -07:00
|
|
|
NS_ConvertUTF8toUTF16 str(commitString);
|
2010-03-18 21:21:16 -07:00
|
|
|
CommitCompositionBy(str); // Be aware, widget can be gone
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::CommitCompositionBy(const nsAString& aString)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-09-22 02:17:40 -07:00
|
|
|
("GtkIMModule(%p): CommitCompositionBy, aString=\"%s\", "
|
|
|
|
"mDispatchedCompositionString=\"%s\"",
|
2010-03-18 21:21:16 -07:00
|
|
|
this, NS_ConvertUTF16toUTF8(aString).get(),
|
2011-09-22 02:17:40 -07:00
|
|
|
NS_ConvertUTF16toUTF8(mDispatchedCompositionString).get()));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!DispatchTextEvent(aString, true)) {
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
// We should dispatch the compositionend event here because some IMEs
|
|
|
|
// might not fire "preedit_end" native event.
|
|
|
|
return DispatchCompositionEnd(); // Be aware, widget can be gone
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::GetCompositionString(nsAString &aCompositionString)
|
|
|
|
{
|
|
|
|
gchar *preedit_string;
|
|
|
|
gint cursor_pos;
|
|
|
|
PangoAttrList *feedback_list;
|
|
|
|
gtk_im_context_get_preedit_string(GetContext(), &preedit_string,
|
|
|
|
&feedback_list, &cursor_pos);
|
|
|
|
if (preedit_string && *preedit_string) {
|
|
|
|
CopyUTF8toUTF16(preedit_string, aCompositionString);
|
|
|
|
} else {
|
|
|
|
aCompositionString.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): GetCompositionString, result=\"%s\"",
|
|
|
|
this, preedit_string));
|
2010-04-14 04:27:20 -07:00
|
|
|
|
|
|
|
pango_attr_list_unref(feedback_list);
|
|
|
|
g_free(preedit_string);
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::DispatchCompositionStart()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): DispatchCompositionStart", this));
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (IsComposing()) {
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" WARNING, we're already in composition"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return true;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window in this module"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status;
|
2011-10-03 00:56:21 -07:00
|
|
|
nsQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT,
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow);
|
|
|
|
InitEvent(selection);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&selection, status);
|
|
|
|
|
|
|
|
if (!selection.mSucceeded || selection.mReply.mOffset == PR_UINT32_MAX) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, cannot query the selection offset"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
// XXX The composition start point might be changed by composition events
|
|
|
|
// even though we strongly hope it doesn't happen.
|
|
|
|
// Every composition event should have the start offset for the result
|
|
|
|
// because it may high cost if we query the offset every time.
|
2010-03-18 21:21:16 -07:00
|
|
|
mCompositionStart = selection.mReply.mOffset;
|
2011-09-22 02:17:40 -07:00
|
|
|
mDispatchedCompositionString.Truncate();
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2011-01-14 05:40:08 -08:00
|
|
|
if (mProcessingKeyEvent && !mKeyDownEventWasSent &&
|
|
|
|
mProcessingKeyEvent->type == GDK_KEY_PRESS) {
|
2010-03-18 21:21:16 -07:00
|
|
|
// If this composition is started by a native keydown event, we need to
|
|
|
|
// dispatch our keydown event here (before composition start).
|
|
|
|
nsCOMPtr<nsIWidget> kungFuDeathGrip = mLastFocusedWindow;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCancelled;
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow->DispatchKeyDownEvent(mProcessingKeyEvent,
|
|
|
|
&isCancelled);
|
2011-01-14 05:40:08 -08:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" keydown event is dispatched"));
|
2010-03-18 21:21:16 -07:00
|
|
|
if (static_cast<nsWindow*>(kungFuDeathGrip.get())->IsDestroyed() ||
|
|
|
|
kungFuDeathGrip != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" NOTE, the focused widget was destroyed/changed by keydown event"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIgnoreNativeCompositionEvent) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" WARNING, mIgnoreNativeCompositionEvent is already TRUE, but we forcedly reset"));
|
2011-10-03 00:56:21 -07:00
|
|
|
mIgnoreNativeCompositionEvent = false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-08-03 15:36:00 -07:00
|
|
|
(" mCompositionStart=%u", mCompositionStart));
|
2012-03-08 20:27:51 -08:00
|
|
|
mCompositionState = eCompositionState_CompositionStartDispatched;
|
2011-10-03 00:56:21 -07:00
|
|
|
nsCompositionEvent compEvent(true, NS_COMPOSITION_START,
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow);
|
|
|
|
InitEvent(compEvent);
|
|
|
|
nsCOMPtr<nsIWidget> kungFuDeathGrip = mLastFocusedWindow;
|
|
|
|
mLastFocusedWindow->DispatchEvent(&compEvent, status);
|
|
|
|
if (static_cast<nsWindow*>(kungFuDeathGrip.get())->IsDestroyed() ||
|
|
|
|
kungFuDeathGrip != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" NOTE, the focused widget was destroyed/changed by compositionstart event"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
return true;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::DispatchCompositionEnd()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-09-22 02:17:40 -07:00
|
|
|
("GtkIMModule(%p): DispatchCompositionEnd, "
|
|
|
|
"mDispatchedCompositionString=\"%s\"",
|
|
|
|
this, NS_ConvertUTF16toUTF8(mDispatchedCompositionString).get()));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing()) {
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" WARNING, we have alrady finished the composition"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
2011-09-22 02:17:40 -07:00
|
|
|
mDispatchedCompositionString.Truncate();
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window in this module"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
nsCompositionEvent compEvent(true, NS_COMPOSITION_END,
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow);
|
|
|
|
InitEvent(compEvent);
|
2011-09-22 02:17:40 -07:00
|
|
|
compEvent.data = mDispatchedCompositionString;
|
2010-03-18 21:21:16 -07:00
|
|
|
nsEventStatus status;
|
|
|
|
nsCOMPtr<nsIWidget> kungFuDeathGrip = mLastFocusedWindow;
|
|
|
|
mLastFocusedWindow->DispatchEvent(&compEvent, status);
|
2012-03-08 20:27:51 -08:00
|
|
|
mCompositionState = eCompositionState_NotComposing;
|
2010-03-18 21:21:16 -07:00
|
|
|
mCompositionStart = PR_UINT32_MAX;
|
2011-09-22 02:17:40 -07:00
|
|
|
mDispatchedCompositionString.Truncate();
|
2010-03-18 21:21:16 -07:00
|
|
|
if (static_cast<nsWindow*>(kungFuDeathGrip.get())->IsDestroyed() ||
|
|
|
|
kungFuDeathGrip != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" NOTE, the focused widget was destroyed/changed by compositionend event"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
return true;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-09-22 02:17:40 -07:00
|
|
|
nsGtkIMModule::DispatchTextEvent(const nsAString &aCompositionString,
|
2012-03-08 20:27:51 -08:00
|
|
|
bool aIsCommit)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): DispatchTextEvent, aIsCommit=%s",
|
|
|
|
this, aIsCommit ? "TRUE" : "FALSE"));
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window in this module"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!IsComposing()) {
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" The composition wasn't started, force starting..."));
|
|
|
|
nsCOMPtr<nsIWidget> kungFuDeathGrip = mLastFocusedWindow;
|
|
|
|
if (!DispatchCompositionStart()) {
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
nsEventStatus status;
|
|
|
|
nsRefPtr<nsWindow> lastFocusedWindow = mLastFocusedWindow;
|
|
|
|
|
|
|
|
if (aCompositionString != mDispatchedCompositionString) {
|
2011-10-03 00:56:21 -07:00
|
|
|
nsCompositionEvent compositionUpdate(true, NS_COMPOSITION_UPDATE,
|
2011-09-22 02:17:40 -07:00
|
|
|
mLastFocusedWindow);
|
|
|
|
InitEvent(compositionUpdate);
|
|
|
|
compositionUpdate.data = aCompositionString;
|
|
|
|
mDispatchedCompositionString = aCompositionString;
|
|
|
|
mLastFocusedWindow->DispatchEvent(&compositionUpdate, status);
|
|
|
|
if (lastFocusedWindow->IsDestroyed() ||
|
|
|
|
lastFocusedWindow != mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" NOTE, the focused widget was destroyed/changed by compositionupdate"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2011-09-22 02:17:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
// Store the selected string which will be removed by following text event.
|
|
|
|
if (mCompositionState == eCompositionState_CompositionStartDispatched) {
|
|
|
|
// XXX We should assume, for now, any web applications don't change
|
|
|
|
// selection at handling this text event.
|
|
|
|
nsQueryContentEvent querySelectedTextEvent(true,
|
|
|
|
NS_QUERY_SELECTED_TEXT,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&querySelectedTextEvent, status);
|
|
|
|
if (querySelectedTextEvent.mSucceeded) {
|
|
|
|
mSelectedString = querySelectedTextEvent.mReply.mString;
|
|
|
|
mCompositionStart = querySelectedTextEvent.mReply.mOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
nsTextEvent textEvent(true, NS_TEXT_TEXT, mLastFocusedWindow);
|
2010-03-18 21:21:16 -07:00
|
|
|
InitEvent(textEvent);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t targetOffset = mCompositionStart;
|
2010-03-18 21:21:16 -07:00
|
|
|
|
|
|
|
nsAutoTArray<nsTextRange, 4> textRanges;
|
2012-03-08 20:27:51 -08:00
|
|
|
if (!aIsCommit) {
|
2011-09-22 02:17:40 -07:00
|
|
|
// NOTE: SetTextRangeList() assumes that mDispatchedCompositionString
|
|
|
|
// has been updated already.
|
2010-03-18 21:21:16 -07:00
|
|
|
SetTextRangeList(textRanges);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < textRanges.Length(); i++) {
|
2010-03-18 21:21:16 -07:00
|
|
|
nsTextRange& range = textRanges[i];
|
|
|
|
if (range.mRangeType == NS_TEXTRANGE_SELECTEDRAWTEXT ||
|
|
|
|
range.mRangeType == NS_TEXTRANGE_SELECTEDCONVERTEDTEXT) {
|
|
|
|
targetOffset += range.mStartOffset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
textEvent.rangeCount = textRanges.Length();
|
|
|
|
textEvent.rangeArray = textRanges.Elements();
|
2011-09-22 02:17:40 -07:00
|
|
|
textEvent.theText = mDispatchedCompositionString.get();
|
2010-03-18 21:21:16 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
mCompositionState = aIsCommit ?
|
|
|
|
eCompositionState_CommitTextEventDispatched :
|
|
|
|
eCompositionState_TextEventDispatched;
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow->DispatchEvent(&textEvent, status);
|
2011-09-22 02:17:40 -07:00
|
|
|
if (lastFocusedWindow->IsDestroyed() ||
|
|
|
|
lastFocusedWindow != mLastFocusedWindow) {
|
2010-03-18 21:21:16 -07:00
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" NOTE, the focused widget was destroyed/changed by text event"));
|
2011-10-03 00:56:21 -07:00
|
|
|
return false;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SetCursorPosition(targetOffset);
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
return true;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGtkIMModule::SetTextRangeList(nsTArray<nsTextRange> &aTextRangeList)
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): SetTextRangeList", this));
|
|
|
|
|
|
|
|
NS_PRECONDITION(aTextRangeList.IsEmpty(), "aTextRangeList must be empty");
|
|
|
|
|
|
|
|
gchar *preedit_string;
|
|
|
|
gint cursor_pos;
|
|
|
|
PangoAttrList *feedback_list;
|
|
|
|
gtk_im_context_get_preedit_string(GetContext(), &preedit_string,
|
|
|
|
&feedback_list, &cursor_pos);
|
|
|
|
if (!preedit_string || !*preedit_string) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" preedit_string is null"));
|
|
|
|
pango_attr_list_unref(feedback_list);
|
|
|
|
g_free(preedit_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PangoAttrIterator* iter;
|
|
|
|
iter = pango_attr_list_get_iterator(feedback_list);
|
|
|
|
if (!iter) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, iterator couldn't be allocated"));
|
|
|
|
pango_attr_list_unref(feedback_list);
|
|
|
|
g_free(preedit_string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Depend on gtk2's implementation on XIM support.
|
|
|
|
* In aFeedback got from gtk2, there are only three types of data:
|
|
|
|
* PANGO_ATTR_UNDERLINE, PANGO_ATTR_FOREGROUND, PANGO_ATTR_BACKGROUND.
|
|
|
|
* Corresponding to XIMUnderline, XIMReverse.
|
|
|
|
* Don't take PANGO_ATTR_BACKGROUND into account, since
|
|
|
|
* PANGO_ATTR_BACKGROUND and PANGO_ATTR_FOREGROUND are always
|
|
|
|
* a couple.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
PangoAttribute* attrUnderline =
|
|
|
|
pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
|
|
|
|
PangoAttribute* attrForeground =
|
|
|
|
pango_attr_iterator_get(iter, PANGO_ATTR_FOREGROUND);
|
|
|
|
if (!attrUnderline && !attrForeground) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the range of the current attribute(s)
|
|
|
|
gint start, end;
|
|
|
|
pango_attr_iterator_range(iter, &start, &end);
|
|
|
|
|
|
|
|
nsTextRange range;
|
|
|
|
// XIMReverse | XIMUnderline
|
|
|
|
if (attrUnderline && attrForeground) {
|
|
|
|
range.mRangeType = NS_TEXTRANGE_SELECTEDCONVERTEDTEXT;
|
|
|
|
}
|
|
|
|
// XIMUnderline
|
|
|
|
else if (attrUnderline) {
|
|
|
|
range.mRangeType = NS_TEXTRANGE_CONVERTEDTEXT;
|
|
|
|
}
|
|
|
|
// XIMReverse
|
|
|
|
else if (attrForeground) {
|
|
|
|
range.mRangeType = NS_TEXTRANGE_SELECTEDRAWTEXT;
|
|
|
|
} else {
|
|
|
|
range.mRangeType = NS_TEXTRANGE_RAWINPUT;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
gunichar2* uniStr = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
if (start == 0) {
|
|
|
|
range.mStartOffset = 0;
|
|
|
|
} else {
|
|
|
|
glong uniStrLen;
|
|
|
|
uniStr = g_utf8_to_utf16(preedit_string, start,
|
|
|
|
NULL, &uniStrLen, NULL);
|
|
|
|
if (uniStr) {
|
|
|
|
range.mStartOffset = uniStrLen;
|
|
|
|
g_free(uniStr);
|
2012-07-30 07:20:58 -07:00
|
|
|
uniStr = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glong uniStrLen;
|
|
|
|
uniStr = g_utf8_to_utf16(preedit_string + start, end - start,
|
|
|
|
NULL, &uniStrLen, NULL);
|
|
|
|
if (!uniStr) {
|
|
|
|
range.mEndOffset = range.mStartOffset;
|
|
|
|
} else {
|
|
|
|
range.mEndOffset = range.mStartOffset + uniStrLen;
|
|
|
|
g_free(uniStr);
|
2012-07-30 07:20:58 -07:00
|
|
|
uniStr = nullptr;
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aTextRangeList.AppendElement(range);
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-08-03 15:36:00 -07:00
|
|
|
(" mStartOffset=%u, mEndOffset=%u, mRangeType=%s",
|
2010-03-18 21:21:16 -07:00
|
|
|
range.mStartOffset, range.mEndOffset,
|
|
|
|
GetRangeTypeName(range.mRangeType)));
|
|
|
|
} while (pango_attr_iterator_next(iter));
|
|
|
|
|
|
|
|
nsTextRange range;
|
|
|
|
if (cursor_pos < 0) {
|
|
|
|
range.mStartOffset = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
} else if (uint32_t(cursor_pos) > mDispatchedCompositionString.Length()) {
|
2011-09-22 02:17:40 -07:00
|
|
|
range.mStartOffset = mDispatchedCompositionString.Length();
|
2010-03-18 21:21:16 -07:00
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
range.mStartOffset = uint32_t(cursor_pos);
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
range.mEndOffset = range.mStartOffset;
|
|
|
|
range.mRangeType = NS_TEXTRANGE_CARETPOSITION;
|
|
|
|
aTextRangeList.AppendElement(range);
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-08-03 15:36:00 -07:00
|
|
|
(" mStartOffset=%u, mEndOffset=%u, mRangeType=%s",
|
2010-03-18 21:21:16 -07:00
|
|
|
range.mStartOffset, range.mEndOffset,
|
|
|
|
GetRangeTypeName(range.mRangeType)));
|
|
|
|
|
|
|
|
pango_attr_iterator_destroy(iter);
|
|
|
|
pango_attr_list_unref(feedback_list);
|
|
|
|
g_free(preedit_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsGtkIMModule::SetCursorPosition(uint32_t aTargetOffset)
|
2010-03-18 21:21:16 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2011-08-03 15:36:00 -07:00
|
|
|
("GtkIMModule(%p): SetCursorPosition, aTargetOffset=%u",
|
2010-03-18 21:21:16 -07:00
|
|
|
this, aTargetOffset));
|
|
|
|
|
|
|
|
if (aTargetOffset == PR_UINT32_MAX) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, aTargetOffset is wrong offset"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIMContext *im = GetContext();
|
|
|
|
if (!im) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no context"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
nsQueryContentEvent charRect(true, NS_QUERY_TEXT_RECT,
|
2010-03-18 21:21:16 -07:00
|
|
|
mLastFocusedWindow);
|
|
|
|
charRect.InitForQueryTextRect(aTargetOffset, 1);
|
|
|
|
InitEvent(charRect);
|
|
|
|
nsEventStatus status;
|
|
|
|
mLastFocusedWindow->DispatchEvent(&charRect, status);
|
|
|
|
if (!charRect.mSucceeded) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, NS_QUERY_TEXT_RECT was failed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsWindow* rootWindow =
|
|
|
|
static_cast<nsWindow*>(mLastFocusedWindow->GetTopLevelWidget());
|
|
|
|
|
|
|
|
// Get the position of the rootWindow in screen.
|
|
|
|
gint rootX, rootY;
|
|
|
|
gdk_window_get_origin(rootWindow->GetGdkWindow(), &rootX, &rootY);
|
|
|
|
|
|
|
|
// Get the position of IM context owner window in screen.
|
|
|
|
gint ownerX, ownerY;
|
|
|
|
gdk_window_get_origin(mOwnerWindow->GetGdkWindow(), &ownerX, &ownerY);
|
|
|
|
|
|
|
|
// Compute the caret position in the IM owner window.
|
|
|
|
GdkRectangle area;
|
|
|
|
area.x = charRect.mReply.mRect.x + rootX - ownerX;
|
|
|
|
area.y = charRect.mReply.mRect.y + rootY - ownerY;
|
|
|
|
area.width = 0;
|
|
|
|
area.height = charRect.mReply.mRect.height;
|
|
|
|
|
|
|
|
gtk_im_context_set_cursor_location(im, &area);
|
|
|
|
}
|
|
|
|
|
2010-03-24 08:04:39 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsGtkIMModule::GetCurrentParagraph(nsAString& aText, uint32_t& aCursorPos)
|
2010-03-24 08:04:39 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): GetCurrentParagraph, mCompositionState=%s",
|
|
|
|
this, GetCompositionStateName()));
|
2010-03-24 08:04:39 -07:00
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window in this module"));
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus status;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t selOffset = mCompositionStart;
|
|
|
|
uint32_t selLength = mSelectedString.Length();
|
2010-03-24 08:04:39 -07:00
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
// If focused editor doesn't have composition string, we should use
|
|
|
|
// current selection.
|
|
|
|
if (!EditorHasCompositionString()) {
|
|
|
|
// Query cursor position & selection
|
|
|
|
nsQueryContentEvent querySelectedTextEvent(true,
|
|
|
|
NS_QUERY_SELECTED_TEXT,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&querySelectedTextEvent, status);
|
|
|
|
NS_ENSURE_TRUE(querySelectedTextEvent.mSucceeded, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
selOffset = querySelectedTextEvent.mReply.mOffset;
|
|
|
|
selLength = querySelectedTextEvent.mReply.mString.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" selOffset=%u, selLength=%u",
|
|
|
|
selOffset, selLength));
|
2010-03-24 08:04:39 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
// XXX nsString::Find and nsString::RFind take int32_t for offset, so,
|
2011-02-27 22:00:41 -08:00
|
|
|
// we cannot support this request when the current offset is larger
|
|
|
|
// than PR_INT32_MAX.
|
|
|
|
if (selOffset > PR_INT32_MAX || selLength > PR_INT32_MAX ||
|
|
|
|
selOffset + selLength > PR_INT32_MAX) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, The selection is out of range"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-03-24 08:04:39 -07:00
|
|
|
// Get all text contents of the focused editor
|
2011-10-03 00:56:21 -07:00
|
|
|
nsQueryContentEvent queryTextContentEvent(true,
|
2010-03-24 08:04:39 -07:00
|
|
|
NS_QUERY_TEXT_CONTENT,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
queryTextContentEvent.InitForQueryTextContent(0, PR_UINT32_MAX);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&queryTextContentEvent, status);
|
|
|
|
NS_ENSURE_TRUE(queryTextContentEvent.mSucceeded, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoString textContent(queryTextContentEvent.mReply.mString);
|
2011-02-27 22:00:41 -08:00
|
|
|
if (selOffset + selLength > textContent.Length()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
(" FAILED, The selection is invalid, textContent.Length()=%u",
|
|
|
|
textContent.Length()));
|
2010-03-24 08:04:39 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
// Remove composing string and restore the selected string because
|
|
|
|
// GtkEntry doesn't remove selected string until committing, however,
|
|
|
|
// our editor does it. We should emulate the behavior for IME.
|
|
|
|
if (EditorHasCompositionString() &&
|
|
|
|
mDispatchedCompositionString != mSelectedString) {
|
|
|
|
textContent.Replace(mCompositionStart,
|
|
|
|
mDispatchedCompositionString.Length(), mSelectedString);
|
|
|
|
}
|
|
|
|
|
2010-03-24 08:04:39 -07:00
|
|
|
// Get only the focused paragraph, by looking for newlines
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t parStart = (selOffset == 0) ? 0 :
|
2011-10-03 00:56:21 -07:00
|
|
|
textContent.RFind("\n", false, selOffset - 1, -1) + 1;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t parEnd = textContent.Find("\n", false, selOffset + selLength, -1);
|
2010-03-24 08:04:39 -07:00
|
|
|
if (parEnd < 0) {
|
|
|
|
parEnd = textContent.Length();
|
|
|
|
}
|
|
|
|
aText = nsDependentSubstring(textContent, parStart, parEnd - parStart);
|
2012-08-22 08:56:38 -07:00
|
|
|
aCursorPos = selOffset - uint32_t(parStart);
|
2011-02-27 22:00:41 -08:00
|
|
|
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
(" aText=%s, aText.Length()=%u, aCursorPos=%u",
|
|
|
|
NS_ConvertUTF16toUTF8(aText).get(),
|
|
|
|
aText.Length(), aCursorPos));
|
2010-03-24 08:04:39 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsGtkIMModule::DeleteText(const int32_t aOffset, const uint32_t aNChars)
|
2010-03-24 08:04:39 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
2012-03-08 20:27:51 -08:00
|
|
|
("GtkIMModule(%p): DeleteText, aOffset=%d, aNChars=%d, "
|
|
|
|
"mCompositionState=%s",
|
|
|
|
this, aOffset, aNChars, GetCompositionStateName()));
|
2010-03-24 08:04:39 -07:00
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there are no focused window in this module"));
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2012-06-02 20:27:48 -07:00
|
|
|
if (!aNChars) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, aNChars must not be zero"));
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
nsRefPtr<nsWindow> lastFocusedWindow(mLastFocusedWindow);
|
2010-03-24 08:04:39 -07:00
|
|
|
nsEventStatus status;
|
|
|
|
|
2012-03-08 20:27:51 -08:00
|
|
|
// First, we should cancel current composition because editor cannot
|
|
|
|
// handle changing selection and deleting text.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t selOffset;
|
2012-03-08 20:27:51 -08:00
|
|
|
bool wasComposing = IsComposing();
|
|
|
|
bool editorHadCompositionString = EditorHasCompositionString();
|
|
|
|
if (wasComposing) {
|
|
|
|
selOffset = mCompositionStart;
|
|
|
|
if (editorHadCompositionString &&
|
|
|
|
!DispatchTextEvent(mSelectedString, false)) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, quitting from DeletText"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!DispatchCompositionEnd()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, quitting from DeletText"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Query cursor position & selection
|
|
|
|
nsQueryContentEvent querySelectedTextEvent(true,
|
|
|
|
NS_QUERY_SELECTED_TEXT,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
lastFocusedWindow->DispatchEvent(&querySelectedTextEvent, status);
|
|
|
|
NS_ENSURE_TRUE(querySelectedTextEvent.mSucceeded, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
selOffset = querySelectedTextEvent.mReply.mOffset;
|
|
|
|
}
|
2010-03-24 08:04:39 -07:00
|
|
|
|
2012-06-02 20:27:48 -07:00
|
|
|
// Get all text contents of the focused editor
|
|
|
|
nsQueryContentEvent queryTextContentEvent(true,
|
|
|
|
NS_QUERY_TEXT_CONTENT,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
queryTextContentEvent.InitForQueryTextContent(0, PR_UINT32_MAX);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&queryTextContentEvent, status);
|
|
|
|
NS_ENSURE_TRUE(queryTextContentEvent.mSucceeded, NS_ERROR_FAILURE);
|
|
|
|
if (queryTextContentEvent.mReply.mString.IsEmpty()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, there is no contents"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 utf8Str(
|
|
|
|
nsDependentSubstring(queryTextContentEvent.mReply.mString,
|
|
|
|
0, selOffset));
|
|
|
|
glong offsetInUTF8Characters =
|
|
|
|
g_utf8_strlen(utf8Str.get(), utf8Str.Length()) + aOffset;
|
|
|
|
if (offsetInUTF8Characters < 0) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, aOffset is too small for current cursor pos "
|
|
|
|
"(computed offset: %d)",
|
|
|
|
offsetInUTF8Characters));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF16toUTF8(
|
|
|
|
nsDependentSubstring(queryTextContentEvent.mReply.mString, selOffset),
|
|
|
|
utf8Str);
|
|
|
|
glong countOfCharactersInUTF8 =
|
|
|
|
g_utf8_strlen(utf8Str.get(), utf8Str.Length());
|
|
|
|
glong endInUTF8Characters =
|
|
|
|
offsetInUTF8Characters + aNChars;
|
|
|
|
if (countOfCharactersInUTF8 < endInUTF8Characters) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, aNChars is too large for current contents "
|
|
|
|
"(content length: %d, computed end offset: %d)",
|
|
|
|
countOfCharactersInUTF8, endInUTF8Characters));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar* charAtOffset =
|
|
|
|
g_utf8_offset_to_pointer(utf8Str.get(), offsetInUTF8Characters);
|
|
|
|
gchar* charAtEnd =
|
|
|
|
g_utf8_offset_to_pointer(utf8Str.get(), endInUTF8Characters);
|
|
|
|
|
2010-03-24 08:04:39 -07:00
|
|
|
// Set selection to delete
|
2011-10-03 00:56:21 -07:00
|
|
|
nsSelectionEvent selectionEvent(true, NS_SELECTION_SET,
|
2010-03-24 08:04:39 -07:00
|
|
|
mLastFocusedWindow);
|
2012-06-02 20:27:48 -07:00
|
|
|
|
|
|
|
nsDependentCSubstring utf8StrBeforeOffset(utf8Str, 0,
|
|
|
|
charAtOffset - utf8Str.get());
|
|
|
|
selectionEvent.mOffset =
|
|
|
|
NS_ConvertUTF8toUTF16(utf8StrBeforeOffset).Length();
|
|
|
|
|
|
|
|
nsDependentCSubstring utf8DeletingStr(utf8Str,
|
|
|
|
utf8StrBeforeOffset.Length(),
|
|
|
|
charAtEnd - charAtOffset);
|
|
|
|
selectionEvent.mLength =
|
|
|
|
NS_ConvertUTF8toUTF16(utf8DeletingStr).Length();
|
|
|
|
|
2011-10-03 00:56:21 -07:00
|
|
|
selectionEvent.mReversed = false;
|
|
|
|
selectionEvent.mExpandToClusterBoundary = false;
|
2012-03-08 20:27:51 -08:00
|
|
|
lastFocusedWindow->DispatchEvent(&selectionEvent, status);
|
|
|
|
|
|
|
|
if (!selectionEvent.mSucceeded ||
|
|
|
|
lastFocusedWindow != mLastFocusedWindow ||
|
|
|
|
lastFocusedWindow->Destroyed()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, setting selection caused focus change "
|
|
|
|
"or window destroyed"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-03-24 08:04:39 -07:00
|
|
|
|
|
|
|
// Delete the selection
|
2011-10-03 00:56:21 -07:00
|
|
|
nsContentCommandEvent contentCommandEvent(true,
|
2010-03-24 08:04:39 -07:00
|
|
|
NS_CONTENT_COMMAND_DELETE,
|
|
|
|
mLastFocusedWindow);
|
|
|
|
mLastFocusedWindow->DispatchEvent(&contentCommandEvent, status);
|
2012-03-08 20:27:51 -08:00
|
|
|
|
|
|
|
if (!contentCommandEvent.mSucceeded ||
|
|
|
|
lastFocusedWindow != mLastFocusedWindow ||
|
|
|
|
lastFocusedWindow->Destroyed()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, deleting the selection caused focus change "
|
|
|
|
"or window destroyed"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasComposing) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the composition at new caret position.
|
|
|
|
if (!DispatchCompositionStart()) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, resterting composition start"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!editorHadCompositionString) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString compositionString;
|
|
|
|
GetCompositionString(compositionString);
|
|
|
|
if (!DispatchTextEvent(compositionString, true)) {
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
(" FAILED, restoring composition string"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-03-24 08:04:39 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:21:16 -07:00
|
|
|
void
|
|
|
|
nsGtkIMModule::InitEvent(nsGUIEvent &aEvent)
|
|
|
|
{
|
|
|
|
aEvent.time = PR_Now() / 1000;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-18 21:21:16 -07:00
|
|
|
nsGtkIMModule::ShouldIgnoreNativeCompositionEvent()
|
|
|
|
{
|
|
|
|
PR_LOG(gGtkIMLog, PR_LOG_ALWAYS,
|
|
|
|
("GtkIMModule(%p): ShouldIgnoreNativeCompositionEvent, mLastFocusedWindow=%p, mIgnoreNativeCompositionEvent=%s",
|
|
|
|
this, mLastFocusedWindow,
|
|
|
|
mIgnoreNativeCompositionEvent ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (!mLastFocusedWindow) {
|
2011-10-03 00:56:21 -07:00
|
|
|
return true; // cannot continue
|
2010-03-18 21:21:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return mIgnoreNativeCompositionEvent;
|
|
|
|
}
|