[gnome-games] gnect: Port to cairo and gtk3



commit 3216a2bf36df188f423b9e57c51f654ef0e6d7b6
Author: Christian Persch <chpe gnome org>
Date:   Thu Oct 21 15:18:22 2010 +0200

    gnect: Port to cairo and gtk3

 gnect/src/gfx.c   |  201 ++++++++++++++++++-----------------------------------
 gnect/src/gfx.h   |    5 +-
 gnect/src/main.c  |   35 ++++-----
 gnect/src/prefs.c |    7 +-
 gnect/src/theme.c |   25 ++-----
 gnect/src/theme.h |    3 +-
 6 files changed, 97 insertions(+), 179 deletions(-)
---
diff --git a/gnect/src/gfx.c b/gnect/src/gfx.c
index 1364cda..aa26c33 100644
--- a/gnect/src/gfx.c
+++ b/gnect/src/gfx.c
@@ -56,16 +56,6 @@ static GdkPixbuf *pb_bground_raw = NULL;
 static GdkPixbuf *pb_tileset = NULL;
 static GdkPixbuf *pb_bground = NULL;
 
-/* background pixmap, we draw the grid on it */
-static GdkPixmap *pm_bground = NULL;
-
-/* the buffer we draw in */
-static GdkPixmap *pm_display = NULL;
-
-static GdkGC *gc = NULL;
-static GdkGC *solidgc = NULL;
-
-
 void
 gfx_free (void)
 {
@@ -85,18 +75,8 @@ gfx_free (void)
     g_object_unref (pb_bground);
     pb_bground = NULL;
   }
-  if (pm_bground != NULL) {
-    g_object_unref (pm_bground);
-    pm_bground = NULL;
-  }
-  if (pm_display != NULL) {
-    g_object_unref (pm_display);
-    pm_display = NULL;
-  }
 }
 
-
-
 gint
 gfx_get_column (gint xpos)
 {
@@ -110,17 +90,16 @@ gfx_get_column (gint xpos)
   return c;
 }
 
-
-
-void
-gfx_draw_tile (gint r, gint c, gboolean refresh)
+static void
+gfx_paint_tile (cairo_t *cr, gint r, gint c)
 {
   gint x = c * tilesize;
   gint y = r * tilesize;
   gint tile = gboard[r][c];
   gint os = 0;
 
-  g_return_if_fail (gc != NULL);
+  if (tile == TILE_CLEAR && r != 0)
+    return;
 
   switch (tile) {
   case TILE_PLAYER1:
@@ -135,37 +114,33 @@ gfx_draw_tile (gint r, gint c, gboolean refresh)
     else
       os = offset[TILE_PLAYER2];
     break;
-  default:
+  case TILE_CLEAR:
+    if (r == 0)
+      os = offset[TILE_CLEAR_CURSOR];
+    else
+      os = offset[TILE_CLEAR];
     break;
   }
 
-  gdk_draw_drawable (pm_display, gc, pm_bground, x, y, x, y, tilesize,
-		     tilesize);
-
-  if (tile != TILE_CLEAR) {
-    gdk_draw_pixbuf (pm_display, NULL, pb_tileset,
-                     os, 0, x, y, tilesize, tilesize,
-                     GDK_RGB_DITHER_NORMAL, 0, 0);
-  }
-
-  if (refresh) {
-    gtk_widget_queue_draw_area (drawarea, x, y, tilesize, tilesize);
-  }
+  cairo_save (cr);
+  gdk_cairo_set_source_pixbuf (cr, pb_tileset, x - os, y);
+  cairo_rectangle (cr, x, y, tilesize, tilesize);
+  cairo_clip (cr);
+  cairo_paint (cr);
+  cairo_restore (cr);
 }
 
-
+void
+gfx_draw_tile (gint r, gint c)
+{
+  gtk_widget_queue_draw_area (drawarea,
+                              c * tilesize, r * tilesize,
+                              tilesize, tilesize);
+}
 
 void
 gfx_draw_all (void)
 {
-  gint r, c;
-
-  for (r = 0; r < 7; r++) {
-    for (c = 0; c < 7; c++) {
-      gfx_draw_tile (r, c, FALSE);
-    }
-  }
-
   gtk_widget_queue_draw_area (drawarea, 0, 0, boardsize, boardsize);
 }
 
