#textdomain wesnoth-lib
###
### The in game portrait dialogs.
### Due to the excessive amount of dialogs it's not possible to split off the 
### window definitions. So this unit is a bit larger as wanted.
###

# These might become global have to evaluate later.
#define _GUI_HEIGHT_OFFSET
	25#enddef

# helper convert the scale factor, since we use it in two places define it as
# helper value. (Two the not working and working version, but want to avoid
# surprises when the not working version is fixed).
#define __GUI_SIZE_FACTOR
	3#enddef

# This is the function I want to use for the image width, but the formula system
# has no way to create variables (outside the AI scope). So instead of this
# macro there's a more verbose version which doesn't use variable.

#define __GUI_IMAGE_WIDTH
([
	max_height = screen_height - {_GUI_HEIGHT_OFFSET},
	best_width = gamemap_width / {__GUI_SIZE_FACTOR},
	best_width = if(best_width > max_height, max_height, best_width),
	if(best_width < 250, 250,
		if(best_width > 500, 500, best_width))
][4])#enddef

#undef __GUI_IMAGE_WIDTH

###
### The verbose version
###

#define __GUI_MAX_HEIGHT
(screen_height - {_GUI_HEIGHT_OFFSET})
#enddef

#define BEST_WIDTH
(gamemap_width / {__GUI_SIZE_FACTOR})
#enddef

#define __GUI_BEST_SIZE
(if({BEST_WIDTH} > {__GUI_MAX_HEIGHT}, {__GUI_MAX_HEIGHT}, {BEST_WIDTH}))
#enddef

#define ___GUI_IMAGE_WIDTH
	if({__GUI_BEST_SIZE} < 250, 250,
		if({__GUI_BEST_SIZE} > 500, 500, {__GUI_BEST_SIZE})
	)
#enddef

# This is the version with debug info
#define __GUI_IMAGE_WIDTH
([[
	debug_print('screen_width ', screen_width),
	debug_print('screen_height ', screen_height),
	debug_print('__GUI_MAX_HEIGHT ', {__GUI_MAX_HEIGHT}),
	debug_print('BEST_WIDTH ', {BEST_WIDTH}),
	debug_print('__GUI_BEST_SIZE ', {__GUI_BEST_SIZE})
],
	{___GUI_IMAGE_WIDTH}
][1]
)
#enddef

#undef __GUI_IMAGE_WIDTH
# This is the version without debug info
#define __GUI_IMAGE_WIDTH
({___GUI_IMAGE_WIDTH})#enddef	


# This is the macro for the image width which is can be used as string
# parameter.
#define _GUI_IMAGE_WIDTH
"{__GUI_IMAGE_WIDTH}"#enddef

# the value the image needs to be scaled by, this version can be used as string
# parameter.
#define _GUI_IMAGE_SCALE_FACTOR
(
	{__GUI_IMAGE_WIDTH} / 500
)#enddef


# The formula to set the widthe of the real image width depending on the
# image real size
#define __GUI_IMAGE_DISPLAYED_WIDTH
(
	if((image_original_width < 300) and (image_original_height < 300), 
		image_original_width, 
		((image_original_width * {__GUI_IMAGE_WIDTH}) / 500))
)#enddef

# The formula to set the heighte of the real image height depending on the
# image real size
#define __GUI_IMAGE_DISPLAYED_HEIGHT
(
	if((image_original_width < 300) and (image_original_height < 300), 
		image_original_height, 
		((image_original_height * {__GUI_IMAGE_WIDTH}) / 500))
)#enddef

# The X location of the image to display, only tested on the left side.
#define __GUI_IMAGE_DISPLAYED_X X
(
	if(image_original_width > 100,
		{X},
		({__GUI_IMAGE_WIDTH} - image_width) / 2)

)#enddef

# The Y location of the image to display, only tested on the left side.
# Note since we don't know the exact height of the text we can't centre
# the image. Instead we use the centre of the reserved image space.
#
# TODO the Y can't be determined properly since we don't know the height
# of the panel. Disabled the centring for now.
#
#define __GUI_IMAGE_DISPLAYED_Y
(
	if(image_original_height > 0,
		(height - image_height),
		height - (({__GUI_IMAGE_WIDTH} - image_height) / 2))

)#enddef

