gecko/widget/gtk/nsGtkIMModule.h

358 lines
15 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:expandtab:shiftwidth=4:tabstop=4:
*/
/* 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/. */
#ifndef __nsGtkIMModule_h__
#define __nsGtkIMModule_h__
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "nsString.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"
#include "nsIWidget.h"
#include "mozilla/EventForwards.h"
class nsWindow;
class nsGtkIMModule
{
protected:
typedef mozilla::widget::InputContext InputContext;
typedef mozilla::widget::InputContextAction InputContextAction;
public:
// aOwnerWindow is a pointer of the owner window. When aOwnerWindow is
// destroyed, the related IME contexts are released (i.e., IME cannot be
// used with the instance after that).
explicit nsGtkIMModule(nsWindow* aOwnerWindow);
NS_INLINE_DECL_REFCOUNTING(nsGtkIMModule)
// "Enabled" means the users can use all IMEs.
// I.e., the focus is in the normal editors.
bool IsEnabled() const;
// OnFocusWindow is a notification that aWindow is going to be focused.
void OnFocusWindow(nsWindow* aWindow);
// OnBlurWindow is a notification that aWindow is going to be unfocused.
void OnBlurWindow(nsWindow* aWindow);
// OnDestroyWindow is a notification that aWindow is going to be destroyed.
void OnDestroyWindow(nsWindow* aWindow);
// OnFocusChangeInGecko is a notification that an editor gets focus.
void OnFocusChangeInGecko(bool aFocus);
// OnSelectionChange is a notification that selection (caret) is changed
// in the focused editor.
void OnSelectionChange(nsWindow* aCaller);
// OnKeyEvent is called when aWindow gets a native key press event or a
// native key release event. If this returns TRUE, the key event was
// filtered by IME. Otherwise, this returns FALSE.
// NOTE: When the keypress event starts composition, this returns TRUE but
// this dispatches keydown event before compositionstart event.
bool OnKeyEvent(nsWindow* aWindow, GdkEventKey* aEvent,
bool aKeyDownEventWasSent = false);
// IME related nsIWidget methods.
nsresult EndIMEComposition(nsWindow* aCaller);
void SetInputContext(nsWindow* aCaller,
const InputContext* aContext,
const InputContextAction* aAction);
InputContext GetInputContext();
void OnUpdateComposition();
protected:
~nsGtkIMModule();
// Owner of an instance of this class. This should be top level window.
// The owner window must release the contexts when it's destroyed because
// the IME contexts need the native window. If OnDestroyWindow() is called
// with the owner window, it'll release IME contexts. Otherwise, it'll
// just clean up any existing composition if it's related to the destroying
// child window.
nsWindow* mOwnerWindow;
// A last focused window in this class's context.
nsWindow* mLastFocusedWindow;
// Actual context. This is used for handling the user's input.
GtkIMContext* mContext;
// mSimpleContext is used for the password field and
// the |ime-mode: disabled;| editors if sUseSimpleContext is true.
// These editors disable IME. But dead keys should work. Fortunately,
// the simple IM context of GTK2 support only them.
GtkIMContext* mSimpleContext;
// mDummyContext is a dummy context and will be used in Focus()
// when the state of mEnabled means disabled. This context's IME state is
// always "closed", so it closes IME forcedly.
GtkIMContext* mDummyContext;
// mComposingContext is not nullptr while one of mContext, mSimpleContext
// and mDummyContext has composition.
// XXX: We don't assume that two or more context have composition same time.
GtkIMContext* mComposingContext;
// IME enabled state and other things defined in InputContext.
// Use following helper methods if you don't need the detail of the status.
InputContext mInputContext;
// mCompositionStart is the start offset of the composition string in the
// current content. When <textarea> or <input> have focus, it means offset
// from the first character of them. When a HTML editor has focus, it
// means offset from the first character of the root element of the editor.
uint32_t mCompositionStart;
// mDispatchedCompositionString is the latest composition string which
// was dispatched by compositionupdate event.
nsString mDispatchedCompositionString;
// mSelectedString is the selected string which was removed by first
// compositionchange event.
nsString mSelectedString;
// OnKeyEvent() temporarily sets mProcessingKeyEvent to the given native
// event.
GdkEventKey* mProcessingKeyEvent;
// current target offset of IME composition
uint32_t mCompositionTargetOffset;
// mCompositionState indicates current status of composition.
enum eCompositionState {
eCompositionState_NotComposing,
eCompositionState_CompositionStartDispatched,
eCompositionState_CompositionChangeEventDispatched,
eCompositionState_CommitCompositionChangeEventDispatched
};
eCompositionState mCompositionState;
bool IsComposing()
{
return (mCompositionState != eCompositionState_NotComposing);
}
bool EditorHasCompositionString()
{
return (mCompositionState ==
eCompositionState_CompositionChangeEventDispatched);
}
/**
* Checks if aContext is valid context for handling composition.
*
* @param aContext An IM context which is specified by native
* composition events.
* @return true if the context is valid context for
* handling composition. Otherwise, false.
*/
bool IsValidContext(GtkIMContext* aContext) const;
#ifdef PR_LOGGING
const char* GetCompositionStateName()
{
switch (mCompositionState) {
case eCompositionState_NotComposing:
return "NotComposing";
case eCompositionState_CompositionStartDispatched:
return "CompositionStartDispatched";
case eCompositionState_CompositionChangeEventDispatched:
return "CompositionChangeEventDispatched";
case eCompositionState_CommitCompositionChangeEventDispatched:
return "CommitCompositionChangeEventDispatched";
default:
return "InvaildState";
}
}
#endif // PR_LOGGING
// mIsIMFocused is set to TRUE when we call gtk_im_context_focus_in(). And
// it's set to FALSE when we call gtk_im_context_focus_out().
bool mIsIMFocused;
// mFilterKeyEvent is used by OnKeyEvent(). If the commit event should
// be processed as simple key event, this is set to TRUE by the commit
// handler.
bool mFilterKeyEvent;
// mKeyDownEventWasSent is used by OnKeyEvent() and
// DispatchCompositionStart(). DispatchCompositionStart() dispatches
// a keydown event if the composition start is caused by a native
// keypress event. If this is true, the keydown event has been dispatched.
// Then, DispatchCompositionStart() doesn't dispatch keydown event.
bool mKeyDownEventWasSent;
// sLastFocusedModule is a pointer to the last focused instance of this
// class. When a instance is destroyed and sLastFocusedModule refers it,
// this is cleared. So, this refers valid pointer always.
static nsGtkIMModule* sLastFocusedModule;
// sUseSimpleContext indeicates if password editors and editors with
// |ime-mode: disabled;| should use GtkIMContextSimple.
// If true, they use GtkIMContextSimple. Otherwise, not.
static bool sUseSimpleContext;
// Callback methods for native IME events. These methods should call
// the related instance methods simply.
static gboolean OnRetrieveSurroundingCallback(GtkIMContext *aContext,
nsGtkIMModule *aModule);
static gboolean OnDeleteSurroundingCallback(GtkIMContext *aContext,
gint aOffset,
gint aNChars,
nsGtkIMModule *aModule);
static void OnCommitCompositionCallback(GtkIMContext *aContext,
const gchar *aString,
nsGtkIMModule* aModule);
static void OnChangeCompositionCallback(GtkIMContext *aContext,
nsGtkIMModule* aModule);
static void OnStartCompositionCallback(GtkIMContext *aContext,
nsGtkIMModule* aModule);
static void OnEndCompositionCallback(GtkIMContext *aContext,
nsGtkIMModule* aModule);
// The instance methods for the native IME events.
gboolean OnRetrieveSurroundingNative(GtkIMContext *aContext);
gboolean OnDeleteSurroundingNative(GtkIMContext *aContext,
gint aOffset,
gint aNChars);
void OnCommitCompositionNative(GtkIMContext *aContext,
const gchar *aString);
void OnChangeCompositionNative(GtkIMContext *aContext);
void OnStartCompositionNative(GtkIMContext *aContext);
void OnEndCompositionNative(GtkIMContext *aContext);
/**
* GetCurrentContext() returns current IM context which is chosen with the
* enabled state.
* WARNING:
* When this class receives some signals for a composition after focus
* is moved in Gecko, the result of this may be different from given
* context by the signals.
*/
GtkIMContext* GetCurrentContext() const;
/**
* GetActiveContext() returns a composing context or current context.
*/
GtkIMContext* GetActiveContext() const
{
return mComposingContext ? mComposingContext : GetCurrentContext();
}
// If the owner window and IM context have been destroyed, returns TRUE.
bool IsDestroyed() { return !mOwnerWindow; }
// Sets focus to the instance of this class.
void Focus();
// Steals focus from the instance of this class.
void Blur();
// Initializes the instance.
void Init();
// Reset the current composition of IME. All native composition events
// during this processing are ignored.
void ResetIME();
// Gets the current composition string by the native APIs.
void GetCompositionString(GtkIMContext* aContext,
nsAString& aCompositionString);
/**
* Generates our text range array from current composition string.
*
* @param aContext A GtkIMContext which is being handled.
* @param aLastDispatchedData The data of the last compositionchange event
* of current composition. This should be
* mDispatchedCompositionString.
*/
already_AddRefed<mozilla::TextRangeArray>
CreateTextRangeArray(GtkIMContext* aContext,
const nsAString& aLastDispatchedData);
/**
* Sets the offset's cursor position to IME.
*
* @param aContext A GtkIMContext which is being handled.
* @param aTargetOffset Offset of a character which is anchor of
* a candidate window. This is offset in
* UTF-16 string.
*/
void SetCursorPosition(GtkIMContext* aContext, uint32_t aTargetOffset);
// Queries the current selection offset of the window.
uint32_t GetSelectionOffset(nsWindow* aWindow);
// Get current paragraph text content and cursor position
nsresult GetCurrentParagraph(nsAString& aText, uint32_t& aCursorPos);
/**
* Delete text portion
*
* @param aContext A GtkIMContext which is being handled.
* @param aOffset Start offset of the range to delete.
* @param aNChars Count of characters to delete. It depends
* on |g_utf8_strlen()| what is one character.
*/
nsresult DeleteText(GtkIMContext* aContext,
int32_t aOffset,
uint32_t aNChars);
// Initializes the GUI event.
void InitEvent(mozilla::WidgetGUIEvent& aEvent);
// Called before destroying the context to work around some platform bugs.
void PrepareToDestroyContext(GtkIMContext *aContext);
/**
* WARNING:
* Following methods dispatch gecko events. Then, the focused widget
* can be destroyed, and also it can be stolen focus. If they returns
* FALSE, callers cannot continue the composition.
* - DispatchCompositionStart
* - DispatchCompositionChangeEvent
* - DispatchCompositionEventsForCommit
*/
/**
* Dispatches a composition start event.
*
* @param aContext A GtkIMContext which is being handled.
* @return true if the focused widget is neither
* destroyed nor changed. Otherwise, false.
*/
bool DispatchCompositionStart(GtkIMContext* aContext);
/**
* Dispatches a compositionchange event.
*
* @param aContext A GtkIMContext which is being handled.
* @param aCompositionString New composition string.
* @return true if the focused widget is neither
* destroyed nor changed. Otherwise, false.
*/
bool DispatchCompositionChangeEvent(GtkIMContext* aContext,
const nsAString& aCompositionString);
/**
* Dispatches a compositionchange event for committing the composition
* string and a compositionend event.
*
* @param aContext A GtkIMContext which is being handled.
* @param aCommitString The string which the composition is
* committed with.
* @return true if the focused widget is neither
* destroyed nor changed. Otherwise, false.
*/
bool DispatchCompositionEventsForCommit(GtkIMContext* aContext,
const nsAString& aCommitString);
};
#endif // __nsGtkIMModule_h__