c************
c  Description:	This module contains the non-XWindows graphics support routines
c		for ANNOTATE FACILITIES related graphics operations
c
c  Author:	JGS   4//90
c		Split from glibf2.for 7/19/95
c
c  $Revision: 1.3 $
c  $Modtime:   03/15/96 11:04:00  $
c  
c  #_SUN OPTIMIZATION -O1 
c
c 11-03 EHG 500002 Modified Code to have additional integer casting for Intel Compiler.
c
c************

c
c mode:  -2=Erase
c
	subroutine MXU$ANNOTATE (Mx, Xf, MQD, mode)

	implicit none

	include 'types.inc'
	include 'midas.inc'
	include 'xprim.inc'
	include 'xfacilities.inc'
	include 'qmessdef.inc'
c Arguments
	record /XFACILITIES/ Xf
	record /MQ_DATA/ MQD
	integer*4 mode
c Locals
	integer*4  n, nw, nwl, i, j, isel, idef, x, y
	integer*4  n1, n2, nk, xoff, yoff, drag
	logical*4  exposure, draw, set, tied2boxes
	character*49 titlebar
	character*128 menu
	character*80 filename

	record /M_PTR/ ptr(1)
	record /XFANNOTATION/ Td, Tn, tt
	pointer (ptr_ptr, ptr)
	pointer (tt_ptr, tt)
	pointer (td_ptr, Td)
c Code
	if (mode.eq.L_Draw .or. mode.eq.-2) then	! draw/erase text
	  if (Xf.aview .and. Xf.nannt.gt.0) then
	    draw = .true.
	    if (mode.eq.L_Draw) then
	      n1 = 1
	      n2 = Xf.nannt
	      nk = 1
	    else
	      n1 = Xf.nannt
	      n2 = 1
	      nk = -1
	    endif
	    ptr_ptr = Xf.annt.p

	    do n = n1, n2, nk
	      if (Xf.clip) then
		tt_ptr = ptr(n).p
		draw = Xf.x2 .gt. tt.xc-5 .and. Xf.x1 .lt. tt.xc+tt.w+5 .and.
     &		       Xf.y2 .gt. tt.yc-5 .and. Xf.y1 .lt. tt.yc+tt.h+5
	      endif
	      if (draw) then
		if (mode.eq.L_Draw) then
		  call MX$TDRAW (%val(ptr(n).p))
		else
		  call MX$TSELECT (%val(ptr(n).p), L_RemoveText)
		endif
	      endif
	    enddo
	  endif

	elseif (mode.eq.L_CleanExit) then		! cleaned exit
	  ptr_ptr = Xf.annt.p
	  do n = 1, Xf.nannt
	    call M$STMFREE(L_xfannotation, ptr(n))
	  enddo
	  if (Xf.annt.p.ne.0) call M$STMFREE(Xf.mannt * L_M_Ptr, Xf.annt)
	  if (Xf.adefault.p.ne.0) call M$STMFREE(L_xfannotation, Xf.adefault)
	  Xf.annt.p = 0
	  Xf.adefault.p = 0
	  Xf.mannt = 0
	  Xf.nannt = 0
	endif

	if (Xf.adefault.p .eq. 0) then
	  call M$STMALLOC(L_xfannotation, Xf.adefault)
	  td_ptr = Xf.adefault.p
	  Td.x = 100
	  Td.y = 100
	  Td.h = 0
	  Td.mode = L_XFAFill
	  Td.size = 0
	  Td.text = ' '
	  Td.color = ' '
	  Td.f_id = 0
	  Td.p_id = 0
	  Td.pixel = L_NotSet
	  Td.font = '*Schoolbook-Bold-I-*-180-75-75-*'
	endif
	
	td_ptr = Xf.adefault.p
