显示其他小部件时,绘图区域中的 X,y 坐标会发生移动

问题描述 投票:0回答:1

请查看随附的屏幕截图以了解问题。我将 GTK 绘图区域与其他小部件打包在一起。当显示这些小部件时,绘图区域中的坐标会发生移动,因此中心线不会在中心绘制。我的代码如下:

    //Let's translate to origin of the drawing area allocation to avoid
//the cross lines to be rotated too
cairo_save(cr);
cairo_identity_matrix(cr);
cairo_translate(cr, allocation.x, allocation.y);

//This to compensate the allocation menubar height gap
//which prevents the horizontal line to be drawn at the center
//I wonder why the coordinates in the drawing area are shifted
//when the blessed menubar is hidden by the TAB key, shouldn't
//they always be 0,0 at the top left?

int menubar_height = gtk_widget_get_allocated_height(img->menubar);
if (img->textbox->draw_horizontal_line)
{
    if (gtk_widget_is_visible(img->menubar))
        center_y = menubar_height + center_y;
    else
        center_y = allocation.height / 2;

//Draw the horizontal centering line
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_set_line_width(cr, 1.0);
    cairo_move_to(cr, 0, center_y - 2);
    cairo_line_to(cr, allocation.width, center_y - 2);
    cairo_stroke(cr);
    cairo_set_source_rgb(cr, 0.8, 0.7, 0.3);
    cairo_move_to(cr, 0, center_y);
    cairo_line_to(cr, allocation.width, center_y);
    cairo_stroke(cr);
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_move_to(cr, 0, center_y + 2);
    cairo_line_to(cr, allocation.width, center_y + 2);
    cairo_stroke(cr);
}

// Draw the vertical centering line
if (img->textbox->draw_vertical_line)
{
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_set_line_width(cr, 1.0);
    cairo_move_to(cr, center_x - 2, 0);
    cairo_line_to(cr, center_x - 2, allocation.height + menubar_height);
    cairo_stroke(cr);
    cairo_set_source_rgb(cr, 0.8, 0.7, 0.3);
    cairo_move_to(cr, center_x, 0);
    cairo_line_to(cr, center_x, allocation.height + menubar_height);
    cairo_stroke(cr);
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
    cairo_move_to(cr, center_x + 2, 0);
    cairo_line_to(cr, center_x + 2, allocation.height + menubar_height);
    cairo_stroke(cr);
}
cairo_restore(cr);

这就是我打包绘图区域的方式:

/* Create the image area */
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
g_object_set(G_OBJECT(vbox), "valign", GTK_ALIGN_CENTER);
gtk_box_pack_start (GTK_BOX (right_horizontal_box), vbox, TRUE, TRUE, 0);

img_struct->image_area = gtk_drawing_area_new();
gtk_box_pack_start(GTK_BOX(vbox), img_struct->image_area, FALSE, FALSE, 0);
gtk_widget_set_hexpand(img_struct->image_area, FALSE);
gtk_widget_set_vexpand(img_struct->image_area, FALSE);

gtk_widget_set_halign(img_struct->image_area, GTK_ALIGN_CENTER);
gtk_widget_set_valign(img_struct->image_area, GTK_ALIGN_CENTER);
gtk_widget_set_events(img_struct->image_area, 
                                  GDK_KEY_PRESS_MASK
                                | GDK_BUTTON_PRESS_MASK
                                | GDK_BUTTON_RELEASE_MASK
                                | GDK_POINTER_MOTION_MASK);

gtk_widget_set_can_focus(img_struct->image_area, TRUE);
gtk_widget_grab_focus(img_struct->image_area);

有人可以向我解释一下为什么当与它一起打包的其他小部件可见时,0,0 坐标会在绘图区域中移动吗?

enter image description here

gtk3 cairo cartesian-coordinates userspace
1个回答
0
投票

好吧,解决方案是避免重置单位矩阵并转换为绘图区域分配 x 和 y,但以负角重用此代码:

        cairo_save(cr);
        cairo_translate(cr, img->textbox->x + img->textbox->width / 2, img->textbox->y + img->textbox->height / 2);
        cairo_rotate(cr, -img->textbox->angle);
        cairo_translate(cr, -(img->textbox->x + img->textbox->width / 2), -(img->textbox->y + img->textbox->height / 2));

//画线 开罗_恢复(cr);

© www.soinside.com 2019 - 2024. All rights reserved.