OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/libgtkui/gtk2_key_bindings_handler.h" | 5 #include "chrome/browser/ui/libgtkui/gtk_key_bindings_handler.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <X11/XKBlib.h> | 10 #include <X11/XKBlib.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "chrome/browser/ui/libgtkui/gtk2_util.h" | 17 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
18 #include "content/public/browser/native_web_keyboard_event.h" | 18 #include "content/public/browser/native_web_keyboard_event.h" |
19 #include "ui/base/ime/text_edit_commands.h" | 19 #include "ui/base/ime/text_edit_commands.h" |
20 #include "ui/base/x/x11_util.h" | 20 #include "ui/base/x/x11_util.h" |
21 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
22 | 22 |
23 using ui::TextEditCommand; | 23 using ui::TextEditCommand; |
24 | 24 |
25 // TODO(erg): Rewrite the old gtk_key_bindings_handler_unittest.cc and get them | 25 // TODO(erg): Rewrite the old gtk_key_bindings_handler_unittest.cc and get them |
26 // in a state that links. This code was adapted from the content layer GTK | 26 // in a state that links. This code was adapted from the content layer GTK |
27 // code, which had some simple unit tests. However, the changes in the public | 27 // code, which had some simple unit tests. However, the changes in the public |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 98 } |
99 | 99 |
100 void Gtk2KeyBindingsHandler::EditCommandMatched(TextEditCommand command, | 100 void Gtk2KeyBindingsHandler::EditCommandMatched(TextEditCommand command, |
101 const std::string& value) { | 101 const std::string& value) { |
102 edit_commands_.push_back(ui::TextEditCommandAuraLinux(command, value)); | 102 edit_commands_.push_back(ui::TextEditCommandAuraLinux(command, value)); |
103 } | 103 } |
104 | 104 |
105 void Gtk2KeyBindingsHandler::BuildGdkEventKeyFromXEvent( | 105 void Gtk2KeyBindingsHandler::BuildGdkEventKeyFromXEvent( |
106 const base::NativeEvent& xevent, | 106 const base::NativeEvent& xevent, |
107 GdkEventKey* gdk_event) { | 107 GdkEventKey* gdk_event) { |
108 GdkKeymap *keymap = gdk_keymap_get_for_display(gdk_display_get_default()); | 108 GdkKeymap* keymap = gdk_keymap_get_for_display(gdk_display_get_default()); |
109 GdkModifierType consumed, state; | 109 GdkModifierType consumed, state; |
110 | 110 |
111 gdk_event->type = xevent->xany.type == KeyPress ? | 111 gdk_event->type = |
112 GDK_KEY_PRESS : GDK_KEY_RELEASE; | 112 xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE; |
113 gdk_event->time = xevent->xkey.time; | 113 gdk_event->time = xevent->xkey.time; |
114 gdk_event->state = static_cast<GdkModifierType>(xevent->xkey.state); | 114 gdk_event->state = static_cast<GdkModifierType>(xevent->xkey.state); |
115 gdk_event->hardware_keycode = xevent->xkey.keycode; | 115 gdk_event->hardware_keycode = xevent->xkey.keycode; |
116 | 116 |
117 if (has_xkb_) { | 117 if (has_xkb_) { |
118 gdk_event->group = XkbGroupForCoreState(xevent->xkey.state); | 118 gdk_event->group = XkbGroupForCoreState(xevent->xkey.state); |
119 } else { | 119 } else { |
120 // The overwhelming majority of people will be using X servers that support | 120 // The overwhelming majority of people will be using X servers that support |
121 // XKB. GDK has a fallback here that does some complicated stuff to detect | 121 // XKB. GDK has a fallback here that does some complicated stuff to detect |
122 // whether a modifier key affects the keybinding, but that should be | 122 // whether a modifier key affects the keybinding, but that should be |
123 // extremely rare. | 123 // extremely rare. |
124 static bool logged = false; | 124 static bool logged = false; |
125 if (!logged) { | 125 if (!logged) { |
126 NOTIMPLEMENTED(); | 126 NOTIMPLEMENTED(); |
127 logged = true; | 127 logged = true; |
128 } | 128 } |
129 gdk_event->group = 0; | 129 gdk_event->group = 0; |
130 } | 130 } |
131 | 131 |
132 gdk_event->keyval = GDK_KEY_VoidSymbol; | 132 gdk_event->keyval = GDK_KEY_VoidSymbol; |
133 gdk_keymap_translate_keyboard_state( | 133 gdk_keymap_translate_keyboard_state( |
134 keymap, | 134 keymap, gdk_event->hardware_keycode, |
135 gdk_event->hardware_keycode, | 135 static_cast<GdkModifierType>(gdk_event->state), gdk_event->group, |
136 static_cast<GdkModifierType>(gdk_event->state), | 136 &gdk_event->keyval, NULL, NULL, &consumed); |
137 gdk_event->group, | |
138 &gdk_event->keyval, | |
139 NULL, NULL, &consumed); | |
140 | 137 |
141 state = static_cast<GdkModifierType>(gdk_event->state & ~consumed); | 138 state = static_cast<GdkModifierType>(gdk_event->state & ~consumed); |
142 gdk_keymap_add_virtual_modifiers(keymap, &state); | 139 gdk_keymap_add_virtual_modifiers(keymap, &state); |
143 gdk_event->state |= state; | 140 gdk_event->state |= state; |
144 } | 141 } |
145 | 142 |
146 void Gtk2KeyBindingsHandler::HandlerInit(Handler *self) { | 143 void Gtk2KeyBindingsHandler::HandlerInit(Handler* self) { |
147 self->owner = NULL; | 144 self->owner = NULL; |
148 } | 145 } |
149 | 146 |
150 void Gtk2KeyBindingsHandler::HandlerClassInit(HandlerClass *klass) { | 147 void Gtk2KeyBindingsHandler::HandlerClassInit(HandlerClass* klass) { |
151 GtkTextViewClass* text_view_class = GTK_TEXT_VIEW_CLASS(klass); | 148 GtkTextViewClass* text_view_class = GTK_TEXT_VIEW_CLASS(klass); |
152 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); | 149 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); |
153 | 150 |
154 // Overrides all virtual methods related to editor key bindings. | 151 // Overrides all virtual methods related to editor key bindings. |
155 text_view_class->backspace = BackSpace; | 152 text_view_class->backspace = BackSpace; |
156 text_view_class->copy_clipboard = CopyClipboard; | 153 text_view_class->copy_clipboard = CopyClipboard; |
157 text_view_class->cut_clipboard = CutClipboard; | 154 text_view_class->cut_clipboard = CutClipboard; |
158 text_view_class->delete_from_cursor = DeleteFromCursor; | 155 text_view_class->delete_from_cursor = DeleteFromCursor; |
159 text_view_class->insert_at_cursor = InsertAtCursor; | 156 text_view_class->insert_at_cursor = InsertAtCursor; |
160 text_view_class->move_cursor = MoveCursor; | 157 text_view_class->move_cursor = MoveCursor; |
161 text_view_class->paste_clipboard = PasteClipboard; | 158 text_view_class->paste_clipboard = PasteClipboard; |
162 text_view_class->set_anchor = SetAnchor; | 159 text_view_class->set_anchor = SetAnchor; |
163 text_view_class->toggle_overwrite = ToggleOverwrite; | 160 text_view_class->toggle_overwrite = ToggleOverwrite; |
164 widget_class->show_help = ShowHelp; | 161 widget_class->show_help = ShowHelp; |
165 | 162 |
166 // "move-focus", "move-viewport", "select-all" and "toggle-cursor-visible" | 163 // "move-focus", "move-viewport", "select-all" and "toggle-cursor-visible" |
167 // have no corresponding virtual methods. Since glib 2.18 (gtk 2.14), | 164 // have no corresponding virtual methods. Since glib 2.18 (gtk 2.14), |
168 // g_signal_override_class_handler() is introduced to override a signal | 165 // g_signal_override_class_handler() is introduced to override a signal |
169 // handler. | 166 // handler. |
170 g_signal_override_class_handler("move-focus", | 167 g_signal_override_class_handler("move-focus", G_TYPE_FROM_CLASS(klass), |
171 G_TYPE_FROM_CLASS(klass), | |
172 G_CALLBACK(MoveFocus)); | 168 G_CALLBACK(MoveFocus)); |
173 | 169 |
174 g_signal_override_class_handler("move-viewport", | 170 g_signal_override_class_handler("move-viewport", G_TYPE_FROM_CLASS(klass), |
175 G_TYPE_FROM_CLASS(klass), | |
176 G_CALLBACK(MoveViewport)); | 171 G_CALLBACK(MoveViewport)); |
177 | 172 |
178 g_signal_override_class_handler("select-all", | 173 g_signal_override_class_handler("select-all", G_TYPE_FROM_CLASS(klass), |
179 G_TYPE_FROM_CLASS(klass), | |
180 G_CALLBACK(SelectAll)); | 174 G_CALLBACK(SelectAll)); |
181 | 175 |
182 g_signal_override_class_handler("toggle-cursor-visible", | 176 g_signal_override_class_handler("toggle-cursor-visible", |
183 G_TYPE_FROM_CLASS(klass), | 177 G_TYPE_FROM_CLASS(klass), |
184 G_CALLBACK(ToggleCursorVisible)); | 178 G_CALLBACK(ToggleCursorVisible)); |
185 } | 179 } |
186 | 180 |
187 GType Gtk2KeyBindingsHandler::HandlerGetType() { | 181 GType Gtk2KeyBindingsHandler::HandlerGetType() { |
188 static volatile gsize type_id_volatile = 0; | 182 static volatile gsize type_id_volatile = 0; |
189 if (g_once_init_enter(&type_id_volatile)) { | 183 if (g_once_init_enter(&type_id_volatile)) { |
190 GType type_id = g_type_register_static_simple( | 184 GType type_id = g_type_register_static_simple( |
191 GTK_TYPE_TEXT_VIEW, | 185 GTK_TYPE_TEXT_VIEW, g_intern_static_string("Gtk2KeyBindingsHandler"), |
192 g_intern_static_string("Gtk2KeyBindingsHandler"), | |
193 sizeof(HandlerClass), | 186 sizeof(HandlerClass), |
194 reinterpret_cast<GClassInitFunc>(HandlerClassInit), | 187 reinterpret_cast<GClassInitFunc>(HandlerClassInit), sizeof(Handler), |
195 sizeof(Handler), | |
196 reinterpret_cast<GInstanceInitFunc>(HandlerInit), | 188 reinterpret_cast<GInstanceInitFunc>(HandlerInit), |
197 static_cast<GTypeFlags>(0)); | 189 static_cast<GTypeFlags>(0)); |
198 g_once_init_leave(&type_id_volatile, type_id); | 190 g_once_init_leave(&type_id_volatile, type_id); |
199 } | 191 } |
200 return type_id_volatile; | 192 return type_id_volatile; |
201 } | 193 } |
202 | 194 |
203 Gtk2KeyBindingsHandler* Gtk2KeyBindingsHandler::GetHandlerOwner( | 195 Gtk2KeyBindingsHandler* Gtk2KeyBindingsHandler::GetHandlerOwner( |
204 GtkTextView* text_view) { | 196 GtkTextView* text_view) { |
205 Handler* handler = G_TYPE_CHECK_INSTANCE_CAST( | 197 Handler* handler = |
206 text_view, HandlerGetType(), Handler); | 198 G_TYPE_CHECK_INSTANCE_CAST(text_view, HandlerGetType(), Handler); |
207 DCHECK(handler); | 199 DCHECK(handler); |
208 return handler->owner; | 200 return handler->owner; |
209 } | 201 } |
210 | 202 |
211 void Gtk2KeyBindingsHandler::BackSpace(GtkTextView* text_view) { | 203 void Gtk2KeyBindingsHandler::BackSpace(GtkTextView* text_view) { |
212 GetHandlerOwner(text_view)->EditCommandMatched( | 204 GetHandlerOwner(text_view)->EditCommandMatched( |
213 TextEditCommand::DELETE_BACKWARD, std::string()); | 205 TextEditCommand::DELETE_BACKWARD, std::string()); |
214 } | 206 } |
215 | 207 |
216 void Gtk2KeyBindingsHandler::CopyClipboard(GtkTextView* text_view) { | 208 void Gtk2KeyBindingsHandler::CopyClipboard(GtkTextView* text_view) { |
217 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::COPY, | 209 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::COPY, |
218 std::string()); | 210 std::string()); |
219 } | 211 } |
220 | 212 |
221 void Gtk2KeyBindingsHandler::CutClipboard(GtkTextView* text_view) { | 213 void Gtk2KeyBindingsHandler::CutClipboard(GtkTextView* text_view) { |
222 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::CUT, | 214 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::CUT, |
223 std::string()); | 215 std::string()); |
224 } | 216 } |
225 | 217 |
226 void Gtk2KeyBindingsHandler::DeleteFromCursor( | 218 void Gtk2KeyBindingsHandler::DeleteFromCursor(GtkTextView* text_view, |
227 GtkTextView* text_view, GtkDeleteType type, gint count) { | 219 GtkDeleteType type, |
| 220 gint count) { |
228 if (!count) | 221 if (!count) |
229 return; | 222 return; |
230 | 223 |
231 TextEditCommand commands[2] = { | 224 TextEditCommand commands[2] = { |
232 TextEditCommand::INVALID_COMMAND, TextEditCommand::INVALID_COMMAND, | 225 TextEditCommand::INVALID_COMMAND, TextEditCommand::INVALID_COMMAND, |
233 }; | 226 }; |
234 switch (type) { | 227 switch (type) { |
235 case GTK_DELETE_CHARS: | 228 case GTK_DELETE_CHARS: |
236 commands[0] = (count > 0 ? TextEditCommand::DELETE_FORWARD | 229 commands[0] = (count > 0 ? TextEditCommand::DELETE_FORWARD |
237 : TextEditCommand::DELETE_BACKWARD); | 230 : TextEditCommand::DELETE_BACKWARD); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 } | 275 } |
283 | 276 |
284 void Gtk2KeyBindingsHandler::InsertAtCursor(GtkTextView* text_view, | 277 void Gtk2KeyBindingsHandler::InsertAtCursor(GtkTextView* text_view, |
285 const gchar* str) { | 278 const gchar* str) { |
286 if (str && *str) { | 279 if (str && *str) { |
287 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::INSERT_TEXT, | 280 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::INSERT_TEXT, |
288 str); | 281 str); |
289 } | 282 } |
290 } | 283 } |
291 | 284 |
292 void Gtk2KeyBindingsHandler::MoveCursor( | 285 void Gtk2KeyBindingsHandler::MoveCursor(GtkTextView* text_view, |
293 GtkTextView* text_view, GtkMovementStep step, gint count, | 286 GtkMovementStep step, |
294 gboolean extend_selection) { | 287 gint count, |
| 288 gboolean extend_selection) { |
295 if (!count) | 289 if (!count) |
296 return; | 290 return; |
297 | 291 |
298 TextEditCommand command; | 292 TextEditCommand command; |
299 switch (step) { | 293 switch (step) { |
300 case GTK_MOVEMENT_LOGICAL_POSITIONS: | 294 case GTK_MOVEMENT_LOGICAL_POSITIONS: |
301 if (extend_selection) { | 295 if (extend_selection) { |
302 command = | 296 command = |
303 (count > 0 ? TextEditCommand::MOVE_FORWARD_AND_MODIFY_SELECTION | 297 (count > 0 ? TextEditCommand::MOVE_FORWARD_AND_MODIFY_SELECTION |
304 : TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION); | 298 : TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 return; | 382 return; |
389 } | 383 } |
390 | 384 |
391 Gtk2KeyBindingsHandler* owner = GetHandlerOwner(text_view); | 385 Gtk2KeyBindingsHandler* owner = GetHandlerOwner(text_view); |
392 if (count < 0) | 386 if (count < 0) |
393 count = -count; | 387 count = -count; |
394 for (; count > 0; --count) | 388 for (; count > 0; --count) |
395 owner->EditCommandMatched(command, std::string()); | 389 owner->EditCommandMatched(command, std::string()); |
396 } | 390 } |
397 | 391 |
398 void Gtk2KeyBindingsHandler::MoveViewport( | 392 void Gtk2KeyBindingsHandler::MoveViewport(GtkTextView* text_view, |
399 GtkTextView* text_view, GtkScrollStep step, gint count) { | 393 GtkScrollStep step, |
| 394 gint count) { |
400 // Not supported by webkit. | 395 // Not supported by webkit. |
401 } | 396 } |
402 | 397 |
403 void Gtk2KeyBindingsHandler::PasteClipboard(GtkTextView* text_view) { | 398 void Gtk2KeyBindingsHandler::PasteClipboard(GtkTextView* text_view) { |
404 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::PASTE, | 399 GetHandlerOwner(text_view)->EditCommandMatched(TextEditCommand::PASTE, |
405 std::string()); | 400 std::string()); |
406 } | 401 } |
407 | 402 |
408 void Gtk2KeyBindingsHandler::SelectAll(GtkTextView* text_view, | 403 void Gtk2KeyBindingsHandler::SelectAll(GtkTextView* text_view, |
409 gboolean select) { | 404 gboolean select) { |
(...skipping 20 matching lines...) Expand all Loading... |
430 // Just for disabling the default handler. | 425 // Just for disabling the default handler. |
431 return FALSE; | 426 return FALSE; |
432 } | 427 } |
433 | 428 |
434 void Gtk2KeyBindingsHandler::MoveFocus(GtkWidget* widget, | 429 void Gtk2KeyBindingsHandler::MoveFocus(GtkWidget* widget, |
435 GtkDirectionType arg1) { | 430 GtkDirectionType arg1) { |
436 // Just for disabling the default handler. | 431 // Just for disabling the default handler. |
437 } | 432 } |
438 | 433 |
439 } // namespace libgtkui | 434 } // namespace libgtkui |
OLD | NEW |