NCBI C Toolkit Cross Reference

C/desktop/fea2seg.c


  1 /*   fea2seg.c
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *            National Center for Biotechnology Information (NCBI)
  6 *
  7 *  This software/database is a "United States Government Work" under the
  8 *  terms of the United States Copyright Act.  It was written as part of
  9 *  the author's official duties as a United States Government employee and
 10 *  thus cannot be copyrighted.  This software/database is freely available
 11 *  to the public for use. The National Library of Medicine and the U.S.
 12 *  Government do not place any restriction on its use or reproduction.
 13 *  We would, however, appreciate having the NCBI and the author cited in
 14 *  any work or product based on this material
 15 *
 16 *  Although all reasonable efforts have been taken to ensure the accuracy
 17 *  and reliability of the software and data, the NLM and the U.S.
 18 *  Government do not and cannot warrant the performance or results that
 19 *  may be obtained by using this software or data. The NLM and the U.S.
 20 *  Government disclaim all warranties, express or implied, including
 21 *  warranties of performance, merchantability or fitness for any particular
 22 *  purpose.
 23 *
 24 * ===========================================================================
 25 *
 26 * File Name:  fea2seg.c
 27 *
 28 * Author:  Denis Vakatov, Jinghui Zhang
 29 *
 30 * $Revision: 6.4 $
 31 *
 32 * File Description:
 33 *  Draw style-dependent feature images for the feature legend map
 34 *  Draw gene images for the gene legend map
 35 *
 36 * ----------------------------------------------------------------------------
 37 * $Log: fea2seg.c,v $
 38 * Revision 6.4  1999/10/04 17:46:22  kans
 39 * include vibrant.h
 40 *
 41 * Revision 6.3  1999/05/06 21:21:13  vakatov
 42 * Stricter type casting
 43 *
 44 * Revision 6.2  1998/06/16 18:33:46  kans
 45 * fixed unix compiler warnings
 46 *
 47 * Revision 6.1  1997/09/16 21:24:50  vakatov
 48 * SmartDrawText():  restore font after the AddTextLabel() call
 49 * fit_string():  use FitStringWidth() functionality
 50 *
 51 * Revision 1.7  1997/08/18 15:47:32  vakatov
 52 * Fixed the doubling Log and Revision templates
 53 *
 54 * Revision 1.6  1997/07/18 21:44:57  zjing
 55 * make SmartDrawText() external
 56 *
 57 * Revision 1.5  1997/07/16 20:51:40  vakatov
 58 * [_DEBUG] typo fixed
 59 *
 60 * Revision 1.4  1997/03/17  23:00:08  zjing
 61 * correct a bug in the function that draws the map legend
 62 *
 63 * Revision 1.3  1997/03/06  17:28:27  vakatov
 64 * Added funcs gene2segment() and compose_g_legend() to draw gene legend map;
 65 * and test code for these.
 66 * Now use "Int4"(instead of "Int2") for all coords and dimensions.
 67 *
 68 * Revision 1.2  1996/12/11  16:51:35  vakatov
 69 * Use color when drawing symbol primitives;  added <fstyle.h>;
 70 * +NCBI header, +log
 71 * ----------------------------------------------------------------------------
 72 */
 73 
 74 #include <vibrant.h>
 75 #include <fea2seg.h>
 76 #include <fstyle.h>
 77 
 78 
 79 /*******************************************************************
 80  *  FEATURE
 81  */
 82 
 83 /* convert_color() and add_attribute_pen() from "drawseq.c" */
 84 
 85 #define RGB_B(x) (Uint1)((x)&255)
 86 #define RGB_G(x) (Uint1)(((x)>>8)&255)
 87 #define RGB_R(x) (Uint1)(((x)>>16)&255)
 88 
 89 static Boolean convert_color(Int4 val, Uint1Ptr color)
 90 {
 91   if(val<0 || color == NULL)
 92     return FALSE;
 93   color[0] = RGB_R(val);
 94   color[1] = RGB_G(val);
 95   color[2] = RGB_B(val);
 96   return TRUE;
 97 }
 98 
 99 static void add_attribute_pen(SegmenT seg, Int2 p_class, Int2 sub_class)