[window_definition]
	id = "wml_message_left"
	description = "bar"

	[resolution]

		[background]

			[draw]

			[/draw]

		[/background]

		[foreground]

			[draw]

				[image]
					x = "{__GUI_IMAGE_DISPLAYED_X X}"
					y = "{__GUI_IMAGE_DISPLAYED_Y}"
					w = "{__GUI_IMAGE_DISPLAYED_WIDTH}"
					h = "{__GUI_IMAGE_DISPLAYED_HEIGHT}"
					name = "(portrait_image)"
					vertical_mirror = "(portrait_mirror)"
				[/image]

			[/draw]

		[/foreground]

	[/resolution]

[/window_definition]

[window_definition]
	id = "wml_message_right"
	description = "bar"

	[resolution]

		[background]

			[draw]

			[/draw]

		[/background]

		[foreground]

			[draw]

				[image]
					x = "(width - {__GUI_IMAGE_DISPLAYED_WIDTH})"
					y = "(height - image_height)"
					w = "{__GUI_IMAGE_DISPLAYED_WIDTH}"
					h = "{__GUI_IMAGE_DISPLAYED_HEIGHT}"
					name = "(portrait_image)"
					vertical_mirror = "(portrait_mirror)"
				[/image]

			[/draw]

		[/foreground]

	[/resolution]

[/window_definition]

#define _GUI_INPUT_LIST
[listbox]
	id = "input_list"
	definition = "wml_message"

	[list_definition]

		[row]

			 [column]
				grow_factor = 1
				horizontal_grow = "true"

				[toggle_panel]
					definition = "wml_message"

					return_value_id = "ok"
					[grid]

						[row]

							[column]
								grow_factor = 0
								horizontal_alignment = "left"

								border = "all"
								border_size = 5

								[image]
									id = "icon"
									definition = "default"
								[/image]

							[/column]

							[column]
								grow_factor = 0
								horizontal_alignment = "left"

								border = "all"
								border_size = 5

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

							[/column]

							[column]
								grow_factor = 1
								horizontal_grow = "true"

								border = "all"
								border_size = 5

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

							[/column]

						[/row]

					[/grid]

				[/toggle_panel]

			[/column]

		[/row]

	[/list_definition]

[/listbox]
#enddef

[window]
	id = "wml_message_left"
	description = "Dialog showing a unit portrait on the left side"

	[resolution]
		window_width = 0
		window_height = 0

		definition = "wml_message_left"

		automatic_placement = "false"

		x = 0
		y = 30
		width = "(gamemap_width)"
		height = "(screen_height - 30)"

		easy_close = "true"
		
		[grid]

			[row]

				[column]
					vertical_alignment = "bottom"
					horizontal_grow = "true"

					[panel]
						definition = "wml_message"
							
						[grid]

							[row]

								[column]
									horizontal_grow = "true"

									[grid]

										[row]

											[column]

												[spacer]
													# reserve place for the image and set a minimum height for the text
													id = "image_place_holder"

													width = {_GUI_IMAGE_WIDTH}
													height = 75
												[/spacer]

											[/column]

											[column]
												grow_factor = 1
												horizontal_grow = "true"

												[grid]

													[row]
														
														[column]
															border = "all"
															border_size = 5
															horizontal_alignment = "left"

															[label]
																id = "title"
																definition = "title"
															[/label]

														[/column]

													[/row]

													[row]

														[column]
															grow_factor = 1
															horizontal_grow = "true"

															border = "all"
															border_size = 5

															[scroll_label] # NOTE maybe change the code to use message instead of label as id
																id = "label"
																definition = "wml_message"
															[/scroll_label]

														[/column]

													[/row]

													[row]

														[column]
															grow_factor = 1
															horizontal_grow = "true"

															[grid]

																[row]

																	[column]
																		border = "all"
																		border_size = 5

																		[label]
																			id = "input_caption"
																			definition = "default"
																		[/label]
																		
																	[/column]

																	[column]
																		grow_factor = 1
																		horizontal_grow = "true"

																		border = "all"
																		border_size = 5

																		[text_box]
																			id = "input"
																			definition = "default"
																		[/text_box]

																	[/column]

																[/row]

															[/grid]

														[/column]

													[/row]

													[row]

														[column]
															border = "all"
															border_size = 5
															horizontal_grow = "true"

															{_GUI_INPUT_LIST}


														[/column]

													[/row]

													[row]

														[column]
															border = "all"
															border_size = 5
															horizontal_alignment = "center"

															[button]
																# This button will be shown or hidden depending on the
																# whether or not a scrollbar is needed to show the
																# text.
																id = "ok"
																definition = "transparent"

																label = _ "OK"
															[/button]

														[/column]

													[/row]

												[/grid]

											[/column]

											[column]

												[spacer]
													width = "(
