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