# IMBROWSE.GUI -- Image browser user interface.

reset-server
appInitialize imbrowse Imbrowse {
    *objects: \
	toplevel		Paned		panel \
\
	panel			Box		statusBox \
	statusBox		MenuButton	dirSelect \
	statusBox		AsciiText	dirName \
	statusBox		Command		imageButton \
	statusBox		Command		helpButton \
	statusBox		Command		quitButton \
\
	panel			Viewport	objView \
	objView			List		objList \
\
	panel			Form		controlForm \
	controlForm		Label		buttonHelp \
	controlForm		Label		templateLabel \
	controlForm		AsciiText	templateText \
	controlForm		Command		prevButton \
	controlForm		Command		nextButton \
	controlForm		Command		headerButton \
	controlForm		Command		displayButton \
\
	panel			AsciiText	headerText \
\
	panel			Box		sectionBox \
	sectionBox		Label		sectionLabel \
	sectionBox		AsciiText	sectionText \
\
	toplevel		TopLevelShell	imageShell \
	imageShell		Form		imageForm \
	imageForm		Label		imageTitle \
	imageForm		Gterm		imageWindow \
\
	toplevel		Parameter	imbrowse \
	imbrowse		Parameter	template \
	imbrowse		Parameter	section \
	imbrowse		Parameter	directory \
	imbrowse		Parameter	subdirs \
	imbrowse		Parameter	files \
	imbrowse		Parameter	image_title \
	imbrowse		Parameter	header \
	imbrowse		Parameter	errormsg \

    *panel.orientation:			vertical
    *displayCaret:			false

    *background:			LightSkyBlue
    *shapeStyle:			Rectangle
    *beNiceToColormap:			False
    *Label*shadowWidth:			2

    *statusBox.orientation:		horizontal
    *statusBox.showGrip:		False
    *statusBox.skipAdjust:		True
!   *statusBox.background:		gray61
    *dirSelect.label:			Directory:
    *dirSelect.menuName:		dirMenu
    *dirName*background:		#a7eeff
    *dirName.width:			403
    *dirName*displayCaret:		True
    *dirName*editType:			edit
    *imageButton.label:			Image
    *helpButton.label:			Help
    *helpButton.sensitive:		False
    *quitButton.label:			Quit

    *objView.allowHoriz:		False
    *objView.allowVert:			True
    *objView.forceBars:			True
    *objView.min:			60
    *objView*background:		#a7eeff
!   *objList*height:			100
    *objList*verticalList:		True

    *controlForm.showGrip:		True
    *controlForm.skipAdjust:		True
    *buttonHelp.borderWidth:		0
    *buttonHelp.width:			613
    *templateLabel.label:		Images:
    *templateLabel.fromVert:		buttonHelp
    *templateLabel.borderWidth:	0
    *templateText*background:		#a7eeff
    *templateText*font:			7x13bold
    *templateText*width:		230
    *templateText.fromHoriz:		templateLabel
    *templateText.fromVert:		buttonHelp
    *templateText*displayCaret:		True
    *templateText*editType:		edit
    *prevButton.label:			PrevIm
    *prevButton.fromHoriz:		templateText
    *prevButton.fromVert:		buttonHelp
    *nextButton.label:			NextIm
    *nextButton.fromHoriz:		prevButton
    *nextButton.fromVert:		buttonHelp
    *headerButton.label:		Show Header
    *headerButton.fromHoriz:		nextButton
    *headerButton.fromVert:		buttonHelp
    *displayButton.label:		Display Image
    *displayButton.fromHoriz:		headerButton
    *displayButton.fromVert:		buttonHelp

    *headerText.showGrip:		False
    *headerText.skipAdjust:		False
    *headerText.min:			100
!   *headerText.height:			100
    *headerText.scrollVertical:		always
    *headerText.scrollHorizontal:	whenNeeded
    *headerText*font:			7x13
    *headerText*background:		#a7eeff

    *sectionBox.orientation:		horizontal
    *sectionBox.showGrip:		False
    *sectionBox.skipAdjust:		True
    *sectionLabel.label:		Display image section:
    *sectionLabel.borderWidth:		0
    *sectionText*background:		#a7eeff
    *sectionText.width:			460
    *sectionText*font:			7x13bold
    *sectionText*displayCaret:		True
    *sectionText*editType:		edit

    *imageTitle.borderWidth:		0
    *imageTitle.width:			512
    *imageWindow.cmapName:		image
    *imageWindow.ginmodeCursor:		circle
    *imageWindow.width:			512
    *imageWindow.height:		512
    *imageWindow.fromVert:		imageTitle

    *imageWindow.translations: \
	       <Btn1Down>:	m_create()                   \n\
        !Shift <Btn2Down>:	crosshair(on)		     \n\
      !Shift <Btn2Motion>:	crosshair(on)		     \n\
		 <Btn2Up>:	crosshair(off)		     \n\
	       <Btn2Down>:	call(zoom,$x,$y)	     \n\
               <Btn3Down>:	call(windowColormap,$x,$y)   \n\
             <Btn3Motion>:	call(windowColormap,$x,$y)   \n\
	    <EnterWindow>:	enter-window()               \n\
	    <LeaveWindow>:	leave-window()               \n\
	       <KeyPress>:	graphics-input()             \n\
		 <Motion>:	track-cursor()
}

# Start up the GUI.
createObjects
send imageShell realize
send imageWindow setGterm
activate

# Global variables.
set version			"Imbrowse 0.1 - Alpha Test Version"

# Display the program version number on startup.
send buttonHelp set label $version

#proc reactivate {} {
#}; send server postActivateCallback reactivate


