NB. Paint Demo
NB. simple paint application with mouse and char events
NB. drawing can be saved wmf/bmp to file and clipboard

require 'gl2'
tolist=: }. @ ; @: (LF&, @ , @ ": each)

NB. =========================================================
PAINT=: 0 : 0
pc paint closeok;pn "Paint Demo";
menupop "File";
menu saveaswmf "Save As WMF..." "" "" "";
menu saveasbmp "Save As BMP..." "" "" "";
menusep ;
menu exit "E&xit" "" "" "";
menupopz;
menupop "Edit";
menu clear "Clear" "" "" "";
menu copyaswmf "Copy as WMF" "" "" "";
menu copyasbmp "Copy as BMP" "" "" "";
menupopz;
xywh 9 13 46 11;cc freehand radiobutton;cn "Freehand";
xywh 58 13 41 11;cc line radiobutton group;cn "Line";
xywh 103 13 41 11;cc box radiobutton group;cn "Box";
xywh 148 13 41 11;cc ellipse radiobutton group;cn "Ellipse";
xywh 193 13 33 11;cc text radiobutton group;cn "Text";
xywh 5 3 230 24;cc groupbox groupbox;cn "Tools";
xywh 13 38 34 11;cc red checkbox;cn "Red";
xywh 48 38 34 11;cc green checkbox;cn "Green";
xywh 83 38 34 11;cc blue checkbox;cn "Blue";
xywh 4 28 119 24;cc groupbox groupbox;cn "Colors";
xywh 144 38 34 11;cc thick radiobutton;cn "Thick";
xywh 179 38 34 11;cc thin radiobutton group;cn "Thin";
xywh 132 28 103 24;cc groupbox groupbox;cn "Line width";
xywh 1 61 238 103;cc g isigraph ws_border rightmove bottommove;
pas 0 0;pcenter;ptop;
rem form end;
)

NB. =========================================================
paint_run=: 3 : 0
D=: DPOS=: 0

if. IFWINCE do.
  p=. <;._2 PAINT
  b=. -. 'menu'&-: @ (4&{.) &> p
  p=. tolist b#p
  FONT=: 'Tahoma'
  FONTSIZE=: 16
else.
  p=. PAINT
  FONT=: 'Arial'
  FONTSIZE=: 30
end.

wd p
wdfit''
GSIZE=: glqwh''
paint_clear_button''
wd 'pshow'
)

NB. =========================================================
paint_g_char=: 3 : 0
if. ".text do.
  b=. sysdata=8{a.
  TEXT=: (_2*b)}.TEXT,sysdata
  glfont FONT,' ',":FONTSIZE
  glrgb 255 * '1'=red,green,blue
  gltextcolor ''
  gltextxy DPOS
  gltext TEXT
  glmove DPOS
  glcaret 0,FONTSIZE
  glshow''
  if. b do. glpaintx'' end.
end.
)

NB. =========================================================
logpos=: 3 : 0
'x y'=. (0 1{d) * GSIZE % 2 3{d=. ".y.
y=. y -~ {:GSIZE
x,y
)


NB. =========================================================
paint_g_mbldown=: 3 : 0
wd'setfocus g'
D=: 1
DPOS=: logpos sysdata
glmove DPOS
glcapture ('1'=freehand,line,box,ellipse,text)#1 2 3 4 0
if. ".text do.
NB.   DPOS=: DPOS + 0,FONTSIZE
  TEXT=: ''
  glmove DPOS
  glcaret 0,FONTSIZE
  glshow''
end.
)

NB. =========================================================
pen=: 3 : 0
glrgb 255 * '1'=red,green,blue
glpen (2+3*".thick),PS_SOLID
)

NB. =========================================================
paint_g_mblup=: 3 : 0
glcapture 0
D=: 0
p=. logpos sysdata
q=. DPOS,p-DPOS
pen''
glbrushnull''
if. ".line do.
  glline p
elseif. ".box do.
  glrect q
elseif. ".ellipse do.
  glellipse q
end.
glshow''
)

NB. =========================================================
paint_g_mmove=: 3 : 0
if. D *. ".freehand do.
  pen''
  glline logpos sysdata
  glshow''
end.
)

NB. =========================================================
paint_close=: 3 : 0
wd'pclose'
)

paint_cancel=: paint_exit_button=: paint_close

NB. =========================================================
paint_clear_button=: 3 : 0
glclear''
glmap MM_TEXT
glwantresize''
GSIZE=: glqwh''
glwindowext GSIZE
glrect 0 0,GSIZE
gltextalign TA_UPDATECP
glshow''
)

NB. =========================================================
paint_copyaswmf_button=: 3 : 0
glfile'' NB. clipboard
glsave 1,10000 * 1,%%/glqwh'' NB. preserve drawing aspect
)

NB. =========================================================
paint_copyasbmp_button=: 3 : 0
glfile'' NB. clipboard
glsavebmp glqwh'' NB. save bitmap same size as window
)

NB. =========================================================
paint_g_size=: 3 : 0
GSIZE=: glqwh''
glwindowext GSIZE
)

NB. =========================================================
paint_saveaswmf_button=: 3 : 0
fn=. wd'mbopen "Save As WMF" "temp" "paint.wmf" "WMF(*.wmf)"'
if. #fn do.
  glfile fn
  glsave 1 0 0
  addwmfheader fn;<.4000 * 1,%%/glqwh'' NB. preserve drawing aspect
end.
)

NB. =========================================================
paint_saveasbmp_button=: 3 : 0
fn=. wd'mbopen "Save As BMP" "temp" "paint.bmp" "BMP(*.BMP)"'
if. #fn do.
  glfile fn
  glsavebmp glqwh'' NB. save bitmap same size as window
end.
)

NB. =========================================================
NB. metafiles used by Word etc. require 22 byte header
NB. outfile addwmfheader infile ; width height (%1000 of inches)
addwmfheader=: 3 : 0
({.y.)addwmfheader y.
:
a=. 16bd7 16bcd 16bc6 16b9a 0 0 0 0 0 0 , 1 0 3 2{,256 256 #: >1{y.
a=. a,232 3 0 0 0 0
a=. a, ([: ~:/&.#: ,)"1 |: 10 2$a
a=. a{a.
(a,1!:1 {.y.) 1!:2 boxopen x.
)

NB. =========================================================
paint_run''