c
	if (mode.eq.3) then
	  if (MQD.ANNOTATE.func.eq.'NEW') then
	    n = Xf.nannt + 1
	  elseif (Xf.iarg.lt.0) then
	    n = max(1, n+Xf.nannt+1)
	  else
	    n = Xf.iarg
	  endif
	  ptr_ptr = Xf.annt.p
	  if (n.gt.0 .and. n.le.Xf.nannt) tt_ptr = ptr(n).p

	  if (Xf.aview) then
	    do i = Xf.nannt, max(1,n), -1
	      call MX$TSELECT(%val(ptr(i).p), L_RemoveText)
	    enddo
	  endif

	  if (MQD.ANNOTATE.func.eq.'NEW') then
	    call M$MOVE(Td, Tn, L_xfannotation)
	    Tn.text = MQD.ANNOTATE.text
	    x = MQD.ANNOTATE.arg1
	    y = MQD.ANNOTATE.arg2
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	    if (IAND(int(Tn.mode),L_XFANoGravity).eq.0) then
c--	    	    
	      x = x * Mx.width / 1000
	      y = y * Mx.height / 1000
	    endif
	    call ADD_ANNOTATION(Mx, Xf, Tn, x, y)
	    ptr_ptr = Xf.annt.p
	  elseif (n.gt.Xf.nannt) then
	    return
	  elseif (MQD.ANNOTATE.func.eq.'DELETE') then
	    call DELETE_ANNOTATE(Xf, n)
	  elseif (MQD.ANNOTATE.func.eq.'TEXT') then
	    tt.text = MQD.ANNOTATE.text
	    tt.h = 0
	  elseif (MQD.ANNOTATE.func.eq.'MOVE') then
	    if (Xf.mbox.le.0 .or. Xf.bnumbers.le.0) then
	      tt.x = MQD.ANNOTATE.arg1
	      tt.y = MQD.ANNOTATE.arg2
	    endif
	  elseif (MQD.ANNOTATE.func.eq.'FONT') then
	    tt.font = MQD.ANNOTATE.text
	    tt.f_id = 0
	  elseif (MQD.ANNOTATE.func.eq.'COLOR') then
	    tt.color = MQD.ANNOTATE.text
	    tt.pixel = L_NotSet
	  elseif (MQD.ANNOTATE.func.eq.'BGFILL') then
	    set = (MQD.ANNOTATE.arg1 .ne. 0)
c--MODIFIED CODE: Intel Compiler, casting integer	    	    	    
	    if (MQD.ANNOTATE.arg1.lt.0) set = (IAND(int(Td.mode),L_XFAFill).eq.0)
c--	    
	    if (set) then
c--MODIFIED CODE: Intel Compiler, casting integer	    	    	    
	      tt.mode = IOR(int(tt.mode),L_XFAFill)
c--	      
	    else
              tt.mode = IAND(int(tt.mode),NOT(L_XFAFill))
	    endif
	  elseif (MQD.ANNOTATE.func.eq.'ANCHOR') then
            tt.mode = IAND(int(tt.mode),
     +                 NOT(L_XFAMidAnchor+L_XFAEndAnchor))
	    if (MQD.ANNOTATE.text .eq. 'CENTER') then
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	      tt.mode = IOR(int(tt.mode),L_XFAMidAnchor)
	    elseif (MQD.ANNOTATE.text .eq. 'RIGHT') then
	      tt.mode = IOR(int(tt.mode),L_XFAEndAnchor)
	    endif
	  elseif (MQD.ANNOTATE.func.eq.'GRAVITY') then
	    set = (MQD.ANNOTATE.arg1 .ne. 0)
	    if (MQD.ANNOTATE.arg1.lt.0) set = (IAND(int(Td.mode),L_XFAFill).eq.0)
	    if (set) then
              tt.mode = IAND(int(tt.mode),NOT(L_XFANoGravity))
	    else
	      tt.mode = IOR(int(tt.mode),L_XFANoGravity)
