class EL_DROP_DOWN_BOX

(source code)

Client examples: PANGO_CAIRO_TEST_MAIN_WINDOW

Description

List of drop down element choices mapped to a type specified by generic paramater G and initialized with the following:

1. an initial value of type G

2. a container conforming to FINITE [G]

3. a change agent of type PROCEDURE [G]

Optional initialization settings:

1. alphabetical ordering

2. Width adjustment for longest display string

note
	description: "[
		List of drop down element choices mapped to a type specified by generic paramater `G' and initialized with the following:
		
		**1. ** an initial value of type `G'
		
		**2. ** a container conforming to `FINITE [G]'
		
		**3. ** a change agent of type `PROCEDURE [G]'

		Optional initialization settings:
		
		**1. ** alphabetical ordering
		
		**2. ** Width adjustment for longest display string
	]"

	author: "Finnian Reilly"
	copyright: "Copyright (c) 2001-2017 Finnian Reilly"
	contact: "finnian at eiffel hyphen loop dot com"

	license: "MIT license (See: en.wikipedia.org/wiki/MIT_License)"
	date: "2018-05-21 12:30:20 GMT (Monday 21st May 2018)"
	revision: "3"

class
	EL_DROP_DOWN_BOX [G]

inherit
	EL_COMBO_BOX
		export
			{NONE} set_text
		redefine
			initialize
		end

	EL_INPUT_WIDGET [G]
		rename
			make as make_unadjusted,
			make_sorted as make_unadjusted_sorted
		undefine
			is_equal, default_create, copy
		end

create
	default_create, make, make_unadjusted, make_sorted, make_unadjusted_sorted

feature {NONE} -- Initialization

	initialize
		do
			Precursor
			create value_list.make (0)
		end

	make (a_initial_value: G; a_values: FINITE [G]; a_value_change_action: like value_change_action)
		do
			is_width_adjusted := True
			make_unadjusted (a_initial_value, a_values, a_value_change_action)
		end

	make_sorted (
		a_initial_value: G; a_values: FINITE [G]; a_value_change_action: like value_change_action; in_ascending_order: BOOLEAN
	)
			-- sorted alphabetially
		do
			is_width_adjusted := True
			make_unadjusted_sorted (a_initial_value, a_values, a_value_change_action, in_ascending_order)
		end

	make_widget (a_value_list: like new_value_list)
			-- make a box with actual values mapped to display values
		local
			selected_string: STRING_32
		do
			default_create
			value_list := a_value_list

			create selected_string.make_empty
			across a_value_list as value loop
				extend (create {EV_LIST_ITEM}.make_with_text (value.item.as_string_32))
				if value.item.is_current then
					selected_string := value.item.as_string_32
				end
			end
			if is_width_adjusted and then not is_empty then
				adjust_width
			end
			if not selected_string.is_empty then
				set_text (selected_string)
			end
			select_actions.extend (agent on_select_value)
		end

feature -- Access

	selected_value: G
		do
			Result := value_list.i_th (selected_index).value
		end

feature -- Element change

	set_value (a_value: G)
		do
			set_text (displayed_value (a_value).to_unicode)
		end

feature {NONE} -- Event handling

	on_select_value
		do
			value_change_action.call ([selected_value])
		end

feature {NONE} -- Implementation

	displayed_value (value: G): ZSTRING
		do
			Result := value.out
		end

	value_list: like new_value_list

end