OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_RENDERER_HOST_GTK_KEY_BINDINGS_HANDLER_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_GTK_KEY_BINDINGS_HANDLER_H_ |
| 7 |
| 8 #include <gtk/gtk.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "chrome/common/edit_command.h" |
| 13 #include "chrome/common/owned_widget_gtk.h" |
| 14 |
| 15 class NativeWebKeyboardEvent; |
| 16 |
| 17 // This class is a convenience class for handling editor key bindings defined |
| 18 // in gtk keyboard theme. |
| 19 // In gtk, only GtkEntry and GtkTextView support customizing editor key bindings |
| 20 // through keyboard theme. And in gtk keyboard theme definition file, each key |
| 21 // binding must be bound to a specific class or object. So existing keyboard |
| 22 // themes only define editor key bindings exactly for GtkEntry and GtkTextView. |
| 23 // Then, the only way for us to intercept editor key bindings defined in |
| 24 // keyboard theme, is to create a GtkEntry or GtkTextView object and call |
| 25 // gtk_bindings_activate_event() against it for the key events. If a key event |
| 26 // matches a predefined key binding, corresponding signal will be emitted. |
| 27 // GtkTextView is used here because it supports more key bindings than GtkEntry, |
| 28 // but in order to minimize the side effect of using a GtkTextView object, a new |
| 29 // class derived from GtkTextView is used, which overrides all signals related |
| 30 // to key bindings, to make sure GtkTextView won't receive them. |
| 31 // |
| 32 // See third_party/WebKit/WebCore/editing/EditorCommand.cpp for detailed |
| 33 // definition of webkit edit commands. |
| 34 // See webkit/glue/editor_client_impl.cc for key bindings predefined in our |
| 35 // webkit glue. |
| 36 class GtkKeyBindingsHandler { |
| 37 public: |
| 38 explicit GtkKeyBindingsHandler(GtkWidget* parent_widget); |
| 39 ~GtkKeyBindingsHandler(); |
| 40 |
| 41 // Key bindings handler will be disabled when IME is disabled by webkit. |
| 42 void set_enabled(bool enabled) { |
| 43 enabled_ = enabled; |
| 44 } |
| 45 |
| 46 // Matches a key event against predefined gtk key bindings, false will be |
| 47 // returned if the key event doesn't correspond to a predefined key binding. |
| 48 // Edit commands matched with |wke| will be stored in |edit_commands|. |
| 49 bool Match(const NativeWebKeyboardEvent& wke, EditCommands* edit_commands); |
| 50 |
| 51 private: |
| 52 // Object structure of Handler class, which is derived from GtkTextView. |
| 53 struct Handler { |
| 54 GtkTextView parent_object; |
| 55 GtkKeyBindingsHandler *owner; |
| 56 }; |
| 57 |
| 58 // Class structure of Handler class. |
| 59 struct HandlerClass { |
| 60 GtkTextViewClass parent_class; |
| 61 }; |
| 62 |
| 63 // Creates a new instance of Handler class. |
| 64 GtkWidget* CreateNewHandler(); |
| 65 |
| 66 // Adds an edit command to the key event. |
| 67 void EditCommandMatched(const std::string& name, const std::string& value); |
| 68 |
| 69 // Initializes Handler structure. |
| 70 static void HandlerInit(Handler *self); |
| 71 |
| 72 // Initializes HandlerClass structure. |
| 73 static void HandlerClassInit(HandlerClass *klass); |
| 74 |
| 75 // Registeres Handler class to GObject type system and return its type id. |
| 76 static GType HandlerGetType(); |
| 77 |
| 78 // Gets the GtkKeyBindingsHandler object which owns the Handler object. |
| 79 static GtkKeyBindingsHandler* GetHandlerOwner(GtkTextView* text_view); |
| 80 |
| 81 // Handler of "backspace" signal. |
| 82 static void BackSpace(GtkTextView* text_view); |
| 83 |
| 84 // Handler of "copy-clipboard" signal. |
| 85 static void CopyClipboard(GtkTextView* text_view); |
| 86 |
| 87 // Handler of "cut-clipboard" signal. |
| 88 static void CutClipboard(GtkTextView* text_view); |
| 89 |
| 90 // Handler of "delete-from-cursor" signal. |
| 91 static void DeleteFromCursor(GtkTextView* text_view, GtkDeleteType type, |
| 92 gint count); |
| 93 |
| 94 // Handler of "insert-at-cursor" signal. |
| 95 static void InsertAtCursor(GtkTextView* text_view, const gchar* str); |
| 96 |
| 97 // Handler of "move-cursor" signal. |
| 98 static void MoveCursor(GtkTextView* text_view, GtkMovementStep step, |
| 99 gint count, gboolean extend_selection); |
| 100 |
| 101 // Handler of "move-viewport" signal. |
| 102 static void MoveViewport(GtkTextView* text_view, GtkScrollStep step, |
| 103 gint count); |
| 104 |
| 105 // Handler of "paste-clipboard" signal. |
| 106 static void PasteClipboard(GtkTextView* text_view); |
| 107 |
| 108 // Handler of "select-all" signal. |
| 109 static void SelectAll(GtkTextView* text_view, gboolean select); |
| 110 |
| 111 // Handler of "set-anchor" signal. |
| 112 static void SetAnchor(GtkTextView* text_view); |
| 113 |
| 114 // Handler of "toggle-cursor-visible" signal. |
| 115 static void ToggleCursorVisible(GtkTextView* text_view); |
| 116 |
| 117 // Handler of "toggle-overwrite" signal. |
| 118 static void ToggleOverwrite(GtkTextView* text_view); |
| 119 |
| 120 OwnedWidgetGtk handler_; |
| 121 |
| 122 // Buffer to store the match results. |
| 123 EditCommands edit_commands_; |
| 124 |
| 125 // Indicates if key bindings handler is enabled or not. |
| 126 // It'll only be enabled if IME is enabled by webkit. |
| 127 bool enabled_; |
| 128 }; |
| 129 |
| 130 #endif // CHROME_BROWSER_RENDERER_HOST_GTK_KEY_BINDINGS_HANDLER_H_ |
OLD | NEW |