1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "mozilla/ArrayUtils.h"
7 #include "mozilla/MathAlgorithms.h"
8 #include "mozilla/TextEvents.h"
9
10 #include "NativeKeyBindings.h"
11 #include "nsString.h"
12 #include "nsMemory.h"
13 #include "nsGtkKeyUtils.h"
14
15 #include <gtk/gtk.h>
16 #include <gdk/gdkkeysyms.h>
17 #include <gdk/gdk.h>
18
19 namespace mozilla {
20 namespace widget {
21
22 static nsIWidget::DoCommandCallback gCurrentCallback;
23 static void *gCurrentCallbackData;
24 static bool gHandled;
25
26 // Common GtkEntry and GtkTextView signals
27 static void
copy_clipboard_cb(GtkWidget * w,gpointer user_data)28 copy_clipboard_cb(GtkWidget *w, gpointer user_data)
29 {
30 gCurrentCallback(CommandCopy, gCurrentCallbackData);
31 g_signal_stop_emission_by_name(w, "copy_clipboard");
32 gHandled = true;
33 }
34
35 static void
cut_clipboard_cb(GtkWidget * w,gpointer user_data)36 cut_clipboard_cb(GtkWidget *w, gpointer user_data)
37 {
38 gCurrentCallback(CommandCut, gCurrentCallbackData);
39 g_signal_stop_emission_by_name(w, "cut_clipboard");
40 gHandled = true;
41 }
42
43 // GTK distinguishes between display lines (wrapped, as they appear on the
44 // screen) and paragraphs, which are runs of text terminated by a newline.
45 // We don't have this distinction, so we always use editor's notion of
46 // lines, which are newline-terminated.
47
48 static const Command sDeleteCommands[][2] = {
49 // backward, forward
50 { CommandDeleteCharBackward, CommandDeleteCharForward }, // CHARS
51 { CommandDeleteWordBackward, CommandDeleteWordForward }, // WORD_ENDS
52 { CommandDeleteWordBackward, CommandDeleteWordForward }, // WORDS
53 { CommandDeleteToBeginningOfLine, CommandDeleteToEndOfLine }, // LINES
54 { CommandDeleteToBeginningOfLine, CommandDeleteToEndOfLine }, // LINE_ENDS
55 { CommandDeleteToBeginningOfLine, CommandDeleteToEndOfLine }, // PARAGRAPH_ENDS
56 { CommandDeleteToBeginningOfLine, CommandDeleteToEndOfLine }, // PARAGRAPHS
57 // This deletes from the end of the previous word to the beginning of the
58 // next word, but only if the caret is not in a word.
59 // XXX need to implement in editor
60 { CommandDoNothing, CommandDoNothing } // WHITESPACE
61 };
62
63 static void
delete_from_cursor_cb(GtkWidget * w,GtkDeleteType del_type,gint count,gpointer user_data)64 delete_from_cursor_cb(GtkWidget *w, GtkDeleteType del_type,
65 gint count, gpointer user_data)
66 {
67 g_signal_stop_emission_by_name(w, "delete_from_cursor");
68 bool forward = count > 0;
69
70 #if (MOZ_WIDGET_GTK == 3)
71 // Ignore GTK's Ctrl-K keybinding introduced in GTK 3.14 and removed in
72 // 3.18 if the user has custom bindings set. See bug 1176929.
73 if (del_type == GTK_DELETE_PARAGRAPH_ENDS && forward && GTK_IS_ENTRY(w) &&
74 !gtk_check_version(3, 14, 1) && gtk_check_version(3, 17, 9)) {
75 GtkStyleContext* context = gtk_widget_get_style_context(w);
76 GtkStateFlags flags = gtk_widget_get_state_flags(w);
77
78 GPtrArray* array;
79 gtk_style_context_get(context, flags, "gtk-key-bindings", &array, nullptr);
80 if (!array)
81 return;
82 g_ptr_array_unref(array);
83 }
84 #endif
85
86 gHandled = true;
87 if (uint32_t(del_type) >= ArrayLength(sDeleteCommands)) {
88 // unsupported deletion type
89 return;
90 }
91
92 if (del_type == GTK_DELETE_WORDS) {
93 // This works like word_ends, except we first move the caret to the
94 // beginning/end of the current word.
95 if (forward) {
96 gCurrentCallback(CommandWordNext, gCurrentCallbackData);
97 gCurrentCallback(CommandWordPrevious, gCurrentCallbackData);
98 } else {
99 gCurrentCallback(CommandWordPrevious, gCurrentCallbackData);
100 gCurrentCallback(CommandWordNext, gCurrentCallbackData);
101 }
102 } else if (del_type == GTK_DELETE_DISPLAY_LINES ||
103 del_type == GTK_DELETE_PARAGRAPHS) {
104
105 // This works like display_line_ends, except we first move the caret to the
106 // beginning/end of the current line.
107 if (forward) {
108 gCurrentCallback(CommandBeginLine, gCurrentCallbackData);
109 } else {
110 gCurrentCallback(CommandEndLine, gCurrentCallbackData);
111 }
112 }
113
114 Command command = sDeleteCommands[del_type][forward];
115 if (!command) {
116 return; // unsupported command
117 }
118
119 unsigned int absCount = Abs(count);
120 for (unsigned int i = 0; i < absCount; ++i) {
121 gCurrentCallback(command, gCurrentCallbackData);
122 }
123 }
124
125 static const Command sMoveCommands[][2][2] = {
126 // non-extend { backward, forward }, extend { backward, forward }
127 // GTK differentiates between logical position, which is prev/next,
128 // and visual position, which is always left/right.
129 // We should fix this to work the same way for RTL text input.
130 { // LOGICAL_POSITIONS
131 { CommandCharPrevious, CommandCharNext },
132 { CommandSelectCharPrevious, CommandSelectCharNext }
133 },
134 { // VISUAL_POSITIONS
135 { CommandCharPrevious, CommandCharNext },
136 { CommandSelectCharPrevious, CommandSelectCharNext }
137 },
138 { // WORDS
139 { CommandWordPrevious, CommandWordNext },
140 { CommandSelectWordPrevious, CommandSelectWordNext }
141 },
142 { // DISPLAY_LINES
143 { CommandLinePrevious, CommandLineNext },
144 { CommandSelectLinePrevious, CommandSelectLineNext }
145 },
146 { // DISPLAY_LINE_ENDS
147 { CommandBeginLine, CommandEndLine },
148 { CommandSelectBeginLine, CommandSelectEndLine }
149 },
150 { // PARAGRAPHS
151 { CommandLinePrevious, CommandLineNext },
152 { CommandSelectLinePrevious, CommandSelectLineNext }
153 },
154 { // PARAGRAPH_ENDS
155 { CommandBeginLine, CommandEndLine },
156 { CommandSelectBeginLine, CommandSelectEndLine }
157 },
158 { // PAGES
159 { CommandMovePageUp, CommandMovePageDown },
160 { CommandSelectPageUp, CommandSelectPageDown }
161 },
162 { // BUFFER_ENDS
163 { CommandMoveTop, CommandMoveBottom },
164 { CommandSelectTop, CommandSelectBottom }
165 },
166 { // HORIZONTAL_PAGES (unsupported)
167 { CommandDoNothing, CommandDoNothing },
168 { CommandDoNothing, CommandDoNothing }
169 }
170 };
171
172 static void
move_cursor_cb(GtkWidget * w,GtkMovementStep step,gint count,gboolean extend_selection,gpointer user_data)173 move_cursor_cb(GtkWidget *w, GtkMovementStep step, gint count,
174 gboolean extend_selection, gpointer user_data)
175 {
176 g_signal_stop_emission_by_name(w, "move_cursor");
177 gHandled = true;
178 bool forward = count > 0;
179 if (uint32_t(step) >= ArrayLength(sMoveCommands)) {
180 // unsupported movement type
181 return;
182 }
183
184 Command command = sMoveCommands[step][extend_selection][forward];
185 if (!command) {
186 return; // unsupported command
187 }
188
189 unsigned int absCount = Abs(count);
190 for (unsigned int i = 0; i < absCount; ++i) {
191 gCurrentCallback(command, gCurrentCallbackData);
192 }
193 }
194
195 static void
paste_clipboard_cb(GtkWidget * w,gpointer user_data)196 paste_clipboard_cb(GtkWidget *w, gpointer user_data)
197 {
198 gCurrentCallback(CommandPaste, gCurrentCallbackData);
199 g_signal_stop_emission_by_name(w, "paste_clipboard");
200 gHandled = true;
201 }
202
203 // GtkTextView-only signals
204 static void
select_all_cb(GtkWidget * w,gboolean select,gpointer user_data)205 select_all_cb(GtkWidget *w, gboolean select, gpointer user_data)
206 {
207 gCurrentCallback(CommandSelectAll, gCurrentCallbackData);
208 g_signal_stop_emission_by_name(w, "select_all");
209 gHandled = true;
210 }
211
212 NativeKeyBindings* NativeKeyBindings::sInstanceForSingleLineEditor = nullptr;
213 NativeKeyBindings* NativeKeyBindings::sInstanceForMultiLineEditor = nullptr;
214
215 // static
216 NativeKeyBindings*
GetInstance(NativeKeyBindingsType aType)217 NativeKeyBindings::GetInstance(NativeKeyBindingsType aType)
218 {
219 switch (aType) {
220 case nsIWidget::NativeKeyBindingsForSingleLineEditor:
221 if (!sInstanceForSingleLineEditor) {
222 sInstanceForSingleLineEditor = new NativeKeyBindings();
223 sInstanceForSingleLineEditor->Init(aType);
224 }
225 return sInstanceForSingleLineEditor;
226
227 default:
228 // fallback to multiline editor case in release build
229 MOZ_FALLTHROUGH_ASSERT("aType is invalid or not yet implemented");
230 case nsIWidget::NativeKeyBindingsForMultiLineEditor:
231 case nsIWidget::NativeKeyBindingsForRichTextEditor:
232 if (!sInstanceForMultiLineEditor) {
233 sInstanceForMultiLineEditor = new NativeKeyBindings();
234 sInstanceForMultiLineEditor->Init(aType);
235 }
236 return sInstanceForMultiLineEditor;
237 }
238 }
239
240 // static
241 void
Shutdown()242 NativeKeyBindings::Shutdown()
243 {
244 delete sInstanceForSingleLineEditor;
245 sInstanceForSingleLineEditor = nullptr;
246 delete sInstanceForMultiLineEditor;
247 sInstanceForMultiLineEditor = nullptr;
248 }
249
250 void
Init(NativeKeyBindingsType aType)251 NativeKeyBindings::Init(NativeKeyBindingsType aType)
252 {
253 switch (aType) {
254 case nsIWidget::NativeKeyBindingsForSingleLineEditor:
255 mNativeTarget = gtk_entry_new();
256 break;
257 default:
258 mNativeTarget = gtk_text_view_new();
259 if (gtk_major_version > 2 ||
260 (gtk_major_version == 2 && (gtk_minor_version > 2 ||
261 (gtk_minor_version == 2 &&
262 gtk_micro_version >= 2)))) {
263 // select_all only exists in gtk >= 2.2.2. Prior to that,
264 // ctrl+a is bound to (move to beginning, select to end).
265 g_signal_connect(mNativeTarget, "select_all",
266 G_CALLBACK(select_all_cb), this);
267 }
268 break;
269 }
270
271 g_object_ref_sink(mNativeTarget);
272
273 g_signal_connect(mNativeTarget, "copy_clipboard",
274 G_CALLBACK(copy_clipboard_cb), this);
275 g_signal_connect(mNativeTarget, "cut_clipboard",
276 G_CALLBACK(cut_clipboard_cb), this);
277 g_signal_connect(mNativeTarget, "delete_from_cursor",
278 G_CALLBACK(delete_from_cursor_cb), this);
279 g_signal_connect(mNativeTarget, "move_cursor",
280 G_CALLBACK(move_cursor_cb), this);
281 g_signal_connect(mNativeTarget, "paste_clipboard",
282 G_CALLBACK(paste_clipboard_cb), this);
283 }
284
~NativeKeyBindings()285 NativeKeyBindings::~NativeKeyBindings()
286 {
287 gtk_widget_destroy(mNativeTarget);
288 g_object_unref(mNativeTarget);
289 }
290
291 bool
Execute(const WidgetKeyboardEvent & aEvent,DoCommandCallback aCallback,void * aCallbackData)292 NativeKeyBindings::Execute(const WidgetKeyboardEvent& aEvent,
293 DoCommandCallback aCallback,
294 void* aCallbackData)
295 {
296 // If the native key event is set, it must be synthesized for tests.
297 // We just ignore such events because this behavior depends on system
298 // settings.
299 if (!aEvent.mNativeKeyEvent) {
300 // It must be synthesized event or dispatched DOM event from chrome.
301 return false;
302 }
303
304 guint keyval;
305
306 if (aEvent.mCharCode) {
307 keyval = gdk_unicode_to_keyval(aEvent.mCharCode);
308 } else {
309 keyval =
310 static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent)->keyval;
311 }
312
313 if (ExecuteInternal(aEvent, aCallback, aCallbackData, keyval)) {
314 return true;
315 }
316
317 for (uint32_t i = 0; i < aEvent.mAlternativeCharCodes.Length(); ++i) {
318 uint32_t ch = aEvent.IsShift() ?
319 aEvent.mAlternativeCharCodes[i].mShiftedCharCode :
320 aEvent.mAlternativeCharCodes[i].mUnshiftedCharCode;
321 if (ch && ch != aEvent.mCharCode) {
322 keyval = gdk_unicode_to_keyval(ch);
323 if (ExecuteInternal(aEvent, aCallback, aCallbackData, keyval)) {
324 return true;
325 }
326 }
327 }
328
329 /*
330 gtk_bindings_activate_event is preferable, but it has unresolved bug:
331 http://bugzilla.gnome.org/show_bug.cgi?id=162726
332 The bug was already marked as FIXED. However, somebody reports that the
333 bug still exists.
334 Also gtk_bindings_activate may work with some non-shortcuts operations
335 (todo: check it). See bug 411005 and bug 406407.
336
337 Code, which should be used after fixing GNOME bug 162726:
338
339 gtk_bindings_activate_event(GTK_OBJECT(mNativeTarget),
340 static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent));
341 */
342
343 return false;
344 }
345
346 bool
ExecuteInternal(const WidgetKeyboardEvent & aEvent,DoCommandCallback aCallback,void * aCallbackData,guint aKeyval)347 NativeKeyBindings::ExecuteInternal(const WidgetKeyboardEvent& aEvent,
348 DoCommandCallback aCallback,
349 void* aCallbackData,
350 guint aKeyval)
351 {
352 guint modifiers =
353 static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent)->state;
354
355 gCurrentCallback = aCallback;
356 gCurrentCallbackData = aCallbackData;
357
358 gHandled = false;
359 #if (MOZ_WIDGET_GTK == 2)
360 gtk_bindings_activate(GTK_OBJECT(mNativeTarget),
361 aKeyval, GdkModifierType(modifiers));
362 #else
363 gtk_bindings_activate(G_OBJECT(mNativeTarget),
364 aKeyval, GdkModifierType(modifiers));
365 #endif
366
367 gCurrentCallback = nullptr;
368 gCurrentCallbackData = nullptr;
369
370 return gHandled;
371 }
372
373 } // namespace widget
374 } // namespace mozilla
375