NB. built from project: ~Source\classes\graphics\graph\graph
NB. initialize graph

coclass 'jzgraph'


COCLASSPATH copath coname''
coinsert 'jgl2'

EXTENT=: 10000
EXTENT2=: -: EXTENT

NB. graph utils

deb=: #~ (+. (1: |. (> </\)))@(' '&~:)
info=: wdinfo @ ('graph'&;)
roundint=: <.@:+&0.5
parents=: <;._2@wd@('qp'"_)
isparent=: boxopen e. parents

NB. =========================================================
NB. form:  opt rgb dat
NB.   opt = 0 integers to RGB   (default)
NB.   dat = red, green, blue values or 3-col matrix of same
NB.
NB.   opt = 1 RGB to integers
NB.   dat = one or more RGB values
NB.
NB. RGB format is a 4 byte integer: unused blue green red
NB.
NB. e.g.  RGB 255 255 0   =  RGB value for bright yellow
rgb=: 0&$: : (4 : 0)
if. x. do. |."1[ 256 256 256 #: y.
else. y. +/ .* 1 256 65536
end.
)

NB. =========================================================
roundrect=: 3 : 0
shape=. $y.
'x y w h'=. |: _4 [\ ,y.
xw=. x+w
yh=. y+h
x=. roundint x
y=. roundint y
w=. roundint xw - x
h=. roundint yh - y
shape $ , |: x, y, w,: h
)

NB. =========================================================
sposxt=: 3 : 'EXTENT2 * >: y.'
srecxt=: 3 : '(($y.) $ EXTENT2,EXTENT2,0 0) + EXTENT2 * y.'
scale01=: 3 : 'EXTENT * y.'

