本文介绍了如何在Gtk 2窗口上创建cairo_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧应用程序,现在正在编译Gtk 2,但我需要介绍开罗的使用。我无法弄清楚如何从我的Widgets创建必要的cairo上下文(cairo_t)。



这是我目前试图学习的代码, * ahem * cribbed)从一个Gtk 3教程。问题的关键是从窗口小部件创建一个开罗表面。就目前来看,这个调用是一个草图,它不会编译,更不用说运行了。请记住,我的目标是Gtk2,而不是3,至少在这一点上。

  / *从http:// zetcode中剔除。 com / gfx / cairo / cairobackends /于2014年1月13日* / 
#include< cairo.h>
#include< cairo-xlib.h>
#include< gtk / gtk.h>

int main(int argc,char * argv [])
{
GtkWidget * window;
GtkWidget * darea;
cairo_surface_t * surface;
cairo_t * cr;

gtk_init(& argc,& argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window),darea);

g_signal_connect(window,destroy,G_CALLBACK(gtk_main_quit),NULL);

gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),400,90);
gtk_window_set_title(GTK_WINDOW(窗口),GTK窗口);

surface = cairo_xlib_surface_create(
gtk_widget_get_display(window),/ *必须是Display *,而不是结构GdkDisplay * * /
窗口,/ *必须是Drawable(无星号)* /
gtk_widget_get_visual(window),/ *必须是Visual * * /
gtk_widget_get_width(window),/ * int * /
gtk_widget_get_height(window)/ * int * /
) ;
cr = cairo_create(surface);
cairo_set_source_rgb(cr,0,0,0);
cairo_select_font_face(cr,Sans,CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr,40.0);

cairo_move_to(cr,10.0,50.0);
cairo_show_text(cr,Discipline is power。);

gtk_widget_show_all(window);

gtk_main();

返回0;
}


解决方案

在main()中进行cairo绘图...绘制GTK小部件的有意义的地方在于expose事件中(如果您想强制从其他地方重绘,只需调用 gtk_widget_queue_draw())。无论如何,获取cairo上下文的简单方法是(最后)。

在我看来,这在GTK3中更加出色。不过,即使你的目标是移植到GTK3,也可以将绘图改为首先使用cairo,之后更改为GTK3应该简化代码。


I have an old app, now compiling on Gtk 2, but I need to introduce the use of Cairo. I can't figure out how to create the necessary cairo context (cairo_t) from my Widgets.

Here's the code I'm trying to learn with so far, modified (*ahem* cribbed) from a Gtk 3 tutorial. The crux of the matter is creating a cairo surface from the window widget. As it stands, that call is a sketch and it won't compile, let alone run. Remember, my target is Gtk2, not 3, at least at this point.

/* Snitched from http://zetcode.com/gfx/cairo/cairobackends/ on 13 Jan 2014 */
#include <cairo.h>
#include <cairo-xlib.h>
#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *darea;
  cairo_surface_t *surface;
  cairo_t *cr;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER(window), darea);

  g_signal_connect(window,          "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 90);
  gtk_window_set_title(GTK_WINDOW(window), "GTK window");

  surface = cairo_xlib_surface_create(
      gtk_widget_get_display(window),   /* must be Display *, not struct GdkDisplay * */
      window,                           /* must be Drawable (no star) */
      gtk_widget_get_visual(window),    /* must be Visual * */
      gtk_widget_get_width(window),     /* int */
      gtk_widget_get_height(window)     /* int */
      );
  cr = cairo_create(surface);
  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, 
     CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size(cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Discipline is power.");

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}
解决方案

I hope the tutorial did not do cairo drawing in main()... The meaningful place to draw onto a GTK widget is in the expose-event (and if you want to force a redraw from somewhere else, just call gtk_widget_queue_draw()). Anyway, the easy way to get a cairo context is gdk_cairo_create().

Something like this:

static gboolean
on_expose_event (GtkWidget *widget,
                 GdkEventExpose *event,
                 gpointer data)
{
    cairo_t *cr;

    cr = gdk_cairo_create (gtk_widget_get_window (widget));
    cairo_move_to (cr, 30, 30);
    cairo_show_text (cr, "Text");

    cairo_destroy (cr);

    return FALSE;
}

g_signal_connect(darea, "expose-event",
                 G_CALLBACK(on_expose_event), NULL);

Jan Bodnar has a more complete example (in the end).

This is all a lot nicer in GTK3 in my opinion. Still, even if your goal is to port to GTK3 it may make sense to change the drawing to use cairo first as you're doing -- changing to GTK3 afterwards should just simplify the code.

这篇关于如何在Gtk 2窗口上创建cairo_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:17