fdd3357ac6e57aa1dfc878217ad0508f29c675a0
[alexxy/gromacs.git] / src / ngmx / nleg.c
1 /*
2  * $Id$
3  * 
4  *       This source code is part of
5  * 
6  *        G   R   O   M   A   C   S
7  * 
8  * GROningen MAchine for Chemical Simulations
9  * 
10  *               VERSION 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_nleg_c = "$Id$";
30
31 #include <ctype.h>
32 #include <string.h>
33 #include <smalloc.h>
34 #include <macros.h>
35 #include "buttons.h"
36 #include "nleg.h"
37 #include "writeps.h"
38
39 typedef struct {
40   char  *tp;
41   unsigned long *col;
42   t_rgb rgb;
43 } t_atomcolor;
44
45 static t_atomcolor ac[] = {
46   { "O",  &LIGHTRED,     { 1,  0,  0   } },
47   { "N",  &LIGHTCYAN,    { 0,  0,  1   } },
48   { "NA", &LIGHTGREY,    { 0.6,0.6,0.6 } },
49   { "S",  &YELLOW,       { 1,  1,  0   } },
50   { "C",  &LIGHTGREEN,   { 0,  1,  0   } },
51   { "CL", &VIOLET,       { 1,  0,  1   } },
52   { "F",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
53   { "Z",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
54   { "P",  &LIGHTBLUE,    { 0.4,0.4,1.0 } },
55   { "H",  &WHITE,        { 0.8,0.8,0.8 } },
56   { "C2", &GREEN,         { 0.8,0.2,0.2 } },
57   { "BE1", &VIOLET,       { 0.8,0.2,0.2 } },
58   { "W6R", &VIOLET,       { 0.8,0.2,0.2 } },
59   { "W5R", &LIGHTGREY,    { 0.8,0.2,0.2 } },
60 };
61 #define NAC asize(ac)
62
63 int search_ac(char *type)
64 {
65   int i,nb,mij,best,besti;
66
67   best=0;
68   besti=0;
69   if (type) {
70     for(i=0; (i<NAC); i++) {
71       mij=min((int)strlen(type),(int)strlen(ac[i].tp));
72       for(nb=0; (nb<mij); nb++)
73         if (type[nb] != ac[i].tp[nb])
74           break;
75       if (nb > best) {
76         best=nb;
77         besti=i;
78       }
79     }
80   }
81   return besti;
82 }
83
84 unsigned long Type2Color(char *type)
85 {
86   int i;
87   
88   i=search_ac(type);
89   
90   return *(ac[i].col);
91 }
92
93 t_rgb *Type2RGB(char *type)
94 {
95   int i;
96   
97   i=search_ac(type);
98   
99   return &(ac[i].rgb);
100 }
101
102 void DrawLegend(t_x11 *x11,t_windata *Win)
103 {
104 #define NLAB 6
105 #define COLS 3
106   static char *lab[NLAB] = { "C", "O", "H", "S", "N", "P" };
107   int  i,i0,dh,dw,w,y,x1,x0;
108   unsigned long cind;
109   real h_2;
110   
111   XClearWindow(x11->disp, Win->self);
112   w=Win->width;
113   h_2=Win->height/(2.0*NLAB/COLS);
114   dh=h_2-2;
115   dw=dh;
116
117   for (i=0; (i<NLAB); i++) {
118     i0=i % (NLAB/COLS);
119     x0=(i / (NLAB/COLS))*(Win->width/COLS)+AIR;
120     x1=x0+2*dw+AIR;
121     cind=Type2Color(lab[i]);
122     XSetForeground(x11->disp,x11->gc,cind);
123     y=((2*i0+1)*h_2);
124     XFillRectangle (x11->disp,Win->self,x11->gc,x0,y-dh,2*dw,2*dh);
125     XSetForeground(x11->disp,x11->gc,WHITE);
126     TextInRect(x11,Win->self,lab[i],x1,y-dh,w-x1,2*dh,
127                eXLeft,eYCenter);
128   }
129   XSetForeground(x11->disp,x11->gc,x11->fg);
130 }
131
132 static bool LegWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
133 {
134   t_legendwin *lw;
135
136   lw=(t_legendwin *)data;
137   switch(event->type) {
138   case Expose:
139     DrawLegend(x11,&lw->wd);
140     break;
141   default:
142     break;
143   }
144   return FALSE;
145 }
146
147 t_legendwin *init_legw(t_x11 *x11,Window Parent,
148                        int x,int y,int width,int height,
149                        unsigned long fg,unsigned long bg)
150 {
151   t_legendwin *lw;
152   
153   snew(lw,1);
154   InitWin(&lw->wd,x,y,width,height,1,"Legend Window");
155   lw->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
156   x11->RegisterCallback(x11,lw->wd.self,Parent,LegWCallBack,lw);
157   x11->SetInputMask(x11,lw->wd.self,ExposureMask);
158
159   return lw;
160 }
161
162 void map_legw(t_x11 *x11,t_legendwin *lw)
163 {
164   XMapWindow(x11->disp,lw->wd.self);
165 }
166
167
168 void done_legw(t_x11 *x11,t_legendwin *lw)
169 {
170   x11->UnRegisterCallback(x11,lw->wd.self);
171   sfree(lw);
172 }
173