@@ -174,10 +149,6 @@ gfx_draw_all (void)
 void
 gfx_refresh_pixmaps (void)
 {
-  gint i;
-
-  g_return_if_fail (gc != NULL);
-
   /* scale the pixbufs */
   if (pb_tileset)
     g_object_unref (pb_tileset);
@@ -190,30 +161,47 @@ gfx_refresh_pixmaps (void)
   pb_bground = gdk_pixbuf_scale_simple (pb_bground_raw,
 					boardsize, boardsize,
 					GDK_INTERP_BILINEAR);
+}
 
-  /* draw the background */
-  gdk_draw_pixbuf (pm_bground, gc, pb_bground,
-		   0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0);
+static void
+gfx_draw_grid (cairo_t *cr)
+{
+  static const double dashes[] = { 4., 4. };
+  gint i;
+
+  gdk_cairo_set_source_color (cr, &theme[p.theme_id].grid_color);
+  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+  cairo_set_line_width (cr, 1);
+  cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
+  cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
+  cairo_set_dash (cr, dashes, G_N_ELEMENTS (dashes), 0);
 
   /* draw the grid on the background pixmap */
   for (i = 1; i < 7; i++) {
-    gdk_draw_line (pm_bground, gc, i * tilesize, 0, i * tilesize, boardsize);
-    gdk_draw_line (pm_bground, gc, 0, i * tilesize, boardsize, i * tilesize);
+    cairo_move_to (cr, i * tilesize + .5, 0);
+    cairo_line_to (cr, i * tilesize + .5, boardsize);
+    cairo_move_to (cr, 0, i * tilesize + .5);
+    cairo_line_to (cr, boardsize, i * tilesize + .5);
   }
-  /* Mark the top off with a solid line. */
-  gdk_draw_line (pm_bground, solidgc, 0, tilesize, boardsize, tilesize);
-}
+  cairo_stroke (cr);
 
+  /* Draw separator line at the top */
+  cairo_set_dash (cr, NULL, 0, 0);
+  cairo_move_to (cr, 0, tilesize + .5);
+  cairo_line_to (cr, boardsize, tilesize + .5);
 
+  cairo_stroke (cr);
+}
 
 void
 gfx_resize (GtkWidget * w)
 {
-  GtkAllocation allocation;
+  int width, height;
 
-  gtk_widget_get_allocation (w, &allocation);
+  width = gtk_widget_get_allocated_width (w);
+  height = gtk_widget_get_allocated_height (w);
 
-  boardsize = MIN (allocation.width, allocation.height);
+  boardsize = MIN (width, height);
   tilesize = boardsize / 7;
 
   offset[TILE_PLAYER1] = 0;
@@ -223,37 +211,30 @@ gfx_resize (GtkWidget * w)
   offset[TILE_PLAYER1_CURSOR] = tilesize * 4;
   offset[TILE_PLAYER2_CURSOR] = tilesize * 5;
 
-  /* create the buffer and background pixmap of the proper size */
-  if (pm_display != NULL)
-    g_object_unref (pm_display);
-  if (pm_bground != NULL)
-    g_object_unref (pm_bground);
-
-  pm_display = gdk_pixmap_new (gtk_widget_get_window (w), boardsize, boardsize, -1);
-  pm_bground = gdk_pixmap_new (gtk_widget_get_window (w), boardsize, boardsize, -1);
-
-  /* the first time the configure signal is emitted, the drawarea
-   * is not shown yet so we do not have the gc.
-   */
-  if (gc) {
-    gfx_refresh_pixmaps ();
-    gfx_draw_all ();
-  }
+  gfx_refresh_pixmaps ();
+  gfx_draw_all ();
 }
 
-
-
 void
-gfx_expose (GdkRectangle * area)
+gfx_expose (cairo_t *cr)
 {
-  gdk_draw_drawable (GDK_DRAWABLE (gtk_widget_get_window (drawarea)),
-                     gc,
-                     pm_display,
-                     area->x, area->y,
-                     area->x, area->y, area->width, area->height);
-}
+  gint r, c;
 
+  /* draw the background */
+  cairo_save (cr);
+  gdk_cairo_set_source_pixbuf (cr, pb_bground, 0, 0);
+  cairo_rectangle (cr, 0, 0, boardsize, boardsize);
+  cairo_paint (cr);
+  cairo_restore (cr);
 
+  for (r = 0; r < 7; r++) {
+    for (c = 0; c < 7; c++) {
+      gfx_paint_tile (cr, r, c);
+    }
+  }
+
+  gfx_draw_grid (cr);
+}
 
 static void
 gfx_load_error (const gchar * fname)
