I know that there isn't a whole lot of interest in reviving the
PolyML/Motif bindings idea,
but it does have the advantage (over GTK+ or QT for example) of already
existing!  (a bird in the hand is worth two in the bush)...

...so, as part of my learning experience in PolyML, I set out to get a
program working
to draw on the screen.  I saw comments in the XReference manual claiming
that there
are examples available, but searching google and the docs came up with
nothing beyond the API so I had to reinvent the wheel.  Since I am new at
SML, I would appreciate
any comments or suggestions on improving this approach to drawing.
Here is what I have so far (and it works!)
-------------------------------------------------------
open XWindows ;
open Motif;
fun square x = x * x ;
fun odd x = x mod 2 = 1 ;
fun power base exp = if exp = 0 then 1
                     else
                     if odd exp then base * power base (exp-1)
                     else square (power base (exp div 2)) ;
fun draw1 w gc x1 y1 = let val p = XPoint {x=x1,y=y1}
                 in XDrawPoint w gc p ;
                    if x1 < 200 then draw1 w gc (x1 + 1) (y1 + 1)
                    else XDrawPoint w gc p
                 end;
fun draw2 w gc x1 y1 = let val p1 = XPoint {x=x1,y=y1} ;
                           val p2 = XPoint {x=x1+100,y=y1}
                 in XDrawLine w gc p1 p2 ;
                    if x1 < 100 then draw2 w gc (x1 + 1) (power x1 2)
                    else let val p = XPoint {x=50,y=95}
                         in XDrawString w gc p "PolyML"
                         end
                 end;
fun main() =
let
  val pt = XPoint {x=0,y=0} ;
  val rt = XRectangle {bottom=0,left=0,right=200,top=200} ;
  val ar = Area {x=0,y=0,w=200,h=200} ;
  val shell = XtAppInitialise "" "xlib" "xlib" [] [] ;
  val display = XtDisplay shell ;
  val dr = RootWindow display ;
  val bp = BlackPixel display ;
  val wp = WhitePixel display ;
  val w = XCreateSimpleWindow dr pt ar 5 bp wp  ;
  val gc = XCreateGC w [] ;
  fun hdraw (Expose {sendEvent,region,window,count},s) =
             draw2 w gc 0 0
    | hdraw (DeleteRequest {window},s) =
             XDestroyWindow window ;
in
  XMapWindow w ;
  XSelectInput w [ExposureMask,ButtonPressMask] ;
  XSetHandler w hdraw () ;
  XFlush display
(*  Posix.Process.pause()  *)
end;
----------------------------------------------------
The Posix pause is needed if compiling with polyc to keep window open,
but in poly interactive it is not.
The attached image is what is produced.
-Dave Topham

Attachment: math49.sml
Description: Binary data

_______________________________________________
polyml mailing list
[email protected]
http://lists.inf.ed.ac.uk/mailman/listinfo/polyml

Reply via email to