module IUP_CD

export
	# From libcd.jl
	cdCanvas,
	cdCreateCanvas,
	cdKillCanvas,
	cdCanvasGetContext,
	cdCanvasActivate,
	cdCanvasDeactivate,
	cdUseContextPlus,
	cdInitContextPlus,
	cdFinishContextPlus,
	cdContextRegisterCallback,
	cdContextCaps,
	cdContextIsPlus,
	cdContextType,
	cdContext,
	cdContextNativeWindow,
	cdCanvasSimulate,
	cdCanvasFlush,
	cdCanvasClear,
	cdCanvasSaveState,
	cdCanvasRestoreState,
	cdReleaseState,
	cdCanvasSetAttribute,
	cdCanvasGetAttribute,
	cdCanvasPlay,
	cdCanvasGetSize,
	cdCanvasUpdateYAxis,
	cdfCanvasUpdateYAxis,
	cdCanvasInvertYAxis,
	cdfCanvasInvertYAxis,
	cdCanvasMM2Pixel,
	cdCanvasPixel2MM,
	cdfCanvasMM2Pixel,
	cdfCanvasPixel2MM,
	cdCanvasOrigin,
	cdfCanvasOrigin,
	cdCanvasGetOrigin,
	cdfCanvasGetOrigin,
	cdCanvasTransform,
	cdCanvasGetTransform,
	cdCanvasTransformMultiply,
	cdCanvasTransformRotate,
	cdCanvasTransformScale,
	cdCanvasTransformTranslate,
	cdCanvasTransformPoint,
	cdfCanvasTransformPoint,
	cdCanvasClip,
	cdCanvasClipArea,
	cdCanvasGetClipArea,
	cdfCanvasClipArea,
	cdfCanvasGetClipArea,
	cdCanvasIsPointInRegion,
	cdCanvasOffsetRegion,
	cdCanvasGetRegionBox,
	cdCanvasRegionCombineMode,
	cdCanvasPixel,
	cdCanvasMark,
	cdCanvasBegin,
	cdCanvasPathSet,
	cdCanvasEnd,
	cdCanvasLine,
	cdCanvasVertex,
	cdCanvasRect,
	cdCanvasBox,
	cdCanvasArc,
	cdCanvasSector,
	cdCanvasChord,
	cdCanvasText,
	cdfCanvasLine,
	cdfCanvasVertex,
	cdfCanvasRect,
	cdfCanvasBox,
	cdfCanvasArc,
	cdfCanvasSector,
	cdfCanvasChord,
	cdfCanvasText,
	cdCanvasSetBackground,
	cdCanvasSetForeground,
	cdCanvasBackground,
	cdCanvasForeground,
	cdCanvasBackOpacity,
	cdCanvasWriteMode,
	cdCanvasLineStyle,
	cdCanvasLineStyleDashes,
	cdCanvasLineWidth,
	cdCanvasLineJoin,
	cdCanvasLineCap,
	cdCanvasInteriorStyle,
	cdCanvasHatch,
	cdCanvasStipple,
	cdCanvasGetStipple,
	cdCanvasPattern,
	cdCanvasGetPattern,
	cdCanvasFillMode,
	cdCanvasFont,
	cdCanvasGetFont,
	cdCanvasNativeFont,
	cdCanvasTextAlignment,
	cdCanvasTextOrientation,
	cdCanvasMarkType,
	cdCanvasMarkSize,
	cdCanvasVectorText,
	cdCanvasMultiLineVectorText,
	cdCanvasVectorFont,
	cdCanvasVectorTextDirection,
	cdCanvasVectorTextTransform,
	cdCanvasVectorTextSize,
	cdCanvasVectorCharSize,
	cdCanvasVectorFontSize,
	cdCanvasGetVectorFontSize,
	cdCanvasGetVectorTextSize,
	cdCanvasGetVectorTextBounds,
	cdCanvasGetVectorTextBox,
	cdfCanvasVectorTextDirection,
	cdfCanvasVectorTextSize,
	cdfCanvasGetVectorTextSize,
	cdfCanvasVectorCharSize,
	cdfCanvasVectorText,
	cdfCanvasMultiLineVectorText,
	cdfCanvasGetVectorTextBounds,
	cdfCanvasGetVectorTextBox,
	cdCanvasGetFontDim,
	cdCanvasGetTextSize,
	cdCanvasGetTextBox,
	cdCanvasGetTextBounds,
	cdCanvasGetColorPlanes,
	cdCanvasPalette,
	cdCanvasGetImageRGB,
	cdCanvasPutImageRectRGB,
	cdCanvasPutImageRectRGBA,
	cdCanvasPutImageRectMap,
	cdCanvasCreateImage,
	cdKillImage,
	cdCanvasGetImage,
	cdCanvasPutImageRect,
	cdCanvasScrollArea,
	cdCreateBitmap,
	cdKillBitmap,
	cdBitmapGetData,
	cdBitmapSetRect,
	cdCanvasPutBitmap,
	cdCanvasGetBitmap,
	cdBitmapRGB2Map,
	cdEncodeColor,
	cdDecodeColor,
	cdDecodeAlpha,
	cdEncodeAlpha,
	cdRGB2Map,
	cdActivate,
	cdActiveCanvas,
	cdSimulate,
	cdFlush,
	cdClear,
	cdSaveState,
	cdRestoreState,
	cdSetAttribute,
	cdGetAttribute,
	cdGetContext,
	cdRegisterCallback,
	cdPlay,
	cdGetCanvasSize,
	cdUpdateYAxis,
	cdMM2Pixel,
	cdPixel2MM,
	cdOrigin,
	cdClip,
	cdGetClipPoly,
	cdClipArea,
	cdGetClipArea,
	cdPointInRegion,
	cdOffsetRegion,
	cdRegionBox,
	cdRegionCombineMode,
	cdPixel,
	cdMark,
	cdLine,
	cdBegin,
	cdVertex,
	cdEnd,
	cdRect,
	cdBox,
	cdArc,
	cdSector,
	cdChord,
	cdText,
	cdBackground,
	cdForeground,
	cdBackOpacity,
	cdWriteMode,
	cdLineStyle,
	cdLineStyleDashes,
	cdLineWidth,
	cdLineJoin,
	cdLineCap,
	cdInteriorStyle,
	cdHatch,
	cdStipple,
	cdGetStipple,
	cdPattern,
	cdGetPattern,
	cdFillMode,
	cdFont,
	cdGetFont,
	cdNativeFont,
	cdTextAlignment,
	cdTextOrientation,
	cdMarkType,
	cdMarkSize,
	cdMultiLineVectorText,
	cdGetVectorTextSize,
	cdGetVectorTextBounds,
	cdFontDim,
	cdImage,
	cdTextSize,
	cdTextBox,
	cdTextBounds,
	cdGetColorPlanes,
	cdPalette,
	cdGetImageRGB,
	cdPutImageRectRGB,
	cdPutImageRectRGBA,
	cdPutImageRectMap,
	cdCreateImage,
	cdGetImage,
	cdPutImageRect,
	cdScrollArea,
	cdPutBitmap,
	cdGetBitmap,
	cdVersion,
	cdVersionDate,
	cdVersionNumber,	
	cdVectorText,
