require 'gl3'

A=: noun define
pc a closeok;
menupop "&Help";
menu help "&Help" "" "" "";
menupopz;
xywh 0 0 100 100;cc g isigraph ws_clipchildren ws_clipsiblings rightmove bottommove;
pas 0 0;
rem form end;
)

a_run=: verb define
wd A
glaRC''
glSelectBuffer 100
wd 'pshow;ptop'
)

board =: 3 3 $ 0

a_g_size =: verb define
glViewport 0 0 ,glqwh''
mm GL_PROJECTION
gluOrtho2D 0 3 0 3
mm GL_MODELVIEW
)

a_g_paint =: verb define
glClearColor 0 0 0 0
glClear GL_COLOR_BUFFER_BIT
drawsquares GL_RENDER
glaSwapBuffers''
)

mm =: verb define
glMatrixMode y.
glLoadIdentity''
)

drawsquares =: verb define
b =. y.=GL_SELECT
i=.0
while. i<3 do.
 if. b do. glLoadName i end.
 j=.0
 while. j<3 do.
  if. b do. glPushName j end.
  glColor (i%3), (j%3) , (i{j{board) , 0
  glRect i,j, 1+i,j
  if. b do. glPopName'' end.
 j=.>:j
 end.
 i=.>:i
end.
)

a_g_mbldown =: verb define
'x y' =. 2{.".sysdata
'w h' =. glqwh''

glRenderMode GL_SELECT
glInitNames''
glPushName _1
glMatrixMode GL_PROJECTION

glPushMatrix''
 glLoadIdentity''
 gluPickMatrix x , (h-y) , 5 5 , 0 0 , w , h
 gluOrtho2D 0 3 0 3
 drawsquares GL_SELECT
glPopMatrix''
glFlush''
selbuf =: glRenderMode GL_RENDER

NB. we know each hit has 5 values (count, min, max, row, col)
i=.(#selbuf)%5
selbuf =: (i,_2){.(i,5)$selbuf
wd'mb "selected cell(s) row,col" "',(,(":selbuf),.LF),'"'

)

a_help_button=: verb define
wd'mb OpenGL *Click mouse on square or at corners.'
wd 'setfocus g'
)

a_run''
