From 75db865555ed81126ba5ec2bf5a3d2ecad4c81f1 Mon Sep 17 00:00:00 2001 From: pascal-niklaus Date: Wed, 21 Feb 2024 20:39:07 +0100 Subject: [PATCH 1/7] first version of --deftool --- CMakeLists.txt | 2 + src/callbacks.c | 241 ++++++++++++++++++- src/config.c | 599 ++++++++++++++++++++++++++---------------------- src/config.h | 24 ++ src/main.c | 20 +- src/main.h | 35 +-- src/parser.c | 139 +++++++++++ src/parser.h | 37 +++ 8 files changed, 798 insertions(+), 299 deletions(-) create mode 100644 src/parser.c create mode 100644 src/parser.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d64d92e..f5e09c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,8 @@ set(sources src/callbacks.h src/config.c src/config.h + src/parser.c + src/parser.h src/drawing.c src/drawing.h src/coordlist_ops.c diff --git a/src/callbacks.c b/src/callbacks.c index 239b590..bdd0a47 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -29,6 +29,7 @@ #include "input.h" #include "callbacks.h" #include "config.h" +#include "parser.h" #include "drawing.h" #include "build-config.h" #include "coordlist_ops.h" @@ -211,7 +212,9 @@ void on_clientapp_selection_get (GtkWidget *widget, g_printerr("DEBUG: clientapp received request.\n"); - if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA || gtk_selection_data_get_target(selection_data) == GA_LINEDATA) + if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA || + gtk_selection_data_get_target(selection_data) == GA_LINEDATA || + gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA) { ans = data->clientdata; } @@ -558,6 +561,11 @@ void on_mainapp_selection_get (GtkWidget *widget, undo_drawing (data); else if (action == GA_REDO) redo_drawing (data); + else if (action == GA_CHGTOOL) + { + gtk_selection_convert(data->win, GA_DATA, GA_CHGTOOLDATA, time); + gtk_main(); + } else uri = "NOK"; @@ -634,6 +642,7 @@ void on_mainapp_selection_received (GtkWidget *widget, GdkRGBA* color = g_malloc (sizeof (GdkRGBA)); GdkRGBA *fg_color=data->red; + if (gdk_rgba_parse (color, hex_code)) { fg_color = color; @@ -667,7 +676,235 @@ void on_mainapp_selection_received (GtkWidget *widget, g_free(line_ctx); g_free (color); - } + } + else if (gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA) + { + gchar *a = (gchar *)gtk_selection_data_get_data(selection_data); + g_printerr("DEFTOOL argument: %s\n", a); + + GScanner *scanner; + scanner = g_scanner_new(NULL); + scanner_init(scanner); + g_scanner_input_text(scanner, a, strlen(a)); + + GTokenType token; + token = g_scanner_get_next_token (scanner); + + gchar *name, *copy; + GromitPaintContext *context_template=NULL; + + GromitPaintType type; + GdkRGBA *fg_color=NULL; + guint width, arrowsize, minwidth, maxwidth; + + if (token != G_TOKEN_EOF) + { + if (token == G_TOKEN_STRING) + { + name = parse_name (scanner); + + if(!name) + goto cleanup; + + token = g_scanner_cur_token(scanner); + + if (token != G_TOKEN_EQUAL_SIGN) + { + g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL, + NULL, NULL, "aborting", TRUE); + goto cleanup; + } + + token = g_scanner_get_next_token (scanner); + + /* defaults */ + type = GROMIT_PEN; + width = 7; + arrowsize = 0; + minwidth = 1; + maxwidth = G_MAXUINT; + fg_color = data->red; + + if (token == G_TOKEN_SYMBOL) + { + type = (GromitPaintType) scanner->value.v_symbol; + token = g_scanner_get_next_token (scanner); + } + else if (token == G_TOKEN_STRING) + { + copy = parse_name (scanner); + if(!copy) + goto cleanup; + token = g_scanner_cur_token(scanner); + context_template = g_hash_table_lookup (data->tool_config, copy); + if (context_template) + { + type = context_template->type; + width = context_template->width; + arrowsize = context_template->arrowsize; + minwidth = context_template->minwidth; + maxwidth = context_template->maxwidth; + fg_color = context_template->paint_color; + } + else + { + g_printerr ("WARNING: Unable to copy \"%s\": " + "not yet defined!\n", copy); + } + } + else + { + g_printerr ("Expected Tool-definition " + "or name of template tool\n"); + goto cleanup; + } + + /* Are there any tool-options? + */ + + if (token == G_TOKEN_LEFT_PAREN) + { + GdkRGBA *color = NULL; + g_scanner_set_scope (scanner, 2); + scanner->config->int_2_float = 1; + token = g_scanner_get_next_token (scanner); + while (token != G_TOKEN_RIGHT_PAREN) + { + if (token == G_TOKEN_SYMBOL) + { + if ((intptr_t) scanner->value.v_symbol == 1) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + goto cleanup; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr ("Missing Size (float)... aborting\n"); + goto cleanup; + } + width = (guint) (scanner->value.v_float + 0.5); + } + else if ((intptr_t) scanner->value.v_symbol == 2) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + goto cleanup; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_STRING) + { + g_printerr ("Missing Color (string)... " + "aborting\n"); + goto cleanup; + } + color = g_malloc (sizeof (GdkRGBA)); + if (gdk_rgba_parse (color, scanner->value.v_string)) + { + fg_color = color; + } + else + { + g_printerr ("Unable to parse color. " + "Keeping default.\n"); + g_free (color); + } + color = NULL; + } + else if ((intptr_t) scanner->value.v_symbol == 3) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + goto cleanup; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr ("Missing Arrowsize (float)... " + "aborting\n"); + goto cleanup; + } + arrowsize = scanner->value.v_float; + } + else if ((intptr_t) scanner->value.v_symbol == 4) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + goto cleanup; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr ("Missing Minsize (float)... " + "aborting\n"); + goto cleanup; + } + minwidth = scanner->value.v_float; + } + else if ((intptr_t) scanner->value.v_symbol == 5) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + goto cleanup; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr ("Missing Maxsize (float)... " + "aborting\n"); + goto cleanup; + } + maxwidth = scanner->value.v_float; + } + else + { + g_printerr ("Unknown tool type ???\n"); + } + } + else + { + g_printerr("skipped unknown token: %d !!!\n", token); + } + token = g_scanner_get_next_token (scanner); + } // while (token != G_TOKEN_RIGHT_PAREN) + g_scanner_set_scope (scanner, 0); + token = g_scanner_get_next_token (scanner); + } // if (token == G_TOKEN_LEFT_PAREN) + + // by now nothing should follow + if (token != G_TOKEN_EOF) + { + g_printerr ("End of tool definition expected !\n"); + goto cleanup; + } + + GromitPaintContext *context = + g_hash_table_lookup(data->tool_config, name); + GromitPaintContext *new_context = + paint_context_new (data, type, fg_color, width, arrowsize, minwidth, maxwidth); + *context = *new_context; + + g_free(new_context); + } // if (token == G_TOKEN_STRING) + + token = g_scanner_get_next_token (scanner); + } // if (token != G_TOKEN_EOF) + + cleanup: + + g_scanner_destroy (scanner); + } } gtk_main_quit (); diff --git a/src/config.c b/src/config.c index c441de2..9acdc1a 100644 --- a/src/config.c +++ b/src/config.c @@ -29,6 +29,7 @@ #include #include "config.h" +#include "parser.h" #include "main.h" #include "math.h" #include "build-config.h" @@ -41,10 +42,64 @@ static gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3; static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; /* - * Functions for parsing the Configuration-file + * IMPORTANT RULE: the color field in GromitPaintContext is + * _always_ individually allocated, i.e. GdkRGBA pointers all point to + * separate copies. */ -static gchar* parse_name (GScanner *scanner) +/* + * initialize GScanner for the parsing of tool definitions + */ +void scanner_init(GScanner *scanner) +{ + scanner->config->case_sensitive = 0; + scanner->config->scan_octal = 0; + scanner->config->identifier_2_string = 0; + scanner->config->char_2_token = 1; + scanner->config->numbers_2_int = 1; + scanner->config->int_2_float = 1; + + g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); + g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); + g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); + g_scanner_scope_add_symbol (scanner, 0, "SMOOTH", (gpointer) GROMIT_SMOOTH); + g_scanner_scope_add_symbol (scanner, 0, "ORTHOGONAL",(gpointer) GROMIT_ORTHOGONAL); + g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); + g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR); + g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); + g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); + + g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5); + g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11); + g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12); + g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13); + g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13); + + g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) SYM_SIZE); + g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) SYM_COLOR); + g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) SYM_ARROWSIZE); + g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE); + g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE); + g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE); + g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS); + g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE); + g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN); + g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY); + g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP); + + g_scanner_set_scope (scanner, 0); + scanner->config->scope_0_fallback = 0; +} + +/* + * returns the name of the tool, or NULL + * the string returned is allocated and then owned by the caller + */ +gchar* parse_name (GScanner *scanner) { GTokenType token; @@ -64,7 +119,6 @@ static gchar* parse_name (GScanner *scanner) len = strlen (scanner->value.v_string); name = g_strndup (scanner->value.v_string, len + 3); - token = g_scanner_get_next_token (scanner); /* @@ -111,20 +165,224 @@ static gchar* parse_name (GScanner *scanner) return name; } +/* + * get the "type" of the tool, e.g. PEN (e.g. =PEN), or a base tool + * style that is inherited (e.g. ="red pen") and store characteristics + * in style. + * + * allocates a new GdkRGBA color + * + * returns FALSE upon error + */ +gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *style) +{ + GTokenType token = g_scanner_cur_token(scanner); + gboolean color_allocated = FALSE; + + if (token != G_TOKEN_EQUAL_SIGN) + { + g_scanner_unexp_token( + scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE); + goto cleanup; + } + + token = g_scanner_get_next_token (scanner); -enum tool_arguments { - SYM_SIZE = 1, - SYM_COLOR, - SYM_ARROWSIZE, - SYM_ARROWTYPE, - SYM_MINSIZE, - SYM_MAXSIZE, - SYM_MINLEN, - SYM_MAXANGLE, - SYM_RADIUS, - SYM_SIMPLIFY, - SYM_SNAP, -}; + // style defaults + style->type = GROMIT_PEN; + style->width = 7; + style->arrowsize = 0; + style->arrow_type = GROMIT_ARROW_END; + style->radius = 10; + style->minlen = style->radius * 5 / 2; + style->maxangle = 15; + style->simplify = 10; + style->snapdist = 0; + style->minwidth = 1; + style->maxwidth = G_MAXUINT; + + // allocate new color and copy default red fields + style->paint_color = g_malloc(sizeof (GdkRGBA)); + color_allocated = TRUE; + *style->paint_color = *data->red; + + if (token == G_TOKEN_SYMBOL) + { + style->type = (GromitPaintType) scanner->value.v_symbol; + token = g_scanner_get_next_token (scanner); + } + else if (token == G_TOKEN_STRING) + { + gchar *copy = parse_name (scanner); + g_printerr("parse_tool: string: %s\n", copy); + if(!copy) + goto cleanup; + token = g_scanner_cur_token(scanner); + GromitPaintContext *context = g_hash_table_lookup (data->tool_config, copy); + if (context) + { + // copy fields of context that is inherited from + style->type = context->type; + style->width = context->width; + style->arrowsize = context->arrowsize; + style->arrow_type = context->arrow_type; + style->radius = context->radius; + style->minlen = context->minlen; + style->maxangle = context->maxangle; + style->simplify = context->simplify; + style->snapdist = context->snapdist; + style->minwidth = context->minwidth; + style->maxwidth = context->maxwidth; + *style->paint_color = *context->paint_color; + + // nullify superfluous fields, although not really necessary + style->paint_ctx = NULL; + style->pressure = 0; + } + else + { + g_printerr ("WARNING: Unable to copy \"%s\": " + "not yet defined!\n", copy); + } + } + else + { + g_printerr ("Expected tool definition or name of template tool\n"); + goto cleanup; + } + return TRUE; + + cleanup: + if (color_allocated) + { + g_free(style->paint_color); + style->paint_color = NULL; + } + return FALSE; +} + +/* + * parse a single attribute (e.g. size=6) and store it in style, + * returning the attribute ID (here SYM_SIZE), or SYM_ERR if something + * went wrong. + */ +ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) +{ + const gint id = (intptr_t) scanner->value.v_symbol; + GTokenType token; + if (id == SYM_SIZE) + { + gfloat v = parse_float(scanner, "Missing size"); + if (isnan(v)) return SYM_ERROR; + style->width = v + 0.5; + } + else if (id == SYM_COLOR) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + return SYM_ERROR; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_STRING) + { + g_printerr ("Missing Color (string)... aborting\n"); + return SYM_ERROR; + } + if (! gdk_rgba_parse (style->paint_color, scanner->value.v_string)) + { + g_printerr ("Unable to parse color. Keeping default.\n"); + } + } + else if (id == SYM_ARROWSIZE) + { + gfloat v = parse_float(scanner, "Missing arrowsize"); + if (isnan(v)) return SYM_ERROR; + style->arrowsize = v; + } + else if (id == SYM_ARROWTYPE) + { + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr ("Missing \"=\"... aborting\n"); + return SYM_ERROR; + } + token = g_scanner_get_next_token (scanner); + if (token != G_TOKEN_STRING) + { + g_printerr ("Missing arrowtype (string)... aborting\n"); + return SYM_ERROR; + } + if (! strcasecmp(scanner->value.v_string, "end")) + { + style->arrow_type = GROMIT_ARROW_END; + } + else if (! strcasecmp(scanner->value.v_string, "start")) + { + style->arrow_type = GROMIT_ARROW_START; + } + else if (! strcasecmp(scanner->value.v_string, "double")) + { + style->arrow_type = GROMIT_ARROW_DOUBLE; + } + else + { + g_printerr ("Arrow type must be \"start\", \"end\", or \"double\"... " + "aborting\n"); + return SYM_ERROR; + } + } + else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS) + { + gfloat v = parse_get_float(scanner, "Missing radius (float)"); + if (isnan(v)) goto cleanup; + style->radius = v; + } + else if ((intptr_t) scanner->value.v_symbol == SYM_MAXANGLE) + { + gfloat v = parse_get_float(scanner, "Missing angle (float)"); + if (isnan(v)) goto cleanup; + style->maxangle = v; + } + else if ((intptr_t) scanner->value.v_symbol == SYM_SIMPLIFY) + { + gfloat v = parse_get_float(scanner, "Missing simplify value (float)"); + if (isnan(v)) goto cleanup; + style->simplify = v; + } + else if ((intptr_t) scanner->value.v_symbol == SYM_MINLEN) + { + gfloat v = parse_get_float(scanner, "Missing minlen value (float)"); + if (isnan(v)) goto cleanup; + style->minlen = v; + } + else if ((intptr_t) scanner->value.v_symbol == SYM_SNAP) + { + gfloat v = parse_get_float(scanner, "Missing snap distance (float)"); + if (isnan(v)) goto cleanup; + style->snapdist = v; + } + else if (id == SYM_MINSIZE) + { + gfloat v = parse_float(scanner, "Missing minsize"); + if (isnan(v)) return SYM_ERROR; + style->minwidth = v + 0.5; + } + else if (id == SYM_MAXSIZE) + { + gfloat v = parse_float(scanner, "Missing maxsize"); + if (isnan(v)) return SYM_ERROR; + style->maxwidth = v + 0.5; + } + else + { + g_printerr ("Unknown tool type?????\n"); + return SYM_ERROR; + } + return id; +} /* * get "=VALUE", where VALUE is a float @@ -148,24 +406,44 @@ gfloat parse_get_float(GScanner *scanner, const gchar *msg) return scanner->value.v_float; } +/* + * parses a pen style definition (e.g. (color="red" size=3) ) + * and stores fields found in GromitStyleDef. + * + * returns FALSE upon any error + */ +gboolean parse_style(GScanner *scanner, GromitPaintContext *style) +{ + g_scanner_set_scope (scanner, 2); + scanner->config->int_2_float = 1; + GTokenType token = g_scanner_get_next_token (scanner); + while (token != G_TOKEN_RIGHT_PAREN) + { + if (token == G_TOKEN_SYMBOL) + { + if (parse_attribute(scanner, style) == SYM_ERROR) + return FALSE; + } + token = g_scanner_get_next_token (scanner); + } + g_scanner_set_scope (scanner, 0); + token = g_scanner_get_next_token (scanner); + return TRUE; +} + +/* + * parse config file + */ gboolean parse_config (GromitData *data) { gboolean status = FALSE; GromitPaintContext *context=NULL; - GromitPaintContext *context_template=NULL; GScanner *scanner; GTokenType token; gchar *filename; int file; - - gchar *name, *copy; - - GromitPaintType type; - GdkRGBA *fg_color=NULL; - guint width, arrowsize, minwidth, maxwidth; - guint minlen, maxangle, radius, simplify, snapdist; - GromitArrowType arrowtype; + gchar *name; /* try user config location */ filename = g_strjoin (G_DIR_SEPARATOR_S, @@ -199,284 +477,49 @@ gboolean parse_config (GromitData *data) return FALSE; } - scanner = g_scanner_new (NULL); + scanner = g_scanner_new(NULL); + scanner_init(scanner); scanner->input_name = filename; - scanner->config->case_sensitive = 0; - scanner->config->scan_octal = 0; - scanner->config->identifier_2_string = 0; - scanner->config->char_2_token = 1; - scanner->config->numbers_2_int = 1; - scanner->config->int_2_float = 1; - - g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); - g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); - g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); - g_scanner_scope_add_symbol (scanner, 0, "SMOOTH", (gpointer) GROMIT_SMOOTH); - g_scanner_scope_add_symbol (scanner, 0, "ORTHOGONAL",(gpointer) GROMIT_ORTHOGONAL); - g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); - g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR); - g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); - g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); - - g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5); - g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11); - g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12); - g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13); - g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13); - - g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) SYM_SIZE); - g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) SYM_COLOR); - g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) SYM_ARROWSIZE); - g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE); - g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE); - g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE); - g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS); - g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE); - g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN); - g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY); - g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP); - - g_scanner_set_scope (scanner, 0); - scanner->config->scope_0_fallback = 0; - g_scanner_input_file (scanner, file); - token = g_scanner_get_next_token (scanner); + + GromitPaintContext style; + while (token != G_TOKEN_EOF) { if (token == G_TOKEN_STRING) { - /* - * New tool definition - */ - + // New tool definition name = parse_name (scanner); - if(!name) goto cleanup; - token = g_scanner_cur_token(scanner); - - if (token != G_TOKEN_EQUAL_SIGN) - { - g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL, - NULL, NULL, "aborting", TRUE); - goto cleanup; - } - - token = g_scanner_get_next_token (scanner); - - /* defaults */ - type = GROMIT_PEN; - width = 7; - arrowsize = 0; - arrowtype = GROMIT_ARROW_END; - minwidth = 1; - maxwidth = G_MAXUINT; - radius = 10; - minlen = 2 * radius + radius / 2; - maxangle = 15; - simplify = 10; - snapdist = 0; - fg_color = data->red; - - if (token == G_TOKEN_SYMBOL) - { - type = (GromitPaintType) scanner->value.v_symbol; - token = g_scanner_get_next_token (scanner); - } - else if (token == G_TOKEN_STRING) - { - copy = parse_name (scanner); - if(!copy) - goto cleanup; - token = g_scanner_cur_token(scanner); - context_template = g_hash_table_lookup (data->tool_config, copy); - if (context_template) - { - type = context_template->type; - width = context_template->width; - arrowsize = context_template->arrowsize; - arrowtype = context_template->arrow_type; - radius = context_template->radius; - simplify = context_template->simplify; - minlen = context_template->minlen; - maxangle = context_template->maxangle; - snapdist = context_template->snapdist; - minwidth = context_template->minwidth; - maxwidth = context_template->maxwidth; - fg_color = context_template->paint_color; - } - else - { - g_printerr ("WARNING: Unable to copy \"%s\": " - "not yet defined!\n", copy); - } - } - else + if (!parse_tool(data, scanner, &style)) { - g_printerr ("Expected Tool-definition " - "or name of template tool\n"); + g_printerr("parse tool failed\n"); goto cleanup; } - /* Are there any tool-options? - */ - + // are there any tool-options? + token = g_scanner_cur_token(scanner); if (token == G_TOKEN_LEFT_PAREN) { - GdkRGBA *color = NULL; - g_scanner_set_scope (scanner, 2); - scanner->config->int_2_float = 1; - token = g_scanner_get_next_token (scanner); - while (token != G_TOKEN_RIGHT_PAREN) - { - if (token == G_TOKEN_SYMBOL) - { - if ((intptr_t) scanner->value.v_symbol == SYM_SIZE) - { - gfloat v = parse_get_float(scanner, "Missing Size (float)"); - if (isnan(v)) goto cleanup; - width = (guint) (v + 0.5); - } - else if ((intptr_t) scanner->value.v_symbol == SYM_COLOR) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_STRING) - { - g_printerr ("Missing Color (string)... " - "aborting\n"); - goto cleanup; - } - color = g_malloc (sizeof (GdkRGBA)); - if (gdk_rgba_parse (color, scanner->value.v_string)) - { - fg_color = color; - } - else - { - g_printerr ("Unable to parse color. " - "Keeping default.\n"); - g_free (color); - } - color = NULL; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_ARROWSIZE) - { - gfloat v = parse_get_float(scanner, "Missing arrowsize (float)"); - if (isnan(v)) goto cleanup; - arrowsize = (guint)(v + 0.5); - arrowtype = GROMIT_ARROW_END; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_ARROWTYPE) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_STRING) - { - g_printerr ("Missing Arrowsize (string)... " - "aborting\n"); - goto cleanup; - } - if (! strcasecmp(scanner->value.v_string, "end")) - arrowtype = GROMIT_ARROW_END; - else if (! strcasecmp(scanner->value.v_string, "start")) - arrowtype = GROMIT_ARROW_START; - else if (! strcasecmp(scanner->value.v_string, "double")) - arrowtype = GROMIT_ARROW_DOUBLE; - else - { - g_printerr ("Arrow type must be \"start\", \"end\", or \"double\"... " - "aborting\n"); - goto cleanup; - } - } - else if ((intptr_t) scanner->value.v_symbol == SYM_MINSIZE) - { - gfloat v = parse_get_float(scanner, "Missing minsize (float)"); - if (isnan(v)) goto cleanup; - minwidth = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_MAXSIZE) - { - gfloat v = parse_get_float(scanner, "Missing maxsize (float)"); - if (isnan(v)) goto cleanup; - maxwidth = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS) - { - gfloat v = parse_get_float(scanner, "Missing radius (float)"); - if (isnan(v)) goto cleanup; - radius = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_MAXANGLE) - { - gfloat v = parse_get_float(scanner, "Missing angle (float)"); - if (isnan(v)) goto cleanup; - maxangle = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_SIMPLIFY) - { - gfloat v = parse_get_float(scanner, "Missing simplify value (float)"); - if (isnan(v)) goto cleanup; - simplify = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_MINLEN) - { - gfloat v = parse_get_float(scanner, "Missing minlen value (float)"); - if (isnan(v)) goto cleanup; - minlen = v; - } - else if ((intptr_t) scanner->value.v_symbol == SYM_SNAP) - { - gfloat v = parse_get_float(scanner, "Missing snap distance (float)"); - if (isnan(v)) goto cleanup; - snapdist = v; - } - else - { - g_printerr ("Unknown tool type?????\n"); - } - } - else - { - g_printerr ("skipped token!!!\n"); - } - token = g_scanner_get_next_token (scanner); - } - g_scanner_set_scope (scanner, 0); - token = g_scanner_get_next_token (scanner); + if (! parse_style(scanner, &style)) + goto cleanup; } - /* - * Finally we expect a semicolon - */ - + // finally we expect a semicolon + token = g_scanner_cur_token (scanner); if (token != ';') { g_printerr ("Expected \";\"\n"); goto cleanup; } - context = paint_context_new (data, type, fg_color, width, - arrowsize, arrowtype, - simplify, radius, maxangle, minlen, snapdist, - minwidth, maxwidth); + context = paint_context_new(data, style.type, style.paint_color, style.width, + style.arrowsize, style.arrow_type, + style.simplify, style.radius, style.maxangle, style.minlen, style.snapdist, + style.minwidth, style.maxwidth); g_hash_table_insert (data->tool_config, name, context); } else if (token == G_TOKEN_SYMBOL && diff --git a/src/config.h b/src/config.h index 52219f1..1e055ff 100644 --- a/src/config.h +++ b/src/config.h @@ -35,6 +35,30 @@ */ gboolean parse_config (GromitData *data); int parse_args (int argc, char **argv, GromitData *data); +gchar* parse_name (GScanner *scanner); + +typedef enum { + SYM_ERROR = 0, + SYM_SIZE = 1, + SYM_COLOR, + SYM_ARROWSIZE, + SYM_ARROWTYPE, + SYM_MINSIZE, + SYM_MAXSIZE, + SYM_MINLEN, + SYM_MAXANGLE, + SYM_RADIUS, + SYM_SIMPLIFY, + SYM_SNAP +} ToolAttribute; + +void scanner_init(GScanner *scanner); +gboolean parse_tool(GromitData *data, GScanner *scanner, + GromitPaintContext *style); +gboolean parse_style(GScanner *scanner, GromitPaintContext *style); +gfloat parse_get_float(GScanner *scanner, const gchar *msg); +ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style); + /* fallback hot key, if not specified on command line or in config file */ #ifndef DEFAULT_HOTKEY diff --git a/src/main.c b/src/main.c index de8c696..624babb 100644 --- a/src/main.c +++ b/src/main.c @@ -754,8 +754,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_selection_add_target (data->win, GA_CONTROL, GA_REDO, 9); gtk_selection_add_target (data->win, GA_CONTROL, GA_LINE, 10); - - + gtk_selection_add_target (data->win, GA_CONTROL, GA_CHGTOOL, 11); /* * Parse Config file @@ -1158,6 +1157,21 @@ int main_client (int argc, char **argv, GromitData *data) { action = GA_REDO; } + else if (strcmp (arg, "--deftool") == 0) + { + g_printerr("argc=%d i=%d\n",argc,i); + if (argc <= i+1) + { + wrong_arg = TRUE; + g_printerr("--deftool requires an argument\n"); + } + else + { + i++; + action = GA_CHGTOOL; + data->clientdata = argv[i]; + } + } else { g_printerr ("Unknown Option to control a running Gromit-MPX process: \"%s\"\n", arg); @@ -1240,7 +1254,7 @@ int main (int argc, char **argv) gtk_selection_owner_set (data->win, GA_DATA, GDK_CURRENT_TIME); gtk_selection_add_target (data->win, GA_DATA, GA_TOGGLEDATA, 1007); gtk_selection_add_target (data->win, GA_DATA, GA_LINEDATA, 1008); - + gtk_selection_add_target (data->win, GA_DATA, GA_CHGTOOLDATA, 1009); /* Try to get a status message. If there is a response gromit diff --git a/src/main.h b/src/main.h index 77ae382..df93da3 100644 --- a/src/main.h +++ b/src/main.h @@ -42,22 +42,25 @@ #define GROMIT_WINDOW_EVENTS ( GROMIT_MOUSE_EVENTS | GDK_EXPOSURE_MASK) /* Atoms used to control Gromit */ -#define GA_CONTROL gdk_atom_intern ("Gromit/control", FALSE) -#define GA_STATUS gdk_atom_intern ("Gromit/status", FALSE) -#define GA_QUIT gdk_atom_intern ("Gromit/quit", FALSE) -#define GA_ACTIVATE gdk_atom_intern ("Gromit/activate", FALSE) -#define GA_DEACTIVATE gdk_atom_intern ("Gromit/deactivate", FALSE) -#define GA_TOGGLE gdk_atom_intern ("Gromit/toggle", FALSE) -#define GA_LINE gdk_atom_intern ("Gromit/line", FALSE) -#define GA_VISIBILITY gdk_atom_intern ("Gromit/visibility", FALSE) -#define GA_CLEAR gdk_atom_intern ("Gromit/clear", FALSE) -#define GA_RELOAD gdk_atom_intern ("Gromit/reload", FALSE) -#define GA_UNDO gdk_atom_intern ("Gromit/undo", FALSE) -#define GA_REDO gdk_atom_intern ("Gromit/redo", FALSE) - -#define GA_DATA gdk_atom_intern ("Gromit/data", FALSE) -#define GA_TOGGLEDATA gdk_atom_intern ("Gromit/toggledata", FALSE) -#define GA_LINEDATA gdk_atom_intern ("Gromit/linedata", FALSE) +#define GA_CONTROL gdk_atom_intern ("Gromit/control", FALSE) +#define GA_STATUS gdk_atom_intern ("Gromit/status", FALSE) +#define GA_QUIT gdk_atom_intern ("Gromit/quit", FALSE) +#define GA_ACTIVATE gdk_atom_intern ("Gromit/activate", FALSE) +#define GA_DEACTIVATE gdk_atom_intern ("Gromit/deactivate", FALSE) +#define GA_TOGGLE gdk_atom_intern ("Gromit/toggle", FALSE) +#define GA_LINE gdk_atom_intern ("Gromit/line", FALSE) +#define GA_VISIBILITY gdk_atom_intern ("Gromit/visibility", FALSE) +#define GA_CLEAR gdk_atom_intern ("Gromit/clear", FALSE) +#define GA_RELOAD gdk_atom_intern ("Gromit/reload", FALSE) +#define GA_UNDO gdk_atom_intern ("Gromit/undo", FALSE) +#define GA_REDO gdk_atom_intern ("Gromit/redo", FALSE) +#define GA_CHGTOOL gdk_atom_intern ("Gromit/chgtool", FALSE) + +#define GA_DATA gdk_atom_intern ("Gromit/data", FALSE) +#define GA_TOGGLEDATA gdk_atom_intern ("Gromit/toggledata", FALSE) +#define GA_LINEDATA gdk_atom_intern ("Gromit/linedata", FALSE) +#define GA_CHGTOOLDATA gdk_atom_intern ("Gromit/chgtooldata", FALSE) + #define GROMIT_MAX_UNDO 100 diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..63527cc --- /dev/null +++ b/src/parser.c @@ -0,0 +1,139 @@ +/* + * Gromit-MPX -- a program for painting on the screen + * + * Gromit Copyright (C) 2000 Simon Budig + * + * Gromit-MPX Copyright (C) 2009,2010 Christian Beier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "parser.h" +#include "main.h" +#include "build-config.h" + +gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3; +gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; + +scanner_init(GScanner* scanner) +{ + scanner->config->case_sensitive = 0; + scanner->config->scan_octal = 0; + scanner->config->identifier_2_string = 0; + scanner->config->char_2_token = 1; + scanner->config->numbers_2_int = 1; + scanner->config->int_2_float = 1; + + g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); + g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); + g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR); + g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); + g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); + + g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4); + g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5); + g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11); + g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12); + g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13); + g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13); + + g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) 1); + g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) 2); + g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) 3); + g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) 4); + g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) 5); + + g_scanner_set_scope (scanner, 0); + scanner->config->scope_0_fallback = 0; +} + +gchar* parse_name (GScanner *scanner) +{ + GTokenType token; + + guint buttons = 0; + guint modifier = 0; + guint len = 0; + gchar *name; + + token = g_scanner_cur_token(scanner); + + if (token != G_TOKEN_STRING) + { + g_scanner_unexp_token (scanner, G_TOKEN_STRING, NULL, + NULL, NULL, "aborting", TRUE); + return NULL; + } + + len = strlen (scanner->value.v_string); + name = g_strndup (scanner->value.v_string, len + 3); + + token = g_scanner_get_next_token (scanner); + + /* + * Are there any options to limit the scope of the definition? + */ + + if (token == G_TOKEN_LEFT_BRACE) + { + g_scanner_set_scope (scanner, 1); + scanner->config->int_2_float = 0; + modifier = buttons = 0; + while ((token = g_scanner_get_next_token (scanner)) + != G_TOKEN_RIGHT_BRACE) + { + if (token == G_TOKEN_SYMBOL) + { + if ((intptr_t) scanner->value.v_symbol < 11) + buttons |= 1 << ((intptr_t) scanner->value.v_symbol - 1); + else + modifier |= 1 << ((intptr_t) scanner->value.v_symbol - 11); + } + else if (token == G_TOKEN_INT) + { + if (scanner->value.v_int <= 5 && scanner->value.v_int > 0) + buttons |= 1 << (scanner->value.v_int - 1); + else + g_printerr ("Only Buttons 1-5 are supported!\n"); + } + else + { + g_printerr ("skipped token\n"); + } + } + g_scanner_set_scope (scanner, 0); + scanner->config->int_2_float = 1; + token = g_scanner_get_next_token (scanner); + } + + name [len] = 124; + name [len+1] = buttons + 64; + name [len+2] = modifier + 48; + name [len+3] = 0; + + return name; +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..abcbb82 --- /dev/null +++ b/src/parser.h @@ -0,0 +1,37 @@ +/* + * Gromit-MPX -- a program for painting on the screen + * + * Gromit Copyright (C) 2000 Simon Budig + * + * Gromit-MPX Copyright (C) 2009,2010 Christian Beier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef PARSER_H +#define PARSER_H + +#include "main.h" + +extern gpointer HOTKEY_SYMBOL_VALUE; +extern gpointer UNDOKEY_SYMBOL_VALUE; + +void scanner_init(GScanner* scanner); +gchar* parse_name (GScanner *scanner); + + + +#endif // PARSER_H From 0c065106ab1a28a4456d5a6f39538e24cd6f2580 Mon Sep 17 00:00:00 2001 From: pascal-niklaus Date: Wed, 21 Feb 2024 22:51:39 +0100 Subject: [PATCH 2/7] duplicated tool parser code moved to separate functions --- src/callbacks.c | 188 ++++-------------------------------------------- src/config.c | 10 +-- src/parser.c | 153 +++++++++++++++++++++++++++++++++++++++ src/parser.h | 14 +++- 4 files changed, 183 insertions(+), 182 deletions(-) diff --git a/src/callbacks.c b/src/callbacks.c index bdd0a47..c136d77 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -693,9 +693,7 @@ void on_mainapp_selection_received (GtkWidget *widget, gchar *name, *copy; GromitPaintContext *context_template=NULL; - GromitPaintType type; - GdkRGBA *fg_color=NULL; - guint width, arrowsize, minwidth, maxwidth; + GromitStyleDef style; if (token != G_TOKEN_EOF) { @@ -706,182 +704,18 @@ void on_mainapp_selection_received (GtkWidget *widget, if(!name) goto cleanup; - token = g_scanner_cur_token(scanner); - - if (token != G_TOKEN_EQUAL_SIGN) - { - g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL, - NULL, NULL, "aborting", TRUE); - goto cleanup; - } + if (!parse_tool(data, scanner, &style)) + goto cleanup; - token = g_scanner_get_next_token (scanner); - - /* defaults */ - type = GROMIT_PEN; - width = 7; - arrowsize = 0; - minwidth = 1; - maxwidth = G_MAXUINT; - fg_color = data->red; + token = g_scanner_cur_token(scanner); - if (token == G_TOKEN_SYMBOL) - { - type = (GromitPaintType) scanner->value.v_symbol; - token = g_scanner_get_next_token (scanner); - } - else if (token == G_TOKEN_STRING) + if (token == G_TOKEN_LEFT_PAREN) { - copy = parse_name (scanner); - if(!copy) - goto cleanup; + if (! parse_style(scanner, &style)) + goto cleanup; token = g_scanner_cur_token(scanner); - context_template = g_hash_table_lookup (data->tool_config, copy); - if (context_template) - { - type = context_template->type; - width = context_template->width; - arrowsize = context_template->arrowsize; - minwidth = context_template->minwidth; - maxwidth = context_template->maxwidth; - fg_color = context_template->paint_color; - } - else - { - g_printerr ("WARNING: Unable to copy \"%s\": " - "not yet defined!\n", copy); - } - } - else - { - g_printerr ("Expected Tool-definition " - "or name of template tool\n"); - goto cleanup; } - /* Are there any tool-options? - */ - - if (token == G_TOKEN_LEFT_PAREN) - { - GdkRGBA *color = NULL; - g_scanner_set_scope (scanner, 2); - scanner->config->int_2_float = 1; - token = g_scanner_get_next_token (scanner); - while (token != G_TOKEN_RIGHT_PAREN) - { - if (token == G_TOKEN_SYMBOL) - { - if ((intptr_t) scanner->value.v_symbol == 1) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr ("Missing Size (float)... aborting\n"); - goto cleanup; - } - width = (guint) (scanner->value.v_float + 0.5); - } - else if ((intptr_t) scanner->value.v_symbol == 2) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_STRING) - { - g_printerr ("Missing Color (string)... " - "aborting\n"); - goto cleanup; - } - color = g_malloc (sizeof (GdkRGBA)); - if (gdk_rgba_parse (color, scanner->value.v_string)) - { - fg_color = color; - } - else - { - g_printerr ("Unable to parse color. " - "Keeping default.\n"); - g_free (color); - } - color = NULL; - } - else if ((intptr_t) scanner->value.v_symbol == 3) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr ("Missing Arrowsize (float)... " - "aborting\n"); - goto cleanup; - } - arrowsize = scanner->value.v_float; - } - else if ((intptr_t) scanner->value.v_symbol == 4) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr ("Missing Minsize (float)... " - "aborting\n"); - goto cleanup; - } - minwidth = scanner->value.v_float; - } - else if ((intptr_t) scanner->value.v_symbol == 5) - { - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr ("Missing \"=\"... aborting\n"); - goto cleanup; - } - token = g_scanner_get_next_token (scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr ("Missing Maxsize (float)... " - "aborting\n"); - goto cleanup; - } - maxwidth = scanner->value.v_float; - } - else - { - g_printerr ("Unknown tool type ???\n"); - } - } - else - { - g_printerr("skipped unknown token: %d !!!\n", token); - } - token = g_scanner_get_next_token (scanner); - } // while (token != G_TOKEN_RIGHT_PAREN) - g_scanner_set_scope (scanner, 0); - token = g_scanner_get_next_token (scanner); - } // if (token == G_TOKEN_LEFT_PAREN) - // by now nothing should follow if (token != G_TOKEN_EOF) { @@ -892,14 +726,16 @@ void on_mainapp_selection_received (GtkWidget *widget, GromitPaintContext *context = g_hash_table_lookup(data->tool_config, name); GromitPaintContext *new_context = - paint_context_new (data, type, fg_color, width, arrowsize, minwidth, maxwidth); + paint_context_new (data, style.type, style.paint_color, + style.width, style.arrowsize, + style.minwidth, style.maxwidth); *context = *new_context; g_free(new_context); - } // if (token == G_TOKEN_STRING) + } token = g_scanner_get_next_token (scanner); - } // if (token != G_TOKEN_EOF) + } cleanup: diff --git a/src/config.c b/src/config.c index 9acdc1a..1db7abd 100644 --- a/src/config.c +++ b/src/config.c @@ -495,13 +495,13 @@ gboolean parse_config (GromitData *data) goto cleanup; if (!parse_tool(data, scanner, &style)) - { - g_printerr("parse tool failed\n"); - goto cleanup; - } + goto cleanup; - // are there any tool-options? token = g_scanner_cur_token(scanner); + // + /* Are there any tool-options? + */ + if (token == G_TOKEN_LEFT_PAREN) { if (! parse_style(scanner, &style)) diff --git a/src/parser.c b/src/parser.c index 63527cc..22c5b79 100644 --- a/src/parser.c +++ b/src/parser.c @@ -137,3 +137,156 @@ gchar* parse_name (GScanner *scanner) return name; } + + +gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style) { + GTokenType token = g_scanner_cur_token(scanner); + + if (token != G_TOKEN_EQUAL_SIGN) { + g_scanner_unexp_token( + scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE); + return FALSE; + } + + token = g_scanner_get_next_token(scanner); + + /* defaults */ + style->type = GROMIT_PEN; + style->width = 7; + style->arrowsize = 0; + style->minwidth = 1; + style->maxwidth = G_MAXUINT; + style->paint_color = data->red; + + if (token == G_TOKEN_SYMBOL) { + style->type = (GromitPaintType)scanner->value.v_symbol; + token = g_scanner_get_next_token(scanner); + } else if (token == G_TOKEN_STRING) { + gchar *copy = parse_name(scanner); + if (!copy) + return FALSE; + token = g_scanner_cur_token(scanner); + GromitPaintContext *context = g_hash_table_lookup(data->tool_config, copy); + if (context) { + style->type = context->type; + style->width = context->width; + style->arrowsize = context->arrowsize; + style->minwidth = context->minwidth; + style->maxwidth = context->maxwidth; + style->paint_color = context->paint_color; + } else { + g_printerr( + "WARNING: Unable to copy \"%s\": " + "not yet defined!\n", + copy); + } + } else { + g_printerr( + "Expected Tool-definition " + "or name of template tool\n"); + return FALSE; + } + return TRUE; +} + +/* + * parses a pen style definition and stores fields found in GromitStyleDef + * returns FALSE upon any error + */ + +gboolean parse_style(GScanner *scanner, GromitStyleDef *style) { + GdkRGBA *color = NULL; + g_scanner_set_scope(scanner, 2); + scanner->config->int_2_float = 1; + GTokenType token = g_scanner_get_next_token(scanner); + while (token != G_TOKEN_RIGHT_PAREN) { + if (token == G_TOKEN_SYMBOL) { + if ((intptr_t)scanner->value.v_symbol == 1) { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) { + g_printerr("Missing Size (float)... aborting\n"); + return FALSE; + } + style->width = (guint)(scanner->value.v_float + 0.5); + } else if ((intptr_t)scanner->value.v_symbol == 2) { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_STRING) { + g_printerr( + "Missing Color (string)... " + "aborting\n"); + return FALSE; + } + color = g_malloc(sizeof(GdkRGBA)); + if (gdk_rgba_parse(color, scanner->value.v_string)) { + style->paint_color = color; + } else { + g_printerr( + "Unable to parse color. " + "Keeping default.\n"); + g_free(color); + } + color = NULL; + } else if ((intptr_t)scanner->value.v_symbol == 3) { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) { + g_printerr( + "Missing Arrowsize (float)... " + "aborting\n"); + return FALSE; + } + style->arrowsize = scanner->value.v_float; + } else if ((intptr_t)scanner->value.v_symbol == 4) { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) { + g_printerr( + "Missing Minsize (float)... " + "aborting\n"); + return FALSE; + } + style->minwidth = scanner->value.v_float; + } else if ((intptr_t)scanner->value.v_symbol == 5) { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) { + g_printerr( + "Missing Maxsize (float)... " + "aborting\n"); + return FALSE; + } + style->maxwidth = scanner->value.v_float; + } else { + g_printerr("Unknown tool type ???\n"); + } + } else { + g_printerr("skipped unknown token: %d !!!\n", token); + } + token = g_scanner_get_next_token(scanner); + } // while (token != G_TOKEN_RIGHT_PAREN) + g_scanner_set_scope(scanner, 0); + g_scanner_get_next_token(scanner); + return TRUE; +} diff --git a/src/parser.h b/src/parser.h index abcbb82..7541053 100644 --- a/src/parser.h +++ b/src/parser.h @@ -26,12 +26,24 @@ #include "main.h" + +typedef struct +{ + GromitPaintType type; + guint width; + gfloat arrowsize; + guint minwidth; + guint maxwidth; + GdkRGBA *paint_color; +} GromitStyleDef; + extern gpointer HOTKEY_SYMBOL_VALUE; extern gpointer UNDOKEY_SYMBOL_VALUE; void scanner_init(GScanner* scanner); gchar* parse_name (GScanner *scanner); - +gboolean parse_style(GScanner *scanner, GromitStyleDef *style); +gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style); #endif // PARSER_H From f446416dcbd27fd04544d25e104428b16a727de6 Mon Sep 17 00:00:00 2001 From: pascal-niklaus Date: Thu, 22 Feb 2024 10:27:40 +0100 Subject: [PATCH 3/7] tool change --- gromit-mpx.1 | 7 ++ src/callbacks.c | 15 +-- src/config.c | 3 +- src/main.c | 9 +- src/parser.c | 314 ++++++++++++++++++++++++++++-------------------- src/parser.h | 3 +- 6 files changed, 204 insertions(+), 147 deletions(-) diff --git a/gromit-mpx.1 b/gromit-mpx.1 index dc48576..c0462b5 100644 --- a/gromit-mpx.1 +++ b/gromit-mpx.1 @@ -95,12 +95,19 @@ running Gromit-MPX process, see above for the options available to start Gromit- .B \-c, \-\-clear will clear the screen. .TP +.B \-l , \-\-line +will draw a line from (x1,y1) to (x2,y2) with color and width . +.TP .B \-q, \-\-quit will cause the main Gromit-MPX process to quit. .TP .B \-t, \-\-toggle will toggle the grabbing of the cursor. .TP +.B \-T , \-\-change-tool +will change the definition of a tool. The syntax is as in the .cfg file +read at startup, except for the trailing semicolon. +.TP .B \-v, \-\-visibility will toggle the visibility of the window. .TP diff --git a/src/callbacks.c b/src/callbacks.c index c136d77..1ce0a82 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -680,7 +680,8 @@ void on_mainapp_selection_received (GtkWidget *widget, else if (gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA) { gchar *a = (gchar *)gtk_selection_data_get_data(selection_data); - g_printerr("DEFTOOL argument: %s\n", a); + if(data->debug) + g_printerr("DEBUG: define tool: %s\n", a); GScanner *scanner; scanner = g_scanner_new(NULL); @@ -690,8 +691,7 @@ void on_mainapp_selection_received (GtkWidget *widget, GTokenType token; token = g_scanner_get_next_token (scanner); - gchar *name, *copy; - GromitPaintContext *context_template=NULL; + gchar *name; GromitStyleDef style; @@ -702,7 +702,7 @@ void on_mainapp_selection_received (GtkWidget *widget, name = parse_name (scanner); if(!name) - goto cleanup; + goto cleanup; if (!parse_tool(data, scanner, &style)) goto cleanup; @@ -716,7 +716,6 @@ void on_mainapp_selection_received (GtkWidget *widget, token = g_scanner_cur_token(scanner); } - // by now nothing should follow if (token != G_TOKEN_EOF) { g_printerr ("End of tool definition expected !\n"); @@ -729,20 +728,18 @@ void on_mainapp_selection_received (GtkWidget *widget, paint_context_new (data, style.type, style.paint_color, style.width, style.arrowsize, style.minwidth, style.maxwidth); - *context = *new_context; + g_free(context->paint_color); + *context = *new_context; g_free(new_context); } - token = g_scanner_get_next_token (scanner); } cleanup: - g_scanner_destroy (scanner); } } - gtk_main_quit (); } diff --git a/src/config.c b/src/config.c index 1db7abd..4bd3447 100644 --- a/src/config.c +++ b/src/config.c @@ -730,7 +730,6 @@ void read_keyfile(GromitData *data) { gchar *filename = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.ini", NULL); - /* set defaults */ @@ -756,7 +755,7 @@ void read_keyfile(GromitData *data) if(data->opacity == 0) data->opacity = DEFAULT_OPACITY; - cleanup: + cleanup: g_free(filename); g_key_file_free(key_file); } diff --git a/src/main.c b/src/main.c index 624babb..2cf111c 100644 --- a/src/main.c +++ b/src/main.c @@ -599,8 +599,6 @@ void main_do_event (GdkEventAny *event, - - void setup_main_app (GromitData *data, int argc, char ** argv) { gboolean activate; @@ -1060,7 +1058,6 @@ void parse_print_help (gpointer key, gpointer value, gpointer user_data) } - /* * Main programs */ @@ -1157,13 +1154,13 @@ int main_client (int argc, char **argv, GromitData *data) { action = GA_REDO; } - else if (strcmp (arg, "--deftool") == 0) + else if (strcmp (arg, "--change-tool") == 0 || + strcmp(arg, "-T") == 0) { - g_printerr("argc=%d i=%d\n",argc,i); if (argc <= i+1) { wrong_arg = TRUE; - g_printerr("--deftool requires an argument\n"); + g_printerr("--change-tool requires an argument\n"); } else { diff --git a/src/parser.c b/src/parser.c index 22c5b79..77196e2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -36,7 +36,11 @@ gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3; gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; -scanner_init(GScanner* scanner) +/* + * set scanner to default initial values + */ + +void scanner_init(GScanner* scanner) { scanner->config->case_sensitive = 0; scanner->config->scan_octal = 0; @@ -71,6 +75,11 @@ scanner_init(GScanner* scanner) scanner->config->scope_0_fallback = 0; } +/* + * get name of tool, e.g. "default"[SHIFT], which is returned as "defaukt|@1" + * returns NULL upon error + */ + gchar* parse_name (GScanner *scanner) { GTokenType token; @@ -94,10 +103,6 @@ gchar* parse_name (GScanner *scanner) token = g_scanner_get_next_token (scanner); - /* - * Are there any options to limit the scope of the definition? - */ - if (token == G_TOKEN_LEFT_BRACE) { g_scanner_set_scope (scanner, 1); @@ -138,155 +143,208 @@ gchar* parse_name (GScanner *scanner) return name; } +/* + * get the "type" of the tool, e.g. PEN (e.g. =PEN), or a base tool + * style that is inherited (e.g. ="red pen") and store characteristics + * in style. + * + * returns FALSE upon error + */ -gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style) { - GTokenType token = g_scanner_cur_token(scanner); +gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style) +{ + GTokenType token = g_scanner_cur_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_scanner_unexp_token( - scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE); - return FALSE; + if (token != G_TOKEN_EQUAL_SIGN) + { + g_scanner_unexp_token( + scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE); + return FALSE; } - token = g_scanner_get_next_token(scanner); + token = g_scanner_get_next_token(scanner); - /* defaults */ - style->type = GROMIT_PEN; - style->width = 7; - style->arrowsize = 0; - style->minwidth = 1; - style->maxwidth = G_MAXUINT; - style->paint_color = data->red; + /* defaults */ + style->type = GROMIT_PEN; + style->width = 7; + style->arrowsize = 0; + style->minwidth = 1; + style->maxwidth = G_MAXUINT; - if (token == G_TOKEN_SYMBOL) { - style->type = (GromitPaintType)scanner->value.v_symbol; - token = g_scanner_get_next_token(scanner); - } else if (token == G_TOKEN_STRING) { - gchar *copy = parse_name(scanner); - if (!copy) - return FALSE; - token = g_scanner_cur_token(scanner); - GromitPaintContext *context = g_hash_table_lookup(data->tool_config, copy); - if (context) { - style->type = context->type; - style->width = context->width; - style->arrowsize = context->arrowsize; - style->minwidth = context->minwidth; - style->maxwidth = context->maxwidth; - style->paint_color = context->paint_color; - } else { - g_printerr( - "WARNING: Unable to copy \"%s\": " - "not yet defined!\n", - copy); - } - } else { - g_printerr( - "Expected Tool-definition " - "or name of template tool\n"); + GdkRGBA *red_color = g_malloc(sizeof (GdkRGBA)); + *red_color = *data->red; + style->paint_color = red_color; + + if (token == G_TOKEN_SYMBOL) + { + style->type = (GromitPaintType)scanner->value.v_symbol; + token = g_scanner_get_next_token(scanner); + } + else if (token == G_TOKEN_STRING) + { + gchar *copy = parse_name(scanner); + if (!copy) return FALSE; + token = g_scanner_cur_token(scanner); + GromitPaintContext *context = g_hash_table_lookup(data->tool_config, copy); + if (context) + { + style->type = context->type; + style->width = context->width; + style->arrowsize = context->arrowsize; + style->minwidth = context->minwidth; + style->maxwidth = context->maxwidth; + style->paint_color = context->paint_color; + } + else + { + g_printerr( + "WARNING: Unable to copy \"%s\": " + "not yet defined!\n", + copy); + } } - return TRUE; + else + { + g_printerr( + "Expected Tool-definition " + "or name of template tool\n"); + return FALSE; + } + return TRUE; } /* - * parses a pen style definition and stores fields found in GromitStyleDef + * parses a pen style definition (e.g. (color="red" size=3) ) + * and stores fields found in GromitStyleDef. + * * returns FALSE upon any error */ -gboolean parse_style(GScanner *scanner, GromitStyleDef *style) { - GdkRGBA *color = NULL; - g_scanner_set_scope(scanner, 2); - scanner->config->int_2_float = 1; - GTokenType token = g_scanner_get_next_token(scanner); - while (token != G_TOKEN_RIGHT_PAREN) { - if (token == G_TOKEN_SYMBOL) { - if ((intptr_t)scanner->value.v_symbol == 1) { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; +gboolean parse_style(GScanner *scanner, GromitStyleDef *style) +{ + GdkRGBA *color = NULL; + g_scanner_set_scope(scanner, 2); + scanner->config->int_2_float = 1; + GTokenType token = g_scanner_get_next_token(scanner); + while (token != G_TOKEN_RIGHT_PAREN) + { + if (token == G_TOKEN_SYMBOL) + { + if ((intptr_t)scanner->value.v_symbol == 1) + { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) { - g_printerr("Missing Size (float)... aborting\n"); - return FALSE; + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr("Missing Size (float)... aborting\n"); + return FALSE; } - style->width = (guint)(scanner->value.v_float + 0.5); - } else if ((intptr_t)scanner->value.v_symbol == 2) { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; + style->width = (guint)(scanner->value.v_float + 0.5); + } + else if ((intptr_t)scanner->value.v_symbol == 2) + { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_STRING) { - g_printerr( - "Missing Color (string)... " - "aborting\n"); - return FALSE; + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_STRING) + { + g_printerr( + "Missing Color (string)... " + "aborting\n"); + return FALSE; } - color = g_malloc(sizeof(GdkRGBA)); - if (gdk_rgba_parse(color, scanner->value.v_string)) { - style->paint_color = color; - } else { - g_printerr( - "Unable to parse color. " - "Keeping default.\n"); - g_free(color); + color = g_malloc(sizeof(GdkRGBA)); + if (gdk_rgba_parse(color, scanner->value.v_string)) + { + style->paint_color = color; } - color = NULL; - } else if ((intptr_t)scanner->value.v_symbol == 3) { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; + else + { + g_printerr( + "Unable to parse color. " + "Keeping default.\n"); + g_free(color); } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) { - g_printerr( - "Missing Arrowsize (float)... " - "aborting\n"); - return FALSE; + color = NULL; + } + else if ((intptr_t)scanner->value.v_symbol == 3) + { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; } - style->arrowsize = scanner->value.v_float; - } else if ((intptr_t)scanner->value.v_symbol == 4) { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr( + "Missing Arrowsize (float)... " + "aborting\n"); + return FALSE; } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) { - g_printerr( - "Missing Minsize (float)... " - "aborting\n"); - return FALSE; + style->arrowsize = scanner->value.v_float; + } + else if ((intptr_t)scanner->value.v_symbol == 4) + { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr( + "Missing Minsize (float)... " + "aborting\n"); + return FALSE; } - style->minwidth = scanner->value.v_float; - } else if ((intptr_t)scanner->value.v_symbol == 5) { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; + style->minwidth = scanner->value.v_float; + } + else if ((intptr_t)scanner->value.v_symbol == 5) + { + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) + { + g_printerr("Missing \"=\"... aborting\n"); + return FALSE; } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) { - g_printerr( - "Missing Maxsize (float)... " - "aborting\n"); - return FALSE; + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) + { + g_printerr( + "Missing Maxsize (float)... " + "aborting\n"); + return FALSE; } - style->maxwidth = scanner->value.v_float; - } else { - g_printerr("Unknown tool type ???\n"); + style->maxwidth = scanner->value.v_float; } - } else { - g_printerr("skipped unknown token: %d !!!\n", token); + else + { + g_printerr("Unknown tool type ! \n"); + return FALSE; + } + } + else + { + g_printerr("Unknown token: %d !\n", token); + return FALSE; } - token = g_scanner_get_next_token(scanner); + token = g_scanner_get_next_token(scanner); } // while (token != G_TOKEN_RIGHT_PAREN) - g_scanner_set_scope(scanner, 0); - g_scanner_get_next_token(scanner); - return TRUE; + g_scanner_set_scope(scanner, 0); + g_scanner_get_next_token(scanner); + return TRUE; } diff --git a/src/parser.h b/src/parser.h index 7541053..1ff684d 100644 --- a/src/parser.h +++ b/src/parser.h @@ -42,8 +42,7 @@ extern gpointer UNDOKEY_SYMBOL_VALUE; void scanner_init(GScanner* scanner); gchar* parse_name (GScanner *scanner); -gboolean parse_style(GScanner *scanner, GromitStyleDef *style); gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style); - +gboolean parse_style(GScanner *scanner, GromitStyleDef *style); #endif // PARSER_H From e1ceeec75ae8ef70b6cfa63c94c3561737feb162 Mon Sep 17 00:00:00 2001 From: pascal-niklaus Date: Thu, 22 Feb 2024 14:45:47 +0100 Subject: [PATCH 4/7] added cairo_destroy --- src/callbacks.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/callbacks.c b/src/callbacks.c index 1ce0a82..a852ce4 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include "cairo.h" #include "main.h" #include "input.h" #include "callbacks.h" @@ -162,7 +162,6 @@ void on_monitors_changed ( GdkScreen *screen, } - void on_composited_changed ( GdkScreen *screen, gpointer user_data) { @@ -197,7 +196,6 @@ void on_composited_changed ( GdkScreen *screen, } - void on_clientapp_selection_get (GtkWidget *widget, GtkSelectionData *selection_data, guint info, @@ -569,7 +567,6 @@ void on_mainapp_selection_get (GtkWidget *widget, else uri = "NOK"; - gtk_selection_data_set (selection_data, gtk_selection_data_get_target(selection_data), 8, (guchar*)uri, strlen (uri)); @@ -729,6 +726,7 @@ void on_mainapp_selection_received (GtkWidget *widget, style.width, style.arrowsize, style.minwidth, style.maxwidth); + cairo_destroy(context->paint_ctx); g_free(context->paint_color); *context = *new_context; g_free(new_context); @@ -777,7 +775,6 @@ void on_device_added (GdkDeviceManager *device_manager, } - gboolean on_toggle_paint(GtkWidget *widget, GdkEventButton *ev, gpointer user_data) From 074531a59cb96f56cdb710c55de8049a82a4adc1 Mon Sep 17 00:00:00 2001 From: pascal Date: Tue, 19 Mar 2024 14:28:24 +0100 Subject: [PATCH 5/7] config, callbacks: add option to change tool definitions and individual tool attributes --- src/callbacks.c | 230 ++++++++++++++++++++++--------- src/callbacks.h | 3 + src/config.c | 140 +++++++++++++++---- src/config.h | 19 +++ src/drawing.c | 118 ++++++++++++++++ src/drawing.h | 15 +++ src/main.c | 81 +++++++++-- src/main.h | 46 ++++--- src/parser.c | 350 ------------------------------------------------ src/parser.h | 48 ------- 10 files changed, 532 insertions(+), 518 deletions(-) delete mode 100644 src/parser.c delete mode 100644 src/parser.h diff --git a/src/callbacks.c b/src/callbacks.c index a852ce4..73d3da1 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -24,12 +24,12 @@ #include #include #include +#include #include "cairo.h" #include "main.h" #include "input.h" #include "callbacks.h" #include "config.h" -#include "parser.h" #include "drawing.h" #include "build-config.h" #include "coordlist_ops.h" @@ -143,10 +143,17 @@ void on_monitors_changed ( GdkScreen *screen, parse_config(data); // also calls paint_context_new() :-( +<<<<<<< HEAD data->default_pen = paint_context_new (data, GROMIT_PEN, data->red, 7, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 1, 0, G_MAXUINT); data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 1, 0, G_MAXUINT); +======= + data->default_pen = paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, + 0, GROMIT_ARROW_END, 1, G_MAXUINT); + data->default_eraser = paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, + 0, GROMIT_ARROW_END, 1, G_MAXUINT); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if(!data->composited) // set shape { @@ -162,6 +169,7 @@ void on_monitors_changed ( GdkScreen *screen, } + void on_composited_changed ( GdkScreen *screen, gpointer user_data) { @@ -196,6 +204,7 @@ void on_composited_changed ( GdkScreen *screen, } + void on_clientapp_selection_get (GtkWidget *widget, GtkSelectionData *selection_data, guint info, @@ -212,7 +221,8 @@ void on_clientapp_selection_get (GtkWidget *widget, if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA || gtk_selection_data_get_target(selection_data) == GA_LINEDATA || - gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA) + gtk_selection_data_get_target(selection_data) == GA_DEFTOOLDATA || + gtk_selection_data_get_target(selection_data) == GA_CHGATTRDATA) { ans = data->clientdata; } @@ -282,7 +292,11 @@ gboolean on_buttonpress (GtkWidget *win, GromitPaintType type = devdata->cur_context->type; // store original state to have dynamic update of line and rect +<<<<<<< HEAD if (type == GROMIT_LINE || type == GROMIT_RECT || type == GROMIT_SMOOTH || type == GROMIT_ORTHOGONAL) +======= + if (type == GROMIT_LINE || type == GROMIT_RECT) +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) { copy_surface(data->aux_backbuffer, data->backbuffer); } @@ -399,7 +413,10 @@ gboolean on_motion (GtkWidget *win, } if (type == GROMIT_LINE) { +<<<<<<< HEAD GromitArrowType atype = devdata->cur_context->arrow_type; +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y); if (devdata->cur_context->arrowsize > 0) { @@ -450,8 +467,14 @@ gboolean on_buttonrelease (GtkWidget *win, gfloat direction = 0; gint width = 0; +<<<<<<< HEAD if(ctx) width = ctx->arrowsize * ctx->width / 2; +======= + if(devdata->cur_context) + width = devdata->cur_context->arrowsize * devdata->cur_context->width / 2; + +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if ((ev->x != devdata->lastx) || (ev->y != devdata->lasty)) @@ -460,6 +483,7 @@ gboolean on_buttonrelease (GtkWidget *win, if (!devdata->is_grabbed) return FALSE; +<<<<<<< HEAD GromitPaintType type = ctx->type; if (type == GROMIT_SMOOTH || type == GROMIT_ORTHOGONAL) @@ -493,6 +517,13 @@ gboolean on_buttonrelease (GtkWidget *win, if (ctx->arrowsize != 0) { GromitArrowType atype = ctx->arrow_type; +======= + GromitPaintType type = devdata->cur_context->type; + + if (devdata->cur_context->arrowsize != 0) + { + GromitArrowType atype = devdata->cur_context->arrow_type; +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if (type == GROMIT_LINE) { direction = atan2 (ev->y - devdata->lasty, ev->x - devdata->lastx); @@ -529,7 +560,7 @@ void on_mainapp_selection_get (GtkWidget *widget, gpointer user_data) { GromitData *data = (GromitData *) user_data; - + gchar *uri = "OK"; GdkAtom action = gtk_selection_data_get_target(selection_data); @@ -559,14 +590,20 @@ void on_mainapp_selection_get (GtkWidget *widget, undo_drawing (data); else if (action == GA_REDO) redo_drawing (data); - else if (action == GA_CHGTOOL) + else if (action == GA_DEFTOOL) { - gtk_selection_convert(data->win, GA_DATA, GA_CHGTOOLDATA, time); + gtk_selection_convert(data->win, GA_DATA, GA_DEFTOOLDATA, time); + gtk_main(); + } + else if (action == GA_CHGATTR) + { + gtk_selection_convert(data->win, GA_DATA, GA_CHGATTRDATA, time); gtk_main(); } else uri = "NOK"; + gtk_selection_data_set (selection_data, gtk_selection_data_get_target(selection_data), 8, (guchar*)uri, strlen (uri)); @@ -579,6 +616,7 @@ void on_mainapp_selection_received (GtkWidget *widget, gpointer user_data) { GromitData *data = (GromitData *) user_data; + gchar *name = NULL; if(gtk_selection_data_get_length(selection_data) < 0) { @@ -590,20 +628,20 @@ void on_mainapp_selection_received (GtkWidget *widget, if(gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA ) { intptr_t dev_nr = strtoull((gchar*)gtk_selection_data_get_data(selection_data), NULL, 10); - + if(data->debug) g_printerr("DEBUG: mainapp got toggle id '%ld' back from client.\n", (long)dev_nr); if(dev_nr < 0) toggle_grab(data, NULL); /* toggle all */ - else + else { /* find dev numbered dev_nr */ GHashTableIter it; gpointer value; - GromitDeviceData* devdata = NULL; + GromitDeviceData* devdata = NULL; g_hash_table_iter_init (&it, data->devdatatable); - while (g_hash_table_iter_next (&it, NULL, &value)) + while (g_hash_table_iter_next (&it, NULL, &value)) { devdata = value; if(devdata->index == dev_nr) @@ -611,14 +649,14 @@ void on_mainapp_selection_received (GtkWidget *widget, else devdata = NULL; } - + if(devdata) toggle_grab(data, devdata->device); else g_printerr("ERROR: No device at index %ld.\n", (long)dev_nr); } } - else if (gtk_selection_data_get_target(selection_data) == GA_LINEDATA) + else if (gtk_selection_data_get_target(selection_data) == GA_LINEDATA) { gchar** line_args = g_strsplit((gchar*)gtk_selection_data_get_data(selection_data), " ", 6); @@ -629,7 +667,7 @@ void on_mainapp_selection_received (GtkWidget *widget, gchar* hex_code = line_args[4]; int thickness = atoi(line_args[5]); - if(data->debug) + if(data->debug) { g_printerr("DEBUG: mainapp got line data back from client:\n"); g_printerr("startX startY endX endY: %d %d %d %d\n", startX, startY, endX, endY); @@ -639,7 +677,6 @@ void on_mainapp_selection_received (GtkWidget *widget, GdkRGBA* color = g_malloc (sizeof (GdkRGBA)); GdkRGBA *fg_color=data->red; - if (gdk_rgba_parse (color, hex_code)) { fg_color = color; @@ -650,8 +687,13 @@ void on_mainapp_selection_received (GtkWidget *widget, "Keeping default.\n"); } GromitPaintContext* line_ctx = +<<<<<<< HEAD paint_context_new(data, GROMIT_PEN, fg_color, thickness, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, thickness, thickness); +======= + paint_context_new(data, GROMIT_PEN, fg_color, thickness, + 0, GROMIT_ARROW_END, thickness, thickness); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GdkRectangle rect; rect.x = MIN (startX,endX) - thickness / 2; @@ -668,76 +710,136 @@ void on_mainapp_selection_received (GtkWidget *widget, cairo_stroke(line_ctx->paint_ctx); data->modified = 1; - gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0); + gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0); data->painted = 1; g_free(line_ctx); g_free (color); } - else if (gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA) + else { - gchar *a = (gchar *)gtk_selection_data_get_data(selection_data); - if(data->debug) - g_printerr("DEBUG: define tool: %s\n", a); - - GScanner *scanner; - scanner = g_scanner_new(NULL); - scanner_init(scanner); - g_scanner_input_text(scanner, a, strlen(a)); + GdkAtom atom = gtk_selection_data_get_target(selection_data); + if (atom == GA_DEFTOOLDATA || atom == GA_CHGATTRDATA) + { + gchar *a = (gchar *)gtk_selection_data_get_data(selection_data); + if(data->debug) g_printerr("DEBUG: define tool: %s\n", a); - GTokenType token; - token = g_scanner_get_next_token (scanner); + GScanner *scanner = g_scanner_new(NULL); + scanner_init(scanner); + g_scanner_input_text(scanner, a, strlen(a)); - gchar *name; + GTokenType token = g_scanner_get_next_token (scanner); - GromitStyleDef style; + GromitPaintContext style; + style.paint_color = NULL; - if (token != G_TOKEN_EOF) - { - if (token == G_TOKEN_STRING) + if (token != G_TOKEN_STRING || ! (name = parse_name (scanner))) { - name = parse_name (scanner); - - if(!name) - goto cleanup; - - if (!parse_tool(data, scanner, &style)) - goto cleanup; + g_printerr("Could not parse tool in expression '%s'!\n", a); + goto cleanup; + } - token = g_scanner_cur_token(scanner); + GromitPaintContext *context = g_hash_table_lookup(data->tool_config, name); + if (context == NULL) + { + g_printerr("Cannot change tool '%s' because it does not exist...\n", name); + goto cleanup; + } - if (token == G_TOKEN_LEFT_PAREN) + if (atom == GA_CHGATTRDATA) + { + if (g_scanner_cur_token(scanner) != G_TOKEN_EQUAL_SIGN) { - if (! parse_style(scanner, &style)) - goto cleanup; - token = g_scanner_cur_token(scanner); + g_printerr("Expected equal sign after tool name...\n"); + goto cleanup; } - - if (token != G_TOKEN_EOF) + token = g_scanner_get_next_token(scanner); + if (token == G_TOKEN_SYMBOL) { - g_printerr ("End of tool definition expected !\n"); - goto cleanup; + context->type = (GromitPaintType) scanner->value.v_symbol; + token = g_scanner_get_next_token (scanner); + } + if (token == G_TOKEN_LEFT_PAREN) + { + style.paint_color = g_malloc(sizeof(GdkRGBA)); + *style.paint_color = *data->red; + scanner->scope_id = 2; + while (token != G_TOKEN_RIGHT_PAREN) + { + token = g_scanner_get_next_token(scanner); + if (token == G_TOKEN_SYMBOL) + { + switch (parse_attribute(scanner, &style)) + { + case SYM_SIZE: + context->width = style.width; + cairo_set_line_width(context->paint_ctx, style.width); + break; + case SYM_COLOR: + *context->paint_color = *style.paint_color; + cairo_set_source_rgba(context->paint_ctx, + style.paint_color->red, + style.paint_color->green, + style.paint_color->blue, + style.paint_color->alpha); + break; + case SYM_ARROWSIZE: + context->arrowsize = style.arrowsize; + break; + case SYM_ARROWTYPE: + context->arrow_type = style.arrow_type; + break; + case SYM_MINSIZE: + context->minwidth = style.minwidth; + break; + case SYM_MAXSIZE: + context->maxwidth = style.maxwidth; + break; + case SYM_ERROR: + break; + } + } + } + if (g_scanner_get_next_token(scanner) != G_TOKEN_EOF) + g_printerr("WARNING: skipping extra content after ')' !\n"); } - - GromitPaintContext *context = - g_hash_table_lookup(data->tool_config, name); - GromitPaintContext *new_context = - paint_context_new (data, style.type, style.paint_color, - style.width, style.arrowsize, - style.minwidth, style.maxwidth); - - cairo_destroy(context->paint_ctx); - g_free(context->paint_color); - *context = *new_context; - g_free(new_context); } - token = g_scanner_get_next_token (scanner); - } + else + { + if (!parse_tool(data, scanner, &style)) goto cleanup; + if (g_scanner_cur_token(scanner) != G_TOKEN_LEFT_PAREN) goto cleanup; + if (! parse_style(scanner, &style)) goto cleanup; + if (g_scanner_cur_token(scanner) != G_TOKEN_EOF) goto cleanup; + + context->type = style.type; + *context->paint_color = *style.paint_color; + context->width = style.width; + context->arrowsize = style.arrowsize; + context->arrow_type = style.arrow_type; + context->minwidth = style.minwidth; + context->maxwidth = style.maxwidth; + + cairo_set_source_rgba(context->paint_ctx, + style.paint_color->red, + style.paint_color->green, + style.paint_color->blue, + style.paint_color->alpha); + cairo_set_line_width(context->paint_ctx, style.width); + } + if (g_scanner_get_next_token (scanner) != G_TOKEN_EOF) + { + g_printerr("WARNING: skipping extra content tool definition!\n"); + } - cleanup: - g_scanner_destroy (scanner); + cleanup: + if (style.paint_color) g_free(style.paint_color); + g_scanner_destroy (scanner); + } // if (tool change) ... } } + + if (name) g_free(name); + gtk_main_quit (); } @@ -775,6 +877,7 @@ void on_device_added (GdkDeviceManager *device_manager, } + gboolean on_toggle_paint(GtkWidget *widget, GdkEventButton *ev, gpointer user_data) @@ -997,6 +1100,7 @@ void on_intro(GtkMenuItem *menuitem, gtk_widget_show_all (assistant); } +<<<<<<< HEAD void on_edit_config(GtkMenuItem *menuitem, gpointer user_data) { @@ -1051,6 +1155,8 @@ void on_edit_config(GtkMenuItem *menuitem, } +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void on_issues(GtkMenuItem *menuitem, gpointer user_data) { diff --git a/src/callbacks.h b/src/callbacks.h index 7bcbdd8..ce7e88b 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -128,9 +128,12 @@ void on_about(GtkMenuItem *menuitem, void on_intro(GtkMenuItem *menuitem, gpointer user_data); +<<<<<<< HEAD void on_edit_config(GtkMenuItem *menuitem, gpointer user_data); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void on_issues(GtkMenuItem *menuitem, gpointer user_data); diff --git a/src/config.c b/src/config.c index 4bd3447..3693a04 100644 --- a/src/config.c +++ b/src/config.c @@ -29,7 +29,7 @@ #include #include "config.h" -#include "parser.h" +#include "math.h" #include "main.h" #include "math.h" #include "build-config.h" @@ -50,6 +50,10 @@ static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; /* * initialize GScanner for the parsing of tool definitions */ +<<<<<<< HEAD +======= + +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void scanner_init(GScanner *scanner) { scanner->config->case_sensitive = 0; @@ -59,6 +63,7 @@ void scanner_init(GScanner *scanner) scanner->config->numbers_2_int = 1; scanner->config->int_2_float = 1; +<<<<<<< HEAD g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); @@ -68,6 +73,15 @@ void scanner_init(GScanner *scanner) g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR); g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); +======= + g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); + g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); + g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); + g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); + g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR); + g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); + g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); @@ -85,11 +99,14 @@ void scanner_init(GScanner *scanner) g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE); g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE); g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE); +<<<<<<< HEAD g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS); g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE); g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN); g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY); g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_scanner_set_scope (scanner, 0); scanner->config->scope_0_fallback = 0; @@ -121,10 +138,13 @@ gchar* parse_name (GScanner *scanner) name = g_strndup (scanner->value.v_string, len + 3); token = g_scanner_get_next_token (scanner); +<<<<<<< HEAD /* * Are there any options to limit the scope of the definition? */ +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if (token == G_TOKEN_LEFT_BRACE) { g_scanner_set_scope (scanner, 1); @@ -193,11 +213,14 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty style->width = 7; style->arrowsize = 0; style->arrow_type = GROMIT_ARROW_END; +<<<<<<< HEAD style->radius = 10; style->minlen = style->radius * 5 / 2; style->maxangle = 15; style->simplify = 10; style->snapdist = 0; +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->minwidth = 1; style->maxwidth = G_MAXUINT; @@ -226,11 +249,14 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty style->width = context->width; style->arrowsize = context->arrowsize; style->arrow_type = context->arrow_type; +<<<<<<< HEAD style->radius = context->radius; style->minlen = context->minlen; style->maxangle = context->maxangle; style->simplify = context->simplify; style->snapdist = context->snapdist; +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->minwidth = context->minwidth; style->maxwidth = context->maxwidth; *style->paint_color = *context->paint_color; @@ -247,7 +273,11 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty } else { +<<<<<<< HEAD g_printerr ("Expected tool definition or name of template tool\n"); +======= + g_printerr ("Expected Tool-definition or name of template tool\n"); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) goto cleanup; } return TRUE; @@ -256,6 +286,10 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty if (color_allocated) { g_free(style->paint_color); +<<<<<<< HEAD +======= + g_printerr("parse_tool: free color @ %p\n", style->paint_color); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->paint_color = NULL; } return FALSE; @@ -299,7 +333,11 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) { gfloat v = parse_float(scanner, "Missing arrowsize"); if (isnan(v)) return SYM_ERROR; +<<<<<<< HEAD style->arrowsize = v; +======= + style->arrowsize = v + 0.5; +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else if (id == SYM_ARROWTYPE) { @@ -334,6 +372,7 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) return SYM_ERROR; } } +<<<<<<< HEAD else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS) { gfloat v = parse_get_float(scanner, "Missing radius (float)"); @@ -375,6 +414,19 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) gfloat v = parse_float(scanner, "Missing maxsize"); if (isnan(v)) return SYM_ERROR; style->maxwidth = v + 0.5; +======= + else if (id == SYM_MINSIZE) + { + gfloat v = parse_float(scanner, "Missing minsize"); + if (isnan(v)) return SYM_ERROR; + style->minwidth = v + 0.5; + } + else if (id == SYM_MAXSIZE) + { + gfloat v = parse_float(scanner, "Missing maxsize"); + if (isnan(v)) return SYM_ERROR; + style->maxwidth = v + 0.5; +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else { @@ -385,6 +437,7 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) } /* +<<<<<<< HEAD * get "=VALUE", where VALUE is a float * returns NAN is an error occurs */ @@ -404,6 +457,24 @@ gfloat parse_get_float(GScanner *scanner, const gchar *msg) return NAN; } return scanner->value.v_float; +======= + * parse "=", with value being a float + * return as gfloat, or NAN on error + */ +gfloat parse_float(GScanner *scanner, const gchar *msg) { + GTokenType token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_EQUAL_SIGN) { + g_printerr("Missing \"=\"... aborting\n"); + return NAN; + } + token = g_scanner_get_next_token(scanner); + if (token != G_TOKEN_FLOAT) { + g_printerr("%s", msg); + g_printerr("... aborting\n"); + return NAN; + } + return scanner->value.v_float; +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } /* @@ -438,14 +509,14 @@ gboolean parse_style(GScanner *scanner, GromitPaintContext *style) gboolean parse_config (GromitData *data) { gboolean status = FALSE; - GromitPaintContext *context=NULL; + GromitPaintContext *context = NULL; GScanner *scanner; GTokenType token; gchar *filename; int file; gchar *name; - /* try user config location */ + // try user config location filename = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.cfg", NULL); if ((file = open(filename, O_RDONLY)) < 0) @@ -454,7 +525,7 @@ gboolean parse_config (GromitData *data) g_print("Using user config %s\n", filename); - /* try global config file */ + // try global config file if (file < 0) { g_free(filename); filename = g_strdup (SYSCONFDIR "/gromit-mpx/gromit-mpx.cfg"); @@ -464,20 +535,21 @@ gboolean parse_config (GromitData *data) g_print("Using system config %s\n", filename); } - /* was the last possibility, no use to go on */ + // was the last possibility, no use to go on if (file < 0) { g_free(filename); - GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win), - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_WARNING, - GTK_BUTTONS_CLOSE, - _("No usable config file found, falling back to default tools.")); + GtkWidget *dialog = gtk_message_dialog_new( + GTK_WINDOW(data->win), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + _("No usable config file found, falling back to default tools.")); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); return FALSE; } - scanner = g_scanner_new(NULL); + scanner = g_scanner_new (NULL); scanner_init(scanner); scanner->input_name = filename; g_scanner_input_file (scanner, file); @@ -495,13 +567,14 @@ gboolean parse_config (GromitData *data) goto cleanup; if (!parse_tool(data, scanner, &style)) - goto cleanup; + { + g_printerr("parse tool failed\n"); + goto cleanup; + } - token = g_scanner_cur_token(scanner); - // - /* Are there any tool-options? - */ + // are there any tool-options? + token = g_scanner_cur_token(scanner); if (token == G_TOKEN_LEFT_PAREN) { if (! parse_style(scanner, &style)) @@ -516,20 +589,29 @@ gboolean parse_config (GromitData *data) goto cleanup; } +<<<<<<< HEAD context = paint_context_new(data, style.type, style.paint_color, style.width, style.arrowsize, style.arrow_type, style.simplify, style.radius, style.maxangle, style.minlen, style.snapdist, style.minwidth, style.maxwidth); g_hash_table_insert (data->tool_config, name, context); +======= + context = paint_context_new(data, + style.type, + style.paint_color, + style.width, + style.arrowsize, + style.arrow_type, + style.minwidth, + style.maxwidth); + g_hash_table_insert(data->tool_config, name, context); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else if (token == G_TOKEN_SYMBOL && (scanner->value.v_symbol == HOTKEY_SYMBOL_VALUE || scanner->value.v_symbol == UNDOKEY_SYMBOL_VALUE)) { - /* - * Hot key definition - */ - + // hot key definition gpointer key_type = scanner->value.v_symbol; token = g_scanner_get_next_token(scanner); @@ -568,7 +650,7 @@ gboolean parse_config (GromitData *data) } else { - g_printerr ("Expected name of Tool to define or Hot key definition\n"); + g_printerr ("Expected name of tool to define or hot key definition\n"); goto cleanup; } @@ -589,12 +671,13 @@ gboolean parse_config (GromitData *data) g_hash_table_remove_all(data->tool_config); /* alert user */ - GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win), - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_WARNING, - GTK_BUTTONS_CLOSE, - _("Failed parsing config file %s, falling back to default tools."), - filename); + GtkWidget *dialog = gtk_message_dialog_new( + GTK_WINDOW(data->win), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + _("Failed parsing config file %s, falling back to default tools."), + filename); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); } @@ -730,6 +813,7 @@ void read_keyfile(GromitData *data) { gchar *filename = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.ini", NULL); + /* set defaults */ @@ -755,7 +839,7 @@ void read_keyfile(GromitData *data) if(data->opacity == 0) data->opacity = DEFAULT_OPACITY; - cleanup: + cleanup: g_free(filename); g_key_file_free(key_file); } diff --git a/src/config.h b/src/config.h index 1e055ff..340186e 100644 --- a/src/config.h +++ b/src/config.h @@ -34,16 +34,26 @@ Returns TRUE if something got parsed successfully, FALSE otherwise. */ gboolean parse_config (GromitData *data); +<<<<<<< HEAD int parse_args (int argc, char **argv, GromitData *data); gchar* parse_name (GScanner *scanner); typedef enum { SYM_ERROR = 0, SYM_SIZE = 1, +======= +int parse_args(int argc, char **argv, GromitData *data); +gchar* parse_name (GScanner *scanner); + +typedef enum { + SYM_ERROR=0, + SYM_SIZE=1, +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) SYM_COLOR, SYM_ARROWSIZE, SYM_ARROWTYPE, SYM_MINSIZE, +<<<<<<< HEAD SYM_MAXSIZE, SYM_MINLEN, SYM_MAXANGLE, @@ -57,6 +67,15 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *style); gboolean parse_style(GScanner *scanner, GromitPaintContext *style); gfloat parse_get_float(GScanner *scanner, const gchar *msg); +======= + SYM_MAXSIZE +} ToolAttribute; + +void scanner_init(GScanner *scanner); +gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *style); +gboolean parse_style(GScanner *scanner, GromitPaintContext *style); +gfloat parse_float(GScanner *scanner, const gchar *msg); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style); diff --git a/src/drawing.c b/src/drawing.c index 1307322..4e258e3 100644 --- a/src/drawing.c +++ b/src/drawing.c @@ -2,6 +2,17 @@ #include #include "drawing.h" #include "main.h" +<<<<<<< HEAD +======= + +typedef struct +{ + gint x; + gint y; + gint width; +} GromitStrokeCoordinate; + +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void draw_line (GromitData *data, GdkDevice *dev, @@ -105,3 +116,110 @@ void draw_arrow (GromitData *data, data->painted = 1; } +<<<<<<< HEAD +======= + +void coord_list_prepend (GromitData *data, + GdkDevice* dev, + gint x, + gint y, + gint width) +{ + /* get the data for this device */ + GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); + + GromitStrokeCoordinate *point; + + point = g_malloc (sizeof (GromitStrokeCoordinate)); + point->x = x; + point->y = y; + point->width = width; + + devdata->coordlist = g_list_prepend (devdata->coordlist, point); +} + + +void coord_list_free (GromitData *data, + GdkDevice* dev) +{ + /* get the data for this device */ + GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); + + GList *ptr; + ptr = devdata->coordlist; + + while (ptr) + { + g_free (ptr->data); + ptr = ptr->next; + } + + g_list_free (devdata->coordlist); + + devdata->coordlist = NULL; +} + +/* + * for double-ended arrows, two separate calls are required + */ + +gboolean coord_list_get_arrow_param (GromitData *data, + GdkDevice *dev, + gint search_radius, + GromitArrowType arrow_end, + gint *x0, + gint *y0, + gint *ret_width, + gfloat *ret_direction) +{ + gint r2, dist; + gboolean success = FALSE; + GromitStrokeCoordinate *cur_point, *valid_point; + /* get the data for this device */ + GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); + GList *ptr = devdata->coordlist; + gfloat width; + + valid_point = NULL; + + if (ptr) + { + if (arrow_end == GROMIT_ARROW_START) + ptr = g_list_last(ptr); + cur_point = ptr->data; + *x0 = cur_point->x; + *y0 = cur_point->y; + r2 = search_radius * search_radius; + dist = 0; + + while (ptr && dist < r2) + { + if (arrow_end == GROMIT_ARROW_END) + ptr = ptr->next; + else + ptr = ptr->prev; + if (ptr) + { + cur_point = ptr->data; + dist = (cur_point->x - *x0) * (cur_point->x - *x0) + + (cur_point->y - *y0) * (cur_point->y - *y0); + width = cur_point->width * devdata->cur_context->arrowsize; + if (width * 2 <= dist && + (!valid_point || valid_point->width < cur_point->width)) + valid_point = cur_point; + } + } + + if (valid_point) + { + *ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize, + 2); + *ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x); + success = TRUE; + } + } + + return success; +} + +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) diff --git a/src/drawing.h b/src/drawing.h index 2abf151..1cb4a38 100644 --- a/src/drawing.h +++ b/src/drawing.h @@ -19,4 +19,19 @@ typedef struct void draw_line (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint x2, gint y2); void draw_arrow (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint width, gfloat direction); +<<<<<<< HEAD +======= +gboolean coord_list_get_arrow_param (GromitData *data, + GdkDevice *dev, + gint search_radius, + GromitArrowType arrow_end, + gint *x0, + gint *y0, + gint *ret_width, + gfloat *ret_direction); +void coord_list_prepend (GromitData *data, GdkDevice* dev, gint x, gint y, gint width); +void coord_list_free (GromitData *data, GdkDevice* dev); + + +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) #endif diff --git a/src/main.c b/src/main.c index 2cf111c..186843b 100644 --- a/src/main.c +++ b/src/main.c @@ -43,11 +43,14 @@ GromitPaintContext *paint_context_new (GromitData *data, guint width, guint arrowsize, GromitArrowType arrowtype, +<<<<<<< HEAD guint simpilfy, guint radius, guint maxangle, guint minlen, guint snapdist, +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) guint minwidth, guint maxwidth) { @@ -79,10 +82,9 @@ GromitPaintContext *paint_context_new (GromitData *data, if (type == GROMIT_ERASER) cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_CLEAR); - else - if (type == GROMIT_RECOLOR) + else if (type == GROMIT_RECOLOR) cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_ATOP); - else /* GROMIT_PEN */ + else /* GROMIT_PEN */ cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_OVER); return context; @@ -96,6 +98,7 @@ void paint_context_print (gchar *name, switch (context->type) { case GROMIT_PEN: +<<<<<<< HEAD g_printerr ("Pen, "); break; case GROMIT_LINE: g_printerr ("Line, "); break; @@ -105,6 +108,13 @@ void paint_context_print (gchar *name, g_printerr ("Smooth, "); break; case GROMIT_ORTHOGONAL: g_printerr ("Orthogonal, "); break; +======= + g_printerr ("Pen, "); break; + case GROMIT_LINE: + g_printerr ("Line, "); break; + case GROMIT_RECT: + g_printerr ("Rect, "); break; +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) case GROMIT_ERASER: g_printerr ("Eraser, "); break; case GROMIT_RECOLOR: @@ -131,6 +141,7 @@ void paint_context_print (gchar *name, break; } } +<<<<<<< HEAD if (context->type == GROMIT_SMOOTH || context->type == GROMIT_ORTHOGONAL) { g_printerr(" simplify: %u, ", context->simplify); @@ -142,6 +153,8 @@ void paint_context_print (gchar *name, g_printerr(" radius: %u, minlen: %u, maxangle: %u ", context->radius, context->minlen, context->maxangle); } +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_printerr ("color: %s\n", gdk_rgba_to_string(context->paint_color)); } @@ -285,9 +298,9 @@ void select_tool (GromitData *data, guint req_buttons = 0, req_modifier = 0; guint i, j, success = 0; GromitPaintContext *context = NULL; - guchar *slave_name; - guchar *name; - guchar *default_name; + guchar *slave_name = NULL; + guchar *name = NULL; + guchar *default_name = NULL; /* get the data for this device */ GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, device); @@ -394,6 +407,7 @@ void select_tool (GromitData *data, g_free (name); g_free (default_name); + g_free (slave_name); } else g_printerr ("ERROR: select_tool attempted to select nonexistent device!\n"); @@ -599,6 +613,8 @@ void main_do_event (GdkEventAny *event, + + void setup_main_app (GromitData *data, int argc, char ** argv) { gboolean activate; @@ -691,6 +707,13 @@ void setup_main_app (GromitData *data, int argc, char ** argv) data->undo_buffer_size[i] = 0; data->undo_buffer[i] = NULL; } +<<<<<<< HEAD +======= + + // original state for LINE and RECT tool + cairo_surface_destroy(data->aux_backbuffer); + data->aux_backbuffer = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, data->width, data->height); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) /* EVENTS */ gtk_widget_add_events (data->win, GROMIT_WINDOW_EVENTS); @@ -751,8 +774,8 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_selection_add_target (data->win, GA_CONTROL, GA_UNDO, 8); gtk_selection_add_target (data->win, GA_CONTROL, GA_REDO, 9); gtk_selection_add_target (data->win, GA_CONTROL, GA_LINE, 10); - - gtk_selection_add_target (data->win, GA_CONTROL, GA_CHGTOOL, 11); + gtk_selection_add_target (data->win, GA_CONTROL, GA_DEFTOOL, 11); + gtk_selection_add_target (data->win, GA_CONTROL, GA_CHGATTR, 12); /* * Parse Config file @@ -849,11 +872,19 @@ void setup_main_app (GromitData *data, int argc, char ** argv) data->modified = 0; data->default_pen = +<<<<<<< HEAD paint_context_new (data, GROMIT_PEN, data->red, 7, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, 1, G_MAXUINT); data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, 1, G_MAXUINT); +======= + paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, + 0, GROMIT_ARROW_END, 1, G_MAXUINT); + data->default_eraser = + paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, + 0, GROMIT_ARROW_END, 1, G_MAXUINT); +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gdk_event_handler_set ((GdkEventFunc) main_do_event, data, NULL); gtk_key_snooper_install (snoop_key_press, data); @@ -894,7 +925,10 @@ void setup_main_app (GromitData *data, int argc, char ** argv) GtkWidget* sep1_item = gtk_separator_menu_item_new(); GtkWidget* intro_item = gtk_menu_item_new_with_mnemonic(_("_Introduction")); +<<<<<<< HEAD GtkWidget* edit_config_item = gtk_menu_item_new_with_mnemonic(_("_Edit Config")); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GtkWidget* issues_item = gtk_menu_item_new_with_mnemonic(_("_Report Bug / Request Feature")); GtkWidget* support_item = gtk_menu_item_new_with_mnemonic(_("_Support Gromit-MPX")); GtkWidget* about_item = gtk_menu_item_new_with_mnemonic(_("_About")); @@ -917,7 +951,10 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep1_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), intro_item); +<<<<<<< HEAD gtk_menu_shell_append (GTK_MENU_SHELL (menu), edit_config_item); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gtk_menu_shell_append (GTK_MENU_SHELL (menu), issues_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), support_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), about_item); @@ -980,9 +1017,12 @@ void setup_main_app (GromitData *data, int argc, char ** argv) g_signal_connect(G_OBJECT (intro_item), "activate", G_CALLBACK (on_intro), data); +<<<<<<< HEAD g_signal_connect(G_OBJECT (edit_config_item), "activate", G_CALLBACK (on_edit_config), data); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_signal_connect(G_OBJECT (issues_item), "activate", G_CALLBACK (on_issues), data); @@ -1007,7 +1047,10 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_widget_show (sep1_item); gtk_widget_show (intro_item); +<<<<<<< HEAD gtk_widget_show (edit_config_item); +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gtk_widget_show (issues_item); gtk_widget_show (support_item); gtk_widget_show (about_item); @@ -1058,6 +1101,7 @@ void parse_print_help (gpointer key, gpointer value, gpointer user_data) } + /* * Main programs */ @@ -1165,7 +1209,22 @@ int main_client (int argc, char **argv, GromitData *data) else { i++; - action = GA_CHGTOOL; + action = GA_DEFTOOL; + data->clientdata = argv[i]; + } + } + else if (strcmp (arg, "--change-attribute") == 0 || + strcmp(arg, "-A") == 0) + { + if (argc <= i+1) + { + wrong_arg = TRUE; + g_printerr("--change-attribute requires an argument\n"); + } + else + { + i++; + action = GA_CHGATTR; data->clientdata = argv[i]; } } @@ -1251,8 +1310,8 @@ int main (int argc, char **argv) gtk_selection_owner_set (data->win, GA_DATA, GDK_CURRENT_TIME); gtk_selection_add_target (data->win, GA_DATA, GA_TOGGLEDATA, 1007); gtk_selection_add_target (data->win, GA_DATA, GA_LINEDATA, 1008); - gtk_selection_add_target (data->win, GA_DATA, GA_CHGTOOLDATA, 1009); - + gtk_selection_add_target (data->win, GA_DATA, GA_DEFTOOLDATA, 1009); + gtk_selection_add_target (data->win, GA_DATA, GA_CHGATTRDATA, 1010); /* Try to get a status message. If there is a response gromit * is already active. diff --git a/src/main.h b/src/main.h index df93da3..79e2afa 100644 --- a/src/main.h +++ b/src/main.h @@ -42,25 +42,26 @@ #define GROMIT_WINDOW_EVENTS ( GROMIT_MOUSE_EVENTS | GDK_EXPOSURE_MASK) /* Atoms used to control Gromit */ -#define GA_CONTROL gdk_atom_intern ("Gromit/control", FALSE) -#define GA_STATUS gdk_atom_intern ("Gromit/status", FALSE) -#define GA_QUIT gdk_atom_intern ("Gromit/quit", FALSE) -#define GA_ACTIVATE gdk_atom_intern ("Gromit/activate", FALSE) -#define GA_DEACTIVATE gdk_atom_intern ("Gromit/deactivate", FALSE) -#define GA_TOGGLE gdk_atom_intern ("Gromit/toggle", FALSE) -#define GA_LINE gdk_atom_intern ("Gromit/line", FALSE) -#define GA_VISIBILITY gdk_atom_intern ("Gromit/visibility", FALSE) -#define GA_CLEAR gdk_atom_intern ("Gromit/clear", FALSE) -#define GA_RELOAD gdk_atom_intern ("Gromit/reload", FALSE) -#define GA_UNDO gdk_atom_intern ("Gromit/undo", FALSE) -#define GA_REDO gdk_atom_intern ("Gromit/redo", FALSE) -#define GA_CHGTOOL gdk_atom_intern ("Gromit/chgtool", FALSE) - -#define GA_DATA gdk_atom_intern ("Gromit/data", FALSE) -#define GA_TOGGLEDATA gdk_atom_intern ("Gromit/toggledata", FALSE) -#define GA_LINEDATA gdk_atom_intern ("Gromit/linedata", FALSE) -#define GA_CHGTOOLDATA gdk_atom_intern ("Gromit/chgtooldata", FALSE) - +#define GA_CONTROL gdk_atom_intern ("Gromit/control", FALSE) +#define GA_STATUS gdk_atom_intern ("Gromit/status", FALSE) +#define GA_QUIT gdk_atom_intern ("Gromit/quit", FALSE) +#define GA_ACTIVATE gdk_atom_intern ("Gromit/activate", FALSE) +#define GA_DEACTIVATE gdk_atom_intern ("Gromit/deactivate", FALSE) +#define GA_TOGGLE gdk_atom_intern ("Gromit/toggle", FALSE) +#define GA_LINE gdk_atom_intern ("Gromit/line", FALSE) +#define GA_VISIBILITY gdk_atom_intern ("Gromit/visibility", FALSE) +#define GA_CLEAR gdk_atom_intern ("Gromit/clear", FALSE) +#define GA_RELOAD gdk_atom_intern ("Gromit/reload", FALSE) +#define GA_UNDO gdk_atom_intern ("Gromit/undo", FALSE) +#define GA_REDO gdk_atom_intern ("Gromit/redo", FALSE) +#define GA_DEFTOOL gdk_atom_intern ("Gromit/deftool", FALSE) +#define GA_CHGATTR gdk_atom_intern ("Gromit/chgattr", FALSE) + +#define GA_DATA gdk_atom_intern ("Gromit/data", FALSE) +#define GA_TOGGLEDATA gdk_atom_intern ("Gromit/toggledata", FALSE) +#define GA_LINEDATA gdk_atom_intern ("Gromit/linedata", FALSE) +#define GA_DEFTOOLDATA gdk_atom_intern ("Gromit/deftooldata", FALSE) +#define GA_CHGATTRDATA gdk_atom_intern ("Gromit/chgattrdata", FALSE) #define GROMIT_MAX_UNDO 100 @@ -69,8 +70,11 @@ typedef enum GROMIT_PEN, GROMIT_LINE, GROMIT_RECT, +<<<<<<< HEAD GROMIT_SMOOTH, GROMIT_ORTHOGONAL, +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GROMIT_ERASER, GROMIT_RECOLOR } GromitPaintType; @@ -172,6 +176,7 @@ typedef struct size_t undo_temp_used; gint undo_head, undo_depth, redo_depth; + gboolean show_intro_on_startup; } GromitData; @@ -198,7 +203,10 @@ void clear_screen (GromitData *data); GromitPaintContext *paint_context_new (GromitData *data, GromitPaintType type, GdkRGBA *fg_color, guint width, guint arrowsize, GromitArrowType arrowtype, +<<<<<<< HEAD guint simpilfy, guint radius, guint maxangle, guint minlen, guint snapdist, +======= +>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) guint minwidth, guint maxwidth); void paint_context_free (GromitPaintContext *context); diff --git a/src/parser.c b/src/parser.c deleted file mode 100644 index 77196e2..0000000 --- a/src/parser.c +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Gromit-MPX -- a program for painting on the screen - * - * Gromit Copyright (C) 2000 Simon Budig - * - * Gromit-MPX Copyright (C) 2009,2010 Christian Beier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "parser.h" -#include "main.h" -#include "build-config.h" - -gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3; -gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; - -/* - * set scanner to default initial values - */ - -void scanner_init(GScanner* scanner) -{ - scanner->config->case_sensitive = 0; - scanner->config->scan_octal = 0; - scanner->config->identifier_2_string = 0; - scanner->config->char_2_token = 1; - scanner->config->numbers_2_int = 1; - scanner->config->int_2_float = 1; - - g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); - g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); - g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR); - g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); - g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); - - g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4); - g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5); - g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11); - g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12); - g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13); - g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13); - - g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) 1); - g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) 2); - g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) 3); - g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) 4); - g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) 5); - - g_scanner_set_scope (scanner, 0); - scanner->config->scope_0_fallback = 0; -} - -/* - * get name of tool, e.g. "default"[SHIFT], which is returned as "defaukt|@1" - * returns NULL upon error - */ - -gchar* parse_name (GScanner *scanner) -{ - GTokenType token; - - guint buttons = 0; - guint modifier = 0; - guint len = 0; - gchar *name; - - token = g_scanner_cur_token(scanner); - - if (token != G_TOKEN_STRING) - { - g_scanner_unexp_token (scanner, G_TOKEN_STRING, NULL, - NULL, NULL, "aborting", TRUE); - return NULL; - } - - len = strlen (scanner->value.v_string); - name = g_strndup (scanner->value.v_string, len + 3); - - token = g_scanner_get_next_token (scanner); - - if (token == G_TOKEN_LEFT_BRACE) - { - g_scanner_set_scope (scanner, 1); - scanner->config->int_2_float = 0; - modifier = buttons = 0; - while ((token = g_scanner_get_next_token (scanner)) - != G_TOKEN_RIGHT_BRACE) - { - if (token == G_TOKEN_SYMBOL) - { - if ((intptr_t) scanner->value.v_symbol < 11) - buttons |= 1 << ((intptr_t) scanner->value.v_symbol - 1); - else - modifier |= 1 << ((intptr_t) scanner->value.v_symbol - 11); - } - else if (token == G_TOKEN_INT) - { - if (scanner->value.v_int <= 5 && scanner->value.v_int > 0) - buttons |= 1 << (scanner->value.v_int - 1); - else - g_printerr ("Only Buttons 1-5 are supported!\n"); - } - else - { - g_printerr ("skipped token\n"); - } - } - g_scanner_set_scope (scanner, 0); - scanner->config->int_2_float = 1; - token = g_scanner_get_next_token (scanner); - } - - name [len] = 124; - name [len+1] = buttons + 64; - name [len+2] = modifier + 48; - name [len+3] = 0; - - return name; -} - -/* - * get the "type" of the tool, e.g. PEN (e.g. =PEN), or a base tool - * style that is inherited (e.g. ="red pen") and store characteristics - * in style. - * - * returns FALSE upon error - */ - -gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style) -{ - GTokenType token = g_scanner_cur_token(scanner); - - if (token != G_TOKEN_EQUAL_SIGN) - { - g_scanner_unexp_token( - scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE); - return FALSE; - } - - token = g_scanner_get_next_token(scanner); - - /* defaults */ - style->type = GROMIT_PEN; - style->width = 7; - style->arrowsize = 0; - style->minwidth = 1; - style->maxwidth = G_MAXUINT; - - GdkRGBA *red_color = g_malloc(sizeof (GdkRGBA)); - *red_color = *data->red; - style->paint_color = red_color; - - if (token == G_TOKEN_SYMBOL) - { - style->type = (GromitPaintType)scanner->value.v_symbol; - token = g_scanner_get_next_token(scanner); - } - else if (token == G_TOKEN_STRING) - { - gchar *copy = parse_name(scanner); - if (!copy) - return FALSE; - token = g_scanner_cur_token(scanner); - GromitPaintContext *context = g_hash_table_lookup(data->tool_config, copy); - if (context) - { - style->type = context->type; - style->width = context->width; - style->arrowsize = context->arrowsize; - style->minwidth = context->minwidth; - style->maxwidth = context->maxwidth; - style->paint_color = context->paint_color; - } - else - { - g_printerr( - "WARNING: Unable to copy \"%s\": " - "not yet defined!\n", - copy); - } - } - else - { - g_printerr( - "Expected Tool-definition " - "or name of template tool\n"); - return FALSE; - } - return TRUE; -} - -/* - * parses a pen style definition (e.g. (color="red" size=3) ) - * and stores fields found in GromitStyleDef. - * - * returns FALSE upon any error - */ - -gboolean parse_style(GScanner *scanner, GromitStyleDef *style) -{ - GdkRGBA *color = NULL; - g_scanner_set_scope(scanner, 2); - scanner->config->int_2_float = 1; - GTokenType token = g_scanner_get_next_token(scanner); - while (token != G_TOKEN_RIGHT_PAREN) - { - if (token == G_TOKEN_SYMBOL) - { - if ((intptr_t)scanner->value.v_symbol == 1) - { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr("Missing Size (float)... aborting\n"); - return FALSE; - } - style->width = (guint)(scanner->value.v_float + 0.5); - } - else if ((intptr_t)scanner->value.v_symbol == 2) - { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_STRING) - { - g_printerr( - "Missing Color (string)... " - "aborting\n"); - return FALSE; - } - color = g_malloc(sizeof(GdkRGBA)); - if (gdk_rgba_parse(color, scanner->value.v_string)) - { - style->paint_color = color; - } - else - { - g_printerr( - "Unable to parse color. " - "Keeping default.\n"); - g_free(color); - } - color = NULL; - } - else if ((intptr_t)scanner->value.v_symbol == 3) - { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr( - "Missing Arrowsize (float)... " - "aborting\n"); - return FALSE; - } - style->arrowsize = scanner->value.v_float; - } - else if ((intptr_t)scanner->value.v_symbol == 4) - { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr( - "Missing Minsize (float)... " - "aborting\n"); - return FALSE; - } - style->minwidth = scanner->value.v_float; - } - else if ((intptr_t)scanner->value.v_symbol == 5) - { - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) - { - g_printerr("Missing \"=\"... aborting\n"); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) - { - g_printerr( - "Missing Maxsize (float)... " - "aborting\n"); - return FALSE; - } - style->maxwidth = scanner->value.v_float; - } - else - { - g_printerr("Unknown tool type ! \n"); - return FALSE; - } - } - else - { - g_printerr("Unknown token: %d !\n", token); - return FALSE; - } - token = g_scanner_get_next_token(scanner); - } // while (token != G_TOKEN_RIGHT_PAREN) - g_scanner_set_scope(scanner, 0); - g_scanner_get_next_token(scanner); - return TRUE; -} diff --git a/src/parser.h b/src/parser.h deleted file mode 100644 index 1ff684d..0000000 --- a/src/parser.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Gromit-MPX -- a program for painting on the screen - * - * Gromit Copyright (C) 2000 Simon Budig - * - * Gromit-MPX Copyright (C) 2009,2010 Christian Beier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - */ - -#ifndef PARSER_H -#define PARSER_H - -#include "main.h" - - -typedef struct -{ - GromitPaintType type; - guint width; - gfloat arrowsize; - guint minwidth; - guint maxwidth; - GdkRGBA *paint_color; -} GromitStyleDef; - -extern gpointer HOTKEY_SYMBOL_VALUE; -extern gpointer UNDOKEY_SYMBOL_VALUE; - -void scanner_init(GScanner* scanner); -gchar* parse_name (GScanner *scanner); -gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style); -gboolean parse_style(GScanner *scanner, GromitStyleDef *style); - -#endif // PARSER_H From 048ec6a10223cb0a5d72bdfeda879925faad0719 Mon Sep 17 00:00:00 2001 From: pascal Date: Tue, 19 Mar 2024 14:37:22 +0100 Subject: [PATCH 6/7] parser.[ch] removed from CMakeLists.txt --- CMakeLists.txt | 2 - README.md | 10 +++- gromit-mpx.1 | 11 ++-- src/callbacks.c | 53 +++-------------- src/callbacks.h | 3 - src/config.c | 147 ++++++++++-------------------------------------- src/config.h | 30 ++-------- src/drawing.c | 118 -------------------------------------- src/drawing.h | 15 ----- src/main.c | 52 +++-------------- src/main.h | 10 +--- 11 files changed, 67 insertions(+), 384 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5e09c0..d64d92e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,8 +55,6 @@ set(sources src/callbacks.h src/config.c src/config.h - src/parser.c - src/parser.h src/drawing.c src/drawing.h src/coordlist_ops.c diff --git a/README.md b/README.md index a7e7fcd..24d6957 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,17 @@ Usage: will undo the last drawing stroke (or "-z") gromit-mpx --redo will redo the last undone drawing stroke (or "-y") + gromit-mpx --change-tool + will redefine a tool, using the same syntax as in the .cfg file + e.g. gromit-mpx --change-tool '"default"=RECT(color="yellow" size=2)' + gromit-mpx --change-attribute + will change one or several attributes of a tool, keeping the others + as they were. This can be used to change e.g. to color or type of a tool + e.g. gromit-mpx --change-attribute '"default"=(color="cyan")' + gromit-mpx --change-attribute '"default"=LINE' gromit-mpx --line will draw a straight line with characteristics specified by the arguments (or "-l") - eg: gromit-mpx -l 200 200 400 400 '#C4A7E7' 6 + e.g.: gromit-mpx -l 200 200 400 400 '#C4A7E7' 6 If activated Gromit-MPX prevents you from using other programs with the mouse. You can press the button and paint on the screen. Key presses diff --git a/gromit-mpx.1 b/gromit-mpx.1 index c0462b5..28476ff 100644 --- a/gromit-mpx.1 +++ b/gromit-mpx.1 @@ -1,5 +1,5 @@ .\" Hey, vim: ft=nroff -.TH GROMIT-MPX 1 "November 3, 2018" +.TH GROMIT-MPX 1 "March 31, 2024" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: @@ -95,9 +95,6 @@ running Gromit-MPX process, see above for the options available to start Gromit- .B \-c, \-\-clear will clear the screen. .TP -.B \-l , \-\-line -will draw a line from (x1,y1) to (x2,y2) with color and width . -.TP .B \-q, \-\-quit will cause the main Gromit-MPX process to quit. .TP @@ -107,6 +104,12 @@ will toggle the grabbing of the cursor. .B \-T , \-\-change-tool will change the definition of a tool. The syntax is as in the .cfg file read at startup, except for the trailing semicolon. +(example: gromit-mpx -T '"default"=RECT(color="yellow" size=2)' +.TP +.B \-A , \-\-change-attribute +will change one or several attributes of a tool, keeping the other settings. +(example 1: change just the color: gromit-mpx -A '"default"=(color="yellow")'; +example 2: change tool type, keeping color, size etc: gromit-mpx -A '"default"=LINE') .TP .B \-v, \-\-visibility will toggle the visibility of the window. diff --git a/src/callbacks.c b/src/callbacks.c index 73d3da1..cf792f4 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -25,7 +25,6 @@ #include #include #include -#include "cairo.h" #include "main.h" #include "input.h" #include "callbacks.h" @@ -142,18 +141,10 @@ void on_monitors_changed ( GdkScreen *screen, parse_config(data); // also calls paint_context_new() :-( - -<<<<<<< HEAD - data->default_pen = paint_context_new (data, GROMIT_PEN, data->red, 7, 0, GROMIT_ARROW_END, + data->default_pen = paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 1, 0, G_MAXUINT); - data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75, 0, GROMIT_ARROW_END, + data->default_eraser = paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 1, 0, G_MAXUINT); -======= - data->default_pen = paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, - 0, GROMIT_ARROW_END, 1, G_MAXUINT); - data->default_eraser = paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, - 0, GROMIT_ARROW_END, 1, G_MAXUINT); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if(!data->composited) // set shape { @@ -221,7 +212,7 @@ void on_clientapp_selection_get (GtkWidget *widget, if (gtk_selection_data_get_target(selection_data) == GA_TOGGLEDATA || gtk_selection_data_get_target(selection_data) == GA_LINEDATA || - gtk_selection_data_get_target(selection_data) == GA_DEFTOOLDATA || + gtk_selection_data_get_target(selection_data) == GA_CHGTOOLDATA || gtk_selection_data_get_target(selection_data) == GA_CHGATTRDATA) { ans = data->clientdata; @@ -292,11 +283,7 @@ gboolean on_buttonpress (GtkWidget *win, GromitPaintType type = devdata->cur_context->type; // store original state to have dynamic update of line and rect -<<<<<<< HEAD if (type == GROMIT_LINE || type == GROMIT_RECT || type == GROMIT_SMOOTH || type == GROMIT_ORTHOGONAL) -======= - if (type == GROMIT_LINE || type == GROMIT_RECT) ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) { copy_surface(data->aux_backbuffer, data->backbuffer); } @@ -413,12 +400,8 @@ gboolean on_motion (GtkWidget *win, } if (type == GROMIT_LINE) { -<<<<<<< HEAD - GromitArrowType atype = devdata->cur_context->arrow_type; -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y); - if (devdata->cur_context->arrowsize > 0) + if (devdata->cur_context->arrowsize > 0.0) { GromitArrowType atype = devdata->cur_context->arrow_type; gint width = devdata->cur_context->arrowsize * devdata->cur_context->width / 2; @@ -467,14 +450,8 @@ gboolean on_buttonrelease (GtkWidget *win, gfloat direction = 0; gint width = 0; -<<<<<<< HEAD if(ctx) width = ctx->arrowsize * ctx->width / 2; -======= - if(devdata->cur_context) - width = devdata->cur_context->arrowsize * devdata->cur_context->width / 2; - ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if ((ev->x != devdata->lastx) || (ev->y != devdata->lasty)) @@ -483,7 +460,6 @@ gboolean on_buttonrelease (GtkWidget *win, if (!devdata->is_grabbed) return FALSE; -<<<<<<< HEAD GromitPaintType type = ctx->type; if (type == GROMIT_SMOOTH || type == GROMIT_ORTHOGONAL) @@ -517,13 +493,6 @@ gboolean on_buttonrelease (GtkWidget *win, if (ctx->arrowsize != 0) { GromitArrowType atype = ctx->arrow_type; -======= - GromitPaintType type = devdata->cur_context->type; - - if (devdata->cur_context->arrowsize != 0) - { - GromitArrowType atype = devdata->cur_context->arrow_type; ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if (type == GROMIT_LINE) { direction = atan2 (ev->y - devdata->lasty, ev->x - devdata->lastx); @@ -590,9 +559,9 @@ void on_mainapp_selection_get (GtkWidget *widget, undo_drawing (data); else if (action == GA_REDO) redo_drawing (data); - else if (action == GA_DEFTOOL) + else if (action == GA_CHGTOOL) { - gtk_selection_convert(data->win, GA_DATA, GA_DEFTOOLDATA, time); + gtk_selection_convert(data->win, GA_DATA, GA_CHGTOOLDATA, time); gtk_main(); } else if (action == GA_CHGATTR) @@ -687,13 +656,8 @@ void on_mainapp_selection_received (GtkWidget *widget, "Keeping default.\n"); } GromitPaintContext* line_ctx = -<<<<<<< HEAD paint_context_new(data, GROMIT_PEN, fg_color, thickness, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, thickness, thickness); -======= - paint_context_new(data, GROMIT_PEN, fg_color, thickness, - 0, GROMIT_ARROW_END, thickness, thickness); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GdkRectangle rect; rect.x = MIN (startX,endX) - thickness / 2; @@ -719,7 +683,7 @@ void on_mainapp_selection_received (GtkWidget *widget, else { GdkAtom atom = gtk_selection_data_get_target(selection_data); - if (atom == GA_DEFTOOLDATA || atom == GA_CHGATTRDATA) + if (atom == GA_CHGTOOLDATA || atom == GA_CHGATTRDATA) { gchar *a = (gchar *)gtk_selection_data_get_data(selection_data); if(data->debug) g_printerr("DEBUG: define tool: %s\n", a); @@ -1100,7 +1064,6 @@ void on_intro(GtkMenuItem *menuitem, gtk_widget_show_all (assistant); } -<<<<<<< HEAD void on_edit_config(GtkMenuItem *menuitem, gpointer user_data) { @@ -1155,8 +1118,6 @@ void on_edit_config(GtkMenuItem *menuitem, } -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void on_issues(GtkMenuItem *menuitem, gpointer user_data) { diff --git a/src/callbacks.h b/src/callbacks.h index ce7e88b..7bcbdd8 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -128,12 +128,9 @@ void on_about(GtkMenuItem *menuitem, void on_intro(GtkMenuItem *menuitem, gpointer user_data); -<<<<<<< HEAD void on_edit_config(GtkMenuItem *menuitem, gpointer user_data); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void on_issues(GtkMenuItem *menuitem, gpointer user_data); diff --git a/src/config.c b/src/config.c index 3693a04..9bf183b 100644 --- a/src/config.c +++ b/src/config.c @@ -29,7 +29,6 @@ #include #include "config.h" -#include "math.h" #include "main.h" #include "math.h" #include "build-config.h" @@ -50,10 +49,6 @@ static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4; /* * initialize GScanner for the parsing of tool definitions */ -<<<<<<< HEAD -======= - ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void scanner_init(GScanner *scanner) { scanner->config->case_sensitive = 0; @@ -63,7 +58,6 @@ void scanner_init(GScanner *scanner) scanner->config->numbers_2_int = 1; scanner->config->int_2_float = 1; -<<<<<<< HEAD g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); @@ -73,15 +67,6 @@ void scanner_init(GScanner *scanner) g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR); g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); -======= - g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN); - g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE); - g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT); - g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER); - g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR); - g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE); - g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1); g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2); @@ -99,14 +84,11 @@ void scanner_init(GScanner *scanner) g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE); g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE); g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE); -<<<<<<< HEAD g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS); g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE); g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN); g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY); g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_scanner_set_scope (scanner, 0); scanner->config->scope_0_fallback = 0; @@ -138,13 +120,6 @@ gchar* parse_name (GScanner *scanner) name = g_strndup (scanner->value.v_string, len + 3); token = g_scanner_get_next_token (scanner); -<<<<<<< HEAD - /* - * Are there any options to limit the scope of the definition? - */ - -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) if (token == G_TOKEN_LEFT_BRACE) { g_scanner_set_scope (scanner, 1); @@ -213,14 +188,11 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty style->width = 7; style->arrowsize = 0; style->arrow_type = GROMIT_ARROW_END; -<<<<<<< HEAD style->radius = 10; style->minlen = style->radius * 5 / 2; style->maxangle = 15; style->simplify = 10; style->snapdist = 0; -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->minwidth = 1; style->maxwidth = G_MAXUINT; @@ -249,14 +221,11 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty style->width = context->width; style->arrowsize = context->arrowsize; style->arrow_type = context->arrow_type; -<<<<<<< HEAD style->radius = context->radius; style->minlen = context->minlen; style->maxangle = context->maxangle; style->simplify = context->simplify; style->snapdist = context->snapdist; -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->minwidth = context->minwidth; style->maxwidth = context->maxwidth; *style->paint_color = *context->paint_color; @@ -273,11 +242,7 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty } else { -<<<<<<< HEAD g_printerr ("Expected tool definition or name of template tool\n"); -======= - g_printerr ("Expected Tool-definition or name of template tool\n"); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) goto cleanup; } return TRUE; @@ -286,10 +251,6 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty if (color_allocated) { g_free(style->paint_color); -<<<<<<< HEAD -======= - g_printerr("parse_tool: free color @ %p\n", style->paint_color); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) style->paint_color = NULL; } return FALSE; @@ -306,7 +267,7 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) GTokenType token; if (id == SYM_SIZE) { - gfloat v = parse_float(scanner, "Missing size"); + gfloat v = parse_get_float(scanner, "Missing size"); if (isnan(v)) return SYM_ERROR; style->width = v + 0.5; } @@ -331,13 +292,9 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) } else if (id == SYM_ARROWSIZE) { - gfloat v = parse_float(scanner, "Missing arrowsize"); + gfloat v = parse_get_float(scanner, "Missing arrowsize"); if (isnan(v)) return SYM_ERROR; -<<<<<<< HEAD style->arrowsize = v; -======= - style->arrowsize = v + 0.5; ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else if (id == SYM_ARROWTYPE) { @@ -372,61 +329,47 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) return SYM_ERROR; } } -<<<<<<< HEAD else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS) { gfloat v = parse_get_float(scanner, "Missing radius (float)"); - if (isnan(v)) goto cleanup; + if (isnan(v)) return SYM_ERROR; style->radius = v; } else if ((intptr_t) scanner->value.v_symbol == SYM_MAXANGLE) { gfloat v = parse_get_float(scanner, "Missing angle (float)"); - if (isnan(v)) goto cleanup; + if (isnan(v)) return SYM_ERROR; style->maxangle = v; } else if ((intptr_t) scanner->value.v_symbol == SYM_SIMPLIFY) { gfloat v = parse_get_float(scanner, "Missing simplify value (float)"); - if (isnan(v)) goto cleanup; + if (isnan(v)) return SYM_ERROR; style->simplify = v; } else if ((intptr_t) scanner->value.v_symbol == SYM_MINLEN) { gfloat v = parse_get_float(scanner, "Missing minlen value (float)"); - if (isnan(v)) goto cleanup; + if (isnan(v)) return SYM_ERROR; style->minlen = v; } else if ((intptr_t) scanner->value.v_symbol == SYM_SNAP) { gfloat v = parse_get_float(scanner, "Missing snap distance (float)"); - if (isnan(v)) goto cleanup; + if (isnan(v)) return SYM_ERROR; style->snapdist = v; } else if (id == SYM_MINSIZE) { - gfloat v = parse_float(scanner, "Missing minsize"); + gfloat v = parse_get_float(scanner, "Missing minsize"); if (isnan(v)) return SYM_ERROR; style->minwidth = v + 0.5; } else if (id == SYM_MAXSIZE) { - gfloat v = parse_float(scanner, "Missing maxsize"); + gfloat v = parse_get_float(scanner, "Missing maxsize"); if (isnan(v)) return SYM_ERROR; style->maxwidth = v + 0.5; -======= - else if (id == SYM_MINSIZE) - { - gfloat v = parse_float(scanner, "Missing minsize"); - if (isnan(v)) return SYM_ERROR; - style->minwidth = v + 0.5; - } - else if (id == SYM_MAXSIZE) - { - gfloat v = parse_float(scanner, "Missing maxsize"); - if (isnan(v)) return SYM_ERROR; - style->maxwidth = v + 0.5; ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else { @@ -437,7 +380,6 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style) } /* -<<<<<<< HEAD * get "=VALUE", where VALUE is a float * returns NAN is an error occurs */ @@ -457,26 +399,7 @@ gfloat parse_get_float(GScanner *scanner, const gchar *msg) return NAN; } return scanner->value.v_float; -======= - * parse "=", with value being a float - * return as gfloat, or NAN on error - */ -gfloat parse_float(GScanner *scanner, const gchar *msg) { - GTokenType token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_EQUAL_SIGN) { - g_printerr("Missing \"=\"... aborting\n"); - return NAN; - } - token = g_scanner_get_next_token(scanner); - if (token != G_TOKEN_FLOAT) { - g_printerr("%s", msg); - g_printerr("... aborting\n"); - return NAN; - } - return scanner->value.v_float; ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } - /* * parses a pen style definition (e.g. (color="red" size=3) ) * and stores fields found in GromitStyleDef. @@ -509,14 +432,14 @@ gboolean parse_style(GScanner *scanner, GromitPaintContext *style) gboolean parse_config (GromitData *data) { gboolean status = FALSE; - GromitPaintContext *context = NULL; + GromitPaintContext *context=NULL; GScanner *scanner; GTokenType token; gchar *filename; int file; gchar *name; - // try user config location + /* try user config location */ filename = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.cfg", NULL); if ((file = open(filename, O_RDONLY)) < 0) @@ -525,7 +448,7 @@ gboolean parse_config (GromitData *data) g_print("Using user config %s\n", filename); - // try global config file + /* try global config file */ if (file < 0) { g_free(filename); filename = g_strdup (SYSCONFDIR "/gromit-mpx/gromit-mpx.cfg"); @@ -535,15 +458,14 @@ gboolean parse_config (GromitData *data) g_print("Using system config %s\n", filename); } - // was the last possibility, no use to go on + /* was the last possibility, no use to go on */ if (file < 0) { g_free(filename); - GtkWidget *dialog = gtk_message_dialog_new( - GTK_WINDOW(data->win), - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_WARNING, - GTK_BUTTONS_CLOSE, - _("No usable config file found, falling back to default tools.")); + GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + _("No usable config file found, falling back to default tools.")); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); return FALSE; @@ -572,7 +494,6 @@ gboolean parse_config (GromitData *data) goto cleanup; } - // are there any tool-options? token = g_scanner_cur_token(scanner); if (token == G_TOKEN_LEFT_PAREN) @@ -589,29 +510,20 @@ gboolean parse_config (GromitData *data) goto cleanup; } -<<<<<<< HEAD context = paint_context_new(data, style.type, style.paint_color, style.width, style.arrowsize, style.arrow_type, style.simplify, style.radius, style.maxangle, style.minlen, style.snapdist, style.minwidth, style.maxwidth); g_hash_table_insert (data->tool_config, name, context); -======= - context = paint_context_new(data, - style.type, - style.paint_color, - style.width, - style.arrowsize, - style.arrow_type, - style.minwidth, - style.maxwidth); - g_hash_table_insert(data->tool_config, name, context); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) } else if (token == G_TOKEN_SYMBOL && (scanner->value.v_symbol == HOTKEY_SYMBOL_VALUE || scanner->value.v_symbol == UNDOKEY_SYMBOL_VALUE)) { - // hot key definition + /* + * Hot key definition + */ + gpointer key_type = scanner->value.v_symbol; token = g_scanner_get_next_token(scanner); @@ -650,7 +562,7 @@ gboolean parse_config (GromitData *data) } else { - g_printerr ("Expected name of tool to define or hot key definition\n"); + g_printerr ("Expected name of Tool to define or Hot key definition\n"); goto cleanup; } @@ -671,13 +583,12 @@ gboolean parse_config (GromitData *data) g_hash_table_remove_all(data->tool_config); /* alert user */ - GtkWidget *dialog = gtk_message_dialog_new( - GTK_WINDOW(data->win), - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_WARNING, - GTK_BUTTONS_CLOSE, - _("Failed parsing config file %s, falling back to default tools."), - filename); + GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + _("Failed parsing config file %s, falling back to default tools."), + filename); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); } diff --git a/src/config.h b/src/config.h index 340186e..306ec98 100644 --- a/src/config.h +++ b/src/config.h @@ -33,27 +33,14 @@ Select and parse system or user .cfg file. Returns TRUE if something got parsed successfully, FALSE otherwise. */ -gboolean parse_config (GromitData *data); -<<<<<<< HEAD -int parse_args (int argc, char **argv, GromitData *data); -gchar* parse_name (GScanner *scanner); typedef enum { SYM_ERROR = 0, SYM_SIZE = 1, -======= -int parse_args(int argc, char **argv, GromitData *data); -gchar* parse_name (GScanner *scanner); - -typedef enum { - SYM_ERROR=0, - SYM_SIZE=1, ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) SYM_COLOR, SYM_ARROWSIZE, SYM_ARROWTYPE, SYM_MINSIZE, -<<<<<<< HEAD SYM_MAXSIZE, SYM_MINLEN, SYM_MAXANGLE, @@ -62,22 +49,17 @@ typedef enum { SYM_SNAP } ToolAttribute; -void scanner_init(GScanner *scanner); -gboolean parse_tool(GromitData *data, GScanner *scanner, - GromitPaintContext *style); -gboolean parse_style(GScanner *scanner, GromitPaintContext *style); -gfloat parse_get_float(GScanner *scanner, const gchar *msg); -======= - SYM_MAXSIZE -} ToolAttribute; - void scanner_init(GScanner *scanner); gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *style); +gboolean parse_config (GromitData *data); +gchar* parse_name (GScanner *scanner); +gfloat parse_get_float(GScanner *scanner, const gchar *msg); gboolean parse_style(GScanner *scanner, GromitPaintContext *style); -gfloat parse_float(GScanner *scanner, const gchar *msg); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style); +int parse_args (int argc, char **argv, GromitData *data); + + /* fallback hot key, if not specified on command line or in config file */ #ifndef DEFAULT_HOTKEY diff --git a/src/drawing.c b/src/drawing.c index 4e258e3..1307322 100644 --- a/src/drawing.c +++ b/src/drawing.c @@ -2,17 +2,6 @@ #include #include "drawing.h" #include "main.h" -<<<<<<< HEAD -======= - -typedef struct -{ - gint x; - gint y; - gint width; -} GromitStrokeCoordinate; - ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) void draw_line (GromitData *data, GdkDevice *dev, @@ -116,110 +105,3 @@ void draw_arrow (GromitData *data, data->painted = 1; } -<<<<<<< HEAD -======= - -void coord_list_prepend (GromitData *data, - GdkDevice* dev, - gint x, - gint y, - gint width) -{ - /* get the data for this device */ - GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); - - GromitStrokeCoordinate *point; - - point = g_malloc (sizeof (GromitStrokeCoordinate)); - point->x = x; - point->y = y; - point->width = width; - - devdata->coordlist = g_list_prepend (devdata->coordlist, point); -} - - -void coord_list_free (GromitData *data, - GdkDevice* dev) -{ - /* get the data for this device */ - GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); - - GList *ptr; - ptr = devdata->coordlist; - - while (ptr) - { - g_free (ptr->data); - ptr = ptr->next; - } - - g_list_free (devdata->coordlist); - - devdata->coordlist = NULL; -} - -/* - * for double-ended arrows, two separate calls are required - */ - -gboolean coord_list_get_arrow_param (GromitData *data, - GdkDevice *dev, - gint search_radius, - GromitArrowType arrow_end, - gint *x0, - gint *y0, - gint *ret_width, - gfloat *ret_direction) -{ - gint r2, dist; - gboolean success = FALSE; - GromitStrokeCoordinate *cur_point, *valid_point; - /* get the data for this device */ - GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev); - GList *ptr = devdata->coordlist; - gfloat width; - - valid_point = NULL; - - if (ptr) - { - if (arrow_end == GROMIT_ARROW_START) - ptr = g_list_last(ptr); - cur_point = ptr->data; - *x0 = cur_point->x; - *y0 = cur_point->y; - r2 = search_radius * search_radius; - dist = 0; - - while (ptr && dist < r2) - { - if (arrow_end == GROMIT_ARROW_END) - ptr = ptr->next; - else - ptr = ptr->prev; - if (ptr) - { - cur_point = ptr->data; - dist = (cur_point->x - *x0) * (cur_point->x - *x0) + - (cur_point->y - *y0) * (cur_point->y - *y0); - width = cur_point->width * devdata->cur_context->arrowsize; - if (width * 2 <= dist && - (!valid_point || valid_point->width < cur_point->width)) - valid_point = cur_point; - } - } - - if (valid_point) - { - *ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize, - 2); - *ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x); - success = TRUE; - } - } - - return success; -} - ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) diff --git a/src/drawing.h b/src/drawing.h index 1cb4a38..2abf151 100644 --- a/src/drawing.h +++ b/src/drawing.h @@ -19,19 +19,4 @@ typedef struct void draw_line (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint x2, gint y2); void draw_arrow (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint width, gfloat direction); -<<<<<<< HEAD -======= -gboolean coord_list_get_arrow_param (GromitData *data, - GdkDevice *dev, - gint search_radius, - GromitArrowType arrow_end, - gint *x0, - gint *y0, - gint *ret_width, - gfloat *ret_direction); -void coord_list_prepend (GromitData *data, GdkDevice* dev, gint x, gint y, gint width); -void coord_list_free (GromitData *data, GdkDevice* dev); - - ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) #endif diff --git a/src/main.c b/src/main.c index 186843b..251537a 100644 --- a/src/main.c +++ b/src/main.c @@ -43,14 +43,11 @@ GromitPaintContext *paint_context_new (GromitData *data, guint width, guint arrowsize, GromitArrowType arrowtype, -<<<<<<< HEAD guint simpilfy, guint radius, guint maxangle, guint minlen, guint snapdist, -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) guint minwidth, guint maxwidth) { @@ -98,7 +95,6 @@ void paint_context_print (gchar *name, switch (context->type) { case GROMIT_PEN: -<<<<<<< HEAD g_printerr ("Pen, "); break; case GROMIT_LINE: g_printerr ("Line, "); break; @@ -108,13 +104,6 @@ void paint_context_print (gchar *name, g_printerr ("Smooth, "); break; case GROMIT_ORTHOGONAL: g_printerr ("Orthogonal, "); break; -======= - g_printerr ("Pen, "); break; - case GROMIT_LINE: - g_printerr ("Line, "); break; - case GROMIT_RECT: - g_printerr ("Rect, "); break; ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) case GROMIT_ERASER: g_printerr ("Eraser, "); break; case GROMIT_RECOLOR: @@ -141,7 +130,6 @@ void paint_context_print (gchar *name, break; } } -<<<<<<< HEAD if (context->type == GROMIT_SMOOTH || context->type == GROMIT_ORTHOGONAL) { g_printerr(" simplify: %u, ", context->simplify); @@ -153,15 +141,16 @@ void paint_context_print (gchar *name, g_printerr(" radius: %u, minlen: %u, maxangle: %u ", context->radius, context->minlen, context->maxangle); } -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) - g_printerr ("color: %s\n", gdk_rgba_to_string(context->paint_color)); + gchar *color_string = gdk_rgba_to_string(context->paint_color); + g_printerr ("color: %s\n", color_string); + g_free(color_string); } void paint_context_free (GromitPaintContext *context) { cairo_destroy(context->paint_ctx); + gdk_rgba_free(context->paint_color); g_free (context); } @@ -707,13 +696,6 @@ void setup_main_app (GromitData *data, int argc, char ** argv) data->undo_buffer_size[i] = 0; data->undo_buffer[i] = NULL; } -<<<<<<< HEAD -======= - - // original state for LINE and RECT tool - cairo_surface_destroy(data->aux_backbuffer); - data->aux_backbuffer = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, data->width, data->height); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) /* EVENTS */ gtk_widget_add_events (data->win, GROMIT_WINDOW_EVENTS); @@ -774,7 +756,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_selection_add_target (data->win, GA_CONTROL, GA_UNDO, 8); gtk_selection_add_target (data->win, GA_CONTROL, GA_REDO, 9); gtk_selection_add_target (data->win, GA_CONTROL, GA_LINE, 10); - gtk_selection_add_target (data->win, GA_CONTROL, GA_DEFTOOL, 11); + gtk_selection_add_target (data->win, GA_CONTROL, GA_CHGTOOL, 11); gtk_selection_add_target (data->win, GA_CONTROL, GA_CHGATTR, 12); /* @@ -872,19 +854,11 @@ void setup_main_app (GromitData *data, int argc, char ** argv) data->modified = 0; data->default_pen = -<<<<<<< HEAD paint_context_new (data, GROMIT_PEN, data->red, 7, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, 1, G_MAXUINT); data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75, 0, GROMIT_ARROW_END, 5, 10, 15, 25, 0, 1, G_MAXUINT); -======= - paint_context_new (data, GROMIT_PEN, gdk_rgba_copy(data->red), 7, - 0, GROMIT_ARROW_END, 1, G_MAXUINT); - data->default_eraser = - paint_context_new (data, GROMIT_ERASER, gdk_rgba_copy(data->red), 75, - 0, GROMIT_ARROW_END, 1, G_MAXUINT); ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gdk_event_handler_set ((GdkEventFunc) main_do_event, data, NULL); gtk_key_snooper_install (snoop_key_press, data); @@ -925,10 +899,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv) GtkWidget* sep1_item = gtk_separator_menu_item_new(); GtkWidget* intro_item = gtk_menu_item_new_with_mnemonic(_("_Introduction")); -<<<<<<< HEAD GtkWidget* edit_config_item = gtk_menu_item_new_with_mnemonic(_("_Edit Config")); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GtkWidget* issues_item = gtk_menu_item_new_with_mnemonic(_("_Report Bug / Request Feature")); GtkWidget* support_item = gtk_menu_item_new_with_mnemonic(_("_Support Gromit-MPX")); GtkWidget* about_item = gtk_menu_item_new_with_mnemonic(_("_About")); @@ -951,10 +922,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep1_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), intro_item); -<<<<<<< HEAD gtk_menu_shell_append (GTK_MENU_SHELL (menu), edit_config_item); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gtk_menu_shell_append (GTK_MENU_SHELL (menu), issues_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), support_item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), about_item); @@ -1017,12 +985,9 @@ void setup_main_app (GromitData *data, int argc, char ** argv) g_signal_connect(G_OBJECT (intro_item), "activate", G_CALLBACK (on_intro), data); -<<<<<<< HEAD g_signal_connect(G_OBJECT (edit_config_item), "activate", G_CALLBACK (on_edit_config), data); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) g_signal_connect(G_OBJECT (issues_item), "activate", G_CALLBACK (on_issues), data); @@ -1047,10 +1012,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv) gtk_widget_show (sep1_item); gtk_widget_show (intro_item); -<<<<<<< HEAD gtk_widget_show (edit_config_item); -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) gtk_widget_show (issues_item); gtk_widget_show (support_item); gtk_widget_show (about_item); @@ -1209,7 +1171,7 @@ int main_client (int argc, char **argv, GromitData *data) else { i++; - action = GA_DEFTOOL; + action = GA_CHGTOOL; data->clientdata = argv[i]; } } @@ -1310,7 +1272,7 @@ int main (int argc, char **argv) gtk_selection_owner_set (data->win, GA_DATA, GDK_CURRENT_TIME); gtk_selection_add_target (data->win, GA_DATA, GA_TOGGLEDATA, 1007); gtk_selection_add_target (data->win, GA_DATA, GA_LINEDATA, 1008); - gtk_selection_add_target (data->win, GA_DATA, GA_DEFTOOLDATA, 1009); + gtk_selection_add_target (data->win, GA_DATA, GA_CHGTOOLDATA, 1009); gtk_selection_add_target (data->win, GA_DATA, GA_CHGATTRDATA, 1010); /* Try to get a status message. If there is a response gromit diff --git a/src/main.h b/src/main.h index 79e2afa..138fba7 100644 --- a/src/main.h +++ b/src/main.h @@ -54,13 +54,13 @@ #define GA_RELOAD gdk_atom_intern ("Gromit/reload", FALSE) #define GA_UNDO gdk_atom_intern ("Gromit/undo", FALSE) #define GA_REDO gdk_atom_intern ("Gromit/redo", FALSE) -#define GA_DEFTOOL gdk_atom_intern ("Gromit/deftool", FALSE) +#define GA_CHGTOOL gdk_atom_intern ("Gromit/chgtool", FALSE) #define GA_CHGATTR gdk_atom_intern ("Gromit/chgattr", FALSE) #define GA_DATA gdk_atom_intern ("Gromit/data", FALSE) #define GA_TOGGLEDATA gdk_atom_intern ("Gromit/toggledata", FALSE) #define GA_LINEDATA gdk_atom_intern ("Gromit/linedata", FALSE) -#define GA_DEFTOOLDATA gdk_atom_intern ("Gromit/deftooldata", FALSE) +#define GA_CHGTOOLDATA gdk_atom_intern ("Gromit/chgtooldata", FALSE) #define GA_CHGATTRDATA gdk_atom_intern ("Gromit/chgattrdata", FALSE) #define GROMIT_MAX_UNDO 100 @@ -70,11 +70,8 @@ typedef enum GROMIT_PEN, GROMIT_LINE, GROMIT_RECT, -<<<<<<< HEAD GROMIT_SMOOTH, GROMIT_ORTHOGONAL, -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) GROMIT_ERASER, GROMIT_RECOLOR } GromitPaintType; @@ -203,10 +200,7 @@ void clear_screen (GromitData *data); GromitPaintContext *paint_context_new (GromitData *data, GromitPaintType type, GdkRGBA *fg_color, guint width, guint arrowsize, GromitArrowType arrowtype, -<<<<<<< HEAD guint simpilfy, guint radius, guint maxangle, guint minlen, guint snapdist, -======= ->>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes) guint minwidth, guint maxwidth); void paint_context_free (GromitPaintContext *context); From c96bd5390542631bbe3a84e088c467d6c81dbffd Mon Sep 17 00:00:00 2001 From: Pascal Niklaus Date: Tue, 2 Apr 2024 12:59:10 +0200 Subject: [PATCH 7/7] README, config, main, callbacks: added tool options -A, -T, moved comment in config.h, free default colors when exiting, add missing snap minlen maxangle simplify to callbacks --- README.md | 4 ++-- src/callbacks.c | 22 +++++++++++++++++++++- src/config.h | 2 +- src/main.c | 3 +++ src/main.h | 1 - 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 24d6957..071d8ef 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,11 @@ Usage: gromit-mpx --redo will redo the last undone drawing stroke (or "-y") gromit-mpx --change-tool - will redefine a tool, using the same syntax as in the .cfg file + will redefine a tool, using the same syntax as in the .cfg file (or "-T") e.g. gromit-mpx --change-tool '"default"=RECT(color="yellow" size=2)' gromit-mpx --change-attribute will change one or several attributes of a tool, keeping the others - as they were. This can be used to change e.g. to color or type of a tool + as they were. This can be used to change e.g. to color or type of a tool (or "-A") e.g. gromit-mpx --change-attribute '"default"=(color="cyan")' gromit-mpx --change-attribute '"default"=LINE' gromit-mpx --line diff --git a/src/callbacks.c b/src/callbacks.c index cf792f4..a4796d9 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -759,6 +759,21 @@ void on_mainapp_selection_received (GtkWidget *widget, case SYM_MAXSIZE: context->maxwidth = style.maxwidth; break; + case SYM_MINLEN: + context->minlen = style.minlen; + break; + case SYM_MAXANGLE: + context->maxangle = style.maxangle; + break; + case SYM_RADIUS: + context->radius = style.radius; + break; + case SYM_SIMPLIFY: + context->simplify = style.simplify; + break; + case SYM_SNAP: + context->snapdist = style.snapdist; + break; case SYM_ERROR: break; } @@ -776,12 +791,17 @@ void on_mainapp_selection_received (GtkWidget *widget, if (g_scanner_cur_token(scanner) != G_TOKEN_EOF) goto cleanup; context->type = style.type; - *context->paint_color = *style.paint_color; context->width = style.width; context->arrowsize = style.arrowsize; context->arrow_type = style.arrow_type; context->minwidth = style.minwidth; context->maxwidth = style.maxwidth; + context->radius = style.radius; + context->minlen = style.minlen; + context->maxangle = style.maxangle; + context->simplify = style.simplify; + context->snapdist = style.snapdist; + *context->paint_color = *style.paint_color; cairo_set_source_rgba(context->paint_ctx, style.paint_color->red, diff --git a/src/config.h b/src/config.h index 306ec98..c3a2bb2 100644 --- a/src/config.h +++ b/src/config.h @@ -33,6 +33,7 @@ Select and parse system or user .cfg file. Returns TRUE if something got parsed successfully, FALSE otherwise. */ +gboolean parse_config (GromitData *data); typedef enum { SYM_ERROR = 0, @@ -51,7 +52,6 @@ typedef enum { void scanner_init(GScanner *scanner); gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *style); -gboolean parse_config (GromitData *data); gchar* parse_name (GScanner *scanner); gfloat parse_get_float(GScanner *scanner, const gchar *msg); gboolean parse_style(GScanner *scanner, GromitPaintContext *style); diff --git a/src/main.c b/src/main.c index 251537a..4b2a667 100644 --- a/src/main.c +++ b/src/main.c @@ -1293,6 +1293,9 @@ int main (int argc, char **argv) gtk_main (); shutdown_input_devices(data); write_keyfile(data); // save keyfile config + g_free(data->red); + g_free(data->white); + g_free(data->black); g_free (data); return 0; } diff --git a/src/main.h b/src/main.h index 138fba7..9ccee8d 100644 --- a/src/main.h +++ b/src/main.h @@ -173,7 +173,6 @@ typedef struct size_t undo_temp_used; gint undo_head, undo_depth, redo_depth; - gboolean show_intro_on_startup; } GromitData;