Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first really basic implementation of key bindings. Look in -[KTextVie…

…w filterInputEvent:] to dig into the code
  • Loading branch information...
commit 233638f58476936b9fab26b0fd7ed740c1e9e9ae 1 parent 93ddda8
Rasmus authored
12 kod.xcodeproj/project.pbxproj
View
@@ -66,6 +66,8 @@
3A6D4D6B1295F0BA00DBA2A3 /* NSCharacterSet-kod.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A6D4D6A1295F0BA00DBA2A3 /* NSCharacterSet-kod.m */; };
3A74A4D3129FF6E000BB5503 /* HUTF8MappedUTF16String.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3A74A4D2129FF6E000BB5503 /* HUTF8MappedUTF16String.mm */; };
3A78955D12B043B400D08FD5 /* hdispatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A78955C12B043B400D08FD5 /* hdispatch.m */; };
+ 3A7D866912F43392005740FF /* KWindow.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3A7D866812F43392005740FF /* KWindow.mm */; };
+ 3A7D86AE12F45EF6005740FF /* KInputBindings.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3A7D86AD12F45EF6005740FF /* KInputBindings.mm */; };
3A7E71131286EAE800D85E4C /* BrowserWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3A7E71121286EAE800D85E4C /* BrowserWindow.xib */; };
3A7E71801286F44D00D85E4C /* KFileOutlineView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3A7E717F1286F44D00D85E4C /* KFileOutlineView.mm */; };
3A7E71831286F54A00D85E4C /* KFileTextFieldCell.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3A7E71821286F54A00D85E4C /* KFileTextFieldCell.mm */; };
@@ -349,6 +351,10 @@
3A74A4D1129FF6E000BB5503 /* HUTF8MappedUTF16String.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HUTF8MappedUTF16String.h; sourceTree = "<group>"; };
3A74A4D2129FF6E000BB5503 /* HUTF8MappedUTF16String.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = HUTF8MappedUTF16String.mm; sourceTree = "<group>"; };
3A78955C12B043B400D08FD5 /* hdispatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = hdispatch.m; sourceTree = "<group>"; };
+ 3A7D866712F43392005740FF /* KWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWindow.h; sourceTree = "<group>"; };
+ 3A7D866812F43392005740FF /* KWindow.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KWindow.mm; sourceTree = "<group>"; };
+ 3A7D86AC12F45EF6005740FF /* KInputBindings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KInputBindings.h; sourceTree = "<group>"; };
+ 3A7D86AD12F45EF6005740FF /* KInputBindings.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KInputBindings.mm; sourceTree = "<group>"; };
3A7E71121286EAE800D85E4C /* BrowserWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = BrowserWindow.xib; sourceTree = "<group>"; };
3A7E717E1286F44D00D85E4C /* KFileOutlineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KFileOutlineView.h; sourceTree = "<group>"; };
3A7E717F1286F44D00D85E4C /* KFileOutlineView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KFileOutlineView.mm; sourceTree = "<group>"; };
@@ -927,6 +933,10 @@
3ABF539712A5D5360020233C /* Main views */ = {
isa = PBXGroup;
children = (
+ 3A7D86AC12F45EF6005740FF /* KInputBindings.h */,
+ 3A7D86AD12F45EF6005740FF /* KInputBindings.mm */,
+ 3A7D866712F43392005740FF /* KWindow.h */,
+ 3A7D866812F43392005740FF /* KWindow.mm */,
3ABF539412A5D52F0020233C /* KSplitView.h */,
3ABF539512A5D52F0020233C /* KSplitView.m */,
3A7E717C1286F40600D85E4C /* Sidebar */,
@@ -1361,6 +1371,8 @@
3A37B54712DA48E500ED811C /* KASTOutlineView.m in Sources */,
3AC866C412DE69360077673C /* KNodeParseEntry.mm in Sources */,
3A1A4A8712E0A6EF00887625 /* KWindowBackgroundCoverView.m in Sources */,
+ 3A7D866912F43392005740FF /* KWindow.mm in Sources */,
+ 3A7D86AE12F45EF6005740FF /* KInputBindings.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
25 resources/BrowserWindow.xib
View
@@ -2,17 +2,17 @@
<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1060</int>
- <string key="IBDocument.SystemVersion">10H574</string>
- <string key="IBDocument.InterfaceBuilderVersion">804</string>
+ <string key="IBDocument.SystemVersion">10J567</string>
+ <string key="IBDocument.InterfaceBuilderVersion">823</string>
<string key="IBDocument.AppKitVersion">1038.35</string>
- <string key="IBDocument.HIToolboxVersion">461.00</string>
+ <string key="IBDocument.HIToolboxVersion">462.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string key="NS.object.0">804</string>
+ <string key="NS.object.0">823</string>
</object>
<array class="NSMutableArray" key="IBDocument.EditedObjectIDs">
- <integer value="2"/>
<integer value="56"/>
+ <integer value="2"/>
</array>
<array key="IBDocument.PluginDependencies">
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -34,7 +34,7 @@
<string key="NSWindowRect">{{60, 229}, {750, 600}}</string>
<int key="NSWTFlags">536872960</int>
<string key="NSWindowTitle"/>
- <string key="NSWindowClass">CTBrowserWindow</string>
+ <string key="NSWindowClass">KWindow</string>
<nil key="NSViewClass"/>
<string key="NSWindowContentMaxSize">{1.79769e+308, 1.79769e+308}</string>
<string key="NSWindowContentMinSize">{400, 250}</string>
@@ -938,31 +938,32 @@
</object>
</object>
<object class="IBPartialClassDescription">
- <string key="className">NSApplication</string>
+ <string key="className">KWindow</string>
+ <string key="superclassName">CTBrowserWindow</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
- <string key="minorKey">deps/bwtoolkit/NSApplication+BWAdditions.h</string>
+ <string key="minorKey">src/KWindow.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
- <string key="className">NSObject</string>
+ <string key="className">NSApplication</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
- <string key="minorKey">deps/hunch-cocoa/HEventEmitter.h</string>
+ <string key="minorKey">deps/bwtoolkit/NSApplication+BWAdditions.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">NSObject</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
- <string key="minorKey">deps/json-framework/Classes/NSObject+SBJSON.h</string>
+ <string key="minorKey">deps/hunch-cocoa/HEventEmitter.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">NSObject</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
- <string key="minorKey">deps/json-framework/Classes/SBProxyForJson.h</string>
+ <string key="minorKey">src/knode_ns_additions.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
55 src/KInputBindings.h
View
@@ -0,0 +1,55 @@
+#include <map>
+#include <string>
+#include <tr1/memory>
+#include "HUnorderedMap.h"
+
+class KInputAction;
+typedef std::tr1::shared_ptr<KInputAction> KInputActionPtr;
+
+class KInputAction {
+ public:
+ virtual BOOL perform(id sender) = 0;
+};
+
+class KSelectorInputAction : public KInputAction {
+ public:
+ KSelectorInputAction(SEL selector, id target=nil) {
+ selector_ = selector;
+ target_ = [target retain];
+ }
+ ~KSelectorInputAction() {
+ [target_ release];
+ }
+ BOOL perform(id sender) {
+ if (!target_ && [sender respondsToSelector:selector_]) {
+ [sender performSelector:selector_];
+ return YES;
+ } else if (target_ && [target_ respondsToSelector:selector_]) {
+ [sender performSelector:selector_ withObject:sender];
+ return YES;
+ }
+ return NO;
+ }
+ protected:
+ SEL selector_;
+ id target_;
+};
+
+class KInputBindings {
+ public:
+ static void set(std::string seq, KInputAction *action) {
+ bindings_.putSync(seq, action);
+ }
+ static void set(NSString *seq, KInputAction *action) {
+ set(std::string([seq UTF8String]), action);
+ }
+
+ static KInputAction *get(std::string seq) {
+ return bindings_.getSync(seq);
+ }
+
+ static KInputAction *get(NSEvent *event);
+
+ protected:
+ static HUnorderedMapSharedPtr<std::string, KInputAction> bindings_;
+};
124 src/KInputBindings.mm
View
@@ -0,0 +1,124 @@
+#import "KInputBindings.h"
+#import "common.h"
+
+HUnorderedMapSharedPtr<std::string, KInputAction> KInputBindings::bindings_;
+
+
+KInputAction *KInputBindings::get(NSEvent *event) {
+ // we only handle key events (for now)
+ NSEventType t = event.type;
+ if (t != NSKeyDown && t != NSKeyUp) return NULL;
+
+ std::string seq;
+ NSUInteger modifiers = [event modifierFlags];
+ NSString *chars = [event charactersIgnoringModifiers];
+ NSString *charsLower = nil;
+ unichar functionKeyChar = 0;
+ // TODO: support more than one key
+
+ if (chars.length != 0) {
+ unichar ch = [chars characterAtIndex:0];
+ if (ch >= 0xF700 && ch <= 0xF8FF) {
+ functionKeyChar = ch;
+ modifiers &= ~NSFunctionKeyMask;
+ } else {
+ chars = [chars substringToIndex:1];
+ charsLower = [chars lowercaseString];
+ if (![chars isEqualToString:charsLower])
+ modifiers |= NSShiftKeyMask;
+ }
+ }
+
+ if (modifiers & NSAlternateKeyMask) seq += "A-";
+ if (modifiers & NSControlKeyMask) seq += "C-";
+ if (modifiers & NSFunctionKeyMask) seq += "F-";
+ if (modifiers & NSHelpKeyMask) seq += "H-";
+ if (modifiers & NSAlphaShiftKeyMask) seq += "L-";
+ if (modifiers & NSCommandKeyMask) seq += "M-";
+ if (modifiers & NSNumericPadKeyMask) seq += "N-";
+ if (modifiers & NSShiftKeyMask) seq += "S-";
+
+ if (charsLower) {
+ seq += [charsLower UTF8String];
+ } else if (functionKeyChar) {
+ switch (functionKeyChar) {
+ case NSUpArrowFunctionKey: seq += "<up>"; break;
+ case NSDownArrowFunctionKey: seq += "<down>"; break;
+ case NSLeftArrowFunctionKey: seq += "<left>"; break;
+ case NSRightArrowFunctionKey: seq += "<right>"; break;
+ case NSF1FunctionKey: seq += "<f1>"; break;
+ case NSF2FunctionKey: seq += "<f2>"; break;
+ case NSF3FunctionKey: seq += "<f3>"; break;
+ case NSF4FunctionKey: seq += "<f4>"; break;
+ case NSF5FunctionKey: seq += "<f5>"; break;
+ case NSF6FunctionKey: seq += "<f6>"; break;
+ case NSF7FunctionKey: seq += "<f7>"; break;
+ case NSF8FunctionKey: seq += "<f8>"; break;
+ case NSF9FunctionKey: seq += "<f9>"; break;
+ case NSF10FunctionKey: seq += "<f10>"; break;
+ case NSF11FunctionKey: seq += "<f11>"; break;
+ case NSF12FunctionKey: seq += "<f12>"; break;
+ case NSF13FunctionKey: seq += "<f13>"; break;
+ case NSF14FunctionKey: seq += "<f14>"; break;
+ case NSF15FunctionKey: seq += "<f15>"; break;
+ case NSF16FunctionKey: seq += "<f16>"; break;
+ case NSF17FunctionKey: seq += "<f17>"; break;
+ case NSF18FunctionKey: seq += "<f18>"; break;
+ case NSF19FunctionKey: seq += "<f19>"; break;
+ case NSF20FunctionKey: seq += "<f20>"; break;
+ case NSF21FunctionKey: seq += "<f21>"; break;
+ case NSF22FunctionKey: seq += "<f22>"; break;
+ case NSF23FunctionKey: seq += "<f23>"; break;
+ case NSF24FunctionKey: seq += "<f24>"; break;
+ case NSF25FunctionKey: seq += "<f25>"; break;
+ case NSF26FunctionKey: seq += "<f26>"; break;
+ case NSF27FunctionKey: seq += "<f27>"; break;
+ case NSF28FunctionKey: seq += "<f28>"; break;
+ case NSF29FunctionKey: seq += "<f29>"; break;
+ case NSF30FunctionKey: seq += "<f30>"; break;
+ case NSF31FunctionKey: seq += "<f31>"; break;
+ case NSF32FunctionKey: seq += "<f32>"; break;
+ case NSF33FunctionKey: seq += "<f33>"; break;
+ case NSF34FunctionKey: seq += "<f34>"; break;
+ case NSF35FunctionKey: seq += "<f35>"; break;
+ case NSInsertFunctionKey: seq += "<insert>"; break;
+ case NSDeleteFunctionKey: seq += "<del>"; break;
+ case NSHomeFunctionKey: seq += "<home>"; break;
+ case NSBeginFunctionKey: seq += "<begin>"; break;
+ case NSEndFunctionKey: seq += "<end>"; break;
+ case NSPageUpFunctionKey: seq += "<pageup>"; break;
+ case NSPageDownFunctionKey: seq += "<pagedown>"; break;
+ case NSPrintScreenFunctionKey: seq += "<printscreen>"; break;
+ case NSScrollLockFunctionKey: seq += "<scrollock>"; break;
+ case NSPauseFunctionKey: seq += "<pause>"; break;
+ case NSSysReqFunctionKey: seq += "<sysreq>"; break;
+ case NSBreakFunctionKey: seq += "<break>"; break;
+ case NSResetFunctionKey: seq += "<reset>"; break;
+ case NSStopFunctionKey: seq += "<stop>"; break;
+ case NSMenuFunctionKey: seq += "<menu>"; break;
+ case NSUserFunctionKey: seq += "<user>"; break;
+ case NSSystemFunctionKey: seq += "<system>"; break;
+ case NSPrintFunctionKey: seq += "<print>"; break;
+ case NSClearLineFunctionKey: seq += "<clearline>"; break;
+ case NSClearDisplayFunctionKey: seq += "<cleardisplay>"; break;
+ case NSInsertLineFunctionKey: seq += "<insertline>"; break;
+ case NSDeleteLineFunctionKey: seq += "<deleteline>"; break;
+ case NSInsertCharFunctionKey: seq += "<insertchar>"; break;
+ case NSDeleteCharFunctionKey: seq += "<deletechar>"; break;
+ case NSPrevFunctionKey: seq += "<prev>"; break;
+ case NSNextFunctionKey: seq += "<next>"; break;
+ case NSSelectFunctionKey: seq += "<select>"; break;
+ case NSExecuteFunctionKey: seq += "<execute>"; break;
+ case NSUndoFunctionKey: seq += "<undo>"; break;
+ case NSRedoFunctionKey: seq += "<redo>"; break;
+ case NSFindFunctionKey: seq += "<find>"; break;
+ case NSHelpFunctionKey: seq += "<help>"; break;
+ case NSModeSwitchFunctionKey: seq += "<modeswitch>"; break;
+ default: break;
+ }
+ }
+
+ DLOG("seq: '%s'", seq.c_str());
+
+ return get(seq);
+}
23 src/KTextView.mm
View
@@ -10,6 +10,7 @@
#import "HEventEmitter.h"
#import "KWordDictionary.h"
#import "KStyle.h"
+#import "KInputBindings.h"
#import "virtual_key_codes.h"
#import "kconf.h"
#import "common.h"
@@ -362,7 +363,29 @@ - (void)mouseMoved:(NSEvent*)event {
#pragma mark Keyboard events
+// return nil for "I handled this"
+- (NSEvent*)filterInputEvent:(NSEvent*)event {
+ DLOG("input event: %@", event);
+
+ static BOOL debugDidSet = NO;
+ if (!debugDidSet) {
+ debugDidSet = YES;
+ KInputBindings::set("A-r",
+ new KSelectorInputAction(@selector(increaseIndentation)));
+ }
+ KInputAction *action = KInputBindings::get(event);
+ DLOG("input action -> %p", action);
+ if (action && action->perform(self))
+ return nil;
+
+ return event;
+}
+
+
- (void)keyDown:(NSEvent*)event {
+ event = [self filterInputEvent:event];
+ if (!event) return;
+
unsigned short keyCode = event.keyCode;
if (keyCode == kVK_Tab && tabControlsIndentationLevel_) {
NSUInteger modifiers = [event modifierFlags];
10 src/KWindow.h
View
@@ -0,0 +1,10 @@
+// Copyright (c) 2010-2011, Rasmus Andersson. All rights reserved.
+// Use of this source code is governed by a MIT-style license that can be
+// found in the LICENSE file.
+
+#import <ChromiumTabs/CTBrowserWindow.h>
+
+@interface KWindow : CTBrowserWindow {
+}
+
+@end
15 src/KWindow.mm
View
@@ -0,0 +1,15 @@
+// Copyright (c) 2010-2011, Rasmus Andersson. All rights reserved.
+// Use of this source code is governed by a MIT-style license that can be
+// found in the LICENSE file.
+
+#import "KWindow.h"
+#import "common.h"
+
+@implementation KWindow
+
+/*- (BOOL)performKeyEquivalent:(NSEvent*)event {
+ DLOG("[win] performKeyEquivalent:%@", event);
+ return [super performKeyEquivalent:event];
+}*/
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.