

define  MAX_WCSLINES	4

define  SZ_WPIX         6
define  WP_CPTR         Memi[$1  ]              # cache pointer
define  WP_PTABSZ       Memi[$1+1]              # pixel table size
define  WP_SYSTEMS      Memi[$1+2]              # WCS readout systems
define  WP_FORMATS      Memi[$1+3]              # WCS readout formats

define  SYSTEMS    Memi[WP_SYSTEMS($1)+$2-1]	# WCS systems per line
define  FORMATS    Memi[WP_FORMATS($1)+$2-1]	# WCS formats per line
define  OBJCACHE   Memi[WP_CPTR($1)+$2]		# object cache

# Element of an object cache.
define  SZ_CNODE        135                     # size of a cache node
define  SZ_OBJREF       128                     # size of a object reference

define  C_OBJID         Memi[$1]                # object id
define  C_REGID         Memi[$1+1]              # region id
define  C_CLASS         Memi[$1+2]              # object class
define  C_DATA          Memi[$1+3]              # object data ptr
define  C_REF           Memc[P2C($1+4)]         # object reference file

# Object class definitions.
define	IMAGE_CLASS	1			# generic image class
define	MEF_CLASS	2			# Mosaic MEF image class
define	MULTISPEC_CLASS	3			# multispec data class

# Class methods.
define	LEN_CL		6			# length of class table
define	MAX_CL		6			# max supported classes
define	SZ_CLNAME	16			# size of a class name

define	CL_INIT		cl_table[1,$1]		# class initializer
define	CL_CACHE	cl_table[2,$1]		# cache the object
define	CL_UNCACHE	cl_table[3,$1]		# uncache the object
define	CL_WCSTRAN	cl_table[4,$1]		# WCS tranformations
define	CL_WCSLIST	cl_table[5,$1]		# list available WCS
define	CL_GETDATA	cl_table[6,$1]		# get object data
define	CL_NAME		cl_names[1,$1]		# class name

# Class common.  
int     cl_nclasses                          	# number of defined functions
int     cl_table[LEN_CL,MAX_CL]         	# class table
char    cl_names[SZ_CLNAME,MAX_CL]      	# class names
common  /class_com/ cl_nclasses, cl_table, cl_names


# Image class data.
define  O_IM            Memi[$1+2]              # image pointer
define  O_MW            Memi[$1+3]              # image wcs pointer
define  O_CO            Memi[$1+3]              # skywcs transform pointer
define  O_CT            Memi[$1+4]              # mwcs transform pointer
define  O_ROT           Memr[$1+5]              # rotation angle
define  O_SCALE         Memr[$1+6]              # plate scale



--------------------------------------------------------------------------------
ISM Methods:
--------------------------------------------------------------------------------

    initialize
    cache <objid> <ref>
    uncache <objid>
    wcstran <objid> <x> <y> [[<region-name> <x> <y>] ["NDC" <x> <y>]]
    wcslist <objid>
    getheader <objid> <template-list>


procedure initialize
begin
	for (each object in the cache)
	    uncache object
	send startup req to GUI
end

procedure cache
begin
end

procedure uncache
begin
end

procedure wcstran
begin
end

procedure wcslist
begin
end

procedure getheader
begin
end


--------------------------------------------------------------------------------
GUI Callbacks
--------------------------------------------------------------------------------

proc ism_msg { param old new } {

    set target [lindex $new 0]		;# name of ism module

    switch [lindex $new 0] {
    source   { source [lindex $new 1] }		;# source Tcl code
    alert    { Wexec client [lindex $new 1] }	;# alert from ism client

    deliver  { set ism [lindex $new 0]		;# determine ISM name
	       set argv [lrange $new 1 end]	;# get args
	       set argc [llength $argv]
	       ${ism}_msg $argc $argv		;# call module
	     }
    }
} ; send ism_msg addCallback ism_msg


proc wpix_msg { argc argv } {

    switch [lindex $argv 0] {
    startup  { wpix_startup	}
    shutdown { wpix_shutdown	}
    cache    { .... save image name to GUI cache list
	     }
    uncache  { .... remove image name from GUI cache list
	     }
    wcstran  { .... parse argv for WCS field and update display
	     }
    pixtab   {
	     }
    wcslist  {
	     }
    wcstype  { set type [lindex $argv 1]		;# Set WCS for a line
     	       set line [lindex $argv 2]
     	       send sysWcs$line set label $type
	       if {$type == "None"} { 
		  send wpWcs$line set on False
	       } else {
		  send wpWcs$line set on True
	       }
	       setCoordPanelHeight
	     }
    wcsfmt   { set fmt  [lindex $argv 1]		;# Set fmt for a line
     	       set line [lindex $argv 2]
     	       send fmtWcs$line set label $fmt
	     }
    header   { set type [lindex $argv 1]		;# write header text
     	       set text [lindex $argv 2]
	       switch $type {
	       imghdr  {send hdrText   append $text}
	       wcshdr  {send hdrKGText append $text}
	       wcsinfo {send hdrIGText append $text}
	       }
	     }
    } ;# end switch
}

proc wpix_startup args {
    global ismEnabled frameCache 

    set ismEnabled 1				;# initialize buttons
    send ismToggle set on True
    send imageHeader setSensitive True
    setCoordPanelSensitivity

    resizeCoordsBox $up_todo			;# resize wcsbox marker
    updateCoordsBox

    foreach c [array names frameCache] { 	;# initialize local frame cache
        if {$c != "0"} { unset frameCache($c) }
    }

    catch { 					;# update ISM with GUI settings
        send wpix set psize $psize
        set wcsfmt [string tolower [send wcsFmtMenu get label]]
        send wpix set wcsfmt $wcsfmt
        if {[send wcsSysAltWCS get on]} {
            setAltSystem
        }
    }
}

proc wpix_shutdown args {
    global ismEnabled

    set ismEnabled 0
    send ismToggle set on False
    send imageHeader setSensitive False
    setCoordPanelSensitivity
    wcsFmtIValue ""
    wcsFmtImWCS  "" "" ""
    wcsFmtAltWCS "" "" ""
    resizeCoordsBox 0
}


