/*
**	PickDisplayMode.c
**
**	User interface for selecting screen display modes
**
**	Copyright © 1990-1996 by Olaf `Olsen' Barthel
**		All Rights Reserved
*/

#ifndef _GLOBAL_H
#include "Global.h"
#endif

enum	{ GAD_USE=1000,GAD_CANCEL };

STATIC ULONG __asm
FilterModeID(REG(d0) ULONG ID,REG(a0) APTR UserData)
{
	struct DimensionInfo DimensionInfo;

	if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,ID))
	{
		if(DimensionInfo.MaxDepth >= (LONG)UserData && DimensionInfo.MaxDepth <= 8)
			return(TRUE);
	}

	return(FALSE);
}

	/* PickDisplayMode(struct Window *Parent,ULONG *CurrentMode,struct Configuration *Config):
	 *
	 *	Pick a screen display mode from a list. If the Config pointer is not NULL,
	 *	will filter display modes out which cannot be used for the `term' main
	 *	screen, given the current colour restrictions.
	 */

BOOL
PickDisplayMode(struct Window *Parent,ULONG *CurrentMode,struct Configuration *Config)
{
	BOOL Success = FALSE;
	LONG MinDepth;

	if(Config)
	{
		switch(Config -> ScreenConfig -> ColourMode)
		{
			case COLOUR_AMIGA:

				MinDepth = 2;
				break;

			case COLOUR_EIGHT:

				MinDepth = 3;
				break;

			case COLOUR_SIXTEEN:

				MinDepth = 4;
				break;

			case COLOUR_MONO:

				MinDepth = 1;
				break;
		}
	}

		// For poor Workbench 2.04 users...

	if(AslBase -> lib_Version < 38)
	{
		struct List	*ModeList;
		LONG		 Index;

		if(ModeList = BuildModeList(&Index,*CurrentMode,Config ? FilterModeID : NULL,(APTR)MinDepth))
		{
			struct LayoutHandle *Handle;

			if(Handle = LT_CreateHandleTags(Parent -> WScreen,
				LH_LocaleHook,	&LocaleHook,
			TAG_DONE))
			{
				struct Window *LocalWindow;

				LT_New(Handle,
					LA_Type,VERTICAL_KIND,
				TAG_DONE);
				{
					LT_New(Handle,
						LA_Type,VERTICAL_KIND,
					TAG_DONE);
					{
						LONG MaxWidth = -1,MaxHeight = 0,Len;
						struct Node *Node;

						for(Node = ModeList -> lh_Head ; Node -> ln_Succ ; Node = Node -> ln_Succ)
						{
							Len = strlen(Node -> ln_Name);

							if(Len > MaxWidth)
								MaxWidth = Len;

							MaxHeight++;
						}

						MaxHeight++;

						LT_New(Handle,
							LA_Type,	LISTVIEW_KIND,
							LA_LabelID,	MSG_V36_0160,
							GTLV_Labels,	ModeList,
							LA_LONG,	&Index,
							LALV_CursorKey,	TRUE,
							LALV_Link,	NIL_LINK,
							LALV_MaxGrowY,	MaxHeight,
							LALV_ResizeY,	TRUE,
							LA_Lines,	MaxHeight > 10 ? 10 : MaxHeight,
							LA_Chars,	MaxWidth,
							LA_CursorKey,	TRUE,
						TAG_DONE);

						LT_EndGroup(Handle);
					}

					LT_New(Handle,
						LA_Type,VERTICAL_KIND,
					TAG_DONE);
					{
						LT_New(Handle,
							LA_Type,	XBAR_KIND,
							LAXB_FullSize,	TRUE,
						TAG_DONE);

						LT_EndGroup(Handle);
					}

					LT_New(Handle,LA_Type,HORIZONTAL_KIND,
						LAGR_SameSize,	TRUE,
						LAGR_Spread,	TRUE,
					TAG_DONE);
					{
						LT_New(Handle,
							LA_Type,	BUTTON_KIND,
							LA_LabelID,	MSG_GLOBAL_USE_GAD,
							LA_ID,		GAD_USE,
							LABT_ReturnKey,	TRUE,
							LABT_ExtraFat,	TRUE,
						TAG_DONE);

						LT_New(Handle,
							LA_Type,	BUTTON_KIND,
							LA_LabelID,	MSG_GLOBAL_CANCEL_GAD,
							LA_ID,		GAD_CANCEL,
							LABT_EscKey,	TRUE,
							LABT_ExtraFat,	TRUE,
						TAG_DONE);

						LT_EndGroup(Handle);
					}

					LT_EndGroup(Handle);
				}

				if(LocalWindow = LT_Build(Handle,
					LAWN_TitleID,		MSG_V36_0161,
					LAWN_IDCMP,		IDCMP_CLOSEWINDOW,
					LAWN_HelpHook,		&GuideHook,
					LAWN_Parent,		Parent,
					WA_DepthGadget,		TRUE,
					WA_CloseGadget,		TRUE,
					WA_DragBar,		TRUE,
					WA_RMBTrap,		TRUE,
					WA_Activate,		TRUE,
					WA_SimpleRefresh,	TRUE,
				TAG_DONE))
				{
					struct IntuiMessage	*Message;
					ULONG			 MsgClass;
					struct Gadget		*MsgGadget;
					LONG			 i;
					struct ModeNode		*ModeNode;
					BOOL			 Done = FALSE;

					do
					{
						WaitPort(LocalWindow -> UserPort);

						while(Message = (struct IntuiMessage *)LT_GetIMsg(Handle))
						{
							MsgClass	= Message -> Class;
							MsgGadget	= (struct Gadget *)Message -> IAddress;

							LT_ReplyIMsg(Message);

							if(MsgClass == IDCMP_CLOSEWINDOW)
								Done = TRUE;

							if(MsgClass == IDCMP_GADGETUP)
							{
								switch(MsgGadget -> GadgetID)
								{
									case GAD_USE:

										for(ModeNode = (struct ModeNode *)ModeList -> lh_Head, i = 0; ModeNode -> VanillaNode . ln_Succ ; ModeNode = (struct ModeNode *)ModeNode -> VanillaNode . ln_Succ, i++)
										{
											if(Index == i)
											{
												*CurrentMode = ModeNode -> DisplayID;

												break;
											}
										}

										Success = Done = TRUE;
										break;

									case GAD_CANCEL:

										Done = TRUE;
										break;
								}
							}

							if(MsgClass == IDCMP_IDCMPUPDATE)
							{
								for(ModeNode = (struct ModeNode *)ModeList -> lh_Head, i = 0; ModeNode -> VanillaNode . ln_Succ ; ModeNode = (struct ModeNode *)ModeNode -> VanillaNode . ln_Succ, i++)
								{
									if(Index == i)
									{
										*CurrentMode = ModeNode -> DisplayID;

										Success = Done = TRUE;

										LT_PressButton(Handle,GAD_USE);

										break;
									}
								}
							}
						}
					}
					while(!Done);

					if(Success && Config)
					{
						Config -> ScreenConfig -> Depth		= 0;
						Config -> ScreenConfig -> OverscanType	= OSCAN_TEXT;
						Config -> ScreenConfig -> DisplayWidth	= 0;
						Config -> ScreenConfig -> DisplayHeight	= 0;
					}
				}

				LT_DeleteHandle(Handle);
			}

			DeleteList(ModeList);
		}
	}
	else
	{
		LONG			DisplayWidth,
					DisplayHeight,
					Depth,
					OverscanType;
		struct Rectangle	DisplayClip;

		struct TagItem			 DimensionTags[5];
		struct ScreenModeRequester	*Request;

		if(Config)
		{
			Depth = Config -> ScreenConfig -> Depth;

			if(Depth < MinDepth)
				Depth = MinDepth;

			OverscanType = Config -> ScreenConfig -> OverscanType;

			if((!Config -> ScreenConfig -> DisplayWidth || !Config -> ScreenConfig -> DisplayHeight) && QueryOverscan(Config -> ScreenConfig -> DisplayMode,&DisplayClip,OverscanType))
			{
				DisplayWidth	= DisplayClip . MaxX - DisplayClip . MinX + 1;
				DisplayHeight	= DisplayClip . MaxY - DisplayClip . MinY + 1;
			}
			else
			{
				DisplayWidth	= Config -> ScreenConfig -> DisplayWidth;
				DisplayHeight	= Config -> ScreenConfig -> DisplayHeight;
			}
		}
		else
			DisplayWidth = DisplayHeight = 0;

		if(Request = (struct ScreenModeRequester *)AllocAslRequestTags(ASL_ScreenModeRequest,
			ASLSM_Window,			Parent,
			ASLSM_InitialDisplayID,		*CurrentMode,
			ASLSM_PrivateIDCMP,		TRUE,

			Config ? ASLSM_InitialDisplayDepth		: TAG_IGNORE,	Depth,
			Config ? ASLSM_MinDepth				: TAG_IGNORE,	MinDepth,
			Config ? ASLSM_MaxDepth				: TAG_IGNORE,	8,
			Config ? ASLSM_DoDepth				: TAG_IGNORE,	TRUE,
			Config ? ASLSM_DoWidth				: TAG_IGNORE,	TRUE,
			Config ? ASLSM_DoHeight				: TAG_IGNORE,	TRUE,
			Config ? ASLSM_DoOverscanType			: TAG_IGNORE,	TRUE,
			Config ? ASLSM_InitialOverscanType		: TAG_IGNORE,	OverscanType,

			DisplayWidth  ? ASLSM_InitialDisplayWidth	: TAG_IGNORE,	DisplayWidth,
			DisplayHeight ? ASLSM_InitialDisplayHeight	: TAG_IGNORE,	DisplayHeight,
		TAG_MORE,GetDimensionTags(Parent,DimensionTags)))
		{
			if(AslRequest(Request,NULL))
			{
				PutDimensionTags(Parent,Request -> sm_LeftEdge,Request -> sm_TopEdge,Request -> sm_Width,Request -> sm_Height);

				*CurrentMode = Request -> sm_DisplayID;

				if(Config)
				{
					if(Request -> sm_DisplayDepth == MinDepth)
						Depth = 0;
					else
						Depth = Request -> sm_DisplayDepth;

					Config -> ScreenConfig -> Depth		= Depth;
					Config -> ScreenConfig -> OverscanType	= Request -> sm_OverscanType;
					Config -> ScreenConfig -> DisplayWidth	= Request -> sm_DisplayWidth;
					Config -> ScreenConfig -> DisplayHeight	= Request -> sm_DisplayHeight;
				}

				Success = TRUE;
			}

			FreeAslRequest(Request);
		}
	}

	return(Success);
}