c--	      
	    endif
	  endif

	  if (Xf.aview) then
	    do i = max(1,n), Xf.nannt
	      call MX$TDRAW(%val(ptr(i).p))
	    enddo
	  endif

	elseif (mode.eq.5) then				! SET messages
	  if (MQD.SET.object .eq. 'ANNTVIEW') then
	    ptr_ptr = Xf.annt.p
	    if (Xf.iarg.eq.-1 .or. Xf.aview .neqv. (Xf.iarg.ne.0)) then
	      if (Xf.aview) then
		do n = Xf.nannt, 1, -1
		  call MX$TSELECT(%val(ptr(n).p), L_RemoveText)
		enddo
	      endif
	      Xf.aview = .not. Xf.aview
	      if (Xf.aview) then
		do n = 1, Xf.nannt
		  call MX$TDRAW(%val(ptr(n).p))
		enddo
	      endif
	    endif
	  elseif (MQD.SET.object .eq. 'ANNTFILL') then
	    set = (Xf.iarg .ne. 0)
c--MODIFIED CODE: Intel Compiler, casting integer	    	    	      	    
	    if (Xf.iarg .eq. -1) set = (IAND(int(Td.mode),L_XFAFill).eq.0)	    
	    if (set) then
	      Td.mode = IOR(int(Td.mode),L_XFAFill)
	    else
              Td.mode = IAND(int(Td.mode),NOT(L_XFAFill))
	    endif
	  elseif (MQD.SET.object .eq. 'ANNTGRAV') then
	    set = (Xf.iarg .eq. 0)
	    if (Xf.iarg .eq. -1) set = (IAND(int(Td.mode),L_XFANoGravity).ne.0)
	    if (set) then
	      Td.mode = IOR(int(Td.mode),L_XFANoGravity)
c--	      
	    else
              Td.mode = IAND(int(Td.mode),NOT(L_XFANoGravity))
	    endif
	  endif
c
	elseif (mode.eq.6) then 		! load file from message
	  if (MQD.NEWFILE.filename .eq. ' ') then
	    call DELETE_ANNOTATE(Xf, 0)
	  else
	    call ANNOTATE_FILE(Xf, MQD.NEWFILE.filename, 1)
	  endif
c
	elseif (mode.eq.L_PreLoad) then 	! preload file
	  call ANNOTATE_FILE(Xf, MQD.NEWFILE.filename, 0)
c
	endif

	if (mode.ne. L_Interactive) return

c**************
c	Interactive mode
c**************

	titlebar = 'Text Annotate: LM-New/Select MM-Menu RM-Delete'
	call MX$SETBAR (titlebar)
	nw = 0
	nwl = 0
	drag = 0
	Mx.button_press = 0
	exposure = .false.
	tied2boxes = (Xf.mbox.gt.0 .and. Xf.bnumbers.gt.0)

	do while (.not. Mc.break .or. Mx.breakflag)
	  call MX$EVENT()
	  if (Mc.break) return
	  if (Mc.sysunlock .or. Mx.breakflag) then	! system unlock (exit)
	    if (Mc.sysunlock) exposure = .true.	! or window Close
	    goto 999
	  elseif (Mx.button_release.eq.1) then
	    ! Modify annotate text
	    if (nw.gt.0 .and. drag.eq.2) then
	      call ANNOTATE_MENU(Mx, Xf, nw, 1)
	      ptr_ptr = Xf.annt.p
	      if (nw.gt.0) tt_ptr = ptr(nw).p
	    endif
	    drag = 0
	  elseif (Mx.motion_notify) then
	    if (nw.gt.0 .and. drag.gt.0) then
	      if (tied2boxes .and. nw.le.Xf.nbox) then
		call MX$MESSAGE('Tied to boxes, cannot be moved.', -2.0)
		drag = 0
	      else
		Mx.xpos = Mx.xpos - xoff
		Mx.ypos = Mx.ypos - yoff
		call MX$TSELECT(tt, L_DeselectText)
		call MX$TSELECT(tt, L_MoveText)
		call MX$TSELECT(tt, L_SelectText)
		drag = 1
	      endif
	    endif
	  elseif (Mx.key_press.eq.13) then		! Select/Deselect
	    if (nw.gt.0) then
	      call MX$TSELECT (tt, L_DeselectText)
	      nw = 0
	    else
	      Mx.button_press = 1
	    endif
	  elseif (Mx.key_upchar.eq.'N') then
	    Mx.button_press = 1
	  elseif (Mx.key_upchar.eq.'S') then			! Select
	    if (nw.gt.0) then		! if already selected, deselect first
	      call MX$TSELECT (tt, L_DeselectText)
	      nw = 0
	    endif
	    Mx.button_press = 1
	  elseif (Mx.key_upchar.eq.'D') then			! Delete
	    Mx.button_press = 3
	  elseif (Mx.key_upchar.eq.'M') then			! Menu
	    Mx.button_press = 2
	  elseif (Mx.key_upchar.eq.'?') then			! Help
	    call MX$MHELP ('~ANNOTATE OPTIONS','Annotate Mode')
	  elseif (Mx.key_upchar.eq.'Q') then			! Quit
	    if (nw.gt.0) call MX$TSELECT (tt, L_DeselectText)
	    goto 999
	  endif

	  if (Mx.button_press.eq.2) then
	    if (nw.le.0) then	! general menu
	      menu = '~ANNOTATE OPTIONS;New Annotation...,Save to '//
     &		     'File...,Load File...,Clear All,Default Setti'//
     &		     'ngs...,- Toggle Display,Exit Annotate'
	      i = M$SEARCH(menu, 'Toggle Display') - 2
	      if (Xf.aview) menu(i:i) = '+' 
	      isel = MX$MENU(0, menu)