# Callbacks for client state variables (UI parameter objects).  When the
# client's state changes it updates a UI parameter to reflect the change.
# This produces a callback to one or more of the callbacks defined below,
# used to update the GUI to reflect the changing state of the client.

proc setTemplate {param old new} {
    send templateText set string $new
}; send template addCallback setTemplate

proc setSection {param old new} {
    send sectionText set string $new
}; send section addCallback setSection

proc setDirectory {param old new} {
    send dirName set string $new
}; send directory addCallback setDirectory

proc setImageTitle {param old new} {
    send imageTitle set label $new
}; send image_title addCallback setImageTitle

proc setFiles {param old new} {
    send objList setList $new resize
}; send files addCallback setFiles

proc setSubdirs {param old new} {
    foreach dir $new {
	lappend itemList "$dir f.exec \{ send client gcmd chdir $dir \}"
    }
    createMenu dirMenu dirSelect $itemList
}; send subdirs addCallback setSubdirs

proc setErrormsg {param old new} {
    send buttonHelp set label $new
}; send errormsg addCallback setErrormsg

proc setHeaderText {param old new} {
    send headerText set string $new
}; send header addCallback setHeaderText


# List widget callback.
proc select {widget cbtype item index} {
    set subdirs [send subdirs getValue]
    if [expr [lsearch $subdirs $item] != -1] {
	send client gcmd chdir $item
    }
}; send objList addCallback select


# Command callbacks.

proc image args {send imageShell map}
send imageButton addCallback image

proc help args { }
send helpButton addCallback help

proc quit args {
    #send imageShell unmap;  send client gkey q;  deactivate unmap
    send client gkey q;  deactivate unmap
}; send quitButton addCallback quit

proc next args {
    send objList getItem itemno
    if {$itemno == "none"} {
	send objList highlight 0
    } else {
	send objList highlight [expr "$itemno + 1"]
    }
}; send nextButton addCallback next

proc prev args {
    send objList getItem itemno
    if {$itemno == "none"} {
	send objList highlight 0
    } else {
	set itemno [expr "$itemno - 1"]
	if {$itemno >= 0} {
	    send objList highlight $itemno
	}
    }
}; send prevButton addCallback prev

proc headerCallback args {
    set image [send objList getItem itemno]
    if {$itemno != "none"} {
	printHeader $image
    }
}; send headerButton addCallback headerCallback

proc printHeader {image} {
    send client gcmd header $image
}

proc displayCallback args {
    set image [send objList getItem itemno]
    if {$itemno != "none"} {
	displayImage $image
    }
}; send displayButton addCallback displayCallback

proc displayImage {image} {
    send imageShell map
    send imageWindow setGterm
    send client gcmd display $image
}

proc directoryCallback {widget cbtype text} {
    send client gcmd chdir $text
}; send dirName addCallback directoryCallback


proc templateCallback {widget cbtype text} {
    send client gcmd template $text
}; send templateText addCallback templateCallback

proc sectionCallback {widget cbtype text} {
    send client gcmd section $text
}; send sectionText addCallback sectionCallback


# Window the displayed image.
proc windowColormap {x y} \
{
    set winWidth [send imageWindow get width]
    set winHeight [send imageWindow get height]

    send imageWindow loadColormap 1 \
	[expr "$x.0 / $winWidth"] \
	[expr "($y.0 - $winHeight / 2.0) / $winHeight * 10.0"]
}

# ZOOM and PAN.
set xcen 0
set ycen 0

# Zoom or pan image at given center.
proc zoom {x y} \
{
        global xcen ycen

        # Convert raw screen coordinates to frame buffer raster coordinates.
        send imageWindow unmapPixel $x $y raster rx ry

        # Select a pixel.
        set rx [expr "int ($rx)"]
        set ry [expr "int ($ry)"]

        # If the pointer did not move (much) zoom the image, otherwise
        # pan it.

        if {sqrt(pow($rx-$xcen, 2) + pow($ry-$ycen, 2)) < 4} {
            send client gkey Z
        } else {
            send client gkey M
        }

	set xcen $rx
	set ycen $ry
}


# Button help feature.

set help(dirName)	   "current directory"
set help(dirSelect)	   "press to get directory selection menu"
set help(displayButton)	   "press to display currently selected image"
set help(templateLabel)    "list of image file templates"
set help(templateText)     "enter new list of image templates and press return"
set help(headerButton)	   "press to list header of currently selected image"
set help(headerText)	   "the image header is displayed here"
set help(helpButton)	   "press to get help for imbrowse"
set help(nextButton)	   "advance to next image"
set help(objList)	   "images in current directory are listed here"
set help(prevButton)	   "back up to previous image"
set help(quitButton)	   "press to exit the imbrowse application"
set help(sectionLabel)	   "image section for displaying image"
set help(sectionText)	   "enter new image section and press return"

set widgets {
    dirSelect dirName helpButton quitButton objList templateLabel
    templateText nextButton prevButton headerButton displayButton
    headerText sectionLabel sectionText
}

proc helpCallback { widget event args } {
    global help
    if {$event == "enterNotify"} {
	send buttonHelp set label $help($widget)
    }
}

foreach widget $widgets {
    send $widget addEventHandler helpCallback enterWindowMask
}

#set timerId 0
#set timerState 0
#proc timer args {
#    global timerId timerState
#    if {$timerState} {
#	send quitButton set background red
#	set timerState 0
#    } else {
#	send quitButton set background blue
#	set timerState 1
#    }
#    set timerId [postTimedCallback timer 1000]
#}; set timerId [postTimedCallback timer 1000]