#	cdVectorFont,
	cdVectorTextDirection,
	cdVectorTextTransform,
	cdVectorTextSize,
	cdVectorCharSize,
	#
	# From wd.jl
	#
	wdCanvasWindow,
	wdCanvasGetWindow,
	wdCanvasViewport,
	wdCanvasGetViewport,
	wdCanvasWorld2Canvas,
	wdCanvasWorld2CanvasSize,
	wdCanvasCanvas2World,
	wdCanvasSetTransform,
	wdCanvasGetTransform,
	wdCanvasTranslate,
	wdCanvasScale,
	wdCanvasClipArea,
	wdCanvasGetClipArea,
	wdCanvasIsPointInRegion,
	wdCanvasOffsetRegion,
	wdCanvasGetRegionBox,
	wdCanvasHardcopy,
	wdCanvasPixel,
	wdCanvasMark,
	wdCanvasLine,
	wdCanvasVertex,
	wdCanvasRect,
	wdCanvasBox,
	wdCanvasArc,
	wdCanvasSector,
	wdCanvasChord,
	wdCanvasText,
	wdCanvasPutImageRect,
	wdCanvasPutImageRectRGB,
	wdCanvasPutImageRectRGBA,
	wdCanvasPutImageRectMap,
	wdCanvasPutBitmap,
	wdCanvasLineWidth,
	wdCanvasFont,
	wdCanvasGetFont,
	wdCanvasMarkSize,
	wdCanvasGetFontDim,
	wdCanvasGetTextSize,
	wdCanvasGetTextBox,
	wdCanvasGetTextBounds,
	wdCanvasStipple,
	wdCanvasPattern,
	wdCanvasVectorTextDirection,
	wdCanvasVectorTextSize,
	wdCanvasGetVectorTextSize,
	wdCanvasVectorCharSize,
	wdCanvasVectorText,
	wdCanvasMultiLineVectorText,
	wdCanvasGetVectorTextBounds,
	wdCanvasGetVectorTextBox,
	wdWindow,
	wdGetWindow,
	wdViewport,
	wdGetViewport,
	wdWorld2Canvas,
	wdWorld2CanvasSize,
	wdCanvas2World,
	wdClipArea,
	wdGetClipArea,
	wdGetClipPoly,
	wdPointInRegion,
	wdOffsetRegion,
	wdRegionBox,
	wdHardcopy,
	wdPixel,
	wdMark,
	wdLine,
	wdVertex,
	wdRect,
	wdBox,
	wdArc,
	wdSector,
	wdChord,
	wdText,
	wdPutImageRect,
	wdPutImageRectRGB,
	wdPutImageRectRGBA,
	wdPutImageRectMap,
	wdPutBitmap,
	wdLineWidth,
	wdFont,
	wdGetFont,
	wdMarkSize,
	wdFontDim,
	wdTextSize,
	wdTextBox,
	wdTextBounds,
	wdStipple,
	wdPattern,
	wdVectorTextDirection,
	wdVectorTextSize,
	wdGetVectorTextSize,
	wdVectorCharSize,
	wdVectorText,
	wdMultiLineVectorText,
	wdGetVectorTextBound,
	#
	# From cdiup.jl
	#
	cdContextIup,
	# From cdxdbuf
	cdContextDBuffer,
	#
	# From handlegraphics
	#
	guidata, setappdata, getappdata, WindowButtonDownFcn, WindowButtonMotionFcn, KeyPressFcn,
	set_current_point, get_current_point, set_current_character, get_current_character, isnada, Handles,
	gc_ref, gc_unref,
	# local
	imcdCanvasPutImage

