Debian Codegraph @

w4/newkey.c

raw
1 /* File: newkey.c
2 * Author: Jean Thierry-Mieg ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1991
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 * -------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (MRC LMB, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description: user interface to edit operations
25 * at level of whole objects:
26 * new object
27 * alias/fuse (separate these ?)
28 * rename
29 * delete
30 * included in graphical ace compilations
31 * Exported functions:
32 * void addKeys (void)
33 * HISTORY:
34 * Last edited: May 6 10:19 2003 (edgrif)
35 * * Dec 1 23:55 1996 (rd)
36 * * Jun 6 18:36 1996 (rd)
37 * * Feb 8 00:04 1992 (rd): added rename, delete and changed alias
38 * * Jan 13 02:31 1992 (mieg): alias.
39 * * Dec 2 19:02 1991 (mieg): graphClassEntry
40 * Created: Mon Dec 2 19:01:40 1991 (mieg)
41 *-------------------------------------------------------------------
42 */
43 /************************************************************/
44
45 #include <wh/acedb.h>
46 #include <wh/aceio.h>
47 #include <wh/lex.h>
48 #include <wh/pick.h>
49 #include <wh/session.h>
50 #include <wh/bs.h>
51 #include <whooks/systags.h>
52 #include <wh/a.h>
53 #include <wh/display.h>
54 #include <wh/main.h>
55 #include <wh/tree.h>
56
57 /************************************************************/
58
59 static Graph addGraph = 0 ;
60 static int firstClassBox = 0 ;
61 static int currClassBox = 0 ;
62 static int currClass = 0 ;
63 static char nameText[128] ;
64 static Array box2class ;
65 static Array classList = 0 ;
66 static int classListBox = -1 ;
67 static char classNameBuf[32] = {'\0'} ;
68
69 /************************************************************/
70
71 static void findAndDisplayKey (void) ;
72 static void editNewKey (void) ;
73 static void (*preferredAction)(void) = editNewKey ;
74 static void destroy (void);
75 static void pick (int box, double x_unused, double y_unused, int modifier_unused);
76 static void resize (void);
77 static int newKeyClassComplete(char *cp, int len);
78 static void makeAlias (void);
79 static void makeAndDisplayKey (void);
80 static void findAndDisplayKey (void);
81 static void changeName (void);
82 static void deleteObject (void);
83 static void editNewKey (void);
84 static void classSelector (void);
85 static void doAction (char *);
86 static BOOL classListMenuCall (KEY k, int box);
87 static void pickObject (void);
88
89 static MENUOPT newMenu[] = {
90 { graphDestroy, "Quit"},
91 { help, "Help"},
92 { menuSpacer, "" },
93 { makeAndDisplayKey, "Create"},
94 { findAndDisplayKey, "Display"},
95 { editNewKey, "Edit"},
96 { makeAlias, "Alias-Fuse"},
97 { changeName, "Rename-Fuse"},
98 { deleteObject, "Delete"},
99 { 0, 0 }
100 } ;
101
102 /******************* main function ****************/
103
104 void addKeys (void)
105 {
106
107 if(!checkWriteAccess())
108 return ;
109
110 if (graphActivate (addGraph))
111 { graphPop () ;
112 resize();
113 return ;
114 }
115
116 addGraph = displayCreate("DtAlias") ;
117 graphRegister (DESTROY, destroy) ;
118 graphRegister (PICK, pick) ;
119 graphRegister (RESIZE, resize) ;
120 graphMenu (newMenu) ;
121
122 *nameText = 0 ;
123 resize();
124
125 return;
126 } /* addKeys */
127
128 /************************************************************/
129
130 static void resize (void)
131 {
132 int graphWidth, graphHeight ;
133 int i, ncol = 3 ;
134 int x, sep = 0 ;
135 float y ;
136 int nclasses = pickVocList[0].key ;
137 int classPickBox ;
138 int firstMenuButton;
139 MENUOPT *buttonItem;
140
141 if (!graphActivate(addGraph))
142 return;
143
144 graphFitBounds (&graphWidth, &graphHeight) ;
145
146 graphClear();
147
148 box2class = arrayReCreate (box2class, 32, int) ;
149
150 for (i = 1 ; i <= nclasses ; ++i)
151 if (strlen(pickVocList[i].text) > sep)
152 sep = strlen (pickVocList[i].text) ;
153 sep += 2 ;
154
155 classList = classListCreate (classList, TRUE, TRUE, FALSE, TRUE) ;
156 classPickBox = graphButton ("Class:", classSelector, 16, 2.2) ;
157 graphBoxFreeMenu(classPickBox, (FreeMenuFunction) classListMenuCall,
158 arrp (classList, 0, FREEOPT) ) ;
159 classListBox = graphBoxStart() ;
160 if (graphWidth > 26)
161 graphTextPtr(classNameBuf, 24, 2.35, graphWidth - 25) ;
162 graphBoxEnd() ;
163 graphBoxDraw(classListBox,BLACK,CYAN) ;
164
165 graphButton ("Pick object", pickObject, 2.0, 2.2) ;
166
167 firstMenuButton = graphButtons (newMenu, 2, 4.5, graphWidth) ;
168
169 x = 0 ;
170 /* find out how many rows graphButtons has drawn - very hacky ! */
171 i = 0;
172 buttonItem = &newMenu[0];
173 while (buttonItem->text)
174 {
175 if (strlen(buttonItem->text) > 0 && buttonItem->f)
176 i++;
177 buttonItem++;
178 }
179 graphBoxDim(firstMenuButton + i-1, 0, 0, 0, &y);
180 y += .5;
181
182
183 firstClassBox = 0;
184 for (i = 1 ; i <= nclasses ; ++i)
185 { KEY class1 = pickVocList[i].key ;
186 unsigned char dummy ;
187 int box;
188
189 pickIsA(&class1, &dummy) ;
190
191 if (pickList[class1 & 255].type != 'B')
192 continue ;
193 if (pickList[class1].protected)
194 continue ;
195 box = graphBoxStart();
196 if (!firstClassBox)
197 firstClassBox = box;
198 array (box2class,box,int) = pickVocList[i].key ;
199 graphText (name(pickVocList[i].key), 2.0 + (float)(x*sep), y) ;
200 graphBoxEnd () ;
201 if (!(++x%ncol))
202 { x = 0 ; y += 1 ; }
203 }
204
205 graphText ("Name: ", 2.0, 0.5) ;
206 graphCompletionEntry (newKeyClassComplete, nameText,
207 127, 9.0, 0.5, doAction) ;
208
209 currClass = currClassBox = 0 ;
210 graphFitBounds (&graphWidth, &graphHeight) ;
211 graphButtons (newMenu, 2, y + 2, graphWidth) ;
212 graphRedraw () ;
213
214 return;
215 } /* resize */
216
217 static void doAction (char *dummy_entry)
218 { (*preferredAction)() ; }
219
220 static BOOL checkName (void)
221 {
222 char *cp;
223
224 if (!checkWriteAccess ()) /* may be lost after graph was created */
225 return FALSE ;
226
227 if (!currClass)
228 { messout ("Please, first pick a class") ; return FALSE ; }
229 if (!*nameText)
230 {
231 ACEIN name_in;
232
233 name_in = messPrompt ("Please enter the name of "
234 "the object to be edited",
235 "","w", 0);
236 if (!name_in)
237 return FALSE ;
238 aceInNext (name_in) ;
239
240 strncpy (nameText, aceInPos(name_in), 127) ;
241 nameText[127] = 0;
242
243 aceInDestroy (name_in);
244 }
245
246 if (!*nameText)
247 { messout ("The name can not be empty") ; return FALSE ; }
248
249 cp = nameText ; cp-- ;
250 while (*cp++)
251 if (*cp == '*' || *cp == '?')
252 { messout("Please do not use * or ? in a new name") ;
253 return FALSE ;
254 }
255 return TRUE ;
256 }
257
258 /******************* action functions ******************/
259
260 static KEY makeKey (void)
261 { KEY key ;
262
263 if (!checkName()) return 0 ;
264 if (!lexaddkey (nameText, &key, currClass))
265 messout ("%s %s already exists.",
266 name(currClass), name(key)) ;
267 return key ;
268 }
269
270 static void displayKey (key)
271 {
272 if (pickType(key) == 'B')
273 { OBJ obj = bsUpdate(key) ; /* to give it a cache entry */
274 bsDestroy (obj) ;
275 display (key, 0, "TREE") ;
276 }
277 else
278 display (key, 0, 0) ; /* A type cannot be shown as TREE */
279 }
280
281 /****************/
282
283 static void findAndDisplayKey (void)
284 {
285 KEY key ;
286
287 preferredAction = findAndDisplayKey ;
288 if (!checkName()) return ;
289
290 if (!lexword2key (nameText, &key, currClass) &&
291 !(messQuery
292 (messprintf ("%s : %s does not yet exist\n"
293 "Do you want to Create it ?",
294 name (currClass), nameText)) &&
295 (key = makeKey())))
296 return ;
297
298 displayKey (key) ;
299 }
300
301 static void makeAndDisplayKey (void)
302 {
303 KEY key = makeKey () ;
304
305 preferredAction = makeAndDisplayKey ;
306 if (key)
307 displayKey (key) ;
308 }
309
310 static void editNewKey (void)
311 {
312 static Graph treeGraph = 0 ;
313 KEY key ;
314 OBJ obj ;
315 KEY class = currClass;
316 unsigned char dummy;
317
318 preferredAction = editNewKey ;
319 if (!checkName()) return ;
320 if (!lexword2key (nameText, &key, currClass))
321 { if (!messQuery
322 (messprintf
323 ("%s : %s does not exist, do you want to create it ?",
324 name (currClass), nameText)))
325 return ;
326 else
327 lexaddkey (nameText, &key, currClass) ;
328 }
329
330 pickIsA(&class, &dummy);
331 if (pickType(KEYMAKE(class, 0)) != 'B')
332 { makeKey () ;
333 messout ("Only Tree objects can be edited here, sorry") ;
334 return ;
335 }
336
337 obj = bsUpdate(key) ;
338 if (graphActivate (treeGraph))
339 treeDisplay (key, 0, TRUE, NULL) ;
340 else
341 { display (key, 0, "TREE") ;
342 treeGraph = graphActive() ;
343 }
344 bsDestroy(obj) ; /* All this is to see empty objects */
345 treeUpdate () ;
346 }
347
348 static void makeAlias (void)
349 {
350 KEY key ;
351 char *aliasName ;
352 int classe = currClass ;
353 ACEIN name_in;
354
355 preferredAction = makeAlias ;
356 if (!checkName())
357 return ;
358 if (!lexword2key (nameText, &key, classe))
359 { messout ("Old name %s is unknown", nameText) ; return ; }
360
361 name_in = messPrompt
362 ("Alias will result in both the old and new names being "
363 "recognized, but the name you type now will be the "
364 "one the database writes out (canonical name). If it is "
365 "the name of an existing object then the two will be fused. "
366 "Cancel and start again if you wish the name you "
367 "originally typed to be the canonical name."
368 "\n"
369 "\n"
370 "Give alias name:",
371 nameText, "t", 0);
372 if (!name_in)
373 return ;
374
375 aliasName = aceInWord (name_in);
376 if (strlen(aliasName) == 0)
377 {
378 messout ("The name can not be empty") ;
379 aceInDestroy (name_in);
380 return ;
381 }
382
383 if (lexAlias (key, aliasName, TRUE, TRUE))
384 display (key, 0, "TREE") ;
385 else
386 messout ("Alias failed, sorry") ;
387
388 aceInDestroy (name_in);
389
390 return;
391 } /* makeAlias */
392
393
394
395 static void changeName (void)
396 {
397 KEY key ;
398 char *newName ;
399 int classe = currClass ;
400 ACEIN name_in;
401
402 preferredAction = changeName ;
403 if (!checkName())
404 return ;
405 if (!lexword2key (nameText, &key, classe))
406 { messout ("Old name %s is unknown", nameText) ; return ; }
407
408 name_in = messPrompt
409 ("Rename is a hard rename function. Unless you are just "
410 "changing case, the old name will no "
411 "longer apply to this or any other current object (you can "
412 "reuse it for a new object). If the new name belongs to "
413 "an existing object the two objects will be fused. "
414 "For both names to apply, cancel "
415 "now and use the Alias menu entry. "
416 "\n"
417 "\n"
418 "Give new name:",
419 nameText, "t", 0);
420
421 if (!name_in)
422 return ;
423
424 newName = aceInWord (name_in) ;
425 if (strlen(newName) == 0)
426 {
427 messout ("The name cannot be empty") ;
428 aceInDestroy (name_in);
429 return ;
430 }
431
432 if (!lexAlias (key, newName, TRUE, FALSE))
433 messout ("Rename failed, sorry") ;
434
435 aceInDestroy (name_in);
436
437 return;
438 } /* changeName */
439
440 /************************* delete functions **********************/
441
442 static void deleteKey (KEY key)
443 { OBJ obj ;
444
445 switch (pickType (key))
446 {
447 case 'A':
448 arrayKill (key) ; /* neat, heh! */
449 break ;
450 case 'B':
451 if ((obj = bsUpdate (key)))
452 bsKill (obj) ;
453 else
454 messout ("Object %s is locked", name (key)) ;
455 break ;
456 }
457 }
458
459 static void deleteObject (void)
460 {
461 KEY key ;
462 int classe = currClass ;
463
464 preferredAction = deleteObject ;
465 if (!checkName()) return ;
466 if (!lexword2key (nameText, &key, classe))
467 messout ("Object %s is unknown", nameText) ;
468 else if (messQuery (messprintf ("Delete all data associated %s ?",
469 name(key))))
470 deleteKey (key) ;
471
472 return;
473 } /* deleteObject */
474
475
476 /****************************************/
477
478 static BOOL classListMenuCall (KEY k, int box)
479 {
480 /*
481 if (currClassBox)
482 graphBoxDraw (currClassBox, BLACK, WHITE) ;
483
484 currClassBox = box ;
485 */
486 currClass = k ;
487 strncpy(classNameBuf,name(currClass),31) ;
488 classNameBuf[31] = '\0' ;
489 graphBoxDraw(classListBox,BLACK,CYAN) ;
490
491 return TRUE ;
492 } /* classListMenuCall */
493
494
495 static void classSelector (void)
496 {
497 KEY key ;
498 if (graphSelect (&key, arrp (classList, 0, FREEOPT)))
499 classListMenuCall (key, 0) ;
500
501 graphBoxDraw(classListBox,BLACK,CYAN) ;
502
503 return;
504 } /* classSelector */
505
506
507 static void pick (int box, double x_unused, double y_unused, int modifier_unused)
508 {
509 if (!box)
510 return ;
511
512 if (box >= firstClassBox && box < arrayMax(box2class))
513 {
514 if (currClassBox)
515 graphBoxDraw (currClassBox, BLACK, WHITE) ;
516
517 currClassBox = box ;
518 graphBoxDraw (currClassBox, WHITE, BLACK) ;
519 currClass = arr(box2class,currClassBox,int) ;
520 strncpy(classNameBuf,name(currClass),31) ;
521 classNameBuf[31] = '\0' ;
522 graphBoxDraw(classListBox,BLACK,CYAN) ;
523 }
524 }
525
526
527 /******************* pick Object button *************/
528
529 static void blockPickObject (KEY key)
530 {
531 int i ;
532 KEY classKey ;
533 unsigned char mask ;
534
535 if (currClassBox)
536 graphBoxDraw (currClassBox, BLACK, WHITE) ;
537 currClassBox = 0 ;
538 currClass = class(key) ;
539
540 for (i = 0 ; i < arrayMax(box2class) ; ++i)
541 { classKey = arr(box2class, i, int) ;
542 pickIsA (&classKey, &mask) ;
543 if (!mask && classKey == currClass)
544 {
545 currClassBox = i ;
546 graphBoxDraw (i, WHITE, BLACK) ;
547 }
548 }
549 strncpy(classNameBuf,className(key),31) ;
550 classNameBuf[31] = '\0' ;
551 graphBoxDraw(classListBox,BLACK,CYAN) ;
552 strncpy (nameText, name(key), 127) ;
553 nameText[127] = 0 ;
554 graphCompletionEntry (0, nameText, 0, 0, 0, 0) ;
555 }
556
557 static void pickObject (void)
558 { displayBlock (blockPickObject, 0) ;
559 }
560
561 /****************************************************/
562
563 static void destroy (void)
564 {
565 addGraph = 0 ;
566 currClassBox = 0 ;
567 }
568
569
570 /*******************************************************/
571
572 static int newKeyClassComplete(char *cp, int len)
573 {
574 if (currClass)
575 return ksetClassComplete (cp, len , currClass) ;
576 else
577 return 0 ;
578 }
579
580
581 /************************** eof *****************************/
582