Debian Codegraph @

w4/prefsubs.c

raw
1 /* File: prefsubs.c
2 * Author: Ian Longden
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1999
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 *
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
20 * -------------------------------------------------------------------
21 * This file is part of the ACEDB genome database package, written by
22 * Richard Durbin (Sanger Centre, UK) [email protected], and
23 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
24 *
25 * Description: Handles user preferences for acedb which are stored in
26 * the file $HOME/.acedbrc
27 *
28 * Exported functions: See wh/pref.h
29 * HISTORY:
30 * Last edited: Oct 13 11:39 2006 (edgrif)
31 * * Jul 5 13:36 2001 (edgrif): Added code to support callback funcs
32 * which means we don't have to hard code them in this
33 * routine as was starting to happen.
34 * * May 1 10:57 1999 (fw): moved all graphical code to prefdisp.c
35 * Created: Sat May 1 10:56:58 1999 (fw)
36 * CVS info: $Id: prefsubs.c,v 1.45 2006/10/18 07:55:11 edgrif Exp $
37 *-------------------------------------------------------------------
38 */
39
40 #include <wh/acedb.h>
41 #ifdef __CYGWIN__
42 #include <windows.h>
43 #endif
44 #include <wh/aceio.h>
45 #include <whooks/systags.h>
46 #include <whooks/sysclass.h>
47 #include <wh/lex.h>
48 #include <wh/pick.h>
49 #include <wh/session.h>
50 #include <wh/bs.h>
51 #include <wh/query.h>
52 #include <wh/dbpath.h>
53 #include <wh/pref_.h>
54
55 #include <wh/graph.h>
56 #include <wh/gex.h>
57
58
59 /************************************************************/
60
61 static void copySystemPrefs(Array existing_pref_list);
62 static Array makePrefDefaults(void);
63 static void prefAdd(Array prefList, PREFLIST *item);
64
65
66 /************************************************************/
67 /*
68 il-27/01/97
69
70 Examples of preferences file lines
71
72 _NAME -T |set Name to TRUE
73 _NAME1 -F |set Name1 to FALSE
74 _NAME2 -I 2 |set Integer Name2 to 2.
75 _NAME3 -R 5.5 |set Float Name3 to 5.5.
76 _NAME4 -C 1 |set colour NAME4 to WHITE (by number)
77 _NAME4 -C WHITE |set colour NAME4 to WHITE (by name)
78 _NAME5 -S string |set NAME5 to string
79
80 where "_" must be the first character on the line followed by the preferences name
81 then (tab or space delimited) a "-" the type and for int,floats and colours the value.
82 Any other lines will be ignored.
83 To use this later on in the program use prefValue(NAME) which will return the BOOL value.
84 prefInt(NAME) will return the value for NAME etc.
85 */
86
87
88 /* We hold a global array of preferences and also record globally whether we managed
89 * to read the users .acedbrc file. */
90 static Array prefList_G = NULL ;
91 static BOOL user_prefs_read_G = FALSE ;
92
93
94 /************************************************************/
95 #ifdef __CYGWIN__
96
97 /* For cygwin we use the registry, under the key
98 HKEY_CURRENT_USER/Software/acedb.org/Acedb/Prefs
99
100 Names of values are [type]name where type is
101 b boolean
102 C colour
103 F float
104 i integer
105 S string
106
107 Values if keys gives values, with colours as numbers and
108 T and F for booleans.
109 */
110
111 static BOOL prefReadReg(HKEY key, Array prefList)
112 {
113 BOOL result = FALSE ;
114 PREFLIST item;
115 DWORD buffsiz = 255;
116 char buff[256];
117 int keys = 0;
118 DWORD prefsiz = 255;
119 char prefbuff[256];
120
121 while (ERROR_SUCCESS == RegEnumValue(key, keys++, buff, &buffsiz,
122 NULL, NULL, prefbuff, &prefsiz))
123 {
124 strncpy(item.name, buff+1, 32);
125
126 switch (*buff)
127 {
128 case 'B': case 'b':
129 item.value.bval = (*prefbuff == 'T');
130 item.type = PREF_BOOLEAN;
131 item.deffunc.bfunc = NULL ;
132 item.editfunc.ifunc = NULL ;
133 item.func.bfunc = NULL ;
134 prefAdd(prefList, &item);
135 break;
136 case 'i': case 'I':
137 if(sscanf(prefbuff,"%d",&item.value.ival))
138 {
139 item.type=PREF_INT;
140 item.deffunc.ifunc = NULL ;
141 item.editfunc.ifunc = NULL ;
142 item.func.ifunc = NULL ;
143 prefAdd(prefList, &item);
144 }
145 break;
146 case 'C': case 'c':
147 if(sscanf(prefbuff,"%d",&item.value.ival))
148 {
149 item.type=PREF_COLOUR;
150 item.deffunc.ifunc = NULL ;
151 item.editfunc.ifunc = NULL ;
152 item.func.ifunc = NULL ;
153 prefAdd(prefList, &item);
154 }
155 break;
156 case 'R': case 'r':
157 if(sscanf(prefbuff,"%f",&item.value.fval))
158 {
159 item.type=PREF_FLOAT;
160 item.deffunc.ffunc = NULL ;
161 item.editfunc.ffunc = NULL ;
162 item.func.ffunc = NULL ;
163 prefAdd(prefList, &item);
164 }
165 break;
166
167 default:
168 break;
169 }
170 /* reset these before looping - they're input parameters too. */
171 buffsiz = 255;
172 prefsiz = 255;
173 }
174
175 if (keys > 0)
176 result = TRUE ;
177
178 return result ;
179 }
180
181 static HKEY prefPrefRegRead(void)
182 {
183 BOOL success = FALSE;
184 HKEY software, acedb_org, acedb, prefs;
185
186 if (RegOpenKeyEx(HKEY_CURRENT_USER,
187 "SOFTWARE", 0L,
188 KEY_EXECUTE,
189 &software) == ERROR_SUCCESS)
190 {
191 if (RegOpenKeyEx(software,
192 "acedb.org", 0L,
193 KEY_EXECUTE,
194 &acedb_org) == ERROR_SUCCESS)
195 {
196 if (RegOpenKeyEx(acedb_org,
197 "Acedb", 0L,
198 KEY_EXECUTE,
199 &acedb) == ERROR_SUCCESS)
200 {
201 if (RegOpenKeyEx(acedb,
202 "Prefs", 0L,
203 KEY_READ,
204 &prefs) == ERROR_SUCCESS)
205 {
206 success = TRUE;
207 }
208 RegCloseKey(acedb);
209 }
210 RegCloseKey(acedb_org);
211 }
212 RegCloseKey(software);
213 }
214
215 if (success)
216 return prefs;
217
218 return 0;
219
220 }
221
222 void prefInit(void)
223 {
224 HKEY prefKey;
225
226 if (!user_prefs_read_G)
227 {
228 prefList_G = makePrefDefaults() ;
229
230 if ((prefKey = prefPrefRegRead()) && (prefReadReg(prefKey, prefList_G)))
231 user_prefs_read_G = TRUE ;
232 }
233 return ;
234 }
235
236 static HKEY prefPrefRegWrite(void)
237 {
238 BOOL success = FALSE;
239 HKEY software, acedb_org, acedb, prefs;
240
241 if (RegOpenKeyEx(HKEY_CURRENT_USER,
242 "SOFTWARE", 0L,
243 KEY_EXECUTE,
244 &software) == ERROR_SUCCESS)
245 {
246 if (RegCreateKeyEx(software,
247 "acedb.org", 0L, NULL,
248 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
249 &acedb_org, NULL) == ERROR_SUCCESS)
250 {
251 if (RegCreateKeyEx(acedb_org,
252 "Acedb", 0L, NULL,
253 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
254 &acedb, NULL) == ERROR_SUCCESS)
255 {
256 if (RegCreateKeyEx(acedb,
257 "Prefs", 0L, NULL,
258 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
259 NULL, &prefs, NULL) == ERROR_SUCCESS)
260 {
261 success = TRUE;
262 }
263 RegCloseKey(acedb);
264 }
265 RegCloseKey(acedb_org);
266 }
267 RegCloseKey(software);
268 }
269
270 if (success)
271 return prefs;
272
273 return 0;
274
275 }
276
277 void prefSave(void)
278 {
279 HKEY prefKey = prefPrefRegWrite();
280 char namebuff[255];
281 char valbuff[255];
282 PREFLIST *item;
283 int i;
284
285 if (prefKey)
286 {
287 for(i=0;i<arrayMax(prefList_G);i++)
288 {
289 item = arrp(prefList_G,i,PREFLIST);
290 if (item->type == PREF_BOOLEAN)
291 {
292 char *val;
293 *namebuff = 'B';
294 strcpy(namebuff+1, item->name);
295 strcpy(valbuff, item->value.bval ? "T" : "F");
296 }
297 else if (item->type == PREF_INT)
298 {
299 *namebuff = 'I';
300 strcpy(namebuff+1, item->name);
301 sprintf(valbuff, "%d", item->value.ival);
302 }
303 else if (item->type == PREF_COLOUR)
304 {
305 *namebuff = 'C';
306 strcpy(namebuff+1, item->name);
307 sprintf(valbuff, "%d", item->value.ival);
308 }
309 else if (item->type == PREF_FLOAT)
310 {
311 *namebuff = 'R';
312 strcpy(namebuff+1, item->name);
313 sprintf(valbuff, "%f", item->value.fval);
314 }
315
316 RegSetValueEx(prefKey, namebuff, 0L,
317 REG_SZ, valbuff, strlen(valbuff)+1);
318 }
319
320 RegCloseKey(prefKey);
321
322 }
323 }
324
325 #else /* !__CYGWIN__ */
326
327 /* This function does the first initialisation of user preferences and tries
328 * to read the users .acedbrc file, it _must_ be called before prefInitDB()
329 * which calls prefInitDB() */
330 void prefInit(void)
331 {
332 ACEIN pref_in = NULL;
333 char filename[MAXPATHLEN] = "";
334
335 /* This can't happen at the moment I think.... */
336 if (prefList_G)
337 {
338 arrayDestroy(prefList_G);
339 prefList_G = NULL ;
340 user_prefs_read_G = FALSE ;
341 }
342
343 prefList_G = makePrefDefaults() ;
344
345 if(getenv("HOME"))
346 {
347 strcat(filename,getenv("HOME"));
348 strcat(filename, "/");
349 }
350 strcat(filename,".acedbrc");
351
352 if (filCheckName(filename,"","r")
353 && (pref_in = aceInCreateFromFile (filename, "r", "", 0)))
354 {
355 prefRead(pref_in, prefList_G) ;
356 aceInDestroy (pref_in);
357 user_prefs_read_G = TRUE ;
358 }
359
360
361 /* We'd probably like to check preferences for validity here but we can't */
362 /* because this is a chicken and egg, this is where we create the struct */
363 /* to hold the checking routine callbacks so they can't have been */
364 /* registered yet....this means its possible for someone to edit their */
365 /* preference file and potentially screw things up. If they do then I */
366 /* guess that's their problem. */
367
368
369 return ;
370 }
371
372
373 void prefSave(void)
374 {
375 char filename[FIL_BUFFER_SIZE];
376 FILE *prefFil = 0;
377 int i=0;
378 PREFLIST *item;
379
380 if(getenv("HOME"))
381 {
382 strcpy(filename,getenv("HOME"));
383 strcat(filename,"/.acedbrc");
384 prefFil = filopen(filename,"","w");
385 }
386
387
388 if (prefFil)
389 {
390 fprintf(prefFil,"This file contains the preferences for ACEDB.\n");
391 fprintf(prefFil,"Do not edit by hand. Use the preference editor from the main menu.\n");
392
393 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
394 {
395 item = arrp(prefList_G,i,PREFLIST) ;
396 if(item->type == PREF_BOOLEAN)
397 fprintf(prefFil,"_%s -%s\n",item->name,
398 (item->value.bval ? "TRUE":"FALSE")) ;
399 else if(item->type == PREF_INT)
400 fprintf(prefFil,"_%s -I %d\n",item->name,item->value.ival) ;
401 else if(item->type == PREF_COLOUR)
402 fprintf(prefFil,"_%s -C %d\n",item->name,item->value.ival) ;
403 else if(item->type == PREF_FLOAT)
404 fprintf(prefFil,"_%s -R %f\n",item->name,item->value.fval) ;
405 else if(item->type == PREF_STRING)
406 fprintf(prefFil, "_%s -S %s\n", item->name, item->value.sval) ;
407 }
408
409 filclose(prefFil) ;
410 }
411
412 return ;
413 }
414
415 #endif /* !__CYGWIN__ */
416
417 /* If there was no user .acedbrc file, there may be an acedbrc file in the
418 * wspec directory so we should try to load that. */
419 void prefReadDBPrefs()
420 {
421 messAssert(prefList_G != NULL) ;
422
423 if (!user_prefs_read_G)
424 copySystemPrefs(prefList_G) ;
425
426 return ;
427 }
428
429 Array prefGet(void)
430 {
431 return prefList_G ;
432 }
433
434 /* This creates the new defaults if none exist.
435 * You will have to add here any new preferences that you want to add.
436 * Remember to add them to the .acedbrc file,
437 * the $ACEDB/wspec/acedbrc or remove the two completely.
438 * To have them take effect. */
439 static Array makePrefDefaults(void)
440 {
441 PREFLIST item;
442 Array prefList;
443
444 prefList = arrayCreate (10, PREFLIST) ;
445
446 strcpy(item.name,"OLD_STYLE_MAIN_WINDOW");
447 item.type = PREF_BOOLEAN;
448 item.value.bval = FALSE ;
449 item.deffunc.bfunc = NULL ;
450 item.editfunc.ifunc = NULL ; /* no bool version. */
451 item.func.bfunc = NULL ;
452 item.display = TRUE;
453 array(prefList,arrayMax(prefList),PREFLIST) = item;
454
455 strcpy(item.name,"AUTO_DISPLAY");
456 item.type = PREF_BOOLEAN;
457 item.value.bval = TRUE ;
458 item.deffunc.bfunc = NULL ;
459 item.editfunc.ifunc = NULL ; /* no bool version. */
460 item.func.bfunc = NULL ;
461 item.display = TRUE;
462 array(prefList,arrayMax(prefList),PREFLIST) = item;
463
464 /* Should acedb splash screen be displayed at start up. */
465 strcpy(item.name, SPLASH_SCREEN) ;
466 item.type = PREF_BOOLEAN ;
467 item.value.bval = TRUE ;
468 item.deffunc.bfunc = NULL ;
469 item.editfunc.ifunc = NULL ;
470 item.func.bfunc = NULL ;
471 item.display = TRUE ;
472 array(prefList,arrayMax(prefList),PREFLIST) = item ;
473
474 strcpy(item.name,"HORIZONTAL_TREE");
475 item.type = PREF_BOOLEAN;
476 item.value.bval = TRUE;
477 item.deffunc.bfunc = NULL ;
478 item.editfunc.ifunc = NULL ; /* no bool version. */
479 item.func.bfunc = NULL ;
480 item.display = TRUE;
481 array(prefList,arrayMax(prefList),PREFLIST) = item;
482
483 strcpy(item.name,"ACTION_MENU_IN_TREE_DISPLAY");
484 item.type = PREF_BOOLEAN;
485 item.value.bval = FALSE;
486 item.deffunc.bfunc = NULL ;
487 item.editfunc.ifunc = NULL ; /* no bool version. */
488 item.func.bfunc = NULL ;
489 item.display = TRUE;
490 array(prefList,arrayMax(prefList),PREFLIST) = item;
491
492 strcpy(item.name,"NO_MESSAGE_WHEN_DISPLAY_BLOCK");
493 item.type = PREF_BOOLEAN;
494 item.value.bval = FALSE;
495 item.deffunc.bfunc = NULL ;
496 item.editfunc.ifunc = NULL ; /* no bool version. */
497 item.func.bfunc = NULL ;
498 item.display = TRUE;
499 array(prefList,arrayMax(prefList),PREFLIST) = item;
500
501 /* not used anywhere MAKE SURE YOU ADD ALL THE CALLBACK FUNC POINTER IF
502 YOU REINSTATE ANY OF THIS....
503 strcpy(item.name,"SHOW_EMPTY_OBJECTS");
504 item.type = PREF_BOOLEAN;
505 item.value.bval = FALSE;
506 item.display = TRUE;
507 array(prefList,arrayMax(prefList),PREFLIST) = item;
508
509 strcpy(item.name,"SHOW_ALIASES");
510 item.type = PREF_BOOLEAN;
511 item.value.bval = FALSE;
512 item.display = TRUE;
513 array(prefList,arrayMax(prefList),PREFLIST) = item;
514 */
515
516 strcpy(item.name,"TAG_COLOUR_IN_TREE_DISPLAY");
517 item.type = PREF_COLOUR;
518 item.value.ival = BROWN ;
519 item.deffunc.ifunc = NULL ;
520 item.editfunc.ifunc = NULL ; /* no bool version. */
521 item.func.ifunc = NULL ;
522 item.display = TRUE;
523 array(prefList,arrayMax(prefList),PREFLIST) = item;
524
525 strcpy(item.name, BLIXEM_SCOPE);
526 item.type = PREF_INT;
527 item.value.ival = 40000;
528 item.deffunc.ifunc = NULL ;
529 item.editfunc.ifunc = NULL ;
530 item.func.ifunc = NULL ;
531 item.display = TRUE;
532 array(prefList,arrayMax(prefList),PREFLIST) = item;
533
534 strcpy(item.name, BLIXEM_HOMOL_MAX);
535 item.type = PREF_INT;
536 item.value.ival = 0;
537 item.deffunc.ifunc = NULL ;
538 item.editfunc.ifunc = NULL ;
539 item.func.ifunc = NULL ;
540 item.display = TRUE;
541 array(prefList,arrayMax(prefList),PREFLIST) = item;
542
543 /* Call blixem as a separate process. */
544 strcpy(item.name, BLIXEM_EXTERNAL);
545 item.type = PREF_BOOLEAN ;
546 item.value.bval = FALSE ;
547 item.deffunc.bfunc = NULL ;
548 item.editfunc.ifunc = NULL ;
549 item.func.bfunc = NULL ;
550 item.display = TRUE ;
551 array(prefList,arrayMax(prefList),PREFLIST) = item ;
552
553 /* If blixem is called as a separate process it is called from a cover script
554 * and this is the scripts path. */
555 strcpy(item.name, BLIXEM_SCRIPT);
556 item.type = PREF_STRING;
557 item.value.sval = strnew("", 0) ;
558 item.string_length = 300 ; /* Should be long enough. */
559 item.deffunc.sfunc = NULL ;
560 item.editfunc.sfunc = NULL ;
561 item.func.sfunc = NULL ;
562 item.display = TRUE;
563 array(prefList,arrayMax(prefList),PREFLIST) = item;
564
565 /* If blixem is called as a separate process tell it not to erase its temp. input files. */
566 strcpy(item.name, BLIXEM_TEMPFILES);
567 item.type = PREF_BOOLEAN ;
568 item.value.bval = FALSE ;
569 item.deffunc.bfunc = NULL ;
570 item.editfunc.ifunc = NULL ;
571 item.func.bfunc = NULL ;
572 item.display = FALSE ;
573 array(prefList,arrayMax(prefList),PREFLIST) = item ;
574
575 /* Next three tell blixem to use pfetch instead of efetch, to do this may */
576 /* require setting of machine address and port for pfetch server. */
577 strcpy(item.name, BLIXEM_PFETCH) ;
578 item.type = PREF_BOOLEAN ;
579 item.value.bval = FALSE ;
580 item.deffunc.bfunc = NULL ;
581 item.editfunc.ifunc = NULL ;
582 item.func.bfunc = NULL ;
583 item.display = TRUE ;
584 array(prefList, arrayMax(prefList), PREFLIST) = item ;
585
586
587 strcpy(item.name, BLIXEM_NETID);
588 item.type = PREF_STRING;
589 item.value.sval = strnew(BLIXEM_DEFAULT_NETID, 0) ;
590 item.string_length = 20 ; /* long enough for dotted decimal and
591 most hostnames ?? */
592 item.deffunc.sfunc = NULL ;
593 item.editfunc.sfunc = NULL ;
594 item.func.sfunc = NULL ;
595 item.display = TRUE;
596 array(prefList,arrayMax(prefList),PREFLIST) = item;
597
598 strcpy(item.name, BLIXEM_PORT_NUMBER);
599 item.type = PREF_INT;
600 item.value.ival = BLIXEM_DEFAULT_PORT_NUMBER ;
601 item.deffunc.ifunc = NULL ;
602 item.editfunc.ifunc = NULL ;
603 item.func.ifunc = NULL ;
604 item.display = TRUE;
605 array(prefList,arrayMax(prefList),PREFLIST) = item;
606
607
608 /* User defaults for scrolling message window. */
609 strcpy(item.name, USE_MSG_LIST);
610 item.type = PREF_BOOLEAN ;
611 item.value.bval = FALSE ;
612 item.deffunc.bfunc = NULL ;
613 item.editfunc.ifunc = NULL ; /* no bool version. */
614 item.func.bfunc = NULL ;
615 item.display = TRUE ;
616 array(prefList,arrayMax(prefList),PREFLIST) = item ;
617
618 strcpy(item.name, MAX_MSG_LIST_LENGTH) ;
619 item.type = PREF_INT ;
620 item.value.ival = 0 ;
621 item.deffunc.ifunc = NULL ;
622 item.editfunc.ifunc = NULL ;
623 item.func.ifunc = NULL ;
624 item.display = TRUE ;
625 array(prefList,arrayMax(prefList),PREFLIST) = item ;
626
627 strcpy(item.name, DNA_HIGHLIGHT_IN_FMAP_DISPLAY);
628 item.type = PREF_COLOUR;
629 item.value.ival = PALERED ;
630 item.deffunc.ifunc = NULL ;
631 item.editfunc.ifunc = NULL ; /* no bool version. */
632 item.func.ifunc = NULL ;
633 item.display = TRUE;
634 array(prefList,arrayMax(prefList),PREFLIST) = item;
635
636 return prefList;
637 } /* makePrefDefaults */
638
639
640 /* Read in the system defaults if they exist. */
641 static void copySystemPrefs(Array prefsList)
642 {
643 ACEIN pref_in = NULL ;
644 char *filename = NULL ;
645
646 if ((filename = dbPathFilName("wspec", "acedbrc","","r", 0))
647 && (pref_in = aceInCreateFromFile(filename, "r","", 0)))
648 {
649 prefRead(pref_in, prefsList);
650 aceInDestroy (pref_in);
651 messfree(filename);
652 }
653
654 return ;
655 } /* copySystemPrefs */
656
657
658 static void prefAdd(Array prefList, PREFLIST *item)
659 {
660 BOOL found = FALSE;
661 int i;
662
663 for(i=0;i<arrayMax(prefList);i++)
664 {
665 if(!strcmp(item->name,arrp(prefList,i,PREFLIST)->name)) /* is it the same name */
666 {
667 if(arrp(prefList,i,PREFLIST)->type != item->type)
668 {
669 messout("ERROR: Prefile reading in the preference %s but does not have the same type definition as that in the master copy. This will be ignored.",item->name);
670 return;
671 }
672
673 if(arrp(prefList,i,PREFLIST)->type == PREF_BOOLEAN)
674 arrp(prefList,i,PREFLIST)->value.bval = item->value.bval;
675 else if(arrp(prefList,i,PREFLIST)->type == PREF_INT ||arrp(prefList,i,PREFLIST)->type == PREF_COLOUR )
676 arrp(prefList,i,PREFLIST)->value.ival = item->value.ival;
677 else if(arrp(prefList,i,PREFLIST)->type == PREF_FLOAT)
678 arrp(prefList,i,PREFLIST)->value.fval = item->value.fval;
679 else if(arrp(prefList,i,PREFLIST)->type == PREF_STRING)
680 {
681 if (arrp(prefList,i,PREFLIST)->value.sval != NULL)
682 messfree(arrp(prefList,i,PREFLIST)->value.sval) ;
683 arrp(prefList,i,PREFLIST)->value.sval = item->value.sval ;
684 }
685 else
686 {
687 messout("ERROR: Prefile reading in the preference %s with invalid type, ignoring.",item->name);
688 return;
689 }
690
691 arrp(prefList,i,PREFLIST)->display = TRUE;
692 found = TRUE;
693 }
694 }
695
696 if(!found)
697 {
698 item->display = FALSE;
699 array(prefList,arrayMax(prefList),PREFLIST) = *item;
700 }
701
702 return;
703 } /* prefAdd */
704
705
706 void prefRead (ACEIN pref_in, Array prefList)
707 {
708 PREFLIST item;
709 int i,j;
710 char *junk, *cp;
711 KEY colKey = 0 ;
712
713 /* read in the preferences */
714 while ((junk = aceInCard (pref_in)))
715 {
716 if(junk[0] == '_')
717 {
718 i =1;
719 j=0;
720
721 while((isalpha((int)junk[i]) || junk[i] == '_') /*&&(junk[i]!=' ') &&junk[i]!='\t'*/ && j<30)
722 {
723 item.name[j++]= junk[i++];
724 }
725
726 item.name[j++] = '\0';
727 if(j == 1)
728 messout("Error reading name after _ in %s .acdbrc \n",junk) ;
729 else
730 {
731 while(junk[i] != '-' && junk[i]!='\n')
732 {
733 i++;
734 }
735
736 if(junk[i]=='-')
737 {
738 if(junk[i+1] == 't' || junk[i+1] == 'T')
739 {
740 item.value.bval = TRUE;
741 item.type = PREF_BOOLEAN;
742 item.deffunc.bfunc = NULL ;
743 item.editfunc.ifunc = NULL ; /* no bool version */
744 item.func.bfunc = NULL ;
745 prefAdd(prefList, &item);
746 }
747 else if(junk[i+1] == 'f' || junk[i+1] == 'F')
748 {
749 item.value.bval = FALSE;
750 item.type = PREF_BOOLEAN;
751 item.deffunc.bfunc = NULL ;
752 item.editfunc.ifunc = NULL ; /* no bool version */
753 item.func.bfunc = NULL ;
754 prefAdd(prefList, &item);
755 }
756 else if(junk[i+1] == 'I' || junk[i+1] == 'i')
757 {
758 cp = &junk[i+2];
759 if(sscanf(cp,"%d",&item.value.ival))
760 {
761 item.type=PREF_INT;
762 item.deffunc.ifunc = NULL ;
763 item.editfunc.ifunc = NULL ;
764 item.func.ifunc = NULL ;
765 prefAdd(prefList, &item);
766 }
767 else
768 messout("ERROR reading integer after %s",item.name);
769 }
770 else if(junk[i+1] == 'C' || junk[i+1] == 'c')
771 {
772 cp = &junk[i+2];
773 if(sscanf(cp,"%d",&item.value.ival))
774 {
775 item.type=PREF_COLOUR;
776 item.deffunc.ifunc = NULL ;
777 item.editfunc.ifunc = NULL ; /* no bool version */
778 item.func.ifunc = NULL ;
779 prefAdd(prefList, &item);
780 }
781 else if (lexword2key(cp,&colKey,0) &&
782 colKey >= _WHITE &&
783 colKey < _WHITE + 32)
784 {
785 item.type=PREF_COLOUR;
786 item.value.ival = WHITE + colKey - _WHITE ; /* tags to color enum */
787 item.deffunc.ifunc = NULL ;
788 item.editfunc.ifunc = NULL ; /* no bool version */
789 item.func.ifunc = NULL ;
790 prefAdd(prefList, &item);
791 }
792 else
793 messout("ERROR reading colour after %s",item.name);
794 }
795 else if(junk[i+1] == 'R' || junk[i+1] == 'r')
796 {
797 cp = &junk[i+2];
798 if(sscanf(cp,"%f",&item.value.fval))
799 {
800 item.type=PREF_FLOAT;
801 item.deffunc.ffunc = NULL ;
802 item.editfunc.ffunc = NULL ;
803 item.func.ffunc = NULL ;
804 prefAdd(prefList, &item);
805 }
806 else
807 messout("ERROR reading float after %s",item.name);
808 }
809 else if(junk[i+1] == 'S' || junk[i+1] == 's')
810 {
811 cp = &junk[i+2] ;
812
813 while((*cp == ' ' || *cp == '\t') && *cp != '\n')
814 {
815 cp++ ;
816 }
817
818 if (*cp != '\n')
819 {
820 item.value.sval = strnew(cp, 0) ;
821 item.type = PREF_STRING ;
822 item.deffunc.sfunc = NULL ;
823 item.editfunc.ifunc = NULL ;
824 item.func.ffunc = NULL ;
825 prefAdd(prefList, &item);
826 }
827 else
828 messout("ERROR reading string after %s", item.name);
829 }
830 else
831 {
832 messout("Error reading value for %s.",item.name);
833 }
834 }
835 }
836 }
837 }
838
839 return ;
840 } /* prefRead */
841
842
843 /* Set a callback function which will check a given value for a preference */
844 /* and if it is invalid will provide a valid "default" value. */
845 /* Note that when the caller sets this function, we call it back immediately */
846 /* to get the default value, this way we don't have to remember later and */
847 /* hopefully avoid synchronisation problems with the caller, i.e. they */
848 /* wouldn't have called us unless their callback function was ready to be */
849 /* called !! ...would they...??? */
850 /* */
851 BOOL prefSetDefFunc(char *name, void *func_ptr)
852 {
853 BOOL result = FALSE ;
854 int i;
855 PREFLIST *item;
856
857 if (!name || !*name || !func_ptr || !arrayExists(prefList_G))
858 messcrash("Internal Coding Error: %s.",
859 ((!name || !*name)
860 ? "null preference name as arg"
861 : ((!func_ptr)
862 ? "Callback function pointer is NULL"
863 : "prefInit() has not been called"))) ;
864
865 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
866 {
867 item = arrp(prefList_G,i,PREFLIST);
868 if(strstr(name, item->name))
869 {
870 if(item->type == PREF_BOOLEAN)
871 {
872 item->deffunc.bfunc = (PrefDefFuncBOOL)func_ptr ;
873 item->value.bval = item->deffunc.bfunc(item->value.bval) ;
874 }
875 else if(item->type == PREF_INT || item->type == PREF_COLOUR)
876 {
877 item->deffunc.ifunc = (PrefDefFuncInt)func_ptr ;
878 item->value.ival = item->deffunc.ifunc(item->value.ival) ;
879 }
880 else if(item->type == PREF_FLOAT)
881 {
882 item->deffunc.ffunc = (PrefDefFuncFloat)func_ptr ;
883 item->value.fval = item->deffunc.ffunc(item->value.fval) ;
884 }
885 else if(item->type == PREF_STRING)
886 {
887 item->deffunc.sfunc = (PrefDefFuncString)func_ptr ;
888 item->value.sval = item->deffunc.sfunc(item->value.sval) ;
889 }
890 result = TRUE ;
891 }
892 }
893
894 return result ;
895 }
896
897
898 /* Set an edit callback (will be called from graphNNNEditor()) to check */
899 /* value a user enters into an edit box. */
900 /* NOTE: this can only be set for int and float prefs. */
901 /* Really this replicates the above function but I don't want to */
902 /* rewrite the graphNNNEditor() stuff so we have to replicate. */
903 /* */
904 BOOL prefSetEditFunc(char *name, void *func_ptr)
905 {
906 BOOL result = FALSE ;
907 int i;
908 PREFLIST *item;
909
910 if (!name || !*name || !func_ptr || !arrayExists(prefList_G))
911 messcrash("Internal Coding Error: %s.",
912 ((!name || !*name)
913 ? "null preference name as arg"
914 : ((!func_ptr)
915 ? "Callback function pointer is NULL"
916 : "prefInit() has not been called"))) ;
917
918 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
919 {
920 item = arrp(prefList_G,i,PREFLIST);
921 if(strstr(name, item->name))
922 {
923 if(item->type == PREF_INT)
924 {
925 item->editfunc.ifunc = (PrefEditFuncInt)func_ptr ;
926 }
927 else if(item->type == PREF_FLOAT)
928 {
929 item->editfunc.ffunc = (PrefEditFuncFloat)func_ptr ;
930 }
931 else if(item->type == PREF_STRING)
932 {
933 item->editfunc.sfunc = (PrefEditFuncString)func_ptr ;
934 }
935 result = TRUE ;
936 }
937 }
938
939 return result ;
940 }
941
942
943 /* Set a callback function for a preference, these get called when someone */
944 /* sets a preference value. */
945 /* */
946 BOOL prefSetFunc(char *name, void *func_ptr)
947 {
948 BOOL result = FALSE ;
949 int i;
950 PREFLIST *item;
951
952 if (!name || !*name || !func_ptr || !arrayExists(prefList_G))
953 messcrash("Internal Coding Error: %s.",
954 ((!name || !*name)
955 ? "null preference name as arg"
956 : ((!func_ptr)
957 ? "Callback function pointer is NULL"
958 : "prefInit() has not been called"))) ;
959
960 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
961 {
962 item = arrp(prefList_G,i,PREFLIST);
963 if(strstr(name, item->name))
964 {
965 if(item->type == PREF_BOOLEAN)
966 {
967 item->func.bfunc = (PrefFuncBOOL)func_ptr ;
968 }
969 else if(item->type == PREF_INT || item->type == PREF_COLOUR)
970 {
971 item->func.ifunc = (PrefFuncInt)func_ptr ;
972 }
973 else if(item->type == PREF_FLOAT)
974 {
975 item->func.ffunc = (PrefFuncFloat)func_ptr ;
976 }
977 else if(item->type == PREF_STRING)
978 {
979 item->func.sfunc = (PrefFuncString)func_ptr ;
980 }
981 result = TRUE ;
982 }
983 }
984
985 return result ;
986 }
987
988
989 /* Returns the value for the char * name if it exists in the preferences. */
990 /* If it does not exist then returns the default FALSE. */
991 /* */
992 BOOL prefValue(char * name)
993 {
994 int i;
995 PREFLIST *item;
996
997 if (!name || !*name || !arrayExists(prefList_G))
998 /* should not need this ??? */
999 return FALSE ;
1000
1001 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
1002 {
1003 item = arrp(prefList_G,i,PREFLIST);
1004 if(strstr(name,item->name))
1005 {
1006 if(item->type == PREF_BOOLEAN)
1007 return item->value.bval ;
1008 else
1009 {
1010 messout("%s not of type boolean",item->name);
1011 return FALSE;
1012 }
1013 }
1014 }
1015 return FALSE; /* Default is set to false */
1016 }
1017
1018
1019 /* Returns the value for the char * name if it exists in the preferences. */
1020 /* If it does not exist then returns the default 0.0. */
1021 /* */
1022 float prefFloat(char * name)
1023 {
1024 int i;
1025 PREFLIST *item;
1026
1027 if (!name || !*name || !arrayExists(prefList_G))
1028 /* should not need this ??? */
1029 return 0.0 ;
1030
1031 for(i=0;i<arrayMax(prefList_G);i++)
1032 {
1033 item = arrp(prefList_G,i,PREFLIST);
1034 if(strstr(name,item->name))
1035 {
1036 if(item->type == PREF_FLOAT)
1037 return item->value.fval;
1038 else
1039 {
1040 messout("%s not of type float",item->name);
1041 return 0.0;
1042 }
1043 }
1044 }
1045 return 0.0; /* Default is set to 0.0 */
1046 }
1047
1048 /* Returns the value for the char * name if it exists in the preferences. */
1049 /* If it does not exist then returns the default FALSE. */
1050 int prefInt(char * name)
1051 {
1052 int i;
1053 PREFLIST *item;
1054
1055 if (!name || !*name || !arrayExists(prefList_G ))
1056 /* should not need this ??? */
1057 return 0 ;
1058
1059 for(i=0;i<arrayMax(prefList_G);i++)
1060 {
1061 item = arrp(prefList_G,i,PREFLIST);
1062 if(strstr(name,item->name))
1063 {
1064 if (item->type == PREF_INT ||
1065 item->type == PREF_COLOUR) /* should really use prefColour */
1066 return item->value.ival;
1067 else
1068 {
1069 messout("%s not of type integer",item->name);
1070 return 0;
1071 }
1072 }
1073 }
1074 return 0; /* Default is set to 0 */
1075 }
1076
1077 int prefColour (char * name)
1078 {
1079 /* Returns the value for the char * name if it exists in the preferences. */
1080 /* If it does not exist then returns the default FALSE. */
1081 int i;
1082 PREFLIST *item;
1083
1084 if (!name || !*name || !arrayExists(prefList_G))
1085 /* should not need this ??? */
1086 return 0 ;
1087
1088 for(i=0;i<arrayMax(prefList_G);i++)
1089 {
1090 item = arrp(prefList_G,i,PREFLIST);
1091 if(strstr(name,item->name))
1092 {
1093 if(item->type == PREF_COLOUR)
1094 return item->value.ival;
1095 else
1096 {
1097 messout("%s not of type colour",item->name);
1098 return 0;
1099 }
1100 }
1101 }
1102 return 0; /* Default is set to 0 */
1103 }
1104
1105
1106 /* Returns the value for the char * name if it exists in the preferences. */
1107 /* If it does not exist then returns the default NULL. */
1108 char *prefString(char * name)
1109 {
1110 char *result = NULL ;
1111 int i ;
1112 PREFLIST *item ;
1113
1114 if (!name || !*name || !arrayExists(prefList_G))
1115 result = NULL ;
1116
1117 for(i = 0 ; i < arrayMax(prefList_G) ; i++)
1118 {
1119 item = arrp(prefList_G,i,PREFLIST);
1120 if(strstr(name,item->name))
1121 {
1122 if (item->type == PREF_STRING)
1123 result = item->value.sval ;
1124 else
1125 messout("%s not of type string", item->name) ;
1126 }
1127 }
1128
1129 return result ;
1130 }
1131
1132
1133 /************************ eof *******************************/