note
	description: "[
		Represents a port in VITP.
		]"
	author: "Jimmy J. Johnson"

deferred class
	PORT_WIDGET

inherit

	LOCATION_WIDGET
		rename
			location as port
		redefine
			create_interface_objects,
--			initialize,
			extend_widgets,
--			build_widgets,
			set_fonts_and_colors,
			position_widgets,
			set_widget_order,
			add_actions,
			set_target,
			paint,
			on_button_released,
--			on_pointer_leave,
--			on_pointer_motion,
			target_imp
		end

feature {NONE} -- Initialization

	create_interface_objects
			-- Initialize attributes
		do
print ("PORT_WIDGET.create_interface_objects %N")
			Precursor {LOCATION_WIDGET}
			create anchor
			create marker.make (port)
			create saved_point
		end

	extend_widgets
			-- Put widgets into Current.  This cannot be done
			-- in `create_interface_object', because cannot call
			-- `extend' feature until all objects [in descendants]
			-- are also created.
		do
			Precursor {LOCATION_WIDGET}
			extend (anchor)
			extend (marker)
		end

--	build_widgets
--			-- Now that widgets were created in `create_interface_objects'
--			-- and added to Current in `extend_widgets', build up the
--			-- widgets by adding internal structure to each widget.
--		do
--			Precursor {LOCATION_WIDGET}
--			build_land
--		end

	set_fonts_and_colors
			-- Set the default fonts for any text widgets and set
			-- the default colors for any contained widgets.
		do
			Precursor {LOCATION_WIDGET}
			if is_red then
				name_mt.set_font (Major_port_font)
				land_color := Red_port_color
			else
				name_mt.set_font (Port_font)
				land_color := Green_port_color
			end
		end

	position_widgets
			-- Move Current and contained widgets to correct
			-- location and set their their sizes
		do
			Precursor {LOCATION_WIDGET}
			marker.set_x_y (x, y)
			anchor.set_x_y (x, y)
			if not is_red then
				anchor.hide
				marker.hide_anchor
			end
		end

	set_widget_order
			-- Ensure the widgets are ordered properly
			-- (i.e. whose on top?)
		do
			Precursor {LOCATION_WIDGET}
			bring_to_front (land)
			bring_to_front (anchor)
			bring_to_front (marker)
			bring_to_front (text_group)
			bring_to_front (dot)
		end

	add_actions
			-- Add actions to the port (really just the marker)
		do
			Precursor {LOCATION_WIDGET}
			pointer_motion_actions.extend (agent on_pointer_motion)
			marker.pointer_button_press_actions.extend (agent on_marker_pressed)
--			pointer_leave_actions.extend (agent on_pointer_leave)
			jj_defining_figure.pointer_leave_actions.extend (agent on_leave_defining_figure)
		end

feature -- Status report

feature -- Status report

	is_red: BOOLEAN
			-- Does Current represent a red (i.e. major) port?
		deferred
		end

--	is_port_widget_initialized: BOOLEAN
--			-- Have the initialization rouines been called for Current
--			-- Current through this interface?
--			-- A check statment guards each initialization routine, preventing
--			-- any attempt to call these features a second time.  A second call
--			-- could happen in the case of muliple inheritance, which is used
--			-- for descendants that inherit multiply from ISLAND_PORT_WIDGET
--			-- and one of RED_PORT_WIDGET and GREEN_PORT_WIDGET (e.g. ATTU_WIDGET,
--			-- and TRUK_WIDGET).

feature -- Basic operations

	set_target (a_target: like port)
			-- Change the `port'.
		do
			Precursor {LOCATION_WIDGET} (a_target)
			marker.set_location (port)
		end

	paint
			-- Set the colors for the land and other areas
		local
			bc, c: EV_COLOR
		do
			Precursor {LOCATION_WIDGET}
			marker.paint
			anchor.set_foreground_color (adjusted_color (Black))
			anchor.set_background_color (adjusted_color (Black))
		end

feature {NONE} -- Implementation (actions)

	on_marker_pressed (ax: INTEGER; ay: INTEGER; a_button: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
								pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
				-- The `marker' was pressed
		do
			io.put_string ("PORT_WIDGET.on_marker_pressed %N")
			is_dragging_marker := True
			enable_capture
		end

	on_button_released (ax: INTEGER; ay: INTEGER; a_button: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
								pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
			-- React to button release by dropping the `marker' if
			-- was `is_dragging_marker'.
		local
			c: detachable EV_COORDINATE
		do
			Precursor {LOCATION_WIDGET} (ax, ay, a_button, x_tilt, y_tilt, pressure, a_screen_x, a_screen_y)
			if is_dragging_marker then
--				create c.make (ax, ay)
--				marker.center_on_point (c)
				marker.set_x_y (ax, ay)
--				disable_capture
			end
			is_dragging_marker := False
--			Precursor {LOCATION_WIDGET} (ax, ay, a_button, x_tilt, y_tilt, pressure, a_screen_x, a_screen_y)
		end

	on_leave_defining_figure
			-- If pointer leaves `defining_figure', then release the
			-- capture and drop the marker
		do
			io.put_string ("PORT_WIDGET.on_leave_defining_figure %N")
			if is_dragging_marker then
--				marker.center_on_point (saved_point)
				marker.set_x_y (saved_point.x, saved_point.y)
--				disable_capture
			end
			is_dragging_marker := False
		end

	on_pointer_motion (ax: INTEGER; ay: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
						pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
			-- The pointer is moving
		do
			io.put_string ("PORT_WIDGET.on_pointer_motion -- is_dragging_marker = " + is_dragging_marker.out + "%N")
			saved_point.set (ax, ay)
			if is_dragging_marker then
				if jj_defining_figure.position_on_figure (ax, ay) then
--					marker.center_on_point (saved_point)
					marker.set_x_y (saved_point.x, saved_point.y)
				else
--					disable_capture
					is_dragging_marker := false
				end
			end
		end

feature {NONE} -- Implementation

	target_imp: detachable PORT
			-- Anchor for the `target' represented by Current

	is_dragging_marker: BOOLEAN
			-- Is the marker being moved?

	saved_point: EV_COORDINATE
			-- The location within Current to where the mouse last moved

	marker: CONTROL_MARKER
			-- The marker associated with this port

	anchor: ANCHOR
			-- Image of an anchor for display on some ports

end
