OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/gtk_key_bindings_handler.h" | 5 #include "content/browser/renderer_host/gtk_key_bindings_handler.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 // We don't need to show the |handler| object on screen, so set its size to | 50 // We don't need to show the |handler| object on screen, so set its size to |
51 // zero. | 51 // zero. |
52 gtk_widget_set_size_request(GTK_WIDGET(handler), 0, 0); | 52 gtk_widget_set_size_request(GTK_WIDGET(handler), 0, 0); |
53 | 53 |
54 // Prevents it from handling any events by itself. | 54 // Prevents it from handling any events by itself. |
55 gtk_widget_set_sensitive(GTK_WIDGET(handler), FALSE); | 55 gtk_widget_set_sensitive(GTK_WIDGET(handler), FALSE); |
56 gtk_widget_set_events(GTK_WIDGET(handler), 0); | 56 gtk_widget_set_events(GTK_WIDGET(handler), 0); |
57 gtk_widget_set_can_focus(GTK_WIDGET(handler), TRUE); | 57 gtk_widget_set_can_focus(GTK_WIDGET(handler), TRUE); |
58 | 58 |
59 #if !GTK_CHECK_VERSION(2, 14, 0) | |
60 // "move-focus", "move-viewport", "select-all" and "toggle-cursor-visible" | |
61 // have no corresponding virtual methods. Prior to glib 2.18 (gtk 2.14), | |
62 // there is no way to override the default class handler of a signal. | |
63 // So we need hook these signal explicitly. | |
64 g_signal_connect(handler, "move-focus", G_CALLBACK(MoveFocus), NULL); | |
65 g_signal_connect(handler, "move-viewport", G_CALLBACK(MoveViewport), NULL); | |
66 g_signal_connect(handler, "select-all", G_CALLBACK(SelectAll), NULL); | |
67 g_signal_connect(handler, "toggle-cursor-visible", | |
68 G_CALLBACK(ToggleCursorVisible), NULL); | |
69 #endif | |
70 return GTK_WIDGET(handler); | 59 return GTK_WIDGET(handler); |
71 } | 60 } |
72 | 61 |
73 void GtkKeyBindingsHandler::EditCommandMatched( | 62 void GtkKeyBindingsHandler::EditCommandMatched( |
74 const std::string& name, const std::string& value) { | 63 const std::string& name, const std::string& value) { |
75 edit_commands_.push_back(EditCommand(name, value)); | 64 edit_commands_.push_back(EditCommand(name, value)); |
76 } | 65 } |
77 | 66 |
78 void GtkKeyBindingsHandler::HandlerInit(Handler *self) { | 67 void GtkKeyBindingsHandler::HandlerInit(Handler *self) { |
79 self->owner = NULL; | 68 self->owner = NULL; |
80 } | 69 } |
81 | 70 |
82 void GtkKeyBindingsHandler::HandlerClassInit(HandlerClass *klass) { | 71 void GtkKeyBindingsHandler::HandlerClassInit(HandlerClass *klass) { |
83 GtkTextViewClass* text_view_class = GTK_TEXT_VIEW_CLASS(klass); | 72 GtkTextViewClass* text_view_class = GTK_TEXT_VIEW_CLASS(klass); |
84 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); | 73 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); |
85 | 74 |
86 // Overrides all virtual methods related to editor key bindings. | 75 // Overrides all virtual methods related to editor key bindings. |
87 text_view_class->backspace = BackSpace; | 76 text_view_class->backspace = BackSpace; |
88 text_view_class->copy_clipboard = CopyClipboard; | 77 text_view_class->copy_clipboard = CopyClipboard; |
89 text_view_class->cut_clipboard = CutClipboard; | 78 text_view_class->cut_clipboard = CutClipboard; |
90 text_view_class->delete_from_cursor = DeleteFromCursor; | 79 text_view_class->delete_from_cursor = DeleteFromCursor; |
91 text_view_class->insert_at_cursor = InsertAtCursor; | 80 text_view_class->insert_at_cursor = InsertAtCursor; |
92 text_view_class->move_cursor = MoveCursor; | 81 text_view_class->move_cursor = MoveCursor; |
93 text_view_class->paste_clipboard = PasteClipboard; | 82 text_view_class->paste_clipboard = PasteClipboard; |
94 text_view_class->set_anchor = SetAnchor; | 83 text_view_class->set_anchor = SetAnchor; |
95 text_view_class->toggle_overwrite = ToggleOverwrite; | 84 text_view_class->toggle_overwrite = ToggleOverwrite; |
96 widget_class->show_help = ShowHelp; | 85 widget_class->show_help = ShowHelp; |
97 | 86 |
98 #if GTK_CHECK_VERSION(2, 14, 0) | |
99 // "move-focus", "move-viewport", "select-all" and "toggle-cursor-visible" | 87 // "move-focus", "move-viewport", "select-all" and "toggle-cursor-visible" |
100 // have no corresponding virtual methods. Since glib 2.18 (gtk 2.14), | 88 // have no corresponding virtual methods. Since glib 2.18 (gtk 2.14), |
101 // g_signal_override_class_handler() is introduced to override a signal | 89 // g_signal_override_class_handler() is introduced to override a signal |
102 // handler. | 90 // handler. |
103 g_signal_override_class_handler("move-focus", | 91 g_signal_override_class_handler("move-focus", |
104 G_TYPE_FROM_CLASS(klass), | 92 G_TYPE_FROM_CLASS(klass), |
105 G_CALLBACK(MoveFocus)); | 93 G_CALLBACK(MoveFocus)); |
106 | 94 |
107 g_signal_override_class_handler("move-viewport", | 95 g_signal_override_class_handler("move-viewport", |
108 G_TYPE_FROM_CLASS(klass), | 96 G_TYPE_FROM_CLASS(klass), |
109 G_CALLBACK(MoveViewport)); | 97 G_CALLBACK(MoveViewport)); |
110 | 98 |
111 g_signal_override_class_handler("select-all", | 99 g_signal_override_class_handler("select-all", |
112 G_TYPE_FROM_CLASS(klass), | 100 G_TYPE_FROM_CLASS(klass), |
113 G_CALLBACK(SelectAll)); | 101 G_CALLBACK(SelectAll)); |
114 | 102 |
115 g_signal_override_class_handler("toggle-cursor-visible", | 103 g_signal_override_class_handler("toggle-cursor-visible", |
116 G_TYPE_FROM_CLASS(klass), | 104 G_TYPE_FROM_CLASS(klass), |
117 G_CALLBACK(ToggleCursorVisible)); | 105 G_CALLBACK(ToggleCursorVisible)); |
118 #endif | |
119 } | 106 } |
120 | 107 |
121 GType GtkKeyBindingsHandler::HandlerGetType() { | 108 GType GtkKeyBindingsHandler::HandlerGetType() { |
122 static volatile gsize type_id_volatile = 0; | 109 static volatile gsize type_id_volatile = 0; |
123 if (g_once_init_enter(&type_id_volatile)) { | 110 if (g_once_init_enter(&type_id_volatile)) { |
124 GType type_id = g_type_register_static_simple( | 111 GType type_id = g_type_register_static_simple( |
125 GTK_TYPE_TEXT_VIEW, | 112 GTK_TYPE_TEXT_VIEW, |
126 g_intern_static_string("GtkKeyBindingsHandler"), | 113 g_intern_static_string("GtkKeyBindingsHandler"), |
127 sizeof(HandlerClass), | 114 sizeof(HandlerClass), |
128 reinterpret_cast<GClassInitFunc>(HandlerClassInit), | 115 reinterpret_cast<GClassInitFunc>(HandlerClassInit), |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 command.append("AndModifySelection"); | 244 command.append("AndModifySelection"); |
258 if (count < 0) | 245 if (count < 0) |
259 count = -count; | 246 count = -count; |
260 for (; count > 0; --count) | 247 for (; count > 0; --count) |
261 owner->EditCommandMatched(command, ""); | 248 owner->EditCommandMatched(command, ""); |
262 } | 249 } |
263 | 250 |
264 void GtkKeyBindingsHandler::MoveViewport( | 251 void GtkKeyBindingsHandler::MoveViewport( |
265 GtkTextView* text_view, GtkScrollStep step, gint count) { | 252 GtkTextView* text_view, GtkScrollStep step, gint count) { |
266 // Not supported by webkit. | 253 // Not supported by webkit. |
267 #if !GTK_CHECK_VERSION(2, 14, 0) | |
268 // Before gtk 2.14.0, there is no way to override a non-virtual default signal | |
269 // handler, so we need stop the signal emission explicitly to prevent the | |
270 // default handler from being executed. | |
271 g_signal_stop_emission_by_name(text_view, "move-viewport"); | |
272 #endif | |
273 } | 254 } |
274 | 255 |
275 void GtkKeyBindingsHandler::PasteClipboard(GtkTextView* text_view) { | 256 void GtkKeyBindingsHandler::PasteClipboard(GtkTextView* text_view) { |
276 GetHandlerOwner(text_view)->EditCommandMatched("Paste", ""); | 257 GetHandlerOwner(text_view)->EditCommandMatched("Paste", ""); |
277 } | 258 } |
278 | 259 |
279 void GtkKeyBindingsHandler::SelectAll(GtkTextView* text_view, gboolean select) { | 260 void GtkKeyBindingsHandler::SelectAll(GtkTextView* text_view, gboolean select) { |
280 if (select) | 261 if (select) |
281 GetHandlerOwner(text_view)->EditCommandMatched("SelectAll", ""); | 262 GetHandlerOwner(text_view)->EditCommandMatched("SelectAll", ""); |
282 else | 263 else |
283 GetHandlerOwner(text_view)->EditCommandMatched("Unselect", ""); | 264 GetHandlerOwner(text_view)->EditCommandMatched("Unselect", ""); |
284 #if !GTK_CHECK_VERSION(2, 14, 0) | |
285 // Before gtk 2.14.0, there is no way to override a non-virtual default signal | |
286 // handler, so we need stop the signal emission explicitly to prevent the | |
287 // default handler from being executed. | |
288 g_signal_stop_emission_by_name(text_view, "select-all"); | |
289 #endif | |
290 } | 265 } |
291 | 266 |
292 void GtkKeyBindingsHandler::SetAnchor(GtkTextView* text_view) { | 267 void GtkKeyBindingsHandler::SetAnchor(GtkTextView* text_view) { |
293 GetHandlerOwner(text_view)->EditCommandMatched("SetMark", ""); | 268 GetHandlerOwner(text_view)->EditCommandMatched("SetMark", ""); |
294 } | 269 } |
295 | 270 |
296 void GtkKeyBindingsHandler::ToggleCursorVisible(GtkTextView* text_view) { | 271 void GtkKeyBindingsHandler::ToggleCursorVisible(GtkTextView* text_view) { |
297 // Not supported by webkit. | 272 // Not supported by webkit. |
298 #if !GTK_CHECK_VERSION(2, 14, 0) | |
299 // Before gtk 2.14.0, there is no way to override a non-virtual default signal | |
300 // handler, so we need stop the signal emission explicitly to prevent the | |
301 // default handler from being executed. | |
302 g_signal_stop_emission_by_name(text_view, "toggle-cursor-visible"); | |
303 #endif | |
304 } | 273 } |
305 | 274 |
306 void GtkKeyBindingsHandler::ToggleOverwrite(GtkTextView* text_view) { | 275 void GtkKeyBindingsHandler::ToggleOverwrite(GtkTextView* text_view) { |
307 // Not supported by webkit. | 276 // Not supported by webkit. |
308 } | 277 } |
309 | 278 |
310 gboolean GtkKeyBindingsHandler::ShowHelp(GtkWidget* widget, | 279 gboolean GtkKeyBindingsHandler::ShowHelp(GtkWidget* widget, |
311 GtkWidgetHelpType arg1) { | 280 GtkWidgetHelpType arg1) { |
312 // Just for disabling the default handler. | 281 // Just for disabling the default handler. |
313 return FALSE; | 282 return FALSE; |
314 } | 283 } |
315 | 284 |
316 void GtkKeyBindingsHandler::MoveFocus(GtkWidget* widget, | 285 void GtkKeyBindingsHandler::MoveFocus(GtkWidget* widget, |
317 GtkDirectionType arg1) { | 286 GtkDirectionType arg1) { |
318 // Just for disabling the default handler. | 287 // Just for disabling the default handler. |
319 #if !GTK_CHECK_VERSION(2, 14, 0) | |
320 // Before gtk 2.14.0, there is no way to override a non-virtual default signal | |
321 // handler, so we need stop the signal emission explicitly to prevent the | |
322 // default handler from being executed. | |
323 g_signal_stop_emission_by_name(widget, "move-focus"); | |
324 #endif | |
325 } | 288 } |
OLD | NEW |