#textdomain wesnoth-lib
###
### Definition of the window used to show large tooltips.
### The placement algorithms used are described in the
### »Tooltip placement« section in the GUI2 design document.
###
### gui/dialogs/title_screen.cpp has test code, which is
### activated by defining the DEBUG_TOOLTIP macro.
###

[window]
	id = "tooltip_large"
	description = "The tooltip popup window with large tooltips, eg in the main menu."

	[resolution]
		definition = "tooltip_large"

		automatic_placement = "true"
		vertical_placement = "bottom"
		horizontal_placement = "center"

		# TODO tooltips in this window make little sense.
		# Have to think of a nice solution.
		[tooltip]
			id = "tooltip_large"
		[/tooltip]

		[helptip]
			id = "tooltip_large"
		[/helptip]

		[grid]

			[row]

				[column]

					[label]
						id = "label"
						definition = "default_large"

						wrap = "true"
					[/label]

				[/column]

			[/row]

		[/grid]

	[/resolution]

[/window]


#define __GUI_WINDOW_HEIGHT
(
	if(window_height = 0
		# Determine the wanted maximum height. #
		# The value should be high enough to avoid an #
		# unable to place exception. #
		, 100000
		# Determine the height to use. #
		, window_height)
)#enddef


#define __GUI_WINDOW_FUNCTIONS
def placement_method(m, w, s)
(
	if((w.y > s.y) or ((w.x * 2) > s.x)
		, 'V'
		, if(m.y >= w.y
			, if(m.x >= w.x, 'I', 'III')
			, if(m.x >= w.x, 'II', 'IV')
		)
	)
);

def set_x(m, w, s)
(
[
	switch(
		  placement_method(m, w, s)
		, 'I'   , m.x - w.x
		, 'II'	, m.x - w.x
		, 'III' , 5
		# extra offset to avoid being obscured by the mouse. #
		, 'IV'  , m.x + 15
		, 'V'   , 5
		, #default# m.x
		),
#
	debug_print('placement_method, mouse ', m),
	debug_print('window ', w),
	debug_print('screen ', s),
	debug_print('y margin', s.y - (m.y + w.y)),
	debug_print('result ', placement_method(m, w, s)),
#

][0]
);

def set_y(m, w, s)
(
	switch(
		  placement_method(m, w, s)
		, 'I'   , m.y - w.y
		, 'II'  , 5
		, 'III' , m.y - w.y
		, 'IV'  , 5
		, 'V'   , 5
		, #default# m.y - w.y
		)
);

def get_maximum_width(w, s)
(
	if(w.x = 0
		# The default width upon the initial run. #
		, 160
		, if(w.y <= s.y
			# If the window's height fits use that. #
			, w.x
			# Else use an increased width, which should reduce #
			# the required height. #
			, 2 * w.x))
);

def set_w(r, w, s)
(
	switch(
		  r
		, 'maximum', get_maximum_width(w, s)
		, 'size', w.x
		)
);

def reevaluate_best_size(w, s)
(
[
	w.y > s.y,
#
	debug_print('window ', w),
	debug_print('screen ', s)
#
][0]
);
#enddef

#define __GUI_WINDOW_X
(
	set_x(
			  loc(mouse_x, mouse_y)
			, loc(window_width, window_height)
			, loc(screen_width, screen_height))
)
#enddef

#define __GUI_WINDOW_Y
(
	set_y(
			  loc(mouse_x, mouse_y)
			, loc(window_width, window_height)
			, loc(screen_width, screen_height))
)#enddef

#define __GUI_WINDOW_WIDTH
(
	set_w(
			  size_request_mode
			, loc(window_width, window_height)
			, loc(screen_width, screen_height))
)#enddef

#define __GUI_WINDOW_REEVALUATE_BEST_SIZE
(
	reevaluate_best_size(
			  loc(window_width, window_height)
			, loc(screen_width, screen_height))
)#enddef

[window]
	id = "tooltip"
	description = "The tooltip popup window with large tooltips, eg in the main menu."

	[resolution]
		definition = "tooltip_large"

		automatic_placement = "false"

		functions = "{__GUI_WINDOW_FUNCTIONS}"

		x = "{__GUI_WINDOW_X}"
		y = "{__GUI_WINDOW_Y}"
		width = "{__GUI_WINDOW_WIDTH}"
		height = "{__GUI_WINDOW_HEIGHT}"
		reevaluate_best_size = "{__GUI_WINDOW_REEVALUATE_BEST_SIZE}"

		# TODO tooltips in this window make little sense.
		# Have to think of a nice solution.
		[tooltip]
			id = "tooltip"
		[/tooltip]

		[helptip]
			id = "tooltip"
		[/helptip]

		[grid]

			[row]

				[column]

					[label]
						id = "label"
						definition = "default"

						wrap = "true"
					[/label]

				[/column]

			[/row]

		[/grid]

	[/resolution]

[/window]

#undef __GUI_WINDOW_REEVALUATE_BEST_SIZE
#undef __GUI_WINDOW_HEIGHT
#undef __GUI_WINDOW_WIDTH
#undef __GUI_WINDOW_Y
#undef __GUI_WINDOW_X
#undef __GUI_WINDOW_FUNCTIONS
