indexing

	description: "[

		Delegate of an EM_TEXTBOX.

	]"
	date: "$Date$"
	revision: "$Revision$"

class EM_ECLIPSE_TEXTBOX_DELEGATE

inherit

	EM_TEXTBOX_DELEGATE
		redefine
			install,
			optimal_width,
			optimal_height,
			draw_body
		end

feature -- Initialisation

	install (textbox: EM_TEXTBOX) is
			-- Install style on `textbox'.
			-- Set up all default values for background, border, font and colors.
		do
			Precursor {EM_TEXTBOX_DELEGATE} (textbox)
			textbox.set_background_color (theme_colors.standard_background)
			textbox.set_border (create {EM_BEVEL_BORDER}.make_down)
		end

feature -- Measurement

	optimal_width (textbox: EM_TEXTBOX): INTEGER is
			-- Optimal width of `textbox'
		do
			Result := textbox.font.string_width (textbox.text) + textbox.border.left + textbox.border.right + 2
		end

	optimal_height (textbox: EM_TEXTBOX): INTEGER is
			-- Optimal height of `textbox'
		do
			Result := textbox.font.string_height (textbox.text) + textbox.border.top + textbox.border.bottom + 2
		end

feature -- Drawing

	draw_body (textbox: EM_TEXTBOX) is
			-- Draw body of `textbox'.
		local
			text_x, text_y: INTEGER
		do
			if textbox.text.count > 0 then
				text_x := textbox.border.left + 1
				text_y := (textbox.height-textbox.optimal_height) // 2 + 1
				textbox.surface.set_drawing_color (textbox.foreground_color)
				textbox.font.draw_string (textbox.text, textbox.surface, text_x, text_y)
			end
			if textbox.is_cursor_visible then
				textbox.surface.put_line_segment (textbox.cursor_x+textbox.border.left, textbox.border.top+1, textbox.cursor_x+textbox.border.left, textbox.height-textbox.border.bottom-1, textbox.foreground_color)
			end
		end

feature -- Basic operations

	position_to_text_index (textbox: EM_TEXTBOX; a_x, a_y: INTEGER): INTEGER is
			-- Index in string at position `a_x' `a_y'.
		local
			current_x, index: INTEGER
		do
			from
				current_x := textbox.border.left
				index := 1
			until
				index > textbox.text.count or current_x >= a_x
			loop
				current_x := 1 + textbox.font.string_width (textbox.text.substring (1, index))
				index := index + 1
			end
			Result := index - 1
		end

end