Can "enter-notify-event" be used on a GtkImage ?



Hi all,

on the gtk_widget devhelp page, there is dome documentation about signals
common to all widgets. supposedly.

I have a gtk_image, and i'd like to receive enter/leave notifications.

I have enabled GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK (as near as I can
tell) on the widget (gtk_widget_add_events), and my signal handler just isn't
firing...

If I instead enable the NOTIFY_MASK's on an EventBox, then the signal handler
see's the signals.

As a proof of concept, I took the paned.c example from the gtk+ examples, and
modified it (attached) (you'll need to supply your own image :) Anyway, if I
change the g_signal_connect's and the gtk_widget_add_events to the eboxprev (a
GtkEventBox), then I get the interrupts I want. But the problem w/ that approach
is that the event box covers an area greater than the image. I really only want
enter/leave on the image.

It seems to me that somehow I ought to be able to trigger enter/leave events on
the image itself. Or can not that be done ?

- -Greg

(p.s. I do note the disclaimer surrounding the enter-notify-event /
leave-notify-event's. It says "This signal will be sent to the grab widget if
there is one." I suppose the issue is that the GtkImage isn't "grabbing" the
focus, yes ? any way around that ?

+---------------------------------------------------------------------+

Please also check the log file at "/dev/null" for additional information.
                (from /var/log/Xorg.setup.log)

| Greg Hosler                                   greg hosler per sg    |
+---------------------------------------------------------------------+
#include <stdio.h>
#include <gtk/gtk.h>
   
/* Create the list of "messages" */
static GtkWidget *create_list( void )
{

    GtkWidget *scrolled_window;
    GtkWidget *tree_view;
    GtkListStore *model;
    GtkTreeIter iter;
    GtkCellRenderer *cell;
    GtkTreeViewColumn *column;

    int i;
   
    /* Create a new scrolled window, with scrollbars only if needed */
    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				    GTK_POLICY_AUTOMATIC, 
				    GTK_POLICY_AUTOMATIC);
   
    model = gtk_list_store_new (1, G_TYPE_STRING);
    tree_view = gtk_tree_view_new ();
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window)
, 
                                           tree_view);
    gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
    gtk_widget_show (tree_view);
   
    /* Add some messages to the window */
    for (i = 0; i < 10; i++) {
        gchar *msg = g_strdup_printf ("Message #%d", i);
        gtk_list_store_append (GTK_LIST_STORE (model), &iter);
        gtk_list_store_set (GTK_LIST_STORE (model), 
	                    &iter,
                            0, msg,
	                    -1);
	g_free (msg);
    }
   
    cell = gtk_cell_renderer_text_new ();

    column = gtk_tree_view_column_new_with_attributes ("Messages",
                                                       cell,
                                                       "text", 0,
                                                       NULL);
  
    gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	  		         GTK_TREE_VIEW_COLUMN (column));

    return scrolled_window;
}
   
/* Add some text to our text widget - this is a callback that is invoked
when our window is realized. We could also force our window to be
realized with gtk_widget_realize, but it would have to be part of
a hierarchy first */

static void insert_text( GtkTextBuffer *buffer )
{
   GtkTextIter iter;
 
   gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);

   gtk_text_buffer_insert (buffer, &iter,   
    "From: pathfinder nasa gov\n"
    "To: mom nasa gov\n"
    "Subject: Made it!\n"
    "\n"
    "We just got in this morning. The weather has been\n"
    "great - clear but cold, and there are lots of fun sights.\n"
    "Sojourner says hi. See you soon.\n"
    " -Path\n", -1);
}
   
/* Create a scrolled text area that displays a "message" */
static GtkWidget *create_text( void )
{
   GtkWidget *scrolled_window;
   GtkWidget *view;
   GtkTextBuffer *buffer;

   view = gtk_text_view_new ();
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));

   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
		   	           GTK_POLICY_AUTOMATIC,
				   GTK_POLICY_AUTOMATIC);

   gtk_container_add (GTK_CONTAINER (scrolled_window), view);
   insert_text (buffer);

   gtk_widget_show_all (scrolled_window);

   return scrolled_window;
}



