summaryrefslogtreecommitdiff
path: root/itches/emacs/pgtk-shift-space.c
blob: 7671d1dbcabf0ce702545045fe002d4a17ee283e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* -*- compile-command: "gcc -g $(pkg-config --cflags gtk+-3.0) pgtk-shift-space.c $(pkg-config --libs gtk+-3.0)" -*- */
#include <stdio.h>

#include <gtk/gtk.h>

static gboolean
show_key (GtkWidget *widget, GdkEventKey *event, gpointer data)
{
  const int mods[] = {GDK_SHIFT_MASK, GDK_CONTROL_MASK, GDK_MOD1_MASK};
  char const * const mods_names[] = {"shift", "control", "alt"};

  fprintf(stderr, "press:\t'%d'", event->keyval);

  for (size_t i=0; i<sizeof(mods)/sizeof(mods[0]); i++)
    if (event->state & mods[i])
      fprintf(stderr, " +%s", mods_names[i]);

  fprintf(stderr, "\n");

  if (gtk_im_context_filter_keypress (data, event))
    fprintf(stderr, "handled by gtkim\n");

  fprintf(stderr, "\n");

  return TRUE;
}

static void
show_commit (GtkIMContext *ctx, gchar *str, gpointer data)
{
  fprintf(stderr, "commit: \"%s\"\n", str);
}

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

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "GTK key scratchpad");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show_all (window);

  GtkIMContext *im_context = gtk_im_context_simple_new();
  gtk_im_context_set_client_window(im_context, gtk_widget_get_window(window));

  gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);

  g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (show_key),    im_context);
  g_signal_connect (im_context,        "commit",          G_CALLBACK (show_commit), NULL);
}

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

  app = gtk_application_new ("org.gtk.key-scratchpad",
#if GLIB_CHECK_VERSION(2, 74, 0)
                             G_APPLICATION_DEFAULT_FLAGS
#else
                             G_APPLICATION_FLAGS_NONE
#endif
                             );
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}