c
	      if (isel.eq.1) then
		call ADD_ANNOTATION(Mx, Xf, Td, Mx.xpos, Mx.ypos)

	      elseif (isel.eq.2) then		! Save to file
		filename = ' '
		call MX$PROMPT ('~Annotation Filename: ', filename)
		if (filename.ne.' ') call ANNOTATE_FILE(Xf, filename, -1)
c
	      elseif (isel.eq.3) then		! Load from file
		filename = ' '
		n = MX$FILES (filename)
		call MX$SETBAR (titlebar)
		if (n.gt.0) call ANNOTATE_FILE(Xf, filename, 2)
c
	      elseif (isel.eq.4) then		! Clear All
		call DELETE_ANNOTATE(Xf, 0)

	      elseif (isel.eq.5) then		! Default Settings
		x = Mx.xpos
		y = Mx.ypos
		idef = -1
		do while (idef.ne.0)
		  Mx.xpos = x
		  Mx.ypos = y
		  menu = '~ANNOTATE DEFAULTS;Font...,- BG Fill,'//
     &		         'Color...,Anchor...,- Gravity'
		  i = M$SEARCH(menu, 'BG Fill') - 2
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
		  if (IAND(int(Td.mode),L_XFAFill).ne.0) menu(i:i) = '+'
		  i = M$SEARCH(menu, 'Gravity') - 2
		  if (IAND(int(Td.mode),L_XFANoGravity).eq.0) menu(i:i) = '+'
		  idef = MX$MENU (0, menu)

		  if (idef.eq.1) then
		    call MX$TFONT (Td, L_FontAttributes)
		  elseif (idef.eq.2) then
		    if (IAND(int(Td.mode),L_XFAFill).ne.0) then
                      Td.mode = IAND (int(Td.mode), NOT(L_XFAFill))
		    else
		      Td.mode = IOR (int(Td.mode), L_XFAFill)
		    endif
		  elseif (idef.eq.3) then
		    call MX$TFONT (Td, L_ChangeColor)
		  elseif (idef.eq.4) then
		    call MX$TFONT (Td, L_ChangeAnchor)
		  elseif (idef.eq.5) then
		    if (IAND(int(Td.mode),L_XFANoGravity).ne.0) then
                      Td.mode = IAND (int(Td.mode), NOT(L_XFANoGravity))
		    else
		      Td.mode = IOR (int(Td.mode), L_XFANoGravity)
