gtk_key_bindings_handler.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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
15struct 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.
36class GtkKeyBindingsHandler {
37 public:
38  explicit GtkKeyBindingsHandler(GtkWidget* parent_widget);
39  ~GtkKeyBindingsHandler();
40
41  // Matches a key event against predefined gtk key bindings, false will be
42  // returned if the key event doesn't correspond to a predefined key binding.
43  // Edit commands matched with |wke| will be stored in |edit_commands|.
44  bool Match(const NativeWebKeyboardEvent& wke, EditCommands* edit_commands);
45
46 private:
47  // Object structure of Handler class, which is derived from GtkTextView.
48  struct Handler {
49    GtkTextView parent_object;
50    GtkKeyBindingsHandler *owner;
51  };
52
53  // Class structure of Handler class.
54  struct HandlerClass {
55    GtkTextViewClass parent_class;
56  };
57
58  // Creates a new instance of Handler class.
59  GtkWidget* CreateNewHandler();
60
61  // Adds an edit command to the key event.
62  void EditCommandMatched(const std::string& name, const std::string& value);
63
64  // Initializes Handler structure.
65  static void HandlerInit(Handler *self);
66
67  // Initializes HandlerClass structure.
68  static void HandlerClassInit(HandlerClass *klass);
69
70  // Registeres Handler class to GObject type system and return its type id.
71  static GType HandlerGetType();
72
73  // Gets the GtkKeyBindingsHandler object which owns the Handler object.
74  static GtkKeyBindingsHandler* GetHandlerOwner(GtkTextView* text_view);
75
76  // Handler of "backspace" signal.
77  static void BackSpace(GtkTextView* text_view);
78
79  // Handler of "copy-clipboard" signal.
80  static void CopyClipboard(GtkTextView* text_view);
81
82  // Handler of "cut-clipboard" signal.
83  static void CutClipboard(GtkTextView* text_view);
84
85  // Handler of "delete-from-cursor" signal.
86  static void DeleteFromCursor(GtkTextView* text_view, GtkDeleteType type,
87                               gint count);
88
89  // Handler of "insert-at-cursor" signal.
90  static void InsertAtCursor(GtkTextView* text_view, const gchar* str);
91
92  // Handler of "move-cursor" signal.
93  static void MoveCursor(GtkTextView* text_view, GtkMovementStep step,
94                         gint count, gboolean extend_selection);
95
96  // Handler of "move-viewport" signal.
97  static void MoveViewport(GtkTextView* text_view, GtkScrollStep step,
98                           gint count);
99
100  // Handler of "paste-clipboard" signal.
101  static void PasteClipboard(GtkTextView* text_view);
102
103  // Handler of "select-all" signal.
104  static void SelectAll(GtkTextView* text_view, gboolean select);
105
106  // Handler of "set-anchor" signal.
107  static void SetAnchor(GtkTextView* text_view);
108
109  // Handler of "toggle-cursor-visible" signal.
110  static void ToggleCursorVisible(GtkTextView* text_view);
111
112  // Handler of "toggle-overwrite" signal.
113  static void ToggleOverwrite(GtkTextView* text_view);
114
115  // Handler of "show-help" signal.
116  static gboolean ShowHelp(GtkWidget* widget, GtkWidgetHelpType arg1);
117
118  // Handler of "move-focus" signal.
119  static void MoveFocus(GtkWidget* widget, GtkDirectionType arg1);
120
121  OwnedWidgetGtk handler_;
122
123  // Buffer to store the match results.
124  EditCommands edit_commands_;
125};
126
127#endif  // CHROME_BROWSER_RENDERER_HOST_GTK_KEY_BINDINGS_HANDLER_H_
128