2013-07-10 18:08:55 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "NativeKeyBindings.h"
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsCocoaUtils.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2014-03-14 17:13:32 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
PRLogModuleInfo* gNativeKeyBindingsLog = nullptr;
|
|
|
|
|
2014-03-14 17:13:31 +04:00
|
|
|
NativeKeyBindings* NativeKeyBindings::sInstanceForSingleLineEditor = nullptr;
|
|
|
|
NativeKeyBindings* NativeKeyBindings::sInstanceForMultiLineEditor = nullptr;
|
|
|
|
|
|
|
|
// static
|
2014-03-14 17:13:32 +04:00
|
|
|
NativeKeyBindings*
|
2014-03-14 17:13:31 +04:00
|
|
|
NativeKeyBindings::GetInstance(NativeKeyBindingsType aType)
|
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case nsIWidget::NativeKeyBindingsForSingleLineEditor:
|
|
|
|
if (!sInstanceForSingleLineEditor) {
|
2014-03-14 17:13:32 +04:00
|
|
|
sInstanceForSingleLineEditor = new NativeKeyBindings();
|
2014-03-14 17:13:31 +04:00
|
|
|
sInstanceForSingleLineEditor->Init(aType);
|
|
|
|
}
|
|
|
|
return sInstanceForSingleLineEditor;
|
|
|
|
case nsIWidget::NativeKeyBindingsForMultiLineEditor:
|
|
|
|
case nsIWidget::NativeKeyBindingsForRichTextEditor:
|
|
|
|
if (!sInstanceForMultiLineEditor) {
|
2014-03-14 17:13:32 +04:00
|
|
|
sInstanceForMultiLineEditor = new NativeKeyBindings();
|
2014-03-14 17:13:31 +04:00
|
|
|
sInstanceForMultiLineEditor->Init(aType);
|
|
|
|
}
|
|
|
|
return sInstanceForMultiLineEditor;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Not implemented");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
NativeKeyBindings::Shutdown()
|
|
|
|
{
|
2014-03-14 17:13:32 +04:00
|
|
|
delete sInstanceForSingleLineEditor;
|
|
|
|
sInstanceForSingleLineEditor = nullptr;
|
|
|
|
delete sInstanceForMultiLineEditor;
|
|
|
|
sInstanceForMultiLineEditor = nullptr;
|
2014-03-14 17:13:31 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 18:08:55 +04:00
|
|
|
NativeKeyBindings::NativeKeyBindings()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SEL_TO_COMMAND(aSel, aCommand) \
|
|
|
|
mSelectorToCommand.Put( \
|
|
|
|
reinterpret_cast<struct objc_selector *>(@selector(aSel)), aCommand)
|
|
|
|
|
2014-03-14 17:13:32 +04:00
|
|
|
void
|
2013-07-10 18:08:55 +04:00
|
|
|
NativeKeyBindings::Init(NativeKeyBindingsType aType)
|
|
|
|
{
|
|
|
|
if (!gNativeKeyBindingsLog) {
|
|
|
|
gNativeKeyBindingsLog = PR_NewLogModule("NativeKeyBindings");
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::Init", this));
|
|
|
|
|
|
|
|
// Many selectors have a one-to-one mapping to a Gecko command. Those mappings
|
|
|
|
// are registered in mSelectorToCommand.
|
|
|
|
|
|
|
|
// Selectors from NSResponder's "Responding to Action Messages" section and
|
|
|
|
// from NSText's "Action Methods for Editing" section
|
|
|
|
|
|
|
|
// TODO: Improves correctness of left / right meaning
|
|
|
|
// TODO: Add real paragraph motions
|
|
|
|
|
|
|
|
// SEL_TO_COMMAND(cancelOperation:, );
|
|
|
|
// SEL_TO_COMMAND(capitalizeWord:, );
|
|
|
|
// SEL_TO_COMMAND(centerSelectionInVisibleArea:, );
|
|
|
|
// SEL_TO_COMMAND(changeCaseOfLetter:, );
|
|
|
|
// SEL_TO_COMMAND(complete:, );
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(copy:, CommandCopy);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(copyFont:, );
|
|
|
|
// SEL_TO_COMMAND(copyRuler:, );
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(cut:, CommandCut);
|
|
|
|
SEL_TO_COMMAND(delete:, CommandDelete);
|
|
|
|
SEL_TO_COMMAND(deleteBackward:, CommandDeleteCharBackward);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(deleteBackwardByDecomposingPreviousCharacter:, );
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(deleteForward:, CommandDeleteCharForward);
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
// TODO: deleteTo* selectors are also supposed to add text to a kill buffer
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(deleteToBeginningOfLine:, CommandDeleteToBeginningOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToBeginningOfParagraph:, CommandDeleteToBeginningOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToEndOfLine:, CommandDeleteToEndOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToEndOfParagraph:, CommandDeleteToEndOfLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(deleteToMark:, );
|
|
|
|
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(deleteWordBackward:, CommandDeleteWordBackward);
|
|
|
|
SEL_TO_COMMAND(deleteWordForward:, CommandDeleteWordForward);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(indent:, );
|
|
|
|
// SEL_TO_COMMAND(insertBacktab:, );
|
|
|
|
// SEL_TO_COMMAND(insertContainerBreak:, );
|
|
|
|
// SEL_TO_COMMAND(insertLineBreak:, );
|
|
|
|
// SEL_TO_COMMAND(insertNewline:, );
|
|
|
|
// SEL_TO_COMMAND(insertNewlineIgnoringFieldEditor:, );
|
|
|
|
// SEL_TO_COMMAND(insertParagraphSeparator:, );
|
|
|
|
// SEL_TO_COMMAND(insertTab:, );
|
|
|
|
// SEL_TO_COMMAND(insertTabIgnoringFieldEditor:, );
|
|
|
|
// SEL_TO_COMMAND(insertDoubleQuoteIgnoringSubstitution:, );
|
|
|
|
// SEL_TO_COMMAND(insertSingleQuoteIgnoringSubstitution:, );
|
|
|
|
// SEL_TO_COMMAND(lowercaseWord:, );
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveBackward:, CommandCharPrevious);
|
|
|
|
SEL_TO_COMMAND(moveBackwardAndModifySelection:, CommandSelectCharPrevious);
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveDown:, CommandEndLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else {
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveDown:, CommandLineNext);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveDownAndModifySelection:, CommandSelectLineNext);
|
|
|
|
SEL_TO_COMMAND(moveForward:, CommandCharNext);
|
|
|
|
SEL_TO_COMMAND(moveForwardAndModifySelection:, CommandSelectCharNext);
|
|
|
|
SEL_TO_COMMAND(moveLeft:, CommandCharPrevious);
|
|
|
|
SEL_TO_COMMAND(moveLeftAndModifySelection:, CommandSelectCharPrevious);
|
2013-07-18 05:00:26 +04:00
|
|
|
SEL_TO_COMMAND(moveParagraphBackwardAndModifySelection:,
|
2014-03-14 17:13:30 +04:00
|
|
|
CommandSelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveParagraphForwardAndModifySelection:, CommandSelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveRight:, CommandCharNext);
|
|
|
|
SEL_TO_COMMAND(moveRightAndModifySelection:, CommandSelectCharNext);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfDocument:, CommandMoveTop);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfDocumentAndModifySelection:,
|
|
|
|
CommandSelectTop);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfLine:, CommandBeginLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
SEL_TO_COMMAND(moveToBeginningOfLineAndModifySelection:,
|
2014-03-14 17:13:30 +04:00
|
|
|
CommandSelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfParagraph:, CommandBeginLine);
|
2013-07-18 05:00:26 +04:00
|
|
|
SEL_TO_COMMAND(moveToBeginningOfParagraphAndModifySelection:,
|
2014-03-14 17:13:30 +04:00
|
|
|
CommandSelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfDocument:, CommandMoveBottom);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfDocumentAndModifySelection:, CommandSelectBottom);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfLine:, CommandEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfLineAndModifySelection:, CommandSelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfParagraph:, CommandEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfParagraphAndModifySelection:, CommandSelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToLeftEndOfLine:, CommandBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToLeftEndOfLineAndModifySelection:,
|
|
|
|
CommandSelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToRightEndOfLine:, CommandEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToRightEndOfLineAndModifySelection:, CommandSelectEndLine);
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveUp:, CommandBeginLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else {
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveUp:, CommandLinePrevious);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(moveUpAndModifySelection:, CommandSelectLinePrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordBackward:, CommandWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordBackwardAndModifySelection:,
|
|
|
|
CommandSelectWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordForward:, CommandWordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordForwardAndModifySelection:, CommandSelectWordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordLeft:, CommandWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordLeftAndModifySelection:, CommandSelectWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordRight:, CommandWordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordRightAndModifySelection:, CommandSelectWordNext);
|
|
|
|
SEL_TO_COMMAND(pageDown:, CommandMovePageDown);
|
|
|
|
SEL_TO_COMMAND(pageDownAndModifySelection:, CommandSelectPageDown);
|
|
|
|
SEL_TO_COMMAND(pageUp:, CommandMovePageUp);
|
|
|
|
SEL_TO_COMMAND(pageUpAndModifySelection:, CommandSelectPageUp);
|
|
|
|
SEL_TO_COMMAND(paste:, CommandPaste);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(pasteFont:, );
|
|
|
|
// SEL_TO_COMMAND(pasteRuler:, );
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(scrollLineDown:, CommandScrollLineDown);
|
|
|
|
SEL_TO_COMMAND(scrollLineUp:, CommandScrollLineUp);
|
|
|
|
SEL_TO_COMMAND(scrollPageDown:, CommandScrollPageDown);
|
|
|
|
SEL_TO_COMMAND(scrollPageUp:, CommandScrollPageUp);
|
|
|
|
SEL_TO_COMMAND(scrollToBeginningOfDocument:, CommandScrollTop);
|
|
|
|
SEL_TO_COMMAND(scrollToEndOfDocument:, CommandScrollBottom);
|
|
|
|
SEL_TO_COMMAND(selectAll:, CommandSelectAll);
|
2013-07-10 18:08:55 +04:00
|
|
|
// selectLine: is complex, see KeyDown
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2014-03-14 17:13:30 +04:00
|
|
|
SEL_TO_COMMAND(selectParagraph:, CommandSelectAll);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
// SEL_TO_COMMAND(selectToMark:, );
|
|
|
|
// selectWord: is complex, see KeyDown
|
|
|
|
// SEL_TO_COMMAND(setMark:, );
|
|
|
|
// SEL_TO_COMMAND(showContextHelp:, );
|
|
|
|
// SEL_TO_COMMAND(supplementalTargetForAction:sender:, );
|
|
|
|
// SEL_TO_COMMAND(swapWithMark:, );
|
|
|
|
// SEL_TO_COMMAND(transpose:, );
|
|
|
|
// SEL_TO_COMMAND(transposeWords:, );
|
|
|
|
// SEL_TO_COMMAND(uppercaseWord:, );
|
|
|
|
// SEL_TO_COMMAND(yank:, );
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SEL_TO_COMMAND
|
|
|
|
|
2014-03-14 17:13:32 +04:00
|
|
|
bool
|
|
|
|
NativeKeyBindings::Execute(const WidgetKeyboardEvent& aEvent,
|
|
|
|
DoCommandCallback aCallback,
|
|
|
|
void* aCallbackData)
|
2013-07-10 18:08:55 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress", this));
|
|
|
|
|
|
|
|
// Recover the current event, which should always be the key down we are
|
|
|
|
// responding to.
|
|
|
|
|
2013-08-24 11:24:32 +04:00
|
|
|
NSEvent* cocoaEvent = reinterpret_cast<NSEvent*>(aEvent.mNativeKeyEvent);
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
if (!cocoaEvent || [cocoaEvent type] != NSKeyDown) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, no Cocoa key down event", this));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, interpreting", this));
|
|
|
|
|
|
|
|
nsAutoTArray<KeyBindingsCommand, 2> bindingCommands;
|
|
|
|
nsCocoaUtils::GetCommandsFromKeyEvent(cocoaEvent, bindingCommands);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, bindingCommands=%u",
|
|
|
|
this, bindingCommands.Length()));
|
|
|
|
|
2014-03-14 17:13:30 +04:00
|
|
|
nsAutoTArray<Command, 4> geckoCommands;
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < bindingCommands.Length(); i++) {
|
|
|
|
SEL selector = bindingCommands[i].selector;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(gNativeKeyBindingsLog, LogLevel::Info)) {
|
2013-07-10 18:08:55 +04:00
|
|
|
NSString* selectorString = NSStringFromSelector(selector);
|
|
|
|
nsAutoString nsSelectorString;
|
|
|
|
nsCocoaUtils::GetStringForNSString(selectorString, nsSelectorString);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, selector=%s",
|
2015-05-07 19:43:39 +03:00
|
|
|
this, NS_LossyConvertUTF16toASCII(nsSelectorString).get()));
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a simple mapping in the hashtable
|
2014-03-14 17:13:30 +04:00
|
|
|
Command geckoCommand = static_cast<Command>(mSelectorToCommand.Get(
|
|
|
|
reinterpret_cast<struct objc_selector*>(selector)));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2014-03-14 17:13:30 +04:00
|
|
|
if (geckoCommand) {
|
|
|
|
geckoCommands.AppendElement(geckoCommand);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else if (selector == @selector(selectLine:)) {
|
|
|
|
// This is functional, but Cocoa's version is direction-less in that
|
|
|
|
// selection direction is not determined until some future directed action
|
|
|
|
// is taken. See bug 282097, comment 79 for more details.
|
2014-03-14 17:13:30 +04:00
|
|
|
geckoCommands.AppendElement(CommandBeginLine);
|
|
|
|
geckoCommands.AppendElement(CommandSelectEndLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else if (selector == @selector(selectWord:)) {
|
|
|
|
// This is functional, but Cocoa's version is direction-less in that
|
|
|
|
// selection direction is not determined until some future directed action
|
|
|
|
// is taken. See bug 282097, comment 79 for more details.
|
2014-03-14 17:13:30 +04:00
|
|
|
geckoCommands.AppendElement(CommandWordPrevious);
|
|
|
|
geckoCommands.AppendElement(CommandSelectWordNext);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geckoCommands.IsEmpty()) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, handled=false", this));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < geckoCommands.Length(); i++) {
|
2014-03-14 17:13:30 +04:00
|
|
|
Command geckoCommand = geckoCommands[i];
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, command=%s",
|
2014-11-22 17:39:03 +03:00
|
|
|
this, WidgetKeyboardEvent::GetCommandStr(geckoCommand)));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
// Execute the Gecko command
|
|
|
|
aCallback(geckoCommand, aCallbackData);
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2013-07-10 18:08:55 +04:00
|
|
|
("%p NativeKeyBindings::KeyPress, handled=true", this));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-14 17:13:32 +04:00
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|