c--		      
		    endif
		  endif
		enddo
		Td.pixel = L_NotSet	! don't set pixel value into default
c
	      elseif (isel.eq.6) then		! Toggle Display
		ptr_ptr = Xf.annt.p
		Xf.aview = .not. Xf.aview
		if (Xf.aview) then
		  do n = 1,Xf.nannt
		    call MX$TDRAW(%val(ptr(n).p))
		  enddo
		else
		  do n = Xf.nannt, 1, -1
		    call MX$TSELECT(%val(ptr(n).p), L_RemoveText)
		  enddo
		endif
c
	      elseif (isel.eq.7) then		! exit
		goto 999
	      endif

	    else					! Text Options Menu

	      ! Note: if you add or move items here, be sure to change the
	      ! branching code down in ANNOTATE_MENU and update all the calls
	      ! to that routine to reflect the new item order
	      menu = '~SELECT OPTIONS;Modify Text,Font...,- BG Fill,'//
     &		     'Color...,Anchor...,- Gravity,Copy,Delete'

	      i = M$SEARCH(menu, 'BG Fill') - 2
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	      if (IAND(int(Tn.mode),L_XFAFill).ne.0) menu(i:i) = '+'
	      i = M$SEARCH(menu, 'Gravity') - 2
	      if (IAND(int(Tn.mode),L_XFANoGravity).eq.0) menu(i:i) = '+'
c--
	      isel = MX$MENU(0, menu)
	      if (isel.ne.0) then
		call ANNOTATE_MENU(Mx, Xf, nw, isel)
		ptr_ptr = Xf.annt.p
		if (nw.gt.0) tt_ptr = ptr(nw).p
	      endif
	    endif

	  elseif (Mx.button_press.ne.0) then
	    nwl = nw
	    if (nw.gt.0) call MX$TSELECT(tt, L_DeselectText)
	    nw = 0
	    drag = 0
c

	    if (Xf.aview .and. Mx.key_upchar.ne.'N') then
	      n = Xf.nannt
	      ptr_ptr = Xf.annt.p
	      do while (nw.le.0 .and. n.gt.0)
		tt_ptr = ptr(n).p
		i = 0
		if (nwl.eq.n) i = 7
		if (Mx.xpos.ge.tt.xc-i .and. Mx.xpos.le.tt.xc+tt.w+i .and.
     &		    Mx.ypos.ge.tt.yc-i .and. Mx.ypos.le.tt.yc+tt.h+i) then
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
		  if (IAND(int(tt.mode),L_XFANoGravity).ne.0) then
c--		  
		    xoff = Mx.xpos - tt.x
		    yoff = Mx.ypos - tt.y
		  else
		    xoff = Mx.xpos - tt.x * Mx.width / 1000
		    yoff = Mx.ypos - tt.y * Mx.height / 1000
		  endif

		  if (Mx.button_press.eq.1) then
		    if (nwl .eq. n) then
		      drag = 2		! second click
		    else
		      drag = 1		! first click
		    endif
		  endif

		  ! Bring new annotation to the front of the stacking order
		  ! unless we're tied to annotations: order is important
		  if (n.lt.Xf.nannt .and.
     &				.not.(tied2boxes .and. n.le.Xf.nbox)) then
		    do i = Xf.nannt , n, -1
		      call MX$TSELECT(%val(ptr(i).p), L_RemoveText)
		    enddo
		    do i = n+1, Xf.nannt
		      call MX$TDRAW (%val(ptr(i).p))
		      ptr(i-1).p = ptr(i).p
		    enddo
		    ptr(Xf.nannt).p = tt_ptr
		    call MX$TDRAW(tt)
		    n = Xf.nannt
		  endif
		  nw = n
		  call MX$TSELECT(tt, L_SelectText)

		endif
		n = n - 1
	      enddo
	    endif

	    if (nw.gt.0) then				! Delete
	      if (Mx.button_press.eq.3) call DELETE_ANNOTATE(Xf, nw)

	    elseif (Mx.key_upchar.eq.'S' .or. Mx.key_press.eq.13) then
	      call M$BEEP(1, 0.0)			! Failed select

	    elseif (Mx.button_press.eq.1) then	! New text
	      call ADD_ANNOTATION(Mx, Xf, Td, Mx.xpos, Mx.ypos)
	    endif
	  endif

	  if (Mx.exposure) exposure = .true.
	  if (Mx.resize) goto 999 	! Resize (may be caught in mhelp)
	enddo
