key event on a GtkTextTag (GTK3) within an uneditable text view




Hello,

My previous emails lost their attachement. I am giving it in the
message here.

In the below example (which you could also read on
http://www.gtkforums.com/viewtopic.php?t=9108 if you want) I cannot
understand why the tag_event_cb callback is not getting any keyboard
event.

######################## file minigtktag.c ##############################
/** file minigtktag.c by Basile Starynkevitch (in the public domain)
 compile it with
   gcc -O -g -Wall minigtktag.c $(pkg-config --cflags --libs gtk
+-x11-3.0) -o minigtktag **/
#include <gtk/gtk.h>

GtkWindow *win;
GtkTextView *txview;
GtkTextBuffer *txbuf;
GtkTextTag *tag;
GtkApplication *appl;

#define myprintf(Fmt, ...) do{printf("%s:%d: <%s> " Fmt "\n", \
                                     __FILE__, __LINE__,  __func__, \
                                     ##__VA_ARGS__);}while(0)


static gboolean
tag_event_cb (GtkTextTag * tagp,
              GObject * object,
              GdkEvent * ev, GtkTextIter * iter, gpointer data)
{
  g_assert (object == (GObject *) txview);
  g_assert (tagp == tag);
  switch (ev->type)
    {
    case GDK_MOTION_NOTIFY:
      myprintf ("tag %p GDK_MOTION_NOTIFY x=%g y=%g",
                tag, ev->motion.x, ev->motion.y);
      break;
    case GDK_BUTTON_PRESS:
      myprintf ("tag %p  GDK_BUTTON_PRESS x=%g y=%g but=%d",
                tag,  ev->button.x, ev->button.y, ev->button.button);
      break;
    case GDK_BUTTON_RELEASE:
      myprintf ("tag %p  GDK_BUTTON_RELEASE x=%g y=%g but=%d",
                tag,  ev->button.x, ev->button.y, ev->button.button);
      break;
    case GDK_KEY_PRESS:
      myprintf ("tag %p  GDK_KEY_PRESS keyval=%ud",
                tag,  ev->key.keyval);
      break;
    case GDK_KEY_RELEASE:
      myprintf ("tag %p  GDK_KEY_RELEASE keyval=%ud",
                tag,  ev->key.keyval);
      break;
    default:
      myprintf ("tag %p txview %p ev %p type %d", tag, txview, ev,
ev->type); break;
    }
  /* return FALSE to propagate the event to other handlers */
  return FALSE;
}

static void activate_application_cb (GObject*obj, gconstpointer data)
{
  g_assert (obj == G_OBJECT(appl));
  myprintf("activated appl %p", appl);
  return;
}

static void
initialize (void)
{
  GtkTextIter endit = { };
  appl = gtk_application_new ("mini.gtktag", G_APPLICATION_FLAGS_NONE);
  win = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
  gtk_application_add_window (appl, win);
  myprintf ("appl %p win %p", appl, win);
  g_signal_connect ((GObject *) appl, "activate",
                    G_CALLBACK (activate_application_cb), NULL);
  gtk_window_set_title (win, "minigtktag");
  gtk_window_set_default_size (win, 600, 400);
  txbuf = gtk_text_buffer_new (NULL);
  txview = GTK_TEXT_VIEW (gtk_text_view_new_with_buffer (txbuf));
  gtk_text_view_set_editable (txview, FALSE);
  gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (txview));
  myprintf ("txbuf %p txview %p", txbuf, txview);
  tag =
    gtk_text_buffer_create_tag (txbuf, "Tag",
                                "foreground", "firebrick",
                                "weight", PANGO_WEIGHT_BOLD, NULL);
  g_signal_connect ((GObject *) tag, "event",
                    G_CALLBACK (tag_event_cb), NULL);
  myprintf ("tag %p", tag);
  gtk_text_buffer_get_end_iter (txbuf, &endit);
  gtk_text_buffer_insert (txbuf, &endit, "plain text\n", -1);
  gtk_text_buffer_insert_with_tags
    (txbuf, &endit, "my tagged text\n", -1, tag, NULL);
  gtk_text_buffer_insert (txbuf, &endit, "other plain text\n", -1);
  gtk_widget_show_all (GTK_WIDGET (win));
  myprintf ("at end txbuf %p", txbuf);
}

int
main (int argc, char **argv)
{
  int nok = 0;
  gtk_init (&argc, &argv);
  myprintf ("start pid %d", (int) getpid ());
  initialize ();
  myprintf ("after initialize appl %p", appl);
  nok = g_application_run (G_APPLICATION (appl), argc, argv);
  myprintf ("ended application nok %d", nok);
  return nok;
}
######################## end file minigtktag.c ##########################

Compile the above file, run  ./minigtktag, type something in the second
line "my tagged text", perhaps even a right-arrow. The tag_event_cb
callback function don't get any key event. I cannot understand why (of
course, the text view is not editable, because I want to control all
editions from my program).

I probably misunderstood something. What?

Regards.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]