ventana_fugitiva_dos.c

   1 /**
   2  * ventana_fugitiva_dos.c
   3  *
   4  * @author Luis Alejandro Bernal Romero
   5  *
   6  * Segunda versión de la famosa venta fugitiva, esta vez con un
   7  * comportamiento un "poquito" más complejo.
   8  *
   9  * Para compilar digite lo siguiente en la línea de comando:
  10  *
  11  *   gcc -Wall -pedantic -o ventana_fugitiva_dos ventana_fugitiva_dos.c `pkg-config --cflags --libs gtk+-2.0`
  12  */
  13 
  14 #include <gtk/gtk.h>
  15 
  16 #define ancho 200
  17 #define alto 200
  18 
  19 static gboolean cerrar(GtkWidget *widget, GdkEvent *evento, gpointer datos){
  20   gtk_main_quit();
  21   return FALSE;
  22 }
  23 
  24 static void correr(GtkWidget *ventana, GdkEvent *evento, gpointer datos){
  25   GdkDisplay *display;
  26   gint pointer_x, pointer_y;
  27   gint ventana_x, ventana_y;
  28   gint inc_x, inc_y; 
  29 
  30   display = gdk_screen_get_display(GDK_SCREEN(gtk_window_get_screen(GTK_WINDOW(ventana))));
  31   gdk_display_get_pointer(display, NULL, &pointer_x, &pointer_y, NULL);
  32   gtk_window_get_position(GTK_WINDOW(ventana), &ventana_x, &ventana_y);
  33   pointer_x -= ventana_x;
  34   pointer_y -= ventana_y;
  35   if(pointer_x < ancho / 2){
  36     inc_x = ancho / 2;
  37   }
  38   else{
  39     inc_x = -ancho / 2;
  40   }
  41   if(pointer_y < alto / 2){
  42     inc_y = alto / 2;
  43   }
  44   else{
  45     inc_y = -alto / 2;
  46   }
  47   gtk_window_move(GTK_WINDOW(ventana), ventana_x + inc_x,  ventana_y + inc_y ); 
  48 }
  49 
  50 int main(int argc, char *argv[]){
  51   GtkWidget *ventana;
  52 
  53   gtk_init(&argc, &argv);
  54   ventana = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  55   gtk_widget_set_size_request(GTK_WIDGET(ventana), ancho, alto);
  56   g_signal_connect(G_OBJECT(ventana), "delete_event", G_CALLBACK(cerrar), NULL);
  57   g_signal_connect(G_OBJECT(ventana), "enter_notify_event", G_CALLBACK(correr), NULL);
  58   gtk_widget_show(ventana);
  59   gtk_main();
  60 
  61   return 0;
  62 }

CategoryLenguajeC | CategoryProgramacion

LenguajeC/Programas/ventana_fugitiva_dos.c (last edited 2008-04-20 14:38:28 by localhost)