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