/* -*- 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 #include #include "nsString.h" #include "nsAutoPtr.h" #include "nsTArray.h" #include "nsGUIEvent.h" #include "nsIWidget.h" // If software keyboard is needed in password field and uses GTK2 IM module // for inputting characters, we need to enable IME in password field too. #ifdef MOZ_PLATFORM_MAEMO #define NS_IME_ENABLED_ON_PASSWORD_FIELD 1 #endif class nsWindow; class nsGtkIMModule { protected: typedef mozilla::widget::InputContext InputContext; typedef mozilla::widget::InputContextAction InputContextAction; public: nsrefcnt AddRef() { NS_PRECONDITION(int32_t(mRefCnt) >= 0, "mRefCnt is negative"); ++mRefCnt; NS_LOG_ADDREF(this, mRefCnt, "nsGtkIMModule", sizeof(*this)); return mRefCnt; } nsrefcnt Release() { NS_PRECONDITION(mRefCnt != 0, "mRefCnt is alrady zero"); --mRefCnt; NS_LOG_RELEASE(this, mRefCnt, "nsGtkIMModule"); if (mRefCnt == 0) { mRefCnt = 1; /* stabilize */ delete this; return 0; } return mRefCnt; } protected: nsAutoRefCnt mRefCnt; 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). nsGtkIMModule(nsWindow* aOwnerWindow); ~nsGtkIMModule(); // "Enabled" means the users can use all IMEs. // I.e., the focus is in the normal editors. bool IsEnabled(); // 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); // 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 CommitIMEComposition(nsWindow* aCaller); void SetInputContext(nsWindow* aCaller, const InputContext* aContext, const InputContextAction* aAction); InputContext GetInputContext(); nsresult CancelIMEComposition(nsWindow* aCaller); // If a software keyboard has been opened, this returns TRUE. // Otherwise, FALSE. static bool IsVirtualKeyboardOpened(); protected: // 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; #ifndef NS_IME_ENABLED_ON_PASSWORD_FIELD // mSimpleContext is used for the password field and // the |ime-mode: disabled;| editors. These editors disable IME. // But dead keys should work. Fortunately, the simple IM context of // GTK2 support only them. GtkIMContext *mSimpleContext; #endif // NS_IME_ENABLED_ON_PASSWORD_FIELD // 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; // 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