78699e4bb9a6e03dc4e6a498f412ae09fecf34cb
[alexxy/gromacs.git] / src / ngmx / nload.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_nload_c = "$Id$";
30
31 #include <math.h>
32 #include <typedefs.h>
33 #include <macros.h>
34 #include <smalloc.h>
35 #include <string.h>
36 #include "nload.h"
37 #include "buttons.h"
38
39 void DrawLoad(t_x11 *x11,t_windata *Win,int nloads,int *loadinfo)
40 {
41   static char *Strings[] = { "Unbalance","Single Node","Your Ad Here ?"};
42   int  i,y0,bwidth,boff,bar,bmax,bmin,ym,yh;
43   int  *lb;
44   real bav,bscale;
45   char maxstr[6];
46
47   return;
48   
49   XClearWindow(x11->disp, Win->self);
50   y0=XTextHeight(x11->font)+AIR;
51   yh=(Win->height-y0)/2;
52   ym=y0+yh;
53   XSetForeground(x11->disp,x11->gc,WHITE);
54   XDrawLine(x11->disp,Win->self,x11->gc,0,y0,Win->width,y0);
55     
56   if (nloads >= 2) {
57     TextInRect(x11,Win->self,Strings[0],AIR,0,Win->width-2*AIR,y0,
58                eXLeft,eYCenter);
59     if (loadinfo[0] == 0) {
60       nloads--;
61       lb=&loadinfo[1];
62     }
63     else {
64       lb=loadinfo;
65       if (loadinfo[nloads-1] == 0) 
66         nloads--;
67     }
68     bwidth = (Win->width) / nloads;
69     boff   = (Win->width % nloads)/2;
70     bav    = 0.0; 
71     
72     bmax=bmin=lb[0];
73     
74     for (i=1; (i<nloads); i++) {
75       bmax = max (bmax,lb[i]);
76       bmin = min (bmin,lb[i]);
77       bav += lb[i];
78     }
79     bav/=nloads;
80     bscale = (yh-2)/max(fabs(bmax-bav),fabs(bav-bmin));
81     sprintf(maxstr,"(%d%%)",(int)(100.0*(bmax-bav)/bav));
82     TextInRect(x11,Win->self,maxstr,AIR,0,Win->width-2*AIR,y0,
83                eXRight,eYCenter);
84
85     XDrawLine(x11->disp,Win->self,x11->gc,0,ym,Win->width,ym);
86     if (bmax-bmin) {
87       for(i=0; i<nloads; i++) {
88         bar=(lb[i]-bav)*bscale;
89         if (bar != 0) {
90           if (bar > 0)
91             XFillRectangle(x11->disp,Win->self,x11->gc,
92                            (i*bwidth)+boff+1,ym-bar+1,bwidth-2,bar);
93           else
94             XFillRectangle(x11->disp,Win->self,x11->gc,
95                            (i*bwidth)+boff+1,ym,bwidth-2,-bar);
96         }
97       }
98       
99     }
100   }
101   else {
102     TextInRect(x11,Win->self,Strings[1],AIR,0,Win->width,y0,eXLeft,eYCenter);
103     TextInRect(x11,Win->self,Strings[2],AIR,y0,Win->width,
104                Win->height-y0,eXLeft,eYCenter);
105   }
106   XSetForeground(x11->disp,x11->gc,x11->fg);
107 }
108
109 static bool LWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
110 {
111   t_loadwin *lw;
112
113   lw=(t_loadwin *)data;
114   switch(event->type) {
115   case Expose:
116     DrawLoad(x11,&lw->wd,lw->nnodes,lw->load);
117     break;
118   default:
119     break;
120   }
121   return FALSE;
122 }
123
124 t_loadwin *init_lw(t_x11 *x11,Window Parent,
125                    int x,int y,int width,int height,
126                    unsigned long fg,unsigned long bg)
127 {
128   t_loadwin *lw;
129   
130   snew(lw,1);
131   snew(lw->load,MAXNODES);
132   lw->nnodes=1;
133   InitWin(&lw->wd,x,y,width,height,1,"Load Window");
134   lw->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
135   x11->RegisterCallback(x11,lw->wd.self,Parent,LWCallBack,lw);
136   x11->SetInputMask(x11,lw->wd.self,ExposureMask);
137
138   return lw;
139 }
140
141 void map_lw(t_x11 *x11,t_loadwin *lw)
142 {
143   XMapWindow(x11->disp,lw->wd.self);
144 }
145
146 void set_load(t_x11 *x11,t_loadwin *lw,int nnodes,int load[])
147 {
148   int  i;
149   bool bChange=FALSE;
150
151   lw->nnodes=nnodes;
152   for(i=0; (i<nnodes); i++)
153     if (lw->load[i] != load[i]) {
154       bChange=TRUE;
155       lw->load[i]=load[i];
156     }
157   if (bChange)
158     ExposeWin(x11->disp,lw->wd.self);
159 }
160
161 void done_lw(t_x11 *x11,t_loadwin *lw)
162 {
163   x11->UnRegisterCallback(x11,lw->wd.self);
164   sfree(lw);
165 }
166