key_press_events and drawing_area



I'm experimenting with GTK and I'm writing this program that reads an
image, displays it and listens to mouse clicks and key_press_events. It
works fine if I connect to the window, but I can't seem to get the
keyboard events straight when connecting to the drawing_area. Right after
the image is displayed, the drawing_area does not react to any keyboard
input. If I press Tab, it starts to work. If I press Tab again, it locks
back.

I know this question has been asked before, and after searching the
archive I came accross gtk_signal_connect_after(). I know it solved the
problem for some, so it must be something else that I'm doing wrong.

I know it might be too much to ask someone else to debug my code, but, who
knows, maybe some kind soul will have mercy on me.

Here's the code.


/********************************* click.c **********************************

gcc `imlib-config --cflags-gdk` -o click click.c `imlib-config --libs-gdk`

******************************************************************************/

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <gdk_imlib.h>
#include <gdk/gdk.h>


GdkImlibImage *IM;

gboolean on_darea_expose (GtkWidget *widget,
			  GdkEventExpose *event,
			  gpointer user_data)
{
    gdk_draw_rgb_image (widget->window,
                        widget->style->fg_gc[GTK_STATE_NORMAL],
			0, 0, IM->rgb_width, IM->rgb_height,
			GDK_RGB_DITHER_MAX, IM->rgb_data, IM->rgb_width *3);
    return TRUE;
}


gint cb_quit(GtkWidget *widget, GdkEventButton * e, gpointer data)
{
    g_print("Mouse: x=%d\ty=%d\n\n", (int) (e->x), (int) (e->y));
    if(e->button & GDK_BUTTON1_MASK)
	g_print("Button 1\n\n");

    g_print("I've been killed...\n\n");
    gtk_main_quit();

    return(TRUE);
}


gint mouse_darea(GtkWidget * widget, GdkEventButton * e, gpointer data)
{
    g_print("Mouse in darea: x=%d\ty=%d\n\n", (int) (e->x), (int) (e->y));

    return TRUE;      /* don't propagate it */
}


gboolean viewer_key_press(GtkWidget *widget,
			  GdkEventKey * event, 
			  gpointer user_data)
{
    switch (event->keyval){
    case GDK_q:
	gtk_main_quit();
	break;
    default:
	g_print("viewer_key_press (darea): `%s'\n", 
		gdk_keyval_name(event->keyval));
	break;
    }

    return FALSE;
}

gboolean key_press_cb(GtkWidget* widget, 
			  GdkEventKey* event, 
			  gpointer data)
{
    switch (event->keyval){
    case GDK_q:
	gtk_main_quit();
	break;
    default:
	g_print("key_press_cb (window): `%s'\n", 
		gdk_keyval_name(event->keyval));
	break;
    }

  return FALSE;
}


int main (int argc, char *argv[])
{
    GtkWidget *window, *darea;
    gint x, y;
    guchar *pos;
    GdkGC * GC;

    gtk_init (&argc, &argv);
    gdk_init(&argc, &argv);
    gdk_imlib_init();
    gdk_rgb_init();

    /* Get gdk to use imlib's visual and colormap */
    gtk_widget_push_visual(gdk_imlib_get_visual());
    gtk_widget_push_colormap(gdk_imlib_get_colormap());
    
    /* Load the image specified as the first argument */
    if(!(IM=gdk_imlib_load_image(argv[1]))){
	g_print("Can't read file %s\n", argv[1]);
	exit(1);
    }


    /* window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GTK_WIDGET_UNSET_FLAGS(window,GTK_CAN_FOCUS);
/*      gtk_signal_connect (GTK_OBJECT (window), "button_press_event",  */
/*  			(GtkSignalFunc) cb_quit, NULL); */
    gtk_signal_connect(GTK_OBJECT (window), "key_press_event",
		     (GtkSignalFunc) key_press_cb, NULL);
/*      gtk_signal_connect(GTK_OBJECT (window), "destroy", */
/*  		       GTK_SIGNAL_FUNC(cb_quit), NULL); */


    /* drawing area */
    darea = gtk_drawing_area_new();
    GTK_WIDGET_SET_FLAGS(darea,GTK_CAN_FOCUS);
    gtk_drawing_area_size (GTK_DRAWING_AREA (darea), IM->rgb_width,
						IM->rgb_height);
    gtk_container_add (GTK_CONTAINER (window), darea);

    gtk_signal_connect (GTK_OBJECT (darea), "button_press_event", 
			(GtkSignalFunc) mouse_darea, NULL);
    gtk_signal_connect_after(GTK_OBJECT (darea), "key_press_event",
		       (GtkSignalFunc) viewer_key_press, NULL);
    gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
			GTK_SIGNAL_FUNC (on_darea_expose), NULL);

    gtk_widget_set_events(darea,
			  GDK_KEY_PRESS_MASK | 
			  GDK_BUTTON_PRESS_MASK |
			  GDK_EXPOSURE_MASK);
  

    gtk_widget_show_all (window);

    gtk_main();
    return 0;
}








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