home *** CD-ROM | disk | FTP | other *** search
- # Iconbar handler for Graph
- # Created by Py_New on Mon,19 Aug
-
- import tbevent
- import window
- import saveas
- import drawf
- import sys
- import swi
- import string
- from math import *
-
- BadFile= 'Bad File'
-
- WINDOW_WIDTH=1280
- WINDOW_HEIGHT=1024
- OS_TO_DRAW=256
- PATH_MOVE=2
- PATH_DRAW=8
- BLACK=0x0
- WHITE=0xffffff00
- TRANSPARENT=-1
- EPSILON=0.00001
-
- trans=swi.block(6,[0x10000,0,0,0x10000,0,0])
-
- colours={'black':0,
- 'red':0xff00,
- 'blue':0xff000000,
- 'green':0xff0000,
- 'yellow':0xffff00,
- 'cyan':0xffff0000,
- 'white':0xffffff00}
-
- minorgrid=(TRANSPARENT,0xffeedd00,512,1)
- middlegrid=(TRANSPARENT,0xffddaa00,512,1)
- majorgrid=(TRANSPARENT,0xffcc8800,512,1)
-
-
- class Orphans:
- actions={}
- def __init__(self):
- tbevent.tbclasses[0]=self
- def E_Q_Quit(self):
- sys.exit()
-
- class Messenger:
- def M_DataLoad(self):
- try:
- a=Parse(tbevent.wblock.nullstring(44))
- except BadFile:
- return
- Display(a)
- def Wimp_MQuit(self):
- sys.exit()
-
- class Parse:
- def __init__(self,fname):
- act={'xrange':(2,self.xrange),
- 'yrange':(2,self.yrange),
- 'plot':(-1,self.plot),
- 'steps':(1,self.steps),
- 'title':(-1,self.title),
- 'axes':(2,self.axes),
- 'grid':(2,self.grid),
- 'colour':(1,self.colour)}
- self.fname=fname
- self.n=30
- self.x0=-1.0
- self.x1= 1.0
- self.setx()
- self.y0=-1.0
- self.y1= 1.0
- self.sety()
- self.setstyle(BLACK)
- self.title=''
- f=open(fname,'r')
- s=f.readlines()
- f.close()
- self.df=drawf.new()
- self.df.fonttable({1:"Trinity.Medium"})
- for line in s:
- words=string.split(line)
- if words:
- command=words[0]
- if command[0]!=';':
- if act.has_key(command):
- a=act[command]
- if a[0]<0 or len(words)==a[0]+1:
- a[1](words[1:])
- else:
- tbevent.errorbox(
- "Command "+command+" needs "+`a[0]`+" arguments")
- else:
- tbevent.errorbox("Unknown command "+command)
- def xrange(self,w):
- self.x0=string.atof(w[0])
- self.x1=string.atof(w[1])
- self.setx()
- def setx(self):
- if self.x1<self.x0+EPSILON:
- tbevent.errorbox("Bad x range")
- raise BadFile
- self.xs=1280*256/(self.x1-self.x0)
- step=(self.x1-self.x0)/self.n
- self.xl=[]
- for m in range(self.n+1):
- self.xl.append(self.x0+m*step)
- def yrange(self,w):
- self.y0=string.atof(w[0])
- self.y1=string.atof(w[1])
- self.sety()
- def sety(self):
- if self.y1<self.y0+EPSILON:
- tbevent.errorbox("Bad y range")
- raise BadFile
- self.ys=WINDOW_HEIGHT*OS_TO_DRAW/(self.y1-self.y0)
- def plot(self,w):
- fn=string.joinfields(w,' ')
- yl=[]
- for m in range(self.n+1):
- try:
- x=self.xl[m]
- yl.append(eval(fn))
- except:
- tbevent.errorbox("Can't find "+fn+"at x="+`x`)
- raise BadFile
- pt=[(int((self.xl[0]-self.x0)*self.xs),int((yl[0]-self.y0)*self.ys))]
- for m in range(1,self.n+1):
- pt.append(PATH_DRAW)
- pt.append((int((self.xl[m]-self.x0)*self.xs),
- int((yl[m]-self.y0)*self.ys)))
- self.df.path(pt,self.style)
- def steps(self,w):
- self.n=string.atoi(w[0])
- if self.n<10:
- tbevent.errorbox("Using 10 steps")
- self.n=10
- if self.n>1000:
- tbevent.errorbox("Using 1000 steps")
- self.n=1000
- self.setx()
- def title(self,w):
- self.title=string.joinfields(w,' ')
- self.df.text((OS_TO_DRAW*80,OS_TO_DRAW*960),self.title,self.textstyle)
- def axes(self,w):
- xaxis=string.atof(w[0])
- yaxis=string.atof(w[1])
- xa=int((xaxis-self.x0)*self.xs)
- ya=int((yaxis-self.y0)*self.ys)
- self.df.path([(xa,0),PATH_DRAW,(xa,WINDOW_HEIGHT*OS_TO_DRAW),
- PATH_MOVE,(0,ya),PATH_DRAW,(WINDOW_WIDTH*OS_TO_DRAW,ya)],
- self.style)
- def colour(self,w):
- if colours.has_key(w[0]):
- self.setstyle(colours[w[0]])
- else:
- self.setstyle(string.atoi(w[0],0))
- def setstyle(self,c):
- self.style=(TRANSPARENT,c,512,1)
- self.textstyle=(c,WHITE,1,640*20,640*20)
- def grid(self,w):
- xint=string.atof(w[0])/10
- yint=string.atof(w[1])/10
- g=[]
- gm=[]
- gmm=[]
- n0=ceil(self.x0/xint)
- n1=floor(self.x1/xint)
- for n in range(n0,n1+1):
- x=int((n*xint-self.x0)*self.xs)
- z=[(x,0),PATH_DRAW,(x,WINDOW_HEIGHT*OS_TO_DRAW),PATH_MOVE]
- if n % 10 == 0:
- g=g+z
- elif n % 5 ==0:
- gm=gm+z
- else:
- gmm=gmm+z
- n0=ceil(self.y0/yint)
- n1=floor(self.y1/yint)
- for n in range(n0,n1+1):
- y=int((n*yint-self.y0)*self.ys)
- z=[(0,y),PATH_DRAW,(WINDOW_WIDTH*OS_TO_DRAW,y),PATH_MOVE]
- if n % 10 == 0:
- g=g+z
- elif n % 5 ==0:
- gm=gm+z
- else:
- gmm=gmm+z
- if gmm:
- self.df.path(gmm[:-1],minorgrid)
- if gm:
- self.df.path(gm[:-1],middlegrid)
- if g:
- self.df.path(g[:-1],majorgrid)
-
- class Display(window.Window):
- actions={}
- def __init__(self,parsed):
- window.Window.__init__(self,"Display")
- tbevent.tbclasses[self.id]=self
- self.handle=self.getwimphandle()
- tbevent.winclasses[self.handle]=self
- self.p=parsed
- self.settitle("Graph: "+self.p.fname)
- self.show()
- def W_Redraw(self):
- global trans
- more=swi.swi(0x400c8,".b;i",tbevent.wblock)
- trans[4]=256*(tbevent.wblock[1]-tbevent.wblock[5])
- trans[5]=256*(tbevent.wblock[4]-tbevent.wblock[6])
- while more:
- self.p.df.render(0,trans.start,tbevent.wblock.start+28,0)
- more=swi.swi(0x400ca,".b;i",tbevent.wblock)
- def E_S_AboutToBeShown(self):
- savewind=saveas.SaveAs(tbevent.idblock[4])
- savewind.setdataaddress(self.p.df.start,self.p.df.size)
-
- tbevent.initialise("<Graph$Dir>")
-
- oi=Orphans()
- tbevent.msghandler=Messenger()
-
- while 1:
- tbevent.poll()