[cairo-commit] cairo-demo/gtk ChangeLog, 1.3, 1.4 Makefile, 1.3, 1.4 README, 1.1, 1.2 mouse_events.c, NONE, 1.1

OEyvind Kolaas commit at pdx.freedesktop.org
Thu Nov 11 11:45:55 PST 2004


Committed by: pippin

Update of /cvs/cairo/cairo-demo/gtk
In directory gabe:/tmp/cvs-serv4358

Modified Files:
	ChangeLog Makefile README 
Added Files:
	mouse_events.c 
Log Message:
added mouse event example

Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/gtk/ChangeLog,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ChangeLog	11 Nov 2004 17:49:08 -0000	1.3
+++ ChangeLog	11 Nov 2004 19:45:53 -0000	1.4
@@ -1,3 +1,9 @@
 2004-11-11  Øyvind Kolås  <pippin at freedesktop.org>
 
-	* Initial checkin, replacing old direct gtk demo. 
+	* mouse_events.c: illustration handling of mouse events, by
+	implementing rectangular rubber banding.
+	* Makefile, README: modified to reflect change.
+
+2004-11-11  Øyvind Kolås  <pippin at freedesktop.org>
+
+	* Initial check-in, replacing old direct gtk demo. 

Index: Makefile
===================================================================
RCS file: /cvs/cairo/cairo-demo/gtk/Makefile,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Makefile	11 Nov 2004 17:49:08 -0000	1.3
+++ Makefile	11 Nov 2004 19:45:53 -0000	1.4
@@ -1,4 +1,6 @@
-APPS = hello
+APPS =				\
+	hello			\
+	mouse_events
 
 CFLAGS  = -g -Wall
 

Index: README
===================================================================
RCS file: /cvs/cairo/cairo-demo/gtk/README,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- README	11 Nov 2004 17:49:08 -0000	1.1
+++ README	11 Nov 2004 19:45:53 -0000	1.2
@@ -1,3 +1,13 @@
 Sample usage of the gtkcairo widget.
 
-hello.c  -  a very simple application with a gtkcairo widget in a window
+hello
+=====
+
+A very simple application with a gtkcairo widget in a window, displaying
+some text.
+
+mouse_events
+============
+
+Illustrating how to catch user events, only functionality new compared
+to hello.c is commented.

--- NEW FILE: mouse_events.c ---
/*  alpha blended rubber band using gtkcairo, demonstrating mouse intercation
 *
 *  only things that are new compared to hello.c are commented
 */

#include <gtk/gtk.h>
#include <gtkcairo.h>

#define DEFAULT_WIDTH  400
#define DEFAULT_HEIGHT 200

/* a structure keeping information about
 * the selection */
typedef struct
{
  gboolean active;   /* whether the selection is active or not */
  gdouble  x, y;
  gdouble  w, h;
}
SelectionInfo;

static void paint (GtkWidget     *widget,
                   cairo_t       *cr,
                   SelectionInfo *sel_info);

/* forward definitions of handler for mouse events
 */
static gboolean event_press   (GtkWidget      *widget,
                               GdkEventButton *bev,
                               SelectionInfo  *sel_info);

static gboolean event_release (GtkWidget      *widget,
                               GdkEventButton *bev,
                               SelectionInfo  *sel_info);

static gboolean event_motion  (GtkWidget      *widget,
                               GdkEventMotion *mev,
                               SelectionInfo  *sel_info);

