Skip to content

Commit 074531a

Browse files
config, callbacks: add option to change tool definitions and individual tool attributes
1 parent e1ceeec commit 074531a

File tree

10 files changed

+532
-518
lines changed

10 files changed

+532
-518
lines changed

src/callbacks.c

+168-62
Large diffs are not rendered by default.

src/callbacks.h

+3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ void on_about(GtkMenuItem *menuitem,
128128
void on_intro(GtkMenuItem *menuitem,
129129
gpointer user_data);
130130

131+
<<<<<<< HEAD
131132
void on_edit_config(GtkMenuItem *menuitem,
132133
gpointer user_data);
133134

135+
=======
136+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
134137
void on_issues(GtkMenuItem *menuitem,
135138
gpointer user_data);
136139

src/config.c

+112-28
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <errno.h>
3030

3131
#include "config.h"
32-
#include "parser.h"
32+
#include "math.h"
3333
#include "main.h"
3434
#include "math.h"
3535
#include "build-config.h"
@@ -50,6 +50,10 @@ static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4;
5050
/*
5151
* initialize GScanner for the parsing of tool definitions
5252
*/
53+
<<<<<<< HEAD
54+
=======
55+
56+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
5357
void scanner_init(GScanner *scanner)
5458
{
5559
scanner->config->case_sensitive = 0;
@@ -59,6 +63,7 @@ void scanner_init(GScanner *scanner)
5963
scanner->config->numbers_2_int = 1;
6064
scanner->config->int_2_float = 1;
6165

66+
<<<<<<< HEAD
6267
g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN);
6368
g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE);
6469
g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT);
@@ -68,6 +73,15 @@ void scanner_init(GScanner *scanner)
6873
g_scanner_scope_add_symbol (scanner, 0, "RECOLOR", (gpointer) GROMIT_RECOLOR);
6974
g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE);
7075
g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE);
76+
=======
77+
g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN);
78+
g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE);
79+
g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT);
80+
g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER);
81+
g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR);
82+
g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE);
83+
g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE);
84+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
7185

7286
g_scanner_scope_add_symbol (scanner, 1, "BUTTON1", (gpointer) 1);
7387
g_scanner_scope_add_symbol (scanner, 1, "BUTTON2", (gpointer) 2);
@@ -85,11 +99,14 @@ void scanner_init(GScanner *scanner)
8599
g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) SYM_ARROWTYPE);
86100
g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) SYM_MINSIZE);
87101
g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) SYM_MAXSIZE);
102+
<<<<<<< HEAD
88103
g_scanner_scope_add_symbol (scanner, 2, "radius", (gpointer) SYM_RADIUS);
89104
g_scanner_scope_add_symbol (scanner, 2, "maxangle", (gpointer) SYM_MAXANGLE);
90105
g_scanner_scope_add_symbol (scanner, 2, "minlen", (gpointer) SYM_MINLEN);
91106
g_scanner_scope_add_symbol (scanner, 2, "simplify", (gpointer) SYM_SIMPLIFY);
92107
g_scanner_scope_add_symbol (scanner, 2, "snap", (gpointer) SYM_SNAP);
108+
=======
109+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
93110

94111
g_scanner_set_scope (scanner, 0);
95112
scanner->config->scope_0_fallback = 0;
@@ -121,10 +138,13 @@ gchar* parse_name (GScanner *scanner)
121138
name = g_strndup (scanner->value.v_string, len + 3);
122139
token = g_scanner_get_next_token (scanner);
123140

141+
<<<<<<< HEAD
124142
/*
125143
* Are there any options to limit the scope of the definition?
126144
*/
127145

