2009-09-30 19:52:50 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-05-21 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/. */
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2011-05-08 03:19:15 -07:00
|
|
|
#include "TextInputHandler.h"
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif // MOZ_LOGGING
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
#include "nsChildView.h"
|
|
|
|
#include "nsObjCExceptions.h"
|
2009-10-16 02:12:09 -07:00
|
|
|
#include "nsBidiUtils.h"
|
2011-02-23 20:48:12 -08:00
|
|
|
#include "nsToolkit.h"
|
2011-07-20 17:33:16 -07:00
|
|
|
#include "nsCocoaUtils.h"
|
2012-05-17 00:04:16 -07:00
|
|
|
#include "WidgetUtils.h"
|
2011-08-25 01:46:01 -07:00
|
|
|
#include "nsPrintfCString.h"
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
#ifdef __LP64__
|
|
|
|
#include "ComplexTextInputPanel.h"
|
|
|
|
#include <objc/runtime.h>
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // __LP64__
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifndef __LP64__
|
|
|
|
enum {
|
|
|
|
// Currently focused ChildView (while this TSM document is active).
|
|
|
|
// Transient (only set while TSMProcessRawKeyEvent() is processing a key
|
|
|
|
// event), and the ChildView will be retained and released around the call
|
|
|
|
// to TSMProcessRawKeyEvent() -- so it can be weak.
|
|
|
|
kFocusedChildViewTSMDocPropertyTag = 'GKFV', // type ChildView* [WEAK]
|
|
|
|
};
|
|
|
|
|
|
|
|
// Undocumented HIToolbox function used by WebKit to allow Carbon-based IME
|
|
|
|
// to work in a Cocoa-based browser (like Safari or Cocoa-widgets Firefox).
|
|
|
|
// (Recent WebKit versions actually use a thin wrapper around this function
|
|
|
|
// called WKSendKeyEventToTSM().)
|
|
|
|
//
|
|
|
|
// Calling TSMProcessRawKeyEvent() from ChildView's keyDown: and keyUp:
|
|
|
|
// methods (when the ChildView is a plugin view) bypasses Cocoa's IME
|
|
|
|
// infrastructure and (instead) causes Carbon TSM events to be sent on each
|
|
|
|
// NSKeyDown event. We install a Carbon event handler
|
|
|
|
// (PluginKeyEventsHandler()) to catch these events and pass them to Gecko
|
|
|
|
// (which in turn passes them to the plugin).
|
|
|
|
extern "C" long TSMProcessRawKeyEvent(EventRef carbonEvent);
|
|
|
|
#endif // __LP64__
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
2011-05-08 03:19:15 -07:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
PRLogModuleInfo* gLog = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
static const char*
|
2011-09-28 23:19:26 -07:00
|
|
|
OnOrOff(bool aBool)
|
2011-07-20 17:33:16 -07:00
|
|
|
{
|
|
|
|
return aBool ? "ON" : "off";
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
static const char*
|
2011-09-28 23:19:26 -07:00
|
|
|
TrueOrFalse(bool aBool)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
return aBool ? "TRUE" : "FALSE";
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
static const char*
|
|
|
|
GetKeyNameForNativeKeyCode(unsigned short aNativeKeyCode)
|
|
|
|
{
|
|
|
|
switch (aNativeKeyCode) {
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Escape: return "Escape";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightCommand: return "Right-Command";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Command: return "Command";
|
|
|
|
case kVK_Shift: return "Shift";
|
|
|
|
case kVK_CapsLock: return "CapsLock";
|
|
|
|
case kVK_Option: return "Option";
|
|
|
|
case kVK_Control: return "Control";
|
|
|
|
case kVK_RightShift: return "Right-Shift";
|
|
|
|
case kVK_RightOption: return "Right-Option";
|
|
|
|
case kVK_RightControl: return "Right-Control";
|
|
|
|
case kVK_ANSI_KeypadClear: return "Clear";
|
|
|
|
|
|
|
|
case kVK_F1: return "F1";
|
|
|
|
case kVK_F2: return "F2";
|
|
|
|
case kVK_F3: return "F3";
|
|
|
|
case kVK_F4: return "F4";
|
|
|
|
case kVK_F5: return "F5";
|
|
|
|
case kVK_F6: return "F6";
|
|
|
|
case kVK_F7: return "F7";
|
|
|
|
case kVK_F8: return "F8";
|
|
|
|
case kVK_F9: return "F9";
|
|
|
|
case kVK_F10: return "F10";
|
|
|
|
case kVK_F11: return "F11";
|
|
|
|
case kVK_F12: return "F12";
|
|
|
|
case kVK_F13: return "F13/PrintScreen";
|
|
|
|
case kVK_F14: return "F14/ScrollLock";
|
|
|
|
case kVK_F15: return "F15/Pause";
|
|
|
|
|
|
|
|
case kVK_ANSI_Keypad0: return "NumPad-0";
|
|
|
|
case kVK_ANSI_Keypad1: return "NumPad-1";
|
|
|
|
case kVK_ANSI_Keypad2: return "NumPad-2";
|
|
|
|
case kVK_ANSI_Keypad3: return "NumPad-3";
|
|
|
|
case kVK_ANSI_Keypad4: return "NumPad-4";
|
|
|
|
case kVK_ANSI_Keypad5: return "NumPad-5";
|
|
|
|
case kVK_ANSI_Keypad6: return "NumPad-6";
|
|
|
|
case kVK_ANSI_Keypad7: return "NumPad-7";
|
|
|
|
case kVK_ANSI_Keypad8: return "NumPad-8";
|
|
|
|
case kVK_ANSI_Keypad9: return "NumPad-9";
|
|
|
|
|
|
|
|
case kVK_ANSI_KeypadMultiply: return "NumPad-*";
|
|
|
|
case kVK_ANSI_KeypadPlus: return "NumPad-+";
|
|
|
|
case kVK_ANSI_KeypadMinus: return "NumPad--";
|
|
|
|
case kVK_ANSI_KeypadDecimal: return "NumPad-.";
|
|
|
|
case kVK_ANSI_KeypadDivide: return "NumPad-/";
|
|
|
|
case kVK_ANSI_KeypadEquals: return "NumPad-=";
|
|
|
|
case kVK_ANSI_KeypadEnter: return "NumPad-Enter";
|
|
|
|
case kVK_Return: return "Return";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Powerbook_KeypadEnter: return "NumPad-EnterOnPowerBook";
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Insert: return "Insert/Help";
|
|
|
|
case kVK_PC_Delete: return "Delete";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Tab: return "Tab";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Backspace: return "Backspace";
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Home: return "Home";
|
|
|
|
case kVK_End: return "End";
|
|
|
|
case kVK_PageUp: return "PageUp";
|
|
|
|
case kVK_PageDown: return "PageDown";
|
|
|
|
case kVK_LeftArrow: return "LeftArrow";
|
|
|
|
case kVK_RightArrow: return "RightArrow";
|
|
|
|
case kVK_UpArrow: return "UpArrow";
|
|
|
|
case kVK_DownArrow: return "DownArrow";
|
|
|
|
|
|
|
|
case kVK_Function: return "Function";
|
|
|
|
case kVK_VolumeUp: return "VolumeUp";
|
|
|
|
case kVK_VolumeDown: return "VolumeDown";
|
|
|
|
case kVK_Mute: return "Mute";
|
|
|
|
|
|
|
|
case kVK_ISO_Section: return "ISO_Section";
|
|
|
|
|
|
|
|
case kVK_JIS_Yen: return "JIS_Yen";
|
|
|
|
case kVK_JIS_Underscore: return "JIS_Underscore";
|
|
|
|
case kVK_JIS_KeypadComma: return "JIS_KeypadComma";
|
|
|
|
case kVK_JIS_Eisu: return "JIS_Eisu";
|
|
|
|
case kVK_JIS_Kana: return "JIS_Kana";
|
|
|
|
|
|
|
|
case kVK_ANSI_A: return "A";
|
|
|
|
case kVK_ANSI_B: return "B";
|
|
|
|
case kVK_ANSI_C: return "C";
|
|
|
|
case kVK_ANSI_D: return "D";
|
|
|
|
case kVK_ANSI_E: return "E";
|
|
|
|
case kVK_ANSI_F: return "F";
|
|
|
|
case kVK_ANSI_G: return "G";
|
|
|
|
case kVK_ANSI_H: return "H";
|
|
|
|
case kVK_ANSI_I: return "I";
|
|
|
|
case kVK_ANSI_J: return "J";
|
|
|
|
case kVK_ANSI_K: return "K";
|
|
|
|
case kVK_ANSI_L: return "L";
|
|
|
|
case kVK_ANSI_M: return "M";
|
|
|
|
case kVK_ANSI_N: return "N";
|
|
|
|
case kVK_ANSI_O: return "O";
|
|
|
|
case kVK_ANSI_P: return "P";
|
|
|
|
case kVK_ANSI_Q: return "Q";
|
|
|
|
case kVK_ANSI_R: return "R";
|
|
|
|
case kVK_ANSI_S: return "S";
|
|
|
|
case kVK_ANSI_T: return "T";
|
|
|
|
case kVK_ANSI_U: return "U";
|
|
|
|
case kVK_ANSI_V: return "V";
|
|
|
|
case kVK_ANSI_W: return "W";
|
|
|
|
case kVK_ANSI_X: return "X";
|
|
|
|
case kVK_ANSI_Y: return "Y";
|
|
|
|
case kVK_ANSI_Z: return "Z";
|
|
|
|
|
|
|
|
case kVK_ANSI_1: return "1";
|
|
|
|
case kVK_ANSI_2: return "2";
|
|
|
|
case kVK_ANSI_3: return "3";
|
|
|
|
case kVK_ANSI_4: return "4";
|
|
|
|
case kVK_ANSI_5: return "5";
|
|
|
|
case kVK_ANSI_6: return "6";
|
|
|
|
case kVK_ANSI_7: return "7";
|
|
|
|
case kVK_ANSI_8: return "8";
|
|
|
|
case kVK_ANSI_9: return "9";
|
|
|
|
case kVK_ANSI_0: return "0";
|
|
|
|
case kVK_ANSI_Equal: return "Equal";
|
|
|
|
case kVK_ANSI_Minus: return "Minus";
|
|
|
|
case kVK_ANSI_RightBracket: return "RightBracket";
|
|
|
|
case kVK_ANSI_LeftBracket: return "LeftBracket";
|
|
|
|
case kVK_ANSI_Quote: return "Quote";
|
|
|
|
case kVK_ANSI_Semicolon: return "Semicolon";
|
|
|
|
case kVK_ANSI_Backslash: return "Backslash";
|
|
|
|
case kVK_ANSI_Comma: return "Comma";
|
|
|
|
case kVK_ANSI_Slash: return "Slash";
|
|
|
|
case kVK_ANSI_Period: return "Period";
|
|
|
|
case kVK_ANSI_Grave: return "Grave";
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
default: return "undefined";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetCharacters(const NSString* aString)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
nsAutoString str;
|
|
|
|
nsCocoaUtils::GetStringForNSString(aString, str);
|
|
|
|
if (str.IsEmpty()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString escapedStr;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < str.Length(); i++) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PRUnichar ch = str[i];
|
|
|
|
if (ch < 0x20) {
|
|
|
|
nsPrintfCString utf8str("(U+%04X)", ch);
|
|
|
|
escapedStr += NS_ConvertUTF8toUTF16(utf8str);
|
|
|
|
} else if (ch <= 0x7E) {
|
|
|
|
escapedStr += ch;
|
|
|
|
} else {
|
|
|
|
nsPrintfCString utf8str("(U+%04X)", ch);
|
|
|
|
escapedStr += ch;
|
|
|
|
escapedStr += NS_ConvertUTF8toUTF16(utf8str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the result will be freed automatically by cocoa.
|
|
|
|
NSString* result = nsCocoaUtils::ToNSString(escapedStr);
|
|
|
|
return [result UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetCharacters(const CFStringRef aString)
|
|
|
|
{
|
2011-07-21 14:05:58 -07:00
|
|
|
const NSString* str = reinterpret_cast<const NSString*>(aString);
|
2011-07-20 17:33:16 -07:00
|
|
|
return GetCharacters(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetNativeKeyEventType(NSEvent* aNativeEvent)
|
|
|
|
{
|
|
|
|
switch ([aNativeEvent type]) {
|
|
|
|
case NSKeyDown: return "NSKeyDown";
|
|
|
|
case NSKeyUp: return "NSKeyUp";
|
|
|
|
case NSFlagsChanged: return "NSFlagsChanged";
|
|
|
|
default: return "not key event";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetGeckoKeyEventType(const nsEvent &aEvent)
|
|
|
|
{
|
|
|
|
switch (aEvent.message) {
|
|
|
|
case NS_KEY_DOWN: return "NS_KEY_DOWN";
|
|
|
|
case NS_KEY_UP: return "NS_KEY_UP";
|
|
|
|
case NS_KEY_PRESS: return "NS_KEY_PRESS";
|
|
|
|
default: return "not key event";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2012-08-22 08:56:38 -07:00
|
|
|
GetRangeTypeName(uint32_t aRangeType)
|
2011-07-20 17:33: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 "invalid range type";
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
GetWindowLevelName(NSInteger aWindowLevel)
|
|
|
|
{
|
|
|
|
switch (aWindowLevel) {
|
|
|
|
case kCGBaseWindowLevelKey:
|
|
|
|
return "kCGBaseWindowLevelKey (NSNormalWindowLevel)";
|
|
|
|
case kCGMinimumWindowLevelKey:
|
|
|
|
return "kCGMinimumWindowLevelKey";
|
|
|
|
case kCGDesktopWindowLevelKey:
|
|
|
|
return "kCGDesktopWindowLevelKey";
|
|
|
|
case kCGBackstopMenuLevelKey:
|
|
|
|
return "kCGBackstopMenuLevelKey";
|
|
|
|
case kCGNormalWindowLevelKey:
|
|
|
|
return "kCGNormalWindowLevelKey";
|
|
|
|
case kCGFloatingWindowLevelKey:
|
|
|
|
return "kCGFloatingWindowLevelKey (NSFloatingWindowLevel)";
|
|
|
|
case kCGTornOffMenuWindowLevelKey:
|
|
|
|
return "kCGTornOffMenuWindowLevelKey (NSSubmenuWindowLevel, NSTornOffMenuWindowLevel)";
|
|
|
|
case kCGDockWindowLevelKey:
|
|
|
|
return "kCGDockWindowLevelKey (NSDockWindowLevel)";
|
|
|
|
case kCGMainMenuWindowLevelKey:
|
|
|
|
return "kCGMainMenuWindowLevelKey (NSMainMenuWindowLevel)";
|
|
|
|
case kCGStatusWindowLevelKey:
|
|
|
|
return "kCGStatusWindowLevelKey (NSStatusWindowLevel)";
|
|
|
|
case kCGModalPanelWindowLevelKey:
|
|
|
|
return "kCGModalPanelWindowLevelKey (NSModalPanelWindowLevel)";
|
|
|
|
case kCGPopUpMenuWindowLevelKey:
|
|
|
|
return "kCGPopUpMenuWindowLevelKey (NSPopUpMenuWindowLevel)";
|
|
|
|
case kCGDraggingWindowLevelKey:
|
|
|
|
return "kCGDraggingWindowLevelKey";
|
|
|
|
case kCGScreenSaverWindowLevelKey:
|
|
|
|
return "kCGScreenSaverWindowLevelKey (NSScreenSaverWindowLevel)";
|
|
|
|
case kCGMaximumWindowLevelKey:
|
|
|
|
return "kCGMaximumWindowLevelKey";
|
|
|
|
case kCGOverlayWindowLevelKey:
|
|
|
|
return "kCGOverlayWindowLevelKey";
|
|
|
|
case kCGHelpWindowLevelKey:
|
|
|
|
return "kCGHelpWindowLevelKey";
|
|
|
|
case kCGUtilityWindowLevelKey:
|
|
|
|
return "kCGUtilityWindowLevelKey";
|
|
|
|
case kCGDesktopIconWindowLevelKey:
|
|
|
|
return "kCGDesktopIconWindowLevelKey";
|
|
|
|
case kCGCursorWindowLevelKey:
|
|
|
|
return "kCGCursorWindowLevelKey";
|
|
|
|
case kCGNumberOfWindowLevelKeys:
|
|
|
|
return "kCGNumberOfWindowLevelKeys";
|
|
|
|
default:
|
|
|
|
return "unknown window level";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
#endif // #ifdef PR_LOGGING
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t gHandlerInstanceCount = 0;
|
2012-10-15 18:43:50 -07:00
|
|
|
static TISInputSourceWrapper gCurrentInputSource;
|
2009-10-16 02:12:09 -07:00
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
static void
|
|
|
|
InitLogModule()
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
// Clear() is always called when TISInputSourceWrappper is created.
|
|
|
|
if (!gLog) {
|
|
|
|
gLog = PR_NewLogModule("TextInputHandlerWidgets");
|
|
|
|
TextInputHandler::DebugPrintAllKeyboardLayouts();
|
|
|
|
IMEInputHandler::DebugPrintAllIMEModes();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-16 02:12:09 -07:00
|
|
|
static void
|
2012-10-15 18:43:50 -07:00
|
|
|
InitCurrentInputSource()
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
|
|
|
if (gHandlerInstanceCount > 0 &&
|
2012-10-15 18:43:50 -07:00
|
|
|
!gCurrentInputSource.IsInitializedByCurrentInputSource()) {
|
|
|
|
gCurrentInputSource.InitByCurrentInputSource();
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-15 18:43:50 -07:00
|
|
|
FinalizeCurrentInputSource()
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
2012-10-15 18:43:50 -07:00
|
|
|
gCurrentInputSource.Clear();
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* TISInputSourceWrapper implementation
|
2009-09-30 19:52:50 -07:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2009-10-16 02:12:09 -07:00
|
|
|
// static
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper&
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper::CurrentInputSource()
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
2012-10-15 18:43:50 -07:00
|
|
|
InitCurrentInputSource();
|
|
|
|
return gCurrentInputSource;
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TISInputSourceWrapper::TranslateToString(UInt32 aKeyCode, UInt32 aModifiers,
|
|
|
|
UInt32 aKbType, nsAString &aStr)
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
2011-02-23 09:25:11 -08:00
|
|
|
aStr.Truncate();
|
|
|
|
|
|
|
|
const UCKeyboardLayout* UCKey = GetUCKeyboardLayout();
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::TranslateToString, aKeyCode=0x%X, "
|
|
|
|
"aModifiers=0x%X, aKbType=0x%X UCKey=%p\n "
|
|
|
|
"Shift: %s, Ctrl: %s, Opt: %s, Cmd: %s, CapsLock: %s, NumLock: %s",
|
|
|
|
this, aKeyCode, aModifiers, aKbType, UCKey,
|
|
|
|
OnOrOff(aModifiers & shiftKey), OnOrOff(aModifiers & controlKey),
|
|
|
|
OnOrOff(aModifiers & optionKey), OnOrOff(aModifiers & cmdKey),
|
|
|
|
OnOrOff(aModifiers & alphaLock),
|
|
|
|
OnOrOff(aModifiers & kEventKeyModifierNumLockMask)));
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(UCKey, false);
|
2011-02-23 09:25:11 -08:00
|
|
|
|
2009-10-16 02:12:09 -07:00
|
|
|
UInt32 deadKeyState = 0;
|
|
|
|
UniCharCount len;
|
|
|
|
UniChar chars[5];
|
2011-02-23 09:25:11 -08:00
|
|
|
OSStatus err = ::UCKeyTranslate(UCKey, aKeyCode,
|
2009-10-16 02:12:09 -07:00
|
|
|
kUCKeyActionDown, aModifiers >> 8,
|
|
|
|
aKbType, kUCKeyTranslateNoDeadKeysMask,
|
|
|
|
&deadKeyState, 5, &len, chars);
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::TranslateToString, err=0x%X, len=%llu",
|
|
|
|
this, err, len));
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(err == noErr, false);
|
2009-10-16 02:12:09 -07:00
|
|
|
if (len == 0) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(EnsureStringLength(aStr, len), false);
|
2009-10-16 02:12:09 -07:00
|
|
|
NS_ASSERTION(sizeof(PRUnichar) == sizeof(UniChar),
|
|
|
|
"size of PRUnichar and size of UniChar are different");
|
|
|
|
memcpy(aStr.BeginWriting(), chars, len * sizeof(PRUnichar));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::TranslateToString, aStr=\"%s\"",
|
|
|
|
this, NS_ConvertUTF16toUTF8(aStr).get()));
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2011-02-23 09:25:11 -08:00
|
|
|
TISInputSourceWrapper::TranslateToChar(UInt32 aKeyCode, UInt32 aModifiers,
|
|
|
|
UInt32 aKbType)
|
|
|
|
{
|
|
|
|
nsAutoString str;
|
|
|
|
if (!TranslateToString(aKeyCode, aModifiers, aKbType, str) ||
|
|
|
|
str.Length() != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
return static_cast<uint32_t>(str.CharAt(0));
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByInputSourceID(const char* aID)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (!aID)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CFStringRef idstr = ::CFStringCreateWithCString(kCFAllocatorDefault, aID,
|
|
|
|
kCFStringEncodingASCII);
|
|
|
|
InitByInputSourceID(idstr);
|
|
|
|
::CFRelease(idstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByInputSourceID(const nsAFlatString &aID)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (aID.IsEmpty())
|
|
|
|
return;
|
|
|
|
CFStringRef idstr = ::CFStringCreateWithCharacters(kCFAllocatorDefault,
|
|
|
|
aID.get(), aID.Length());
|
|
|
|
InitByInputSourceID(idstr);
|
|
|
|
::CFRelease(idstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByInputSourceID(const CFStringRef aID)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (!aID)
|
|
|
|
return;
|
|
|
|
const void* keys[] = { kTISPropertyInputSourceID };
|
|
|
|
const void* values[] = { aID };
|
|
|
|
CFDictionaryRef filter =
|
|
|
|
::CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);
|
|
|
|
NS_ASSERTION(filter, "failed to create the filter");
|
|
|
|
mInputSourceList = ::TISCreateInputSourceList(filter, true);
|
|
|
|
::CFRelease(filter);
|
|
|
|
if (::CFArrayGetCount(mInputSourceList) > 0) {
|
|
|
|
mInputSource = static_cast<TISInputSourceRef>(
|
|
|
|
const_cast<void *>(::CFArrayGetValueAtIndex(mInputSourceList, 0)));
|
2012-10-15 18:43:50 -07:00
|
|
|
if (IsKeyboardLayout()) {
|
|
|
|
mKeyboardLayout = mInputSource;
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-02-23 09:25:11 -08:00
|
|
|
TISInputSourceWrapper::InitByLayoutID(SInt32 aLayoutID,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aOverrideKeyboard)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-02-23 09:25:11 -08:00
|
|
|
// NOTE: Doument new layout IDs in TextInputHandler.h when you add ones.
|
2009-09-30 19:52:50 -07:00
|
|
|
switch (aLayoutID) {
|
|
|
|
case 0:
|
|
|
|
InitByInputSourceID("com.apple.keylayout.US");
|
|
|
|
break;
|
2012-03-29 20:37:40 -07:00
|
|
|
case 1:
|
2009-09-30 19:52:50 -07:00
|
|
|
InitByInputSourceID("com.apple.keylayout.Greek");
|
|
|
|
break;
|
2012-03-29 20:37:40 -07:00
|
|
|
case 2:
|
2009-09-30 19:52:50 -07:00
|
|
|
InitByInputSourceID("com.apple.keylayout.German");
|
|
|
|
break;
|
2012-03-29 20:37:40 -07:00
|
|
|
case 3:
|
2009-09-30 19:52:50 -07:00
|
|
|
InitByInputSourceID("com.apple.keylayout.Swedish-Pro");
|
|
|
|
break;
|
2012-03-29 20:37:40 -07:00
|
|
|
case 4:
|
|
|
|
InitByInputSourceID("com.apple.keylayout.DVORAK-QWERTYCMD");
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
InitByInputSourceID("com.apple.keylayout.Thai");
|
|
|
|
break;
|
2011-02-23 09:25:11 -08:00
|
|
|
default:
|
|
|
|
Clear();
|
|
|
|
break;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
2011-02-23 09:25:11 -08:00
|
|
|
mOverrideKeyboard = aOverrideKeyboard;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByCurrentInputSource()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyCurrentKeyboardInputSource();
|
2012-10-15 18:43:50 -07:00
|
|
|
mKeyboardLayout = ::TISCopyInputMethodKeyboardLayoutOverride();
|
|
|
|
if (!mKeyboardLayout) {
|
|
|
|
mKeyboardLayout = ::TISCopyCurrentKeyboardLayoutInputSource();
|
|
|
|
}
|
|
|
|
// If this causes composition, the current keyboard layout may input non-ASCII
|
|
|
|
// characters such as Japanese Kana characters or Hangul characters.
|
|
|
|
// However, we need to set ASCII characters to DOM key events for consistency
|
|
|
|
// with other platforms.
|
|
|
|
if (IsOpenedIMEMode()) {
|
|
|
|
TISInputSourceWrapper tis(mKeyboardLayout);
|
|
|
|
if (!tis.IsASCIICapable()) {
|
|
|
|
mKeyboardLayout =
|
|
|
|
::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByCurrentKeyboardLayout()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyCurrentKeyboardLayoutInputSource();
|
2012-10-15 18:43:50 -07:00
|
|
|
mKeyboardLayout = mInputSource;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByCurrentASCIICapableInputSource()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyCurrentASCIICapableKeyboardInputSource();
|
2012-10-15 18:43:50 -07:00
|
|
|
mKeyboardLayout = ::TISCopyInputMethodKeyboardLayoutOverride();
|
|
|
|
if (mKeyboardLayout) {
|
|
|
|
TISInputSourceWrapper tis(mKeyboardLayout);
|
|
|
|
if (!tis.IsASCIICapable()) {
|
|
|
|
mKeyboardLayout = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!mKeyboardLayout) {
|
|
|
|
mKeyboardLayout =
|
|
|
|
::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByCurrentASCIICapableKeyboardLayout()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
|
2012-10-15 18:43:50 -07:00
|
|
|
mKeyboardLayout = mInputSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TISInputSourceWrapper::InitByCurrentInputMethodKeyboardLayoutOverride()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyInputMethodKeyboardLayoutOverride();
|
|
|
|
mKeyboardLayout = mInputSource;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByTISInputSourceRef(TISInputSourceRef aInputSource)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = aInputSource;
|
2012-10-15 18:43:50 -07:00
|
|
|
if (IsKeyboardLayout()) {
|
|
|
|
mKeyboardLayout = mInputSource;
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::InitByLanguage(CFStringRef aLanguage)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
mInputSource = ::TISCopyInputSourceForLanguage(aLanguage);
|
2012-10-15 18:43:50 -07:00
|
|
|
if (IsKeyboardLayout()) {
|
|
|
|
mKeyboardLayout = mInputSource;
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const UCKeyboardLayout*
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetUCKeyboardLayout()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2012-10-15 18:43:50 -07:00
|
|
|
NS_ENSURE_TRUE(mKeyboardLayout, nullptr);
|
2009-10-16 02:12:09 -07:00
|
|
|
if (mUCKeyboardLayout) {
|
|
|
|
return mUCKeyboardLayout;
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
CFDataRef uchr = static_cast<CFDataRef>(
|
2012-10-15 18:43:50 -07:00
|
|
|
::TISGetInputSourceProperty(mKeyboardLayout,
|
2009-09-30 19:52:50 -07:00
|
|
|
kTISPropertyUnicodeKeyLayoutData));
|
|
|
|
|
|
|
|
// We should be always able to get the layout here.
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(uchr, nullptr);
|
2009-10-16 02:12:09 -07:00
|
|
|
mUCKeyboardLayout =
|
|
|
|
reinterpret_cast<const UCKeyboardLayout*>(CFDataGetBytePtr(uchr));
|
|
|
|
return mUCKeyboardLayout;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetBoolProperty(const CFStringRef aKey)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
CFBooleanRef ret = static_cast<CFBooleanRef>(
|
|
|
|
::TISGetInputSourceProperty(mInputSource, aKey));
|
|
|
|
return ::CFBooleanGetValue(ret);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetStringProperty(const CFStringRef aKey,
|
|
|
|
CFStringRef &aStr)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
aStr = static_cast<CFStringRef>(
|
|
|
|
::TISGetInputSourceProperty(mInputSource, aKey));
|
2012-07-30 07:20:58 -07:00
|
|
|
return aStr != nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetStringProperty(const CFStringRef aKey,
|
|
|
|
nsAString &aStr)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
CFStringRef str;
|
|
|
|
GetStringProperty(aKey, str);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::GetStringForNSString((const NSString*)str, aStr);
|
2009-09-30 19:52:50 -07:00
|
|
|
return !aStr.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::IsOpenedIMEMode()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
if (!IsIMEMode())
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2009-09-30 19:52:50 -07:00
|
|
|
return !IsASCIICapable();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::IsIMEMode()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
CFStringRef str;
|
|
|
|
GetInputSourceType(str);
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(str, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
return ::CFStringCompare(kTISTypeKeyboardInputMode,
|
|
|
|
str, 0) == kCFCompareEqualTo;
|
|
|
|
}
|
|
|
|
|
2012-10-15 18:43:50 -07:00
|
|
|
bool
|
|
|
|
TISInputSourceWrapper::IsKeyboardLayout()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
|
|
|
CFStringRef str;
|
|
|
|
GetInputSourceType(str);
|
|
|
|
NS_ENSURE_TRUE(str, false);
|
|
|
|
return ::CFStringCompare(kTISTypeKeyboardLayout,
|
|
|
|
str, 0) == kCFCompareEqualTo;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetLanguageList(CFArrayRef &aLanguageList)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
aLanguageList = static_cast<CFArrayRef>(
|
|
|
|
::TISGetInputSourceProperty(mInputSource,
|
|
|
|
kTISPropertyInputSourceLanguages));
|
2012-07-30 07:20:58 -07:00
|
|
|
return aLanguageList != nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetPrimaryLanguage(CFStringRef &aPrimaryLanguage)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
CFArrayRef langList;
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(GetLanguageList(langList), false);
|
2009-09-30 19:52:50 -07:00
|
|
|
if (::CFArrayGetCount(langList) == 0)
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2009-09-30 19:52:50 -07:00
|
|
|
aPrimaryLanguage =
|
|
|
|
static_cast<CFStringRef>(::CFArrayGetValueAtIndex(langList, 0));
|
2012-07-30 07:20:58 -07:00
|
|
|
return aPrimaryLanguage != nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::GetPrimaryLanguage(nsAString &aPrimaryLanguage)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(mInputSource, false);
|
2009-09-30 19:52:50 -07:00
|
|
|
CFStringRef primaryLanguage;
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(GetPrimaryLanguage(primaryLanguage), false);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::GetStringForNSString((const NSString*)primaryLanguage,
|
|
|
|
aPrimaryLanguage);
|
2009-09-30 19:52:50 -07:00
|
|
|
return !aPrimaryLanguage.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::IsForRTLLanguage()
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
|
|
|
if (mIsRTL < 0) {
|
|
|
|
// Get the input character of the 'A' key of ANSI keyboard layout.
|
|
|
|
nsAutoString str;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ret = TranslateToString(kVK_ANSI_A, 0, eKbdType_ANSI, str);
|
2009-10-16 02:12:09 -07:00
|
|
|
NS_ENSURE_TRUE(ret, ret);
|
|
|
|
PRUnichar ch = str.IsEmpty() ? PRUnichar(0) : str.CharAt(0);
|
|
|
|
mIsRTL = UCS2_CHAR_IS_BIDI(ch) || ch == 0xD802 || ch == 0xD803;
|
|
|
|
}
|
|
|
|
return mIsRTL != 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper::IsInitializedByCurrentInputSource()
|
2009-10-16 02:12:09 -07:00
|
|
|
{
|
2012-10-15 18:43:50 -07:00
|
|
|
return mInputSource == ::TISCopyCurrentKeyboardInputSource();
|
2009-10-16 02:12:09 -07:00
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::Select()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (!mInputSource)
|
|
|
|
return;
|
|
|
|
::TISSelectInputSource(mInputSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper::Clear()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
// Clear() is always called when TISInputSourceWrappper is created.
|
|
|
|
InitLogModule();
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
if (mInputSourceList) {
|
|
|
|
::CFRelease(mInputSourceList);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mInputSourceList = nullptr;
|
|
|
|
mInputSource = nullptr;
|
2012-10-15 18:43:50 -07:00
|
|
|
mKeyboardLayout = nullptr;
|
2009-10-16 02:12:09 -07:00
|
|
|
mIsRTL = -1;
|
2012-07-30 07:20:58 -07:00
|
|
|
mUCKeyboardLayout = nullptr;
|
2011-09-30 17:20:33 -07:00
|
|
|
mOverrideKeyboard = false;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TISInputSourceWrapper::InitKeyEvent(NSEvent *aNativeKeyEvent,
|
2012-07-20 16:24:48 -07:00
|
|
|
nsKeyEvent& aKeyEvent,
|
|
|
|
const nsAString *aInsertString)
|
2011-02-23 09:25:11 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
2012-05-17 00:04:16 -07:00
|
|
|
("%p TISInputSourceWrapper::InitKeyEvent, aNativeKeyEvent=%p, "
|
2012-07-20 16:24:48 -07:00
|
|
|
"aKeyEvent.message=%s, aInsertString=%p",
|
|
|
|
this, aNativeKeyEvent, GetGeckoKeyEventType(aKeyEvent), aInsertString));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
NS_ENSURE_TRUE(aNativeKeyEvent, );
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
nsCocoaUtils::InitInputEvent(aKeyEvent, aNativeKeyEvent);
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
aKeyEvent.refPoint = nsIntPoint(0, 0);
|
|
|
|
|
2012-05-17 00:04:16 -07:00
|
|
|
// If a keyboard layout override is set, we also need to force the keyboard
|
|
|
|
// type to something ANSI to avoid test failures on machines with JIS
|
|
|
|
// keyboards (since the pair of keyboard layout and physical keyboard type
|
|
|
|
// form the actual key layout). This assumes that the test setting the
|
|
|
|
// override was written assuming an ANSI keyboard.
|
|
|
|
UInt32 kbType = mOverrideKeyboard ? eKbdType_ANSI : ::LMGetKbdType();
|
|
|
|
|
2012-07-20 16:24:48 -07:00
|
|
|
UInt32 nativeKeyCode = [aNativeKeyEvent keyCode];
|
|
|
|
|
|
|
|
bool isPrintableKey = !TextInputHandler::IsSpecialGeckoKey(nativeKeyCode);
|
2012-08-30 20:55:23 -07:00
|
|
|
if (isPrintableKey &&
|
|
|
|
[aNativeKeyEvent type] != NSKeyDown &&
|
2012-08-30 21:22:26 -07:00
|
|
|
[aNativeKeyEvent type] != NSKeyUp) {
|
2012-08-30 20:55:23 -07:00
|
|
|
NS_WARNING("Why the printable key doesn't cause NSKeyDown or NSKeyUp?");
|
|
|
|
isPrintableKey = false;
|
|
|
|
}
|
2012-07-20 16:24:48 -07:00
|
|
|
|
|
|
|
// Decide what string will be input.
|
|
|
|
nsAutoString insertString;
|
|
|
|
if (aInsertString) {
|
|
|
|
// If the caller expects that the aInsertString will be input, we shouldn't
|
|
|
|
// change it.
|
|
|
|
insertString = *aInsertString;
|
|
|
|
} else if (isPrintableKey) {
|
2012-10-15 18:43:50 -07:00
|
|
|
// If IME is open, [aNativeKeyEvent characters] may be a character
|
|
|
|
// which will be appended to the composition string. However, especially,
|
|
|
|
// while IME is disabled, most users and developers expect the key event
|
|
|
|
// works as IME closed. So, we should compute the insertString with
|
|
|
|
// the ASCII capable keyboard layout.
|
|
|
|
// NOTE: Such keyboard layouts typically change the layout to its ASCII
|
|
|
|
// capable layout when Command key is pressed. And we don't worry
|
|
|
|
// when Control key is pressed too because it causes inputting
|
|
|
|
// control characters.
|
|
|
|
if (!aKeyEvent.IsMeta() && !aKeyEvent.IsControl() && IsOpenedIMEMode()) {
|
|
|
|
UInt32 state =
|
|
|
|
nsCocoaUtils::ConvertToCarbonModifier([aNativeKeyEvent modifierFlags]);
|
2012-10-25 11:44:37 -07:00
|
|
|
uint32_t ch = TranslateToChar(nativeKeyCode, state, kbType);
|
2012-10-15 18:43:50 -07:00
|
|
|
if (ch) {
|
|
|
|
insertString = ch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the caller isn't sure what string will be input, let's use
|
|
|
|
// characters of NSEvent.
|
|
|
|
nsCocoaUtils::GetStringForNSString([aNativeKeyEvent characters],
|
|
|
|
insertString);
|
|
|
|
}
|
|
|
|
|
2012-07-20 16:24:48 -07:00
|
|
|
// If control key is pressed and the eventChars is a non-printable control
|
|
|
|
// character, we should convert it to ASCII alphabet.
|
|
|
|
if (aKeyEvent.IsControl() &&
|
|
|
|
!insertString.IsEmpty() && insertString[0] <= PRUnichar(26)) {
|
|
|
|
insertString = (aKeyEvent.IsShift() ^ aKeyEvent.IsCapsLocked()) ?
|
|
|
|
static_cast<PRUnichar>(insertString[0] + ('A' - 1)) :
|
|
|
|
static_cast<PRUnichar>(insertString[0] + ('a' - 1));
|
|
|
|
}
|
|
|
|
// If Meta key is pressed, it may cause to switch the keyboard layout like
|
|
|
|
// Arabic, Russian, Hebrew, Greek and Dvorak-QWERTY.
|
|
|
|
else if (aKeyEvent.IsMeta() &&
|
|
|
|
!(aKeyEvent.IsControl() || aKeyEvent.IsAlt())) {
|
|
|
|
UInt32 numLockState =
|
|
|
|
aKeyEvent.IsNumLocked() ? kEventKeyModifierNumLockMask : 0;
|
|
|
|
UInt32 capsLockState = aKeyEvent.IsCapsLocked() ? alphaLock : 0;
|
|
|
|
UInt32 shiftState = aKeyEvent.IsShift() ? shiftKey : 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t uncmdedChar =
|
2012-07-20 16:24:48 -07:00
|
|
|
TranslateToChar(nativeKeyCode, numLockState, kbType);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cmdedChar =
|
2012-07-20 16:24:48 -07:00
|
|
|
TranslateToChar(nativeKeyCode, cmdKey | numLockState, kbType);
|
|
|
|
// If we can make a good guess at the characters that the user would
|
|
|
|
// expect this key combination to produce (with and without Shift) then
|
|
|
|
// use those characters. This also corrects for CapsLock.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ch = 0;
|
2012-07-20 16:24:48 -07:00
|
|
|
if (uncmdedChar == cmdedChar) {
|
|
|
|
// The characters produced with Command seem similar to those without
|
|
|
|
// Command.
|
|
|
|
ch = TranslateToChar(nativeKeyCode,
|
|
|
|
shiftState | capsLockState | numLockState, kbType);
|
|
|
|
} else {
|
|
|
|
TISInputSourceWrapper USLayout("com.apple.keylayout.US");
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t uncmdedUSChar =
|
2012-07-20 16:24:48 -07:00
|
|
|
USLayout.TranslateToChar(nativeKeyCode, numLockState, kbType);
|
|
|
|
// If it looks like characters from US keyboard layout when Command key
|
|
|
|
// is pressed, we should compute a character in the layout.
|
|
|
|
if (uncmdedUSChar == cmdedChar) {
|
|
|
|
ch = USLayout.TranslateToChar(nativeKeyCode,
|
|
|
|
shiftState | capsLockState | numLockState, kbType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a more preferred character for the commanded key event,
|
|
|
|
// we should use it.
|
|
|
|
if (ch) {
|
|
|
|
insertString = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
aKeyEvent.keyCode =
|
2012-07-20 16:24:48 -07:00
|
|
|
ComputeGeckoKeyCode(nativeKeyCode, kbType, aKeyEvent.IsMeta());
|
2011-02-23 09:25:11 -08:00
|
|
|
|
2012-07-20 16:24:48 -07:00
|
|
|
switch (nativeKeyCode) {
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Command:
|
|
|
|
case kVK_Shift:
|
|
|
|
case kVK_Option:
|
|
|
|
case kVK_Control:
|
2012-05-03 01:35:02 -07:00
|
|
|
aKeyEvent.location = nsIDOMKeyEvent::DOM_KEY_LOCATION_LEFT;
|
|
|
|
break;
|
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightCommand:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightShift:
|
|
|
|
case kVK_RightOption:
|
|
|
|
case kVK_RightControl:
|
2012-05-03 01:35:02 -07:00
|
|
|
aKeyEvent.location = nsIDOMKeyEvent::DOM_KEY_LOCATION_RIGHT;
|
|
|
|
break;
|
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_ANSI_Keypad0:
|
|
|
|
case kVK_ANSI_Keypad1:
|
|
|
|
case kVK_ANSI_Keypad2:
|
|
|
|
case kVK_ANSI_Keypad3:
|
|
|
|
case kVK_ANSI_Keypad4:
|
|
|
|
case kVK_ANSI_Keypad5:
|
|
|
|
case kVK_ANSI_Keypad6:
|
|
|
|
case kVK_ANSI_Keypad7:
|
|
|
|
case kVK_ANSI_Keypad8:
|
|
|
|
case kVK_ANSI_Keypad9:
|
|
|
|
case kVK_ANSI_KeypadMultiply:
|
|
|
|
case kVK_ANSI_KeypadPlus:
|
|
|
|
case kVK_ANSI_KeypadMinus:
|
|
|
|
case kVK_ANSI_KeypadDecimal:
|
|
|
|
case kVK_ANSI_KeypadDivide:
|
|
|
|
case kVK_ANSI_KeypadEquals:
|
|
|
|
case kVK_ANSI_KeypadEnter:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Powerbook_KeypadEnter:
|
2012-05-03 01:35:02 -07:00
|
|
|
aKeyEvent.location = nsIDOMKeyEvent::DOM_KEY_LOCATION_NUMPAD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
aKeyEvent.location = nsIDOMKeyEvent::DOM_KEY_LOCATION_STANDARD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyEvent, "
|
|
|
|
"shift=%s, ctrl=%s, alt=%s, meta=%s",
|
2012-04-24 20:00:02 -07:00
|
|
|
this, OnOrOff(aKeyEvent.IsShift()), OnOrOff(aKeyEvent.IsControl()),
|
|
|
|
OnOrOff(aKeyEvent.IsAlt()), OnOrOff(aKeyEvent.IsMeta())));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
if (aKeyEvent.message == NS_KEY_PRESS &&
|
2012-07-20 16:24:48 -07:00
|
|
|
(isPrintableKey || !insertString.IsEmpty())) {
|
|
|
|
InitKeyPressEvent(aNativeKeyEvent,
|
|
|
|
insertString.IsEmpty() ? 0 : insertString[0],
|
|
|
|
aKeyEvent, kbType);
|
2013-04-23 20:49:47 -07:00
|
|
|
} else {
|
|
|
|
aKeyEvent.charCode = 0;
|
|
|
|
aKeyEvent.isChar = false; // XXX not used in XP level
|
2011-02-23 09:25:11 -08:00
|
|
|
|
2013-04-23 20:49:47 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyEvent, keyCode=0x%X charCode=0x0",
|
|
|
|
this, aKeyEvent.keyCode));
|
|
|
|
}
|
2011-02-23 09:25:11 -08:00
|
|
|
|
2013-04-23 20:49:47 -07:00
|
|
|
// Compute the key for non-printable keys and some special printable keys.
|
|
|
|
aKeyEvent.mKeyNameIndex = ComputeGeckoKeyNameIndex(nativeKeyCode);
|
|
|
|
if (isPrintableKey &&
|
|
|
|
aKeyEvent.mKeyNameIndex == KEY_NAME_INDEX_Unidentified) {
|
|
|
|
// If the key name isn't in the list and the key is a printable key but
|
|
|
|
// inserting no characters without control key nor command key, then,
|
|
|
|
// check if the key is dead key.
|
|
|
|
if (insertString.IsEmpty() &&
|
|
|
|
!aKeyEvent.IsControl() && !aKeyEvent.IsMeta()) {
|
|
|
|
UInt32 state =
|
|
|
|
nsCocoaUtils::ConvertToCarbonModifier([aNativeKeyEvent modifierFlags]);
|
|
|
|
uint32_t ch = TranslateToChar(nativeKeyCode, state, kbType);
|
|
|
|
if (ch) {
|
|
|
|
aKeyEvent.mKeyNameIndex =
|
|
|
|
WidgetUtils::GetDeadKeyNameIndex(static_cast<PRUnichar>(ch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the printable key isn't a dead key, we should set printable key name
|
|
|
|
// for now.
|
|
|
|
if (aKeyEvent.mKeyNameIndex == KEY_NAME_INDEX_Unidentified) {
|
|
|
|
aKeyEvent.mKeyNameIndex = KEY_NAME_INDEX_PrintableKey;
|
|
|
|
}
|
|
|
|
}
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TISInputSourceWrapper::InitKeyPressEvent(NSEvent *aNativeKeyEvent,
|
2012-07-20 16:24:48 -07:00
|
|
|
PRUnichar aInsertChar,
|
2012-05-17 00:04:16 -07:00
|
|
|
nsKeyEvent& aKeyEvent,
|
|
|
|
UInt32 aKbType)
|
2011-02-23 09:25:11 -08:00
|
|
|
{
|
2012-05-25 19:35:58 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2012-10-15 18:43:50 -07:00
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
NS_ASSERTION(aKeyEvent.message == NS_KEY_PRESS,
|
|
|
|
"aKeyEvent must be NS_KEY_PRESS event");
|
|
|
|
|
2012-05-25 19:35:58 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(gLog, PR_LOG_ALWAYS)) {
|
|
|
|
nsAutoString chars;
|
|
|
|
nsCocoaUtils::GetStringForNSString([aNativeKeyEvent characters], chars);
|
|
|
|
NS_ConvertUTF16toUTF8 utf8Chars(chars);
|
2012-07-20 16:24:48 -07:00
|
|
|
PRUnichar expectedChar = static_cast<PRUnichar>(aInsertChar);
|
2012-05-25 19:35:58 -07:00
|
|
|
NS_ConvertUTF16toUTF8 utf8ExpectedChar(&expectedChar, 1);
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, aNativeKeyEvent=%p, "
|
2012-07-20 16:24:48 -07:00
|
|
|
"[aNativeKeyEvent characters]=\"%s\", aInsertChar=0x%X(%s), "
|
2012-05-25 19:35:58 -07:00
|
|
|
"aKeyEvent.message=%s, aKbType=0x%X, IsOpenedIMEMode()=%s",
|
2012-07-20 16:24:48 -07:00
|
|
|
this, aNativeKeyEvent, utf8Chars.get(), aInsertChar,
|
2012-05-25 19:35:58 -07:00
|
|
|
utf8ExpectedChar.get(), GetGeckoKeyEventType(aKeyEvent), aKbType,
|
|
|
|
TrueOrFalse(IsOpenedIMEMode())));
|
|
|
|
}
|
|
|
|
#endif // #ifdef PR_LOGGING
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
aKeyEvent.isChar = true; // this is not a special key XXX not used in XP
|
2012-07-20 16:24:48 -07:00
|
|
|
aKeyEvent.charCode = aInsertChar;
|
2011-02-23 09:25:11 -08:00
|
|
|
if (aKeyEvent.charCode != 0) {
|
|
|
|
aKeyEvent.keyCode = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, "
|
|
|
|
"aKeyEvent.keyCode=0x%X, aKeyEvent.charCode=0x%X",
|
|
|
|
this, aKeyEvent.keyCode, aKeyEvent.charCode));
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
if (!aKeyEvent.IsControl() && !aKeyEvent.IsMeta() && !aKeyEvent.IsAlt()) {
|
2011-02-23 09:25:11 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TISInputSourceWrapper USLayout("com.apple.keylayout.US");
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isRomanKeyboardLayout = IsASCIICapable();
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
UInt32 key = [aNativeKeyEvent keyCode];
|
|
|
|
|
|
|
|
// Caps lock and num lock modifier state:
|
|
|
|
UInt32 lockState = 0;
|
|
|
|
if ([aNativeKeyEvent modifierFlags] & NSAlphaShiftKeyMask) {
|
|
|
|
lockState |= alphaLock;
|
|
|
|
}
|
|
|
|
if ([aNativeKeyEvent modifierFlags] & NSNumericPadKeyMask) {
|
|
|
|
lockState |= kEventKeyModifierNumLockMask;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, "
|
2012-05-17 00:04:16 -07:00
|
|
|
"isRomanKeyboardLayout=%s, key=0x%X",
|
|
|
|
this, TrueOrFalse(isRomanKeyboardLayout), aKbType, key));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
nsString str;
|
|
|
|
|
|
|
|
// normal chars
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t unshiftedChar = TranslateToChar(key, lockState, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
UInt32 shiftLockMod = shiftKey | lockState;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t shiftedChar = TranslateToChar(key, shiftLockMod, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// characters generated with Cmd key
|
|
|
|
// XXX we should remove CapsLock state, which changes characters from
|
|
|
|
// Latin to Cyrillic with Russian layout on 10.4 only when Cmd key
|
|
|
|
// is pressed.
|
|
|
|
UInt32 numState = (lockState & ~alphaLock); // only num lock state
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t uncmdedChar = TranslateToChar(key, numState, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
UInt32 shiftNumMod = numState | shiftKey;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t uncmdedShiftChar = TranslateToChar(key, shiftNumMod, aKbType);
|
|
|
|
uint32_t uncmdedUSChar = USLayout.TranslateToChar(key, numState, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
UInt32 cmdNumMod = cmdKey | numState;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cmdedChar = TranslateToChar(key, cmdNumMod, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
UInt32 cmdShiftNumMod = shiftKey | cmdNumMod;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cmdedShiftChar = TranslateToChar(key, cmdShiftNumMod, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// Is the keyboard layout changed by Cmd key?
|
|
|
|
// E.g., Arabic, Russian, Hebrew, Greek and Dvorak-QWERTY.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCmdSwitchLayout = uncmdedChar != cmdedChar;
|
2011-02-23 09:25:11 -08:00
|
|
|
// Is the keyboard layout for Latin, but Cmd key switches the layout?
|
|
|
|
// I.e., Dvorak-QWERTY
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isDvorakQWERTY = isCmdSwitchLayout && isRomanKeyboardLayout;
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// If the current keyboard is not Dvorak-QWERTY or Cmd is not pressed,
|
|
|
|
// we should append unshiftedChar and shiftedChar for handling the
|
|
|
|
// normal characters. These are the characters that the user is most
|
|
|
|
// likely to associate with this key.
|
|
|
|
if ((unshiftedChar || shiftedChar) &&
|
2012-04-24 20:00:02 -07:00
|
|
|
(!aKeyEvent.IsMeta() || !isDvorakQWERTY)) {
|
2011-02-23 09:25:11 -08:00
|
|
|
nsAlternativeCharCode altCharCodes(unshiftedChar, shiftedChar);
|
|
|
|
aKeyEvent.alternativeCharCodes.AppendElement(altCharCodes);
|
|
|
|
}
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, "
|
|
|
|
"aKeyEvent.isMeta=%s, isDvorakQWERTY=%s, "
|
|
|
|
"unshiftedChar=U+%X, shiftedChar=U+%X",
|
2012-04-24 20:00:02 -07:00
|
|
|
this, OnOrOff(aKeyEvent.IsMeta()), TrueOrFalse(isDvorakQWERTY),
|
2011-07-20 17:33:16 -07:00
|
|
|
unshiftedChar, shiftedChar));
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// Most keyboard layouts provide the same characters in the NSEvents
|
|
|
|
// with Command+Shift as with Command. However, with Command+Shift we
|
|
|
|
// want the character on the second level. e.g. With a US QWERTY
|
|
|
|
// layout, we want "?" when the "/","?" key is pressed with
|
|
|
|
// Command+Shift.
|
|
|
|
|
|
|
|
// On a German layout, the OS gives us '/' with Cmd+Shift+SS(eszett)
|
|
|
|
// even though Cmd+SS is 'SS' and Shift+'SS' is '?'. This '/' seems
|
|
|
|
// like a hack to make the Cmd+"?" event look the same as the Cmd+"?"
|
|
|
|
// event on a US keyboard. The user thinks they are typing Cmd+"?", so
|
|
|
|
// we'll prefer the "?" character, replacing charCode with shiftedChar
|
|
|
|
// when Shift is pressed. However, in case there is a layout where the
|
|
|
|
// character unique to Cmd+Shift is the character that the user expects,
|
|
|
|
// we'll send it as an alternative char.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasCmdShiftOnlyChar =
|
2011-02-23 09:25:11 -08:00
|
|
|
cmdedChar != cmdedShiftChar && uncmdedShiftChar != cmdedShiftChar;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t originalCmdedShiftChar = cmdedShiftChar;
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// If we can make a good guess at the characters that the user would
|
|
|
|
// expect this key combination to produce (with and without Shift) then
|
|
|
|
// use those characters. This also corrects for CapsLock, which was
|
|
|
|
// ignored above.
|
|
|
|
if (!isCmdSwitchLayout) {
|
|
|
|
// The characters produced with Command seem similar to those without
|
|
|
|
// Command.
|
|
|
|
if (unshiftedChar) {
|
|
|
|
cmdedChar = unshiftedChar;
|
|
|
|
}
|
|
|
|
if (shiftedChar) {
|
|
|
|
cmdedShiftChar = shiftedChar;
|
|
|
|
}
|
|
|
|
} else if (uncmdedUSChar == cmdedChar) {
|
|
|
|
// It looks like characters from a US layout are provided when Command
|
|
|
|
// is down.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ch = USLayout.TranslateToChar(key, lockState, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
if (ch) {
|
|
|
|
cmdedChar = ch;
|
|
|
|
}
|
2012-05-17 00:04:16 -07:00
|
|
|
ch = USLayout.TranslateToChar(key, shiftLockMod, aKbType);
|
2011-02-23 09:25:11 -08:00
|
|
|
if (ch) {
|
|
|
|
cmdedShiftChar = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current keyboard layout is switched by the Cmd key,
|
|
|
|
// we should append cmdedChar and shiftedCmdChar that are
|
2012-03-29 20:37:40 -07:00
|
|
|
// Latin char for the key.
|
|
|
|
// If the keyboard layout is Dvorak-QWERTY, we should append them only when
|
|
|
|
// command key is pressed because when command key isn't pressed, uncmded
|
|
|
|
// chars have been appended already.
|
|
|
|
if ((cmdedChar || cmdedShiftChar) && isCmdSwitchLayout &&
|
2012-04-24 20:00:02 -07:00
|
|
|
(aKeyEvent.IsMeta() || !isDvorakQWERTY)) {
|
2011-02-23 09:25:11 -08:00
|
|
|
nsAlternativeCharCode altCharCodes(cmdedChar, cmdedShiftChar);
|
|
|
|
aKeyEvent.alternativeCharCodes.AppendElement(altCharCodes);
|
|
|
|
}
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, "
|
|
|
|
"hasCmdShiftOnlyChar=%s, isCmdSwitchLayout=%s, isDvorakQWERTY=%s, "
|
|
|
|
"cmdedChar=U+%X, cmdedShiftChar=U+%X",
|
|
|
|
this, TrueOrFalse(hasCmdShiftOnlyChar), TrueOrFalse(isDvorakQWERTY),
|
|
|
|
TrueOrFalse(isDvorakQWERTY), cmdedChar, cmdedShiftChar));
|
2011-02-23 09:25:11 -08:00
|
|
|
// Special case for 'SS' key of German layout. See the comment of
|
|
|
|
// hasCmdShiftOnlyChar definition for the detail.
|
|
|
|
if (hasCmdShiftOnlyChar && originalCmdedShiftChar) {
|
|
|
|
nsAlternativeCharCode altCharCodes(0, originalCmdedShiftChar);
|
|
|
|
aKeyEvent.alternativeCharCodes.AppendElement(altCharCodes);
|
|
|
|
}
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::InitKeyPressEvent, "
|
|
|
|
"hasCmdShiftOnlyChar=%s, originalCmdedShiftChar=U+%X",
|
|
|
|
this, TrueOrFalse(hasCmdShiftOnlyChar), originalCmdedShiftChar));
|
2012-05-25 19:35:58 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2012-05-17 00:04:16 -07:00
|
|
|
TISInputSourceWrapper::ComputeGeckoKeyCode(UInt32 aNativeKeyCode,
|
|
|
|
UInt32 aKbType,
|
|
|
|
bool aCmdIsPressed)
|
|
|
|
{
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TISInputSourceWrapper::ComputeGeckoKeyCode, aNativeKeyCode=0x%X, "
|
|
|
|
"aKbType=0x%X, aCmdIsPressed=%s, IsOpenedIMEMode()=%s, "
|
|
|
|
"IsASCIICapable()=%s",
|
|
|
|
this, aNativeKeyCode, aKbType, TrueOrFalse(aCmdIsPressed),
|
|
|
|
TrueOrFalse(IsOpenedIMEMode()), TrueOrFalse(IsASCIICapable())));
|
|
|
|
|
|
|
|
switch (aNativeKeyCode) {
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Space: return NS_VK_SPACE;
|
|
|
|
case kVK_Escape: return NS_VK_ESCAPE;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// modifiers
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightCommand:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Command: return NS_VK_META;
|
|
|
|
case kVK_RightShift:
|
|
|
|
case kVK_Shift: return NS_VK_SHIFT;
|
|
|
|
case kVK_CapsLock: return NS_VK_CAPS_LOCK;
|
|
|
|
case kVK_RightControl:
|
|
|
|
case kVK_Control: return NS_VK_CONTROL;
|
|
|
|
case kVK_RightOption:
|
|
|
|
case kVK_Option: return NS_VK_ALT;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_ANSI_KeypadClear: return NS_VK_CLEAR;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// function keys
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_F1: return NS_VK_F1;
|
|
|
|
case kVK_F2: return NS_VK_F2;
|
|
|
|
case kVK_F3: return NS_VK_F3;
|
|
|
|
case kVK_F4: return NS_VK_F4;
|
|
|
|
case kVK_F5: return NS_VK_F5;
|
|
|
|
case kVK_F6: return NS_VK_F6;
|
|
|
|
case kVK_F7: return NS_VK_F7;
|
|
|
|
case kVK_F8: return NS_VK_F8;
|
|
|
|
case kVK_F9: return NS_VK_F9;
|
|
|
|
case kVK_F10: return NS_VK_F10;
|
|
|
|
case kVK_F11: return NS_VK_F11;
|
|
|
|
case kVK_F12: return NS_VK_F12;
|
|
|
|
// case kVK_F13: return NS_VK_F13; // clash with the 3 below
|
|
|
|
// case kVK_F14: return NS_VK_F14;
|
|
|
|
// case kVK_F15: return NS_VK_F15;
|
|
|
|
case kVK_F16: return NS_VK_F16;
|
|
|
|
case kVK_F17: return NS_VK_F17;
|
|
|
|
case kVK_F18: return NS_VK_F18;
|
|
|
|
case kVK_F19: return NS_VK_F19;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Pause: return NS_VK_PAUSE;
|
|
|
|
case kVK_PC_ScrollLock: return NS_VK_SCROLL_LOCK;
|
|
|
|
case kVK_PC_PrintScreen: return NS_VK_PRINTSCREEN;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// keypad
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_ANSI_Keypad0: return NS_VK_NUMPAD0;
|
|
|
|
case kVK_ANSI_Keypad1: return NS_VK_NUMPAD1;
|
|
|
|
case kVK_ANSI_Keypad2: return NS_VK_NUMPAD2;
|
|
|
|
case kVK_ANSI_Keypad3: return NS_VK_NUMPAD3;
|
|
|
|
case kVK_ANSI_Keypad4: return NS_VK_NUMPAD4;
|
|
|
|
case kVK_ANSI_Keypad5: return NS_VK_NUMPAD5;
|
|
|
|
case kVK_ANSI_Keypad6: return NS_VK_NUMPAD6;
|
|
|
|
case kVK_ANSI_Keypad7: return NS_VK_NUMPAD7;
|
|
|
|
case kVK_ANSI_Keypad8: return NS_VK_NUMPAD8;
|
|
|
|
case kVK_ANSI_Keypad9: return NS_VK_NUMPAD9;
|
|
|
|
|
|
|
|
case kVK_ANSI_KeypadMultiply: return NS_VK_MULTIPLY;
|
|
|
|
case kVK_ANSI_KeypadPlus: return NS_VK_ADD;
|
|
|
|
case kVK_ANSI_KeypadMinus: return NS_VK_SUBTRACT;
|
|
|
|
case kVK_ANSI_KeypadDecimal: return NS_VK_DECIMAL;
|
|
|
|
case kVK_ANSI_KeypadDivide: return NS_VK_DIVIDE;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// IME keys
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_JIS_Eisu: return NS_VK_EISU;
|
|
|
|
case kVK_JIS_Kana: return NS_VK_KANA;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// these may clash with forward delete and help
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Insert: return NS_VK_INSERT;
|
|
|
|
case kVK_PC_Delete: return NS_VK_DELETE;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Backspace: return NS_VK_BACK;
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Tab: return NS_VK_TAB;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Home: return NS_VK_HOME;
|
|
|
|
case kVK_End: return NS_VK_END;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PageUp: return NS_VK_PAGE_UP;
|
|
|
|
case kVK_PageDown: return NS_VK_PAGE_DOWN;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_LeftArrow: return NS_VK_LEFT;
|
|
|
|
case kVK_RightArrow: return NS_VK_RIGHT;
|
|
|
|
case kVK_UpArrow: return NS_VK_UP;
|
|
|
|
case kVK_DownArrow: return NS_VK_DOWN;
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
case kVK_ANSI_1: return NS_VK_1;
|
|
|
|
case kVK_ANSI_2: return NS_VK_2;
|
|
|
|
case kVK_ANSI_3: return NS_VK_3;
|
|
|
|
case kVK_ANSI_4: return NS_VK_4;
|
|
|
|
case kVK_ANSI_5: return NS_VK_5;
|
|
|
|
case kVK_ANSI_6: return NS_VK_6;
|
|
|
|
case kVK_ANSI_7: return NS_VK_7;
|
|
|
|
case kVK_ANSI_8: return NS_VK_8;
|
|
|
|
case kVK_ANSI_9: return NS_VK_9;
|
|
|
|
case kVK_ANSI_0: return NS_VK_0;
|
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_ANSI_KeypadEnter:
|
|
|
|
case kVK_Return:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Powerbook_KeypadEnter: return NS_VK_RETURN;
|
2012-05-17 00:04:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If Cmd key is pressed, that causes switching keyboard layout temporarily.
|
|
|
|
// E.g., Dvorak-QWERTY. Therefore, if Cmd key is pressed, we should honor it.
|
|
|
|
UInt32 modifiers = aCmdIsPressed ? cmdKey : 0;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t charCode = TranslateToChar(aNativeKeyCode, modifiers, aKbType);
|
2012-05-17 00:04:16 -07:00
|
|
|
|
|
|
|
// Special case for Mac. Mac inputs Yen sign (U+00A5) directly instead of
|
|
|
|
// Back slash (U+005C). We should return NS_VK_BACK_SLASH for compatibility
|
|
|
|
// with other platforms.
|
|
|
|
// XXX How about Won sign (U+20A9) which has same problem as Yen sign?
|
|
|
|
if (charCode == 0x00A5) {
|
|
|
|
return NS_VK_BACK_SLASH;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t keyCode = WidgetUtils::ComputeKeyCodeFromChar(charCode);
|
2012-05-17 00:04:16 -07:00
|
|
|
if (keyCode) {
|
|
|
|
return keyCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the unshifed char isn't an ASCII character, use shifted char.
|
|
|
|
charCode = TranslateToChar(aNativeKeyCode, modifiers | shiftKey, aKbType);
|
|
|
|
keyCode = WidgetUtils::ComputeKeyCodeFromChar(charCode);
|
|
|
|
if (keyCode) {
|
|
|
|
return keyCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is ASCII capable, give up to compute it.
|
|
|
|
if (IsASCIICapable()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry with ASCII capable keyboard layout.
|
|
|
|
TISInputSourceWrapper currentKeyboardLayout;
|
|
|
|
currentKeyboardLayout.InitByCurrentASCIICapableKeyboardLayout();
|
|
|
|
NS_ENSURE_TRUE(mInputSource != currentKeyboardLayout.mInputSource, 0);
|
|
|
|
keyCode = currentKeyboardLayout.ComputeGeckoKeyCode(aNativeKeyCode, aKbType,
|
|
|
|
aCmdIsPressed);
|
|
|
|
|
|
|
|
// However, if keyCode isn't for an alphabet keys or a numeric key, we should
|
|
|
|
// ignore it. For example, comma key of Thai layout is same as close-square-
|
|
|
|
// bracket key of US layout and an unicode character key of Thai layout is
|
|
|
|
// same as comma key of US layout. If we return NS_VK_COMMA for latter key,
|
|
|
|
// web application developers cannot distinguish with the former key.
|
|
|
|
return ((keyCode >= NS_VK_A && keyCode <= NS_VK_Z) ||
|
|
|
|
(keyCode >= NS_VK_0 && keyCode <= NS_VK_9)) ? keyCode : 0;
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:49:47 -07:00
|
|
|
// static
|
|
|
|
KeyNameIndex
|
|
|
|
TISInputSourceWrapper::ComputeGeckoKeyNameIndex(UInt32 aNativeKeyCode)
|
|
|
|
{
|
|
|
|
switch (aNativeKeyCode) {
|
|
|
|
|
|
|
|
#define NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX(aNativeKey, aKeyNameIndex) \
|
|
|
|
case aNativeKey: return aKeyNameIndex;
|
|
|
|
|
|
|
|
#include "NativeKeyToDOMKeyName.h"
|
|
|
|
|
|
|
|
#undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX
|
|
|
|
|
|
|
|
default:
|
|
|
|
return KEY_NAME_INDEX_Unidentified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* TextInputHandler implementation (static methods)
|
2009-09-30 19:52:50 -07:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2011-08-01 18:20:52 -07:00
|
|
|
NSUInteger TextInputHandler::sLastModifierState = 0;
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
// static
|
|
|
|
CFArrayRef
|
2011-05-08 03:19:15 -07:00
|
|
|
TextInputHandler::CreateAllKeyboardLayoutList()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
const void* keys[] = { kTISPropertyInputSourceType };
|
|
|
|
const void* values[] = { kTISTypeKeyboardLayout };
|
|
|
|
CFDictionaryRef filter =
|
|
|
|
::CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);
|
|
|
|
NS_ASSERTION(filter, "failed to create the filter");
|
|
|
|
CFArrayRef list = ::TISCreateInputSourceList(filter, true);
|
|
|
|
::CFRelease(filter);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-07-20 17:33:16 -07:00
|
|
|
TextInputHandler::DebugPrintAllKeyboardLayouts()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(gLog, PR_LOG_ALWAYS)) {
|
|
|
|
CFArrayRef list = CreateAllKeyboardLayoutList();
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS, ("Keyboard layout configuration:"));
|
|
|
|
CFIndex idx = ::CFArrayGetCount(list);
|
|
|
|
TISInputSourceWrapper tis;
|
|
|
|
for (CFIndex i = 0; i < idx; ++i) {
|
|
|
|
TISInputSourceRef inputSource = static_cast<TISInputSourceRef>(
|
|
|
|
const_cast<void *>(::CFArrayGetValueAtIndex(list, i)));
|
|
|
|
tis.InitByTISInputSourceRef(inputSource);
|
|
|
|
nsAutoString name, isid;
|
|
|
|
tis.GetLocalizedName(name);
|
|
|
|
tis.GetInputSourceID(isid);
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
(" %s\t<%s>%s%s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(name).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(isid).get(),
|
|
|
|
tis.IsASCIICapable() ? "" : "\t(Isn't ASCII capable)",
|
2012-10-15 18:43:50 -07:00
|
|
|
tis.IsKeyboardLayout() && tis.GetUCKeyboardLayout() ?
|
|
|
|
"" : "\t(uchr is NOT AVAILABLE)"));
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
|
|
|
::CFRelease(list);
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
2011-07-20 17:33:16 -07:00
|
|
|
#endif // #ifdef PR_LOGGING
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* TextInputHandler implementation
|
2009-09-30 19:52:50 -07:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
TextInputHandler::TextInputHandler(nsChildView* aWidget,
|
|
|
|
NSView<mozView> *aNativeView) :
|
|
|
|
IMEInputHandler(aWidget, aNativeView)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
InitLogModule();
|
2011-05-08 03:19:23 -07:00
|
|
|
[mView installTextInputHandler:this];
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:15 -07:00
|
|
|
TextInputHandler::~TextInputHandler()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-05-08 03:19:23 -07:00
|
|
|
[mView uninstallTextInputHandler];
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-02-23 10:06:26 -08:00
|
|
|
TextInputHandler::HandleKeyDownEvent(NSEvent* aNativeEvent)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, "
|
|
|
|
"widget has been already destroyed", this));
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, aNativeEvent=%p, "
|
|
|
|
"type=%s, keyCode=%lld (0x%X), modifierFlags=0x%X, characters=\"%s\", "
|
|
|
|
"charactersIgnoringModifiers=\"%s\"",
|
|
|
|
this, aNativeEvent, GetNativeKeyEventType(aNativeEvent),
|
|
|
|
[aNativeEvent keyCode], [aNativeEvent keyCode],
|
|
|
|
[aNativeEvent modifierFlags], GetCharacters([aNativeEvent characters]),
|
|
|
|
GetCharacters([aNativeEvent charactersIgnoringModifiers])));
|
|
|
|
|
2011-02-23 10:06:26 -08:00
|
|
|
nsRefPtr<nsChildView> kungFuDeathGrip(mWidget);
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
KeyEventState* currentKeyEvent = PushKeyEvent(aNativeEvent);
|
2011-02-23 10:06:26 -08:00
|
|
|
AutoKeyEventStateCleaner remover(this);
|
|
|
|
|
2013-04-23 20:49:47 -07:00
|
|
|
if (!IsIMEComposing()) {
|
2011-02-23 10:06:26 -08:00
|
|
|
NSResponder* firstResponder = [[mView window] firstResponder];
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keydownEvent(true, NS_KEY_DOWN, mWidget);
|
2011-02-23 10:06:26 -08:00
|
|
|
InitKeyEvent(aNativeEvent, keydownEvent);
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
currentKeyEvent->mKeyDownHandled = DispatchEvent(keydownEvent);
|
2011-02-23 10:06:26 -08:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, "
|
|
|
|
"widget was destroyed by keydown event", this));
|
2011-10-04 19:19:24 -07:00
|
|
|
return currentKeyEvent->KeyDownOrPressHandled();
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The key down event may have shifted the focus, in which
|
|
|
|
// case we should not fire the key press.
|
|
|
|
// XXX This is a special code only on Cocoa widget, why is this needed?
|
|
|
|
if (firstResponder != [[mView window] firstResponder]) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, "
|
|
|
|
"view lost focus by keydown event", this));
|
2011-10-04 19:19:24 -07:00
|
|
|
return currentKeyEvent->KeyDownOrPressHandled();
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the context menu key command, send a context menu key event.
|
|
|
|
NSUInteger modifierFlags =
|
|
|
|
[aNativeEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
|
|
|
if (modifierFlags == NSControlKeyMask &&
|
|
|
|
[[aNativeEvent charactersIgnoringModifiers] isEqualToString:@" "]) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsMouseEvent contextMenuEvent(true, NS_CONTEXTMENU,
|
2011-02-23 10:06:26 -08:00
|
|
|
[mView widget], nsMouseEvent::eReal,
|
|
|
|
nsMouseEvent::eContextMenuKey);
|
2012-04-24 20:00:02 -07:00
|
|
|
contextMenuEvent.modifiers = 0;
|
2011-02-23 10:06:26 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool cmEventHandled = DispatchEvent(contextMenuEvent);
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, "
|
|
|
|
"context menu event dispatched, handled=%s%s",
|
|
|
|
this, TrueOrFalse(cmEventHandled),
|
|
|
|
Destroyed() ? " and widget was destroyed" : ""));
|
2011-02-23 10:06:26 -08:00
|
|
|
[mView maybeInitContextMenuTracking];
|
|
|
|
// Bail, there is nothing else to do here.
|
2011-10-04 19:19:24 -07:00
|
|
|
return (cmEventHandled || currentKeyEvent->KeyDownOrPressHandled());
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let Cocoa interpret the key events, caching IsIMEComposing first.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool wasComposing = IsIMEComposing();
|
|
|
|
bool interpretKeyEventsCalled = false;
|
2011-02-23 10:06:26 -08:00
|
|
|
if (IsIMEEnabled() || IsASCIICapableOnly()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, calling interpretKeyEvents",
|
|
|
|
this));
|
2011-02-23 10:06:26 -08:00
|
|
|
[mView interpretKeyEvents:[NSArray arrayWithObject:aNativeEvent]];
|
2011-09-30 17:20:33 -07:00
|
|
|
interpretKeyEventsCalled = true;
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, called interpretKeyEvents",
|
|
|
|
this));
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, widget was destroyed",
|
|
|
|
this));
|
2011-10-04 19:19:24 -07:00
|
|
|
return currentKeyEvent->KeyDownOrPressHandled();
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, wasComposing=%s, "
|
|
|
|
"IsIMEComposing()=%s",
|
|
|
|
this, TrueOrFalse(wasComposing), TrueOrFalse(IsIMEComposing())));
|
|
|
|
|
2013-04-23 20:49:47 -07:00
|
|
|
if (!currentKeyEvent->mKeyPressDispatched &&
|
2011-02-23 10:06:26 -08:00
|
|
|
!wasComposing && !IsIMEComposing()) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keypressEvent(true, NS_KEY_PRESS, mWidget);
|
2011-02-23 10:06:26 -08:00
|
|
|
InitKeyEvent(aNativeEvent, keypressEvent);
|
|
|
|
|
|
|
|
// If we called interpretKeyEvents and this isn't normal character input
|
|
|
|
// then IME probably ate the event for some reason. We do not want to
|
|
|
|
// send a key press event in that case.
|
|
|
|
// TODO:
|
|
|
|
// There are some other cases which IME eats the current event.
|
|
|
|
// 1. If key events were nested during calling interpretKeyEvents, it means
|
|
|
|
// that IME did something. Then, we should do nothing.
|
|
|
|
// 2. If one or more commands are called like "deleteBackward", we should
|
|
|
|
// dispatch keypress event at that time. Note that the command may have
|
|
|
|
// been a converted or generated action by IME. Then, we shouldn't do
|
|
|
|
// our default action for this key.
|
|
|
|
if (!(interpretKeyEventsCalled &&
|
|
|
|
IsNormalCharInputtingEvent(keypressEvent))) {
|
2011-10-04 19:19:24 -07:00
|
|
|
if (currentKeyEvent->mKeyDownHandled ||
|
|
|
|
currentKeyEvent->mCausedOtherKeyEvents) {
|
2012-12-15 17:26:04 -08:00
|
|
|
keypressEvent.mFlags.mDefaultPrevented = true;
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
2011-10-04 19:19:24 -07:00
|
|
|
currentKeyEvent->mKeyPressHandled = DispatchEvent(keypressEvent);
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, keypress event dispatched",
|
|
|
|
this));
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: mWidget might have become null here. Don't count on it from here on.
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyDownEvent, "
|
|
|
|
"keydown handled=%s, keypress handled=%s",
|
2011-10-04 19:19:24 -07:00
|
|
|
this, TrueOrFalse(currentKeyEvent->mKeyDownHandled),
|
|
|
|
TrueOrFalse(currentKeyEvent->mKeyPressHandled)));
|
|
|
|
return currentKeyEvent->KeyDownOrPressHandled();
|
2011-02-23 10:06:26 -08:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
void
|
|
|
|
TextInputHandler::HandleKeyUpEvent(NSEvent* aNativeEvent)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyUpEvent, aNativeEvent=%p, "
|
|
|
|
"type=%s, keyCode=%lld (0x%X), modifierFlags=0x%X, characters=\"%s\", "
|
|
|
|
"charactersIgnoringModifiers=\"%s\", "
|
|
|
|
"mIgnoreNextKeyUpEvent=%s, IsIMEComposing()=%s",
|
|
|
|
this, aNativeEvent, GetNativeKeyEventType(aNativeEvent),
|
|
|
|
[aNativeEvent keyCode], [aNativeEvent keyCode],
|
|
|
|
[aNativeEvent modifierFlags], GetCharacters([aNativeEvent characters]),
|
|
|
|
GetCharacters([aNativeEvent charactersIgnoringModifiers]),
|
|
|
|
TrueOrFalse(mIgnoreNextKeyUpEvent), TrueOrFalse(IsIMEComposing())));
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
if (mIgnoreNextKeyUpEvent) {
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = false;
|
2011-07-20 17:33:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleKeyUpEvent, "
|
|
|
|
"widget has been already destroyed", this));
|
2011-07-20 17:33:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have any characters we can't generate a keyUp event
|
2013-04-23 20:49:47 -07:00
|
|
|
if (IsIMEComposing()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keyupEvent(true, NS_KEY_UP, mWidget);
|
2011-07-20 17:33:16 -07:00
|
|
|
InitKeyEvent(aNativeEvent, keyupEvent);
|
|
|
|
|
|
|
|
DispatchEvent(keyupEvent);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextInputHandler::HandleFlagsChanged(NSEvent* aNativeEvent)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleFlagsChanged, "
|
|
|
|
"widget has been already destroyed", this));
|
2011-07-20 17:33:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsChildView> kungFuDeathGrip(mWidget);
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::HandleFlagsChanged, aNativeEvent=%p, "
|
2012-10-18 18:17:07 -07:00
|
|
|
"type=%s, keyCode=%s (0x%X), modifierFlags=0x%08X, "
|
|
|
|
"sLastModifierState=0x%08X, IsIMEComposing()=%s",
|
2011-07-20 17:33:16 -07:00
|
|
|
this, aNativeEvent, GetNativeKeyEventType(aNativeEvent),
|
|
|
|
GetKeyNameForNativeKeyCode([aNativeEvent keyCode]), [aNativeEvent keyCode],
|
2012-10-18 18:17:07 -07:00
|
|
|
[aNativeEvent modifierFlags], sLastModifierState,
|
|
|
|
TrueOrFalse(IsIMEComposing())));
|
|
|
|
|
|
|
|
MOZ_ASSERT([aNativeEvent type] == NSFlagsChanged);
|
|
|
|
|
|
|
|
NSUInteger diff = [aNativeEvent modifierFlags] ^ sLastModifierState;
|
|
|
|
// Device dependent flags for left-control key, both shift keys, both command
|
|
|
|
// keys and both option keys have been defined in Next's SDK. But we
|
|
|
|
// shouldn't use it directly as far as possible since Cocoa SDK doesn't
|
|
|
|
// define them. Fortunately, we need them only when we dispatch keyup
|
|
|
|
// events. So, we can usually know the actual relation between keyCode and
|
|
|
|
// device dependent flags. However, we need to remove following flags first
|
|
|
|
// since the differences don't indicate modifier key state.
|
|
|
|
// NX_STYLUSPROXIMITYMASK: Probably used for pen like device.
|
|
|
|
// kCGEventFlagMaskNonCoalesced (= NX_NONCOALSESCEDMASK): See the document for
|
|
|
|
// Quartz Event Services.
|
|
|
|
diff &= ~(NX_STYLUSPROXIMITYMASK | kCGEventFlagMaskNonCoalesced);
|
|
|
|
|
|
|
|
switch ([aNativeEvent keyCode]) {
|
|
|
|
// CapsLock state and other modifier states are different:
|
|
|
|
// CapsLock state does not revert when the CapsLock key goes up, as the
|
|
|
|
// modifier state does for other modifier keys on key up.
|
|
|
|
case kVK_CapsLock: {
|
|
|
|
// Fire key down event for caps lock.
|
|
|
|
DispatchKeyEventForFlagsChanged(aNativeEvent, true);
|
|
|
|
// XXX should we fire keyup event too? The keyup event for CapsLock key
|
|
|
|
// is never dispatched on Gecko.
|
|
|
|
// XXX WebKit dispatches keydown event when CapsLock is locked, otherwise,
|
|
|
|
// keyup event. If we do so, we cannot keep the consistency with other
|
|
|
|
// platform's behavior...
|
|
|
|
break;
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
2012-10-18 18:17:07 -07:00
|
|
|
|
|
|
|
// If the event is caused by pressing or releasing a modifier key, just
|
|
|
|
// dispatch the key's event.
|
|
|
|
case kVK_Shift:
|
|
|
|
case kVK_RightShift:
|
|
|
|
case kVK_Command:
|
|
|
|
case kVK_RightCommand:
|
|
|
|
case kVK_Control:
|
|
|
|
case kVK_RightControl:
|
|
|
|
case kVK_Option:
|
|
|
|
case kVK_RightOption:
|
|
|
|
case kVK_Help: {
|
|
|
|
// We assume that at most one modifier is changed per event if the event
|
|
|
|
// is caused by pressing or releasing a modifier key.
|
|
|
|
bool isKeyDown = ([aNativeEvent modifierFlags] & diff) != 0;
|
|
|
|
DispatchKeyEventForFlagsChanged(aNativeEvent, isKeyDown);
|
2012-12-21 00:31:24 -08:00
|
|
|
// XXX Some applications might send the event with incorrect device-
|
|
|
|
// dependent flags.
|
2012-10-18 18:17:07 -07:00
|
|
|
if (isKeyDown && ((diff & ~NSDeviceIndependentModifierFlagsMask) != 0)) {
|
|
|
|
unsigned short keyCode = [aNativeEvent keyCode];
|
|
|
|
ModifierKey* modifierKey = GetModifierKeyForDeviceDependentFlags(diff);
|
|
|
|
if (modifierKey) {
|
|
|
|
MOZ_ASSERT(modifierKey->keyCode == keyCode);
|
|
|
|
} else {
|
|
|
|
mModifierKeys.AppendElement(ModifierKey(diff, keyCode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently we don't support Fn key since other browsers don't dispatch
|
|
|
|
// events for it and we don't have keyCode for this key.
|
|
|
|
// It should be supported when we implement .key and .char.
|
|
|
|
case kVK_Function:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If the event is caused by something else than pressing or releasing a
|
|
|
|
// single modifier key (for example by the app having been deactivated
|
|
|
|
// using command-tab), use the modifiers themselves to determine which
|
|
|
|
// key's event to dispatch, and whether it's a keyup or keydown event.
|
|
|
|
// In all cases we assume one or more modifiers are being deactivated
|
|
|
|
// (never activated) -- otherwise we'd have received one or more events
|
|
|
|
// corresponding to a single modifier key being pressed.
|
|
|
|
default: {
|
|
|
|
NSUInteger modifiers = sLastModifierState;
|
|
|
|
for (int32_t bit = 0; bit < 32; ++bit) {
|
|
|
|
NSUInteger flag = 1 << bit;
|
|
|
|
if (!(diff & flag)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-21 00:31:24 -08:00
|
|
|
// Given correct information from the application, a flag change here
|
|
|
|
// will normally be a deactivation (except for some lockable modifiers
|
|
|
|
// such as CapsLock). But some applications (like VNC) can send an
|
|
|
|
// activating event with a zero keyCode. So we need to check for that
|
|
|
|
// here.
|
|
|
|
bool dispatchKeyDown = ((flag & [aNativeEvent modifierFlags]) != 0);
|
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
unsigned short keyCode = 0;
|
|
|
|
if (flag & NSDeviceIndependentModifierFlagsMask) {
|
|
|
|
switch (flag) {
|
|
|
|
case NSAlphaShiftKeyMask:
|
|
|
|
keyCode = kVK_CapsLock;
|
|
|
|
dispatchKeyDown = true;
|
|
|
|
break;
|
2012-12-21 00:31:24 -08:00
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
case NSNumericPadKeyMask:
|
2012-12-21 00:31:24 -08:00
|
|
|
// NSNumericPadKeyMask is fired by VNC a lot. But not all of
|
|
|
|
// these events can really be Clear key events, so we just ignore
|
|
|
|
// them.
|
|
|
|
continue;
|
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
case NSHelpKeyMask:
|
|
|
|
keyCode = kVK_Help;
|
|
|
|
break;
|
2012-12-21 00:31:24 -08:00
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
case NSFunctionKeyMask:
|
2012-12-21 00:31:24 -08:00
|
|
|
// An NSFunctionKeyMask change here will normally be a
|
|
|
|
// deactivation. But sometimes it will be an activation send (by
|
|
|
|
// VNC for example) with a zero keyCode.
|
2012-10-18 18:17:07 -07:00
|
|
|
continue;
|
2012-12-21 00:31:24 -08:00
|
|
|
|
|
|
|
// These cases (NSShiftKeyMask, NSControlKeyMask, NSAlternateKeyMask
|
|
|
|
// and NSCommandKeyMask) should be handled by the other branch of
|
|
|
|
// the if statement, below (which handles device dependent flags).
|
|
|
|
// However, some applications (like VNC) can send key events without
|
|
|
|
// any device dependent flags, so we handle them here instead.
|
|
|
|
case NSShiftKeyMask:
|
|
|
|
keyCode = (modifiers & 0x0004) ? kVK_RightShift : kVK_Shift;
|
|
|
|
break;
|
|
|
|
case NSControlKeyMask:
|
|
|
|
keyCode = (modifiers & 0x2000) ? kVK_RightControl : kVK_Control;
|
|
|
|
break;
|
|
|
|
case NSAlternateKeyMask:
|
|
|
|
keyCode = (modifiers & 0x0040) ? kVK_RightOption : kVK_Option;
|
|
|
|
break;
|
|
|
|
case NSCommandKeyMask:
|
|
|
|
keyCode = (modifiers & 0x0010) ? kVK_RightCommand : kVK_Command;
|
|
|
|
break;
|
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForDeviceDependentFlags(flag);
|
|
|
|
if (!modifierKey) {
|
2012-12-21 00:31:24 -08:00
|
|
|
// See the note above (in the other branch of the if statement)
|
|
|
|
// about the NSShiftKeyMask, NSControlKeyMask, NSAlternateKeyMask
|
|
|
|
// and NSCommandKeyMask cases.
|
2012-10-18 18:17:07 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyCode = modifierKey->keyCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove flags
|
|
|
|
modifiers &= ~flag;
|
|
|
|
switch (keyCode) {
|
|
|
|
case kVK_Shift: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_RightShift);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSShiftKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_RightShift: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_Shift);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSShiftKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_Command: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_RightCommand);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSCommandKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_RightCommand: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_Command);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSCommandKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_Control: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_RightControl);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSControlKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_RightControl: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_Control);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSControlKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_Option: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_RightOption);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSAlternateKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_RightOption: {
|
|
|
|
ModifierKey* modifierKey =
|
|
|
|
GetModifierKeyForNativeKeyCode(kVK_Option);
|
|
|
|
if (!modifierKey ||
|
|
|
|
!(modifiers & modifierKey->GetDeviceDependentFlags())) {
|
|
|
|
modifiers &= ~NSAlternateKeyMask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVK_Help:
|
|
|
|
modifiers &= ~NSHelpKeyMask;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSEvent* event =
|
|
|
|
[NSEvent keyEventWithType:NSFlagsChanged
|
|
|
|
location:[aNativeEvent locationInWindow]
|
|
|
|
modifierFlags:modifiers
|
|
|
|
timestamp:[aNativeEvent timestamp]
|
|
|
|
windowNumber:[aNativeEvent windowNumber]
|
|
|
|
context:[aNativeEvent context]
|
|
|
|
characters:nil
|
|
|
|
charactersIgnoringModifiers:nil
|
|
|
|
isARepeat:NO
|
|
|
|
keyCode:keyCode];
|
|
|
|
DispatchKeyEventForFlagsChanged(event, dispatchKeyDown);
|
2011-07-20 17:33:16 -07:00
|
|
|
if (Destroyed()) {
|
2012-10-18 18:17:07 -07:00
|
|
|
break;
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop if focus has changed.
|
|
|
|
// Check to see if mView is still the first responder.
|
|
|
|
if (![mView isFirstResponder]) {
|
|
|
|
break;
|
|
|
|
}
|
2012-10-18 18:17:07 -07:00
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
2012-10-18 18:17:07 -07:00
|
|
|
break;
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
// Be aware, the widget may have been destroyed.
|
|
|
|
sLastModifierState = [aNativeEvent modifierFlags];
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2012-10-18 18:17:07 -07:00
|
|
|
TextInputHandler::ModifierKey*
|
|
|
|
TextInputHandler::GetModifierKeyForNativeKeyCode(unsigned short aKeyCode) const
|
|
|
|
{
|
|
|
|
for (ModifierKeyArray::index_type i = 0; i < mModifierKeys.Length(); ++i) {
|
|
|
|
if (mModifierKeys[i].keyCode == aKeyCode) {
|
|
|
|
return &((ModifierKey&)mModifierKeys[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextInputHandler::ModifierKey*
|
|
|
|
TextInputHandler::GetModifierKeyForDeviceDependentFlags(NSUInteger aFlags) const
|
|
|
|
{
|
|
|
|
for (ModifierKeyArray::index_type i = 0; i < mModifierKeys.Length(); ++i) {
|
|
|
|
if (mModifierKeys[i].GetDeviceDependentFlags() ==
|
|
|
|
(aFlags & ~NSDeviceIndependentModifierFlagsMask)) {
|
|
|
|
return &((ModifierKey&)mModifierKeys[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
void
|
|
|
|
TextInputHandler::DispatchKeyEventForFlagsChanged(NSEvent* aNativeEvent,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDispatchKeyDown)
|
2011-07-20 17:33:16 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::DispatchKeyEventForFlagsChanged, aNativeEvent=%p, "
|
|
|
|
"type=%s, keyCode=%s (0x%X), aDispatchKeyDown=%s, IsIMEComposing()=%s",
|
|
|
|
this, aNativeEvent, GetNativeKeyEventType(aNativeEvent),
|
|
|
|
GetKeyNameForNativeKeyCode([aNativeEvent keyCode]), [aNativeEvent keyCode],
|
|
|
|
TrueOrFalse(aDispatchKeyDown), TrueOrFalse(IsIMEComposing())));
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
if ([aNativeEvent type] != NSFlagsChanged || IsIMEComposing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t message = aDispatchKeyDown ? NS_KEY_DOWN : NS_KEY_UP;
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
NPCocoaEvent cocoaEvent;
|
|
|
|
|
|
|
|
// Fire a key event.
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keyEvent(true, message, mWidget);
|
2011-07-20 17:33:16 -07:00
|
|
|
InitKeyEvent(aNativeEvent, keyEvent);
|
|
|
|
|
|
|
|
// create event for use by plugins
|
|
|
|
if ([mView isPluginView]) {
|
|
|
|
if ([mView pluginEventModel] == NPEventModelCocoa) {
|
|
|
|
ConvertCocoaKeyEventToNPCocoaEvent(aNativeEvent, cocoaEvent);
|
|
|
|
keyEvent.pluginEvent = &cocoaEvent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchEvent(keyEvent);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-02-23 10:06:26 -08:00
|
|
|
void
|
|
|
|
TextInputHandler::InsertText(NSAttributedString *aAttrString)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
KeyEventState* currentKeyEvent = GetCurrentKeyEvent();
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::InsertText, aAttrString=\"%s\", "
|
|
|
|
"IsIMEComposing()=%s, IgnoreIMEComposition()=%s, "
|
|
|
|
"keyevent=%p, keypressDispatched=%s",
|
|
|
|
this, GetCharacters([aAttrString string]), TrueOrFalse(IsIMEComposing()),
|
2011-10-04 19:19:24 -07:00
|
|
|
TrueOrFalse(IgnoreIMEComposition()),
|
2012-07-30 07:20:58 -07:00
|
|
|
currentKeyEvent ? currentKeyEvent->mKeyEvent : nullptr,
|
2011-10-04 19:19:24 -07:00
|
|
|
currentKeyEvent ?
|
|
|
|
TrueOrFalse(currentKeyEvent->mKeyPressDispatched) : "N/A"));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-02-23 10:06:26 -08:00
|
|
|
if (IgnoreIMEComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-20 16:24:48 -07:00
|
|
|
nsAutoString str;
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::GetStringForNSString([aAttrString string], str);
|
2011-02-23 10:06:26 -08:00
|
|
|
if (!IsIMEComposing() && str.IsEmpty()) {
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str.Length() != 1 || IsIMEComposing()) {
|
|
|
|
InsertTextAsCommittingComposition(aAttrString);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't let the same event be fired twice when hitting
|
|
|
|
// enter/return! (Bug 420502)
|
2011-10-04 19:19:24 -07:00
|
|
|
if (currentKeyEvent && currentKeyEvent->mKeyPressDispatched) {
|
2011-02-23 10:06:26 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsChildView> kungFuDeathGrip(mWidget);
|
|
|
|
|
|
|
|
// Dispatch keypress event with char instead of textEvent
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keypressEvent(true, NS_KEY_PRESS, mWidget);
|
2012-07-20 16:24:48 -07:00
|
|
|
keypressEvent.isChar = IsPrintableChar(str.CharAt(0));
|
2011-02-23 10:06:26 -08:00
|
|
|
|
|
|
|
// Don't set other modifiers from the current event, because here in
|
|
|
|
// -insertText: they've already been taken into account in creating
|
|
|
|
// the input string.
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
if (currentKeyEvent) {
|
|
|
|
NSEvent* keyEvent = currentKeyEvent->mKeyEvent;
|
2012-07-20 16:24:48 -07:00
|
|
|
InitKeyEvent(keyEvent, keypressEvent, &str);
|
2011-10-04 19:19:24 -07:00
|
|
|
if (currentKeyEvent->mKeyDownHandled) {
|
2012-12-15 17:26:04 -08:00
|
|
|
keypressEvent.mFlags.mDefaultPrevented = true;
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
} else {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsCocoaUtils::InitInputEvent(keypressEvent, static_cast<NSEvent*>(nullptr));
|
2013-01-10 19:30:12 -08:00
|
|
|
if (keypressEvent.isChar) {
|
|
|
|
keypressEvent.charCode = str.CharAt(0);
|
|
|
|
}
|
2011-02-23 10:06:26 -08:00
|
|
|
// Note that insertText is not called only at key pressing.
|
2012-05-25 19:35:58 -07:00
|
|
|
if (!keypressEvent.charCode) {
|
2011-02-23 10:06:26 -08:00
|
|
|
keypressEvent.keyCode =
|
2012-05-17 00:04:16 -07:00
|
|
|
WidgetUtils::ComputeKeyCodeFromChar(keypressEvent.charCode);
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
// Remove basic modifiers from keypress event because if they are included,
|
|
|
|
// nsPlaintextEditor ignores the event.
|
|
|
|
keypressEvent.modifiers &= ~(widget::MODIFIER_CONTROL |
|
|
|
|
widget::MODIFIER_ALT |
|
|
|
|
widget::MODIFIER_META);
|
|
|
|
|
2011-02-23 10:06:26 -08:00
|
|
|
// TODO:
|
|
|
|
// If mCurrentKeyEvent.mKeyEvent is null and when we implement textInput
|
|
|
|
// event of DOM3 Events, we should dispatch it instead of keypress event.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool keyPressHandled = DispatchEvent(keypressEvent);
|
2011-02-23 10:06:26 -08:00
|
|
|
|
|
|
|
// Note: mWidget might have become null here. Don't count on it from here on.
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
if (currentKeyEvent) {
|
|
|
|
currentKeyEvent->mKeyPressHandled = keyPressHandled;
|
|
|
|
currentKeyEvent->mKeyPressDispatched = true;
|
2011-02-23 10:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-07-20 17:33:16 -07:00
|
|
|
TextInputHandler::DoCommandBySelector(const char* aSelector)
|
|
|
|
{
|
2011-10-04 19:19:25 -07:00
|
|
|
nsRefPtr<nsChildView> kungFuDeathGrip(mWidget);
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
KeyEventState* currentKeyEvent = GetCurrentKeyEvent();
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::DoCommandBySelector, aSelector=\"%s\", "
|
2011-10-04 19:19:24 -07:00
|
|
|
"Destroyed()=%s, keypressHandled=%s, causedOtherKeyEvents=%s",
|
2011-07-20 17:33:16 -07:00
|
|
|
this, aSelector ? aSelector : "", TrueOrFalse(Destroyed()),
|
2011-10-04 19:19:24 -07:00
|
|
|
currentKeyEvent ?
|
2011-10-04 19:19:24 -07:00
|
|
|
TrueOrFalse(currentKeyEvent->mKeyPressHandled) : "N/A",
|
|
|
|
currentKeyEvent ?
|
|
|
|
TrueOrFalse(currentKeyEvent->mCausedOtherKeyEvents) : "N/A"));
|
2011-07-20 17:33:16 -07:00
|
|
|
|
2011-10-04 19:19:25 -07:00
|
|
|
if (currentKeyEvent && !currentKeyEvent->mKeyPressDispatched) {
|
|
|
|
nsKeyEvent keypressEvent(true, NS_KEY_PRESS, mWidget);
|
|
|
|
InitKeyEvent(currentKeyEvent->mKeyEvent, keypressEvent);
|
|
|
|
if (currentKeyEvent->mKeyDownHandled ||
|
|
|
|
currentKeyEvent->mCausedOtherKeyEvents) {
|
2012-12-15 17:26:04 -08:00
|
|
|
keypressEvent.mFlags.mDefaultPrevented = true;
|
2011-10-04 19:19:25 -07:00
|
|
|
}
|
|
|
|
currentKeyEvent->mKeyPressHandled = DispatchEvent(keypressEvent);
|
|
|
|
currentKeyEvent->mKeyPressDispatched = true;
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandler::DoCommandBySelector, keypress event "
|
|
|
|
"dispatched, Destroyed()=%s, keypressHandled=%s",
|
|
|
|
this, TrueOrFalse(Destroyed()),
|
|
|
|
TrueOrFalse(currentKeyEvent->mKeyPressHandled)));
|
|
|
|
}
|
|
|
|
|
2011-10-04 19:19:24 -07:00
|
|
|
return !Destroyed() && currentKeyEvent &&
|
|
|
|
(currentKeyEvent->mKeyPressHandled ||
|
|
|
|
currentKeyEvent->mCausedOtherKeyEvents);
|
2011-07-20 17:33:16 -07:00
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* IMEInputHandler implementation (static methods)
|
2009-09-30 19:52:50 -07:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IMEInputHandler::sStaticMembersInitialized = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
CFStringRef IMEInputHandler::sLatestIMEOpenedModeInputSourceID = nullptr;
|
|
|
|
IMEInputHandler* IMEInputHandler::sFocusedIMEHandler = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::InitStaticMembers()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (sStaticMembersInitialized)
|
|
|
|
return;
|
2011-09-30 17:20:33 -07:00
|
|
|
sStaticMembersInitialized = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
// We need to check the keyboard layout changes on all applications.
|
|
|
|
CFNotificationCenterRef center = ::CFNotificationCenterGetDistributedCenter();
|
|
|
|
// XXX Don't we need to remove the observer at shut down?
|
|
|
|
// Mac Dev Center's document doesn't say how to remove the observer if
|
|
|
|
// the second parameter is NULL.
|
|
|
|
::CFNotificationCenterAddObserver(center, NULL,
|
|
|
|
OnCurrentTextInputSourceChange,
|
|
|
|
kTISNotifySelectedKeyboardInputSourceChanged, NULL,
|
|
|
|
CFNotificationSuspensionBehaviorDeliverImmediately);
|
|
|
|
// Initiailize with the current keyboard layout
|
|
|
|
OnCurrentTextInputSourceChange(NULL, NULL,
|
|
|
|
kTISNotifySelectedKeyboardInputSourceChanged,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::OnCurrentTextInputSourceChange(CFNotificationCenterRef aCenter,
|
|
|
|
void* aObserver,
|
|
|
|
CFStringRef aName,
|
|
|
|
const void* aObject,
|
|
|
|
CFDictionaryRef aUserInfo)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
// Cache the latest IME opened mode to sLatestIMEOpenedModeInputSourceID.
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.InitByCurrentInputSource();
|
|
|
|
if (tis.IsOpenedIMEMode()) {
|
|
|
|
tis.GetInputSourceID(sLatestIMEOpenedModeInputSourceID);
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(gLog, PR_LOG_ALWAYS)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
static CFStringRef sLastTIS = nullptr;
|
2011-07-20 17:33:16 -07:00
|
|
|
CFStringRef newTIS;
|
|
|
|
tis.GetInputSourceID(newTIS);
|
|
|
|
if (!sLastTIS ||
|
|
|
|
::CFStringCompare(sLastTIS, newTIS, 0) != kCFCompareEqualTo) {
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper tis1, tis2, tis3, tis4, tis5;
|
2011-07-20 17:33:16 -07:00
|
|
|
tis1.InitByCurrentKeyboardLayout();
|
|
|
|
tis2.InitByCurrentASCIICapableInputSource();
|
|
|
|
tis3.InitByCurrentASCIICapableKeyboardLayout();
|
2012-10-15 18:43:50 -07:00
|
|
|
tis4.InitByCurrentInputMethodKeyboardLayoutOverride();
|
|
|
|
tis5.InitByTISInputSourceRef(tis.GetKeyboardLayoutInputSource());
|
|
|
|
CFStringRef is0 = nullptr, is1 = nullptr, is2 = nullptr, is3 = nullptr,
|
|
|
|
is4 = nullptr, is5 = nullptr, type0 = nullptr,
|
|
|
|
lang0 = nullptr, bundleID0 = nullptr;
|
2011-07-20 17:33:16 -07:00
|
|
|
tis.GetInputSourceID(is0);
|
|
|
|
tis1.GetInputSourceID(is1);
|
|
|
|
tis2.GetInputSourceID(is2);
|
|
|
|
tis3.GetInputSourceID(is3);
|
2012-10-15 18:43:50 -07:00
|
|
|
tis4.GetInputSourceID(is4);
|
|
|
|
tis5.GetInputSourceID(is5);
|
2011-07-20 17:33:16 -07:00
|
|
|
tis.GetInputSourceType(type0);
|
|
|
|
tis.GetPrimaryLanguage(lang0);
|
|
|
|
tis.GetBundleID(bundleID0);
|
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("IMEInputHandler::OnCurrentTextInputSourceChange,\n"
|
|
|
|
" Current Input Source is changed to:\n"
|
|
|
|
" currentInputManager=%p\n"
|
|
|
|
" %s\n"
|
|
|
|
" type=%s %s\n"
|
2012-10-15 18:43:50 -07:00
|
|
|
" overridden keyboard layout=%s\n"
|
|
|
|
" used keyboard layout for translation=%s\n"
|
2011-07-20 17:33:16 -07:00
|
|
|
" primary language=%s\n"
|
|
|
|
" bundle ID=%s\n"
|
|
|
|
" current ASCII capable Input Source=%s\n"
|
|
|
|
" current Keyboard Layout=%s\n"
|
|
|
|
" current ASCII capable Keyboard Layout=%s",
|
|
|
|
[NSInputManager currentInputManager], GetCharacters(is0),
|
|
|
|
GetCharacters(type0), tis.IsASCIICapable() ? "- ASCII capable " : "",
|
2012-10-15 18:43:50 -07:00
|
|
|
GetCharacters(is4), GetCharacters(is5),
|
2011-07-20 17:33:16 -07:00
|
|
|
GetCharacters(lang0), GetCharacters(bundleID0),
|
|
|
|
GetCharacters(is2), GetCharacters(is1), GetCharacters(is3)));
|
|
|
|
}
|
|
|
|
sLastTIS = newTIS;
|
|
|
|
}
|
|
|
|
#endif // #ifdef PR_LOGGING
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::FlushPendingMethods(nsITimer* aTimer, void* aClosure)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aClosure, "aClosure is null");
|
2011-05-08 03:19:15 -07:00
|
|
|
static_cast<IMEInputHandler*>(aClosure)->ExecutePendingMethods();
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CFArrayRef
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::CreateAllIMEModeList()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
const void* keys[] = { kTISPropertyInputSourceType };
|
|
|
|
const void* values[] = { kTISTypeKeyboardInputMode };
|
|
|
|
CFDictionaryRef filter =
|
|
|
|
::CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);
|
|
|
|
NS_ASSERTION(filter, "failed to create the filter");
|
|
|
|
CFArrayRef list = ::TISCreateInputSourceList(filter, true);
|
|
|
|
::CFRelease(filter);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2011-07-20 17:33:16 -07:00
|
|
|
IMEInputHandler::DebugPrintAllIMEModes()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(gLog, PR_LOG_ALWAYS)) {
|
|
|
|
CFArrayRef list = CreateAllIMEModeList();
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS, ("IME mode configuration:"));
|
|
|
|
CFIndex idx = ::CFArrayGetCount(list);
|
|
|
|
TISInputSourceWrapper tis;
|
|
|
|
for (CFIndex i = 0; i < idx; ++i) {
|
|
|
|
TISInputSourceRef inputSource = static_cast<TISInputSourceRef>(
|
|
|
|
const_cast<void *>(::CFArrayGetValueAtIndex(list, i)));
|
|
|
|
tis.InitByTISInputSourceRef(inputSource);
|
|
|
|
nsAutoString name, isid;
|
|
|
|
tis.GetLocalizedName(name);
|
|
|
|
tis.GetInputSourceID(isid);
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
(" %s\t<%s>%s%s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(name).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(isid).get(),
|
|
|
|
tis.IsASCIICapable() ? "" : "\t(Isn't ASCII capable)",
|
|
|
|
tis.IsEnabled() ? "" : "\t(Isn't Enabled)"));
|
|
|
|
}
|
|
|
|
::CFRelease(list);
|
|
|
|
}
|
|
|
|
#endif // #ifdef PR_LOGGING
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2010-08-16 01:20:27 -07:00
|
|
|
//static
|
|
|
|
TSMDocumentID
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::GetCurrentTSMDocumentID()
|
2010-08-16 01:20:27 -07:00
|
|
|
{
|
|
|
|
// On OS X 10.6.x at least, ::TSMGetActiveDocument() has a bug that prevents
|
|
|
|
// it from returning accurate results unless
|
|
|
|
// [NSInputManager currentInputManager] is called first.
|
|
|
|
// So, we need to call [NSInputManager currentInputManager] first here.
|
|
|
|
[NSInputManager currentInputManager];
|
|
|
|
return ::TSMGetActiveDocument();
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-02-23 10:06:26 -08:00
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* IMEInputHandler implementation #1
|
2009-09-30 19:52:50 -07:00
|
|
|
* The methods are releated to the pending methods. Some jobs should be
|
|
|
|
* run after the stack is finished, e.g, some methods cannot run the jobs
|
|
|
|
* during processing the focus event. And also some other jobs should be
|
|
|
|
* run at the next focus event is processed.
|
|
|
|
* The pending methods are recorded in mPendingMethods. They are executed
|
|
|
|
* by ExecutePendingMethods via FlushPendingMethods.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::ResetIMEWindowLevel()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::ResetIMEWindowLevel, "
|
|
|
|
"Destroyed()=%s, IsFocused()=%s, GetCurrentTSMDocumentID()=%p",
|
|
|
|
this, TrueOrFalse(Destroyed()), TrueOrFalse(IsFocused()),
|
|
|
|
GetCurrentTSMDocumentID()));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
2011-05-08 03:19:23 -07:00
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (!IsFocused()) {
|
|
|
|
// retry at next focus event
|
|
|
|
mPendingMethods |= kResetIMEWindowLevel;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-16 01:20:27 -07:00
|
|
|
TSMDocumentID doc = GetCurrentTSMDocumentID();
|
2009-09-30 19:52:50 -07:00
|
|
|
if (!doc) {
|
|
|
|
// retry
|
|
|
|
mPendingMethods |= kResetIMEWindowLevel;
|
|
|
|
NS_WARNING("Application is active but there is no active document");
|
|
|
|
ResetTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the focus of Gecko is on a text box of a popup panel, the actual
|
|
|
|
// focused view is the panel's parent view (mView). But the editor is
|
|
|
|
// displayed on the popuped widget's view (editorView). So, their window
|
|
|
|
// level may be different.
|
2011-05-08 03:19:23 -07:00
|
|
|
NSView<mozView>* editorView = mWidget->GetEditorView();
|
2009-09-30 19:52:50 -07:00
|
|
|
if (!editorView) {
|
|
|
|
NS_ERROR("editorView is null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to set the focused window level to TSMDocument. Then, the popup
|
|
|
|
// windows of IME (E.g., a candidate list window) will be over the focused
|
|
|
|
// view. See http://developer.apple.com/technotes/tn2005/tn2128.html#TNTAG1
|
|
|
|
NSInteger windowLevel = [[editorView window] level];
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::ResetIMEWindowLevel, windowLevel=%s (%X)",
|
|
|
|
this, GetWindowLevelName(windowLevel), windowLevel));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
// Chinese IMEs on 10.5 don't work fine if the level is NSNormalWindowLevel,
|
|
|
|
// then, we need to increment the value.
|
|
|
|
if (windowLevel == NSNormalWindowLevel)
|
|
|
|
windowLevel++;
|
|
|
|
|
2010-08-16 01:20:27 -07:00
|
|
|
::TSMSetDocumentProperty(GetCurrentTSMDocumentID(),
|
2009-09-30 19:52:50 -07:00
|
|
|
kTSMDocumentWindowLevelPropertyTag,
|
|
|
|
sizeof(windowLevel), &windowLevel);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::DiscardIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::DiscardIMEComposition, "
|
|
|
|
"Destroyed()=%s, IsFocused()=%s, currentInputManager=%p",
|
|
|
|
this, TrueOrFalse(Destroyed()), TrueOrFalse(IsFocused()),
|
|
|
|
[NSInputManager currentInputManager]));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
2011-05-08 03:19:23 -07:00
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (!IsFocused()) {
|
|
|
|
// retry at next focus event
|
|
|
|
mPendingMethods |= kDiscardIMEComposition;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSInputManager* im = [NSInputManager currentInputManager];
|
|
|
|
if (!im) {
|
|
|
|
// retry
|
|
|
|
mPendingMethods |= kDiscardIMEComposition;
|
|
|
|
NS_WARNING("Application is active but there is no currentInputManager");
|
|
|
|
ResetTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreIMECommit = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
[im markedTextAbandoned: mView];
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreIMECommit = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::SyncASCIICapableOnly()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SyncASCIICapableOnly, "
|
|
|
|
"Destroyed()=%s, IsFocused()=%s, mIsASCIICapableOnly=%s, "
|
|
|
|
"GetCurrentTSMDocumentID()=%p",
|
|
|
|
this, TrueOrFalse(Destroyed()), TrueOrFalse(IsFocused()),
|
|
|
|
TrueOrFalse(mIsASCIICapableOnly), GetCurrentTSMDocumentID()));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
2011-05-08 03:19:23 -07:00
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (!IsFocused()) {
|
|
|
|
// retry at next focus event
|
|
|
|
mPendingMethods |= kSyncASCIICapableOnly;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-16 01:20:27 -07:00
|
|
|
TSMDocumentID doc = GetCurrentTSMDocumentID();
|
2009-09-30 19:52:50 -07:00
|
|
|
if (!doc) {
|
|
|
|
// retry
|
|
|
|
mPendingMethods |= kSyncASCIICapableOnly;
|
|
|
|
NS_WARNING("Application is active but there is no active document");
|
|
|
|
ResetTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsASCIICapableOnly) {
|
|
|
|
CFArrayRef ASCIICapableTISList = ::TISCreateASCIICapableInputSourceList();
|
|
|
|
::TSMSetDocumentProperty(doc,
|
|
|
|
kTSMDocumentEnabledInputSourcesPropertyTag,
|
|
|
|
sizeof(CFArrayRef),
|
|
|
|
&ASCIICapableTISList);
|
|
|
|
::CFRelease(ASCIICapableTISList);
|
|
|
|
} else {
|
|
|
|
::TSMRemoveDocumentProperty(doc,
|
|
|
|
kTSMDocumentEnabledInputSourcesPropertyTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::ResetTimer()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mPendingMethods != 0,
|
|
|
|
"There are not pending methods, why this is called?");
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2010-08-16 01:20:27 -07:00
|
|
|
} else {
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mTimer, );
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
mTimer->InitWithFuncCallback(FlushPendingMethods, this, 0,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::ExecutePendingMethods()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2010-08-16 01:20:27 -07:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 07:20:58 -07:00
|
|
|
mTimer = nullptr;
|
2010-08-16 01:20:27 -07:00
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
if (![[NSApplication sharedApplication] isActive]) {
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsInFocusProcessing = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
// If we're not active, we should retry at focus event
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t pendingMethods = mPendingMethods;
|
2009-09-30 19:52:50 -07:00
|
|
|
// First, reset the pending method flags because if each methods cannot
|
|
|
|
// run now, they can reentry to the pending flags by theirselves.
|
|
|
|
mPendingMethods = 0;
|
|
|
|
|
|
|
|
if (pendingMethods & kDiscardIMEComposition)
|
|
|
|
DiscardIMEComposition();
|
|
|
|
if (pendingMethods & kSyncASCIICapableOnly)
|
|
|
|
SyncASCIICapableOnly();
|
|
|
|
if (pendingMethods & kResetIMEWindowLevel)
|
|
|
|
ResetIMEWindowLevel();
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsInFocusProcessing = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-02-16 23:41:36 -08:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* IMEInputHandler implementation (native event handlers)
|
2011-02-16 23:41:36 -08:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
|
|
|
IMEInputHandler::ConvertToTextRangeType(uint32_t aUnderlineStyle,
|
2011-05-08 03:19:15 -07:00
|
|
|
NSRange& aSelectedRange)
|
2011-02-16 23:41:36 -08:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::ConvertToTextRangeType, "
|
|
|
|
"aUnderlineStyle=%llu, aSelectedRange.length=%llu,",
|
|
|
|
this, aUnderlineStyle, aSelectedRange.length));
|
|
|
|
|
2011-02-16 23:41:36 -08:00
|
|
|
// We assume that aUnderlineStyle is NSUnderlineStyleSingle or
|
|
|
|
// NSUnderlineStyleThick. NSUnderlineStyleThick should indicate a selected
|
|
|
|
// clause. Otherwise, should indicate non-selected clause.
|
|
|
|
|
|
|
|
if (aSelectedRange.length == 0) {
|
|
|
|
switch (aUnderlineStyle) {
|
|
|
|
case NSUnderlineStyleSingle:
|
|
|
|
return NS_TEXTRANGE_RAWINPUT;
|
|
|
|
case NSUnderlineStyleThick:
|
|
|
|
return NS_TEXTRANGE_SELECTEDRAWTEXT;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected line style");
|
|
|
|
return NS_TEXTRANGE_SELECTEDRAWTEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aUnderlineStyle) {
|
|
|
|
case NSUnderlineStyleSingle:
|
|
|
|
return NS_TEXTRANGE_CONVERTEDTEXT;
|
|
|
|
case NSUnderlineStyleThick:
|
|
|
|
return NS_TEXTRANGE_SELECTEDCONVERTEDTEXT;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected line style");
|
|
|
|
return NS_TEXTRANGE_SELECTEDCONVERTEDTEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::GetRangeCount(NSAttributedString *aAttrString)
|
2011-02-16 23:41:36 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
// Iterate through aAttrString for the NSUnderlineStyleAttributeName and
|
|
|
|
// count the different segments adjusting limitRange as we go.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 0;
|
2011-02-16 23:41:36 -08:00
|
|
|
NSRange effectiveRange;
|
|
|
|
NSRange limitRange = NSMakeRange(0, [aAttrString length]);
|
|
|
|
while (limitRange.length > 0) {
|
|
|
|
[aAttrString attribute:NSUnderlineStyleAttributeName
|
|
|
|
atIndex:limitRange.location
|
|
|
|
longestEffectiveRange:&effectiveRange
|
|
|
|
inRange:limitRange];
|
|
|
|
limitRange =
|
|
|
|
NSMakeRange(NSMaxRange(effectiveRange),
|
|
|
|
NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
|
|
|
|
count++;
|
|
|
|
}
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::GetRangeCount, aAttrString=\"%s\", count=%llu",
|
|
|
|
this, GetCharacters([aAttrString string]), count));
|
|
|
|
|
2011-02-16 23:41:36 -08:00
|
|
|
return count;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::SetTextRangeList(nsTArray<nsTextRange>& aTextRangeList,
|
|
|
|
NSAttributedString *aAttrString,
|
|
|
|
NSRange& aSelectedRange)
|
2011-02-16 23:41:36 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
// Convert the Cocoa range into the nsTextRange Array used in Gecko.
|
|
|
|
// Iterate through the attributed string and map the underline attribute to
|
|
|
|
// Gecko IME textrange attributes. We may need to change the code here if
|
|
|
|
// we change the implementation of validAttributesForMarkedText.
|
|
|
|
NSRange limitRange = NSMakeRange(0, [aAttrString length]);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t rangeCount = GetRangeCount(aAttrString);
|
|
|
|
for (uint32_t i = 0; i < rangeCount && limitRange.length > 0; i++) {
|
2011-02-16 23:41:36 -08:00
|
|
|
NSRange effectiveRange;
|
|
|
|
id attributeValue = [aAttrString attribute:NSUnderlineStyleAttributeName
|
|
|
|
atIndex:limitRange.location
|
|
|
|
longestEffectiveRange:&effectiveRange
|
|
|
|
inRange:limitRange];
|
|
|
|
|
|
|
|
nsTextRange range;
|
|
|
|
range.mStartOffset = effectiveRange.location;
|
|
|
|
range.mEndOffset = NSMaxRange(effectiveRange);
|
|
|
|
range.mRangeType =
|
|
|
|
ConvertToTextRangeType([attributeValue intValue], aSelectedRange);
|
|
|
|
aTextRangeList.AppendElement(range);
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetTextRangeList, "
|
|
|
|
"range={ mStartOffset=%llu, mEndOffset=%llu, mRangeType=%s }",
|
|
|
|
this, range.mStartOffset, range.mEndOffset,
|
|
|
|
GetRangeTypeName(range.mRangeType)));
|
|
|
|
|
2011-02-16 23:41:36 -08:00
|
|
|
limitRange =
|
|
|
|
NSMakeRange(NSMaxRange(effectiveRange),
|
|
|
|
NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current caret position.
|
|
|
|
nsTextRange range;
|
|
|
|
range.mStartOffset = aSelectedRange.location + aSelectedRange.length;
|
|
|
|
range.mEndOffset = range.mStartOffset;
|
|
|
|
range.mRangeType = NS_TEXTRANGE_CARETPOSITION;
|
|
|
|
aTextRangeList.AppendElement(range);
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetTextRangeList, "
|
|
|
|
"range={ mStartOffset=%llu, mEndOffset=%llu, mRangeType=%s }",
|
|
|
|
this, range.mStartOffset, range.mEndOffset,
|
|
|
|
GetRangeTypeName(range.mRangeType)));
|
|
|
|
|
2011-02-16 23:41:36 -08:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::DispatchTextEvent(const nsString& aText,
|
|
|
|
NSAttributedString* aAttrString,
|
|
|
|
NSRange& aSelectedRange,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDoCommit)
|
2011-02-16 23:41:36 -08:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::DispatchTextEvent, "
|
|
|
|
"aText=\"%s\", aAttrString=\"%s\", "
|
|
|
|
"aSelectedRange={ location=%llu, length=%llu }, "
|
|
|
|
"aDoCommit=%s, Destroyed()=%s",
|
|
|
|
this, NS_ConvertUTF16toUTF8(aText).get(),
|
|
|
|
GetCharacters([aAttrString string]),
|
|
|
|
aSelectedRange.location, aSelectedRange.length,
|
|
|
|
TrueOrFalse(aDoCommit), TrueOrFalse(Destroyed())));
|
2011-02-16 23:41:36 -08:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(!Destroyed(), false);
|
2011-05-08 03:19:23 -07:00
|
|
|
|
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsTextEvent textEvent(true, NS_TEXT_TEXT, mWidget);
|
2011-02-16 23:41:36 -08:00
|
|
|
textEvent.time = PR_IntervalNow();
|
|
|
|
textEvent.theText = aText;
|
|
|
|
nsAutoTArray<nsTextRange, 4> textRanges;
|
|
|
|
if (!aDoCommit) {
|
|
|
|
SetTextRangeList(textRanges, aAttrString, aSelectedRange);
|
|
|
|
}
|
|
|
|
textEvent.rangeArray = textRanges.Elements();
|
|
|
|
textEvent.rangeCount = textRanges.Length();
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
if (textEvent.theText != mLastDispatchedCompositionString) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsCompositionEvent compositionUpdate(true, NS_COMPOSITION_UPDATE,
|
2011-09-22 02:17:40 -07:00
|
|
|
mWidget);
|
|
|
|
compositionUpdate.time = textEvent.time;
|
|
|
|
compositionUpdate.data = textEvent.theText;
|
|
|
|
mLastDispatchedCompositionString = textEvent.theText;
|
|
|
|
DispatchEvent(compositionUpdate);
|
|
|
|
if (mIsInFocusProcessing || Destroyed()) {
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::DispatchTextEvent, compositionupdate causes "
|
|
|
|
"aborting the composition, mIsInFocusProcessing=%s, Destryoed()=%s",
|
|
|
|
this, TrueOrFalse(mIsInFocusProcessing), TrueOrFalse(Destroyed())));
|
|
|
|
if (Destroyed()) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2011-09-22 02:17:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
return DispatchEvent(textEvent);
|
2011-02-16 23:41:36 -08:00
|
|
|
}
|
|
|
|
|
2011-02-18 20:55:34 -08:00
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::InitCompositionEvent(nsCompositionEvent& aCompositionEvent)
|
2011-02-18 20:55:34 -08:00
|
|
|
{
|
|
|
|
aCompositionEvent.time = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::InsertTextAsCommittingComposition(
|
|
|
|
NSAttributedString* aAttrString)
|
2011-02-18 20:55:34 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
|
|
|
|
"aAttrString=\"%s\", Destroyed()=%s, IsIMEComposing()=%s, "
|
|
|
|
"mMarkedRange={ location=%llu, length=%llu }",
|
|
|
|
this, GetCharacters([aAttrString string]), TrueOrFalse(Destroyed()),
|
|
|
|
TrueOrFalse(IsIMEComposing()),
|
|
|
|
mMarkedRange.location, mMarkedRange.length));
|
2011-02-18 20:55:34 -08:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
2011-02-18 20:55:34 -08:00
|
|
|
|
|
|
|
nsString str;
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::GetStringForNSString([aAttrString string], str);
|
2011-02-18 20:55:34 -08:00
|
|
|
|
|
|
|
if (!IsIMEComposing()) {
|
|
|
|
// XXXmnakano Probably, we shouldn't emulate composition in this case.
|
|
|
|
// I think that we should just fire DOM3 textInput event if we implement it.
|
2011-09-30 17:20:33 -07:00
|
|
|
nsCompositionEvent compStart(true, NS_COMPOSITION_START, mWidget);
|
2011-02-18 20:55:34 -08:00
|
|
|
InitCompositionEvent(compStart);
|
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(compStart);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
|
|
|
|
"destroyed by compositionstart event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OnStartIMEComposition();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IgnoreIMECommit()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
|
|
|
|
"IgnoreIMECommit()=%s", this, TrueOrFalse(IgnoreIMECommit())));
|
2011-02-18 20:55:34 -08:00
|
|
|
str.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRange range = NSMakeRange(0, str.Length());
|
2011-09-30 17:20:33 -07:00
|
|
|
DispatchTextEvent(str, aAttrString, range, true);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
|
|
|
|
"destroyed by text event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OnUpdateIMEComposition([aAttrString string]);
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsCompositionEvent compEnd(true, NS_COMPOSITION_END, mWidget);
|
2011-02-18 20:55:34 -08:00
|
|
|
InitCompositionEvent(compEnd);
|
2011-09-22 02:17:40 -07:00
|
|
|
compEnd.data = mLastDispatchedCompositionString;
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(compEnd);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
|
|
|
|
"destroyed by compositionend event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OnEndIMEComposition();
|
|
|
|
|
|
|
|
mMarkedRange = NSMakeRange(NSNotFound, 0);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::SetMarkedText(NSAttributedString* aAttrString,
|
|
|
|
NSRange& aSelectedRange)
|
2011-02-18 20:55:34 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetMarkedText, "
|
|
|
|
"aAttrString=\"%s\", aSelectedRange={ location=%llu, length=%llu }, "
|
|
|
|
"Destroyed()=%s, IgnoreIMEComposition()=%s, IsIMEComposing()=%s, "
|
|
|
|
"mMarkedRange={ location=%llu, length=%llu }",
|
|
|
|
this, GetCharacters([aAttrString string]),
|
|
|
|
aSelectedRange.location, aSelectedRange.length, TrueOrFalse(Destroyed()),
|
|
|
|
TrueOrFalse(IgnoreIMEComposition()), TrueOrFalse(IsIMEComposing()),
|
|
|
|
mMarkedRange.location, mMarkedRange.length));
|
2011-02-18 20:55:34 -08:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed() || IgnoreIMEComposition()) {
|
2011-02-18 20:55:34 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
2011-02-18 20:55:34 -08:00
|
|
|
|
|
|
|
nsString str;
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::GetStringForNSString([aAttrString string], str);
|
2011-02-18 20:55:34 -08:00
|
|
|
|
|
|
|
mMarkedRange.length = str.Length();
|
|
|
|
|
|
|
|
if (!IsIMEComposing() && !str.IsEmpty()) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT,
|
2011-05-08 03:19:23 -07:00
|
|
|
mWidget);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(selection);
|
2011-02-18 20:55:34 -08:00
|
|
|
mMarkedRange.location = selection.mSucceeded ? selection.mReply.mOffset : 0;
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsCompositionEvent compStart(true, NS_COMPOSITION_START, mWidget);
|
2011-02-18 20:55:34 -08:00
|
|
|
InitCompositionEvent(compStart);
|
|
|
|
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(compStart);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetMarkedText, "
|
|
|
|
"destroyed by compositionstart event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OnStartIMEComposition();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsIMEComposing()) {
|
|
|
|
OnUpdateIMEComposition([aAttrString string]);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doCommit = str.IsEmpty();
|
2011-02-18 20:55:34 -08:00
|
|
|
DispatchTextEvent(str, aAttrString, aSelectedRange, doCommit);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetMarkedText, "
|
|
|
|
"destroyed by text event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doCommit) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsCompositionEvent compEnd(true, NS_COMPOSITION_END, mWidget);
|
2011-02-18 20:55:34 -08:00
|
|
|
InitCompositionEvent(compEnd);
|
2011-09-22 02:17:40 -07:00
|
|
|
compEnd.data = mLastDispatchedCompositionString;
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(compEnd);
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SetMarkedText, "
|
|
|
|
"destroyed by compositionend event", this));
|
2011-05-08 03:19:23 -07:00
|
|
|
return;
|
2011-02-18 20:55:34 -08:00
|
|
|
}
|
|
|
|
OnEndIMEComposition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
NSInteger
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::ConversationIdentifier()
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::ConversationIdentifier, Destroyed()=%s",
|
|
|
|
this, TrueOrFalse(Destroyed())));
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
|
|
|
return reinterpret_cast<NSInteger>(mView);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
// NOTE: The size of NSInteger is same as pointer size.
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent textContent(true, NS_QUERY_TEXT_CONTENT, mWidget);
|
2011-02-18 20:55:49 -08:00
|
|
|
textContent.InitForQueryTextContent(0, 0);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(textContent);
|
2011-02-18 20:55:49 -08:00
|
|
|
if (!textContent.mSucceeded) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::ConversationIdentifier, Failed", this));
|
2011-02-18 20:55:49 -08:00
|
|
|
return reinterpret_cast<NSInteger>(mView);
|
|
|
|
}
|
|
|
|
// XXX This might return same ID as a previously existing editor if the
|
|
|
|
// deleted editor was created at the same address. Is there a better way?
|
|
|
|
return reinterpret_cast<NSInteger>(textContent.mReply.mContentsRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSAttributedString*
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::GetAttributedSubstringFromRange(NSRange& aRange)
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::GetAttributedSubstringFromRange, "
|
|
|
|
"aRange={ location=%llu, length=%llu }, Destroyed()=%s",
|
|
|
|
this, aRange.location, aRange.length, TrueOrFalse(Destroyed())));
|
2011-02-18 20:55:49 -08:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed() || aRange.location == NSNotFound || aRange.length == 0) {
|
2011-02-18 20:55:49 -08:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
nsAutoString str;
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent textContent(true, NS_QUERY_TEXT_CONTENT, mWidget);
|
2011-02-18 20:55:49 -08:00
|
|
|
textContent.InitForQueryTextContent(aRange.location, aRange.length);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(textContent);
|
2011-02-18 20:55:49 -08:00
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::GetAttributedSubstringFromRange, "
|
|
|
|
"textContent={ mSucceeded=%s, mReply.mString=\"%s\"",
|
|
|
|
this, TrueOrFalse(textContent.mSucceeded),
|
|
|
|
NS_ConvertUTF16toUTF8(textContent.mReply.mString).get()));
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
if (!textContent.mSucceeded || textContent.mReply.mString.IsEmpty()) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
NSString* nsstr = nsCocoaUtils::ToNSString(textContent.mReply.mString);
|
2011-02-18 20:55:49 -08:00
|
|
|
NSAttributedString* result =
|
|
|
|
[[[NSAttributedString alloc] initWithString:nsstr
|
|
|
|
attributes:nil] autorelease];
|
|
|
|
return result;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-07-20 17:33:16 -07:00
|
|
|
IMEInputHandler::HasMarkedText()
|
|
|
|
{
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::HasMarkedText, "
|
|
|
|
"mMarkedRange={ location=%llu, length=%llu }",
|
|
|
|
this, mMarkedRange.location, mMarkedRange.length));
|
|
|
|
|
|
|
|
return (mMarkedRange.location != NSNotFound) && (mMarkedRange.length != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRange
|
|
|
|
IMEInputHandler::MarkedRange()
|
|
|
|
{
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::MarkedRange, "
|
|
|
|
"mMarkedRange={ location=%llu, length=%llu }",
|
|
|
|
this, mMarkedRange.location, mMarkedRange.length));
|
|
|
|
|
|
|
|
if (!HasMarkedText()) {
|
|
|
|
return NSMakeRange(NSNotFound, 0);
|
|
|
|
}
|
|
|
|
return mMarkedRange;
|
|
|
|
}
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
NSRange
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::SelectedRange()
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SelectedRange, Destroyed()=%s",
|
|
|
|
this, TrueOrFalse(Destroyed())));
|
2011-05-08 03:19:23 -07:00
|
|
|
|
|
|
|
NSRange range = NSMakeRange(NSNotFound, 0);
|
|
|
|
if (Destroyed()) {
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, mWidget);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(selection);
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SelectedRange, selection={ mSucceeded=%s, "
|
|
|
|
"mReply={ mOffset=%llu, mString.Length()=%llu } }",
|
|
|
|
this, TrueOrFalse(selection.mSucceeded), selection.mReply.mOffset,
|
|
|
|
selection.mReply.mString.Length()));
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
if (!selection.mSucceeded) {
|
2011-05-08 03:19:23 -07:00
|
|
|
return range;
|
2011-02-18 20:55:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NSMakeRange(selection.mReply.mOffset,
|
|
|
|
selection.mReply.mString.Length());
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NSMakeRange(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRect
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::FirstRectForCharacterRange(NSRange& aRange)
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::FirstRectForCharacterRange, Destroyed()=%s"
|
|
|
|
"aRange={ location=%llu, length=%llu }",
|
|
|
|
this, TrueOrFalse(Destroyed()), aRange.location, aRange.length));
|
2011-05-08 03:19:23 -07:00
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
// XXX this returns first character rect or caret rect, it is limitation of
|
|
|
|
// now. We need more work for returns first line rect. But current
|
|
|
|
// implementation is enough for IMEs.
|
|
|
|
|
|
|
|
NSRect rect;
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed() || aRange.location == NSNotFound) {
|
2011-02-18 20:55:49 -08:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
nsIntRect r;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool useCaretRect = (aRange.length == 0);
|
2011-02-18 20:55:49 -08:00
|
|
|
if (!useCaretRect) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent charRect(true, NS_QUERY_TEXT_RECT, mWidget);
|
2011-02-18 20:55:49 -08:00
|
|
|
charRect.InitForQueryTextRect(aRange.location, 1);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(charRect);
|
2011-02-18 20:55:49 -08:00
|
|
|
if (charRect.mSucceeded) {
|
|
|
|
r = charRect.mReply.mRect;
|
|
|
|
} else {
|
2011-09-30 17:20:33 -07:00
|
|
|
useCaretRect = true;
|
2011-02-18 20:55:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useCaretRect) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsQueryContentEvent caretRect(true, NS_QUERY_CARET_RECT, mWidget);
|
2011-02-18 20:55:49 -08:00
|
|
|
caretRect.InitForQueryCaretRect(aRange.location);
|
2011-02-23 09:25:11 -08:00
|
|
|
DispatchEvent(caretRect);
|
2011-02-18 20:55:49 -08:00
|
|
|
if (!caretRect.mSucceeded) {
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
r = caretRect.mReply.mRect;
|
|
|
|
r.width = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
nsIWidget* rootWidget = mWidget->GetTopLevelWidget();
|
2011-02-18 20:55:49 -08:00
|
|
|
NSWindow* rootWindow =
|
|
|
|
static_cast<NSWindow*>(rootWidget->GetNativeData(NS_NATIVE_WINDOW));
|
|
|
|
NSView* rootView =
|
|
|
|
static_cast<NSView*>(rootWidget->GetNativeData(NS_NATIVE_WIDGET));
|
|
|
|
if (!rootWindow || !rootView) {
|
|
|
|
return rect;
|
|
|
|
}
|
2012-10-10 11:09:29 -07:00
|
|
|
rect = nsCocoaUtils::DevPixelsToCocoaPoints(r, mWidget->GetDefaultScale());
|
2011-02-18 20:55:49 -08:00
|
|
|
rect = [rootView convertRect:rect toView:nil];
|
|
|
|
rect.origin = [rootWindow convertBaseToScreen:rect.origin];
|
2011-07-20 17:33:16 -07:00
|
|
|
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::FirstRectForCharacterRange, "
|
|
|
|
"useCaretRect=%s rect={ x=%f, y=%f, width=%f, height=%f }",
|
|
|
|
this, TrueOrFalse(useCaretRect), rect.origin.x, rect.origin.y,
|
|
|
|
rect.size.width, rect.size.height));
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
return rect;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NSMakeRect(0.0, 0.0, 0.0, 0.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::CharacterIndexForPoint(NSPoint& aPoint)
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::CharacterIndexForPoint, aPoint={ x=%f, y=%f }",
|
|
|
|
this, aPoint.x, aPoint.y));
|
2011-05-08 03:19:23 -07:00
|
|
|
|
|
|
|
//nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
// To implement this, we'd have to grovel in text frames looking at text
|
|
|
|
// offsets.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray*
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::GetValidAttributesForMarkedText()
|
2011-02-18 20:55:49 -08:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::GetValidAttributesForMarkedText", this));
|
2011-02-18 20:55:49 -08:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
//nsRefPtr<IMEInputHandler> kungFuDeathGrip(this);
|
|
|
|
|
2011-02-18 20:55:49 -08:00
|
|
|
//return [NSArray arrayWithObjects:NSUnderlineStyleAttributeName,
|
|
|
|
// NSMarkedClauseSegmentAttributeName,
|
|
|
|
// NSTextInputReplacementRangeAttributeName,
|
|
|
|
// nil];
|
|
|
|
// empty array; we don't support any attributes right now
|
|
|
|
return [NSArray array];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
|
|
|
}
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
2011-05-08 03:19:15 -07:00
|
|
|
* IMEInputHandler implementation #2
|
2009-09-30 19:52:50 -07:00
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
IMEInputHandler::IMEInputHandler(nsChildView* aWidget,
|
|
|
|
NSView<mozView> *aNativeView) :
|
|
|
|
PluginTextInputHandler(aWidget, aNativeView),
|
2012-07-30 07:20:58 -07:00
|
|
|
mPendingMethods(0), mIMECompositionString(nullptr),
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsIMEComposing(false), mIsIMEEnabled(true),
|
|
|
|
mIsASCIICapableOnly(false), mIgnoreIMECommit(false),
|
|
|
|
mIsInFocusProcessing(false)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
InitStaticMembers();
|
2011-02-18 20:55:34 -08:00
|
|
|
|
|
|
|
mMarkedRange.location = NSNotFound;
|
|
|
|
mMarkedRange.length = 0;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::~IMEInputHandler()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 07:20:58 -07:00
|
|
|
mTimer = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
if (sFocusedIMEHandler == this) {
|
2012-07-30 07:20:58 -07:00
|
|
|
sFocusedIMEHandler = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
IMEInputHandler::OnFocusChangeInGecko(bool aFocus)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OnFocusChangeInGecko, aFocus=%s, Destroyed()=%s, "
|
|
|
|
"sFocusedIMEHandler=%p",
|
|
|
|
this, TrueOrFalse(aFocus), TrueOrFalse(Destroyed()), sFocusedIMEHandler));
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
// This is called when the native focus is changed and when the native focus
|
|
|
|
// isn't changed but the focus is changed in Gecko.
|
|
|
|
if (!aFocus) {
|
|
|
|
if (sFocusedIMEHandler == this)
|
2012-07-30 07:20:58 -07:00
|
|
|
sFocusedIMEHandler = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sFocusedIMEHandler = this;
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsInFocusProcessing = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
// We need to reset the IME's window level by the current focused view of
|
|
|
|
// Gecko. It may be different from mView. However, we cannot get the
|
|
|
|
// new focused view here because the focus change process in Gecko hasn't
|
|
|
|
// been finished yet. So, we should post the job to the todo list.
|
|
|
|
mPendingMethods |= kResetIMEWindowLevel;
|
|
|
|
ResetTimer();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:23 -07:00
|
|
|
IMEInputHandler::OnDestroyWidget(nsChildView* aDestroyingWidget)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OnDestroyWidget, aDestroyingWidget=%p, "
|
|
|
|
"sFocusedIMEHandler=%p, IsIMEComposing()=%s",
|
|
|
|
this, aDestroyingWidget, sFocusedIMEHandler,
|
|
|
|
TrueOrFalse(IsIMEComposing())));
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
// If we're not focused, the focused IMEInputHandler may have been
|
|
|
|
// created by another widget/nsChildView.
|
2009-09-30 19:52:50 -07:00
|
|
|
if (sFocusedIMEHandler && sFocusedIMEHandler != this) {
|
2011-05-08 03:19:23 -07:00
|
|
|
sFocusedIMEHandler->OnDestroyWidget(aDestroyingWidget);
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (!PluginTextInputHandler::OnDestroyWidget(aDestroyingWidget)) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsIMEComposing()) {
|
|
|
|
// If our view is in the composition, we should clean up it.
|
|
|
|
CancelIMEComposition();
|
|
|
|
OnEndIMEComposition();
|
|
|
|
}
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::OnStartIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OnStartIMEComposition, mView=%p, mWidget=%p"
|
|
|
|
"currentInputManager=%p, mIsIMEComposing=%s",
|
|
|
|
this, mView, mWidget, [NSInputManager currentInputManager],
|
|
|
|
TrueOrFalse(mIsIMEComposing)));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(!mIsIMEComposing, "There is a composition already");
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsIMEComposing = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
mLastDispatchedCompositionString.Truncate();
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::OnUpdateIMEComposition(NSString* aIMECompositionString)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OnUpdateIMEComposition, mView=%p, mWidget=%p, "
|
|
|
|
"currentInputManager=%p, mIsIMEComposing=%s, aIMECompositionString=\"%s\"",
|
|
|
|
this, mView, mWidget, [NSInputManager currentInputManager],
|
|
|
|
TrueOrFalse(mIsIMEComposing), GetCharacters(aIMECompositionString)));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(mIsIMEComposing, "We're not in composition");
|
|
|
|
|
|
|
|
if (mIMECompositionString)
|
|
|
|
[mIMECompositionString release];
|
|
|
|
mIMECompositionString = [aIMECompositionString retain];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::OnEndIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OnEndIMEComposition, mView=%p, mWidget=%p, "
|
|
|
|
"currentInputManager=%p, mIsIMEComposing=%s",
|
|
|
|
this, mView, mWidget, [NSInputManager currentInputManager],
|
|
|
|
TrueOrFalse(mIsIMEComposing)));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(mIsIMEComposing, "We're not in composition");
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
mIsIMEComposing = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (mIMECompositionString) {
|
|
|
|
[mIMECompositionString release];
|
2012-07-30 07:20:58 -07:00
|
|
|
mIMECompositionString = nullptr;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-22 02:17:40 -07:00
|
|
|
mLastDispatchedCompositionString.Truncate();
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::SendCommittedText(NSString *aString)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::SendCommittedText, mView=%p, mWidget=%p, "
|
|
|
|
"currentInputManager=%p, mIsIMEComposing=%s",
|
|
|
|
this, mView, mWidget, [NSInputManager currentInputManager],
|
|
|
|
TrueOrFalse(mIsIMEComposing), mWidget));
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
NS_ENSURE_TRUE(mWidget, );
|
|
|
|
// XXX We should send the string without mView.
|
|
|
|
if (!mView) {
|
|
|
|
return;
|
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
NSAttributedString* attrStr =
|
|
|
|
[[NSAttributedString alloc] initWithString:aString];
|
|
|
|
[mView insertText:attrStr];
|
|
|
|
[attrStr release];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::KillIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::KillIMEComposition, mView=%p, mWidget=%p, "
|
|
|
|
"currentInputManager=%p, mIsIMEComposing=%s, "
|
|
|
|
"Destroyed()=%s, IsFocused()=%s",
|
|
|
|
this, mView, mWidget, [NSInputManager currentInputManager],
|
|
|
|
TrueOrFalse(mIsIMEComposing), TrueOrFalse(Destroyed()),
|
|
|
|
TrueOrFalse(IsFocused())));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (Destroyed()) {
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
2011-05-08 03:19:23 -07:00
|
|
|
}
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (IsFocused()) {
|
|
|
|
[[NSInputManager currentInputManager] markedTextAbandoned: mView];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::KillIMEComposition, Pending...", this));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
// Commit the composition internally.
|
|
|
|
SendCommittedText(mIMECompositionString);
|
|
|
|
NS_ASSERTION(!mIsIMEComposing, "We're still in a composition");
|
|
|
|
// The pending method will be fired by the next focus event.
|
|
|
|
mPendingMethods |= kDiscardIMEComposition;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::CommitIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (!IsIMEComposing())
|
|
|
|
return;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::CommitIMEComposition, mIMECompositionString=%s",
|
|
|
|
this, GetCharacters(mIMECompositionString)));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
KillIMEComposition();
|
|
|
|
|
|
|
|
if (!IsIMEComposing())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the composition is still there, KillIMEComposition only kills the
|
|
|
|
// composition in TSM. We also need to finish the our composition too.
|
|
|
|
SendCommittedText(mIMECompositionString);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::CancelIMEComposition()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (!IsIMEComposing())
|
|
|
|
return;
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::CancelIMEComposition, mIMECompositionString=%s",
|
|
|
|
this, GetCharacters(mIMECompositionString)));
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
// For canceling the current composing, we need to ignore the param of
|
|
|
|
// insertText. But this code is ugly...
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreIMECommit = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
KillIMEComposition();
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreIMECommit = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
|
|
|
|
if (!IsIMEComposing())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the composition is still there, KillIMEComposition only kills the
|
|
|
|
// composition in TSM. We also need to kill the our composition too.
|
|
|
|
SendCommittedText(@"");
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::IsFocused()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(!Destroyed(), false);
|
2009-09-30 19:52:50 -07:00
|
|
|
NSWindow* window = [mView window];
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_ENSURE_TRUE(window, false);
|
2010-08-16 01:20:27 -07:00
|
|
|
return [window firstResponder] == mView &&
|
|
|
|
([window isMainWindow] || [window isSheet]) &&
|
2009-09-30 19:52:50 -07:00
|
|
|
[[NSApplication sharedApplication] isActive];
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::IsIMEOpened()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.InitByCurrentInputSource();
|
|
|
|
return tis.IsOpenedIMEMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
IMEInputHandler::SetASCIICapableOnly(bool aASCIICapableOnly)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (aASCIICapableOnly == mIsASCIICapableOnly)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CommitIMEComposition();
|
|
|
|
mIsASCIICapableOnly = aASCIICapableOnly;
|
|
|
|
SyncASCIICapableOnly();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
IMEInputHandler::EnableIME(bool aEnableIME)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (aEnableIME == mIsIMEEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CommitIMEComposition();
|
|
|
|
mIsIMEEnabled = aEnableIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
IMEInputHandler::SetIMEOpenState(bool aOpenIME)
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
|
|
|
if (!IsFocused() || IsIMEOpened() == aOpenIME)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!aOpenIME) {
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.InitByCurrentASCIICapableInputSource();
|
|
|
|
tis.Select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we know the latest IME opened mode, we should select it.
|
|
|
|
if (sLatestIMEOpenedModeInputSourceID) {
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.InitByInputSourceID(sLatestIMEOpenedModeInputSourceID);
|
|
|
|
tis.Select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX If the current input source is a mode of IME, we should turn on it,
|
|
|
|
// but we haven't found such way...
|
|
|
|
|
|
|
|
// Finally, we should refer the system locale but this is a little expensive,
|
|
|
|
// we shouldn't retry this (if it was succeeded, we already set
|
|
|
|
// sLatestIMEOpenedModeInputSourceID at that time).
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool sIsPrefferredIMESearched = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
if (sIsPrefferredIMESearched)
|
|
|
|
return;
|
2011-09-30 17:20:33 -07:00
|
|
|
sIsPrefferredIMESearched = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
OpenSystemPreferredLanguageIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 03:19:15 -07:00
|
|
|
IMEInputHandler::OpenSystemPreferredLanguageIME()
|
2009-09-30 19:52:50 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OpenSystemPreferredLanguageIME", this));
|
|
|
|
|
2009-09-30 19:52:50 -07:00
|
|
|
CFArrayRef langList = ::CFLocaleCopyPreferredLanguages();
|
|
|
|
if (!langList) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OpenSystemPreferredLanguageIME, langList is NULL",
|
|
|
|
this));
|
2009-09-30 19:52:50 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CFIndex count = ::CFArrayGetCount(langList);
|
|
|
|
for (CFIndex i = 0; i < count; i++) {
|
|
|
|
CFLocaleRef locale =
|
|
|
|
::CFLocaleCreate(kCFAllocatorDefault,
|
|
|
|
static_cast<CFStringRef>(::CFArrayGetValueAtIndex(langList, i)));
|
|
|
|
if (!locale) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool changed = false;
|
2009-09-30 19:52:50 -07:00
|
|
|
CFStringRef lang = static_cast<CFStringRef>(
|
|
|
|
::CFLocaleGetValue(locale, kCFLocaleLanguageCode));
|
|
|
|
NS_ASSERTION(lang, "lang is null");
|
|
|
|
if (lang) {
|
2011-05-08 03:19:15 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.InitByLanguage(lang);
|
|
|
|
if (tis.IsOpenedIMEMode()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (PR_LOG_TEST(gLog, PR_LOG_ALWAYS)) {
|
|
|
|
CFStringRef foundTIS;
|
|
|
|
tis.GetInputSourceID(foundTIS);
|
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p IMEInputHandler::OpenSystemPreferredLanguageIME, "
|
|
|
|
"foundTIS=%s, lang=%s",
|
|
|
|
this, GetCharacters(foundTIS), GetCharacters(lang)));
|
|
|
|
}
|
|
|
|
#endif // #ifdef PR_LOGGING
|
2009-09-30 19:52:50 -07:00
|
|
|
tis.Select();
|
2011-09-30 17:20:33 -07:00
|
|
|
changed = true;
|
2009-09-30 19:52:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
::CFRelease(locale);
|
|
|
|
if (changed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
::CFRelease(langList);
|
|
|
|
}
|
2011-05-08 03:19:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* PluginTextInputHandler implementation
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
PluginTextInputHandler::PluginTextInputHandler(nsChildView* aWidget,
|
|
|
|
NSView<mozView> *aNativeView) :
|
2011-02-23 20:48:12 -08:00
|
|
|
TextInputHandlerBase(aWidget, aNativeView),
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent(false),
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifndef __LP64__
|
2011-09-30 17:20:33 -07:00
|
|
|
mPluginTSMDoc(0), mPluginTSMInComposition(false),
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifndef __LP64__
|
2011-09-30 17:20:33 -07:00
|
|
|
mPluginComplexTextInputRequested(false)
|
2011-05-08 03:19:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginTextInputHandler::~PluginTextInputHandler()
|
|
|
|
{
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifndef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
if (mPluginTSMDoc) {
|
|
|
|
::DeleteTSMDocument(mPluginTSMDoc);
|
|
|
|
}
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifndef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
PluginTextInputHandler::ConvertCocoaKeyEventToNPCocoaEvent(
|
|
|
|
NSEvent* aCocoaEvent,
|
|
|
|
NPCocoaEvent& aPluginEvent)
|
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitNPCocoaEvent(&aPluginEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
NSEventType nativeType = [aCocoaEvent type];
|
|
|
|
switch (nativeType) {
|
|
|
|
case NSKeyDown:
|
|
|
|
aPluginEvent.type = NPCocoaEventKeyDown;
|
|
|
|
break;
|
|
|
|
case NSKeyUp:
|
|
|
|
aPluginEvent.type = NPCocoaEventKeyUp;
|
|
|
|
break;
|
|
|
|
case NSFlagsChanged:
|
|
|
|
aPluginEvent.type = NPCocoaEventFlagsChanged;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Asked to convert key event of unknown type to Cocoa plugin event!");
|
|
|
|
}
|
|
|
|
aPluginEvent.data.key.modifierFlags = [aCocoaEvent modifierFlags];
|
|
|
|
aPluginEvent.data.key.keyCode = [aCocoaEvent keyCode];
|
|
|
|
// don't try to access character data for flags changed events,
|
|
|
|
// it will raise an exception
|
|
|
|
if (nativeType != NSFlagsChanged) {
|
|
|
|
aPluginEvent.data.key.characters = (NPNSString*)[aCocoaEvent characters];
|
|
|
|
aPluginEvent.data.key.charactersIgnoringModifiers =
|
|
|
|
(NPNSString*)[aCocoaEvent charactersIgnoringModifiers];
|
|
|
|
aPluginEvent.data.key.isARepeat = [aCocoaEvent isARepeat];
|
|
|
|
}
|
2011-05-08 03:19:15 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifndef __LP64__
|
2011-02-23 10:06:26 -08:00
|
|
|
|
2011-02-23 20:48:12 -08:00
|
|
|
EventHandlerRef PluginTextInputHandler::sPluginKeyEventsHandler = NULL;
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
PluginTextInputHandler::InstallPluginKeyEventsHandler()
|
|
|
|
{
|
|
|
|
if (sPluginKeyEventsHandler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static const EventTypeSpec sTSMEvents[] =
|
|
|
|
{ { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } };
|
|
|
|
::InstallEventHandler(::GetEventDispatcherTarget(),
|
|
|
|
::NewEventHandlerUPP(PluginKeyEventsHandler),
|
|
|
|
GetEventTypeCount(sTSMEvents),
|
|
|
|
sTSMEvents,
|
|
|
|
NULL,
|
|
|
|
&sPluginKeyEventsHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
PluginTextInputHandler::RemovePluginKeyEventsHandler()
|
|
|
|
{
|
|
|
|
if (!sPluginKeyEventsHandler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::RemoveEventHandler(sPluginKeyEventsHandler);
|
|
|
|
sPluginKeyEventsHandler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
PluginTextInputHandler::SwizzleMethods()
|
|
|
|
{
|
|
|
|
Class IMKInputSessionClass = ::NSClassFromString(@"IMKInputSession");
|
|
|
|
nsToolkit::SwizzleMethods(IMKInputSessionClass, @selector(handleEvent:),
|
|
|
|
@selector(PluginTextInputHandler_IMKInputSession_handleEvent:));
|
|
|
|
nsToolkit::SwizzleMethods(IMKInputSessionClass, @selector(commitComposition),
|
|
|
|
@selector(PluginTextInputHandler_IMKInputSession_commitComposition));
|
|
|
|
nsToolkit::SwizzleMethods(IMKInputSessionClass, @selector(finishSession),
|
|
|
|
@selector(PluginTextInputHandler_IMKInputSession_finishSession));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ OSStatus
|
|
|
|
PluginTextInputHandler::PluginKeyEventsHandler(EventHandlerCallRef aHandlerRef,
|
|
|
|
EventRef aEvent,
|
|
|
|
void *aUserData)
|
|
|
|
{
|
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
|
|
|
TSMDocumentID activeDoc = ::TSMGetActiveDocument();
|
|
|
|
NS_ENSURE_TRUE(activeDoc, eventNotHandledErr);
|
|
|
|
|
|
|
|
ChildView *target = nil;
|
|
|
|
OSStatus status = ::TSMGetDocumentProperty(activeDoc,
|
|
|
|
kFocusedChildViewTSMDocPropertyTag,
|
|
|
|
sizeof(ChildView *), nil, &target);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, eventNotHandledErr);
|
|
|
|
NS_ENSURE_TRUE(target, eventNotHandledErr);
|
|
|
|
|
|
|
|
EventRef keyEvent = NULL;
|
|
|
|
status = ::GetEventParameter(aEvent, kEventParamTextInputSendKeyboardEvent,
|
|
|
|
typeEventRef, NULL, sizeof(EventRef), NULL,
|
|
|
|
&keyEvent);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, eventNotHandledErr);
|
|
|
|
NS_ENSURE_TRUE(keyEvent, eventNotHandledErr);
|
|
|
|
|
|
|
|
nsIWidget* widget = [target widget];
|
|
|
|
NS_ENSURE_TRUE(widget, eventNotHandledErr);
|
|
|
|
TextInputHandler* handler =
|
|
|
|
static_cast<nsChildView*>(widget)->GetTextInputHandler();
|
|
|
|
NS_ENSURE_TRUE(handler, eventNotHandledErr);
|
|
|
|
handler->HandleCarbonPluginKeyEvent(keyEvent);
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from PluginKeyEventsHandler() (a handler for Carbon TSM events) to
|
|
|
|
// process a Carbon key event for the currently focused plugin. Both Unicode
|
|
|
|
// characters and "Mac encoding characters" (in the MBCS or "multibyte
|
|
|
|
// character system") are (or should be) available from aKeyEvent, but here we
|
|
|
|
// use the MCBS characters. This is how the WebKit does things, and seems to
|
|
|
|
// be what plugins expect.
|
|
|
|
void
|
|
|
|
PluginTextInputHandler::HandleCarbonPluginKeyEvent(EventRef aKeyEvent)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mView, "mView must not be NULL");
|
|
|
|
|
|
|
|
nsRefPtr<nsChildView> kungFuDeathGrip(mWidget);
|
|
|
|
|
|
|
|
if ([mView pluginEventModel] == NPEventModelCocoa) {
|
|
|
|
UInt32 size;
|
|
|
|
OSStatus status =
|
|
|
|
::GetEventParameter(aKeyEvent, kEventParamKeyUnicodes, typeUnicodeText,
|
|
|
|
NULL, 0, &size, NULL);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
|
|
|
UniChar* chars = (UniChar*)malloc(size);
|
|
|
|
NS_ENSURE_TRUE(chars, );
|
|
|
|
|
|
|
|
status = ::GetEventParameter(aKeyEvent, kEventParamKeyUnicodes,
|
|
|
|
typeUnicodeText, NULL, size, NULL, chars);
|
|
|
|
if (status != noErr) {
|
|
|
|
free(chars);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFStringRef text =
|
|
|
|
::CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, chars,
|
|
|
|
(size / sizeof(UniChar)),
|
|
|
|
kCFAllocatorNull);
|
|
|
|
if (!text) {
|
|
|
|
free(chars);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NPCocoaEvent cocoaTextEvent;
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitNPCocoaEvent(&cocoaTextEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
cocoaTextEvent.type = NPCocoaEventTextInput;
|
|
|
|
cocoaTextEvent.data.text.text = (NPNSString*)text;
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsPluginEvent pluginEvent(true, NS_PLUGIN_INPUT_EVENT, mWidget);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitPluginEvent(pluginEvent, cocoaTextEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
DispatchEvent(pluginEvent);
|
|
|
|
|
|
|
|
::CFRelease(text);
|
|
|
|
free(chars);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt32 numCharCodes;
|
|
|
|
OSStatus status = ::GetEventParameter(aKeyEvent, kEventParamKeyMacCharCodes,
|
|
|
|
typeChar, NULL, 0, &numCharCodes, NULL);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
|
|
|
nsAutoTArray<unsigned char, 3> charCodes;
|
|
|
|
charCodes.SetLength(numCharCodes);
|
|
|
|
status = ::GetEventParameter(aKeyEvent, kEventParamKeyMacCharCodes,
|
|
|
|
typeChar, NULL, numCharCodes, NULL,
|
|
|
|
charCodes.Elements());
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
|
|
|
UInt32 modifiers;
|
|
|
|
status = ::GetEventParameter(aKeyEvent, kEventParamKeyModifiers,
|
|
|
|
typeUInt32, NULL, sizeof(modifiers), NULL,
|
|
|
|
&modifiers);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
NSUInteger cocoaModifiers = 0;
|
|
|
|
if (modifiers & shiftKey) {
|
|
|
|
cocoaModifiers |= NSShiftKeyMask;
|
|
|
|
}
|
|
|
|
if (modifiers & controlKey) {
|
|
|
|
cocoaModifiers |= NSControlKeyMask;
|
|
|
|
}
|
|
|
|
if (modifiers & optionKey) {
|
|
|
|
cocoaModifiers |= NSAlternateKeyMask;
|
|
|
|
}
|
|
|
|
if (modifiers & cmdKey) { // Should never happen
|
|
|
|
cocoaModifiers |= NSCommandKeyMask;
|
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:12 -08:00
|
|
|
UInt32 macKeyCode;
|
|
|
|
status = ::GetEventParameter(aKeyEvent, kEventParamKeyCode,
|
|
|
|
typeUInt32, NULL, sizeof(macKeyCode), NULL,
|
|
|
|
&macKeyCode);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper& currentInputSource =
|
|
|
|
TISInputSourceWrapper::CurrentInputSource();
|
2012-05-17 00:04:16 -07:00
|
|
|
|
2011-02-23 20:48:12 -08:00
|
|
|
EventRef cloneEvent = ::CopyEvent(aKeyEvent);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < numCharCodes; ++i) {
|
2011-02-23 20:48:12 -08:00
|
|
|
status = ::SetEventParameter(cloneEvent, kEventParamKeyMacCharCodes,
|
|
|
|
typeChar, 1, charCodes.Elements() + i);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, );
|
|
|
|
|
|
|
|
EventRecord eventRec;
|
|
|
|
if (::ConvertEventRefToEventRecord(cloneEvent, &eventRec)) {
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keydownEvent(true, NS_KEY_DOWN, mWidget);
|
2012-04-24 20:00:02 -07:00
|
|
|
nsCocoaUtils::InitInputEvent(keydownEvent, cocoaModifiers);
|
2011-02-23 20:48:12 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t keyCode =
|
2012-10-15 18:43:50 -07:00
|
|
|
currentInputSource.ComputeGeckoKeyCode(macKeyCode, ::LMGetKbdType(),
|
|
|
|
keydownEvent.IsMeta());
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t charCode(charCodes.ElementAt(i));
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
keydownEvent.time = PR_IntervalNow();
|
|
|
|
keydownEvent.pluginEvent = &eventRec;
|
|
|
|
if (IsSpecialGeckoKey(macKeyCode)) {
|
|
|
|
keydownEvent.keyCode = keyCode;
|
|
|
|
} else {
|
2012-05-17 00:04:16 -07:00
|
|
|
// XXX This is wrong. charCode must be 0 for keydown event.
|
2011-02-23 20:48:12 -08:00
|
|
|
keydownEvent.charCode = charCode;
|
2011-09-30 17:20:33 -07:00
|
|
|
keydownEvent.isChar = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
DispatchEvent(keydownEvent);
|
|
|
|
if (Destroyed()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::ReleaseEvent(cloneEvent);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginTextInputHandler::ActivatePluginTSMDocument()
|
|
|
|
{
|
|
|
|
if (!mPluginTSMDoc) {
|
|
|
|
// Create a TSM document that supports both non-Unicode and Unicode input.
|
|
|
|
// Though ProcessPluginKeyEvent() only sends Mac char codes to
|
|
|
|
// the plugin, this makes the input window behave better when it contains
|
|
|
|
// more than one kind of input (say Hiragana and Romaji). This is what
|
|
|
|
// the OS does when it creates a TSM document for use by an
|
|
|
|
// NSTSMInputContext class.
|
|
|
|
InterfaceTypeList supportedServices;
|
|
|
|
supportedServices[0] = kTextServiceDocumentInterfaceType;
|
|
|
|
supportedServices[1] = kUnicodeDocumentInterfaceType;
|
|
|
|
::NewTSMDocument(2, supportedServices, &mPluginTSMDoc, 0);
|
|
|
|
// We'll need to use the "input window".
|
|
|
|
::UseInputWindow(mPluginTSMDoc, YES);
|
|
|
|
::ActivateTSMDocument(mPluginTSMDoc);
|
|
|
|
} else if (::TSMGetActiveDocument() != mPluginTSMDoc) {
|
|
|
|
::ActivateTSMDocument(mPluginTSMDoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifndef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
PluginTextInputHandler::HandleKeyDownEventForPlugin(NSEvent* aNativeKeyEvent)
|
|
|
|
{
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mView, "mView must not be NULL");
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifdef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
if ([mView pluginEventModel] != NPEventModelCocoa) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComplexTextInputPanel* ctiPanel =
|
|
|
|
[ComplexTextInputPanel sharedComplexTextInputPanel];
|
|
|
|
|
|
|
|
// If a composition is in progress then simply let the input panel continue
|
|
|
|
// it.
|
|
|
|
if (IsInPluginComposition()) {
|
|
|
|
// Don't send key up events for key downs associated with compositions.
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
NSString* textString = nil;
|
|
|
|
[ctiPanel interpretKeyEvent:aNativeKeyEvent string:&textString];
|
|
|
|
if (textString) {
|
|
|
|
DispatchCocoaNPAPITextEvent(textString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset complex text input request flag.
|
2011-09-30 17:20:33 -07:00
|
|
|
mPluginComplexTextInputRequested = false;
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
// Send key down event to the plugin.
|
2011-09-30 17:20:33 -07:00
|
|
|
nsPluginEvent pluginEvent(true, NS_PLUGIN_INPUT_EVENT, mWidget);
|
2011-02-23 20:48:12 -08:00
|
|
|
NPCocoaEvent cocoaEvent;
|
|
|
|
ConvertCocoaKeyEventToNPCocoaEvent(aNativeKeyEvent, cocoaEvent);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitPluginEvent(pluginEvent, cocoaEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
DispatchEvent(pluginEvent);
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start complex text composition if requested.
|
|
|
|
if (mPluginComplexTextInputRequested) {
|
|
|
|
// Don't send key up events for key downs associated with compositions.
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
NSString* textString = nil;
|
|
|
|
[ctiPanel interpretKeyEvent:aNativeKeyEvent string:&textString];
|
|
|
|
if (textString) {
|
|
|
|
DispatchCocoaNPAPITextEvent(textString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#else // #ifdef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool wasInComposition = false;
|
2011-02-23 20:48:12 -08:00
|
|
|
if ([mView pluginEventModel] == NPEventModelCocoa) {
|
|
|
|
if (IsInPluginComposition()) {
|
2011-09-30 17:20:33 -07:00
|
|
|
wasInComposition = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
// Don't send key up events for key downs associated with compositions.
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
} else {
|
|
|
|
// Reset complex text input request flag.
|
2011-09-30 17:20:33 -07:00
|
|
|
mPluginComplexTextInputRequested = false;
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
// Send key down event to the plugin.
|
2011-09-30 17:20:33 -07:00
|
|
|
nsPluginEvent pluginEvent(true, NS_PLUGIN_INPUT_EVENT, mWidget);
|
2011-02-23 20:48:12 -08:00
|
|
|
NPCocoaEvent cocoaEvent;
|
|
|
|
ConvertCocoaKeyEventToNPCocoaEvent(aNativeKeyEvent, cocoaEvent);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitPluginEvent(pluginEvent, cocoaEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
DispatchEvent(pluginEvent);
|
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only continue if plugin wants complex text input.
|
|
|
|
if (!mPluginComplexTextInputRequested) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't send key up events for key downs associated with compositions.
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = true;
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't send complex text input to a plugin in Cocoa event mode if
|
|
|
|
// either the Control key or the Command key is pressed -- even if the
|
|
|
|
// plugin has requested it, or we are already in IME composition. This
|
|
|
|
// conforms to our behavior in 64-bit mode and fixes bug 619217.
|
|
|
|
NSUInteger modifierFlags = [aNativeKeyEvent modifierFlags];
|
|
|
|
if ((modifierFlags & NSControlKeyMask) ||
|
|
|
|
(modifierFlags & NSCommandKeyMask)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will take care of all Carbon plugin events and also send Cocoa plugin
|
|
|
|
// text events when NSInputContext is not available (ifndef NP_NO_CARBON).
|
|
|
|
ActivatePluginTSMDocument();
|
|
|
|
|
|
|
|
// We use the active TSM document to pass a pointer to ourselves (the
|
|
|
|
// currently focused ChildView) to PluginKeyEventsHandler(). Because this
|
|
|
|
// pointer is weak, we should retain and release ourselves around the call
|
|
|
|
// to TSMProcessRawKeyEvent().
|
|
|
|
nsAutoRetainCocoaObject kungFuDeathGrip(mView);
|
|
|
|
::TSMSetDocumentProperty(mPluginTSMDoc,
|
|
|
|
kFocusedChildViewTSMDocPropertyTag,
|
|
|
|
sizeof(ChildView *), &mView);
|
|
|
|
::TSMProcessRawKeyEvent([aNativeKeyEvent _eventRef]);
|
|
|
|
::TSMRemoveDocumentProperty(mPluginTSMDoc,
|
|
|
|
kFocusedChildViewTSMDocPropertyTag);
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifdef __LP64__ else
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginTextInputHandler::HandleKeyUpEventForPlugin(NSEvent* aNativeKeyEvent)
|
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
if (mIgnoreNextKeyUpEvent) {
|
2011-09-30 17:20:33 -07:00
|
|
|
mIgnoreNextKeyUpEvent = false;
|
2011-07-20 17:33:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:12 -08:00
|
|
|
if (Destroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mView, "mView must not be NULL");
|
|
|
|
|
|
|
|
NPEventModel eventModel = [mView pluginEventModel];
|
|
|
|
if (eventModel == NPEventModelCocoa) {
|
|
|
|
// Don't send key up events to Cocoa plugins during composition.
|
|
|
|
if (IsInPluginComposition()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsKeyEvent keyupEvent(true, NS_KEY_UP, mWidget);
|
2011-02-23 20:48:12 -08:00
|
|
|
InitKeyEvent(aNativeKeyEvent, keyupEvent);
|
|
|
|
NPCocoaEvent pluginEvent;
|
|
|
|
ConvertCocoaKeyEventToNPCocoaEvent(aNativeKeyEvent, pluginEvent);
|
|
|
|
keyupEvent.pluginEvent = &pluginEvent;
|
|
|
|
DispatchEvent(keyupEvent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-02-23 20:48:12 -08:00
|
|
|
PluginTextInputHandler::IsInPluginComposition()
|
|
|
|
{
|
|
|
|
return
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifdef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
[[ComplexTextInputPanel sharedComplexTextInputPanel] inComposition] != NO;
|
2012-10-15 05:38:34 -07:00
|
|
|
#else // #ifdef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
mPluginTSMInComposition;
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifdef __LP64__ else
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-02-23 20:48:12 -08:00
|
|
|
PluginTextInputHandler::DispatchCocoaNPAPITextEvent(NSString* aString)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
NPCocoaEvent cocoaTextEvent;
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitNPCocoaEvent(&cocoaTextEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
cocoaTextEvent.type = NPCocoaEventTextInput;
|
|
|
|
cocoaTextEvent.data.text.text = (NPNSString*)aString;
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
nsPluginEvent pluginEvent(true, NS_PLUGIN_INPUT_EVENT, mWidget);
|
2011-07-20 17:33:16 -07:00
|
|
|
nsCocoaUtils::InitPluginEvent(pluginEvent, cocoaTextEvent);
|
2011-02-23 20:48:12 -08:00
|
|
|
return DispatchEvent(pluginEvent);
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#ifndef __LP64__
|
2011-02-23 20:48:12 -08:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* PluginTextInputHandler_IMKInputSession_* implementation
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
// IMKInputSession is an undocumented class in the HIToolbox framework. It's
|
|
|
|
// present on both Leopard and SnowLeopard, and is used at a low level to
|
|
|
|
// process IME input regardless of which high-level API is used (Text Services
|
|
|
|
// Manager or Cocoa). It works the same way in both 32-bit and 64-bit code.
|
|
|
|
@interface NSObject (IMKInputSessionMethodSwizzling)
|
|
|
|
- (BOOL)PluginTextInputHandler_IMKInputSession_handleEvent:(EventRef)theEvent;
|
|
|
|
- (void)PluginTextInputHandler_IMKInputSession_commitComposition;
|
|
|
|
- (void)PluginTextInputHandler_IMKInputSession_finishSession;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSObject (IMKInputSessionMethodSwizzling)
|
|
|
|
|
|
|
|
- (BOOL)PluginTextInputHandler_IMKInputSession_handleEvent:(EventRef)theEvent
|
|
|
|
{
|
|
|
|
[self retain];
|
|
|
|
BOOL retval =
|
|
|
|
[self PluginTextInputHandler_IMKInputSession_handleEvent:theEvent];
|
|
|
|
NSUInteger retainCount = [self retainCount];
|
|
|
|
[self release];
|
|
|
|
// Return without doing anything if we've been deleted.
|
|
|
|
if (retainCount == 1) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSWindow *mainWindow = [NSApp mainWindow];
|
|
|
|
NSResponder *firstResponder = [mainWindow firstResponder];
|
|
|
|
if (![firstResponder isKindOfClass:[ChildView class]]) {
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'charactersEntered' is the length (in bytes) of currently "marked text"
|
|
|
|
// -- text that's been entered in IME but not yet committed. If it's
|
|
|
|
// non-zero we're composing text in an IME session; if it's zero we're
|
|
|
|
// not in an IME session.
|
|
|
|
NSInteger entered = 0;
|
|
|
|
object_getInstanceVariable(self, "charactersEntered",
|
|
|
|
(void **) &entered);
|
|
|
|
nsIWidget* widget = [(ChildView*)firstResponder widget];
|
|
|
|
NS_ENSURE_TRUE(widget, retval);
|
|
|
|
TextInputHandler* handler =
|
|
|
|
static_cast<nsChildView*>(widget)->GetTextInputHandler();
|
|
|
|
NS_ENSURE_TRUE(handler, retval);
|
|
|
|
handler->SetPluginTSMInComposition(entered != 0);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is called whenever IME input is committed as a result of an
|
|
|
|
// "abnormal" termination -- for example when changing the keyboard focus from
|
|
|
|
// one input field to another.
|
|
|
|
- (void)PluginTextInputHandler_IMKInputSession_commitComposition
|
|
|
|
{
|
|
|
|
NSWindow *mainWindow = [NSApp mainWindow];
|
|
|
|
NSResponder *firstResponder = [mainWindow firstResponder];
|
|
|
|
if ([firstResponder isKindOfClass:[ChildView class]]) {
|
|
|
|
nsIWidget* widget = [(ChildView*)firstResponder widget];
|
|
|
|
if (widget) {
|
|
|
|
TextInputHandler* handler =
|
|
|
|
static_cast<nsChildView*>(widget)->GetTextInputHandler();
|
|
|
|
if (handler) {
|
2011-09-30 17:20:33 -07:00
|
|
|
handler->SetPluginTSMInComposition(false);
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self PluginTextInputHandler_IMKInputSession_commitComposition];
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is called just before we're deallocated.
|
|
|
|
- (void)PluginTextInputHandler_IMKInputSession_finishSession
|
|
|
|
{
|
|
|
|
NSWindow *mainWindow = [NSApp mainWindow];
|
|
|
|
NSResponder *firstResponder = [mainWindow firstResponder];
|
|
|
|
if ([firstResponder isKindOfClass:[ChildView class]]) {
|
|
|
|
nsIWidget* widget = [(ChildView*)firstResponder widget];
|
|
|
|
if (widget) {
|
|
|
|
TextInputHandler* handler =
|
|
|
|
static_cast<nsChildView*>(widget)->GetTextInputHandler();
|
|
|
|
if (handler) {
|
2011-09-30 17:20:33 -07:00
|
|
|
handler->SetPluginTSMInComposition(false);
|
2011-02-23 20:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self PluginTextInputHandler_IMKInputSession_finishSession];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-10-15 05:38:34 -07:00
|
|
|
#endif // #ifndef __LP64__
|
2011-02-23 10:06:26 -08:00
|
|
|
|
2011-05-08 03:19:15 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* TextInputHandlerBase implementation
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-05-24 09:27:52 -07:00
|
|
|
int32_t TextInputHandlerBase::sSecureEventInputCount = 0;
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
TextInputHandlerBase::TextInputHandlerBase(nsChildView* aWidget,
|
|
|
|
NSView<mozView> *aNativeView) :
|
|
|
|
mWidget(aWidget)
|
2011-05-08 03:19:15 -07:00
|
|
|
{
|
|
|
|
gHandlerInstanceCount++;
|
2011-05-08 03:19:23 -07:00
|
|
|
mView = [aNativeView retain];
|
2011-05-08 03:19:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TextInputHandlerBase::~TextInputHandlerBase()
|
|
|
|
{
|
2011-05-08 03:19:23 -07:00
|
|
|
[mView release];
|
2011-05-08 03:19:15 -07:00
|
|
|
if (--gHandlerInstanceCount == 0) {
|
2012-10-15 18:43:50 -07:00
|
|
|
FinalizeCurrentInputSource();
|
2011-05-08 03:19:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-05-08 03:19:23 -07:00
|
|
|
TextInputHandlerBase::OnDestroyWidget(nsChildView* aDestroyingWidget)
|
2011-05-08 03:19:15 -07:00
|
|
|
{
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandlerBase::OnDestroyWidget, "
|
|
|
|
"aDestroyingWidget=%p, mWidget=%p",
|
|
|
|
this, aDestroyingWidget, mWidget));
|
|
|
|
|
2011-05-08 03:19:23 -07:00
|
|
|
if (aDestroyingWidget != mWidget) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2011-05-08 03:19:15 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mWidget = nullptr;
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2011-05-08 03:19:15 -07:00
|
|
|
}
|
2011-02-23 09:25:11 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TextInputHandlerBase::DispatchEvent(nsGUIEvent& aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent.message == NS_KEY_PRESS) {
|
|
|
|
nsInputEvent& inputEvent = static_cast<nsInputEvent&>(aEvent);
|
2012-04-24 20:00:02 -07:00
|
|
|
if (!inputEvent.IsMeta()) {
|
2011-07-20 17:33:16 -07:00
|
|
|
PR_LOG(gLog, PR_LOG_ALWAYS,
|
|
|
|
("%p TextInputHandlerBase::DispatchEvent, hiding mouse cursor", this));
|
2011-02-23 09:25:11 -08:00
|
|
|
[NSCursor setHiddenUntilMouseMoves:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mWidget->DispatchWindowEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextInputHandlerBase::InitKeyEvent(NSEvent *aNativeKeyEvent,
|
2012-07-20 16:24:48 -07:00
|
|
|
nsKeyEvent& aKeyEvent,
|
|
|
|
const nsAString* aInsertString)
|
2011-02-23 09:25:11 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNativeKeyEvent, "aNativeKeyEvent must not be NULL");
|
|
|
|
|
|
|
|
if (mKeyboardOverride.mOverrideEnabled) {
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper tis;
|
2011-09-30 17:20:33 -07:00
|
|
|
tis.InitByLayoutID(mKeyboardOverride.mKeyboardLayout, true);
|
2012-10-15 18:43:50 -07:00
|
|
|
tis.InitKeyEvent(aNativeKeyEvent, aKeyEvent, aInsertString);
|
|
|
|
return;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
2012-10-15 18:43:50 -07:00
|
|
|
TISInputSourceWrapper::CurrentInputSource().
|
|
|
|
InitKeyEvent(aNativeKeyEvent, aKeyEvent, aInsertString);
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TextInputHandlerBase::SynthesizeNativeKeyEvent(
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNativeKeyboardLayout,
|
|
|
|
int32_t aNativeKeyCode,
|
|
|
|
uint32_t aModifierFlags,
|
2011-02-23 09:25:11 -08:00
|
|
|
const nsAString& aCharacters,
|
|
|
|
const nsAString& aUnmodifiedCharacters)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t sModifierFlagMap[][2] = {
|
2011-02-23 09:25:11 -08:00
|
|
|
{ nsIWidget::CAPS_LOCK, NSAlphaShiftKeyMask },
|
2012-10-18 18:17:07 -07:00
|
|
|
{ nsIWidget::SHIFT_L, NSShiftKeyMask | 0x0002 },
|
|
|
|
{ nsIWidget::SHIFT_R, NSShiftKeyMask | 0x0004 },
|
|
|
|
{ nsIWidget::CTRL_L, NSControlKeyMask | 0x0001 },
|
|
|
|
{ nsIWidget::CTRL_R, NSControlKeyMask | 0x2000 },
|
|
|
|
{ nsIWidget::ALT_L, NSAlternateKeyMask | 0x0020 },
|
|
|
|
{ nsIWidget::ALT_R, NSAlternateKeyMask | 0x0040 },
|
|
|
|
{ nsIWidget::COMMAND_L, NSCommandKeyMask | 0x0008 },
|
|
|
|
{ nsIWidget::COMMAND_R, NSCommandKeyMask | 0x0010 },
|
2011-02-23 09:25:11 -08:00
|
|
|
{ nsIWidget::NUMERIC_KEY_PAD, NSNumericPadKeyMask },
|
|
|
|
{ nsIWidget::HELP, NSHelpKeyMask },
|
|
|
|
{ nsIWidget::FUNCTION, NSFunctionKeyMask }
|
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t modifierFlags = 0;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(sModifierFlagMap); ++i) {
|
2011-02-23 09:25:11 -08:00
|
|
|
if (aModifierFlags & sModifierFlagMap[i][0]) {
|
|
|
|
modifierFlags |= sModifierFlagMap[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSInteger windowNumber = [[mView window] windowNumber];
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sendFlagsChangedEvent = IsModifierKey(aNativeKeyCode);
|
2011-02-23 09:25:11 -08:00
|
|
|
NSEventType eventType = sendFlagsChangedEvent ? NSFlagsChanged : NSKeyDown;
|
|
|
|
NSEvent* downEvent =
|
|
|
|
[NSEvent keyEventWithType:eventType
|
|
|
|
location:NSMakePoint(0,0)
|
|
|
|
modifierFlags:modifierFlags
|
|
|
|
timestamp:0
|
|
|
|
windowNumber:windowNumber
|
|
|
|
context:[NSGraphicsContext currentContext]
|
2011-07-20 17:33:16 -07:00
|
|
|
characters:nsCocoaUtils::ToNSString(aCharacters)
|
|
|
|
charactersIgnoringModifiers:nsCocoaUtils::ToNSString(aUnmodifiedCharacters)
|
2011-02-23 09:25:11 -08:00
|
|
|
isARepeat:NO
|
|
|
|
keyCode:aNativeKeyCode];
|
|
|
|
|
2011-07-20 17:33:16 -07:00
|
|
|
NSEvent* upEvent = sendFlagsChangedEvent ?
|
|
|
|
nil : nsCocoaUtils::MakeNewCocoaEventWithType(NSKeyUp, downEvent);
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
if (downEvent && (sendFlagsChangedEvent || upEvent)) {
|
|
|
|
KeyboardLayoutOverride currentLayout = mKeyboardOverride;
|
|
|
|
mKeyboardOverride.mKeyboardLayout = aNativeKeyboardLayout;
|
2011-09-30 17:20:33 -07:00
|
|
|
mKeyboardOverride.mOverrideEnabled = true;
|
2011-02-23 09:25:11 -08:00
|
|
|
[NSApp sendEvent:downEvent];
|
|
|
|
if (upEvent) {
|
|
|
|
[NSApp sendEvent:upEvent];
|
|
|
|
}
|
|
|
|
// processKeyDownEvent and keyUp block exceptions so we're sure to
|
|
|
|
// reach here to restore mKeyboardOverride
|
|
|
|
mKeyboardOverride = currentLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* static */ bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TextInputHandlerBase::IsPrintableChar(PRUnichar aChar)
|
|
|
|
{
|
|
|
|
return (aChar >= 0x20 && aChar <= 0x7E) || aChar >= 0xA0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* static */ bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TextInputHandlerBase::IsSpecialGeckoKey(UInt32 aNativeKeyCode)
|
|
|
|
{
|
|
|
|
// this table is used to determine which keys are special and should not
|
|
|
|
// generate a charCode
|
|
|
|
switch (aNativeKeyCode) {
|
|
|
|
// modifiers - we don't get separate events for these yet
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Escape:
|
|
|
|
case kVK_Shift:
|
|
|
|
case kVK_RightShift:
|
|
|
|
case kVK_Command:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightCommand:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_CapsLock:
|
|
|
|
case kVK_Control:
|
|
|
|
case kVK_RightControl:
|
|
|
|
case kVK_Option:
|
|
|
|
case kVK_RightOption:
|
|
|
|
case kVK_ANSI_KeypadClear:
|
2012-10-18 18:17:07 -07:00
|
|
|
case kVK_Function:
|
2011-02-23 09:25:11 -08:00
|
|
|
|
|
|
|
// function keys
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_F1:
|
|
|
|
case kVK_F2:
|
|
|
|
case kVK_F3:
|
|
|
|
case kVK_F4:
|
|
|
|
case kVK_F5:
|
|
|
|
case kVK_F6:
|
|
|
|
case kVK_F7:
|
|
|
|
case kVK_F8:
|
|
|
|
case kVK_F9:
|
|
|
|
case kVK_F10:
|
|
|
|
case kVK_F11:
|
|
|
|
case kVK_F12:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Pause:
|
|
|
|
case kVK_PC_ScrollLock:
|
|
|
|
case kVK_PC_PrintScreen:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_F16:
|
|
|
|
case kVK_F17:
|
|
|
|
case kVK_F18:
|
|
|
|
case kVK_F19:
|
|
|
|
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Insert:
|
|
|
|
case kVK_PC_Delete:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Tab:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_PC_Backspace:
|
2012-07-03 22:59:50 -07:00
|
|
|
|
|
|
|
case kVK_JIS_Eisu:
|
|
|
|
case kVK_JIS_Kana:
|
|
|
|
|
|
|
|
case kVK_Home:
|
|
|
|
case kVK_End:
|
|
|
|
case kVK_PageUp:
|
|
|
|
case kVK_PageDown:
|
|
|
|
case kVK_LeftArrow:
|
|
|
|
case kVK_RightArrow:
|
|
|
|
case kVK_UpArrow:
|
|
|
|
case kVK_DownArrow:
|
|
|
|
case kVK_Return:
|
|
|
|
case kVK_ANSI_KeypadEnter:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Powerbook_KeypadEnter:
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* static */ bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TextInputHandlerBase::IsNormalCharInputtingEvent(const nsKeyEvent& aKeyEvent)
|
|
|
|
{
|
|
|
|
// this is not character inputting event, simply.
|
2012-04-24 20:00:02 -07:00
|
|
|
if (!aKeyEvent.isChar || !aKeyEvent.charCode || aKeyEvent.IsMeta()) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
// if this is unicode char inputting event, we don't need to check
|
|
|
|
// ctrl/alt/command keys
|
|
|
|
if (aKeyEvent.charCode > 0x7F) {
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
// ASCII chars should be inputted without ctrl/alt/command keys
|
2012-04-24 20:00:02 -07:00
|
|
|
return !aKeyEvent.IsControl() && !aKeyEvent.IsAlt();
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* static */ bool
|
2011-02-23 09:25:11 -08:00
|
|
|
TextInputHandlerBase::IsModifierKey(UInt32 aNativeKeyCode)
|
|
|
|
{
|
|
|
|
switch (aNativeKeyCode) {
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_CapsLock:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_RightCommand:
|
2012-07-03 22:59:50 -07:00
|
|
|
case kVK_Command:
|
|
|
|
case kVK_Shift:
|
|
|
|
case kVK_Option:
|
|
|
|
case kVK_Control:
|
|
|
|
case kVK_RightShift:
|
|
|
|
case kVK_RightOption:
|
|
|
|
case kVK_RightControl:
|
2012-10-18 18:17:07 -07:00
|
|
|
case kVK_Function:
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2011-02-23 09:25:11 -08:00
|
|
|
}
|
2013-05-24 09:27:52 -07:00
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
TextInputHandlerBase::EnableSecureEventInput()
|
|
|
|
{
|
|
|
|
sSecureEventInputCount++;
|
|
|
|
::EnableSecureEventInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
TextInputHandlerBase::DisableSecureEventInput()
|
|
|
|
{
|
|
|
|
if (!sSecureEventInputCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sSecureEventInputCount--;
|
|
|
|
::DisableSecureEventInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
TextInputHandlerBase::IsSecureEventInputEnabled()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!!sSecureEventInputCount == !!::IsSecureEventInputEnabled(),
|
|
|
|
"Some other process has enabled secure event input");
|
|
|
|
return !!sSecureEventInputCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
TextInputHandlerBase::EnsureSecureEventInputDisabled()
|
|
|
|
{
|
|
|
|
while (sSecureEventInputCount) {
|
|
|
|
TextInputHandlerBase::DisableSecureEventInput();
|
|
|
|
}
|
|
|
|
}
|