if(gamemap_width - ({__GUI_IMAGE_WIDTH}) > 675
, gamemap_width - (({__GUI_IMAGE_WIDTH}) + 675)
, 0
))"
													height = 75
												[/spacer]

											[/column]

										[/row]

									[/grid]

								[/column]

							[/row]

						[/grid]

					[/panel]

				[/column]

			[/row]

		[/grid]

	[/resolution]

[/window]

[window]
	id = "wml_message_right"
	description = "Dialog showing a unit portrait on the right side"

	[resolution]
		window_width = 0
		window_height = 0

		definition = "wml_message_right"

		automatic_placement = "false"

		x = 0
		y = 30
		width = "(gamemap_width)"
		height = "(screen_height - 30)"

		easy_close = "true"
		
		[grid]

			[row]

				[column]
					vertical_alignment = "bottom"
					horizontal_grow = "true"

					[panel]
						definition = "wml_message"
							
						[grid]

							[row]

								[column]
									horizontal_grow = "true"

									[grid]

										[row]

											[column]

												[spacer]
													width = 10
													height = 75
												[/spacer]

											[/column]

											[column]
												grow_factor = 1
												horizontal_grow = "true"

												[grid]

													[row]
														
														[column]
															border = "all"
															border_size = 5
															horizontal_alignment = "left"

															[label]
																id = "title"
																definition = "title"
															[/label]

														[/column]

													[/row]

													[row]

														[column]
															grow_factor = 1
															horizontal_grow = "true"

															border = "all"
															border_size = 5

															[scroll_label] # NOTE maybe change the code to use message instead of label as id
																id = "label"
																definition = "default"
															[/scroll_label]

														[/column]

													[/row]

													[row]

														[column]
															grow_factor = 1
															horizontal_grow = "true"

															[grid]

																[row]

																	[column]
																		border = "all"
																		border_size = 5

																		[label]
																			id = "input_caption"
																			definition = "default"
																		[/label]
																		
																	[/column]

																	[column]
																		grow_factor = 1
																		horizontal_grow = "true"

																		border = "all"
																		border_size = 5

																		[text_box]
																			id = "input"
																			definition = "default"
																		[/text_box]

																	[/column]

																[/row]

															[/grid]

														[/column]

													[/row]

													[row]

														[column]
															border = "all"
															border_size = 5
															horizontal_grow = "true"

															{_GUI_INPUT_LIST}

														[/column]

													[/row]

													[row]

														[column]
															border = "all"
															border_size = 5
															horizontal_alignment = "center"

															[button]
																# This button will be shown or hidden depending on the
																# whether or not a scrollbar is needed to show the
																# text.
																id = "ok"
																definition = "transparent"

																label = _ "OK"
															[/button]

														[/column]

													[/row]

												[/grid]

											[/column]

											[column]

												[spacer]
													width = "(
if(gamemap_width - (10 + ({__GUI_IMAGE_WIDTH})) > 675
, gamemap_width - (10 + ({__GUI_IMAGE_WIDTH}) + 675)
, 0
))"
													height = 75
												[/spacer]

											[/column]

											[column]

												[spacer]
													# reserve place for the image and set a minimum height for the text
													id = "image_place_holder"

													width = {_GUI_IMAGE_WIDTH}
													height = 75
												[/spacer]

											[/column]


										[/row]

									[/grid]

								[/column]

							[/row]

						[/grid]

					[/panel]

				[/column]

			[/row]

		[/grid]

	[/resolution]

[/window]

#undef _GUI_INPUT_LIST

#undef __GUI_IMAGE_DISPLAYED_Y
#undef __GUI_IMAGE_DISPLAYED_X
#undef __GUI_IMAGE_DISPLAYED_HEIGHT
#undef __GUI_IMAGE_DISPLAYED_WIDTH
#undef _GUI_IMAGE_SCALE_FACTOR
#undef _GUI_IMAGE_WIDTH
#undef __GUI_IMAGE_WIDTH
#undef ___GUI_IMAGE_WIDTH
#undef __GUI_BEST_SIZE
#undef __GUI_MAX_HEIGHT
#undef __GUI_SIZE_FACTOR
#undef _GUI_HEIGHT_OFFSET

