Vim keybindings setup in GtkSourceView

I couldn’t get gtksourceview’s vim bindings to work in my app (using gtk-rs), so I tried to get them working in the simplest C program I could but still failed. What am I doing wrong?

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  //GtkWidget *button;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Vim Test");

  GtkSourceView *view;
  view = (GtkSourceView*) gtk_source_view_new ();
  GtkIMContext *im_context = gtk_source_vim_im_context_new ();
  GtkEventController *key = gtk_event_controller_key_new ();

  gtk_event_controller_set_propagation_phase (key, GTK_PHASE_CAPTURE);
  gtk_event_controller_key_set_im_context (GTK_EVENT_CONTROLLER_KEY (key), im_context);
  gtk_widget_add_controller (GTK_WIDGET (view), key);

  gtk_window_set_child (GTK_WINDOW (window), GTK_WIDGET(view));

  gtk_window_present (GTK_WINDOW (window));
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("uk.hugom.vimimtestc", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Using gtk version 4.6.5 and sourceview 5.4.1 .

Forgot to mention, it is based on the code snippet in the documentation.

Cheers,
Hugo

Looking at gnome-text-editor, there’s a call to gtk_im_context_set_client_widget(), so maybe that’s missing?

Yup that’s exactly it. We need access to the widget to setup various event plumbing. I’ll update the documentation.

1 Like

That was it!
Thank you both so much!