Skip to content

Commit

Permalink
use GTK+ key bindings for the EditView
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed May 1, 2017
1 parent 59e5c7c commit e5a5498
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
2 changes: 0 additions & 2 deletions Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class Application: Gtk.Application {
});
add_accelerator("save", "<Control>S", () => notebook.get_current_edit_view().save());
add_accelerator("save-as", "<Control><Shift>S", () => notebook.get_current_edit_view().save_as());
add_accelerator("undo", "<Control>Z", () => notebook.get_current_edit_view().undo());
add_accelerator("redo", "<Control>Y", () => notebook.get_current_edit_view().redo());
add_accelerator("quit", "<Control>Q", () => window.close());
notebook = new Notebook();
window.add(notebook);
Expand Down
91 changes: 39 additions & 52 deletions EditView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ class EditView: Gtk.DrawingArea, Gtk.Scrollable {
}
}

private static void add_move_binding(Gtk.BindingSet binding_set, uint key, Gdk.ModifierType modifier, string command) {
Gtk.BindingEntry.add_signal(binding_set, key, modifier, "send-edit", 1, typeof(string), command);
Gtk.BindingEntry.add_signal(binding_set, key, modifier|Gdk.ModifierType.SHIFT_MASK, "send-edit", 1, typeof(string), command + "_and_modify_selection");
}

static construct {
unowned Gtk.BindingSet binding_set = Gtk.BindingSet.by_class((ObjectClass)typeof(EditView).class_ref());
add_move_binding(binding_set, Gdk.Key.Right, 0, "move_right");
add_move_binding(binding_set, Gdk.Key.Right, Gdk.ModifierType.CONTROL_MASK, "move_word_right");
add_move_binding(binding_set, Gdk.Key.Left, 0, "move_left");
add_move_binding(binding_set, Gdk.Key.Left, Gdk.ModifierType.CONTROL_MASK, "move_word_left");
add_move_binding(binding_set, Gdk.Key.Up, 0, "move_up");
add_move_binding(binding_set, Gdk.Key.Down, 0, "move_down");
add_move_binding(binding_set, Gdk.Key.Home, 0, "move_to_left_end_of_line");
add_move_binding(binding_set, Gdk.Key.Home, Gdk.ModifierType.CONTROL_MASK, "move_to_beginning_of_document");
add_move_binding(binding_set, Gdk.Key.End, 0, "move_to_right_end_of_line");
add_move_binding(binding_set, Gdk.Key.End, Gdk.ModifierType.CONTROL_MASK, "move_to_end_of_document");
add_move_binding(binding_set, Gdk.Key.Page_Up, 0, "page_up");
add_move_binding(binding_set, Gdk.Key.Page_Down, 0, "page_down");

Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.Return, 0, "send-edit", 1, typeof(string), "insert_newline");
Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.Tab, 0, "send-edit", 1, typeof(string), "insert_tab");
Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.BackSpace, 0, "send-edit", 1, typeof(string), "delete_backward");
Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.Delete, 0, "send-edit", 1, typeof(string), "delete_forward");

Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.a, Gdk.ModifierType.CONTROL_MASK, "send-edit", 1, typeof(string), "select_all");
Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.z, Gdk.ModifierType.CONTROL_MASK, "send-edit", 1, typeof(string), "undo");
Gtk.BindingEntry.add_signal(binding_set, Gdk.Key.y, Gdk.ModifierType.CONTROL_MASK, "send-edit", 1, typeof(string), "redo");
}

~EditView() {
core_connection.send_close_view(view_id);
}
Expand Down Expand Up @@ -128,55 +158,18 @@ class EditView: Gtk.DrawingArea, Gtk.Scrollable {
}

public override bool key_press_event(Gdk.EventKey event) {
if (im_context.filter_keypress(event)) {
if (base.key_press_event(event)) {
return Gdk.EVENT_STOP;
}
unowned string suffix = (event.state & Gdk.ModifierType.SHIFT_MASK) != 0 ? "_and_modify_selection" : "";
switch (event.keyval) {
case Gdk.Key.Return:
send_edit("insert_newline");
return Gdk.EVENT_STOP;
case Gdk.Key.BackSpace:
send_edit("delete_backward");
return Gdk.EVENT_STOP;
case Gdk.Key.Delete:
send_edit("delete_forward");
return Gdk.EVENT_STOP;
case Gdk.Key.Tab:
send_edit("insert_tab");
return Gdk.EVENT_STOP;
case Gdk.Key.Up:
send_edit("move_up" + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Right:
unowned string command = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0 ? "move_word_right" : "move_right";
send_edit(command + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Down:
send_edit("move_down" + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Left:
unowned string command = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0 ? "move_word_left" : "move_left";
send_edit(command + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Page_Up:
send_edit("page_up" + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Page_Down:
send_edit("page_down" + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.Home:
unowned string command = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0 ? "move_to_beginning_of_document" : "move_to_left_end_of_line";
send_edit(command + suffix);
return Gdk.EVENT_STOP;
case Gdk.Key.End:
unowned string command = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0 ? "move_to_end_of_document" : "move_to_right_end_of_line";
send_edit(command + suffix);
return Gdk.EVENT_STOP;
if (im_context.filter_keypress(event)) {
return Gdk.EVENT_STOP;
}
return Gdk.EVENT_PROPAGATE;
}
public override bool key_release_event(Gdk.EventKey event) {
if (base.key_release_event(event)) {
return Gdk.EVENT_STOP;
}
if (im_context.filter_keypress(event)) {
return Gdk.EVENT_STOP;
}
Expand Down Expand Up @@ -248,8 +241,9 @@ class EditView: Gtk.DrawingArea, Gtk.Scrollable {
}
}

public void send_edit(string method, Json.Object edit_params = new Json.Object()) {
core_connection.send_edit(view_id, method, edit_params);
[Signal(action=true)]
public virtual signal void send_edit(string method) {
core_connection.send_edit(view_id, method, new Json.Object());
}

public void save() {
Expand All @@ -269,13 +263,6 @@ class EditView: Gtk.DrawingArea, Gtk.Scrollable {
}
dialog.destroy();
}

public void undo() {
core_connection.send_edit(view_id, "undo");
}
public void redo() {
core_connection.send_edit(view_id, "redo");
}
}

}

0 comments on commit e5a5498

Please sign in to comment.