gpt4 book ai didi

c - cairo 上下文对象 cr 在哪里声明?

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:48 25 4
gpt4 key购买 nike

其中,在下面zetcode , 是否声明了 cairo 上下文 cr

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

static void do_drawing(cairo_t *);

struct {
int count;
double coordx[100];
double coordy[100];
} glob;

static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
do_drawing(cr);

return FALSE;
}

static void do_drawing(cairo_t *cr)
{
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 0.5);

int i, j;
for (i = 0; i <= glob.count - 1; i++ ) {
for (j = 0; j <= glob.count - 1; j++ ) {
cairo_move_to(cr, glob.coordx[i], glob.coordy[i]);
cairo_line_to(cr, glob.coordx[j], glob.coordy[j]);
}
}

glob.count = 0;
cairo_stroke(cr);
}

static gboolean clicked(GtkWidget *widget, GdkEventButton *event,
gpointer user_data)
{
if (event->button == 1) {
glob.coordx[glob.count] = event->x;
glob.coordy[glob.count++] = event->y;
}

if (event->button == 3) {
gtk_widget_queue_draw(widget);
}

return TRUE;
}


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

glob.count = 0;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

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

gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);

g_signal_connect(G_OBJECT(darea), "draw",
G_CALLBACK(on_draw_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);

g_signal_connect(window, "button-press-event",
G_CALLBACK(clicked), NULL);

gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
gtk_window_set_title(GTK_WINDOW(window), "Lines");

gtk_widget_show_all(window);

gtk_main();

return 0;
}

cairo context cr 是否在代码中自动声明并与 darea(绘图的不幸名称)关联area) 当我们调用函数时

  g_signal_connect(G_OBJECT(darea), "draw", 
G_CALLBACK(on_draw_event), NULL);

?

最佳答案

小部件将发出信号并传递它的内部 cairo 上下文。当您连接一个回调来处理信号时,cairo 上下文由小部件发送,您接收并处理它。

Draw 信号属于 Gtk Widget 类:

gboolean user_function (GtkWidget *widget, CairoContext *cr, gpointer user_data)

来自draw documentation :

This signal is emitted when a widget is supposed to render itself. The widget 's top left corner must be painted at the origin of the passed in context and be sized to the values returned by gtk_widget_get_allocated_width() and gtk_widget_get_allocated_height().

Signal handlers connected to this signal can modify the cairo context passed as cr in any way they like and don't need to restore it. The signal emission takes care of calling cairo_save() before and cairo_restore() after invoking the handler.

The signal handler will get a cr with a clip region already set to the widget's dirty region, i.e. to the area that needs repainting. Complicated widgets that want to avoid redrawing themselves completely can get the full extents of the clip region with gdk_cairo_get_clip_rectangle(), or they can get a finer-grained representation of the dirty region with cairo_copy_clip_rectangle_list().

关于c - cairo 上下文对象 cr 在哪里声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43633012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com