WebKit2/WebProcess/WebPage/win/WebPageWin.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
       
    14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    23  * THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #include "WebPage.h"
       
    27 
       
    28 #include <WebCore/FontRenderingMode.h>
       
    29 #include <WebCore/KeyboardEvent.h>
       
    30 #include <WebCore/Page.h>
       
    31 #include <WebCore/PlatformKeyboardEvent.h>
       
    32 #include <WebCore/Settings.h>
       
    33 #include <WinUser.h>
       
    34 
       
    35 using namespace WebCore;
       
    36 
       
    37 namespace WebKit {
       
    38 
       
    39 void WebPage::platformInitialize()
       
    40 {
       
    41     m_page->settings()->setFontRenderingMode(AlternateRenderingMode);
       
    42 }
       
    43 
       
    44 static const unsigned CtrlKey = 1 << 0;
       
    45 static const unsigned AltKey = 1 << 1;
       
    46 static const unsigned ShiftKey = 1 << 2;
       
    47 
       
    48 struct KeyDownEntry {
       
    49     unsigned virtualKey;
       
    50     unsigned modifiers;
       
    51     const char* name;
       
    52 };
       
    53 
       
    54 struct KeyPressEntry {
       
    55     unsigned charCode;
       
    56     unsigned modifiers;
       
    57     const char* name;
       
    58 };
       
    59 
       
    60 static const KeyDownEntry keyDownEntries[] = {
       
    61     { VK_LEFT,   0,                  "MoveLeft"                                    },
       
    62     { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection"                  },
       
    63     { VK_LEFT,   CtrlKey,            "MoveWordLeft"                                },
       
    64     { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"              },
       
    65     { VK_RIGHT,  0,                  "MoveRight"                                   },
       
    66     { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection"                 },
       
    67     { VK_RIGHT,  CtrlKey,            "MoveWordRight"                               },
       
    68     { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection"             },
       
    69     { VK_UP,     0,                  "MoveUp"                                      },
       
    70     { VK_UP,     ShiftKey,           "MoveUpAndModifySelection"                    },
       
    71     { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection"                },
       
    72     { VK_DOWN,   0,                  "MoveDown"                                    },
       
    73     { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection"                  },
       
    74     { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection"              },
       
    75     { VK_PRIOR,  0,                  "MovePageUp"                                  },
       
    76     { VK_NEXT,   0,                  "MovePageDown"                                },
       
    77     { VK_HOME,   0,                  "MoveToBeginningOfLine"                       },
       
    78     { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection"     },
       
    79     { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument"                   },
       
    80     { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
       
    81 
       
    82     { VK_END,    0,                  "MoveToEndOfLine"                             },
       
    83     { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection"           },
       
    84     { VK_END,    CtrlKey,            "MoveToEndOfDocument"                         },
       
    85     { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection"       },
       
    86 
       
    87     { VK_BACK,   0,                  "DeleteBackward"                              },
       
    88     { VK_BACK,   ShiftKey,           "DeleteBackward"                              },
       
    89     { VK_DELETE, 0,                  "DeleteForward"                               },
       
    90     { VK_BACK,   CtrlKey,            "DeleteWordBackward"                          },
       
    91     { VK_DELETE, CtrlKey,            "DeleteWordForward"                           },
       
    92     
       
    93     { 'B',       CtrlKey,            "ToggleBold"                                  },
       
    94     { 'I',       CtrlKey,            "ToggleItalic"                                },
       
    95 
       
    96     { VK_ESCAPE, 0,                  "Cancel"                                      },
       
    97     { VK_OEM_PERIOD, CtrlKey,        "Cancel"                                      },
       
    98     { VK_TAB,    0,                  "InsertTab"                                   },
       
    99     { VK_TAB,    ShiftKey,           "InsertBacktab"                               },
       
   100     { VK_RETURN, 0,                  "InsertNewline"                               },
       
   101     { VK_RETURN, CtrlKey,            "InsertNewline"                               },
       
   102     { VK_RETURN, AltKey,             "InsertNewline"                               },
       
   103     { VK_RETURN, ShiftKey,           "InsertNewline"                               },
       
   104     { VK_RETURN, AltKey | ShiftKey,  "InsertNewline"                               },
       
   105 
       
   106     // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
       
   107     // in the application or in WebKit. We chose WebKit.
       
   108     { 'C',       CtrlKey,            "Copy"                                        },
       
   109     { 'V',       CtrlKey,            "Paste"                                       },
       
   110     { 'X',       CtrlKey,            "Cut"                                         },
       
   111     { 'A',       CtrlKey,            "SelectAll"                                   },
       
   112     { VK_INSERT, CtrlKey,            "Copy"                                        },
       
   113     { VK_DELETE, ShiftKey,           "Cut"                                         },
       
   114     { VK_INSERT, ShiftKey,           "Paste"                                       },
       
   115     { 'Z',       CtrlKey,            "Undo"                                        },
       
   116     { 'Z',       CtrlKey | ShiftKey, "Redo"                                        },
       
   117 };
       
   118 
       
   119 static const KeyPressEntry keyPressEntries[] = {
       
   120     { '\t',   0,                  "InsertTab"                                   },
       
   121     { '\t',   ShiftKey,           "InsertBacktab"                               },
       
   122     { '\r',   0,                  "InsertNewline"                               },
       
   123     { '\r',   CtrlKey,            "InsertNewline"                               },
       
   124     { '\r',   AltKey,             "InsertNewline"                               },
       
   125     { '\r',   ShiftKey,           "InsertNewline"                               },
       
   126     { '\r',   AltKey | ShiftKey,  "InsertNewline"                               },
       
   127 };
       
   128 
       
   129 const char* WebPage::interpretKeyEvent(const KeyboardEvent* evt)
       
   130 {
       
   131     ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
       
   132 
       
   133     static HashMap<int, const char*>* keyDownCommandsMap = 0;
       
   134     static HashMap<int, const char*>* keyPressCommandsMap = 0;
       
   135 
       
   136     if (!keyDownCommandsMap) {
       
   137         keyDownCommandsMap = new HashMap<int, const char*>;
       
   138         keyPressCommandsMap = new HashMap<int, const char*>;
       
   139 
       
   140         for (unsigned i = 0; i < _countof(keyDownEntries); i++)
       
   141             keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
       
   142 
       
   143         for (unsigned i = 0; i < _countof(keyPressEntries); i++)
       
   144             keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
       
   145     }
       
   146 
       
   147     unsigned modifiers = 0;
       
   148     if (evt->shiftKey())
       
   149         modifiers |= ShiftKey;
       
   150     if (evt->altKey())
       
   151         modifiers |= AltKey;
       
   152     if (evt->ctrlKey())
       
   153         modifiers |= CtrlKey;
       
   154 
       
   155     if (evt->type() == eventNames().keydownEvent) {
       
   156         int mapKey = modifiers << 16 | evt->keyCode();
       
   157         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
       
   158     }
       
   159 
       
   160     int mapKey = modifiers << 16 | evt->charCode();
       
   161     return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
       
   162 }
       
   163 
       
   164 } // namespace WebKit