Add Node to a specific location from context menu
vgrcs opened this issue · 4 comments
Hi,
Regarding adding Nodes to the NodeView using the context menu provided in example "demo.c", can it be possible to set the initial position of a new Node to the position of the mouse right-click?
I found that the NodeView container uses the GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT/HEIGHT_FOR_WIDTH size request mode. But when a sketch is loaded from file, the installed properties "x", "y", "width" and "height" seems to override the layout. Can it be possible to do this when a Node is created on the fly?
Thank you,
Vítor
Hi,
node items in a NodeView have properties x, y, width and height.
When creating the particular item, you can set these to place it.
Below is the relevant code for C and python, I've added it to the respective demos as well:
static void node_view_create_pulse_cb(GtkWidget *menu, GtkWidget *node_view)
{
gint x, y;
GtkWidget *w;
GdkWindow *win;
GdkSeat *seat;
GdkDevice *ptr;
GValue pos = G_VALUE_INIT;
seat = gdk_display_get_default_seat(gdk_display_get_default());
ptr = gdk_seat_get_pointer(seat);
win = gtk_widget_get_window(node_view);
gdk_window_get_device_position(win, ptr, &x, &y, NULL);
w = node_pulse_new();
gtk_container_add(GTK_CONTAINER(node_view), w);
g_value_init(&pos, G_TYPE_INT);
g_value_set_int (&pos, x);
gtk_container_child_set_property(GTK_CONTAINER(node_view), w, "x", &pos);
g_value_set_int (&pos, y);
gtk_container_child_set_property(GTK_CONTAINER(node_view), w, "y", &pos);
}
def create_imgsrcnode(self, widget=None):
# how to place a node at an arbitrary location:
pointer = self.node_view.get_display().get_default_seat().get_pointer()
# x, y are in [1,2]
pos = self.node_view.get_window().get_device_position(pointer)
node = ImgSrcNode()
self.node_view.add(node)
self.node_view.child_set_property(node, "x", pos[1]);
# we set x only, we're out of the canvas when selecting from the button bar
# self.node_view.child_set_property(node, "y", pos[2]);
self.node_view.show_all()
Hi,
In fact, after specifying both "x" and "y", the node is positioned along the diagonal given by the MAX_WIDTH/MAX_HEIGHT.
Then, if I only specify "x", then I also need to set the scrolling policy on the "y" axis to AUTOMATIC.
This option resembles what you described in your example.
If you're using pos[2] for y when using the pointer coordinates from the button bar, the value is actually negative since it is outside of the canvas. That appears to be the source of the placement issue.
Hi,
When using the toolbar, the pos[2] is in fact negative.
But if you use a constant positive number (say 5, 10 or 20) and choose to set the child's "y", the actual "y" on the screen is equal to "x" because "x" is bigger "y". I think this conforms to the layout, since the node is placed along a diagonal inside the canvas.