146+
=======
147+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
128148
if (token == G_TOKEN_LEFT_BRACE)
129149
{
130150
g_scanner_set_scope (scanner, 1);
@@ -193,11 +213,14 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty
193213
style->width = 7;
194214
style->arrowsize = 0;
195215
style->arrow_type = GROMIT_ARROW_END;
216+
<<<<<<< HEAD
196217
style->radius = 10;
197218
style->minlen = style->radius * 5 / 2;
198219
style->maxangle = 15;
199220
style->simplify = 10;
200221
style->snapdist = 0;
222+
=======
223+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
201224
style->minwidth = 1;
202225
style->maxwidth = G_MAXUINT;
203226

@@ -226,11 +249,14 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty
226249
style->width = context->width;
227250
style->arrowsize = context->arrowsize;
228251
style->arrow_type = context->arrow_type;
252+
<<<<<<< HEAD
229253
style->radius = context->radius;
230254
style->minlen = context->minlen;
231255
style->maxangle = context->maxangle;
232256
style->simplify = context->simplify;
233257
style->snapdist = context->snapdist;
258+
=======
259+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
234260
style->minwidth = context->minwidth;
235261
style->maxwidth = context->maxwidth;
236262
*style->paint_color = *context->paint_color;
@@ -247,7 +273,11 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty
247273
}
248274
else
249275
{
276+
<<<<<<< HEAD
250277
g_printerr ("Expected tool definition or name of template tool\n");
278+
=======
279+
g_printerr ("Expected Tool-definition or name of template tool\n");
280+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
251281
goto cleanup;
252282
}
253283
return TRUE;
@@ -256,6 +286,10 @@ gboolean parse_tool(GromitData *data, GScanner *scanner, GromitPaintContext *sty
256286
if (color_allocated)
257287
{
258288
g_free(style->paint_color);
289+
<<<<<<< HEAD
290+
=======
291+
g_printerr("parse_tool: free color @ %p\n", style->paint_color);
292+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
259293
style->paint_color = NULL;
260294
}
261295
return FALSE;
@@ -299,7 +333,11 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style)
299333
{
300334
gfloat v = parse_float(scanner, "Missing arrowsize");
301335
if (isnan(v)) return SYM_ERROR;
336+
<<<<<<< HEAD
302337
style->arrowsize = v;
338+
=======
339+
style->arrowsize = v + 0.5;
340+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
303341
}
304342
else if (id == SYM_ARROWTYPE)
305343
{
@@ -334,6 +372,7 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style)
334372
return SYM_ERROR;
335373
}
336374
}
375+
<<<<<<< HEAD
337376
else if ((intptr_t) scanner->value.v_symbol == SYM_RADIUS)
338377
{
339378
gfloat v = parse_get_float(scanner, "Missing radius (float)");
@@ -375,6 +414,19 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style)
375414
gfloat v = parse_float(scanner, "Missing maxsize");
376415
if (isnan(v)) return SYM_ERROR;
377416
style->maxwidth = v + 0.5;
417+
=======
418+
else if (id == SYM_MINSIZE)
419+
{
420+
gfloat v = parse_float(scanner, "Missing minsize");
421+
if (isnan(v)) return SYM_ERROR;
422+
style->minwidth = v + 0.5;
423+
}
424+
else if (id == SYM_MAXSIZE)
425+
{
426+
gfloat v = parse_float(scanner, "Missing maxsize");
427+
if (isnan(v)) return SYM_ERROR;
428+
style->maxwidth = v + 0.5;
429+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
378430
}
379431
else
380432
{
@@ -385,6 +437,7 @@ ToolAttribute parse_attribute(GScanner *scanner, GromitPaintContext *style)
385437
}
386438

387439
/*
440+
<<<<<<< HEAD
388441
* get "=VALUE", where VALUE is a float
389442
* returns NAN is an error occurs
390443
*/
@@ -404,6 +457,24 @@ gfloat parse_get_float(GScanner *scanner, const gchar *msg)
404457
return NAN;
405458
}
406459
return scanner->value.v_float;
460+
=======
461+
* parse "=<VALUE>", with value being a float
462+
* return <VALUE> as gfloat, or NAN on error
463+
*/
464+
gfloat parse_float(GScanner *scanner, const gchar *msg) {
465+
GTokenType token = g_scanner_get_next_token(scanner);
466+
if (token != G_TOKEN_EQUAL_SIGN) {
467+
g_printerr("Missing \"=\"... aborting\n");
468+
return NAN;
469+
}
470+
token = g_scanner_get_next_token(scanner);
471+
if (token != G_TOKEN_FLOAT) {
472+
g_printerr("%s", msg);
473+
g_printerr("... aborting\n");
474+
return NAN;
475+
}
476+
return scanner->value.v_float;
477+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
407478
}
408479

409480
/*
@@ -438,14 +509,14 @@ gboolean parse_style(GScanner *scanner, GromitPaintContext *style)
438509
gboolean parse_config (GromitData *data)
439510
{
440511
gboolean status = FALSE;
441-
GromitPaintContext *context=NULL;
512+
GromitPaintContext *context = NULL;
442513
GScanner *scanner;
443514
GTokenType token;
444515
gchar *filename;
445516
int file;
446517
gchar *name;
447518

448-
/* try user config location */
519+
// try user config location
449520
filename = g_strjoin (G_DIR_SEPARATOR_S,
450521
g_get_user_config_dir(), "gromit-mpx.cfg", NULL);
451522
if ((file = open(filename, O_RDONLY)) < 0)
@@ -454,7 +525,7 @@ gboolean parse_config (GromitData *data)
454525
g_print("Using user config %s\n", filename);
455526

456527

457-
/* try global config file */
528+
// try global config file
458529
if (file < 0) {
459530
g_free(filename);
460531
filename = g_strdup (SYSCONFDIR "/gromit-mpx/gromit-mpx.cfg");
@@ -464,20 +535,21 @@ gboolean parse_config (GromitData *data)
464535
g_print("Using system config %s\n", filename);
465536
}
466537