@@ -270,7 +251,6 @@ gfx_load_error (const gchar * fname)
   gtk_widget_destroy (dialog);
 }
 
-
 gboolean
 gfx_load_pixmaps (void)
 {
@@ -351,57 +331,12 @@ gfx_load_pixmaps (void)
   return TRUE;
 }
 
-
-
-gboolean
-gfx_set_grid_style (void)
-{
-  GdkColormap *cmap;
-  GdkColor color;
-
-  g_return_val_if_fail (drawarea != NULL, FALSE);
-
-  if (!gc) {
-    gc = gdk_gc_new (gtk_widget_get_window (drawarea));
-    solidgc = gdk_gc_new (gtk_widget_get_window (drawarea));
-  }
-
-  if (theme[p.theme_id].grid_rgb == NULL)
-    return FALSE;
-
-  if (!gdk_color_parse (theme[p.theme_id].grid_rgb, &color))
-    gdk_color_parse ("#727F8C", &color);
-
-  cmap = gtk_widget_get_colormap (drawarea);
-  gdk_colormap_alloc_color (cmap, &color, FALSE, TRUE);
-
-  gdk_gc_set_foreground (gc, &color);
-
-  gdk_colormap_free_colors (cmap, &color, 1);
-  g_object_unref (cmap);
-
-  gdk_gc_set_line_attributes (gc, 0, theme[p.theme_id].grid_style,
-			      GDK_CAP_BUTT, GDK_JOIN_MITER);
-
-  gdk_gc_copy (solidgc, gc);
-  gdk_gc_set_line_attributes (solidgc, 0, GDK_LINE_SOLID,
-			      GDK_CAP_BUTT, GDK_JOIN_MITER);
-
-
-  return TRUE;
-}
-
-
-
 gboolean
 gfx_change_theme (void)
 {
   if (!gfx_load_pixmaps ())
     return FALSE;
 
-  if (!gfx_set_grid_style ())
-    return FALSE;
-
   gfx_refresh_pixmaps ();
   gfx_draw_all ();
 
diff --git a/gnect/src/gfx.h b/gnect/src/gfx.h
index 76b0d2c..922d4cd 100644
--- a/gnect/src/gfx.h
+++ b/gnect/src/gfx.h
@@ -3,12 +3,11 @@
 
 
 gboolean gfx_load_pixmaps (void);
-gboolean gfx_set_grid_style (void);
 gboolean gfx_change_theme (void);
 void gfx_free (void);
 void gfx_resize (GtkWidget * w);
-void gfx_expose (GdkRectangle * area);
-void gfx_draw_tile (gint r, gint c, gboolean refresh);
+void gfx_expose (cairo_t *cr);
+void gfx_draw_tile (gint r, gint c);
 void gfx_draw_all (void);
 gint gfx_get_column (gint xpos);
 void gfx_refresh_pixmaps (void);
diff --git a/gnect/src/main.c b/gnect/src/main.c
index c248624..920a2c4 100644
--- a/gnect/src/main.c
+++ b/gnect/src/main.c
@@ -172,7 +172,7 @@ drop_marble (gint r, gint c)
     tile = TILE_PLAYER2;
 
   gboard[r][c] = tile;
-  gfx_draw_tile (r, c, TRUE);
+  gfx_draw_tile (r, c);
 
   column = column_moveto = c;
   row = row_dropto = r;
@@ -191,11 +191,11 @@ drop (void)
     tile = TILE_PLAYER2;
 
   gboard[row][column] = TILE_CLEAR;
-  gfx_draw_tile (row, column, TRUE);
+  gfx_draw_tile (row, column);
 
   row++;
   gboard[row][column] = tile;
-  gfx_draw_tile (row, column, TRUE);
+  gfx_draw_tile (row, column);
 }
 
 
