| OLD | NEW |
| 1 // Copyright (c), 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c), 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/cocoa/rwhvm_editcommand_helper.h" | 5 #import "chrome/browser/cocoa/rwhvm_editcommand_helper.h" |
| 6 | 6 |
| 7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
| 8 | 8 |
| 9 #include "chrome/browser/renderer_host/render_widget_host.h" | 9 #include "chrome/browser/renderer_host/render_widget_host.h" |
| 10 #import "chrome/browser/renderer_host/render_widget_host_view_mac.h" | 10 #import "chrome/browser/renderer_host/render_widget_host_view_mac.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 "showGuessPanel", | 98 "showGuessPanel", |
| 99 "subscript", | 99 "subscript", |
| 100 "superscript", | 100 "superscript", |
| 101 "swapWithMark", | 101 "swapWithMark", |
| 102 "transpose", | 102 "transpose", |
| 103 "underline", | 103 "underline", |
| 104 "unscript", | 104 "unscript", |
| 105 "yank", | 105 "yank", |
| 106 "yankAndSelect"}; | 106 "yankAndSelect"}; |
| 107 | 107 |
| 108 // Maps an objc-selector to a core command name. | |
| 109 // | |
| 110 // Returns the core command name (which is the selector name with the trailing | |
| 111 // ':' stripped in most cases). | |
| 112 // | |
| 113 // Adapted from a function by the same name in | |
| 114 // WebKit/mac/WebView/WebHTMLView.mm . | |
| 115 // Capitalized names are returned from this function, but that's simply | |
| 116 // matching WebHTMLView.mm. | |
| 117 NSString* CommandNameForSelector(SEL selector) { | |
| 118 if (selector == @selector(insertParagraphSeparator:) || | |
| 119 selector == @selector(insertNewlineIgnoringFieldEditor:)) | |
| 120 return @"InsertNewline"; | |
| 121 if (selector == @selector(insertTabIgnoringFieldEditor:)) | |
| 122 return @"InsertTab"; | |
| 123 if (selector == @selector(pageDown:)) | |
| 124 return @"MovePageDown"; | |
| 125 if (selector == @selector(pageDownAndModifySelection:)) | |
| 126 return @"MovePageDownAndModifySelection"; | |
| 127 if (selector == @selector(pageUp:)) | |
| 128 return @"MovePageUp"; | |
| 129 if (selector == @selector(pageUpAndModifySelection:)) | |
| 130 return @"MovePageUpAndModifySelection"; | |
| 131 | |
| 132 // Remove the trailing colon. | |
| 133 NSString* selector_str = NSStringFromSelector(selector); | |
| 134 int selector_len = [selector_str length]; | |
| 135 return [selector_str substringToIndex:selector_len - 1]; | |
| 136 } | |
| 137 | 108 |
| 138 // This function is installed via the objc runtime as the implementation of all | 109 // This function is installed via the objc runtime as the implementation of all |
| 139 // the various editing selectors. | 110 // the various editing selectors. |
| 140 // The objc runtime hookup occurs in | 111 // The objc runtime hookup occurs in |
| 141 // RWHVMEditCommandHelper::AddEditingSelectorsToClass(). | 112 // RWHVMEditCommandHelper::AddEditingSelectorsToClass(). |
| 142 // | 113 // |
| 143 // self - the object we're attached to; it must implement the | 114 // self - the object we're attached to; it must implement the |
| 144 // RenderWidgetHostViewMacOwner protocol. | 115 // RenderWidgetHostViewMacOwner protocol. |
| 145 // _cmd - the selector that fired. | 116 // _cmd - the selector that fired. |
| 146 // sender - the id of the object that sent the message. | 117 // sender - the id of the object that sent the message. |
| 147 // | 118 // |
| 148 // The selector is translated into an edit comand and then forwarded down the | 119 // The selector is translated into an edit comand and then forwarded down the |
| 149 // pipeline to WebCore. | 120 // pipeline to WebCore. |
| 150 // The route the message takes is: | 121 // The route the message takes is: |
| 151 // RenderWidgetHostViewMac -> RenderViewHost -> | 122 // RenderWidgetHostViewMac -> RenderViewHost -> |
| 152 // | IPC | -> | 123 // | IPC | -> |
| 153 // RenderView -> currently focused WebFrame. | 124 // RenderView -> currently focused WebFrame. |
| 154 // The WebFrame is in the Chrome glue layer and forwards the message to WebCore. | 125 // The WebFrame is in the Chrome glue layer and forwards the message to WebCore. |
| 155 void EditCommandImp(id self, SEL _cmd, id sender) { | 126 void EditCommandImp(id self, SEL _cmd, id sender) { |
| 156 // Make sure |self| is the right type. | 127 // Make sure |self| is the right type. |
| 157 DCHECK([self conformsToProtocol:@protocol(RenderWidgetHostViewMacOwner)]); | 128 DCHECK([self conformsToProtocol:@protocol(RenderWidgetHostViewMacOwner)]); |
| 158 | 129 |
| 159 // SEL -> command name string. | 130 // SEL -> command name string. |
| 160 NSString* command_name_ns = CommandNameForSelector(_cmd); | 131 NSString* command_name_ns = |
| 132 RWHVMEditCommandHelper::CommandNameForSelector(_cmd); |
| 161 std::string edit_command([command_name_ns UTF8String]); | 133 std::string edit_command([command_name_ns UTF8String]); |
| 162 | 134 |
| 163 // Forward the edit command string down the pipeline. | 135 // Forward the edit command string down the pipeline. |
| 164 RenderWidgetHostViewMac* rwhv = [(id<RenderWidgetHostViewMacOwner>)self | 136 RenderWidgetHostViewMac* rwhv = [(id<RenderWidgetHostViewMacOwner>)self |
| 165 renderWidgetHostViewMac]; | 137 renderWidgetHostViewMac]; |
| 166 DCHECK(rwhv); | 138 DCHECK(rwhv); |
| 167 | 139 |
| 168 // The second parameter is the core command value which isn't used here. | 140 // The second parameter is the core command value which isn't used here. |
| 169 rwhv->GetRenderWidgetHost()->ForwardEditCommand(edit_command, ""); | 141 rwhv->GetRenderWidgetHost()->ForwardEditCommand(edit_command, ""); |
| 170 } | 142 } |
| 171 | 143 |
| 172 } // namespace | 144 } // namespace |
| 173 | 145 |
| 146 // Maps an objc-selector to a core command name. |
| 147 // |
| 148 // Returns the core command name (which is the selector name with the trailing |
| 149 // ':' stripped in most cases). |
| 150 // |
| 151 // Adapted from a function by the same name in |
| 152 // WebKit/mac/WebView/WebHTMLView.mm . |
| 153 // Capitalized names are returned from this function, but that's simply |
| 154 // matching WebHTMLView.mm. |
| 155 NSString* RWHVMEditCommandHelper::CommandNameForSelector(SEL selector) { |
| 156 if (selector == @selector(insertParagraphSeparator:) || |
| 157 selector == @selector(insertNewlineIgnoringFieldEditor:)) |
| 158 return @"InsertNewline"; |
| 159 if (selector == @selector(insertTabIgnoringFieldEditor:)) |
| 160 return @"InsertTab"; |
| 161 if (selector == @selector(pageDown:)) |
| 162 return @"MovePageDown"; |
| 163 if (selector == @selector(pageDownAndModifySelection:)) |
| 164 return @"MovePageDownAndModifySelection"; |
| 165 if (selector == @selector(pageUp:)) |
| 166 return @"MovePageUp"; |
| 167 if (selector == @selector(pageUpAndModifySelection:)) |
| 168 return @"MovePageUpAndModifySelection"; |
| 169 |
| 170 // Remove the trailing colon. |
| 171 NSString* selector_str = NSStringFromSelector(selector); |
| 172 int selector_len = [selector_str length]; |
| 173 return [selector_str substringToIndex:selector_len - 1]; |
| 174 } |
| 175 |
| 174 RWHVMEditCommandHelper::RWHVMEditCommandHelper() { | 176 RWHVMEditCommandHelper::RWHVMEditCommandHelper() { |
| 175 for (size_t i = 0; i < arraysize(kEditCommands); ++i) { | 177 for (size_t i = 0; i < arraysize(kEditCommands); ++i) { |
| 176 edit_command_set_.insert(kEditCommands[i]); | 178 edit_command_set_.insert(kEditCommands[i]); |
| 177 } | 179 } |
| 178 } | 180 } |
| 179 | 181 |
| 180 // Dynamically adds Selectors to the aformentioned class. | 182 // Dynamically adds Selectors to the aformentioned class. |
| 181 void RWHVMEditCommandHelper::AddEditingSelectorsToClass(Class klass) { | 183 void RWHVMEditCommandHelper::AddEditingSelectorsToClass(Class klass) { |
| 182 for (size_t i = 0; i < arraysize(kEditCommands); ++i) { | 184 for (size_t i = 0; i < arraysize(kEditCommands); ++i) { |
| 183 // Append trailing ':' to command name to get selector name. | 185 // Append trailing ':' to command name to get selector name. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 NSArray* RWHVMEditCommandHelper::GetEditSelectorNames() { | 218 NSArray* RWHVMEditCommandHelper::GetEditSelectorNames() { |
| 217 size_t num_edit_commands = arraysize(kEditCommands); | 219 size_t num_edit_commands = arraysize(kEditCommands); |
| 218 NSMutableArray* ret = [NSMutableArray arrayWithCapacity:num_edit_commands]; | 220 NSMutableArray* ret = [NSMutableArray arrayWithCapacity:num_edit_commands]; |
| 219 | 221 |
| 220 for (size_t i = 0; i < num_edit_commands; ++i) { | 222 for (size_t i = 0; i < num_edit_commands; ++i) { |
| 221 [ret addObject:[NSString stringWithUTF8String:kEditCommands[i]]]; | 223 [ret addObject:[NSString stringWithUTF8String:kEditCommands[i]]]; |
| 222 } | 224 } |
| 223 | 225 |
| 224 return ret; | 226 return ret; |
| 225 } | 227 } |
| OLD | NEW |