c
 999	call MX$UNSETBAR ()
	Mx.exposure = exposure
	return
	end

	subroutine ANNOTATE_MENU (Mx, Xf, nw, item)
	implicit none
	include 'types.inc'
	include 'xprim.inc'
	include 'xfacilities.inc'
	record /M_PTR/ ptr(1)
	record /XFACILITIES/ Xf
	record /XFANNOTATION/ Tn, tt
	integer*4 nw, item
	pointer (ptr_ptr, ptr)
	pointer (tt_ptr, tt)

	ptr_ptr = Xf.annt.p
	tt_ptr = ptr(nw).p
	call M$MOVE(tt, Tn, L_xfannotation)

	if (item.eq.1) then
	  call MX$PROMPT('~Annotation Text: ', Tn.text)
	  Tn.size = M$LENGTH(Tn.text)
	  if (Tn.size.eq.0) then
	    call DELETE_ANNOTATE(Xf, nw)
	    return
	  elseif (Tn.text .eq. tt.text) then
	    return
	  endif
	  Tn.h = 0
	elseif (item.eq.2) then
	  call MX$TFONT(Tn, L_FontAttributes)
	elseif (item.eq.3) then
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	  if (IAND(int(Tn.mode),L_XFAFill).ne.0) then
            Tn.mode = IAND(int(Tn.mode), NOT(L_XFAFill))
	  else
	    Tn.mode = IOR(int(Tn.mode), L_XFAFill)
c--	    
	  endif
	elseif (item.eq.4) then
	  call MX$TFONT(Tn, L_ChangeColor)
	elseif (item.eq.5) then
	  call MX$TFONT(Tn, L_ChangeAnchor)
	  if (Xf.bnumbers.gt.0) then
	    Tn.x = tt.x
	    Tn.y = tt.y
	  endif
	elseif (item.eq.6) then
	  if (Xf.mbox.gt.0 .and. Xf.bnumbers.gt.0) then
	    call MX$MESSAGE('Illegal when tied to annotations', 1.0)
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	  elseif (IAND(int(Tn.mode),L_XFANoGravity).ne.0) then	  
            Tn.mode = IAND(int(Tn.mode), NOT(L_XFANoGravity))
	    Tn.x = NINT(Tn.x * 1000.0 / Mx.width)
	    Tn.y = NINT(Tn.y * 1000.0 / Mx.height)
	  else
	    Tn.mode = IOR(int(Tn.mode), L_XFANoGravity)
c--	    
	    Tn.x = NINT(Tn.x * Mx.width / 1000.0)
	    Tn.y = NINT(Tn.y * Mx.height / 1000.0)
	  endif
	elseif (item.eq.7) then
	  Tn.x = Tn.x + 20
	  Tn.y = Tn.y + 20
	  call NEW_ANNOTATE(Xf)
	  ptr_ptr = Xf.annt.p
	  nw = Xf.nannt
	elseif (item.eq.8) then
	  call DELETE_ANNOTATE(Xf, nw)
	  return
	endif

	call MX$TSELECT(tt, L_DeselectText)
	if (item .ne. 7) call MX$TSELECT(tt, L_RemoveText)
	Tn.p_id = 0
	call M$MOVE(Tn, %val(ptr(nw).p), L_xfannotation)
	call MX$TDRAW(%val(ptr(nw).p))
	call MX$TSELECT(%val(ptr(nw).p), L_SelectText)

	return
	end

	subroutine ANNOTATE_FILE (Xf, filename, mode)
	implicit none
	include 'types.inc'
	include 'headers.inc'
	include 'xfacilities.inc'
