Improved version for builds from modified trees.
[gromacs/qmmm-gamess-us.git] / src / ngmx / scrollw.c
blob20bd01c02ab2560391d20fab2e94fb4e9a77c2e1
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.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Gyas ROwers Mature At Cryogenic Speed
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <sysstuff.h>
40 #include <Xstuff.h>
41 #include <xutil.h>
42 #include <smalloc.h>
43 #include <macros.h>
44 #include <futil.h>
45 #include <string2.h>
47 #define YSPACE 2
49 typedef struct {
50 t_windata wd; /* Window structure */
51 int nlines,top; /* Number of lines, current top line */
52 char **lines; /* The strings */
53 int wheight,wwidth; /* The size of the window in chars */
54 XFontStruct *font; /* Font */
55 unsigned long fg,bg; /* Colours */
56 } t_scrollw;
58 static void calc_scrollw(t_scrollw *sw,int w,int h)
60 sw->wd.width=w;
61 sw->wd.height=h;
62 sw->wheight=h/(YSPACE+XTextHeight(sw->font));
63 sw->wwidth=w/XTextWidth(sw->font,"W",1);
66 static bool SWCallback(t_x11 *x11,XEvent *event,Window w,void *data)
68 t_scrollw *sw;
69 int i,y,nl,barw,btop,bheight;
70 real h,frac;
72 sw=(t_scrollw *)data;
74 /* Calc some bar data */
75 barw=20;
76 h=XTextHeight(sw->font)+YSPACE;
77 frac=min(1.0,((real)sw->wheight)/((real)sw->nlines));
78 btop=((((real)sw->top)/((real)sw->nlines)))*(sw->wd.height);
79 bheight=frac*sw->wd.height;
80 bheight-=bheight/h;
82 switch(event->type) {
83 case Expose:
84 nl=min(sw->nlines,sw->top+sw->wheight);
85 y=0;
87 XClearWindow(x11->disp,w);
88 #ifdef DEBUG
89 printf("btop: %d, bheight: %d, frac: %e, h: %e\n",btop,bheight,frac,h);
90 #endif
91 /* Draw the bar */
92 XSetForeground(x11->disp,x11->gc,LIGHTGREY);
93 XFillRectangle(x11->disp,w,x11->gc,2,btop+2,barw-4,bheight-4);
94 XDrawLine(x11->disp,w,x11->gc,barw,0,barw,sw->wd.height);
96 /* Draw the text */
97 XSetForeground(x11->disp,x11->gc,sw->fg);
98 for(i=sw->top; (i<nl); i++) {
99 SpecialTextInRect(x11,sw->font,w,
100 sw->lines[i],barw+2,y,sw->wd.width-barw-4,(int)h,
101 eXLeft,eYCenter);
102 y+=h;
104 XSetForeground(x11->disp,x11->gc,x11->fg);
105 break;
106 case ConfigureNotify:
107 calc_scrollw(sw,event->xconfigure.width,event->xconfigure.height);
108 break;
109 case ButtonPress:
110 if (event->xbutton.x < barw) {
111 int y=event->xbutton.y;
113 if (sw->nlines > sw->wheight) {
114 if (y<btop)
115 sw->top=max(0,sw->top-1);
116 else if (y>btop+bheight)
117 sw->top=min(sw->nlines-sw->wheight,sw->top+1);
118 else
119 break;
120 ExposeWin(x11->disp,sw->wd.self);
123 break;
124 default:
125 break;
128 return FALSE;
131 t_scrollw *init_scrollw(t_x11 *x11,Window parent,int x,int y,int w,int h,
132 unsigned long fg,unsigned long bg)
134 t_scrollw *sw;
136 snew(sw,1);
138 InitWin(&sw->wd,x,y,w,h,1,"Scroll Window");
139 sw->fg=fg;
140 sw->bg=bg;
141 sw->font=x11->font;
142 sw->wd.self=XCreateSimpleWindow(x11->disp,parent,x,y,w,h,
143 sw->wd.bwidth,fg,bg);
144 x11->RegisterCallback(x11,sw->wd.self,parent,SWCallback,sw);
145 x11->SetInputMask(x11,sw->wd.self,ExposureMask | ButtonPressMask |
146 StructureNotifyMask);
147 calc_scrollw(sw,w,h);
149 return sw;
152 void show_scrollw(t_x11 *x11,t_scrollw *sw)
154 XMapWindow(x11->disp,sw->wd.self);
157 char *tab2spc(char *buf)
159 char *buf2;
160 char *new;
161 int i,j;
163 snew(buf2,8*strlen(buf)+1);
164 for(i=j=0; (buf[i]!='\0'); i++)
165 if (buf[i]=='\t')
166 do {
167 buf2[j++]=' ';
168 } while ((j % 8)!=0);
169 else
170 buf2[j++]=buf[i];
171 buf2[j]='\0';
172 new=strdup(buf2);
173 sfree(buf2);
174 return new;
177 void read_lines(FILE *in,t_scrollw *sw)
179 char buf[1024];
181 while (fgets2(buf,1023,in)) {
182 sw->nlines++;
183 srenew(sw->lines,sw->nlines);
184 sw->lines[sw->nlines-1]=tab2spc(buf);
189 main(int argc, char *argv[])
191 t_x11 *x11;
192 t_scrollw *sw;
194 if ((x11=GetX11(&argc,argv))==NULL) {
195 fprintf(stderr,"No X!\n");
196 exit(1);
198 sw=init_scrollw(x11,x11->root,0,0,600,200,WHITE,BLACK);
199 read_lines(stdin,sw);
200 show_scrollw(x11,sw);
201 x11->MainLoop(x11);
203 x11->CleanUp(x11);