467-
/* was the last possibility, no use to go on */
538+
// was the last possibility, no use to go on
468539
if (file < 0) {
469540
g_free(filename);
470-
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win),
471-
GTK_DIALOG_DESTROY_WITH_PARENT,
472-
GTK_MESSAGE_WARNING,
473-
GTK_BUTTONS_CLOSE,
474-
_("No usable config file found, falling back to default tools."));
541+
GtkWidget *dialog = gtk_message_dialog_new(
542+
GTK_WINDOW(data->win),
543+
GTK_DIALOG_DESTROY_WITH_PARENT,
544+
GTK_MESSAGE_WARNING,
545+
GTK_BUTTONS_CLOSE,
546+
_("No usable config file found, falling back to default tools."));
475547
gtk_dialog_run (GTK_DIALOG (dialog));
476548
gtk_widget_destroy (dialog);
477549
return FALSE;
478550
}
479551

480-
scanner = g_scanner_new(NULL);
552+
scanner = g_scanner_new (NULL);
481553
scanner_init(scanner);
482554
scanner->input_name = filename;
483555
g_scanner_input_file (scanner, file);
@@ -495,13 +567,14 @@ gboolean parse_config (GromitData *data)
495567
goto cleanup;
496568

497569
if (!parse_tool(data, scanner, &style))
498-
goto cleanup;
570+
{
571+
g_printerr("parse tool failed\n");
572+
goto cleanup;
573+
}
499574

500-
token = g_scanner_cur_token(scanner);
501-
//
502-
/* Are there any tool-options?
503-
*/
504575

576+
// are there any tool-options?
577+
token = g_scanner_cur_token(scanner);
505578
if (token == G_TOKEN_LEFT_PAREN)
506579
{
507580
if (! parse_style(scanner, &style))
@@ -516,20 +589,29 @@ gboolean parse_config (GromitData *data)
516589
goto cleanup;
517590
}
518591

592+
<<<<<<< HEAD
519593
context = paint_context_new(data, style.type, style.paint_color, style.width,
520594
style.arrowsize, style.arrow_type,
521595
style.simplify, style.radius, style.maxangle, style.minlen, style.snapdist,
522596
style.minwidth, style.maxwidth);
523597
g_hash_table_insert (data->tool_config, name, context);
598+
=======
599+
context = paint_context_new(data,
600+
style.type,
601+
style.paint_color,
602+
style.width,
603+
style.arrowsize,
604+
style.arrow_type,
605+
style.minwidth,
606+
style.maxwidth);
607+
g_hash_table_insert(data->tool_config, name, context);
608+
>>>>>>> 5385d7b (config, callbacks: add option to change tool definitions and individual tool attributes)
524609
}
525610
else if (token == G_TOKEN_SYMBOL &&
526611
(scanner->value.v_symbol == HOTKEY_SYMBOL_VALUE ||
527612
scanner->value.v_symbol == UNDOKEY_SYMBOL_VALUE))
528613
{
529-
/*
530-
* Hot key definition
531-
*/
532-
614+
// hot key definition
533615
gpointer key_type = scanner->value.v_symbol;
534616
token = g_scanner_get_next_token(scanner);
535617

@@ -568,7 +650,7 @@ gboolean parse_config (GromitData *data)
568650
}
569651
else
570652
{
571-
g_printerr ("Expected name of Tool to define or Hot key definition\n");
653+
g_printerr ("Expected name of tool to define or hot key definition\n");
572654
goto cleanup;
573655
}
574656

@@ -589,12 +671,13 @@ gboolean parse_config (GromitData *data)
589671
g_hash_table_remove_all(data->tool_config);
590672

591673
/* alert user */
592-
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(data->win),
593-
GTK_DIALOG_DESTROY_WITH_PARENT,
594-
GTK_MESSAGE_WARNING,
595-
GTK_BUTTONS_CLOSE,
596-
_("Failed parsing config file %s, falling back to default tools."),
597-
filename);
674+
GtkWidget *dialog = gtk_message_dialog_new(
675+
GTK_WINDOW(data->win),
676+
GTK_DIALOG_DESTROY_WITH_PARENT,
677+
GTK_MESSAGE_WARNING,
678+
GTK_BUTTONS_CLOSE,
679+
_("Failed parsing config file %s, falling back to default tools."),
680+
filename);
598681
gtk_dialog_run (GTK_DIALOG (dialog));
599682
gtk_widget_destroy (dialog);
600683
}
@@ -730,6 +813,7 @@ void read_keyfile(GromitData *data)
730813
{
731814
gchar *filename = g_strjoin (G_DIR_SEPARATOR_S,
732815
g_get_user_config_dir(), "gromit-mpx.ini", NULL);
816+
733817
/*
734818
set defaults
735819
*/
@@ -755,7 +839,7 @@ void read_keyfile(GromitData *data)
755839
if(data->opacity == 0)
756840
data->opacity = DEFAULT_OPACITY;
757841

758-
cleanup:
842+
cleanup:
759843
g_free(filename);
760844
g_key_file_free(key_file);
761845
}

0 commit comments

Comments
 (0)