gboolean on_image_enter(GtkWidget *widget, GdkEventCrossing *event, gpointer dat
a)
{

	printf("on image_entered\n");
	return TRUE;
}

gboolean on_image_leave(GtkWidget *widget, GdkEventCrossing *event, gpointer dat
a)
{

	printf("on image_leave\n");
	return TRUE;
}

gboolean on_image_motion(GtkWidget *widget, GdkEventCrossing *event, gpointer da
ta)
{

	printf("on image_motion\n");
	return TRUE;
}

   
int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *vpaned;
    GtkWidget *list;
    GtkWidget *text;

    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Paned Windows");
    g_signal_connect (G_OBJECT (window), "destroy",
	              G_CALLBACK (gtk_main_quit), NULL);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);

    /* create a vpaned widget and add it to our toplevel window */
   
    vpaned = gtk_vpaned_new ();
    gtk_container_add (GTK_CONTAINER (window), vpaned);
    gtk_widget_show (vpaned);
    /* Now create the contents of the two halves of the window */
   
    list = create_list ();
    gtk_paned_add1 (GTK_PANED (vpaned), list);
    gtk_widget_show (list);
   
    /*
     *  vbox (photoshare_vbox)			photoshare->vbox
     *    event_box (eboxprev)
     *      vbox (preview_image_frame)		photoshare->preview_image_frame
     *        scrolled_window (scrollbox)
     */


    GtkWidget *photoshare_vbox;
    GtkWidget *eboxprev;
    GtkWidget *preview_image_frame;
    GtkWidget *preview_image_canvas;
    GtkWidget *preview_image_ebox;
    GtkWidget *scrollbox;
    GtkWidget *preview_image;
    GdkPixbuf *pixbuf = NULL;
    GError *error = NULL;

	photoshare_vbox = gtk_vbox_new(FALSE, 0);
	gtk_paned_pack2(GTK_PANED(vpaned), photoshare_vbox, TRUE, TRUE);

        eboxprev = gtk_event_box_new(); 
        gtk_box_pack_start(GTK_BOX(photoshare_vbox), eboxprev, TRUE, TRUE, 0);


        preview_image_frame = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(eboxprev), preview_image_frame);


        scrollbox = gtk_scrolled_window_new(NULL, NULL);
        gtk_container_add(GTK_CONTAINER(preview_image_frame), scrollbox);

        gtk_container_set_border_width(GTK_CONTAINER(scrollbox), 0);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbox),
                                       GTK_POLICY_AUTOMATIC,
                                       GTK_POLICY_AUTOMATIC);

	preview_image_canvas = gtk_vbox_new(FALSE, 0);
        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbox), pr
eview_image_canvas);

	preview_image_ebox = gtk_event_box_new();
	gtk_box_pack_start(preview_image_canvas, preview_image_ebox, FALSE, FALSE, 0);

        preview_image = gtk_image_new();
	gtk_container_add(GTK_CONTAINER(preview_image_ebox), preview_image);

	pixbuf = gdk_pixbuf_new_from_file_at_size("/home/hosler/jpg/02930006.JPG", 500,
500, &error);
	gtk_image_set_from_pixbuf(GTK_IMAGE(preview_image), pixbuf);


    g_signal_connect(preview_image_ebox, "enter-notify-event",  G_CALLBACK(on_im
age_enter),  0);
    g_signal_connect(preview_image_ebox, "leave-notify-event",  G_CALLBACK(on_im
age_leave),  0);
    //    g_signal_connect(preview_image_ebox, "motion-notify-event", G_CALLBACK
(on_image_motion), 0);

    gtk_widget_add_events(preview_image_ebox,
			  GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK);

    gtk_widget_show_all (window);

    gtk_main ();

    return 0;
}


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