@@ -204,7 +204,7 @@ static void
 move_cursor (gint c)
 {
   gboard[0][column] = TILE_CLEAR;
-  gfx_draw_tile (0, column, TRUE);
+  gfx_draw_tile (0, column);
 
   column = c;
 
@@ -213,7 +213,7 @@ move_cursor (gint c)
   else
     gboard[0][c] = TILE_PLAYER2;
 
-  gfx_draw_tile (0, c, TRUE);
+  gfx_draw_tile (0, c);
 
   column = column_moveto = c;
   row = row_dropto = 0;
@@ -225,7 +225,7 @@ static void
 move (gint c)
 {
   gboard[0][column] = TILE_CLEAR;
-  gfx_draw_tile (0, column, TRUE);
+  gfx_draw_tile (0, column);
 
   column = c;
 
@@ -234,7 +234,7 @@ move (gint c)
   else
     gboard[0][c] = TILE_PLAYER2;
 
-  gfx_draw_tile (0, c, TRUE);
+  gfx_draw_tile (0, c);
 }
 
 
@@ -261,7 +261,7 @@ draw_line (gint r1, gint c1, gint r2, gint c2, gint tile)
   do {
     done = (r1 == r2 && c1 == c2);
     gboard[r1][c1] = tile;
-    gfx_draw_tile (r1, c1, TRUE);
+    gfx_draw_tile (r1, c1);
     if (r1 != r2)
       r1 += d_row;
     if (c1 != c2)
@@ -641,7 +641,7 @@ on_game_undo (GtkMenuItem * m, gpointer data)
   move_cursor (c);
 
   gboard[r][c] = TILE_CLEAR;
-  gfx_draw_tile (r, c, TRUE);
+  gfx_draw_tile (r, c);
 
   if (get_n_human_players () == 1 && !is_player_human ()) {
     if (moves > 0) {
@@ -653,7 +653,7 @@ on_game_undo (GtkMenuItem * m, gpointer data)
       swap_player ();
       move_cursor (c);
       gboard[r][c] = TILE_CLEAR;
-      gfx_draw_tile (r, c, TRUE);
+      gfx_draw_tile (r, c);
     }
   }
 }
@@ -774,12 +774,11 @@ on_game_scores (GtkMenuItem * m, gpointer data)
 					  GTK_STOCK_CLOSE,
 					  GTK_RESPONSE_CLOSE, NULL);
 
-  gtk_dialog_set_has_separator (GTK_DIALOG (scorebox), FALSE);
   gtk_window_set_resizable (GTK_WINDOW (scorebox), FALSE);
   gtk_container_set_border_width (GTK_CONTAINER (scorebox), 5);
   gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (scorebox))), 2);
 
-  g_signal_connect (GTK_OBJECT (scorebox), "destroy",
+  g_signal_connect (scorebox, "destroy",
 		    G_CALLBACK (gtk_widget_destroyed), &scorebox);
 
   vbox = gtk_vbox_new (FALSE, 6);
@@ -1187,9 +1186,9 @@ on_drawarea_resize (GtkWidget * w, GdkEventConfigure * e, gpointer data)
 }
 
 static gboolean
-on_drawarea_expose (GtkWidget * w, GdkEventExpose * e, gpointer data)
+on_drawarea_draw (GtkWidget * w, cairo_t *cr, gpointer data)
 {
-  gfx_expose (&e->area);
+  gfx_expose (cr);
 
   return FALSE;
 }
@@ -1360,7 +1359,6 @@ create_app (void)
   statusbar = gtk_statusbar_new ();
   ui_manager = gtk_ui_manager_new ();
 
-  gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (statusbar), FALSE);
   games_stock_prepare_for_statusbar_tooltips (ui_manager, statusbar);
   create_game_menus (ui_manager);
   menubar = gtk_ui_manager_get_widget (ui_manager, "/MainMenu");