c Parameters
	record /XFACILITIES/ Xf
	character*(*) filename
	integer*4 mode		! -1=save, 0=preload, 1=newfile msg, 2=load
c Locals
	record /HEADER/ HCB
	integer*4 i
	record /M_PTR/ ptr(1)	! pointers to XFANNOTATION structs
	record /XFANNOTATION/ tt, td
	pointer (tt_ptr, tt)
	pointer (td_ptr, td)
	pointer (ptr_ptr, ptr)

	call M$INITIALIZE (HCB)    		    
	HCB.file_name = filename
	ptr_ptr = Xf.annt.p

	if (mode .lt. 0) then
	  HCB.type = 3900
	  HCB.format = 'NH'
	  HCB.switch = 'NOABORT'
	  call M$ADDSREC(HCB, 'X', 'I')
	  call M$ADDSREC(HCB, 'Y', 'I')
	  call M$ADDSREC(HCB, 'MODE', '1I')
	  call M$ADDSREC(HCB, 'SIZE', '1I')
	  call M$ADDSREC(HCB, 'TEXT', 'TA')
	  call M$ADDSREC(HCB, 'FONT', 'TA')
	  call M$ADDSREC(HCB, 'COLR', '3A')
	  HCB.size = Xf.nannt
	  call M$ALLOCATE(HCB)
	  if (HCB.open) then
	    do i = 1, Xf.nannt
	      call M$FILAD(HCB, %val(ptr(i).p), 1)
	    enddo
	    call M$CLOSE(HCB)
	  else
	    call MX$MESSAGE('Error opening file', 1.0)
	  endif
	else
	  HCB.type_allow = '3900'
	  HCB.format_allow = 'NH'
	  HCB.switch = 'NOABORT'
	  call M$INOK (HCB)
	  if (HCB.open) then
	    if (mode.eq.1 .and. Xf.iarg.eq.-1) then
	      do i = Xf.nannt, 1, -1
	        if (Xf.aview) call MX$TSELECT(%val(ptr(i).p), L_RemoveText)
	        call M$STMFREE (L_xfannotation, ptr(i))	
	      enddo
	      Xf.nannt = 0
	    endif
	    td_ptr = Xf.adefault.p
	    do i = 1, NINT(HCB.size)
	      call NEW_ANNOTATE(Xf)
	      ptr_ptr = Xf.annt.p
	      tt_ptr = ptr(Xf.nannt).p
