# $Id: Form_classes,v 2.7 1995-07-12 11:29:37+01 zkoppany Rel $
#
# $Log: Form_classes,v $
# Revision 2.7  1995-07-12 11:29:37+01  zkoppany
# Checkin for: 2.7
#
# Revision 2.6  1995/05/30  16:22:49  zkoppany
# Checkin for: 2.6
#
# Revision 2.5  1994/10/31  10:51:37  zkoppany
# Checkin for: 2.5
#
# Revision 2.4  1994/08/01  13:37:14  zkoppany
# Checkin for: 2.4
#
# Revision 2.3.2.1  1994/07/22  10:05:31  zkoppany
# General master-detail examples.
# The photo is part of the canvas and it can be dragged&dropped.
#
# Revision 2.3  1994/06/15  14:28:56  zkoppany
# *** empty log message ***
#
#

itcl_class Form_detail {
	inherit Form@

	destructor {
		catch {$this-detail delete}

		if {$exit_process} {
			exit;			# Terminates the process.
		}
	}

	method UpdateFormular {} {
		Form@::UpdateFormular

		set fld_cont [field get $master_field]
		if {[SQLischar [lindex $qry(field_type) \
			[field index $master_field]]]} {

			set sep "'"
		} else {
			set sep ""
		}

		set fld ${sep}${fld_cont}$sep

		set cmd "$detail_query where $detail_field"

		if {[string compare $fld_cont ""] == 0} {
			append cmd " is null"
		} else {
			append cmd "=${fld}"
		}

		if {[catch {$this Query $cmd det}]} {
			global SQLerrorInfo

			SQLError $SQLerrorInfo

			return
		}

		if {[llength [itcl_info objects $this-detail]] == 0} {
			frame $frame.detail
			Grid@ $this-detail -frame $frame.detail\
				-Buttons 0\
				-data_list $det(record)\
				-header_list $det(field_name)\
				-type_list $det(field_type)\
				-mark_only_rows 0\
				-max_height 2

			pack $frame.detail -side top -fill x -expand no
		} else {
			$this-detail config -data_list $det(record)
		}
	}

	public master_field {}
	public detail_field {}
	public detail_query {}
	public exit_process 0
}

itcl_class Form_detail_photo {
	inherit Form_detail

	method UpdateFormular {} {
		Form_detail::UpdateFormular

		$this iconCmd
	}

	method iconCmd {} {
		global odd_home

# Takes the name into photo and creates a file name.
		set photo [field get $photo_field]
		set file $odd_home/demos/bitmaps/$photo.gif

		if {[string compare $photo ""] == 0 || ![file exists $file]} {
			set file ""
		} else {
			set file @$file
		}
# Checks whether it already exists.
		set id [$canv find withtag Photo]

# 	It does not exist, thus we create it.
		if {$id == ""} {
			set id [$canv create bitmap 300 5 \
				-tag Photo -anchor nw -bitmap $file]

#	Drag & Drop
			$canv bind $id <Any-B1-Motion> \
				"$this MovePhoto %W %x %y"
		} else {
			$canv itemconfigure $id -bitmap $file
		}
	}

# This method changes the position of the photo.

	method MovePhoto {c x y} {
		set coords [$c coords current]
	
		set x [$c canvasx $x]
		set y [$c canvasy $y]
	
		set y_add [expr $y - [lindex $coords 1]]
		set x_add [expr $x - [lindex $coords 0]]

		$c move current $x_add $y_add
	}

	public photo_field {}
}