scalexy=: roundint @ sposxt [ psel
scalearc=: scalexy [ psel
scalerect=: roundrect @: srecxt [ psel

scalexy01=: roundint @ scale01 [ psel
scalearc01=: scalexy01 [ psel
scalerect01=: roundrect @: scale01 [ psel


NB. gdfns

NB. =========================================================
NB. state verbs:
gdbrush=: glbrush [ psel
gdclear=: pclear
gdcolor=: glbrush @: glrgb [ psel
gdfit=: pfit
gdopen=: popen
gdshow=: pshow

NB. =========================================================
gddraw=: 1 : 0
gdopen''
gdshow u. y.
:
gdopen''
gdshow x. u. y.
)

NB. =========================================================
NB.*gdpencolor v   set color for pen
NB. form: gdcolor color[;style]
gdpencolor=: 3 : 0
pselect''
'c p'=. 2 {. boxopen y.
if. 0=#p do.
  getstate''
  p=. PEN
else.
  PEN=: 2 {. p
  putstate ''
end.
glrgb c
ppen p
)

NB. =========================================================
gdpen=: 3 : 0
pselect''
PEN=: 2 {. y.
putstate''
ppen PEN
)

NB. =========================================================
gdrgb=: 3 : 0
pselect''
clr=. y.
if. 1=#clr do. clr=. |. 256 256 256 #: {.clr end.
glrgb y
)

NB. =========================================================
NB. set state variables:
NB.   ani = IFANI
gdset=: 3 : 0
pselect''
getstate''
ani=. IFANI
if. 0=#y=. tolower deb y. do. return. end.
f=. i.&' ' ({. ; }.@}.) ]
dat=. f &> ';' cutopen y.
nms=. {."1 dat
val=. 0 ". each {:"1 dat
(nms)=. val
if. IFANI ~: ani do.
  IFANI=: ani
end.
putstate''
)

NB. =========================================================
NB. drawing verbs:

f=. 1 : ' u. : ($:@] [ gdcolor@[)'
g=. 1 : ' u. : ($:@] [ gdpencolor@[)'

gdarc=: glarc @: scalexy f "1 L: 0
gdchord=: glchord @: scalexy f "1 L: 0
gdellipse=: glellipse @: scalerect f "1 L: 0
gdlines=: gllines @: scalexy g "1 L: 0
gdpie=: glpie @: scalexy f "1 L: 0
gdpixel=: glpixel @: scalexy f "1 L: 0
gdpolygon=: glpolygon @: scalexy f "1 L: 0
gdrect=: glrect @: scalerect f "1 L: 0
gdroundr=: glroundr @: scalerect f "1 L: 0

gdarc01=: glarc @: scalexy01 f "1 L: 0
gdchord01=: glchord @: scalexy01 f "1 L: 0
gdellipse01=: glellipse @: scalerect01 f "1 L: 0
gdlines01=: gllines @: scalexy01 g "1 L: 0
gdpie01=: glpie @: scalexy01 f "1 L: 0
gdpixel01=: glpixel @: scalexy01 f "1 L: 0
gdpolygon01=: glpolygon & scalexy01 f "1 L: 0
gdrect01=: glrect @: scalerect01 f "1 L: 0
gdroundr01=: glroundr @: scalerect01 f "1 L: 0


NB. state read & write
NB.
NB. state variables
NB.   IFANI    if anisotropic, default 0
NB.   PEN      size, style, default 1 0
NB.
NB. defstate
NB. getstate
NB. putstate
NB. readstate

NB. =========================================================
defstate=: 3 : 0
IFANI=: 0
PEN=: 1 0
)

defstate''

NB. =========================================================
getstate=: 3 : 0
if. 0=readstate'' do.
  defstate''
else.
  if. 0=#ccgraphstate do.
    defstate''
  else.
    j=. 3 {. 0 ". ccgraphstate
    IFANI=: 0 { j
    PEN=: 1 2 { j
  end.
end.
)

NB. =========================================================
NB. readstate
NB. defines global ccgraphstate
NB. returns 1 if ccgraphstate is in form
readstate=: 3 : 0
ccgraphstate=: ''
name=. <'ccgraphstate'
qd=. wd 'qd'
nms=. {."1 qd
if. name e. nms do.
  ccgraphstate=: > {: (nms i. name) { qd
  1
else.
  0
end.
)

NB. =========================================================
putstate=: 3 : 0
if. readstate'' do.
  wd 'set ccgraphstate *',":IFANI,PEN
end.
)


NB. window commands

NB. =========================================================
NB. pclear   clear graphics window
pclear=: 3 : 0
pselect''
glclear''
glwantresize''
glwindoworg 0 0
glwindowext ,~ EXTENT
if. #y. do.
  gdbrush y.
  gdrect _1 _1 2 2
end.
)

NB. =========================================================
NB. popen       open graphics window
NB. y. = 'parentid';'parentname'
NB.    if parentid or parent empty, default to 'graph'
NB.
NB. e.g   popen ''
NB.       popen '';'J Graphics'
popen=: 3 : 0
'pid pnm'=. 2{.boxopen y.
pid=. pid, (0=#pid) # 'graph'
pnm=. pnm, (0=#pnm) # pid

if. isparent pid do.
  wd 'psel ',pid
else.
  wd 'pc ',pid,' closeok;pn *',pnm
  wd 'xywh 0 0 100 100;cc g isigraph rightmove bottommove'
  wd 'xywh 0 0 0 0;cc ccgraphstate edit;setshow ccgraphstate 0'
  wd 'pas 0 0;pcenter;pmove 150 5 -1 -1;'
end.

INIWH=: getsize''
PARENT=: pid
CHILD=: ,'g'

pclear''
". pid,'_g_size=: psize'

wd 'ptop'
empty''
)

NB. NB. =========================================================
NB. NB.*pmapmode v set mapping mode (ani = 1 or iso = 0)
NB. pmapmode=: 3 : 0
NB. if. IFANI do.
NB.   glwindoworg 0 0
NB.   glshow''
NB.   wd 'pshow'
NB. else.
NB.   psize''
NB. end.
NB. )

NB. =========================================================
NB.*ppen v set pen
ppen=: 3 : 0
'p s'=. 2 {. y.
if. p > 0 do.
  p=. 1 >. roundint p * EXTENT % >./ WH
end.
glpen p,s
)

NB. =========================================================
NB. pselect parent, child
pselect=: 3 : 0
if. #y. do.
  'p c'=. 2 {. boxopen y.
  PARENT=: p,(0=#p)#PARENT
  CHILD=: c,(0=#c)#CHILD
end.
wd 'psel ',PARENT
glsel CHILD
)

NB. form used by gdxxx verbs:
psel=: pselect bind ''

NB. =========================================================
NB.*pshow v show graph window
pshow=: 3 : 0
pselect''
glshow''
wd 'pshow;ptop'
)

NB. =========================================================
NB.*getsize v get size of graph window
getsize=: 3 : 0
WH=: _2 {. 0 ". wd 'qchildxywhx g'
)

NB. =========================================================
NB.*psize v resize of graph window
psize=: 3 : 0
getstate''
getsize''
'w h'=. WH
if. IFANI do.
  glwindowext EXTENT
else.
  select. * w - h
  case. 0 do.
    s=. 0 0
  case. 1 do.
    s=. 0 ,~ (-:h-w) % h
  case. _1 do.
    s=. 0 , (-:w-h) % w
  end.
  glwindoworg <. s * EXTENT
  glwindowext EXTENT * (% <./) WH % INIWH
end.
)