100 {
101   Uint1 color[3];
102   Uint1 flags = COLOR_ATT|STYLE_ATT|SHADING_ATT|WIDTH_ATT;
103   Int4  c_val;
104   Uint1 linestyle;
105   Uint1 penwidth;
106   Uint1 shading;
107 
108   c_val = GetMuskCParam(p_class, sub_class, MSM_COLOR);
109   convert_color(c_val, color);
110 
111   linestyle = (Uint1)GetMuskCParam(p_class, sub_class, MSM_LTYPE);
112   penwidth  = (Uint1)GetMuskCParam(p_class, sub_class, MSM_PENWIDTH);
113   shading   = (Uint1)GetMuskCParam(p_class, sub_class, MSM_SHADING);
114 
115   AddAttribute(seg, flags, &color[0], linestyle, shading, penwidth, COPY_MODE);
116 }
117 
118 
119 static CharPtr feature_name(Int2 feature)
120 {
121   /* from "fstyle.c" */
122   extern CharPtr mSM_allfeatureNames[];
123 
124   if (feature >= FEATDEF_ANY)
125     return NULL;
126 
127   if (feature > 0)
128     return  mSM_allfeatureNames[feature];
129 
130   switch ( feature )
131     {
132     case MSM_EXTRA_GENBANK:
133       return "GenBank";
134     case MSM_EXTRA_MEDLINE:
135       return "MedLine";
136     case MSM_EXTRA_BOTH:
137       return "GB+Med";
138     }
139 
140   return NULL;
141 }
142 
143 
144 static Boolean feature2image(SegmenT seg,
145                              Int2 feature,
146                              const BoxInfo PNTR limits)
147 {
148   BigScalar style, shape;
149   Int2      arrow = NO_ARROW;
150   Int4      x0 = 0, x1 = 0, x2 = 0, x3 = 0;
151   Int4      y = (limits->top + limits->bottom) / 2;
152   Int4      x = (limits->left + limits->right) / 2;
153 
154   /* get the feature style's attributes */
155   style = GetMuskCParam(feature, MSM_SEGMENT, MSM_STYLE);
156   shape = style & MSM_SEG_FORM;
157 
158   /* special processing -- for BOX and LINE only */ 
159   if (shape == MSM_SEG_BOX  ||  shape == MSM_SEG_LINE)
160     {
161       Int4 prim_width = (limits->right - limits->left) / 3;
162       x0 = limits->left;
163       x1 = limits->left  + prim_width;
164       x2 = limits->right - prim_width;
165       x3 = limits->right;
166 
167       /* draw gap, if necessary */
168       switch ( Nlm_GetMuskCParam(feature, MSM_FGAP, MSM_STYLE) )
169         {
170         case MSM_GAP_LINE:
171           add_attribute_pen(seg, feature, MSM_FGAP);
172           AddLine(seg, x1, y, x2, y, FALSE, 0);
173           break;
174         case MSM_GAP_ANGLE:
175           add_attribute_pen(seg, feature, MSM_FGAP);
176           AddLine(seg, x1, y, x, limits->top, FALSE, 0);
177           AddLine(seg, x2, y, x, limits->top, FALSE, 0);
178           break;
179         }
180 
181       if (style & (Int4)MSM_SEG_SHOWORIENT)
182         arrow = RIGHT_ARROW;
183       add_attribute_pen(seg, feature, MSM_SEGMENT);
184     }
185         
186 
187   /* draw the feature */
188   switch ( shape )
189     {
190     case MSM_SEG_BOX:
191       {
192         AddRectangle(seg,
193                      x0, limits->top, x1, limits->bottom, FALSE, TRUE, 0);
194         AddRectangle(seg,
195                      x2, limits->top, x3, limits->bottom, arrow, TRUE, 0);
196 
197         if ( Nlm_GetMuskCParam(feature, MSM_SEG_BORD, MSM_TRUEFALSE) )
198           {
199             add_attribute_pen(seg, feature, MSM_SEG_BORD);
200             AddRectangle(seg,  x0, limits->top, x1, limits->bottom,
201                          FALSE,     FALSE, 0);
202             AddRectangle(seg,  x2, limits->top, x3, limits->bottom,
203                          arrow, FALSE, 0);
204           }
205         break;
206       }
207 
208     case MSM_SEG_LINE:
209       {
210         AddLine(seg, x0, y, x1, y, FALSE,     0);
211         AddLine(seg, x2, y, x3, y, (Boolean)arrow, 0);
212         break;
213       }
214 
215     default:
216       { 
217         Int2 symbol;
218         switch ( shape )
219           {
220           case MSM_SEG_SYM_RECT:
221             symbol = RECT_SYMBOL;
222             break;
223           case MSM_SEG_SYM_DIAMOND:
224             symbol = DIAMOND_SYMBOL;
225             break;
226           case MSM_SEG_SYM_OVAL:
227             symbol = OVAL_SYMBOL;
228             break;
229           case MSM_SEG_SYM_TRIANGLE:
230             symbol = UP_TRIANGLE_SYMBOL;
231             break;
232           default:
233             return FALSE;  /* unknown feature shape */
234           }
235 
236         add_attribute_pen(seg, feature, MSM_SEGMENT);
237         AddSymbol(seg, x, y, symbol, TRUE, MIDDLE_CENTER, 0);
238         break;
239       }
240   }
241 
242   return TRUE;
243 }
244 
245 
246 /*
247  * Calculate(based on the presently active font) maximum number
248  * of characters from the string "str" which can be fit into
249  * "*max_width" pixels.
250  * Put the size of the resulting string(in pixels) to "*max_width".
251  */
252 static size_t fit_string(const Char *str, Int4 *max_width)
253 {
254   size_t len = FitStringWidth(str, *max_width);
255   *max_width = TextWidth((CharPtr)str, len);
256   return len;
257 }
258 
259 
260 extern Boolean SmartDrawText(SegmenT seg,
261                              const Char *text, FonT font,
262                              Int4 left, Int4 top,
263                              Int4Ptr width, Int4Ptr height)
264 {
265   Int4 x_height    = (height  &&  *height > 0) ? *height : INT4_MAX;
266   Int4 line_height = LineHeight();
267   Int4 n, n_lines  = x_height / line_height;
268   Int4 max_width   = 0;
269   Nlm_CharPtr str, stream;
270 
271   SelectFont( font );
272   if (!seg  ||  !text  ||  !*text  ||  !width  ||  line_height < 0  ||
273       x_height < line_height  ||  *width < MaxCharWidth()  ||
274       (stream = str = text2stream( text )) == NULL)
275     {
276       if ( height )
277         *height = 0;
278       return FALSE;
279     }
280 
281   for (n = 0;  *str != '\0'  &&  n < n_lines;  n++, top -= line_height)
282     {
283       Int4    x_width = *width;
284       size_t  n_fit   = fit_string(str, &x_width);
285       int     dash;
286       size_t  n_print = stream2text(str, n_fit, &dash);
287       CharPtr s       = (CharPtr)MemNew(n_fit + 1);
288       Nlm_CharPtr ruled_str;
289 
290       ASSERT ( n_print <= n_fit ); 
291 
292       MemCpy(s, str, n_print);
293       s[n_print] = '\0';
294       if ( dash )
295         {
296           size_t pos = n_print;
297           while (--pos  &&  !IS_WHITESP(s[pos]));
298           if ( pos )
299             s[n_print = pos] = '\0';
300           else
301             s[--n_print] = '-';
302         }
303       str += n_print;
304 
305       ruled_str = rule_line(s, n_fit, RL_Spread);
306       MemFree( s );
307 
308       x_width = StringWidth( ruled_str );
309       if (x_width > max_width)
310         max_width = x_width;
311 
312       AddTextLabel(seg, left, top, ruled_str, font, 0, LOWER_RIGHT, 0);
313       MemFree( ruled_str );
314       SelectFont( font ); /* restore -- as it gets reset in AddTextLabel() */
315     }
316   MemFree( stream );
317 
318   *width = max_width;
319   if ( height )
320     *height = n * line_height;
321 
322   return (Boolean)(max_width > 0);
323 }
324 
325 
326 /*
327  *  The font must be already selected by the moment of the function call
328  */
329 static Boolean feature2label(SegmenT seg, Int2 feature,
330                              BigScalar align,
331                              Int4 left, Int4 top, Int4 width,
332                              const Char *name)
333 {
334   FonT font  = (FonT)GetMuskCParam(feature, MSM_FLABEL, MSM_FONT);
335   Int4 color = (Int4)GetMuskCParam(feature, MSM_FLABEL, MSM_COLOR);
336   const Char *str  = name ? name : feature_name( feature );
337   Uint1  color3arr[3];
338   Boolean ok, free_str = FALSE;
339 
340   if (str == NULL  ||  *str == '\0'  ||
341       !convert_color(color, color3arr)  ||  width < 1)
342     return FALSE;
343 
344   /* fit the label into the given maximum width(in pixels) */
345   SelectFont( font );
346   {{
347     size_t max_length = fit_string(str, &width);
348     if ( !max_length )
349       return FALSE;
350 
351     if (max_length < StringLen( str ))
352       {
353         str = StringNCat((char*)MemNew(max_length + 1), str, max_length);
354         if ( !str )
355           return FALSE;
356         free_str = TRUE;
357       }
358   }}
359 
360   /* draw label */
361   AddAttribute(seg, COLOR_ATT, color3arr, 0, 0, 0, 0);
362   ok = (Boolean)(AddTextLabel(seg, left, top, (CharPtr)str, font,
363                               0, (Int2)align, 0) != NULL);
364 
365   if ( free_str )
366     MemFree( (CharPtr)str );
367   return ok;
368 }
369 
370 
371 extern SegmenT feature2segment(SegmenT parent,
372                                const Char PNTR style_name,
373                                Int2 feature,
374                                Int4 left, Int4 top,
375                                Int4 label_width, Int4 image_width,
376                                Int4Ptr height)
377 {
378   SegmenT   seg = NULL;
379   Int2      actual_style = Nlm_GetMuskCurrentSt();
380   Boolean   switch_style = (Boolean)StrCmp(style_name,
381                                            Nlm_GetMuskStyleName(actual_style));
382   Int4      seg_space;
383   BigScalar label_align;
384   FonT      label_font;
385   Int4      image_height, label_height, max_height;
386   BoxInfo   image_box;
387   Int4      label_left, label_top;
388 
389   if (feature_name( feature ) == NULL)
390     return NULL;
391 
392   /* create segment to draw in */
393   seg = CreateSegment(parent, 0, 0);
394   if (seg == NULL)
395     return NULL;
396 
397   /* set working style, if necessary */
398   if ( switch_style )
399     SetMuskCurrentSt( (CharPtr)style_name );
400 
401   /* get the feature size and alignment attributes */
402   seg_space    = (Int4)GetMuskCParam(MSM_TOPSTYLE, MSM_SPACE,   MSM_HEIGHT);
403   image_height = (Int4)GetMuskCParam(feature,      MSM_SEGMENT, MSM_HEIGHT);
404   label_align  =       GetMuskCParam(MSM_TOPSTYLE, MSM_LABEL,   MSM_STYLE );
405   label_font   = (FonT)GetMuskCParam(feature,      MSM_FLABEL,  MSM_FONT  );
406   SelectFont( label_font );
407   label_height = LineHeight();
408 
409   if ((label_align == MSM_LABEL_TOP  ||  label_align == MSM_LABEL_BOTTOM))
410     {
411       Int2 l_width = StringWidth( feature_name( feature ) );
412       if (l_width < label_width)
413         label_width = l_width;
414       if (label_width < image_width)
415         label_width = image_width;
416       max_height = label_height + image_height;
417     }
418   else
419     max_height = MAX(label_height, image_height);
420 
421   if (height != NULL)
422     *height = max_height + seg_space;
423 
424   /* deploy the feature's image and label */
425   image_box.left = left;
426   image_box.top  = top;
427   label_left      = left;
428   label_top       = top;
429   switch ( label_align )
430     {
431     case MSM_LABEL_BOTTOM:
432       label_top      -= image_height;
433       label_left     += label_width / 2;
434       label_align = LOWER_CENTER;
435       break;
436     case MSM_LABEL_TOP:
437       image_box.top  -= label_height;
438       label_left     += label_width / 2;
439       label_align = LOWER_CENTER;
440       break;
441     case MSM_LABEL_LEFT:
442       image_box.left += label_width;
443       label_left     += label_width;
444       label_top      += (label_height - max_height) / 2;
445       image_box.top  += (image_height - max_height) / 2;
446       label_align = LOWER_LEFT;
447       break;
448     case MSM_LABEL_RIGHT:
449     default:
450       label_left     += image_width;
451       label_top      += (label_height - max_height) / 2;
452       image_box.top  += (image_height - max_height) / 2;
453       label_align = LOWER_RIGHT;
454       break;
455     }
456   image_box.right  = image_box.left + image_width;
457   image_box.bottom = image_box.top  - image_height;
458 
459   /* draw the fearure's image and label */
460   if (!feature2image(seg, feature, &image_box)  ||
461       !feature2label(seg, feature, label_align,
462                      label_left, label_top, label_width, NULL))
463     {
464       DeleteSegment( seg );
465       return NULL;
466     }
467 
468   return seg;
469 }
470       
471 
472 extern SegmenT pic_for_f_legend(ValNodePtr list, CharPtr style_name,
473                                 Int4 image_width)
474 {
475   ValNodePtr curr;
476   Int4 max_width = 0;  /* calculate the maximum label width */
477 
478   if (list == NULL)
479     return NULL;
480 
481   for (curr = list;  curr != NULL;  curr = curr->next)
482     {
483       Int2 type = (Int2)curr->data.intvalue;
484       FonT font = (FonT)GetMuskCParam(type, MSM_FLABEL, MSM_FONT);
485       SelectFont(font);
486       {{
487         Int2 x_width = StringWidth( feature_name(type) );
488         max_width = MAX(max_width, x_width);
489       }}
490     }
491 
492   {{
493     SegmenT pic = CreatePicture();
494     Int4 left = 0;
495     Int4 top  = 0;
496     for (curr = list;  curr != NULL;  curr = curr->next)
497       {
498         Int4 height;
499         Int2 type = (Int2)curr->data.intvalue;
500         feature2segment(pic, style_name, type,
501                         left, top, max_width, image_width,
502                         &height);
503         top -= height;
504       }
505 
506     return pic;
507   }}
508 }
509 
510 
511 /*******************************************************************
512  *  GENE
513  */
514 
515 #include <legend.h>
516 
517 extern SegmenT gene2segment(SegmenT parent,
518                             const UserField *gene,
519                             Int4 left, Int4 top,
520                             Int4 image_width, Int4 label_width,
521                             Int4Ptr height)
522 {
523   SegmenT seg = NULL;
524 
525   typedef struct {
526     Int4  shape;
527     Int4  num;
528     Int4  style;
529     Uint4 rgb[3];
530   }  *GeneDataPtr;
531 
532   GeneDataPtr gene_data;
533 
534   if (!gene  ||  gene->num != 6  ||  gene->choice != 8  ||
535       !gene->data.ptrvalue)
536     return NULL;
537 
538   gene_data = (GeneDataPtr)gene->data.ptrvalue;
539 
540   /* create the segment to draw in */
541   seg = CreateSegment(parent, 0, 0);
542   if (seg == NULL)
543     return NULL;
544 
545   {{ /* set image color */
546     static const Uint1 default_rgb[3] = { 0, 0, 0 };
547     Uint1 color3arr[3];
548 
549     int i;
550     for (i = 0;  i < 3;  i++)
551       color3arr[i] = (Uint1)(gene_data->rgb[i] != NULL_COLOR ?
552                              gene_data->rgb[i] : default_rgb[i]);
553 
554     AddAttribute(seg, COLOR_ATT, color3arr, 0, 0, 0, 0); 
555   }}
556 
557   {{ /* draw the gene image */
558     Int2    symbol = (Int2)gene_data->shape;
559     Boolean filled = (Boolean)(gene_data->style == SOLID_STYLE);
560     Int4    nSym   = gene_data->num;
561     if (symbol == NULL_SYMBOL)
562       {
563         symbol = RECT_SYMBOL;
564         filled = TRUE;
565         nSym   = 1;
566       }
567 
568     ASSERT ( nSym > 0 );
569     AddSymbol(seg, left, top, symbol, filled, LOWER_RIGHT, 0);
570     {{
571       Int4 x = left;
572 
573 #define symbol_width 8
574 #ifndef symbol_width
575       /* (for some weird reason, most of the PrimitivE' "try-get-limits"
576          functions return a 4-pixel outset box...) */
577       BoxInfo box;
578       Int4    symbol_width;
579       SegmentBox(seg, &box);
580       symbol_width = box.right - box.left;
581 #endif
582       {{
583         Int4 max_nSym = image_width / symbol_width;
584         if (nSym > max_nSym)
585           nSym = max_nSym;
586       }}
587       while ( --nSym )
588         {
589           x += symbol_width + 2;
590           AddSymbol(seg, x, top, symbol, filled, LOWER_RIGHT, 0);
591         }
592     }}      
593   }}
594 
595   /* draw the gene description, if any */
596   {{
597     FonT font  = (FonT)GetMuskCParam(FEATDEF_GENE, MSM_FLABEL, MSM_FONT);
598     Int4 color = (Int4)GetMuskCParam(FEATDEF_GENE, MSM_FLABEL, MSM_COLOR);
599     Uint1  color3arr[3];
600 
601     if ( convert_color(color, color3arr) )
602       AddAttribute(seg, COLOR_ATT, color3arr, 0, 0, 0, 0);
603 
604     SmartDrawText(seg, gene->label->str, font,
605                     left + image_width, top, &label_width, NULL);
606   }}
607 
608   /* calculate the resulting height, if necessary */
609   if ( height )
610     {
611       BoxInfo box;
612       SegmentBox(seg, &box);
613       *height = box.top - box.bottom;
614     }
615 
616   return seg;
617 }
618 
619 
620 extern SegmenT compose_g_legend(SegmenT     parent,
621                                 const Char *title,
622                                 FonT        title_font,
623                                 const UserObject *genes,
624                                 Int4 left, Int4 top,
625                                 Int4 max_width, Int4Ptr height)
626 {
627   Int4 x_width  = max_width;
628   Int4 x_height = 0;
629   Int4 x_top    = top;
630 
631   SegmenT seg = CreateSegment(parent, 0, 0);
632   if (seg == NULL)
633     return NULL;
634 
635   /* title */
636   if ( SmartDrawText(seg, title, title_font,
637                        left, x_top, &x_width, &x_height) )
638     {
639       x_top -= x_height + 3;
640       /* AddLine(seg, left, x_top, left + max_width, x_top, FALSE, 0); 
641       x_top -= 5; */
642     }
643 
644   /* gene images/descriptions */
645   {{
646     UserFieldPtr ufp;
647 
648     for (ufp = genes->data;  ufp;  ufp = ufp->next)
649       {
650         gene2segment(seg, ufp, left, x_top, 32, max_width-32, &x_height);
651         x_top -= x_height;
652       }
653   }}
654 
655   if ( height )
656     *height = top - x_top + 1;
657 
658   return seg;
659 }
660 
661 
662 
663 /*******************************************************************
664  *  TESTs
665  *
666  * Output to window(s):
667  *   LIBRARIES:  ncbidesk vibrant ncbiobj ncbi
668  *   CPP_DEFS:   -DTEST_MODULE_FEA2SEG
669  *
670  * Output to GIF-file(s):
671  *   LIBRARIES:  ncbidesk vibgif vibrant ncbiobj ncbi
672  *   CPP_DEFS:   -DTEST_MODULE_FEA2SEG -DWIN_GIF -UWIN_MOTIF
673  *
674  * NB:  Use -DNO_TEST_STYLE to suppress feature legend test.
675  *      Use -DNO_TEST_GENES to suppress gene    legend test.
676  */
677 
678 
679 #ifdef TEST_MODULE_FEA2SEG
680 
681 #ifndef WIN_GIF
682 #include <vibrant.h>
683 #include <viewer.h>
684 #else
685 #include <ncbigif.h>
686 #include <ncbifile.h>
687 #endif /* WIN_GIF */
688 
689 #include <picture.h> 
690 #include <fstyle.h>
691 #include <fea2seg.h>
692 
693 /* Quit/Cleanup for GUI tests
694  */
695 #ifndef WIN_GIF
696 static void ThatsAll(WindoW w) {
697   QuitProgram();
698 }
699 
700 typedef struct
701 {
702   SegmenT       ppp;
703 #ifndef NO_TEST_GENES
704   UserObjectPtr uop;
705 #endif
706 }  structCleanUp, *structCleanUpPtr;
707 
708 static void CleanUp(structCleanUpPtr cleanup_data)
709 {
710   DeletePicture( cleanup_data->ppp );
711 #ifndef NO_TEST_GENES
712   UserObjectFree( cleanup_data->uop );
713 #endif
714 }
715 
716 static void CleanUpCB(Nlm_GraphiC www, Nlm_VoidPtr cleanup_data)
717 {
718   CleanUp( (structCleanUpPtr)cleanup_data );
719 }
720 #endif
721 
722 
723 /* Draw feature legend for the given style("style_name")
724  */
725 #ifndef NO_TEST_STYLE
726 static void test_style(const Char PNTR style_name)
727 {
728   static int style_no = 0;
729 
730   SegmenT ppp = CreatePicture();
731   SetMuskCurrentSt( (CharPtr)style_name );
732 
733   {{  /* draw features one after another */
734   Int4 height  = 0;
735   Int4 width   = 0;
736   Int4 top     = 700;
737   Int2 feature = 1;
738 
739   {{
740   SegmenT seg = NULL;
741   seg = feature2segment(ppp, style_name, MSM_EXTRA_MEDLINE,
742                         100, top, 70, 70,
743                         &height);
744   ASSERT ( seg );
745 
746   seg = feature2segment(ppp, style_name, MSM_EXTRA_GENBANK,
747                         300, top, 70, 70,
748                         &height);
749   ASSERT ( seg );
750 
751   seg = feature2segment(ppp, style_name, MSM_EXTRA_BOTH,
752                         500, top, 70, 70,
753                         &height);
754   ASSERT ( seg );
755   }}
756 
757   for (top  = 650;  feature <= 85  && top        > height;  top  -= height   )
758   {
759   Int4 left;
760   for (left = 0;    feature <= 85  && left+width <    700;  left += width + 5)
761     {
762       Int4 image_width = 40;
763       Int4 label_width = 70;
764       SegmenT seg = feature2segment(ppp, style_name, feature,
765                                     left, top, label_width, image_width,
766                                     &height);
767       ASSERT ( seg );
768       width = image_width + label_width;
769 
770       feature++;
771     }
772   }
773   }}
774 
775   {{  /* write the result to GIF-file or show it in the window */
776 #ifdef WIN_GIF
777     char s[128];
778     FILE *f;
779 
780     sprintf(s, "%d.gif", (int)Nlm_GetMuskCurrentSt());
781     f = FileOpen(s, "wb");
782     ASSERT ( f );
783     PictureToGIF(f, 700, 710, ppp, -10, -10, LOWER_LEFT, 1, 1, FALSE);
784     FileClose( f );
785 
786     DeletePicture( ppp );
787 #else
788     WindoW www = DocumentWindow((Int2)(-10 * style_no), (Int2)(-10 * style_no),
789                                 725, 725,
790                                 (CharPtr)style_name, ThatsAll, NULL);
791     VieweR vvv = CreateViewer(www, 700, 700, FALSE, FALSE);
792 
793     AttachPicture(vvv, ppp, 10, 10, LOWER_LEFT, 1, 1, NULL);
794 
795     {{
796       structCleanUpPtr cleanup_data = (structCleanUpPtr)
797         MemNew( sizeof(structCleanUp) );
798       cleanup_data->ppp = ppp;
799       SetObjectExtra(www, cleanup_data, CleanUpCB);
800     }}
801 
802     RealizeWindow( www );
803     Show( www );
804 #endif  /* WIN_GIF */
805   }}
806 
807   style_no++;
808 }
809 #endif /* !NO_TEST_STYLE */
810 
811 
812 /* Draw the gene legend map;  read its description from ASN-file
813  * ("legend_asn_file").
814  */
815 #ifndef NO_TEST_GENES
816 static void test_genes(const Char PNTR legend_asn_file)
817 {
818   static Char title[] = "\
819 0 1 2 3 4 5 6 7 8 9 a b c d e f g A B C D E F G \
820 W W W W W W W W W W W W W W W W W W W W W W W W W \
821 The NCBI (..... ..... .... .....) Software Development Toolkit \
822 was (!!!! !!!!!! !!!! !!!!) developed (QQQ QQQQQQQQ QQQQQQ QQQQ Q) \
823 for the production and (???? ????? ????? ? ??? ??? ) \
824 distribution of GenBank, Entrez, BLAST, and related services by NCBI. We make \
825 it freely available to the public without restriction to facilitate the";
826 
827   AsnIoPtr      aip = AsnIoOpen((CharPtr)legend_asn_file, "r");
828   UserObjectPtr uop = UserObjectAsnRead(aip, NULL);
829   UserFieldPtr  ufp = uop->data;
830   SegmenT       ppp = CreatePicture();
831 
832   Int4 left = 10;
833   Int4 top  = 700;
834 
835   AsnIoClose( aip );
836 
837   {{ /* draw gene legends one after(below) another */
838     ASSERT ( ufp );
839     for ( ;  ufp;  ufp = ufp->next)
840       {
841         Int4 height = 0;
842         VERIFY ( gene2segment(ppp, ufp, left, top, 30, 555, &height) );
843         top -= height;
844       }
845 
846     {{
847       FonT helv = CreateFont( Helvetica(10,0) );
848       Int4 width  = 150;
849       Int4 height = 150;
850       AddRectangle(ppp, left, top, left + width, top - height, FALSE,FALSE, 0);
851       VERIFY ( SmartDrawText(ppp, title, helv, left, top, &width, &height) );
852       AddRectangle(ppp, left, top, left + width, top - height, FALSE,FALSE, 0);
853 
854       top -= height + 10;
855     }}
856 
857     {{
858       FonT times = CreateFont( Times(14,0) );
859       CharPtr s;
860       size_t  i;
861       for (i = 1, s = title;  *s;  s++)
862         if (*s == '?')
863           *s = (Char)i++;
864       VERIFY ( compose_g_legend(ppp, title, times, uop, left,top, 250, NULL) );
865     }}
866   }}
867 
868 
869   {{ /* write the result to GIF-file or show it in the window */
870 #ifdef WIN_GIF
871     FILE *f;
872 
873     CharPtr s = StringSave( legend_asn_file );
874     StringNCpy(s + StringLen(s) - 3, "gif", 3);
875     f = FileOpen(s, "wb");
876     ASSERT ( f );
877     MemFree( s );
878 
879     PictureToGIF(f, 700, 710, ppp, -10, -10, LOWER_LEFT, 1, 1, FALSE);
880     FileClose( f );
881 
882     DeletePicture( ppp );
883     UserObjectFree( uop );
884 #else
885     WindoW www = DocumentWindow(-55, -55, 725, 725,
886                                 (CharPtr)legend_asn_file, ThatsAll, NULL);
887     VieweR vvv = CreateViewer(www, 700, 700, FALSE, FALSE);
888 
889     AttachPicture(vvv, ppp, 10, 10, LOWER_LEFT, 1, 1, NULL);
890 
891     {{
892       structCleanUpPtr cleanup_data = (structCleanUpPtr)
893         MemNew( sizeof(structCleanUp) );
894       cleanup_data->ppp = ppp;
895       cleanup_data->uop = uop;
896       SetObjectExtra(www, cleanup_data, CleanUpCB);
897     }}
898 
899     RealizeWindow( www );
900     Show( www );
901 #endif /* WIN_GIF */
902   }}
903 }
904 #endif /* !NO_TEST_GENES */
905 
906 
907 
908 
909 extern Int2 Main( void )
910 {
911 #ifdef WIN_GIF
912   SetUpDrawingTools();
913 #endif
914 
915   InitMuskStyles();
916 
917 #ifndef NO_TEST_STYLE
918   {{
919     Int2 style;
920 
921     for (style = GetMuskTotalSt();  style ;  )
922       {
923         test_style( GetMuskStyleName(--style) );
924       }
925   }}
926 #endif /* !NO_TEST_STYLE */
927 
928 #ifndef NO_TEST_GENES
929   {{
930     test_genes( "fea2seg.asn" );
931   }}
932 #endif
933 
934   ExitMuskStyles();
935 
936 #ifndef WIN_GIF
937   ProcessEvents();
938 #endif
939 
940   return 0;
941 }
942 
943 #endif /* TEST_MODULE_FEA2SEG */
944 
945 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.