用gtk和C中的cairo绘图

时间:2015-03-21 15:50:58

标签: c xcode animation gtk cairo

我正在尝试将png图像加载到cairo表面。我想把它放在一个gtk界面并绘制它。 问题是我不知道如何将这个表面包含在界面中。 gtk_container_add功能不起作用?还是吗? 这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>

cairo_surface_t *surface=NULL;
gint surface_ret_value=0;

static gboolean draw_points(GtkWidget *widget, cairo_t *cr, gpointer data)
{
    gint i=0;
    //Draw .png
    g_print("png\n");
    guint png_width=0;
    guint png_height=0;
    guint da_width=gtk_widget_get_allocated_width(GTK_WIDGET(widget));
    guint da_height=gtk_widget_get_allocated_height(GTK_WIDGET(widget));
    if(surface_ret_value==CAIRO_STATUS_SUCCESS)
    {
      png_width=cairo_image_surface_get_width(surface);
      png_height=cairo_image_surface_get_height(surface);
      cairo_scale(cr, (float)da_width/(float)png_width,      (float)da_height/(float)png_height);
      cairo_set_source_surface(cr, surface, 0, 0);
      cairo_paint(cr);
      cairo_scale(cr, (float)png_width/(float)da_width, (float)png_height/(float)da_height);
    }
   else
   {
      g_print("Need a PNG file.\n");
   }
 }

int main(int argc, char *argv[])
{
   gtk_init(&argc, &argv);
   GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
   gtk_window_set_title(GTK_WINDOW(window), "Draw Points");
   g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
   //Get a PNG file.
  surface=cairo_image_surface_create_from_png("/Users/imadmesselmani/Desktop/Marianne.png");
  surface_ret_value = cairo_surface_status(surface);
  if(surface_ret_value!=CAIRO_STATUS_SUCCESS)
  {
     const char *surface_status=cairo_status_to_string(surface_ret_value);
     g_print("%s\n", surface_status);
     return 1;
  }

    GtkWidget *drawing_area = gtk_drawing_area_new();
    gtk_widget_set_size_request(drawing_area, 500, 500);

    g_signal_connect(G_OBJECT(drawing_area), "draw", G_CALLBACK(draw_points), NULL);

    gtk_container_add(GTK_CONTAINER(window), drawing_area);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

0 个答案:

没有答案