key_press_event problems



i am having trouble with events. i want to get keyboard presses through a
gtk_gl_area widget but nothing happens. i tried button press events to see
if events were working at all in my code. i set up the button event
handler to print "button pressed" whenever it was called. it does get
called, however, you don't see the output until the program is exited. i
also put a print function in my key event handler, but that doesn't get
printed at all. is there some function or attribute in the code that is
disturbing the handling of events?

i have attached the code to htis email. the code has been modified from
the simple.c example from gtkglarea. it is just a gtk/X11 front end to a
gl proggy.


thanks.




"ta-ta for now!"-- t-i-double 'g'-er

http://www.op.net/~finklesk/index.html



                                   " STUFF. "


#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "gtkglarea.h"
#include "glmod.h"

gint init(GtkWidget *widget)
{
  /* OpenGL functions can be called only if begingl returns true */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {
	PreSceneSetup();
    SetViewPort( widget->allocation.width, widget->allocation.height);

/*    glViewport(0,0, widget->allocation.width,
widget->allocation.height);*/
    /* end opengl calls by calling endgl */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }
  return TRUE;
}


/* When widget is exposed it's contents are redrawn. */
gint draw(GtkWidget *widget, GdkEventExpose *event)
{
  /* Draw only last expose. */
  if (event->count > 0) {
    return TRUE;
  }

  /* OpenGL functions can be called only if gtk_gl_area_begingl
     returns true, you can't call gl* functions anywhere except
     inside gtk_gl_area_begingl()/gtk_gl_area_endgl() pairs.
  */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {

	DrawFrame();
   /* Opengl rendering is done for now. */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }

  /* Swap backbuffer to front */
  gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
  
  return TRUE;
}

/* When glarea widget size changes, viewport size is set to match the new size */
gint reshape(GtkWidget *widget, GdkEventConfigure *event)
{
  /* OpenGL functions can be called only if begingl returns true */
  if (gtk_gl_area_begingl(GTK_GL_AREA(widget))) {

    SetViewPort( widget->allocation.width, widget->allocation.height);
	DrawFrame();
    /* end opengl calls by calling endgl */
    gtk_gl_area_endgl(GTK_GL_AREA(widget));
  }
  return TRUE;
}

gint Button(GtkWidget *wid, GdkEventButton *event)
	{
	g_print("button pressed");
	}

gint ProcessInput(GtkWidget *wid, GdkEventKey *keypress)
	{
/*	char chr = (guint *) keypress->keyval;
	
	ProcessKey( chr);
	
	if(gtk_gl_area_begingl(GTK_GL_AREA(wid)))
		{
		DrawFrame();
		gtk_gl_area_endgl(GTK_GL_AREA(wid));
		} */
	g_print("%s\n", keypress->keyval);
	g_print("where am i?");
	return TRUE;
	}


void glmodQuit(GtkWidget *wid, gchar *dat)
	{
	PostSceneCleanup();
	gtk_main_quit();
	}


int main(int argc, char **argv)
{
  GtkWidget *window,*glarea;

  /* Attribute list for gtkglarea widget. Specifies a
     list of Boolean attributes and enum/integer
     attribute/value pairs. The last attribute must be
     GDK_GL_NONE. See glXChooseVisual manpage for further
     explanation.
  */
  gint attrlist[] = {
    GDK_GL_RGBA,
    GDK_GL_DOUBLEBUFFER,
    GDK_GL_DEPTH_SIZE,1,
    GDK_GL_NONE
  };

  /* initialize gtk */
  gtk_init(&argc, &argv);

  /* Check if OpenGL (GLX extension) is supported. */
  if (gdk_gl_query() == FALSE) {
    g_print("OpenGL not supported\n");
    return 0;
  }

  /* Create new top level window. */
  window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "glmod");
  gtk_container_border_width(GTK_CONTAINER(window), 10);

  /* Quit form main if got delete event */
  gtk_signal_connect(GTK_OBJECT(window), "delete_event",
		     GTK_SIGNAL_FUNC(glmodQuit), NULL);

  /* You should always delete gtk_gl_area widgets before exit or else
     GLX contexts are left undeleted, this may cause problems (=core dump)
     in some systems.
     Destroy method of objects is not automatically called on exit.
     You need to manually enable this feature. Do gtk_quit_add_destroy()
     for all your top level windows unless you are certain that they get
     destroy signal by other means.
  */
  gtk_quit_add_destroy(1, GTK_OBJECT(window));


  /* Create new OpenGL widget. */
  glarea = GTK_WIDGET(gtk_gl_area_new(attrlist));
  /* Events for widget must be set before X Window is created */
  gtk_widget_set_events(GTK_WIDGET(glarea),
			GDK_EXPOSURE_MASK|
			GDK_KEY_PRESS_MASK|
			GDK_BUTTON_PRESS_MASK);

  /* Connect signal handlers */
  /* Redraw image when exposed. */
  gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
		     GTK_SIGNAL_FUNC(draw), NULL);
  /* When window is resized viewport needs to be resized also. */
  gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
		     GTK_SIGNAL_FUNC(reshape), NULL);
  /* Do initialization when widget has been realized. */
  gtk_signal_connect(GTK_OBJECT(glarea), "realize",
		     GTK_SIGNAL_FUNC(init), NULL);

  gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
			GTK_SIGNAL_FUNC(Button), NULL);

  gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
			GTK_SIGNAL_FUNC(ProcessInput), NULL);

  /* set minimum size */
  gtk_widget_set_usize(GTK_WIDGET(glarea), 400,400);
		    
  /* put glarea into window and show it all */
  gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(glarea));
  gtk_widget_show(GTK_WIDGET(window));

  gtk_main();

  return 0;
}


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