gint
main (gint    argc,
      gchar **argv)
{
  GtkWidget     *window;
  GtkWidget     *gtkcairo;
  SelectionInfo  selection = {FALSE, 0, 0, 0, 0};

  gtk_init (&argc, &argv);

  window   = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "delete-event",
                    G_CALLBACK (gtk_main_quit), NULL);

  gtkcairo = gtk_cairo_new ();
  gtk_widget_set_size_request (gtkcairo, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
  /* connect our drawing method to the "paint" signal
   * passing in a pointer to the selection structure as the user data
   */
  g_signal_connect (G_OBJECT (gtkcairo), "paint",
                    G_CALLBACK (paint),
                    &selection
                  );

  /* add additional events the gtkcairo widget will listen for
   */
  gtk_widget_add_events (gtkcairo,
                    GDK_BUTTON1_MOTION_MASK |
                    GDK_BUTTON_PRESS_MASK   |
                    GDK_BUTTON_RELEASE_MASK);
  
  
  /* connect our rubber banding callbacks, like the paint callback
   * we pass the selection as userdata
   */
  g_signal_connect (G_OBJECT (gtkcairo), "button_press_event",
                    G_CALLBACK (event_press),
                    &selection
                  );
  g_signal_connect (G_OBJECT (gtkcairo), "button_release_event",
                    G_CALLBACK (event_release),
                    &selection
                  );
  g_signal_connect (G_OBJECT (gtkcairo), "motion_notify_event",
                    G_CALLBACK (event_motion),
                    &selection
                  );

  gtk_container_add (GTK_CONTAINER (window), gtkcairo);
  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}


/* function to draw the rectangular selection
 */
static void
paint_selection (cairo_t       *cr,
                 SelectionInfo *sel_info)
{
  if (!sel_info->active)
    return;
  cairo_save (cr);
    cairo_rectangle (cr, sel_info->x, sel_info->y,
                         sel_info->w, sel_info->h);
    cairo_save (cr);
      cairo_set_rgb_color (cr, 0, 0, 1);
      cairo_set_alpha (cr, 0.2);
      cairo_fill (cr);
    cairo_restore (cr);

    cairo_set_rgb_color (cr, 0, 0, 0);
    cairo_set_alpha (cr, 0.5);
    cairo_stroke (cr);
  cairo_restore (cr);
}


static void
paint (GtkWidget     *widget,
       cairo_t       *cr,
       SelectionInfo *sel_info)
{
  gint width, height;
  gint i;

  width  = widget->allocation.width;
  height = widget->allocation.height;
  
  cairo_save (cr);
  
    cairo_rectangle (cr, 0, 0, width, height);
    cairo_set_rgb_color (cr, 1,1,1);
    cairo_fill (cr);
    
    cairo_select_font (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
                                   CAIRO_FONT_WEIGHT_BOLD);
    cairo_save (cr);
      cairo_scale_font (cr, 20);
      cairo_move_to (cr, 40, 60);
      cairo_set_rgb_color (cr, 0,0,0);
      cairo_show_text (cr, "Drag your mouse here");
    cairo_restore (cr);

    cairo_set_rgb_color (cr, 1,0,0);
    cairo_scale_font (cr, 15);
    cairo_move_to (cr, 50, 100);
    cairo_show_text (cr, "(and see alpha blended selection)");

    cairo_set_rgb_color (cr, 0,0,1);

    cairo_move_to (cr, 0, 150);
    for (i=0; i< width/10; i++)
      {
        cairo_rel_line_to (cr, 5,  10);
        cairo_rel_line_to (cr, 5, -10);
      }

    cairo_stroke (cr);

  /* paint the selection */
  paint_selection (cr, sel_info);
    
  cairo_restore (cr);
}


static gboolean
event_press (GtkWidget      *widget,
             GdkEventButton *bev,
             SelectionInfo  *sel_info)
{
  sel_info->active = TRUE;

  sel_info->x = bev->x;
  sel_info->y = bev->y;
  sel_info->w = 0;
  sel_info->h = 0;

  /* tell the gtkcairo widget that it needs to redraw itself */
  gtk_widget_queue_draw (widget);

  return TRUE;
}


static gboolean
event_motion (GtkWidget      *widget,
              GdkEventMotion *mev,
              SelectionInfo  *sel_info)
{
  sel_info->w = mev->x - sel_info->x;
  sel_info->h = mev->y - sel_info->y;

  /* tell the gtkcairo widget that it needs to redraw itself */
  gtk_widget_queue_draw (widget);
  return TRUE;
}

static gboolean
event_release (GtkWidget      *widget,
               GdkEventButton *bev,
               SelectionInfo  *sel_info)
{
  sel_info->active = FALSE;

  /* tell the gtkcairo widget that it needs to redraw itself */
  gtk_widget_queue_draw (widget);
  return TRUE;
}




More information about the cairo-commit mailing list