X,y coordinates in the drawing area are shifted when other widgets are shown

Please see the attached screenshots to realize the problem. My code is as follows:

        //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);

This is how I pack the drawing area instead:

/* 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);

Can someone explain to me why the 0,0 coords are shifted in the drawing area when other widgets packed together with it are visible?


Does anybody have a clue please?

In GTK3 (which is what I assume you’re using) the coordinates are always relative to the GdkWindow of the widget: Gtk – 3.0: The GTK Drawing Model

Does this mean I have to use the allocation on the drawing area’s Gdkwindow rather than the area itself?

Ok the solution was to avoid resetting the identity matrix and translating to the drawing area allocation x and y but reuse this code with the negative angle:

        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->t