| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "webkit/glue/webmenurunner_mac.h" | 5 #include "webkit/glue/webmenurunner_mac.h" |
| 6 | 6 |
| 7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
| 8 | 8 |
| 9 #if !defined(MAC_OS_X_VERSION_10_6) || \ | 9 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
| 10 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 | 10 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 if ([self menuItemWasChosen]) | 160 if ([self menuItemWasChosen]) |
| 161 index_ = [cell indexOfSelectedItem]; | 161 index_ = [cell indexOfSelectedItem]; |
| 162 } | 162 } |
| 163 | 163 |
| 164 - (int)indexOfSelectedItem { | 164 - (int)indexOfSelectedItem { |
| 165 return index_; | 165 return index_; |
| 166 } | 166 } |
| 167 | 167 |
| 168 @end // WebMenuRunner | 168 @end // WebMenuRunner |
| 169 | |
| 170 namespace webkit_glue { | |
| 171 | |
| 172 // Helper function for manufacturing input events to send to WebKit. | |
| 173 NSEvent* EventWithMenuAction(BOOL item_chosen, int window_num, | |
| 174 int item_height, int selected_index, | |
| 175 NSRect menu_bounds, NSRect view_bounds) { | |
| 176 NSEvent* event = nil; | |
| 177 double event_time = (double)(AbsoluteToDuration(UpTime())) / 1000.0; | |
| 178 | |
| 179 if (item_chosen) { | |
| 180 // Construct a mouse up event to simulate the selection of an appropriate | |
| 181 // menu item. | |
| 182 NSPoint click_pos; | |
| 183 click_pos.x = menu_bounds.size.width / 2; | |
| 184 | |
| 185 // This is going to be hard to calculate since the button is painted by | |
| 186 // WebKit, the menu by Cocoa, and we have to translate the selected_item | |
| 187 // index to a coordinate that WebKit's PopupMenu expects which uses a | |
| 188 // different font *and* expects to draw the menu below the button like we do | |
| 189 // on Windows. | |
| 190 // The WebKit popup menu thinks it will draw just below the button, so | |
| 191 // create the click at the offset based on the selected item's index and | |
| 192 // account for the different coordinate system used by NSView. | |
| 193 int item_offset = selected_index * item_height + item_height / 2; | |
| 194 click_pos.y = view_bounds.size.height - item_offset; | |
| 195 event = [NSEvent mouseEventWithType:NSLeftMouseUp | |
| 196 location:click_pos | |
| 197 modifierFlags:0 | |
| 198 timestamp:event_time | |
| 199 windowNumber:window_num | |
| 200 context:nil | |
| 201 eventNumber:0 | |
| 202 clickCount:1 | |
| 203 pressure:1.0]; | |
| 204 } else { | |
| 205 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and | |
| 206 // forward that to WebKit. | |
| 207 NSPoint key_pos; | |
| 208 key_pos.x = 0; | |
| 209 key_pos.y = 0; | |
| 210 NSString* escape_str = [NSString stringWithFormat:@"%c", 0x1B]; | |
| 211 event = [NSEvent keyEventWithType:NSKeyDown | |
| 212 location:key_pos | |
| 213 modifierFlags:0 | |
| 214 timestamp:event_time | |
| 215 windowNumber:window_num | |
| 216 context:nil | |
| 217 characters:@"" | |
| 218 charactersIgnoringModifiers:escape_str | |
| 219 isARepeat:NO | |
| 220 keyCode:0x1B]; | |
| 221 } | |
| 222 | |
| 223 return event; | |
| 224 } | |
| 225 | |
| 226 } // namespace webkit_glue | |
| OLD | NEW |