#lang racket (require ffi/unsafe "base.rkt") (define libiup-plot (case (system-type 'os) [(windows) (ffi-lib "iup_plot")] [else (ffi-lib "libiup_plot")])) ;; plot control (define plot (make-constructor-procedure (get-ffi-obj "IupPlot" libiup-plot (_fun -> [handle : _ihandle])))) ;; Plotting functions (define call-with-plot (letrec ([plot-begin (get-ffi-obj "IupPlotBegin" libiup-plot (_fun [handle : _ihandle] [xdata-string? : _bool] -> _void))] [plot-end (get-ffi-obj "IupPlotEnd" libiup-plot (_fun [handle : _ihandle] -> _void))]) (λ (handle proc #:x-string? [x-string? #f]) (dynamic-wind (λ () (plot-begin handle x-string?)) (λ () (proc handle)) (λ () (plot-end handle)))))) (define plot-add! (letrec ([append/real (get-ffi-obj "IupPlotAdd" libiup-plot (_fun [handle : _ihandle] [x : _double] [y : _double] -> _void))] [append/string (get-ffi-obj "IupPlotAddStr" libiup-plot (_fun [handle : _ihandle] [x : _string/utf-8] [y : _double] -> _void))] [insert/real (get-ffi-obj "IupPlotInsert" libiup-plot (_fun [handle : _ihandle] [index : _int] [sample-index : _int] [x : _double] [y : _double] -> _void))] [insert/string (get-ffi-obj "IupPlotInsertStr" libiup-plot (_fun [handle : _ihandle] [index : _int] [sample-index : _int] [x : _string/utf-8] [y : _double] -> _void))] [current-index (λ (handle) (string->number (attribute handle 'current)))]) (λ (handle x y [sample-index #f] [index #f]) (if (string? x) (if sample-index (insert/string handle (or index (current-index handle)) sample-index x (exact->inexact y)) (append/string handle x (exact->inexact y))) (if sample-index (insert/real handle (or index (current-index handle)) sample-index (exact->inexact x) (exact->inexact y)) (append/real handle (exact->inexact x) (exact->inexact y))))))) (define plot-x/y->pixel-x/y (get-ffi-obj "IupPlotTransform" libiup-plot (_fun (handle x y) :: [handle : _ihandle] [plot-x : _double = (exact->inexact x)] [plot-y : _double = (exact->inexact y)] [pixel-x : (_ptr o _double)] [pixel-y : (_ptr o _double)] -> _void -> (values pixel-x pixel-y)))) (define plot-paint-to (get-ffi-obj "IupPlotPaintTo" libiup-plot (_fun [handle : _ihandle] [canvas : _pointer] -> _void))) ;; Library setup (letrec ([open (get-ffi-obj "IupPlotOpen" libiup-plot (_fun -> _void))]) (open)) (provide plot call-with-plot plot-add! plot-x/y->pixel-x/y plot-paint-to)