作図ソフト dia の改良版
Revision | 6d96da135d83ad049c7047e5e907df359b1c83f4 (tree) |
---|---|
Time | 2004-04-01 22:49:52 |
Author | Lars Clausen <lclausen@src....> |
Commiter | Lars Clausen |
More persistence. Yay!
@@ -1,3 +1,13 @@ | ||
1 | +2004-04-01 Lars Clausen <lars@raeder.dk> | |
2 | + | |
3 | + * app/preferences.c: All preferences are now | |
4 | + handled by persistence, old prefs load and store code is gone. | |
5 | + | |
6 | + * lib/persistence.[ch]: Added string and color persistents. This is | |
7 | + a different string from the one used in the printer dialog, that | |
8 | + one has a GtkEntry attached -- may want to make that one an | |
9 | + encapsulation of this one or something. | |
10 | + | |
1 | 11 | 2004-03-31 Lars Clausen <lars@raeder.dk> |
2 | 12 | |
3 | 13 | * app/app_procs.c: Do persistence before prefs. Prefs are to be |
@@ -190,51 +190,6 @@ DiaPrefData prefs_data[] = | ||
190 | 190 | |
191 | 191 | #define NUM_PREFS_DATA (sizeof(prefs_data)/sizeof(DiaPrefData)) |
192 | 192 | |
193 | -static const GScannerConfig dia_prefs_scanner_config = | |
194 | -{ | |
195 | - ( | |
196 | - " \t\n" | |
197 | - ) /* cset_skip_characters */, | |
198 | - ( | |
199 | - G_CSET_a_2_z | |
200 | - "_" | |
201 | - G_CSET_A_2_Z | |
202 | - ) /* cset_identifier_first */, | |
203 | - ( | |
204 | - G_CSET_a_2_z | |
205 | - "_-0123456789" | |
206 | - G_CSET_A_2_Z | |
207 | - ) /* cset_identifier_nth */, | |
208 | - ( "#\n" ) /* cpair_comment_single */, | |
209 | - | |
210 | - TRUE /* case_sensitive */, | |
211 | - | |
212 | - FALSE /* skip_comment_multi */, | |
213 | - TRUE /* skip_comment_single */, | |
214 | - FALSE /* scan_comment_multi */, | |
215 | - TRUE /* scan_identifier */, | |
216 | - TRUE /* scan_identifier_1char */, | |
217 | - FALSE /* scan_identifier_NULL */, | |
218 | - TRUE /* scan_symbols */, | |
219 | - FALSE /* scan_binary */, | |
220 | - FALSE /* scan_octal */, | |
221 | - TRUE /* scan_float */, | |
222 | - TRUE /* scan_hex */, | |
223 | - FALSE /* scan_hex_dollar */, | |
224 | - FALSE /* scan_string_sq */, | |
225 | - TRUE /* scan_string_dq */, | |
226 | - TRUE /* numbers_2_int */, | |
227 | - FALSE /* int_2_float */, | |
228 | - FALSE /* identifier_2_string */, | |
229 | - TRUE /* char_2_token */, | |
230 | - FALSE /* symbol_2_token */, | |
231 | - FALSE /* scope_0_fallback */, | |
232 | -}; | |
233 | - | |
234 | -typedef enum { | |
235 | - DIA_PREFS_TOKEN_BOOLEAN = G_TOKEN_LAST | |
236 | -} DiaPrefsTokenType; | |
237 | - | |
238 | 193 | static void prefs_create_dialog(void); |
239 | 194 | static void prefs_set_value_in_widget(GtkWidget * widget, DiaPrefData *data, char *ptr); |
240 | 195 | static void prefs_get_value_from_widget(GtkWidget * widget, DiaPrefData *data, char *ptr); |
@@ -271,20 +226,27 @@ prefs_set_defaults(void) | ||
271 | 226 | |
272 | 227 | switch (prefs_data[i].type) { |
273 | 228 | case PREF_BOOLEAN: |
229 | + *(int *)ptr = *(int *)prefs_data[i].default_value; | |
230 | + *(int *)ptr = persistence_register_boolean(prefs_data[i].name, *(int *)ptr); | |
231 | + break; | |
274 | 232 | case PREF_INT: |
275 | 233 | case PREF_UINT: |
276 | 234 | *(int *)ptr = *(int *)prefs_data[i].default_value; |
235 | + *(int *)ptr = persistence_register_integer(prefs_data[i].name, *(int *)ptr); | |
277 | 236 | break; |
278 | 237 | case PREF_REAL: |
279 | 238 | case PREF_UREAL: |
280 | 239 | *(real *)ptr = *(real *)prefs_data[i].default_value; |
240 | + *(real *)ptr = persistence_register_real(prefs_data[i].name, *(real *)ptr); | |
281 | 241 | break; |
282 | 242 | case PREF_COLOUR: |
283 | 243 | *(Color *)ptr = *(Color *)prefs_data[i].default_value; |
244 | + *(Color *)ptr = *persistence_register_color(prefs_data[i].name, (Color *)ptr); | |
284 | 245 | break; |
285 | 246 | case PREF_CHOICE: |
286 | 247 | case PREF_STRING: |
287 | 248 | *(gchar **)ptr = *(gchar **)prefs_data[i].default_value; |
249 | + *(gchar **)ptr = persistence_register_string(prefs_data[i].name, *(gchar **)ptr); | |
288 | 250 | break; |
289 | 251 | case PREF_NONE: |
290 | 252 | case PREF_END_GROUP: |
@@ -298,213 +260,46 @@ prefs_save(void) | ||
298 | 260 | { |
299 | 261 | int i; |
300 | 262 | char *ptr; |
301 | - gchar *filename; | |
302 | - char *old_locale; | |
303 | - FILE *file; | |
304 | - | |
305 | - filename = dia_config_filename("diarc"); | |
306 | - | |
307 | - file = fopen(filename, "w"); | |
308 | - | |
309 | - if (!file) { | |
310 | - message_error(_("Could not open `%s' for writing"), filename); | |
311 | - g_free(filename); | |
312 | - return; | |
313 | - } | |
314 | - | |
315 | - g_free(filename); | |
316 | - | |
317 | - old_locale = setlocale(LC_NUMERIC, "C"); | |
318 | - fprintf(file, "# Note: This file is automatically generated by Dia\n"); | |
319 | 263 | for (i=0;i<NUM_PREFS_DATA;i++) { |
320 | 264 | if ((prefs_data[i].type == PREF_NONE) || (prefs_data[i].type == PREF_END_GROUP)) |
321 | 265 | continue; |
322 | 266 | |
323 | - fprintf(file, "%s=", prefs_data[i].name); | |
324 | - | |
325 | 267 | ptr = (char *)&prefs + prefs_data[i].offset; |
326 | 268 | |
327 | 269 | switch (prefs_data[i].type) { |
328 | 270 | case PREF_BOOLEAN: |
329 | - fprintf(file, (*(int *)ptr)?"true\n":"false\n"); | |
330 | 271 | persistence_set_boolean(prefs_data[i].name, *(gint *)ptr); |
331 | 272 | break; |
332 | 273 | case PREF_INT: |
333 | 274 | case PREF_UINT: |
334 | - fprintf(file, "%d\n", *(int *)ptr); | |
335 | 275 | persistence_set_integer(prefs_data[i].name, *(gint *)ptr); |
336 | 276 | break; |
337 | 277 | case PREF_REAL: |
338 | 278 | case PREF_UREAL: |
339 | 279 | |
340 | - fprintf(file, "%f\n", (double) *(real *)ptr); | |
341 | 280 | persistence_set_real(prefs_data[i].name, *(real *)ptr); |
342 | 281 | break; |
343 | 282 | case PREF_COLOUR: |
344 | - fprintf(file, "%f %f %f\n", (double) ((Color *)ptr)->red, | |
345 | - (double) ((Color *)ptr)->green, (double) ((Color *)ptr)->blue); | |
283 | + persistence_set_color(prefs_data[i].name, (Color *)ptr); | |
346 | 284 | break; |
347 | 285 | case PREF_CHOICE: |
348 | 286 | case PREF_STRING: |
349 | - fprintf(file, "\"%s\"\n", *(gchar **)ptr); | |
287 | + persistence_set_string(prefs_data[i].name, *(gchar **)ptr); | |
350 | 288 | break; |
351 | 289 | case PREF_NONE: |
352 | 290 | case PREF_END_GROUP: |
353 | 291 | break; |
354 | 292 | } |
355 | 293 | } |
356 | - setlocale(LC_NUMERIC, old_locale); | |
357 | - fclose(file); | |
358 | 294 | } |
359 | 295 | |
360 | 296 | |
361 | -static guint | |
362 | -prefs_parse_line(GScanner *scanner) | |
363 | -{ | |
364 | - guint token; | |
365 | - int symbol_nr; | |
366 | - char *ptr; | |
367 | - | |
368 | - token = g_scanner_get_next_token(scanner); | |
369 | - if (token != G_TOKEN_SYMBOL) | |
370 | - return G_TOKEN_SYMBOL; | |
371 | - | |
372 | - symbol_nr = GPOINTER_TO_INT(scanner->value.v_symbol); | |
373 | - | |
374 | - token = g_scanner_get_next_token(scanner); | |
375 | - if (token != G_TOKEN_EQUAL_SIGN) | |
376 | - return G_TOKEN_EQUAL_SIGN; | |
377 | - | |
378 | - token = g_scanner_get_next_token(scanner); | |
379 | - | |
380 | - ptr = (unsigned char *)&prefs + prefs_data[symbol_nr].offset; | |
381 | - | |
382 | - switch (prefs_data[symbol_nr].type) { | |
383 | - case PREF_BOOLEAN: | |
384 | - if (token != G_TOKEN_IDENTIFIER) | |
385 | - return G_TOKEN_IDENTIFIER; | |
386 | - | |
387 | - if (strcmp(scanner->value.v_string, "true")==0) | |
388 | - *(int *)ptr = 1; | |
389 | - else | |
390 | - *(int *)ptr = 0; | |
391 | - persistence_register_boolean(prefs_data[symbol_nr].name, *(int *)ptr); | |
392 | - break; | |
393 | - | |
394 | - case PREF_INT: | |
395 | - case PREF_UINT: | |
396 | - if (token != G_TOKEN_INT) | |
397 | - return G_TOKEN_INT; | |
398 | - | |
399 | - *(int *)ptr = scanner->value.v_int; | |
400 | - persistence_register_integer(prefs_data[symbol_nr].name, *(int *)ptr); | |
401 | - break; | |
402 | - | |
403 | - case PREF_REAL: | |
404 | - case PREF_UREAL: | |
405 | - if (token != G_TOKEN_FLOAT) | |
406 | - return G_TOKEN_FLOAT; | |
407 | - | |
408 | - *(real *)ptr = scanner->value.v_float; | |
409 | - printf("Real is %f\n", scanner->value.v_float); | |
410 | - persistence_register_real(prefs_data[symbol_nr].name, scanner->value.v_float); | |
411 | - break; | |
412 | - case PREF_COLOUR: | |
413 | - if (token != G_TOKEN_FLOAT) | |
414 | - return G_TOKEN_FLOAT; | |
415 | - ((Color *)ptr)->red = scanner->value.v_float; | |
416 | - | |
417 | - token = g_scanner_get_next_token(scanner); | |
418 | - if (token != G_TOKEN_FLOAT) | |
419 | - return G_TOKEN_FLOAT; | |
420 | - ((Color *)ptr)->green = scanner->value.v_float; | |
421 | - | |
422 | - token = g_scanner_get_next_token(scanner); | |
423 | - if (token != G_TOKEN_FLOAT) | |
424 | - return G_TOKEN_FLOAT; | |
425 | - ((Color *)ptr)->blue = scanner->value.v_float; | |
426 | - | |
427 | - break; | |
428 | - case PREF_CHOICE: | |
429 | - case PREF_STRING: | |
430 | - if (token != G_TOKEN_STRING) | |
431 | - return G_TOKEN_STRING; | |
432 | - | |
433 | - *(char **)ptr = g_strdup(scanner->value.v_string); | |
434 | - break; | |
435 | - case PREF_NONE: | |
436 | - case PREF_END_GROUP: | |
437 | - break; | |
438 | - } | |
439 | - | |
440 | - return G_TOKEN_NONE; | |
441 | -} | |
442 | - | |
443 | 297 | |
444 | 298 | void |
445 | 299 | prefs_load(void) |
446 | 300 | { |
447 | - int i; | |
448 | - gchar *filename; | |
449 | - int fd; | |
450 | - GScanner *scanner; | |
451 | - guint expected_token; | |
452 | - | |
453 | - filename = dia_config_filename("diarc"); | |
454 | - | |
455 | - fd = open(filename, O_RDONLY); | |
456 | - | |
457 | - if (fd < 0) { | |
458 | - const gchar *homedir = g_get_home_dir(); | |
459 | - | |
460 | - g_free(filename); | |
461 | - filename = g_strconcat(homedir, G_DIR_SEPARATOR_S ".diarc", NULL); | |
462 | - fd = open(filename, O_RDONLY); | |
463 | - } | |
464 | - g_free(filename); | |
465 | - | |
466 | 301 | prefs_set_defaults(); |
467 | 302 | |
468 | - if (fd < 0) { | |
469 | - return; | |
470 | - } | |
471 | - | |
472 | - scanner = g_scanner_new ((GScannerConfig *) &dia_prefs_scanner_config); | |
473 | - | |
474 | - g_scanner_input_file (scanner, fd); | |
475 | - | |
476 | - scanner->input_name = filename; | |
477 | - for (i = 0; i < NUM_PREFS_DATA; i++) | |
478 | - if (prefs_data[i].name != NULL) { | |
479 | - g_scanner_add_symbol(scanner, prefs_data[i].name, | |
480 | - GINT_TO_POINTER(i)); | |
481 | - } | |
482 | - while (1) { | |
483 | - if (g_scanner_peek_next_token(scanner) == G_TOKEN_EOF) { | |
484 | - break; | |
485 | - } | |
486 | - | |
487 | - expected_token = prefs_parse_line(scanner); | |
488 | - | |
489 | - if (expected_token != G_TOKEN_NONE) { | |
490 | - gchar *symbol_name; | |
491 | - gchar *msg; | |
492 | - | |
493 | - msg = NULL; | |
494 | - symbol_name = NULL; | |
495 | - g_scanner_unexp_token (scanner, | |
496 | - expected_token, | |
497 | - NULL, | |
498 | - "keyword", | |
499 | - symbol_name, | |
500 | - msg, | |
501 | - TRUE); | |
502 | - } | |
503 | - } | |
504 | - | |
505 | - g_scanner_destroy (scanner); | |
506 | - | |
507 | - close(fd); | |
508 | 303 | render_bounding_boxes = prefs.render_bounding_boxes; |
509 | 304 | } |
510 | 305 |
@@ -561,8 +356,7 @@ prefs_get_value_from_widget(GtkWidget * widget, DiaPrefData *data, | ||
561 | 356 | break; |
562 | 357 | case PREF_INT: |
563 | 358 | case PREF_UINT: |
564 | - *((int *)ptr) = | |
565 | - gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)); | |
359 | + *((int *)ptr) = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)); | |
566 | 360 | break; |
567 | 361 | case PREF_REAL: |
568 | 362 | case PREF_UREAL: |
@@ -579,8 +373,7 @@ prefs_get_value_from_widget(GtkWidget * widget, DiaPrefData *data, | ||
579 | 373 | break; |
580 | 374 | } |
581 | 375 | case PREF_STRING: |
582 | - *((gchar **)ptr) = (gchar *) | |
583 | - gtk_entry_get_text(GTK_ENTRY(widget)); | |
376 | + *((gchar **)ptr) = (gchar *)gtk_entry_get_text(GTK_ENTRY(widget)); | |
584 | 377 | break; |
585 | 378 | case PREF_NONE: |
586 | 379 | case PREF_END_GROUP: |
@@ -40,9 +40,10 @@ | ||
40 | 40 | |
41 | 41 | /* Hash table from window role (string) to PersistentWindow structure. |
42 | 42 | */ |
43 | -static GHashTable *persistent_windows, *persistent_strings, *persistent_lists; | |
43 | +static GHashTable *persistent_windows, *persistent_entrystrings, *persistent_lists; | |
44 | 44 | static GHashTable *persistent_integers, *persistent_reals; |
45 | -static GHashTable *persistent_booleans, *persistent_enums; | |
45 | +static GHashTable *persistent_booleans, *persistent_strings; | |
46 | +static GHashTable *persistent_colors; | |
46 | 47 | |
47 | 48 | /* *********************** LOADING FUNCTIONS *********************** */ |
48 | 49 |
@@ -75,7 +76,7 @@ persistence_load_window(gchar *role, xmlNodePtr node) | ||
75 | 76 | |
76 | 77 | /** Load a persistent string into the strings hashtable */ |
77 | 78 | static void |
78 | -persistence_load_string(gchar *role, xmlNodePtr node) | |
79 | +persistence_load_entrystring(gchar *role, xmlNodePtr node) | |
79 | 80 | { |
80 | 81 | AttributeNode attr; |
81 | 82 | gchar *string = NULL; |
@@ -88,7 +89,7 @@ persistence_load_string(gchar *role, xmlNodePtr node) | ||
88 | 89 | return; |
89 | 90 | |
90 | 91 | if (string != NULL) |
91 | - g_hash_table_insert(persistent_strings, role, string); | |
92 | + g_hash_table_insert(persistent_entrystrings, role, string); | |
92 | 93 | } |
93 | 94 | |
94 | 95 | static void |
@@ -182,6 +183,45 @@ persistence_load_boolean(gchar *role, xmlNodePtr node) | ||
182 | 183 | printf("Boolean %s registered before loading persistence!\n", role); |
183 | 184 | } |
184 | 185 | |
186 | +static void | |
187 | +persistence_load_string(gchar *role, xmlNodePtr node) | |
188 | +{ | |
189 | + AttributeNode attr; | |
190 | + gchar *stringval; | |
191 | + | |
192 | + /* Find the contents? */ | |
193 | + attr = composite_find_attribute(node, "stringvalue"); | |
194 | + if (attr != NULL) { | |
195 | + stringval = data_string(attribute_first_data(attr)); | |
196 | + } else | |
197 | + return; | |
198 | + | |
199 | + if (g_hash_table_lookup(persistent_strings, role) == NULL) | |
200 | + g_hash_table_insert(persistent_strings, role, stringval); | |
201 | + else | |
202 | + printf("String %s registered before loading persistence!\n", role); | |
203 | +} | |
204 | + | |
205 | +static void | |
206 | +persistence_load_color(gchar *role, xmlNodePtr node) | |
207 | +{ | |
208 | + AttributeNode attr; | |
209 | + Color *colorval; | |
210 | + | |
211 | + /* Find the contents? */ | |
212 | + attr = composite_find_attribute(node, "colorvalue"); | |
213 | + if (attr != NULL) { | |
214 | + colorval = g_new(Color, 1); | |
215 | + data_color(attribute_first_data(attr), colorval); | |
216 | + } else | |
217 | + return; | |
218 | + | |
219 | + if (g_hash_table_lookup(persistent_colors, role) == NULL) | |
220 | + g_hash_table_insert(persistent_colors, role, colorval); | |
221 | + else | |
222 | + printf("Color %s registered before loading persistence!\n", role); | |
223 | +} | |
224 | + | |
185 | 225 | static xmlNodePtr |
186 | 226 | find_node_named (xmlNodePtr p, const char *name) |
187 | 227 | { |
@@ -229,17 +269,19 @@ static void | ||
229 | 269 | persistence_init() |
230 | 270 | { |
231 | 271 | persistence_set_type_handler("window", persistence_load_window); |
232 | - persistence_set_type_handler("entrystring", persistence_load_string); | |
272 | + persistence_set_type_handler("entrystring", persistence_load_entrystring); | |
233 | 273 | persistence_set_type_handler("list", persistence_load_list); |
234 | 274 | persistence_set_type_handler("integer", persistence_load_integer); |
235 | 275 | persistence_set_type_handler("real", persistence_load_real); |
236 | 276 | persistence_set_type_handler("boolean", persistence_load_boolean); |
277 | + persistence_set_type_handler("string", persistence_load_string); | |
278 | + persistence_set_type_handler("color", persistence_load_color); | |
237 | 279 | |
238 | 280 | if (persistent_windows == NULL) { |
239 | 281 | persistent_windows = g_hash_table_new(g_str_hash, g_str_equal); |
240 | 282 | } |
241 | - if (persistent_strings == NULL) { | |
242 | - persistent_strings = g_hash_table_new(g_str_hash, g_str_equal); | |
283 | + if (persistent_entrystrings == NULL) { | |
284 | + persistent_entrystrings = g_hash_table_new(g_str_hash, g_str_equal); | |
243 | 285 | } |
244 | 286 | if (persistent_lists == NULL) { |
245 | 287 | persistent_lists = g_hash_table_new(g_str_hash, g_str_equal); |
@@ -253,6 +295,12 @@ persistence_init() | ||
253 | 295 | if (persistent_booleans == NULL) { |
254 | 296 | persistent_booleans = g_hash_table_new(g_str_hash, g_str_equal); |
255 | 297 | } |
298 | + if (persistent_strings == NULL) { | |
299 | + persistent_strings = g_hash_table_new(g_str_hash, g_str_equal); | |
300 | + } | |
301 | + if (persistent_colors == NULL) { | |
302 | + persistent_colors = g_hash_table_new(g_str_hash, g_str_equal); | |
303 | + } | |
256 | 304 | } |
257 | 305 | |
258 | 306 | /* Load all persistent data. */ |
@@ -306,18 +354,6 @@ persistence_save_window(gpointer key, gpointer value, gpointer data) | ||
306 | 354 | |
307 | 355 | /* Save the contents of a string */ |
308 | 356 | static void |
309 | -persistence_save_string(gpointer key, gpointer value, gpointer data) | |
310 | -{ | |
311 | - xmlNodePtr tree = (xmlNodePtr)data; | |
312 | - ObjectNode stringnode; | |
313 | - | |
314 | - stringnode = (ObjectNode)xmlNewChild(tree, NULL, "entrystring", NULL); | |
315 | - | |
316 | - xmlSetProp(stringnode, "role", (char *)key); | |
317 | - data_add_string(new_attribute(stringnode, "stringvalue"), (char *)value); | |
318 | -} | |
319 | - | |
320 | -static void | |
321 | 357 | persistence_save_list(gpointer key, gpointer value, gpointer data) |
322 | 358 | { |
323 | 359 | xmlNodePtr tree = (xmlNodePtr)data; |
@@ -342,6 +378,18 @@ persistence_save_list(gpointer key, gpointer value, gpointer data) | ||
342 | 378 | } |
343 | 379 | |
344 | 380 | static void |
381 | +persistence_save_entrystring(gpointer key, gpointer value, gpointer data) | |
382 | +{ | |
383 | + xmlNodePtr tree = (xmlNodePtr)data; | |
384 | + ObjectNode stringnode; | |
385 | + | |
386 | + stringnode = (ObjectNode)xmlNewChild(tree, NULL, "entrystring", NULL); | |
387 | + | |
388 | + xmlSetProp(stringnode, "role", (char *)key); | |
389 | + data_add_string(new_attribute(stringnode, "stringvalue"), (char *)value); | |
390 | +} | |
391 | + | |
392 | +static void | |
345 | 393 | persistence_save_integer(gpointer key, gpointer value, gpointer data) |
346 | 394 | { |
347 | 395 | xmlNodePtr tree = (xmlNodePtr)data; |
@@ -377,6 +425,30 @@ persistence_save_boolean(gpointer key, gpointer value, gpointer data) | ||
377 | 425 | data_add_boolean(new_attribute(booleannode, "booleanvalue"), *(gboolean *)value); |
378 | 426 | } |
379 | 427 | |
428 | +static void | |
429 | +persistence_save_string(gpointer key, gpointer value, gpointer data) | |
430 | +{ | |
431 | + xmlNodePtr tree = (xmlNodePtr)data; | |
432 | + ObjectNode stringnode; | |
433 | + | |
434 | + stringnode = (ObjectNode)xmlNewChild(tree, NULL, "string", NULL); | |
435 | + | |
436 | + xmlSetProp(stringnode, "role", (char *)key); | |
437 | + data_add_string(new_attribute(stringnode, "stringvalue"), (gchar *)value); | |
438 | +} | |
439 | + | |
440 | +static void | |
441 | +persistence_save_color(gpointer key, gpointer value, gpointer data) | |
442 | +{ | |
443 | + xmlNodePtr tree = (xmlNodePtr)data; | |
444 | + ObjectNode colornode; | |
445 | + | |
446 | + colornode = (ObjectNode)xmlNewChild(tree, NULL, "color", NULL); | |
447 | + | |
448 | + xmlSetProp(colornode, "role", (char *)key); | |
449 | + data_add_color(new_attribute(colornode, "colorvalue"), (Color *)value); | |
450 | +} | |
451 | + | |
380 | 452 | |
381 | 453 | void |
382 | 454 | persistence_save_type(xmlDocPtr doc, GHashTable *entries, GHFunc func) |
@@ -403,18 +475,14 @@ persistence_save() | ||
403 | 475 | "dia"); |
404 | 476 | xmlSetNs(doc->xmlRootNode, name_space); |
405 | 477 | |
406 | - printf("Saving windows\n"); | |
407 | 478 | persistence_save_type(doc, persistent_windows, persistence_save_window); |
408 | - printf("Saving strings\n"); | |
409 | - persistence_save_type(doc, persistent_strings, persistence_save_string); | |
410 | - printf("Saving lists\n"); | |
479 | + persistence_save_type(doc, persistent_entrystrings, persistence_save_string); | |
411 | 480 | persistence_save_type(doc, persistent_lists, persistence_save_list); |
412 | - printf("Saving integers\n"); | |
413 | 481 | persistence_save_type(doc, persistent_integers, persistence_save_integer); |
414 | - printf("Saving reals\n"); | |
415 | 482 | persistence_save_type(doc, persistent_reals, persistence_save_real); |
416 | - printf("Saving booleans\n"); | |
417 | 483 | persistence_save_type(doc, persistent_booleans, persistence_save_boolean); |
484 | + persistence_save_type(doc, persistent_strings, persistence_save_string); | |
485 | + persistence_save_type(doc, persistent_colors, persistence_save_color); | |
418 | 486 | |
419 | 487 | xmlDiaSaveFile(filename, doc); |
420 | 488 | g_free(filename); |
@@ -559,10 +627,10 @@ persistence_update_string_entry(GtkWidget *widget, GdkEvent *event, | ||
559 | 627 | gchar *role = (gchar*)userdata; |
560 | 628 | |
561 | 629 | if (event->type == GDK_FOCUS_CHANGE) { |
562 | - gchar *string = (gchar *)g_hash_table_lookup(persistent_strings, role); | |
630 | + gchar *string = (gchar *)g_hash_table_lookup(persistent_entrystrings, role); | |
563 | 631 | gchar *entrystring = gtk_entry_get_text(GTK_ENTRY(widget)); |
564 | 632 | if (string == NULL || strcmp(string, entrystring)) { |
565 | - g_hash_table_insert(persistent_strings, role, g_strdup(entrystring)); | |
633 | + g_hash_table_insert(persistent_entrystrings, role, g_strdup(entrystring)); | |
566 | 634 | if (string != NULL) g_free(string); |
567 | 635 | } |
568 | 636 | } |
@@ -579,12 +647,12 @@ gboolean | ||
579 | 647 | persistence_change_string_entry(gchar *role, gchar *string, |
580 | 648 | GtkWidget *widget) |
581 | 649 | { |
582 | - gchar *old_string = (gchar*)g_hash_table_lookup(persistent_strings, role); | |
650 | + gchar *old_string = (gchar*)g_hash_table_lookup(persistent_entrystrings, role); | |
583 | 651 | if (old_string != NULL) { |
584 | 652 | if (widget != NULL) { |
585 | 653 | gtk_entry_set_text(GTK_ENTRY(widget), string); |
586 | 654 | } |
587 | - g_hash_table_insert(persistent_strings, role, g_strdup(string)); | |
655 | + g_hash_table_insert(persistent_entrystrings, role, g_strdup(string)); | |
588 | 656 | g_free(old_string); |
589 | 657 | } |
590 | 658 |
@@ -600,15 +668,15 @@ persistence_register_string_entry(gchar *role, GtkWidget *entry) | ||
600 | 668 | { |
601 | 669 | gchar *string; |
602 | 670 | if (role == NULL) return; |
603 | - if (persistent_strings == NULL) { | |
604 | - persistent_strings = g_hash_table_new(g_str_hash, g_str_equal); | |
671 | + if (persistent_entrystrings == NULL) { | |
672 | + persistent_entrystrings = g_hash_table_new(g_str_hash, g_str_equal); | |
605 | 673 | } |
606 | - string = (gchar *)g_hash_table_lookup(persistent_strings, role); | |
674 | + string = (gchar *)g_hash_table_lookup(persistent_entrystrings, role); | |
607 | 675 | if (string != NULL) { |
608 | 676 | gtk_entry_set_text(GTK_ENTRY(entry), string); |
609 | 677 | } else { |
610 | 678 | string = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry))); |
611 | - g_hash_table_insert(persistent_strings, role, string); | |
679 | + g_hash_table_insert(persistent_entrystrings, role, string); | |
612 | 680 | } |
613 | 681 | g_signal_connect(G_OBJECT(entry), "event", |
614 | 682 | G_CALLBACK(persistence_update_string_entry), role); |
@@ -626,7 +694,6 @@ persistence_register_list(const gchar *role) | ||
626 | 694 | { |
627 | 695 | PersistentList *list; |
628 | 696 | if (role == NULL) return NULL; |
629 | - printf("Registering %s\n", role); | |
630 | 697 | if (persistent_lists == NULL) { |
631 | 698 | persistent_lists = g_hash_table_new(g_str_hash, g_str_equal); |
632 | 699 | } else { |
@@ -794,7 +861,7 @@ persistence_get_real(gchar *role) | ||
794 | 861 | realval = (real *)g_hash_table_lookup(persistent_reals, role); |
795 | 862 | if (realval != NULL) return *realval; |
796 | 863 | printf("No real to get for %s\n", role); |
797 | - return 0; | |
864 | + return 0.0; | |
798 | 865 | } |
799 | 866 | |
800 | 867 | void |
@@ -806,7 +873,6 @@ persistence_set_real(gchar *role, real newvalue) | ||
806 | 873 | return; |
807 | 874 | } |
808 | 875 | realval = (real *)g_hash_table_lookup(persistent_reals, role); |
809 | - printf("Setting real %s to %f\n", role, newvalue); | |
810 | 876 | if (realval != NULL) *realval = newvalue; |
811 | 877 | else printf("No real to set for %s\n", role); |
812 | 878 | } |
@@ -841,7 +907,7 @@ persistence_get_boolean(gchar *role) | ||
841 | 907 | booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role); |
842 | 908 | if (booleanval != NULL) return *booleanval; |
843 | 909 | printf("No boolean to get for %s\n", role); |
844 | - return 0; | |
910 | + return FALSE; | |
845 | 911 | } |
846 | 912 | |
847 | 913 | void |
@@ -853,7 +919,100 @@ persistence_set_boolean(gchar *role, gboolean newvalue) | ||
853 | 919 | return; |
854 | 920 | } |
855 | 921 | booleanval = (gboolean *)g_hash_table_lookup(persistent_booleans, role); |
856 | - printf("Setting boolean %s to %d\n", role, newvalue); | |
857 | 922 | if (booleanval != NULL) *booleanval = newvalue; |
858 | 923 | else printf("No boolean to set for %s\n", role); |
859 | 924 | } |
925 | + | |
926 | +/* ********* STRINGS ********** */ | |
927 | +gchar * | |
928 | +persistence_register_string(gchar *role, gchar *defaultvalue) | |
929 | +{ | |
930 | + gchar *stringval; | |
931 | + if (role == NULL) return 0; | |
932 | + if (persistent_strings == NULL) { | |
933 | + persistent_strings = g_hash_table_new(g_str_hash, g_str_equal); | |
934 | + } | |
935 | + stringval = (gchar *)g_hash_table_lookup(persistent_strings, role); | |
936 | + if (stringval == NULL) { | |
937 | + stringval = g_strdup(defaultvalue); | |
938 | + g_hash_table_insert(persistent_strings, role, stringval); | |
939 | + } | |
940 | + return stringval; | |
941 | +} | |
942 | + | |
943 | +gchar * | |
944 | +persistence_get_string(gchar *role) | |
945 | +{ | |
946 | + gchar *stringval; | |
947 | + if (persistent_strings == NULL) { | |
948 | + printf("No persistent strings to get for %s!\n", role); | |
949 | + return 0; | |
950 | + } | |
951 | + stringval = (gchar *)g_hash_table_lookup(persistent_strings, role); | |
952 | + if (stringval != NULL) return stringval; | |
953 | + printf("No string to get for %s\n", role); | |
954 | + return NULL; | |
955 | +} | |
956 | + | |
957 | +void | |
958 | +persistence_set_string(gchar *role, gchar *newvalue) | |
959 | +{ | |
960 | + gchar *stringval; | |
961 | + if (persistent_strings == NULL) { | |
962 | + printf("No persistent strings yet for %s!\n", role); | |
963 | + return; | |
964 | + } | |
965 | + stringval = (gchar *)g_hash_table_lookup(persistent_strings, role); | |
966 | + if (stringval != NULL) { | |
967 | + g_hash_table_insert(persistent_strings, role, g_strdup(newvalue)); | |
968 | + g_free(stringval); | |
969 | + } | |
970 | + else printf("No string to set for %s\n", role); | |
971 | +} | |
972 | + | |
973 | +/* ********* COLORS ********** */ | |
974 | +/* Remember that colors returned are private, not to be deallocated. | |
975 | + * They will be smashed in some undefined way by persistence_set_color */ | |
976 | +Color * | |
977 | +persistence_register_color(gchar *role, Color *defaultvalue) | |
978 | +{ | |
979 | + Color *colorval; | |
980 | + if (role == NULL) return 0; | |
981 | + if (persistent_colors == NULL) { | |
982 | + persistent_colors = g_hash_table_new(g_str_hash, g_str_equal); | |
983 | + } | |
984 | + colorval = (Color *)g_hash_table_lookup(persistent_colors, role); | |
985 | + if (colorval == NULL) { | |
986 | + colorval = g_new(Color, 1); | |
987 | + *colorval = *defaultvalue; | |
988 | + g_hash_table_insert(persistent_colors, role, colorval); | |
989 | + } | |
990 | + return colorval; | |
991 | +} | |
992 | + | |
993 | +Color * | |
994 | +persistence_get_color(gchar *role) | |
995 | +{ | |
996 | + Color *colorval; | |
997 | + if (persistent_colors == NULL) { | |
998 | + printf("No persistent colors to get for %s!\n", role); | |
999 | + return 0; | |
1000 | + } | |
1001 | + colorval = (Color *)g_hash_table_lookup(persistent_colors, role); | |
1002 | + if (colorval != NULL) return colorval; | |
1003 | + printf("No color to get for %s\n", role); | |
1004 | + return 0; | |
1005 | +} | |
1006 | + | |
1007 | +void | |
1008 | +persistence_set_color(gchar *role, Color *newvalue) | |
1009 | +{ | |
1010 | + Color *colorval; | |
1011 | + if (persistent_colors == NULL) { | |
1012 | + printf("No persistent colors yet for %s!\n", role); | |
1013 | + return; | |
1014 | + } | |
1015 | + colorval = (Color *)g_hash_table_lookup(persistent_colors, role); | |
1016 | + if (colorval != NULL) *colorval = *newvalue; | |
1017 | + else printf("No color to set for %s\n", role); | |
1018 | +} |
@@ -77,4 +77,12 @@ gboolean persistence_register_boolean(gchar *role, gboolean defaultvalue); | ||
77 | 77 | gboolean persistence_get_boolean(gchar *role); |
78 | 78 | void persistence_set_boolean(gchar *role, gboolean newvalue); |
79 | 79 | |
80 | +gchar *persistence_register_string(gchar *role, gchar *defaultvalue); | |
81 | +gchar *persistence_get_string(gchar *role); | |
82 | +void persistence_set_string(gchar *role, gchar *newvalue); | |
83 | + | |
84 | +Color *persistence_register_color(gchar *role, Color *defaultvalue); | |
85 | +Color *persistence_get_color(gchar *role); | |
86 | +void persistence_set_color(gchar *role, Color *newvalue); | |
87 | + | |
80 | 88 | #endif |