include("libcd_h.jl")
include("libcd.jl")
include("cdiup.jl")
include("wd.jl")
include("im_image_h.jl")
include("handlegraphics.jl")

# ----------------------------------------------------------------------------------------
# Utility function to draw the image in a CD library canvas (from im_image.h)
# Works only for data_type IM_BYTE, and color spaces: IM_RGB, IM_MAP, IMGRAY and IM_BINARY.
function imcdCanvasPutImage(_canvas, _image, _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax)
	if (isa(_image, Ptr{imImage}))
		_image = unsafe_load(_image)
	end
	const IM_RGB = 0		# Redefining here to not have to import it from libim.h (Ghrrr)
	if (_image.color_space == IM_RGB)
		data = [convert(Ptr{UInt8}, unsafe_load(_image.data,1)),
			convert(Ptr{UInt8}, unsafe_load(_image.data,2)),
			convert(Ptr{UInt8}, unsafe_load(_image.data,3))]

		if (_image.has_alpha != 0)
			alfa = convert(Ptr{UInt8}, unsafe_load(_image.data,4))
			cdCanvasPutImageRectRGBA(_canvas, _image.width, _image.height, data[1], data[2],
			                         data[3], alfa, _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax)
		else
			cdCanvasPutImageRectRGB(_canvas, _image.width, _image.height, data[1], data[2],
			                        data[3], _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax)
		end
	else
		data = [convert(Ptr{UInt8}, unsafe_load(_image.data,1))]
		cdCanvasPutImageRectMap(_canvas, _image.width, _image.height, data[1], _image.palette,
		                        _x, _y, _w, _h, _xmin, _xmax, _ymin, _ymax)
	end
end

end  # module
