From cdc4fcd817e86253cbe03a3ceb77edb07ae253d3 Mon Sep 17 00:00:00 2001 From: Nikita Zlobin Date: Tue, 17 May 2011 01:20:36 +0600 Subject: [PATCH] Began to implement connections. Incompleted. --- src/gtkgraph-demo.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/src/gtkgraph-demo.c b/src/gtkgraph-demo.c index 66ae21d..05ff484 100644 --- a/src/gtkgraph-demo.c +++ b/src/gtkgraph-demo.c @@ -42,9 +42,12 @@ static gboolean on_module_dragged (GooCanvasItem * item, GooCanvasIt static gboolean on_module_text_notify (GooCanvasItem * item, GParamSpec * event, gpointer data); static gboolean on_port_text_notify (GooCanvasItem * item, GParamSpec * event, gpointer data); +static gboolean on_port_size_notify (GooCanvasItem * item, GParamSpec * event, gpointer data); +static gboolean on_port_moved (GooCanvasItem * item, GParamSpec * event, gpointer data); static gboolean wire_entered (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); static gboolean wire_left (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); +static gboolean on_wire_buttonPress_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data); double border_threshold = 1.0; @@ -84,6 +87,7 @@ struct { /* List of ports groups */ GList * ports; + GList * connections; /* Dragging data */ double drag_hold_x, drag_hold_y; @@ -105,6 +109,8 @@ struct { unsigned width; unsigned p_count; GooCanvasItem * container; + + GModularGraph_Module * module; } GModularGraphPortsGroup; #define G_MODULAR_GRAPH_PORTS_GROUP(p) ((GModularGraphPortsGroup *) (p)) @@ -117,6 +123,7 @@ struct { GModularGraphPortsGroup * ports_group; double min_width; + GList * connections; double wire_x, wire_y; double direction; /* In deegreens. Also affects wire direction. */ } GModularGraph_Port; @@ -143,6 +150,15 @@ struct { #define G_MODULAR_GRAPH_WIRE(p) ((GModularGraph_Wire *) (p)) +typedef +struct { + GModularGraph_Wire * wire; + GModularGraph_Port * port1; + GModularGraph_Port * port2; +} GModularGraph_Connection; + +#define G_MODULAR_GRAPH_CONNECTION(p) ((GModularGraph_Connection *) (p)) + GModularGraph_Wire * g_modular_graph_wire_new (double x1, double y1, double angle1, double x2, double y2, double angle2, char * tooltip, unsigned color); @@ -245,6 +261,7 @@ GModularGraph_Module * g_modular_graph_module_new (GooCanvasItem * parent, g_signal_connect (G_OBJECT (module->group), "enter-notify-event", G_CALLBACK (on_module_enterNotify_event), module); g_signal_connect (G_OBJECT (module->group), "leave-notify-event", G_CALLBACK (on_module_leaveNotify_event), module); + module->connections = NULL; g_object_set (G_OBJECT (module->caption), "text", caption, NULL); return module; } @@ -272,6 +289,7 @@ void g_modular_graph_module_add_ports_group (GModularGraphPortsGroup ** ret, GMo goo_canvas_item_set_child_properties (module->layout, group->container, "y-align", 0.0, NULL); module->ports = g_list_append (module->ports, group); + group->module = module; if (ret) * ret = group; } @@ -337,6 +355,11 @@ void g_modular_graph_module_add_port (GModularGraph_Port ** ret, "stroke-color", "gray70", "fill-color", "gray60", NULL); + g_signal_connect (G_OBJECT (port->box), "notify::width", G_CALLBACK (on_port_size_notify), port); + g_signal_connect (G_OBJECT (port->box), "notify::height", G_CALLBACK (on_port_size_notify), port); + g_signal_connect (G_OBJECT (port->box), "notify::x", G_CALLBACK (on_port_moved), port); + g_signal_connect (G_OBJECT (port->box), "notify::y", G_CALLBACK (on_port_moved), port); + /* Caption */ port->text = goo_canvas_text_new (group->container, "", rounding, 0, -1, GTK_ANCHOR_NORTH_WEST, @@ -366,6 +389,7 @@ void g_modular_graph_module_add_port (GModularGraph_Port ** ret, /* Complete initialization */ g_object_set (G_OBJECT (port->text), "text", name, NULL); module_update_size (module); + port->connections = NULL; group->p_count++; if (ret) *ret = port; @@ -441,6 +465,33 @@ void color_adjust_lightness (unsigned * value, double diff) * value = color.value; } +GModularGraph_Connection * g_modular_graph_connection_new (GModularGraph_Port * out, GModularGraph_Port * in) +{ + GModularGraph_Connection * connection = calloc (1, sizeof (GModularGraph_Connection)); + connection->port1 = out; + connection->port2 = in; + + GooCanvasBounds p_bounds [2]; + goo_canvas_item_get_bounds (out->box, & p_bounds [0]); + goo_canvas_item_get_bounds (in->box, & p_bounds [1]); + connection->wire = g_modular_graph_wire_new (p_bounds[0].x2, (p_bounds[0].y1 + p_bounds[0].y2) / 2, out->direction, + p_bounds[1].x1, (p_bounds[1].y1 + p_bounds[1].y2) / 2, in->direction, + "Test connection", 0x803030ff); + + GModularGraph_Module * module; + module = out->ports_group->module; + module->connections = g_list_append (module->connections, connection); + out->connections = g_list_append (out->connections, connection); + + module = in->ports_group->module; + module->connections = g_list_append (module->connections, connection); + in->connections = g_list_append (in->connections, connection); + + g_signal_connect (G_OBJECT (connection->wire->group), "button-release-event", + G_CALLBACK (on_wire_buttonPress_event), connection->wire->group); + return NULL; +} + GModularGraph_Wire * g_modular_graph_wire_new (double x1, double y1, double angle1, double x2, double y2, double angle2, char * tooltip, unsigned color) { char * wire_data; @@ -489,6 +540,13 @@ GModularGraph_Wire * g_modular_graph_wire_new (double x1, double y1, double angl return wire; } +static gboolean after_init (gpointer data) +{ + goo_canvas_item_remove (G_MODULAR_GRAPH_PORT (data)->box); + goo_canvas_item_remove (G_MODULAR_GRAPH_PORT (data)->text); + return TRUE; +} + int main( int argc, char ** argv ) { GtkWidget * win; @@ -519,21 +577,24 @@ int main( int argc, char ** argv ) g_signal_connect (G_OBJECT (root_item), "button-press-enter", G_CALLBACK (on_root_buttonPress_event), NULL); g_signal_connect (G_OBJECT (root_item), "button-press-enter", G_CALLBACK (on_root_buttonRelease_event), NULL); + GModularGraph_Port * port1, * port2; module[0] = g_modular_graph_module_new (root_item, "Module 1", "Module 1 tip", 0, 50, 50); g_modular_graph_module_add_port (NULL, module[0], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "in-1", "Port 1 tip"); - g_modular_graph_module_add_port (NULL, module[0], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "in-2", "Port 2 tip"); + g_modular_graph_module_add_port (& port2, module[0], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "in-2", "Port 2 tip"); module[1] = g_modular_graph_module_new (root_item, "Module 2", "Module 2 tip", 0, 200, 10); g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "in-1", "Port 1 tip"); g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "in-2", "Port 2 tip"); g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "input-3", "Port 3 tip"); - g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_OUT, "out-1", "Out 1 tip"); + g_modular_graph_module_add_port (& port1, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_OUT, "out-1", "Out 1 tip"); g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_OUT, "out-2", "Out 2 tip"); /* 2. Connection */ g_modular_graph_wire_new (50, 100, 0.0, 300, 300, 180, "Test wire", 0x5f0000ff); g_modular_graph_wire_new (50, 300, 0.0, 300, 100, 180, "Test wire", 0x5f0000ff); - /* * * * */ + g_modular_graph_connection_new (port1, port2); + + //g_timeout_add (3000, after_init, myport); gtk_widget_set_has_tooltip (canvas, TRUE); @@ -686,6 +747,35 @@ static gboolean on_port_text_notify (GooCanvasItem * item, GParamSpec * event, g return FALSE; } +static void port_update_wire_pos (GModularGraph_Port * port) +{ + double w, h; + g_object_get (port->box, "width", & w, "height", & h, NULL); + + port->wire_y = h / 2; + if (port->direction == 0.0) + port->wire_x = w; + else if (port->direction == 180.0) + port->wire_x = 0.0; +} + +static gboolean on_port_size_notify (GooCanvasItem * item, GParamSpec * event, gpointer data) +{ + g_print ("on_port_size_notify: %s\n", event->name); + port_update_wire_pos (data); + return FALSE; +} + +static gboolean on_port_moved (GooCanvasItem * item, GParamSpec * event, gpointer data) +{ + g_print ("on_port_moved: %s\n", event->name); + return FALSE; +} + +#define DRAG_CB( mod ) (G_MODULAR_GRAPH_PORT( mod )->drag_cb) +#define DRAG_HOLD_X( mod ) (G_MODULAR_GRAPH_PORT( mod )->drag_hold_x) +#define DRAG_HOLD_Y( mod ) (G_MODULAR_GRAPH_PORT( mod )->drag_hold_y) + /* Wire callbacks */ static @@ -713,3 +803,13 @@ gboolean wire_left (GooCanvasItem *item, g_object_set (G_MODULAR_GRAPH_WIRE (data)->path_t, "stroke-color-rgba", color, NULL); return FALSE; } + +static +gboolean on_wire_buttonPress_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data) +{ + if (event->button == 2) + { + g_print ("Wire was clicked\n"); + } + return FALSE; +} -- 2.11.4.GIT