From 3c9d81a39c2525c1e0acf50b8753f36064d90719 Mon Sep 17 00:00:00 2001 From: Nikita Zlobin Date: Fri, 13 May 2011 11:17:57 +0600 Subject: [PATCH] Ports group; Code optimization; Begining to work at paning by dragging; --- src/gtkgraph-demo.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 14 deletions(-) diff --git a/src/gtkgraph-demo.c b/src/gtkgraph-demo.c index 7ade580..da20f75 100644 --- a/src/gtkgraph-demo.c +++ b/src/gtkgraph-demo.c @@ -23,6 +23,10 @@ #include #include +static gboolean on_root_buttonPress_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data); +static gboolean on_root_buttonRelease_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data); +static gboolean on_root_dragged (GooCanvasItem * item, GooCanvasItem * target, GdkEventMotion * event, gpointer data); + static gboolean on_module_enterNotify_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); static gboolean on_module_leaveNotify_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); @@ -85,6 +89,23 @@ struct { #define G_MODULAR_GRAPH_MODULE(p) ((GModularGraph_Module *) p) +typedef enum { + G_MODULAR_GRAPH_PORT_DIRECTION_IN = 1, + G_MODULAR_GRAPH_PORT_DIRECTION_OUT +} GModularGraphPortDirection; + +typedef +struct { + GList * ports; + GModularGraphPortDirection direction; + + unsigned width; + unsigned p_count; + GooCanvasItem * container; +} GModularGraphPortsGroup; + +#define G_MODULAR_GRAPH_PORTS_GROUP(p) ((GModularGraphPortsGroup *) p) + typedef struct { GooCanvasItem * box; @@ -158,31 +179,90 @@ GModularGraph_Module * g_modular_graph_module_new (GooCanvasItem * parent, return module; } +void g_modular_graph_module_add_ports_group (GModularGraphPortsGroup ** ret, GModularGraph_Module * module, GModularGraphPortDirection direction) +{ + GModularGraphPortsGroup * group; + + group = calloc (1, sizeof(GModularGraphPortsGroup)); + group->direction = direction; + group->container = goo_canvas_table_new (module->group, "x", 0.0, "y", 20.0, "row-spacing", 1.0, "column-spacing", 1.0, + /*"horz-grid-line-width", 1.0,*/ NULL); + module->ports = g_list_append (module->ports, group); + + if (ret) * ret = group; +} + +static void canvas_text_size (GooCanvasItem * item, double * w, double * h) +{ + PangoRectangle rect; + + goo_canvas_text_get_natural_extents (GOO_CANVAS_TEXT (item), NULL, & rect); + if (w) *w = (double) PANGO_PIXELS (rect.width); + if (h) *h = (double) PANGO_PIXELS (rect.height); +} + +void g_modular_graph_ports_group_add_port (GModularGraphPortsGroup * group, GModularGraph_Port * port) +{ + /* Add to list */ + if (! g_list_find (group->ports, port)) + group->ports = g_list_append (group->ports, port); + + /* Update group width */ + double w; + canvas_text_size (port->text, &w, NULL); + if (w > group->width) + group->width = w; +} + void g_modular_graph_module_add_port (GModularGraph_Port ** ret, GModularGraph_Module * module, int direction, char * name, char * tooltip) { - /* Fundament */ + /* Create ports group */ + GModularGraphPortsGroup * group = NULL; + if (module->ports) + { + GList * elem = module->ports; + while (elem->data) + if (G_MODULAR_GRAPH_PORTS_GROUP (elem->data)->direction == direction) + { + group = elem->data; + break; + } + } + if (! group) + { + g_modular_graph_module_add_ports_group (& group, module, direction); + } + GModularGraph_Port * port = calloc (1, sizeof (GModularGraph_Port)); - port->group = goo_canvas_group_new (module->group, "x", rounding, "y", 20.0, "tooltip", tooltip, NULL); /* Decoration */ - port->box = goo_canvas_rect_new (port->group, - 0.5, 0.5, 50, 20, + port->box = goo_canvas_rect_new (group->container, + 0.5, 0.5, 50, 15, + "tooltip", tooltip, "radius-x", rounding, "radius-y", rounding, + "line-width", border_threshold, "stroke-color", "gray60", "fill-color", "gray50", NULL); /* Caption */ - port->text = goo_canvas_text_new (port->group, "", + port->text = goo_canvas_text_new (group->container, "", rounding, 0, -1, GTK_ANCHOR_NORTH_WEST, + "tooltip", tooltip, "font", "sans 8", "fill-color", "white", NULL); + goo_canvas_item_set_child_properties (group->container, port->box, "row", group->p_count, "x-expand", TRUE, "x-fill", TRUE, NULL); + goo_canvas_item_set_child_properties (group->container, port->text, "row", group->p_count, "x-expand", TRUE, "x-fill", TRUE, + "left-padding", rounding, NULL); g_signal_connect (G_OBJECT (port->text), "notify::text", G_CALLBACK (on_port_text_notify), port); + g_object_set (G_OBJECT (group->container), "width", 100.0, NULL); g_object_set (G_OBJECT (port->text), "text", name, NULL); + + group->p_count++; if (ret) *ret = port; } @@ -213,10 +293,14 @@ int main( int argc, char ** argv ) g_object_set (G_OBJECT (root_item), "line-join", CAIRO_LINE_JOIN_ROUND, NULL); g_object_set (G_OBJECT (root_item), "line-cap", CAIRO_LINE_CAP_ROUND, NULL); g_object_set (G_OBJECT (root_item), "line-width", border_threshold, NULL); + 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); module[0] = g_modular_graph_module_new (root_item, "Module 1", "Module 1 tip", 0, 50, 50); 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], 0, "Port 1", "Port 1 tip"); + g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "Port 1", "Port 1 tip"); + g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "Port 2", "Port 2 tip"); + g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_IN, "Port 3 new", "Port 3 tip"); /* 2. Connection */ char * wire_data = "M50,100 C100,100 50,200 100,200"; @@ -241,6 +325,23 @@ int main( int argc, char ** argv ) /* Helpers (nothing yet) */ +/* Root callbacks */ + +static gboolean on_root_buttonPress_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data) +{ + return FALSE; +} + +static gboolean on_root_buttonRelease_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data) +{ + return FALSE; +} + +static gboolean on_root_dragged (GooCanvasItem * item, GooCanvasItem * target, GdkEventMotion * event, gpointer data) +{ + return FALSE; +} + /* Module callbacks */ static gboolean on_module_enterNotify_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data) @@ -338,12 +439,11 @@ gboolean on_module_dragged (GooCanvasItem * item, GooCanvasItem * target, GdkEve static gboolean on_module_text_notify (GooCanvasItem * item, GParamSpec * event, gpointer data) { - PangoRectangle rect; double w, h; - goo_canvas_text_get_natural_extents (GOO_CANVAS_TEXT (item), NULL, & rect); - w = (double) PANGO_PIXELS (rect.width) + rounding * 2; - h = (double) PANGO_PIXELS (rect.height); + canvas_text_size (item, &w, &h); + w += rounding * 2; + g_object_set (G_MODULAR_GRAPH_MODULE (data)->box, "width", w, NULL); g_object_set (G_MODULAR_GRAPH_MODULE (data)->box, "height", h, NULL); @@ -354,12 +454,11 @@ static gboolean on_module_text_notify (GooCanvasItem * item, GParamSpec * event, static gboolean on_port_text_notify (GooCanvasItem * item, GParamSpec * event, gpointer data) { - PangoRectangle rect; double w, h; - goo_canvas_text_get_natural_extents (GOO_CANVAS_TEXT (item), NULL, & rect); - w = (double) PANGO_PIXELS (rect.width) + rounding * 2; - h = (double) PANGO_PIXELS (rect.height); + canvas_text_size (item, &w, &h); + w += rounding * 2; + g_object_set (G_MODULAR_GRAPH_PORT (data)->box, "width", w, NULL); g_object_set (G_MODULAR_GRAPH_PORT (data)->box, "height", h, NULL); -- 2.11.4.GIT