Tagged files with gromacs 3.0 header and added some license info
[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 3.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Good gRace! Old Maple Actually Chews Slate
35  */
36 static char *SRCID_nleg_c = "$Id$";
37 #include <ctype.h>
38 #include <string.h>
39 #include <smalloc.h>
40 #include <macros.h>
41 #include "buttons.h"
42 #include "nleg.h"
43 #include "writeps.h"
44
45 typedef struct {
46   char  *tp;
47   unsigned long *col;
48   t_rgb rgb;
49 } t_atomcolor;
50
51 static t_atomcolor ac[] = {
52   { "O",  &LIGHTRED,     { 1,  0,  0   } },
53   { "N",  &LIGHTCYAN,    { 0,  0,  1   } },
54   { "NA", &LIGHTGREY,    { 0.6,0.6,0.6 } },
55   { "S",  &YELLOW,       { 1,  1,  0   } },
56   { "C",  &LIGHTGREEN,   { 0,  1,  0   } },
57   { "CL", &VIOLET,       { 1,  0,  1   } },
58   { "F",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
59   { "Z",  &LIGHTGREY,    { 0.6,0.6,0.6 } },
60   { "P",  &LIGHTBLUE,    { 0.4,0.4,1.0 } },
61   { "H",  &WHITE,        { 0.8,0.8,0.8 } },
62   { "C2", &GREEN,         { 0.8,0.2,0.2 } },
63   { "BE1", &VIOLET,       { 0.8,0.2,0.2 } },
64   { "W6R", &VIOLET,       { 0.8,0.2,0.2 } },
65   { "W5R", &LIGHTGREY,    { 0.8,0.2,0.2 } },
66 };
67 #define NAC asize(ac)
68
69 int search_ac(char *type)
70 {
71   int i,nb,mij,best,besti;
72
73   best=0;
74   besti=0;
75   if (type) {
76     for(i=0; (i<NAC); i++) {
77       mij=min((int)strlen(type),(int)strlen(ac[i].tp));
78       for(nb=0; (nb<mij); nb++)
79         if (type[nb] != ac[i].tp[nb])
80           break;
81       if (nb > best) {
82         best=nb;
83         besti=i;
84       }
85     }
86   }
87   return besti;
88 }
89
90 unsigned long Type2Color(char *type)
91 {
92   int i;
93   
94   i=search_ac(type);
95   
96   return *(ac[i].col);
97 }
98
99 t_rgb *Type2RGB(char *type)
100 {
101   int i;
102   
103   i=search_ac(type);
104   
105   return &(ac[i].rgb);
106 }
107
108 void DrawLegend(t_x11 *x11,t_windata *Win)
109 {
110 #define NLAB 6
111 #define COLS 3
112   static char *lab[NLAB] = { "C", "O", "H", "S", "N", "P" };
113   int  i,i0,dh,dw,w,y,x1,x0;
114   unsigned long cind;
115   real h_2;
116   
117   XClearWindow(x11->disp, Win->self);
118   w=Win->width;
119   h_2=Win->height/(2.0*NLAB/COLS);
120   dh=h_2-2;
121   dw=dh;
122
123   for (i=0; (i<NLAB); i++) {
124     i0=i % (NLAB/COLS);
125     x0=(i / (NLAB/COLS))*(Win->width/COLS)+AIR;
126     x1=x0+2*dw+AIR;
127     cind=Type2Color(lab[i]);
128     XSetForeground(x11->disp,x11->gc,cind);
129     y=((2*i0+1)*h_2);
130     XFillRectangle (x11->disp,Win->self,x11->gc,x0,y-dh,2*dw,2*dh);
131     XSetForeground(x11->disp,x11->gc,WHITE);
132     TextInRect(x11,Win->self,lab[i],x1,y-dh,w-x1,2*dh,
133                eXLeft,eYCenter);
134   }
135   XSetForeground(x11->disp,x11->gc,x11->fg);
136 }
137
138 static bool LegWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
139 {
140   t_legendwin *lw;
141
142   lw=(t_legendwin *)data;
143   switch(event->type) {
144   case Expose:
145     DrawLegend(x11,&lw->wd);
146     break;
147   default:
148     break;
149   }
150   return FALSE;
151 }
152
153 t_legendwin *init_legw(t_x11 *x11,Window Parent,
154                        int x,int y,int width,int height,
155                        unsigned long fg,unsigned long bg)
156 {
157   t_legendwin *lw;
158   
159   snew(lw,1);
160   InitWin(&lw->wd,x,y,width,height,1,"Legend Window");
161   lw->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
162   x11->RegisterCallback(x11,lw->wd.self,Parent,LegWCallBack,lw);
163   x11->SetInputMask(x11,lw->wd.self,ExposureMask);
164
165   return lw;
166 }
167
168 void map_legw(t_x11 *x11,t_legendwin *lw)
169 {
170   XMapWindow(x11->disp,lw->wd.self);
171 }
172
173
174 void done_legw(t_x11 *x11,t_legendwin *lw)
175 {
176   x11->UnRegisterCallback(x11,lw->wd.self);
177   sfree(lw);
178 }
179