#lang racket (require ffi/unsafe "base.rkt") (define libiup-mglplot (case (system-type 'os) [(windows) (ffi-lib "iup_mglplot")] [else (ffi-lib "libiup_mglplot")])) ;; MglPlot controls (define mglplot (make-constructor-procedure (get-ffi-obj "IupMglPlot" libiup-mglplot (_fun -> [handle : _ihandle])))) (define mgllabel (make-constructor-procedure (get-ffi-obj "IupMglLabel" libiup-mglplot (_fun ([title #f]) :: [title : _string/utf-8] -> [handle : _ihandle])))) ;; Plotting functions (define call-with-mglplot (letrec ([mglplot-begin (get-ffi-obj "IupMglPlotBegin" libiup-mglplot (_fun [handle : _ihandle] [dimension : _int] -> _void))] [mglplot-end (get-ffi-obj "IupMglPlotEnd" libiup-mglplot (_fun [handle : _ihandle] -> _void))]) (λ (handle proc #:dimension [dimension 2]) (dynamic-wind (λ () (mglplot-begin handle dimension)) (λ () (proc handle)) (λ () (mglplot-end handle)))))) (define mglplot-add! (letrec ([append/1d (get-ffi-obj "IupMglPlotAdd1D" libiup-mglplot (_fun [handle : _ihandle] [x : _string/utf-8] [y : _double] -> _void))] [append/2d (get-ffi-obj "IupMglPlotAdd2D" libiup-mglplot (_fun [handle : _ihandle] [x : _double] [y : _double] -> _void))] [append/3d (get-ffi-obj "IupMglPlotAdd3D" libiup-mglplot (_fun [handle : _ihandle] [x : _double] [y : _double] [z : _double] -> _void))] [insert/1d (get-ffi-obj "IupMglPlotInsert1D" libiup-mglplot (_fun [handle : _ihandle] [index : _int] [sample-index : _int] [x : (_ptr i _string/utf-8)] [y : (_ptr i _double)] [count : _int = 1] -> _void))] [insert/2d (get-ffi-obj "IupMglPlotInsert2D" libiup-mglplot (_fun [handle : _ihandle] [index : _int] [sample-index : _int] [x : (_ptr i _double)] [y : (_ptr i _double)] [count : _int = 1] -> _void))] [insert/3d (get-ffi-obj "IupMglPlotInsert3D" libiup-mglplot (_fun [handle : _ihandle] [index : _int] [sample-index : _int] [x : (_ptr i _double)] [y : (_ptr i _double)] [z : (_ptr i _double)] [count : _int = 1] -> _void))] [current-index (λ (handle) (string->number (attribute handle 'current)))]) (λ (handle x y [z #f] [sample-index #f] [index #f]) (cond [z (if sample-index (insert/3d handle (or index (current-index handle)) sample-index (exact->inexact x) (exact->inexact y) (exact->inexact z)) (append/3d handle (exact->inexact x) (exact->inexact y) (exact->inexact z)))] [(string? x) (if sample-index (insert/1d handle (or index (current-index handle)) sample-index x (exact->inexact y)) (append/1d handle x (exact->inexact y)))] [else (if sample-index (insert/2d handle (or index (current-index handle)) sample-index (exact->inexact x) (exact->inexact y)) (append/2d handle (exact->inexact x) (exact->inexact y)))])))) (define mglplot-x/y/z->pixel-x/y (get-ffi-obj "IupMglPlotTransform" libiup-mglplot (_fun (handle x y [z 0.0]) :: [handle : _ihandle] [mglplot-x : _double = (exact->inexact x)] [mglplot-y : _double = (exact->inexact y)] [mglplot-z : _double = (exact->inexact z)] [pixel-x : (_ptr o _int)] [pixel-y : (_ptr o _int)] -> _void -> (values pixel-x pixel-y)))) (define mglplot-paint-to (get-ffi-obj "IupMglPlotPaintTo" libiup-mglplot (_fun (handle format file [width 0] [height 0] [dpi 0.0]) :: [handle : _ihandle] [format : _string/utf-8] [width : _int] [height : _int] [dpi : _double] [file : _string] -> _void))) ;; Library setup (letrec ([open (get-ffi-obj "IupMglPlotOpen" libiup-mglplot (_fun -> _void))]) (open)) (provide mglplot mgllabel call-with-mglplot mglplot-add! mglplot-x/y/z->pixel-x/y mglplot-paint-to)