@@ -1394,8 +1392,8 @@ create_app (void)
   gtk_widget_set_events (drawarea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
   g_signal_connect (G_OBJECT (drawarea), "configure_event",
 		    G_CALLBACK (on_drawarea_resize), NULL);
-  g_signal_connect (G_OBJECT (drawarea), "expose_event",
-		    G_CALLBACK (on_drawarea_expose), NULL);
+  g_signal_connect (G_OBJECT (drawarea), "draw",
+		    G_CALLBACK (on_drawarea_draw), NULL);
   g_signal_connect (G_OBJECT (drawarea), "button_press_event",
 		    G_CALLBACK (on_button_press), NULL);
   g_signal_connect (G_OBJECT (app), "key_press_event",
@@ -1412,9 +1410,6 @@ create_app (void)
   gtk_widget_hide (chat);
 #endif
 
-  if (!gfx_set_grid_style ())
-    return FALSE;
-
   gfx_refresh_pixmaps ();
   gfx_draw_all ();
 
diff --git a/gnect/src/prefs.c b/gnect/src/prefs.c
index 5cb766c..058852b 100644
--- a/gnect/src/prefs.c
+++ b/gnect/src/prefs.c
@@ -44,9 +44,9 @@
 #define DEFAULT_LEVEL_PLAYER1  LEVEL_HUMAN
 #define DEFAULT_LEVEL_PLAYER2  LEVEL_WEAK
 #define DEFAULT_THEME_ID       0
-#define DEFAULT_KEY_LEFT       GDK_Left
-#define DEFAULT_KEY_RIGHT      GDK_Right
-#define DEFAULT_KEY_DROP       GDK_Down
+#define DEFAULT_KEY_LEFT       GDK_KEY_Left
+#define DEFAULT_KEY_RIGHT      GDK_KEY_Right
+#define DEFAULT_KEY_DROP       GDK_KEY_Down
 #define DEFAULT_DO_SOUND       TRUE
 #define DEFAULT_DO_ANIMATE     TRUE
 
@@ -282,7 +282,6 @@ prefsbox_open (void)
 					  GTK_DIALOG_DESTROY_WITH_PARENT,
 					  GTK_STOCK_CLOSE,
 					  GTK_RESPONSE_ACCEPT, NULL);
-  gtk_dialog_set_has_separator (GTK_DIALOG (prefsbox), FALSE);
   gtk_container_set_border_width (GTK_CONTAINER (prefsbox), 5);
   gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (prefsbox))),
 		       2);
diff --git a/gnect/src/theme.c b/gnect/src/theme.c
index 96ff4ed..34dd6c5 100644
--- a/gnect/src/theme.c
+++ b/gnect/src/theme.c
@@ -34,67 +34,58 @@
 #include "prefs.h"
 
 
-#define DEFAULT_GRID_STYLE     GDK_LINE_ON_OFF_DASH
-
-
 extern Prefs p;
 
+#define COLOR(r,g,b) { 0, 0x##r##00, 0x##g##00, 0x##b##00 }
 
 Theme theme[] = {
   {
    N_("Classic"),
    "tileset_50x50_sunspot.svg",
    NULL,
-   "#9999CC",
-   DEFAULT_GRID_STYLE,
+   COLOR (99, 99, CC),
    N_("Red"), N_("Yellow")
    },
   {
    N_("High Contrast"),
    "tileset_50x50_hcontrast.svg",
    NULL,
-   "#000000",
-   DEFAULT_GRID_STYLE,
+   COLOR (00, 00, 00),
    N_("Light"), N_("Dark")
    },
   {
    N_("High Contrast Inverse"),
    "tileset_50x50_hcinverse.svg",
    NULL,
-   "#FFFFFF",
-   DEFAULT_GRID_STYLE,
+   COLOR (FF, FF, FF),
    N_("Light"), N_("Dark")
    },
   {
    N_("Cream Marbles"),
    "tileset_50x50_catseyes.png",
    "bg_toplight.png",
-   "#727F8C",
-   DEFAULT_GRID_STYLE,
+   COLOR (72, 7F, 8C),
    N_("Red"), N_("Blue")
    },
   {
    N_("Glass Marbles"),
    "tileset_50x50_glassy.png",
    "bg_grotty.jpg",
-   "#727F8C",
-   DEFAULT_GRID_STYLE,
+   COLOR (72, 7F, 8C),
    N_("Red"), N_("Blue")
    },
   {
    N_("Nightfall"),
    "tileset_50x50_glassy.png",
    "bg_nightfall.jpg",
-   "#727F8C",
-   DEFAULT_GRID_STYLE,
+   COLOR (72, 7F, 8C),
    N_("Red"), N_("Yellow")
    },
   {
    N_("Blocks"),
    "tileset_50x50_square.png",
    NULL,
-   "#666666",
-   DEFAULT_GRID_STYLE,
+   COLOR (66, 66, 66),
    N_("Orange"), N_("Blue")
    }
 };
diff --git a/gnect/src/theme.h b/gnect/src/theme.h
index bdf211e..8b47181 100644
--- a/gnect/src/theme.h
+++ b/gnect/src/theme.h
@@ -7,8 +7,7 @@ struct _Theme {
   const gchar *title;
   const gchar *fname_tileset;
   const gchar *fname_bground;
-  const gchar *grid_rgb;
-  const GdkLineStyle grid_style;
+  const GdkColor grid_color;
   const gchar *player1;
   const gchar *player2;
 };



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