WebKit2/WebProcess/WebPage/qt/WebPageQt.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     4  *
       
     5  * Redistribution and use in source and binary forms, with or without
       
     6  * modification, are permitted provided that the following conditions
       
     7  * are met:
       
     8  * 1. Redistributions of source code must retain the above copyright
       
     9  *    notice, this list of conditions and the following disclaimer.
       
    10  * 2. Redistributions in binary form must reproduce the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer in the
       
    12  *    documentation and/or other materials provided with the distribution.
       
    13  *
       
    14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
       
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    24  * THE POSSIBILITY OF SUCH DAMAGE.
       
    25  */
       
    26 
       
    27 #include "WebPage.h"
       
    28 
       
    29 #include <WebCore/KeyboardEvent.h>
       
    30 #include <WebCore/Page.h>
       
    31 #include <WebCore/PlatformKeyboardEvent.h>
       
    32 #include <WebCore/Settings.h>
       
    33 
       
    34 #ifndef VK_UNKNOWN
       
    35 #define VK_UNKNOWN 0
       
    36 #define VK_BACK 0x08
       
    37 #define VK_TAB 0x09
       
    38 #define VK_CLEAR 0x0C
       
    39 #define VK_RETURN 0x0D
       
    40 #define VK_SHIFT 0x10
       
    41 #define VK_CONTROL 0x11 // CTRL key
       
    42 #define VK_MENU 0x12 // ALT key
       
    43 #define VK_PAUSE 0x13 // PAUSE key
       
    44 #define VK_CAPITAL 0x14 // CAPS LOCK key
       
    45 #define VK_KANA 0x15 // Input Method Editor (IME) Kana mode
       
    46 #define VK_HANGUL 0x15 // IME Hangul mode
       
    47 #define VK_JUNJA 0x17 // IME Junja mode
       
    48 #define VK_FINAL 0x18 // IME final mode
       
    49 #define VK_HANJA 0x19 // IME Hanja mode 
       
    50 #define VK_KANJI 0x19 // IME Kanji mode
       
    51 #define VK_ESCAPE 0x1B // ESC key
       
    52 #define VK_CONVERT 0x1C // IME convert
       
    53 #define VK_NONCONVERT 0x1D // IME nonconvert
       
    54 #define VK_ACCEPT 0x1E // IME accept
       
    55 #define VK_MODECHANGE 0x1F // IME mode change request
       
    56 #define VK_SPACE 0x20 // SPACE key
       
    57 #define VK_PRIOR 0x21 // PAGE UP key
       
    58 #define VK_NEXT 0x22 // PAGE DOWN key
       
    59 #define VK_END 0x23 // END key
       
    60 #define VK_HOME 0x24 // HOME key
       
    61 #define VK_LEFT 0x25 // LEFT ARROW key
       
    62 #define VK_UP 0x26 // UP ARROW key
       
    63 #define VK_RIGHT 0x27 // RIGHT ARROW key
       
    64 #define VK_DOWN 0x28 // DOWN ARROW key
       
    65 #define VK_SELECT 0x29 // SELECT key
       
    66 #define VK_PRINT 0x2A // PRINT key
       
    67 #define VK_EXECUTE 0x2B // EXECUTE key
       
    68 #define VK_SNAPSHOT 0x2C // PRINT SCREEN key
       
    69 #define VK_INSERT 0x2D // INS key
       
    70 #define VK_DELETE 0x2E // DEL key
       
    71 #define VK_HELP 0x2F // HELP key
       
    72 // Windows 2000/XP: For any country/region, the '.' key
       
    73 #define VK_OEM_PERIOD 0xBE
       
    74 #endif
       
    75 
       
    76 using namespace WebCore;
       
    77 
       
    78 namespace WebKit {
       
    79     
       
    80 void WebPage::platformInitialize()
       
    81 {
       
    82 }
       
    83 
       
    84 static const unsigned CtrlKey = 1 << 0;
       
    85 static const unsigned AltKey = 1 << 1;
       
    86 static const unsigned ShiftKey = 1 << 2;
       
    87 
       
    88 struct KeyDownEntry {
       
    89     unsigned virtualKey;
       
    90     unsigned modifiers;
       
    91     const char* name;
       
    92 };
       
    93 
       
    94 struct KeyPressEntry {
       
    95     unsigned charCode;
       
    96     unsigned modifiers;
       
    97     const char* name;
       
    98 };
       
    99 
       
   100 static const KeyDownEntry keyDownEntries[] = {
       
   101     { VK_LEFT,   0,                  "MoveLeft"                                    },
       
   102     { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection"                  },
       
   103     { VK_LEFT,   CtrlKey,            "MoveWordLeft"                                },
       
   104     { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"              },
       
   105     { VK_RIGHT,  0,                  "MoveRight"                                   },
       
   106     { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection"                 },
       
   107     { VK_RIGHT,  CtrlKey,            "MoveWordRight"                               },
       
   108     { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection"             },
       
   109     { VK_UP,     0,                  "MoveUp"                                      },
       
   110     { VK_UP,     ShiftKey,           "MoveUpAndModifySelection"                    },
       
   111     { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection"                },
       
   112     { VK_DOWN,   0,                  "MoveDown"                                    },
       
   113     { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection"                  },
       
   114     { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection"              },
       
   115     { VK_PRIOR,  0,                  "MovePageUp"                                  },
       
   116     { VK_NEXT,   0,                  "MovePageDown"                                },
       
   117     { VK_HOME,   0,                  "MoveToBeginningOfLine"                       },
       
   118     { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection"     },
       
   119     { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument"                   },
       
   120     { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
       
   121     
       
   122     { VK_END,    0,                  "MoveToEndOfLine"                             },
       
   123     { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection"           },
       
   124     { VK_END,    CtrlKey,            "MoveToEndOfDocument"                         },
       
   125     { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection"       },
       
   126     
       
   127     { VK_BACK,   0,                  "DeleteBackward"                              },
       
   128     { VK_BACK,   ShiftKey,           "DeleteBackward"                              },
       
   129     { VK_DELETE, 0,                  "DeleteForward"                               },
       
   130     { VK_BACK,   CtrlKey,            "DeleteWordBackward"                          },
       
   131     { VK_DELETE, CtrlKey,            "DeleteWordForward"                           },
       
   132     
       
   133     { 'B',       CtrlKey,            "ToggleBold"                                  },
       
   134     { 'I',       CtrlKey,            "ToggleItalic"                                },
       
   135     
       
   136     { VK_ESCAPE, 0,                  "Cancel"                                      },
       
   137     { VK_OEM_PERIOD, CtrlKey,        "Cancel"                                      },
       
   138     { VK_TAB,    0,                  "InsertTab"                                   },
       
   139     { VK_TAB,    ShiftKey,           "InsertBacktab"                               },
       
   140     { VK_RETURN, 0,                  "InsertNewline"                               },
       
   141     { VK_RETURN, CtrlKey,            "InsertNewline"                               },
       
   142     { VK_RETURN, AltKey,             "InsertNewline"                               },
       
   143     { VK_RETURN, ShiftKey,           "InsertNewline"                               },
       
   144     { VK_RETURN, AltKey | ShiftKey,  "InsertNewline"                               },
       
   145     
       
   146     // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
       
   147     // in the application or in WebKit. We chose WebKit.
       
   148     { 'C',       CtrlKey,            "Copy"                                        },
       
   149     { 'V',       CtrlKey,            "Paste"                                       },
       
   150     { 'X',       CtrlKey,            "Cut"                                         },
       
   151     { 'A',       CtrlKey,            "SelectAll"                                   },
       
   152     { VK_INSERT, CtrlKey,            "Copy"                                        },
       
   153     { VK_DELETE, ShiftKey,           "Cut"                                         },
       
   154     { VK_INSERT, ShiftKey,           "Paste"                                       },
       
   155     { 'Z',       CtrlKey,            "Undo"                                        },
       
   156     { 'Z',       CtrlKey | ShiftKey, "Redo"                                        },
       
   157 };
       
   158 
       
   159 static const KeyPressEntry keyPressEntries[] = {
       
   160     { '\t',   0,                  "InsertTab"                                   },
       
   161     { '\t',   ShiftKey,           "InsertBacktab"                               },
       
   162     { '\r',   0,                  "InsertNewline"                               },
       
   163     { '\r',   CtrlKey,            "InsertNewline"                               },
       
   164     { '\r',   AltKey,             "InsertNewline"                               },
       
   165     { '\r',   ShiftKey,           "InsertNewline"                               },
       
   166     { '\r',   AltKey | ShiftKey,  "InsertNewline"                               },
       
   167 };
       
   168 
       
   169 const char* WebPage::interpretKeyEvent(const KeyboardEvent* evt)
       
   170 {
       
   171     ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
       
   172     
       
   173     static HashMap<int, const char*>* keyDownCommandsMap = 0;
       
   174     static HashMap<int, const char*>* keyPressCommandsMap = 0;
       
   175     
       
   176     if (!keyDownCommandsMap) {
       
   177         keyDownCommandsMap = new HashMap<int, const char*>;
       
   178         keyPressCommandsMap = new HashMap<int, const char*>;
       
   179         
       
   180         for (unsigned i = 0; i < (sizeof(keyDownEntries) / sizeof(keyDownEntries[0])); i++)
       
   181             keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
       
   182         
       
   183         for (unsigned i = 0; i < (sizeof(keyPressEntries) / sizeof(keyPressEntries[0])); i++)
       
   184             keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
       
   185     }
       
   186     
       
   187     unsigned modifiers = 0;
       
   188     if (evt->shiftKey())
       
   189         modifiers |= ShiftKey;
       
   190     if (evt->altKey())
       
   191         modifiers |= AltKey;
       
   192     if (evt->ctrlKey())
       
   193         modifiers |= CtrlKey;
       
   194     
       
   195     if (evt->type() == eventNames().keydownEvent) {
       
   196         int mapKey = modifiers << 16 | evt->keyEvent()->windowsVirtualKeyCode();
       
   197         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
       
   198     }
       
   199     
       
   200     int mapKey = modifiers << 16 | evt->charCode();
       
   201     return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
       
   202 }
       
   203     
       
   204 } // namespace WebKit