!	      tt = td   !! bug in PFC cant handle this yet
	      call M$MOVE (td,tt,L_xfannotation) !! using this temporarily
	      tt.f_id = 0
	      call M$GRAB(HCB, tt, DBLE(i), 1, -1)
	      if (mode.ne.0.and.Xf.aview) call MX$TDRAW(tt)
	    enddo
	    call M$CLOSE (HCB)
	  elseif (mode.eq.2) then
	    call MX$MESSAGE('Error opening file '//HCB.file_name, 1.0)
	  else
	    call M$WARNING('Error opening file '//HCB.file_name)
	  endif
	endif

	return
	end

	subroutine NEW_ANNOTATE (Xf)
	include 'types.inc'
	include 'xfacilities.inc'
	record /XFACILITIES/ Xf
	record /M_PTR/ ptr(1)
	pointer (ptr_ptr, ptr)
	integer*4 mannt

	if (Xf.annt.p .eq. 0) then
	  call M$STMALLOC(Xf.mannt * L_M_Ptr, Xf.annt)
	elseif (Xf.nannt .ge. Xf.mannt) then
	  mannt = Xf.nannt + 10
	  call M$STMALLOC(mannt * L_M_Ptr, ptr_ptr)
	  call M$MOVE(%val(Xf.annt.p), ptr, Xf.nannt * L_M_Ptr)
	  call M$STMFREE(Xf.mannt * L_M_Ptr, Xf.annt)
	  Xf.annt.p = ptr_ptr
	  Xf.mannt = mannt
	endif
	ptr_ptr = Xf.annt.p
	Xf.nannt = Xf.nannt + 1
	call M$STMALLOC(L_xfannotation, ptr(Xf.nannt))
	return
	end

	subroutine DELETE_ANNOTATE (Xf, n)
	include 'types.inc'
	include 'xfacilities.inc'
	record /XFACILITIES/ Xf
	integer*4 n, i, n1, n2
	record /M_PTR/ ptr(1)
	pointer (ptr_ptr, ptr)

	ptr_ptr = Xf.annt.p
	if (n.le.0) then
	  do while (Xf.nannt.gt.0)
	    call MX$TSELECT(%val(ptr(Xf.nannt).p), L_RemoveText)
	    call M$STMFREE(L_xfannotation, ptr(Xf.nannt))
	    Xf.nannt = Xf.nannt - 1
	  enddo
	elseif (n.le.Xf.nannt) then
	  call MX$TSELECT(%val(ptr(n).p), L_DeselectText)
	  call MX$TSELECT(%val(ptr(n).p), L_RemoveText)
	  call M$STMFREE (L_xfannotation, ptr(n))	
	  do i = n+1,Xf.nannt
	    ptr(i-1).p = ptr(i).p
	  enddo
	  Xf.nannt = Xf.nannt - 1
	  n = 0
	endif

	return
	end

c************
c Actual routine called by MX$ANNOTATE passing the Mx structure.  
c MX$ANNOTATE is a C jacket in gjackets.c that passes its global 
c pointer to the Mx structure along with the other arguments.  See HELP 
c MX$ANNOTATE for more info.
c************

	subroutine MXF$ANNOTATE (Mx, mode)

	implicit none

	include 'types.inc'
	include 'midas.inc'
	include 'xprim.inc'
	include 'xfacilities.inc'
	integer*4 mtext, mode
	parameter (mtext=20)
	integer*4 n

	SAVE
	logical*4  uninitialized /.TRUE./
	record /XFACILITIES/ Xf

	if (mode .eq. L_CleanExit) then
	  if (uninitialized) return
	  uninitialized = .true.
	elseif (uninitialized) then
	  Xf.annt.p = 0
	  Xf.adefault.p = 0
	  Xf.aview = .true.
	  Xf.nannt = 0
	  Xf.mannt = mtext
	  uninitialized = .false.
	endif

	call MXU$ANNOTATE(Mx, Xf, 0, mode)

	return
	end

	subroutine ADD_ANNOTATION(Mx, Xf, Td, x, y)
	include 'types.inc'
	include 'xprim.inc'
	include 'xfacilities.inc'
	record /XFACILITIES/ Xf
	record /XFANNOTATION/ Td, Tn
	integer*4 x, y

	record /M_PTR/ ptr(1)
	pointer (ptr_ptr, ptr)

	call M$MOVE(Td, Tn, L_xfannotation)

	if (Tn.text.eq.' ') call MX$PROMPT('~Annotation Text: ', Tn.text)
	Tn.size = M$LENGTH(Tn.text)
	if (Tn.size.gt.0) then
c--MODIFIED CODE: Intel Compiler, casting integer	    	    
	  if (IAND(int(Tn.mode),L_XFANoGravity) .eq. 0) then
c--
	    Tn.x = 1000 * x / Mx.width
	    Tn.y = 1000 * y / Mx.height
	  else
	    Tn.x = x
	    Tn.y = y
	  endif
	  call MX$TDRAW (Tn)
	  if (.not.Xf.aview) then
	    call MX$TSELECT (Tn, L_RemoveText)
	    call MX$MESSAGE ('Display toggled off', 1.0)
	  endif
	  call NEW_ANNOTATE(Xf)
	  ptr_ptr = Xf.annt.p
	  call M$MOVE(Tn, %val(ptr(Xf.nannt).p), L_xfannotation)
	endif

	return
	end
