# Binding to the CD library
#
# TODO: 
# - images
# - various get functions
#
require 'ffi'
require 'rbconfig'

module CdLib
  extend FFI::Library

  case RbConfig::CONFIG['host_os']
#  when /mswin|mingw/
#    ffi_lib 'lib/library/iup.dll'
  when /linux|cygwin/
    ffi_lib 'libiupcd'
  else
    raise Exception, 'iup libraries not found for this platform'
  end

  attach_function :cdContextIup, [], :pointer

  # -- initialisation

  attach_function :cdCreateCanvas, [:pointer, :pointer], :pointer
  attach_function :cdKillCanvas, [:pointer], :void
  attach_function :cdCanvasActivate, [:pointer], :int
  attach_function :cdCanvasDeactivate, [:pointer], :void
  attach_function :cdUseContextPlus, [:int], :int
  attach_function :cdInitContextPlus, [], :void
  attach_function :cdCanvasGetContext, [:pointer], :pointer
  attach_function :cdContextCaps, [:pointer], :int
  attach_function :cdContextType, [:pointer], :int
  attach_function :cdContextIsPlus, [:pointer], :int
  attach_function :cdCanvasSimulate, [:pointer, :int], :int

  attach_function :wdCanvasViewport, [:pointer, :int, :int, :int, :int], :void
  attach_function :wdCanvasWindow, [:pointer, :double, :double, :double, :double], :void

  # -- general attributes
  attach_function :cdCanvasForeground, [:pointer, :long], :long
  attach_function :cdCanvasBackground, [:pointer, :long], :long
  attach_function :cdCanvasWriteMode, [:pointer, :int], :int

  # -- control

  attach_function :cdCanvasClear, [:pointer], :int

  # -- begin/end blocks
  attach_function :cdCanvasBegin, [:pointer, :int], :void
  attach_function :cdCanvasVertex, [:pointer, :int, :int], :void
  attach_function :cdCanvasEnd, [:pointer], :void
  attach_function :cdCanvasPathSet, [:pointer, :int], :void

  # --- marks
  attach_function :cdCanvasPixel, [:pointer, :int, :int], :void
  attach_function :cdCanvasMark, [:pointer, :int, :int], :void
  attach_function :cdCanvasMarkType, [:pointer, :int], :int
  attach_function :cdCanvasMarkSize, [:pointer, :int], :int

  # --- lines

  attach_function :cdCanvasLine, [:pointer, :int, :int, :int, :int], :void
  attach_function :cdCanvasRect, [:pointer, :int, :int, :int, :int], :void
  attach_function :cdCanvasArc, [:pointer, :int, :int, :int, :int, :double, :double], :void
  attach_function :cdCanvasLineStyle, [:pointer, :int], :int
#  attach_function :cdCanvasLineStyleDashes, [:pointer, :pointer, :int], :void # TODO: pointer to int array?
  attach_function :cdCanvasLineWidth, [:pointer, :int], :int
  attach_function :cdCanvasLineJoin, [:pointer, :int], :int
  attach_function :cdCanvasLineCap, [:pointer, :int], :int

  # --- filled areas

  attach_function :cdCanvasBox, [:pointer, :int, :int, :int, :int], :void
  attach_function :cdCanvasSector, [:pointer, :int, :int, :int, :int, :double, :double], :int
  attach_function :cdCanvasChord, [:pointer, :int, :int, :int, :int, :double, :double], :int
  attach_function :cdCanvasBackOpacity, [:pointer, :int], :int
  attach_function :cdCanvasFillMode, [:pointer, :int], :int
  attach_function :cdCanvasInteriorStyle, [:pointer, :int], :int
  attach_function :cdCanvasHatch, [:pointer, :int], :int
  attach_function :cdCanvasStipple, [:pointer, :int, :int, :string], :void

  # --- text

  attach_function :cdCanvasText, [:pointer, :int, :int, :string], :void
  attach_function :cdCanvasFont, [:pointer, :string, :int, :int], :void
  attach_function :cdCanvasNativeFont, [:pointer, :string], :string
  attach_function :cdCanvasTextAlignment, [:pointer, :int], :int
  attach_function :cdCanvasTextOrientation, [:pointer, :double], :double
  # TODO: Get functions

  # --- vector text

  attach_function :cdCanvasVectorText, [:pointer, :int, :int, :string], :void
  attach_function :cdCanvasVectorTextDirection, [:pointer, :int, :int, :int, :int], :void
  attach_function :cdCanvasVectorTextSize, [:pointer, :int, :int, :string], :void
  attach_function :cdCanvasVectorCharSize, [:pointer, :int], :void
  attach_function :cdCanvasVectorFontSize, [:pointer, :double, :double], :void
  attach_function :cdCanvasVectorFont, [:pointer, :string], :string
  # TODO: Get functions

  # --- client images

  # --- server images

  # -- colour functions

  attach_function :cdEncodeColor, [:int, :int, :int], :long
end
