4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
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)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_scrollw_c = "$Id$";
42 t_windata wd; /* Window structure */
43 int nlines,top; /* Number of lines, current top line */
44 char **lines; /* The strings */
45 int wheight,wwidth; /* The size of the window in chars */
46 XFontStruct *font; /* Font */
47 unsigned long fg,bg; /* Colours */
50 static void calc_scrollw(t_scrollw *sw,int w,int h)
54 sw->wheight=h/(YSPACE+XTextHeight(sw->font));
55 sw->wwidth=w/XTextWidth(sw->font,"W",1);
58 static bool SWCallback(t_x11 *x11,XEvent *event,Window w,void *data)
61 int i,y,nl,barw,btop,bheight;
66 /* Calc some bar data */
68 h=XTextHeight(sw->font)+YSPACE;
69 frac=min(1.0,((real)sw->wheight)/((real)sw->nlines));
70 btop=((((real)sw->top)/((real)sw->nlines)))*(sw->wd.height);
71 bheight=frac*sw->wd.height;
76 nl=min(sw->nlines,sw->top+sw->wheight);
79 XClearWindow(x11->disp,w);
81 printf("btop: %d, bheight: %d, frac: %e, h: %e\n",btop,bheight,frac,h);
84 XSetForeground(x11->disp,x11->gc,LIGHTGREY);
85 XFillRectangle(x11->disp,w,x11->gc,2,btop+2,barw-4,bheight-4);
86 XDrawLine(x11->disp,w,x11->gc,barw,0,barw,sw->wd.height);
89 XSetForeground(x11->disp,x11->gc,sw->fg);
90 for(i=sw->top; (i<nl); i++) {
91 SpecialTextInRect(x11,sw->font,w,
92 sw->lines[i],barw+2,y,sw->wd.width-barw-4,(int)h,
96 XSetForeground(x11->disp,x11->gc,x11->fg);
99 calc_scrollw(sw,event->xconfigure.width,event->xconfigure.height);
102 if (event->xbutton.x < barw) {
103 int y=event->xbutton.y;
105 if (sw->nlines > sw->wheight) {
107 sw->top=max(0,sw->top-1);
108 else if (y>btop+bheight)
109 sw->top=min(sw->nlines-sw->wheight,sw->top+1);
112 ExposeWin(x11->disp,sw->wd.self);
123 t_scrollw *init_scrollw(t_x11 *x11,Window parent,int x,int y,int w,int h,
124 unsigned long fg,unsigned long bg)
130 InitWin(&sw->wd,x,y,w,h,1,"Scroll Window");
134 sw->wd.self=XCreateSimpleWindow(x11->disp,parent,x,y,w,h,
135 sw->wd.bwidth,fg,bg);
136 x11->RegisterCallback(x11,sw->wd.self,parent,SWCallback,sw);
137 x11->SetInputMask(x11,sw->wd.self,ExposureMask | ButtonPressMask |
138 StructureNotifyMask);
139 calc_scrollw(sw,w,h);
144 void show_scrollw(t_x11 *x11,t_scrollw *sw)
146 XMapWindow(x11->disp,sw->wd.self);
149 char *tab2spc(char *buf)
155 snew(buf2,8*strlen(buf)+1);
156 for(i=j=0; (buf[i]!='\0'); i++)
160 } while ((j % 8)!=0);
169 void read_lines(FILE *in,t_scrollw *sw)
173 while (fgets2(buf,1023,in)) {
175 srenew(sw->lines,sw->nlines);
176 sw->lines[sw->nlines-1]=tab2spc(buf);
180 void main(int argc, char *argv[])
185 if ((x11=GetX11(&argc,argv))==NULL) {
186 fprintf(stderr,"No X!\n");
189 sw=init_scrollw(x11,x11->root,0,0,600,200,WHITE,BLACK);
190 read_lines(stdin,sw);
191 show_scrollw(x11,sw);